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