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