nd6_nbr.c revision 1.72 1 /* $NetBSD: nd6_nbr.c,v 1.72 2007/03/15 23:39:51 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.72 2007/03/15 23:39:51 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_in6 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, (struct route *)&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((struct route *)&ro);
528 return;
529
530 bad:
531 rtcache_free((struct route *)&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_in6 dst_sa;
863 struct in6_addr *src, daddr6;
864 int icmp6len, maxlen, error;
865 void *mac;
866 struct route_in6 ro;
867
868 mac = NULL;
869 memset(&ro, 0, sizeof(ro));
870
871 daddr6 = *daddr6_0; /* make a local copy for modification */
872
873 /* estimate the size of message */
874 maxlen = sizeof(*ip6) + sizeof(*nd_na);
875 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
876 #ifdef DIAGNOSTIC
877 if (max_linkhdr + maxlen >= MCLBYTES) {
878 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
879 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
880 panic("nd6_na_output: insufficient MCLBYTES");
881 /* NOTREACHED */
882 }
883 #endif
884
885 MGETHDR(m, M_DONTWAIT, MT_DATA);
886 if (m && max_linkhdr + maxlen >= MHLEN) {
887 MCLGET(m, M_DONTWAIT);
888 if ((m->m_flags & M_EXT) == 0) {
889 m_free(m);
890 m = NULL;
891 }
892 }
893 if (m == NULL)
894 return;
895 m->m_pkthdr.rcvif = NULL;
896
897 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
898 m->m_flags |= M_MCAST;
899 im6o.im6o_multicast_ifp = ifp;
900 im6o.im6o_multicast_hlim = 255;
901 im6o.im6o_multicast_loop = 0;
902 }
903
904 icmp6len = sizeof(*nd_na);
905 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
906 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
907
908 /* fill neighbor advertisement packet */
909 ip6 = mtod(m, struct ip6_hdr *);
910 ip6->ip6_flow = 0;
911 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
912 ip6->ip6_vfc |= IPV6_VERSION;
913 ip6->ip6_nxt = IPPROTO_ICMPV6;
914 ip6->ip6_hlim = 255;
915 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
916 /* reply to DAD */
917 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
918 daddr6.s6_addr16[1] = 0;
919 daddr6.s6_addr32[1] = 0;
920 daddr6.s6_addr32[2] = 0;
921 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
922 if (in6_setscope(&daddr6, ifp, NULL))
923 goto bad;
924
925 flags &= ~ND_NA_FLAG_SOLICITED;
926 }
927 ip6->ip6_dst = daddr6;
928 memset(&dst_sa, 0, sizeof(dst_sa));
929 dst_sa.sin6_family = AF_INET6;
930 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
931 dst_sa.sin6_addr = daddr6;
932
933 /*
934 * Select a source whose scope is the same as that of the dest.
935 */
936 ro.ro_dst = dst_sa;
937 src = in6_selectsrc(&dst_sa, NULL, NULL, (struct route *)&ro, NULL,
938 NULL, &error);
939 if (src == NULL) {
940 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
941 "determined: dst=%s, error=%d\n",
942 ip6_sprintf(&dst_sa.sin6_addr), error));
943 goto bad;
944 }
945 ip6->ip6_src = *src;
946 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
947 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
948 nd_na->nd_na_code = 0;
949 nd_na->nd_na_target = *taddr6;
950 in6_clearscope(&nd_na->nd_na_target); /* XXX */
951
952 /*
953 * "tlladdr" indicates NS's condition for adding tlladdr or not.
954 * see nd6_ns_input() for details.
955 * Basically, if NS packet is sent to unicast/anycast addr,
956 * target lladdr option SHOULD NOT be included.
957 */
958 if (tlladdr) {
959 /*
960 * sdl0 != NULL indicates proxy NA. If we do proxy, use
961 * lladdr in sdl0. If we are not proxying (sending NA for
962 * my address) use lladdr configured for the interface.
963 */
964 if (sdl0 == NULL)
965 mac = nd6_ifptomac(ifp);
966 else if (sdl0->sa_family == AF_LINK) {
967 struct sockaddr_dl *sdl;
968 sdl = (struct sockaddr_dl *)sdl0;
969 if (sdl->sdl_alen == ifp->if_addrlen)
970 mac = LLADDR(sdl);
971 }
972 }
973 if (tlladdr && mac) {
974 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
975 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
976
977 /* roundup to 8 bytes alignment! */
978 optlen = (optlen + 7) & ~7;
979
980 m->m_pkthdr.len += optlen;
981 m->m_len += optlen;
982 icmp6len += optlen;
983 bzero((void *)nd_opt, optlen);
984 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
985 nd_opt->nd_opt_len = optlen >> 3;
986 bcopy(mac, (void *)(nd_opt + 1), ifp->if_addrlen);
987 } else
988 flags &= ~ND_NA_FLAG_OVERRIDE;
989
990 ip6->ip6_plen = htons((u_int16_t)icmp6len);
991 nd_na->nd_na_flags_reserved = flags;
992 nd_na->nd_na_cksum = 0;
993 nd_na->nd_na_cksum =
994 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
995
996 ip6_output(m, NULL, NULL, 0, &im6o, (struct socket *)NULL, NULL);
997
998 icmp6_ifstat_inc(ifp, ifs6_out_msg);
999 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1000 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1001
1002 rtcache_free((struct route *)&ro);
1003 return;
1004
1005 bad:
1006 rtcache_free((struct route *)&ro);
1007 m_freem(m);
1008 return;
1009 }
1010
1011 void *
1012 nd6_ifptomac(struct ifnet *ifp)
1013 {
1014 switch (ifp->if_type) {
1015 case IFT_ARCNET:
1016 case IFT_ETHER:
1017 case IFT_FDDI:
1018 case IFT_IEEE1394:
1019 case IFT_PROPVIRTUAL:
1020 case IFT_CARP:
1021 case IFT_L2VLAN:
1022 case IFT_IEEE80211:
1023 return LLADDR(ifp->if_sadl);
1024 default:
1025 return NULL;
1026 }
1027 }
1028
1029 TAILQ_HEAD(dadq_head, dadq);
1030 struct dadq {
1031 TAILQ_ENTRY(dadq) dad_list;
1032 struct ifaddr *dad_ifa;
1033 int dad_count; /* max NS to send */
1034 int dad_ns_tcount; /* # of trials to send NS */
1035 int dad_ns_ocount; /* NS sent so far */
1036 int dad_ns_icount;
1037 int dad_na_icount;
1038 struct callout dad_timer_ch;
1039 };
1040
1041 static struct dadq_head dadq;
1042 static int dad_init = 0;
1043
1044 static struct dadq *
1045 nd6_dad_find(struct ifaddr *ifa)
1046 {
1047 struct dadq *dp;
1048
1049 TAILQ_FOREACH(dp, &dadq, dad_list) {
1050 if (dp->dad_ifa == ifa)
1051 return dp;
1052 }
1053 return NULL;
1054 }
1055
1056 static void
1057 nd6_dad_starttimer(struct dadq *dp, int ticks)
1058 {
1059
1060 callout_reset(&dp->dad_timer_ch, ticks,
1061 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1062 }
1063
1064 static void
1065 nd6_dad_stoptimer(dp)
1066 struct dadq *dp;
1067 {
1068
1069 callout_stop(&dp->dad_timer_ch);
1070 }
1071
1072 /*
1073 * Start Duplicate Address Detection (DAD) for specified interface address.
1074 *
1075 * xtick: minimum delay ticks for IFF_UP event
1076 */
1077 void
1078 nd6_dad_start(struct ifaddr *ifa, int xtick)
1079 {
1080 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1081 struct dadq *dp;
1082
1083 if (!dad_init) {
1084 TAILQ_INIT(&dadq);
1085 dad_init++;
1086 }
1087
1088 /*
1089 * If we don't need DAD, don't do it.
1090 * There are several cases:
1091 * - DAD is disabled (ip6_dad_count == 0)
1092 * - the interface address is anycast
1093 */
1094 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1095 log(LOG_DEBUG,
1096 "nd6_dad_start: called with non-tentative address "
1097 "%s(%s)\n",
1098 ip6_sprintf(&ia->ia_addr.sin6_addr),
1099 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1100 return;
1101 }
1102 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1103 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1104 return;
1105 }
1106 if (!ip6_dad_count) {
1107 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1108 return;
1109 }
1110 if (ifa->ifa_ifp == NULL)
1111 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1112 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1113 return;
1114 if (nd6_dad_find(ifa) != NULL) {
1115 /* DAD already in progress */
1116 return;
1117 }
1118
1119 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1120 if (dp == NULL) {
1121 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1122 "%s(%s)\n",
1123 ip6_sprintf(&ia->ia_addr.sin6_addr),
1124 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1125 return;
1126 }
1127 bzero(dp, sizeof(*dp));
1128 callout_init(&dp->dad_timer_ch);
1129 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1130
1131 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1132 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1133
1134 /*
1135 * Send NS packet for DAD, ip6_dad_count times.
1136 * Note that we must delay the first transmission, if this is the
1137 * first packet to be sent from the interface after interface
1138 * (re)initialization.
1139 */
1140 dp->dad_ifa = ifa;
1141 IFAREF(ifa); /* just for safety */
1142 dp->dad_count = ip6_dad_count;
1143 dp->dad_ns_icount = dp->dad_na_icount = 0;
1144 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1145 if (xtick == 0) {
1146 nd6_dad_ns_output(dp, ifa);
1147 nd6_dad_starttimer(dp,
1148 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1149 } else
1150 nd6_dad_starttimer(dp, xtick);
1151 }
1152
1153 /*
1154 * terminate DAD unconditionally. used for address removals.
1155 */
1156 void
1157 nd6_dad_stop(struct ifaddr *ifa)
1158 {
1159 struct dadq *dp;
1160
1161 if (!dad_init)
1162 return;
1163 dp = nd6_dad_find(ifa);
1164 if (dp == NULL) {
1165 /* DAD wasn't started yet */
1166 return;
1167 }
1168
1169 nd6_dad_stoptimer(dp);
1170
1171 TAILQ_REMOVE(&dadq, dp, dad_list);
1172 free(dp, M_IP6NDP);
1173 dp = NULL;
1174 IFAFREE(ifa);
1175 }
1176
1177 static void
1178 nd6_dad_timer(struct ifaddr *ifa)
1179 {
1180 int s;
1181 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1182 struct dadq *dp;
1183
1184 s = splsoftnet(); /* XXX */
1185
1186 /* Sanity check */
1187 if (ia == NULL) {
1188 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1189 goto done;
1190 }
1191 dp = nd6_dad_find(ifa);
1192 if (dp == NULL) {
1193 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1194 goto done;
1195 }
1196 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1197 log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
1198 "%s(%s)\n",
1199 ip6_sprintf(&ia->ia_addr.sin6_addr),
1200 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1201 goto done;
1202 }
1203 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1204 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1205 "%s(%s)\n",
1206 ip6_sprintf(&ia->ia_addr.sin6_addr),
1207 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1208 goto done;
1209 }
1210
1211 /* timeouted with IFF_{RUNNING,UP} check */
1212 if (dp->dad_ns_tcount > dad_maxtry) {
1213 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1214 if_name(ifa->ifa_ifp)));
1215
1216 TAILQ_REMOVE(&dadq, dp, dad_list);
1217 free(dp, M_IP6NDP);
1218 dp = NULL;
1219 IFAFREE(ifa);
1220 goto done;
1221 }
1222
1223 /* Need more checks? */
1224 if (dp->dad_ns_ocount < dp->dad_count) {
1225 /*
1226 * We have more NS to go. Send NS packet for DAD.
1227 */
1228 nd6_dad_ns_output(dp, ifa);
1229 nd6_dad_starttimer(dp,
1230 (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1231 } else {
1232 /*
1233 * We have transmitted sufficient number of DAD packets.
1234 * See what we've got.
1235 */
1236 int duplicate;
1237
1238 duplicate = 0;
1239
1240 if (dp->dad_na_icount) {
1241 /*
1242 * the check is in nd6_dad_na_input(),
1243 * but just in case
1244 */
1245 duplicate++;
1246 }
1247
1248 if (dp->dad_ns_icount) {
1249 /* We've seen NS, means DAD has failed. */
1250 duplicate++;
1251 }
1252
1253 if (duplicate) {
1254 /* (*dp) will be freed in nd6_dad_duplicated() */
1255 dp = NULL;
1256 nd6_dad_duplicated(ifa);
1257 } else {
1258 /*
1259 * We are done with DAD. No NA came, no NS came.
1260 * No duplicate address found.
1261 */
1262 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1263
1264 nd6log((LOG_DEBUG,
1265 "%s: DAD complete for %s - no duplicates found\n",
1266 if_name(ifa->ifa_ifp),
1267 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1268
1269 TAILQ_REMOVE(&dadq, dp, dad_list);
1270 free(dp, M_IP6NDP);
1271 dp = NULL;
1272 IFAFREE(ifa);
1273 }
1274 }
1275
1276 done:
1277 splx(s);
1278 }
1279
1280 void
1281 nd6_dad_duplicated(struct ifaddr *ifa)
1282 {
1283 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1284 struct ifnet *ifp;
1285 struct dadq *dp;
1286
1287 dp = nd6_dad_find(ifa);
1288 if (dp == NULL) {
1289 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1290 return;
1291 }
1292
1293 ifp = ifa->ifa_ifp;
1294 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1295 "NS in/out=%d/%d, NA in=%d\n",
1296 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1297 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1298
1299 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1300 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1301
1302 /* We are done with DAD, with duplicated address found. (failure) */
1303 nd6_dad_stoptimer(dp);
1304
1305 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1306 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1307 log(LOG_ERR, "%s: manual intervention required\n",
1308 if_name(ifp));
1309
1310 /*
1311 * If the address is a link-local address formed from an interface
1312 * identifier based on the hardware address which is supposed to be
1313 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1314 * operation on the interface SHOULD be disabled.
1315 * [rfc2462bis-03 Section 5.4.5]
1316 */
1317 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1318 struct in6_addr in6;
1319
1320 /*
1321 * To avoid over-reaction, we only apply this logic when we are
1322 * very sure that hardware addresses are supposed to be unique.
1323 */
1324 switch (ifp->if_type) {
1325 case IFT_ETHER:
1326 case IFT_FDDI:
1327 case IFT_ATM:
1328 case IFT_IEEE1394:
1329 #ifdef IFT_IEEE80211
1330 case IFT_IEEE80211:
1331 #endif
1332 in6 = ia->ia_addr.sin6_addr;
1333 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1334 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1335 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1336 log(LOG_ERR, "%s: possible hardware address "
1337 "duplication detected, disable IPv6\n",
1338 if_name(ifp));
1339 }
1340 break;
1341 }
1342 }
1343
1344 TAILQ_REMOVE(&dadq, dp, dad_list);
1345 free(dp, M_IP6NDP);
1346 dp = NULL;
1347 IFAFREE(ifa);
1348 }
1349
1350 static void
1351 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1352 {
1353 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1354 struct ifnet *ifp = ifa->ifa_ifp;
1355
1356 dp->dad_ns_tcount++;
1357 if ((ifp->if_flags & IFF_UP) == 0) {
1358 #if 0
1359 printf("%s: interface down?\n", if_name(ifp));
1360 #endif
1361 return;
1362 }
1363 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1364 #if 0
1365 printf("%s: interface not running?\n", if_name(ifp));
1366 #endif
1367 return;
1368 }
1369
1370 dp->dad_ns_tcount = 0;
1371 dp->dad_ns_ocount++;
1372 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1373 }
1374
1375 static void
1376 nd6_dad_ns_input(struct ifaddr *ifa)
1377 {
1378 struct in6_ifaddr *ia;
1379 const struct in6_addr *taddr6;
1380 struct dadq *dp;
1381 int duplicate;
1382
1383 if (ifa == NULL)
1384 panic("ifa == NULL in nd6_dad_ns_input");
1385
1386 ia = (struct in6_ifaddr *)ifa;
1387 taddr6 = &ia->ia_addr.sin6_addr;
1388 duplicate = 0;
1389 dp = nd6_dad_find(ifa);
1390
1391 /* Quickhack - completely ignore DAD NS packets */
1392 if (dad_ignore_ns) {
1393 nd6log((LOG_INFO,
1394 "nd6_dad_ns_input: ignoring DAD NS packet for "
1395 "address %s(%s)\n", ip6_sprintf(taddr6),
1396 if_name(ifa->ifa_ifp)));
1397 return;
1398 }
1399
1400 /*
1401 * if I'm yet to start DAD, someone else started using this address
1402 * first. I have a duplicate and you win.
1403 */
1404 if (dp == NULL || dp->dad_ns_ocount == 0)
1405 duplicate++;
1406
1407 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1408
1409 if (duplicate) {
1410 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1411 nd6_dad_duplicated(ifa);
1412 } else {
1413 /*
1414 * not sure if I got a duplicate.
1415 * increment ns count and see what happens.
1416 */
1417 if (dp)
1418 dp->dad_ns_icount++;
1419 }
1420 }
1421
1422 static void
1423 nd6_dad_na_input(struct ifaddr *ifa)
1424 {
1425 struct dadq *dp;
1426
1427 if (ifa == NULL)
1428 panic("ifa == NULL in nd6_dad_na_input");
1429
1430 dp = nd6_dad_find(ifa);
1431 if (dp)
1432 dp->dad_na_icount++;
1433
1434 /* remove the address. */
1435 nd6_dad_duplicated(ifa);
1436 }
1437