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