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