network - Fix socket & mbuf leak
[dragonfly.git] / sys / netinet6 / nd6_nbr.c
blob2c4a8e301bfff1ba826108d6e9966e85e40cfdf5
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.24 2008/10/03 07:59:20 hasso 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;
152 } else {
154 * Make sure the source address is from a neighbor's address.
156 if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
157 nd6log((LOG_INFO, "nd6_ns_input: "
158 "NS packet from non-neighbor\n"));
159 goto bad;
163 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
164 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
165 goto bad;
168 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
169 taddr6.s6_addr16[1] = htons(ifp->if_index);
171 icmp6len -= sizeof(*nd_ns);
172 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
173 if (nd6_options(&ndopts) < 0) {
174 nd6log((LOG_INFO,
175 "nd6_ns_input: invalid ND option, ignored\n"));
176 /* nd6_options have incremented stats */
177 goto freeit;
180 if (ndopts.nd_opts_src_lladdr) {
181 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
182 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
185 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
186 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
187 "(link-layer address option)\n"));
188 goto bad;
192 * Attaching target link-layer address to the NA?
193 * (RFC 2461 7.2.4)
195 * NS IP dst is unicast/anycast MUST NOT add
196 * NS IP dst is solicited-node multicast MUST add
198 * In implementation, we add target link-layer address by default.
199 * We do not add one in MUST NOT cases.
201 #if 0 /* too much! */
202 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
203 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
204 tlladdr = 0;
205 else
206 #endif
207 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
208 tlladdr = 0;
209 else
210 tlladdr = 1;
213 * Target address (taddr6) must be either:
214 * (1) Valid unicast/anycast address for my receiving interface,
215 * (2) Unicast address for which I'm offering proxy service, or
216 * (3) "tentative" address on which DAD is being performed.
218 /* (1) and (3) check. */
219 #ifdef CARP
220 if (ifp->if_carp)
221 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
222 if (!ifa)
223 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
224 #else
225 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
226 #endif
228 /* (2) check. */
229 if (!ifa) {
230 struct rtentry *rt;
231 struct sockaddr_in6 tsin6;
233 bzero(&tsin6, sizeof tsin6);
234 tsin6.sin6_len = sizeof(struct sockaddr_in6);
235 tsin6.sin6_family = AF_INET6;
236 tsin6.sin6_addr = taddr6;
238 rt = rtpurelookup((struct sockaddr *)&tsin6);
239 if (rt != NULL && (rt->rt_flags & RTF_ANNOUNCE) &&
240 rt->rt_gateway->sa_family == AF_LINK) {
242 * proxy NDP for single entry
244 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
245 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
246 if (ifa) {
247 proxy = 1;
248 proxydl = SDL(rt->rt_gateway);
251 if (rt != NULL)
252 --rt->rt_refcnt;
254 if (ifa == NULL) {
256 * We've got an NS packet, and we don't have that adddress
257 * assigned for us. We MUST silently ignore it.
258 * See RFC2461 7.2.3.
260 goto freeit;
262 myaddr6 = *IFA_IN6(ifa);
263 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
264 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
265 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
266 goto freeit;
268 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
269 nd6log((LOG_INFO,
270 "nd6_ns_input: lladdrlen mismatch for %s "
271 "(if %d, NS packet %d)\n",
272 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
273 goto bad;
276 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
277 nd6log((LOG_INFO,
278 "nd6_ns_input: duplicate IP6 address %s\n",
279 ip6_sprintf(&saddr6)));
280 goto freeit;
284 * We have neighbor solicitation packet, with target address equals to
285 * one of my tentative address.
287 * src addr how to process?
288 * --- ---
289 * multicast of course, invalid (rejected in ip6_input)
290 * unicast somebody is doing address resolution -> ignore
291 * unspec dup address detection
293 * The processing is defined in RFC 2462.
295 if (tentative) {
297 * If source address is unspecified address, it is for
298 * duplicated address detection.
300 * If not, the packet is for addess resolution;
301 * silently ignore it.
303 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
304 nd6_dad_ns_input(ifa);
306 goto freeit;
310 * If the source address is unspecified address, entries must not
311 * be created or updated.
312 * It looks that sender is performing DAD. Output NA toward
313 * all-node multicast address, to tell the sender that I'm using
314 * the address.
315 * S bit ("solicited") must be zero.
317 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
318 saddr6 = kin6addr_linklocal_allnodes;
319 saddr6.s6_addr16[1] = htons(ifp->if_index);
320 nd6_na_output(ifp, &saddr6, &taddr6,
321 ((anycast || proxy || !tlladdr)
322 ? 0 : ND_NA_FLAG_OVERRIDE)
323 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
324 tlladdr, (struct sockaddr *)proxydl);
325 goto freeit;
328 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
330 nd6_na_output(ifp, &saddr6, &taddr6,
331 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
332 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
333 | ND_NA_FLAG_SOLICITED,
334 tlladdr, (struct sockaddr *)proxydl);
335 freeit:
336 m_freem(m);
337 return;
339 bad:
340 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
341 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
342 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
343 icmp6stat.icp6s_badns++;
344 m_freem(m);
348 * Output an Neighbor Solicitation Message. Caller specifies:
349 * - ICMP6 header source IP6 address
350 * - ND6 header target IP6 address
351 * - ND6 header source datalink address
353 * Based on RFC 2461
354 * Based on RFC 2462 (duplicated address detection)
356 void
357 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
358 const struct in6_addr *taddr6,
359 struct llinfo_nd6 *ln, /* for source address determination */
360 int dad) /* duplicated address detection */
362 struct mbuf *m;
363 struct ip6_hdr *ip6;
364 struct nd_neighbor_solicit *nd_ns;
365 struct in6_ifaddr *ia = NULL;
366 struct ip6_moptions im6o;
367 int icmp6len;
368 int maxlen;
369 caddr_t mac;
370 struct ifnet *outif = NULL;
372 if (IN6_IS_ADDR_MULTICAST(taddr6))
373 return;
375 /* estimate the size of message */
376 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
377 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
378 if (max_linkhdr + maxlen >= MCLBYTES) {
379 #ifdef DIAGNOSTIC
380 kprintf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
381 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
382 #endif
383 return;
386 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
387 if (m == NULL)
388 return;
390 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
391 m->m_flags |= M_MCAST;
392 im6o.im6o_multicast_ifp = ifp;
393 im6o.im6o_multicast_hlim = 255;
394 im6o.im6o_multicast_loop = 0;
397 icmp6len = sizeof(*nd_ns);
398 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
399 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
401 /* fill neighbor solicitation packet */
402 ip6 = mtod(m, struct ip6_hdr *);
403 ip6->ip6_flow = 0;
404 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
405 ip6->ip6_vfc |= IPV6_VERSION;
406 /* ip6->ip6_plen will be set later */
407 ip6->ip6_nxt = IPPROTO_ICMPV6;
408 ip6->ip6_hlim = 255;
409 if (daddr6)
410 ip6->ip6_dst = *daddr6;
411 else {
412 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
413 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
414 ip6->ip6_dst.s6_addr32[1] = 0;
415 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
416 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
417 ip6->ip6_dst.s6_addr8[12] = 0xff;
419 if (!dad) {
420 #if 0 /* KAME way, exact address scope match */
422 * Select a source whose scope is the same as that of the dest.
423 * Typically, the dest is link-local solicitation multicast
424 * (i.e. neighbor discovery) or link-local/global unicast
425 * (i.e. neighbor un-reachability detection).
427 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
428 if (ia == NULL) {
429 m_freem(m);
430 return;
432 ip6->ip6_src = ia->ia_addr.sin6_addr;
433 #else /* spec-wise correct */
435 * RFC2461 7.2.2:
436 * "If the source address of the packet prompting the
437 * solicitation is the same as one of the addresses assigned
438 * to the outgoing interface, that address SHOULD be placed
439 * in the IP Source Address of the outgoing solicitation.
440 * Otherwise, any one of the addresses assigned to the
441 * interface should be used."
443 * We use the source address for the prompting packet
444 * (saddr6), if:
445 * - saddr6 is given from the caller (by giving "ln"), and
446 * - saddr6 belongs to the outgoing interface.
447 * Otherwise, we perform a scope-wise match.
449 struct ip6_hdr *hip6; /* hold ip6 */
450 struct in6_addr *saddr6;
452 if (ln && ln->ln_hold) {
453 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
454 /* XXX pullup? */
455 if (sizeof(*hip6) < ln->ln_hold->m_len)
456 saddr6 = &hip6->ip6_src;
457 else
458 saddr6 = NULL;
459 } else
460 saddr6 = NULL;
461 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
462 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
463 else {
464 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
465 if (ia == NULL) {
466 m_freem(m);
467 return;
469 ip6->ip6_src = ia->ia_addr.sin6_addr;
471 #endif
472 } else {
474 * Source address for DAD packet must always be IPv6
475 * unspecified address. (0::0)
477 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
479 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
480 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
481 nd_ns->nd_ns_code = 0;
482 nd_ns->nd_ns_reserved = 0;
483 nd_ns->nd_ns_target = *taddr6;
485 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
486 nd_ns->nd_ns_target.s6_addr16[1] = 0;
489 * Add source link-layer address option.
491 * spec implementation
492 * --- ---
493 * DAD packet MUST NOT do not add the option
494 * there's no link layer address:
495 * impossible do not add the option
496 * there's link layer address:
497 * Multicast NS MUST add one add the option
498 * Unicast NS SHOULD add one add the option
500 if (!dad && (mac = nd6_ifptomac(ifp))) {
501 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
502 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
503 /* 8 byte alignments... */
504 optlen = (optlen + 7) & ~7;
506 m->m_pkthdr.len += optlen;
507 m->m_len += optlen;
508 icmp6len += optlen;
509 bzero((caddr_t)nd_opt, optlen);
510 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
511 nd_opt->nd_opt_len = optlen >> 3;
512 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
515 ip6->ip6_plen = htons((u_short)icmp6len);
516 nd_ns->nd_ns_cksum = 0;
517 nd_ns->nd_ns_cksum
518 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
520 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, NULL);
521 if (outif) {
522 icmp6_ifstat_inc(outif, ifs6_out_msg);
523 icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
525 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
529 * Neighbor advertisement input handling.
531 * Based on RFC 2461
532 * Based on RFC 2462 (duplicated address detection)
534 * the following items are not implemented yet:
535 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
536 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
538 void
539 nd6_na_input(struct mbuf *m, int off, int icmp6len)
541 struct ifnet *ifp = m->m_pkthdr.rcvif;
542 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
543 struct nd_neighbor_advert *nd_na;
544 struct in6_addr saddr6 = ip6->ip6_src;
545 struct in6_addr daddr6 = ip6->ip6_dst;
546 struct in6_addr taddr6;
547 int flags;
548 int is_router;
549 int is_solicited;
550 int is_override;
551 char *lladdr = NULL;
552 int lladdrlen = 0;
553 struct ifaddr *ifa;
554 struct llinfo_nd6 *ln;
555 struct rtentry *rt;
556 struct sockaddr_dl *sdl;
557 union nd_opts ndopts;
559 if (ip6->ip6_hlim != 255) {
560 nd6log((LOG_ERR,
561 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
562 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
563 ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
564 goto bad;
567 #ifndef PULLDOWN_TEST
568 IP6_EXTHDR_CHECK(m, off, icmp6len,);
569 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
570 #else
571 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
572 if (nd_na == NULL) {
573 icmp6stat.icp6s_tooshort++;
574 return;
576 #endif
577 taddr6 = nd_na->nd_na_target;
578 flags = nd_na->nd_na_flags_reserved;
579 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
580 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
581 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
583 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
584 taddr6.s6_addr16[1] = htons(ifp->if_index);
586 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
587 nd6log((LOG_ERR,
588 "nd6_na_input: invalid target address %s\n",
589 ip6_sprintf(&taddr6)));
590 goto bad;
592 if (IN6_IS_ADDR_MULTICAST(&daddr6))
593 if (is_solicited) {
594 nd6log((LOG_ERR,
595 "nd6_na_input: a solicited adv is multicasted\n"));
596 goto bad;
599 icmp6len -= sizeof(*nd_na);
600 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
601 if (nd6_options(&ndopts) < 0) {
602 nd6log((LOG_INFO,
603 "nd6_na_input: invalid ND option, ignored\n"));
604 /* nd6_options have incremented stats */
605 goto freeit;
608 if (ndopts.nd_opts_tgt_lladdr) {
609 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
610 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
613 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
616 * Target address matches one of my interface address.
618 * If my address is tentative, this means that there's somebody
619 * already using the same address as mine. This indicates DAD failure.
620 * This is defined in RFC 2462.
622 * Otherwise, process as defined in RFC 2461.
624 if (ifa
625 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
626 nd6_dad_na_input(ifa);
627 goto freeit;
630 /* Just for safety, maybe unnecessary. */
631 if (ifa) {
632 log(LOG_ERR,
633 "nd6_na_input: duplicate IP6 address %s\n",
634 ip6_sprintf(&taddr6));
635 goto freeit;
639 * Make sure the source address is from a neighbor's address.
641 if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
642 nd6log((LOG_INFO, "nd6_na_input: "
643 "NA packet from non-neighbor\n"));
644 goto bad;
647 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
648 nd6log((LOG_INFO,
649 "nd6_na_input: lladdrlen mismatch for %s "
650 "(if %d, NA packet %d)\n",
651 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
652 goto bad;
656 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
658 rt = nd6_lookup(&taddr6, 0, ifp);
659 if ((rt == NULL) ||
660 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
661 ((sdl = SDL(rt->rt_gateway)) == NULL))
662 goto freeit;
664 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
666 * If the link-layer has address, and no lladdr option came,
667 * discard the packet.
669 if (ifp->if_addrlen && !lladdr)
670 goto freeit;
673 * Record link-layer address, and update the state.
675 sdl->sdl_alen = ifp->if_addrlen;
676 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
677 if (is_solicited) {
678 ln->ln_state = ND6_LLINFO_REACHABLE;
679 ln->ln_byhint = 0;
680 if (ln->ln_expire)
681 ln->ln_expire = time_second +
682 ND_IFINFO(rt->rt_ifp)->reachable;
683 } else {
684 ln->ln_state = ND6_LLINFO_STALE;
685 ln->ln_expire = time_second + nd6_gctimer;
687 if ((ln->ln_router = is_router) != 0) {
689 * This means a router's state has changed from
690 * non-reachable to probably reachable, and might
691 * affect the status of associated prefixes..
693 pfxlist_onlink_check();
695 } else {
696 int llchange;
699 * Check if the link-layer address has changed or not.
701 if (!lladdr)
702 llchange = 0;
703 else {
704 if (sdl->sdl_alen) {
705 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
706 llchange = 1;
707 else
708 llchange = 0;
709 } else
710 llchange = 1;
714 * This is VERY complex. Look at it with care.
716 * override solicit lladdr llchange action
717 * (L: record lladdr)
719 * 0 0 n -- (2c)
720 * 0 0 y n (2b) L
721 * 0 0 y y (1) REACHABLE->STALE
722 * 0 1 n -- (2c) *->REACHABLE
723 * 0 1 y n (2b) L *->REACHABLE
724 * 0 1 y y (1) REACHABLE->STALE
725 * 1 0 n -- (2a)
726 * 1 0 y n (2a) L
727 * 1 0 y y (2a) L *->STALE
728 * 1 1 n -- (2a) *->REACHABLE
729 * 1 1 y n (2a) L *->REACHABLE
730 * 1 1 y y (2a) L *->REACHABLE
732 if (!is_override && (lladdr && llchange)) { /* (1) */
734 * If state is REACHABLE, make it STALE.
735 * no other updates should be done.
737 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
738 ln->ln_state = ND6_LLINFO_STALE;
739 ln->ln_expire = time_second + nd6_gctimer;
741 goto freeit;
742 } else if (is_override /* (2a) */
743 || (!is_override && (lladdr && !llchange)) /* (2b) */
744 || !lladdr) { /* (2c) */
746 * Update link-local address, if any.
748 if (lladdr) {
749 sdl->sdl_alen = ifp->if_addrlen;
750 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
754 * If solicited, make the state REACHABLE.
755 * If not solicited and the link-layer address was
756 * changed, make it STALE.
758 if (is_solicited) {
759 ln->ln_state = ND6_LLINFO_REACHABLE;
760 ln->ln_byhint = 0;
761 if (ln->ln_expire) {
762 ln->ln_expire = time_second +
763 ND_IFINFO(ifp)->reachable;
765 } else {
766 if (lladdr && llchange) {
767 ln->ln_state = ND6_LLINFO_STALE;
768 ln->ln_expire = time_second + nd6_gctimer;
773 if (ln->ln_router && !is_router) {
775 * The peer dropped the router flag.
776 * Remove the sender from the Default Router List and
777 * update the Destination Cache entries.
779 struct nd_defrouter *dr;
780 struct in6_addr *in6;
782 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
785 * Lock to protect the default router list.
786 * XXX: this might be unnecessary, since this function
787 * is only called under the network software interrupt
788 * context. However, we keep it just for safety.
790 crit_enter();
791 dr = defrouter_lookup(in6, rt->rt_ifp);
792 if (dr)
793 defrtrlist_del(dr);
794 else if (!ip6_forwarding && ip6_accept_rtadv) {
796 * Even if the neighbor is not in the default
797 * router list, the neighbor may be used
798 * as a next hop for some destinations
799 * (e.g. redirect case). So we must
800 * call rt6_flush explicitly.
802 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
804 crit_exit();
806 ln->ln_router = is_router;
808 rt->rt_flags &= ~RTF_REJECT;
809 ln->ln_asked = 0;
810 if (ln->ln_hold) {
812 * we assume ifp is not a loopback here, so just set the 2nd
813 * argument as the 1st one.
815 nd6_output(ifp, ifp, ln->ln_hold,
816 (struct sockaddr_in6 *)rt_key(rt), rt);
817 ln->ln_hold = 0;
820 freeit:
821 m_freem(m);
822 return;
824 bad:
825 icmp6stat.icp6s_badna++;
826 m_freem(m);
830 * Neighbor advertisement output handling.
832 * Based on RFC 2461
834 * the following items are not implemented yet:
835 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
836 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
838 void
839 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
840 const struct in6_addr *taddr6, u_long flags,
841 int tlladdr, /* 1 if include target link-layer address */
842 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
844 struct mbuf *m;
845 struct ip6_hdr *ip6;
846 struct nd_neighbor_advert *nd_na;
847 struct in6_ifaddr *ia = NULL;
848 struct ip6_moptions im6o;
849 int icmp6len;
850 int maxlen;
851 caddr_t mac;
852 struct ifnet *outif = NULL;
854 /* estimate the size of message */
855 maxlen = sizeof(*ip6) + sizeof(*nd_na);
856 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
857 if (max_linkhdr + maxlen >= MCLBYTES) {
858 #ifdef DIAGNOSTIC
859 kprintf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
860 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
861 #endif
862 return;
865 m = m_getb(max_linkhdr + maxlen, MB_DONTWAIT, MT_DATA, M_PKTHDR);
866 if (m == NULL)
867 return;
869 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
870 m->m_flags |= M_MCAST;
871 im6o.im6o_multicast_ifp = ifp;
872 im6o.im6o_multicast_hlim = 255;
873 im6o.im6o_multicast_loop = 0;
876 icmp6len = sizeof(*nd_na);
877 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
878 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
880 /* fill neighbor advertisement packet */
881 ip6 = mtod(m, struct ip6_hdr *);
882 ip6->ip6_flow = 0;
883 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
884 ip6->ip6_vfc |= IPV6_VERSION;
885 ip6->ip6_nxt = IPPROTO_ICMPV6;
886 ip6->ip6_hlim = 255;
887 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
888 /* reply to DAD */
889 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
890 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
891 ip6->ip6_dst.s6_addr32[1] = 0;
892 ip6->ip6_dst.s6_addr32[2] = 0;
893 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
894 flags &= ~ND_NA_FLAG_SOLICITED;
895 } else
896 ip6->ip6_dst = *daddr6;
899 * Select a source whose scope is the same as that of the dest.
901 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
902 if (ia == NULL) {
903 m_freem(m);
904 return;
906 ip6->ip6_src = ia->ia_addr.sin6_addr;
907 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
908 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
909 nd_na->nd_na_code = 0;
910 nd_na->nd_na_target = *taddr6;
911 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
912 nd_na->nd_na_target.s6_addr16[1] = 0;
915 * "tlladdr" indicates NS's condition for adding tlladdr or not.
916 * see nd6_ns_input() for details.
917 * Basically, if NS packet is sent to unicast/anycast addr,
918 * target lladdr option SHOULD NOT be included.
920 mac = NULL;
921 if (tlladdr) {
923 * sdl0 != NULL indicates proxy NA. If we do proxy, use
924 * lladdr in sdl0. If we are not proxying (sending NA for
925 * my address) use lladdr configured for the interface.
927 if (sdl0 == NULL) {
928 #ifdef CARP
929 if (ifp->if_carp)
930 mac = carp_macmatch6(ifp->if_carp, m, taddr6);
931 if (mac == NULL)
932 mac = nd6_ifptomac(ifp);
933 #else
934 mac = nd6_ifptomac(ifp);
935 #endif
936 } else if (sdl0->sa_family == AF_LINK) {
937 struct sockaddr_dl *sdl;
938 sdl = (struct sockaddr_dl *)sdl0;
939 if (sdl->sdl_alen == ifp->if_addrlen)
940 mac = LLADDR(sdl);
943 if (mac != NULL) {
944 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
945 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
947 /* roundup to 8 bytes alignment! */
948 optlen = (optlen + 7) & ~7;
950 m->m_pkthdr.len += optlen;
951 m->m_len += optlen;
952 icmp6len += optlen;
953 bzero((caddr_t)nd_opt, optlen);
954 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
955 nd_opt->nd_opt_len = optlen >> 3;
956 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
957 } else
958 flags &= ~ND_NA_FLAG_OVERRIDE;
960 ip6->ip6_plen = htons((u_short)icmp6len);
961 nd_na->nd_na_flags_reserved = flags;
962 nd_na->nd_na_cksum = 0;
963 nd_na->nd_na_cksum =
964 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
966 ip6_output(m, NULL, NULL, 0, &im6o, &outif, NULL);
967 if (outif) {
968 icmp6_ifstat_inc(outif, ifs6_out_msg);
969 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
971 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
974 caddr_t
975 nd6_ifptomac(struct ifnet *ifp)
977 switch (ifp->if_type) {
978 case IFT_ETHER:
979 case IFT_IEEE1394:
980 #ifdef IFT_L2VLAN
981 case IFT_L2VLAN:
982 #endif
983 #ifdef IFT_IEEE80211
984 case IFT_IEEE80211:
985 #endif
986 #ifdef IFT_CARP
987 case IFT_CARP:
988 #endif
989 return ((caddr_t)(ifp + 1));
990 break;
991 default:
992 return NULL;
996 TAILQ_HEAD(dadq_head, dadq);
997 struct dadq {
998 TAILQ_ENTRY(dadq) dad_list;
999 struct ifaddr *dad_ifa;
1000 int dad_count; /* max NS to send */
1001 int dad_ns_tcount; /* # of trials to send NS */
1002 int dad_ns_ocount; /* NS sent so far */
1003 int dad_ns_icount;
1004 int dad_na_icount;
1005 struct callout dad_timer_ch;
1008 static struct dadq_head dadq;
1009 static int dad_init = 0;
1011 static struct dadq *
1012 nd6_dad_find(struct ifaddr *ifa)
1014 struct dadq *dp;
1016 TAILQ_FOREACH(dp, &dadq, dad_list) {
1017 if (dp->dad_ifa == ifa)
1018 return dp;
1020 return NULL;
1023 static void
1024 nd6_dad_starttimer(struct dadq *dp, int ticks)
1027 callout_reset(&dp->dad_timer_ch, ticks,
1028 (void (*) (void *))nd6_dad_timer, (void *)dp->dad_ifa);
1031 static void
1032 nd6_dad_stoptimer(struct dadq *dp)
1035 callout_stop(&dp->dad_timer_ch);
1039 * Start Duplicated Address Detection (DAD) for specified interface address.
1041 void
1042 nd6_dad_start(struct ifaddr *ifa,
1043 int *tick) /* minimum delay ticks for IFF_UP event */
1045 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1046 struct dadq *dp;
1048 if (!dad_init) {
1049 TAILQ_INIT(&dadq);
1050 dad_init++;
1054 * If we don't need DAD, don't do it.
1055 * There are several cases:
1056 * - DAD is disabled (ip6_dad_count == 0)
1057 * - the interface address is anycast
1059 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1060 log(LOG_DEBUG,
1061 "nd6_dad_start: called with non-tentative address "
1062 "%s(%s)\n",
1063 ip6_sprintf(&ia->ia_addr.sin6_addr),
1064 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1065 return;
1067 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1068 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1069 return;
1071 if (!ip6_dad_count) {
1072 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1073 return;
1075 if (!ifa->ifa_ifp)
1076 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1077 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1078 return;
1079 if (nd6_dad_find(ifa) != NULL) {
1080 /* DAD already in progress */
1081 return;
1084 dp = kmalloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1085 if (dp == NULL) {
1086 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1087 "%s(%s)\n",
1088 ip6_sprintf(&ia->ia_addr.sin6_addr),
1089 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1090 return;
1092 callout_init(&dp->dad_timer_ch);
1093 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1095 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1096 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1099 * Send NS packet for DAD, ip6_dad_count times.
1100 * Note that we must delay the first transmission, if this is the
1101 * first packet to be sent from the interface after interface
1102 * (re)initialization.
1104 dp->dad_ifa = ifa;
1105 crit_enter(); /* XXX MP not MP safe */
1106 _IFAREF(ifa, 0); /* just for safety */
1107 crit_exit();
1108 dp->dad_count = ip6_dad_count;
1109 dp->dad_ns_icount = dp->dad_na_icount = 0;
1110 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1111 if (tick == NULL) {
1112 nd6_dad_ns_output(dp, ifa);
1113 nd6_dad_starttimer(dp,
1114 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1115 } else {
1116 int ntick;
1118 if (*tick == 0)
1119 ntick = krandom() % (MAX_RTR_SOLICITATION_DELAY * hz);
1120 else
1121 ntick = *tick + krandom() % (hz / 2);
1122 *tick = ntick;
1123 nd6_dad_starttimer(dp, ntick);
1128 * terminate DAD unconditionally. used for address removals.
1130 void
1131 nd6_dad_stop(struct ifaddr *ifa)
1133 struct dadq *dp;
1135 if (!dad_init)
1136 return;
1137 dp = nd6_dad_find(ifa);
1138 if (!dp) {
1139 /* DAD wasn't started yet */
1140 return;
1143 nd6_dad_stoptimer(dp);
1145 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1146 kfree(dp, M_IP6NDP);
1147 dp = NULL;
1148 crit_enter(); /* XXX MP not MP safe */
1149 _IFAFREE(ifa, 0);
1150 crit_exit();
1153 static void
1154 nd6_dad_timer(struct ifaddr *ifa)
1156 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1157 struct dadq *dp;
1159 crit_enter(); /* XXX */
1161 /* Sanity check */
1162 if (ia == NULL) {
1163 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1164 goto done;
1166 dp = nd6_dad_find(ifa);
1167 if (dp == NULL) {
1168 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1169 goto done;
1171 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1172 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1173 "%s(%s)\n",
1174 ip6_sprintf(&ia->ia_addr.sin6_addr),
1175 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1176 goto done;
1178 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1179 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1180 "%s(%s)\n",
1181 ip6_sprintf(&ia->ia_addr.sin6_addr),
1182 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1183 goto done;
1186 /* timeouted with IFF_{RUNNING,UP} check */
1187 if (dp->dad_ns_tcount > dad_maxtry) {
1188 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1189 if_name(ifa->ifa_ifp)));
1191 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1192 kfree(dp, M_IP6NDP);
1193 dp = NULL;
1194 crit_enter(); /* XXX MP not MP safe */
1195 _IFAFREE(ifa, 0);
1196 crit_exit();
1197 goto done;
1200 /* Need more checks? */
1201 if (dp->dad_ns_ocount < dp->dad_count) {
1203 * We have more NS to go. Send NS packet for DAD.
1205 nd6_dad_ns_output(dp, ifa);
1206 nd6_dad_starttimer(dp,
1207 ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1208 } else {
1210 * We have transmitted sufficient number of DAD packets.
1211 * See what we've got.
1213 int duplicate;
1215 duplicate = 0;
1217 if (dp->dad_na_icount) {
1219 * the check is in nd6_dad_na_input(),
1220 * but just in case
1222 duplicate++;
1225 if (dp->dad_ns_icount) {
1226 #if 0 /* heuristics */
1228 * if
1229 * - we have sent many(?) DAD NS, and
1230 * - the number of NS we sent equals to the
1231 * number of NS we've got, and
1232 * - we've got no NA
1233 * we may have a faulty network card/driver which
1234 * loops back multicasts to myself.
1236 if (3 < dp->dad_count
1237 && dp->dad_ns_icount == dp->dad_count
1238 && dp->dad_na_icount == 0) {
1239 log(LOG_INFO, "DAD questionable for %s(%s): "
1240 "network card loops back multicast?\n",
1241 ip6_sprintf(&ia->ia_addr.sin6_addr),
1242 if_name(ifa->ifa_ifp));
1243 /* XXX consider it a duplicate or not? */
1244 /* duplicate++; */
1245 } else {
1246 /* We've seen NS, means DAD has failed. */
1247 duplicate++;
1249 #else
1250 /* We've seen NS, means DAD has failed. */
1251 duplicate++;
1252 #endif
1255 if (duplicate) {
1256 /* (*dp) will be freed in nd6_dad_duplicated() */
1257 dp = NULL;
1258 nd6_dad_duplicated(ifa);
1259 } else {
1261 * We are done with DAD. No NA came, no NS came.
1262 * duplicated address found.
1264 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1266 nd6log((LOG_DEBUG,
1267 "%s: DAD complete for %s - no duplicates found\n",
1268 if_name(ifa->ifa_ifp),
1269 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1271 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1272 kfree(dp, M_IP6NDP);
1273 dp = NULL;
1274 _IFAFREE(ifa, 0);
1278 done:
1279 crit_exit();
1282 void
1283 nd6_dad_duplicated(struct ifaddr *ifa)
1285 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1286 struct dadq *dp;
1288 dp = nd6_dad_find(ifa);
1289 if (dp == NULL) {
1290 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1291 return;
1294 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1295 "NS in/out=%d/%d, NA in=%d\n",
1296 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1297 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1299 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1300 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1302 /* We are done with DAD, with duplicated address found. (failure) */
1303 nd6_dad_stoptimer(dp);
1305 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1306 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1307 log(LOG_ERR, "%s: manual intervention required\n",
1308 if_name(ifa->ifa_ifp));
1310 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1311 kfree(dp, M_IP6NDP);
1312 dp = NULL;
1313 crit_enter(); /* XXX MP not MP safe */
1314 _IFAFREE(ifa, 0);
1315 crit_exit();
1318 static void
1319 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1321 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1322 struct ifnet *ifp = ifa->ifa_ifp;
1324 dp->dad_ns_tcount++;
1325 if (!(ifp->if_flags & IFF_UP)) {
1326 #if 0
1327 kprintf("%s: interface down?\n", if_name(ifp));
1328 #endif
1329 return;
1331 if (!(ifp->if_flags & IFF_RUNNING)) {
1332 #if 0
1333 kprintf("%s: interface not running?\n", if_name(ifp));
1334 #endif
1335 return;
1338 dp->dad_ns_ocount++;
1339 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1342 static void
1343 nd6_dad_ns_input(struct ifaddr *ifa)
1345 struct in6_ifaddr *ia;
1346 struct ifnet *ifp;
1347 const struct in6_addr *taddr6;
1348 struct dadq *dp;
1349 int duplicate;
1351 if (!ifa)
1352 panic("ifa == NULL in nd6_dad_ns_input");
1354 ia = (struct in6_ifaddr *)ifa;
1355 ifp = ifa->ifa_ifp;
1356 taddr6 = &ia->ia_addr.sin6_addr;
1357 duplicate = 0;
1358 dp = nd6_dad_find(ifa);
1360 /* Quickhack - completely ignore DAD NS packets */
1361 if (dad_ignore_ns) {
1362 nd6log((LOG_INFO,
1363 "nd6_dad_ns_input: ignoring DAD NS packet for "
1364 "address %s(%s)\n", ip6_sprintf(taddr6),
1365 if_name(ifa->ifa_ifp)));
1366 return;
1370 * if I'm yet to start DAD, someone else started using this address
1371 * first. I have a duplicate and you win.
1373 if (!dp || dp->dad_ns_ocount == 0)
1374 duplicate++;
1376 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1378 if (duplicate) {
1379 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1380 nd6_dad_duplicated(ifa);
1381 } else {
1383 * not sure if I got a duplicate.
1384 * increment ns count and see what happens.
1386 if (dp)
1387 dp->dad_ns_icount++;
1391 static void
1392 nd6_dad_na_input(struct ifaddr *ifa)
1394 struct dadq *dp;
1396 if (!ifa)
1397 panic("ifa == NULL in nd6_dad_na_input");
1399 dp = nd6_dad_find(ifa);
1400 if (dp)
1401 dp->dad_na_icount++;
1403 /* remove the address. */
1404 nd6_dad_duplicated(ifa);