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