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