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