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