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