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