nd6_nbr.c revision 1.6 1 /* $NetBSD: nd6_nbr.c,v 1.6 1999/07/10 19:46:10 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
33 #include "opt_inet.h"
34 #include "opt_ipsec.h"
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
47 #include <sys/ioctl.h>
48 #endif
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
51
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/if_dl.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/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/nd6.h>
63 #include <netinet6/icmp6.h>
64
65 #define SDL(s) ((struct sockaddr_dl *)s)
66
67 #if 0
68 extern struct timeval time;
69 #endif
70
71 struct dadq;
72 static struct dadq *nd6_dad_find __P((struct ifaddr *));
73 static void nd6_dad_timer __P((struct ifaddr *));
74 static void nd6_dad_ns_input __P((struct ifaddr *));
75 static void nd6_dad_na_input __P((struct ifaddr *));
76
77 /* ignore NS in DAD - specwise incorrect, */
78 int dad_ignore_ns = 0;
79
80 /*
81 * Input an Neighbor Solicitation Message.
82 *
83 * Based on RFC 2461
84 * Based on RFC 2462 (duplicated address detection)
85 *
86 * XXX proxy advertisement
87 */
88 void
89 nd6_ns_input(m, off, icmp6len)
90 struct mbuf *m;
91 int off, icmp6len;
92 {
93 struct ifnet *ifp = m->m_pkthdr.rcvif;
94 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
95 struct nd_neighbor_solicit *nd_ns
96 = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
97 struct in6_addr saddr6 = ip6->ip6_src;
98 struct in6_addr daddr6 = ip6->ip6_dst;
99 struct in6_addr taddr6 = nd_ns->nd_ns_target;
100 struct in6_addr myaddr6;
101 char *lladdr = NULL;
102 struct ifaddr *ifa;
103 int lladdrlen = 0;
104 int anycast = 0, proxy = 0, tentative = 0;
105 int tlladdr;
106 union nd_opts ndopts;
107
108 if (ip6->ip6_hlim != 255) {
109 log(LOG_ERR,
110 "nd6_ns_input: invalid hlim %d\n", ip6->ip6_hlim);
111 return;
112 }
113
114 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
115 /* dst has to be solicited node multicast address. */
116 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
117 /*don't check ifindex portion*/
118 && daddr6.s6_addr32[1] == 0
119 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
120 && daddr6.s6_addr8[12] == 0xff) {
121 ; /*good*/
122 } else {
123 log(LOG_INFO, "nd6_ns_input: bad DAD packet "
124 "(wrong ip6 dst)\n");
125 goto bad;
126 }
127 }
128
129 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
130 log(LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n");
131 goto bad;
132 }
133
134 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
135 taddr6.s6_addr16[1] = htons(ifp->if_index);
136
137 icmp6len -= sizeof(*nd_ns);
138 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
139 if (nd6_options(&ndopts) < 0) {
140 log(LOG_INFO, "nd6_ns_input: invalid ND option, ignored\n");
141 goto bad;
142 }
143
144 if (ndopts.nd_opts_src_lladdr) {
145 lladdr = (char *)(ndopts.nd_opts_src_lladdr +1);
146 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
147 }
148
149 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
150 log(LOG_INFO, "nd6_ns_input: bad DAD packet "
151 "(link-layer address option)\n");
152 goto bad;
153 }
154
155 /*
156 * Attaching target link-layer address to the NA?
157 * (RFC 2461 7.2.4)
158 *
159 * NS IP dst is unicast/anycast MUST NOT add
160 * NS IP dst is solicited-node multicast MUST add
161 *
162 * In implementation, we add target link-layer address by default.
163 * We do not add one in MUST NOT cases.
164 */
165 #if 0 /* too much! */
166 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
167 if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
168 tlladdr = 0;
169 else
170 #endif
171 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
172 tlladdr = 0;
173 else
174 tlladdr = 1;
175
176 /*
177 * Target address (taddr6) must be either:
178 * (1) Valid unicast/anycast address for my receiving interface,
179 * (2) Unicast address for which I'm offering proxy service, or
180 * (3) "tentative" address on which DAD is being performed.
181 */
182 /* (1) and (3) check. */
183 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
184
185 /* (2) check. */
186 if (!ifa && nd6_proxyall) {
187 struct rtentry *rt;
188 struct sockaddr_in6 tsin6;
189
190 bzero(&tsin6, sizeof tsin6);
191 tsin6.sin6_len = sizeof(struct sockaddr_in6);
192 tsin6.sin6_family = AF_INET6;
193 tsin6.sin6_addr = taddr6;
194
195 rt = rtalloc1((struct sockaddr *)&tsin6, 0
196 #ifdef __FreeBSD__
197 , 0
198 #endif /* __FreeBSD__ */
199 );
200 if (rt && rt->rt_ifp != ifp) {
201 /*
202 * search link local addr for ifp, and use it for
203 * proxy NA.
204 */
205 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
206 if (ifa)
207 proxy = 1;
208 }
209 rtfree(rt);
210 }
211 if (!ifa) {
212 /*
213 * We've got a NS packet, and we don't have that adddress
214 * assigned for us. We MUST silently ignore it.
215 * See RFC2461 7.2.3.
216 */
217 return;
218 }
219 myaddr6 = IFA_IN6(ifa);
220 anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
221 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
222 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
223 return;
224
225 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
226 log(LOG_INFO,
227 "nd6_ns_input: lladdrlen mismatch for %s "
228 "(if %d, NS packet %d)\n",
229 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
230 }
231
232 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
233 log(LOG_INFO,
234 "nd6_ns_input: duplicate IP6 address %s\n",
235 ip6_sprintf(&saddr6));
236 return;
237 }
238
239 /*
240 * We have neighbor solicitation packet, with target address equals to
241 * one of my tentative address.
242 *
243 * src addr how to process?
244 * --- ---
245 * multicast of course, invalid (rejected in ip6_input)
246 * unicast somebody is doing address resolution -> ignore
247 * unspec dup address detection
248 *
249 * The processing is defined in RFC 2462.
250 */
251 if (tentative) {
252 /*
253 * If source address is unspecified address, it is for
254 * duplicated address detection.
255 *
256 * If not, the packet is for addess resolution;
257 * silently ignore it.
258 */
259 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
260 nd6_dad_ns_input(ifa);
261
262 return;
263 }
264
265 /*
266 * If the source address is unspecified address, entries must not
267 * be created or updated.
268 * It looks that sender is performing DAD. Output NA toward
269 * all-node multicast address, to tell the sender that I'm using
270 * the address.
271 * S bit ("solicited") must be zero.
272 */
273 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
274 saddr6 = in6addr_linklocal_allnodes;
275 saddr6.s6_addr16[1] = htons(ifp->if_index);
276 nd6_na_output(ifp, &saddr6, &taddr6,
277 ((anycast || proxy || !tlladdr)
278 ? 0 : ND_NA_FLAG_OVERRIDE)
279 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
280 tlladdr);
281 return;
282 }
283
284 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT);
285
286 nd6_na_output(ifp, &saddr6, &taddr6,
287 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
288 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
289 | ND_NA_FLAG_SOLICITED,
290 tlladdr);
291 return;
292
293 bad:
294 log(LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
295 log(LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
296 log(LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
297 return;
298 }
299
300 /*
301 * Output an Neighbor Solicitation Message. Caller specifies:
302 * - ICMP6 header source IP6 address
303 * - ND6 header target IP6 address
304 * - ND6 header source datalink address
305 *
306 * Based on RFC 2461
307 * Based on RFC 2462 (duplicated address detection)
308 */
309 void
310 nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
311 struct ifnet *ifp;
312 struct in6_addr *daddr6, *taddr6;
313 struct llinfo_nd6 *ln; /* for source address determination */
314 int dad; /* duplicated address detection */
315 {
316 struct mbuf *m;
317 struct ip6_hdr *ip6;
318 struct nd_neighbor_solicit *nd_ns;
319 struct in6_ifaddr *ia = NULL;
320 struct ip6_moptions im6o;
321 int icmp6len;
322 caddr_t mac;
323
324 if (IN6_IS_ADDR_MULTICAST(taddr6))
325 return;
326
327 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
328 return;
329
330 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
331 m->m_flags |= M_MCAST;
332 im6o.im6o_multicast_ifp = ifp;
333 im6o.im6o_multicast_hlim = 255;
334 im6o.im6o_multicast_loop = 0;
335 }
336
337 icmp6len = sizeof(*nd_ns);
338 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
339 MH_ALIGN(m, m->m_len + 16); /* 1+1+6 is enought. but just in case */
340
341 /* fill neighbor solicitation packet */
342 ip6 = mtod(m, struct ip6_hdr *);
343 ip6->ip6_flow = 0;
344 ip6->ip6_vfc = IPV6_VERSION;
345 /* ip6->ip6_plen will be set later */
346 ip6->ip6_nxt = IPPROTO_ICMPV6;
347 ip6->ip6_hlim = 255;
348 if (daddr6)
349 ip6->ip6_dst = *daddr6;
350 else {
351 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
352 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
353 ip6->ip6_dst.s6_addr32[1] = 0;
354 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
355 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
356 ip6->ip6_dst.s6_addr8[12] = 0xff;
357 }
358 if (!dad) {
359 #if 0 /* KAME way, exact address scope match */
360 /*
361 * Select a source whose scope is the same as that of the dest.
362 * Typically, the dest is link-local solicitation multicast
363 * (i.e. neighbor discovery) or link-local/global unicast
364 * (i.e. neighbor un-reachability detection).
365 */
366 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
367 if (ia == NULL) {
368 m_freem(m);
369 return;
370 }
371 ip6->ip6_src = ia->ia_addr.sin6_addr;
372 #else /* spec-wise correct */
373 /*
374 * RFC2461 7.2.2:
375 * "If the source address of the packet prompting the
376 * solicitation is the same as one of the addresses assigned
377 * to the outgoing interface, that address SHOULD be placed
378 * in the IP Source Address of the outgoing solicitation.
379 * Otherwise, any one of the addresses assigned to the
380 * interface should be used."
381 *
382 * We use the source address for the prompting packet
383 * (saddr6), if:
384 * - saddr6 is given from the caller (by giving "ln"), and
385 * - saddr6 belongs to the outgoing interface.
386 * Otherwise, we perform a scope-wise match.
387 */
388 struct ip6_hdr *hip6; /*hold ip6*/
389 struct in6_addr *saddr6;
390
391 if (ln && ln->ln_hold) {
392 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
393 /* XXX pullup? */
394 if (sizeof(*hip6) < ln->ln_hold->m_len)
395 saddr6 = &hip6->ip6_src;
396 else
397 saddr6 = NULL;
398 } else
399 saddr6 = NULL;
400 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
401 bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
402 else {
403 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
404 if (ia == NULL) {
405 m_freem(m); /*XXX*/
406 return;
407 }
408 ip6->ip6_src = ia->ia_addr.sin6_addr;
409 }
410 #endif
411 } else {
412 /*
413 * Source address for DAD packet must always be IPv6
414 * unspecified address. (0::0)
415 */
416 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
417 }
418 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
419 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
420 nd_ns->nd_ns_code = 0;
421 nd_ns->nd_ns_reserved = 0;
422 nd_ns->nd_ns_target = *taddr6;
423
424 if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
425 nd_ns->nd_ns_target.s6_addr16[1] = 0;
426
427 /*
428 * Add source link-layer address option.
429 *
430 * spec implementation
431 * --- ---
432 * DAD packet MUST NOT do not add the option
433 * there's no link layer address:
434 * impossible do not add the option
435 * there's link layer address:
436 * Multicast NS MUST add one add the option
437 * Unicast NS SHOULD add one add the option
438 */
439 if (!dad && (mac = nd6_ifptomac(ifp))) {
440 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
441 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
442
443 m->m_pkthdr.len += optlen;
444 m->m_len += optlen;
445 icmp6len += optlen;
446 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
447 /* xxx 8 byte alignments? */
448 nd_opt->nd_opt_len = optlen >> 3;
449 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
450 }
451
452 ip6->ip6_plen = htons((u_short)icmp6len);
453 nd_ns->nd_ns_cksum = 0;
454 nd_ns->nd_ns_cksum
455 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
456
457 #ifdef IPSEC
458 m->m_pkthdr.rcvif = NULL;
459 #endif /*IPSEC*/
460 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o);
461 icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
462 }
463
464 /*
465 * Neighbor advertisement input handling.
466 *
467 * Based on RFC 2461
468 * Based on RFC 2462 (duplicated address detection)
469 */
470 void
471 nd6_na_input(m, off, icmp6len)
472 struct mbuf *m;
473 int off, icmp6len;
474 {
475 struct ifnet *ifp = m->m_pkthdr.rcvif;
476 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
477 struct nd_neighbor_advert *nd_na
478 = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
479 #if 0
480 struct in6_addr saddr6 = ip6->ip6_src;
481 #endif
482 struct in6_addr daddr6 = ip6->ip6_dst;
483 struct in6_addr taddr6 = nd_na->nd_na_target;
484 int flags = nd_na->nd_na_flags_reserved;
485 int is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
486 int is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
487 int is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
488 char *lladdr = NULL;
489 int lladdrlen = 0;
490 struct ifaddr *ifa;
491 struct llinfo_nd6 *ln;
492 struct rtentry *rt;
493 struct sockaddr_dl *sdl;
494 union nd_opts ndopts;
495
496 if (ip6->ip6_hlim != 255) {
497 log(LOG_ERR,
498 "nd6_na_input: invalid hlim %d\n", ip6->ip6_hlim);
499 return;
500 }
501
502 if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
503 taddr6.s6_addr16[1] = htons(ifp->if_index);
504
505 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
506 log(LOG_ERR,
507 "nd6_na_input: invalid target address %s\n",
508 ip6_sprintf(&taddr6));
509 return;
510 }
511 if (IN6_IS_ADDR_MULTICAST(&daddr6))
512 if (is_solicited) {
513 log(LOG_ERR,
514 "nd6_na_input: a solicited adv is multicasted\n");
515 return;
516 }
517
518 icmp6len -= sizeof(*nd_na);
519 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
520 if (nd6_options(&ndopts) < 0) {
521 log(LOG_INFO, "nd6_na_input: invalid ND option, ignored\n");
522 return;
523 }
524
525 if (ndopts.nd_opts_tgt_lladdr) {
526 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
527 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
528 }
529
530 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
531
532 /*
533 * Target address matches one of my interface address.
534 *
535 * If my address is tentative, this means that there's somebody
536 * already using the same address as mine. This indicates DAD failure.
537 * This is defined in RFC 2462.
538 *
539 * Otherwise, process as defined in RFC 2461.
540 */
541 if (ifa
542 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
543 nd6_dad_na_input(ifa);
544 return;
545 }
546
547 /* Just for safety, maybe unnecessery. */
548 if (ifa) {
549 log(LOG_ERR,
550 "nd6_na_input: duplicate IP6 address %s\n",
551 ip6_sprintf(&taddr6));
552 return;
553 }
554
555 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
556 log(LOG_INFO,
557 "nd6_na_input: lladdrlen mismatch for %s "
558 "(if %d, NA packet %d)\n",
559 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
560 }
561
562 /*
563 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
564 */
565 rt = nd6_lookup(&taddr6, 0, ifp);
566 if ((rt == NULL) ||
567 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
568 ((sdl = SDL(rt->rt_gateway)) == NULL))
569 return;
570
571 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
572 /*
573 * If the link-layer has address, and no lladdr option came,
574 * discard the packet.
575 */
576 if (ifp->if_addrlen && !lladdr)
577 return;
578
579 /*
580 * Record link-layer address, and update the state.
581 */
582 sdl->sdl_alen = ifp->if_addrlen;
583 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
584 if (is_solicited) {
585 ln->ln_state = ND6_LLINFO_REACHABLE;
586 if (ln->ln_expire)
587 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
588 ln->ln_expire = time.tv_sec +
589 #else
590 ln->ln_expire = time_second +
591 #endif
592 nd_ifinfo[rt->rt_ifp->if_index].reachable;
593 } else
594 ln->ln_state = ND6_LLINFO_STALE;
595 ln->ln_router = is_router;
596 } else {
597 int llchange;
598
599 /*
600 * Check if the link-layer address has changed or not.
601 */
602 if (!lladdr)
603 llchange = 0;
604 else {
605 if (sdl->sdl_alen) {
606 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
607 llchange = 1;
608 else
609 llchange = 0;
610 } else
611 llchange = 1;
612 }
613
614 /*
615 * This is VERY complex. Look at it with care.
616 *
617 * override solicit lladdr llchange action
618 * (L: record lladdr)
619 *
620 * 0 0 n -- (2c)
621 * 0 0 y n (2b) L
622 * 0 0 y y (1) REACHABLE->STALE
623 * 0 1 n -- (2c) *->REACHABLE
624 * 0 1 y n (2b) L *->REACHABLE
625 * 0 1 y y (1) REACHABLE->STALE
626 * 1 0 n -- (2a)
627 * 1 0 y n (2a) L
628 * 1 0 y y (2a) L *->STALE
629 * 1 1 n -- (2a) *->REACHABLE
630 * 1 1 y n (2a) L *->REACHABLE
631 * 1 1 y y (2a) L *->REACHABLE
632 */
633 if (!is_override && (lladdr && llchange)) { /* (1) */
634 /*
635 * If state is REACHABLE, make it STALE.
636 * no other updates should be done.
637 */
638 if (ln->ln_state == ND6_LLINFO_REACHABLE)
639 ln->ln_state = ND6_LLINFO_STALE;
640 return;
641 } else if (is_override /* (2a) */
642 || (!is_override && (lladdr && !llchange)) /* (2b) */
643 || !lladdr) { /* (2c) */
644 /*
645 * Update link-local address, if any.
646 */
647 if (lladdr) {
648 sdl->sdl_alen = ifp->if_addrlen;
649 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
650 }
651
652 /*
653 * If solicited, make the state REACHABLE.
654 * If not solicited and the link-layer address was
655 * changed, make it STALE.
656 */
657 if (is_solicited) {
658 ln->ln_state = ND6_LLINFO_REACHABLE;
659 if (ln->ln_expire) {
660 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
661 ln->ln_expire = time.tv_sec +
662 #else
663 ln->ln_expire = time_second +
664 #endif
665 nd_ifinfo[ifp->if_index].reachable;
666 }
667 } else {
668 if (lladdr && llchange)
669 ln->ln_state = ND6_LLINFO_STALE;
670 }
671 }
672
673 if (ln->ln_router && !is_router) {
674 /*
675 * The peer dropped the router flag.
676 * Remove the sender from the Default Router List and
677 * update the Destination Cache entries.
678 */
679 struct nd_defrouter *dr;
680 struct in6_addr *in6;
681 int s;
682
683 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
684 s = splsoftnet();
685 dr = defrouter_lookup(in6, rt->rt_ifp);
686 if (dr)
687 defrtrlist_del(dr);
688 else if (!ip6_forwarding && ip6_accept_rtadv) {
689 /*
690 * Even if the neighbor is not in the default
691 * router list, the neighbor may be used
692 * as a next hop for some destinations
693 * (e.g. redirect case). So we must
694 * call rt6_flush explicitly.
695 */
696 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
697 }
698 splx(s);
699 }
700 ln->ln_router = is_router;
701 }
702 rt->rt_flags &= ~RTF_REJECT;
703 ln->ln_asked = 0;
704 if (ln->ln_hold) {
705 (*ifp->if_output)(ifp, ln->ln_hold, rt_key(rt), rt);
706 ln->ln_hold = 0;
707 }
708 }
709
710 /*
711 * Neighbor advertisement output handling.
712 *
713 * Based on RFC 2461
714 *
715 * XXX NA delay for anycast address is not implemented yet
716 * (RFC 2461 7.2.7)
717 * XXX proxy advertisement?
718 */
719 void
720 nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr)
721 struct ifnet *ifp;
722 struct in6_addr *daddr6, *taddr6;
723 u_long flags;
724 int tlladdr; /* 1 if include target link-layer address */
725 {
726 struct mbuf *m;
727 struct ip6_hdr *ip6;
728 struct nd_neighbor_advert *nd_na;
729 struct in6_ifaddr *ia = NULL;
730 struct ip6_moptions im6o;
731 int icmp6len;
732 caddr_t mac;
733
734 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
735 return;
736
737 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
738 m->m_flags |= M_MCAST;
739 im6o.im6o_multicast_ifp = ifp;
740 im6o.im6o_multicast_hlim = 255;
741 im6o.im6o_multicast_loop = 0;
742 }
743
744 icmp6len = sizeof(*nd_na);
745 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
746 MH_ALIGN(m, m->m_len + 16); /* 1+1+6 is enough. but just in case */
747
748 /* fill neighbor advertisement packet */
749 ip6 = mtod(m, struct ip6_hdr *);
750 ip6->ip6_flow = 0;
751 ip6->ip6_vfc = IPV6_VERSION;
752 ip6->ip6_nxt = IPPROTO_ICMPV6;
753 ip6->ip6_hlim = 255;
754 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
755 /* reply to DAD */
756 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
757 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
758 ip6->ip6_dst.s6_addr32[1] = 0;
759 ip6->ip6_dst.s6_addr32[2] = 0;
760 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
761 flags &= ~ND_NA_FLAG_SOLICITED;
762 } else
763 ip6->ip6_dst = *daddr6;
764
765 /*
766 * Select a source whose scope is the same as that of the dest.
767 */
768 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
769 if (ia == NULL) {
770 m_freem(m);
771 return;
772 }
773 ip6->ip6_src = ia->ia_addr.sin6_addr;
774 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
775 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
776 nd_na->nd_na_code = 0;
777 nd_na->nd_na_target = *taddr6;
778 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
779 nd_na->nd_na_target.s6_addr16[1] = 0;
780
781 /*
782 * "tlladdr" indicates NS's condition for adding tlladdr or not.
783 * see nd6_ns_input() for details.
784 * Basically, if NS packet is sent to unicast/anycast addr,
785 * target lladdr option SHOULD NOT be included.
786 */
787 if (tlladdr && (mac = nd6_ifptomac(ifp))) {
788 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
789 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
790
791 m->m_pkthdr.len += optlen;
792 m->m_len += optlen;
793 icmp6len += optlen;
794 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
795 /* xxx 8 bytes alignment? */
796 nd_opt->nd_opt_len = optlen >> 3;
797 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
798 } else
799 flags &= ~ND_NA_FLAG_OVERRIDE;
800
801 ip6->ip6_plen = htons((u_short)icmp6len);
802 nd_na->nd_na_flags_reserved = flags;
803 nd_na->nd_na_cksum = 0;
804 nd_na->nd_na_cksum =
805 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
806
807 #ifdef IPSEC
808 m->m_pkthdr.rcvif = NULL;
809 #endif /*IPSEC*/
810 ip6_output(m, NULL, NULL, 0, &im6o);
811 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
812 }
813
814 caddr_t
815 nd6_ifptomac(ifp)
816 struct ifnet *ifp;
817 {
818 switch (ifp->if_type) {
819 case IFT_ETHER:
820 case IFT_FDDI:
821 #ifdef __NetBSD__
822 return LLADDR(ifp->if_sadl);
823 #else
824 return ((caddr_t)(ifp + 1));
825 #endif
826 break;
827 default:
828 return NULL;
829 }
830 }
831
832 TAILQ_HEAD(dadq_head, dadq);
833 struct dadq {
834 TAILQ_ENTRY(dadq) dad_list;
835 struct ifaddr *dad_ifa;
836 int dad_count; /* max NS to send */
837 int dad_ns_ocount; /* NS sent so far */
838 int dad_ns_icount;
839 int dad_na_icount;
840 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
841 struct callout_handle dad_timer;
842 #endif
843 };
844
845 static struct dadq_head dadq;
846
847 static struct dadq *
848 nd6_dad_find(ifa)
849 struct ifaddr *ifa;
850 {
851 struct dadq *dp;
852
853 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
854 if (dp->dad_ifa == ifa)
855 return dp;
856 }
857 return NULL;
858 }
859
860 /*
861 * Start Duplicated Address Detection (DAD) for specified interface address.
862 */
863 void
864 nd6_dad_start(ifa, tick)
865 struct ifaddr *ifa;
866 int *tick; /* minimum delay ticks for IFF_UP event */
867 {
868 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
869 struct dadq *dp;
870 static int dad_init = 0;
871
872 if (!dad_init) {
873 TAILQ_INIT(&dadq);
874 dad_init++;
875 }
876
877 /*
878 * If we don't need DAD, don't do it.
879 * There are several cases:
880 * - DAD is disabled (ip6_dad_count == 0)
881 * - the interface address is anycast
882 */
883 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
884 printf("nd6_dad_start: called with non-tentative address "
885 "%s(%s)\n",
886 ip6_sprintf(&ia->ia_addr.sin6_addr),
887 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
888 return;
889 }
890 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
891 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
892 return;
893 }
894 if (!ip6_dad_count) {
895 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
896 return;
897 }
898 if (!ifa->ifa_ifp)
899 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
900 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
901 return;
902 if (nd6_dad_find(ifa) != NULL) {
903 /* DAD already in progress */
904 return;
905 }
906
907 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
908 if (dp == NULL) {
909 printf("nd6_dad_start: memory allocation failed for "
910 "%s(%s)\n",
911 ip6_sprintf(&ia->ia_addr.sin6_addr),
912 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
913 return;
914 }
915 bzero(dp, sizeof(*dp));
916 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
917
918 /* XXXJRT This is probably a purely debugging message. */
919 printf("%s: starting DAD for %s\n", ifa->ifa_ifp->if_xname,
920 ip6_sprintf(&ia->ia_addr.sin6_addr));
921
922 /*
923 * Send NS packet for DAD, ip6_dad_count times.
924 * Note that we must delay the first transmission, if this is the
925 * first packet to be sent from the interface after interface
926 * (re)initialization.
927 */
928 dp->dad_ifa = ifa;
929 ifa->ifa_refcnt++; /*just for safety*/
930 dp->dad_count = ip6_dad_count;
931 dp->dad_ns_icount = dp->dad_na_icount = 0;
932 dp->dad_ns_ocount = 0;
933 if (!tick) {
934 dp->dad_ns_ocount++;
935 nd6_ns_output(ifa->ifa_ifp, NULL, &ia->ia_addr.sin6_addr,
936 NULL, 1);
937 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
938 dp->dad_timer =
939 #endif
940 timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
941 nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
942 } else {
943 int ntick;
944
945 if (*tick == 0)
946 ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
947 else
948 ntick = *tick + random() % (hz / 2);
949 *tick = ntick;
950 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
951 dp->dad_timer =
952 #endif
953 timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
954 ntick);
955 }
956 }
957
958 static void
959 nd6_dad_timer(ifa)
960 struct ifaddr *ifa;
961 {
962 int s;
963 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
964 struct dadq *dp;
965
966 s = splsoftnet(); /*XXX*/
967
968 /* Sanity check */
969 if (ia == NULL) {
970 printf("nd6_dad_timer: called with null parameter\n");
971 goto done;
972 }
973 dp = nd6_dad_find(ifa);
974 if (dp == NULL) {
975 printf("nd6_dad_timer: DAD structure not found\n");
976 goto done;
977 }
978 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
979 printf("nd6_dad_timer: called with duplicated address "
980 "%s(%s)\n",
981 ip6_sprintf(&ia->ia_addr.sin6_addr),
982 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
983 goto done;
984 }
985 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
986 printf("nd6_dad_timer: called with non-tentative address "
987 "%s(%s)\n",
988 ip6_sprintf(&ia->ia_addr.sin6_addr),
989 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
990 goto done;
991 }
992
993 /* Need more checks? */
994 if (dp->dad_ns_ocount < dp->dad_count) {
995 /*
996 * We have more NS to go. Send NS packet for DAD.
997 */
998 dp->dad_ns_ocount++;
999 nd6_ns_output(ifa->ifa_ifp, NULL, &ia->ia_addr.sin6_addr,
1000 NULL, 1);
1001 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1002 dp->dad_timer =
1003 #endif
1004 timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
1005 nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
1006 } else {
1007 /*
1008 * We have transmitted sufficient number of DAD packets.
1009 * See what we've got.
1010 */
1011 int duplicate;
1012
1013 duplicate = 0;
1014
1015 if (dp->dad_na_icount) {
1016 /*
1017 * the check is in nd6_dad_na_input(),
1018 * but just in case
1019 */
1020 duplicate++;
1021 }
1022
1023 if (dp->dad_ns_icount) {
1024 #if 0 /*heuristics*/
1025 /*
1026 * if
1027 * - we have sent many(?) DAD NS, and
1028 * - the number of NS we sent equals to the
1029 * number of NS we've got, and
1030 * - we've got no NA
1031 * we may have a faulty network card/driver which
1032 * loops back multicasts to myself.
1033 */
1034 if (3 < dp->dad_count
1035 && dp->dad_ns_icount == dp->dad_count
1036 && dp->dad_na_icount == 0) {
1037 log(LOG_INFO, "DAD questionable for %s(%s): "
1038 "network card loops back multicast?\n",
1039 ip6_sprintf(&ia->ia_addr.sin6_addr),
1040 ifa->ifa_ifp->if_xname);
1041 /* XXX consider it a duplicate or not? */
1042 /* duplicate++; */
1043 } else {
1044 /* We've seen NS, means DAD has failed. */
1045 duplicate++;
1046 }
1047 #else
1048 /* We've seen NS, means DAD has failed. */
1049 duplicate++;
1050 #endif
1051 }
1052
1053 if (duplicate) {
1054 /* (*dp) will be freed in nd6_dad_duplicated() */
1055 dp = NULL;
1056 nd6_dad_duplicated(ifa);
1057 } else {
1058 /*
1059 * We are done with DAD. No NA came, no NS came.
1060 * duplicated address found.
1061 */
1062 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1063
1064 /* XXXJRT This is probably a purely debugging message */
1065 printf("%s: DAD complete for %s - no duplicates "
1066 "found\n", ifa->ifa_ifp->if_xname,
1067 ip6_sprintf(&ia->ia_addr.sin6_addr));
1068
1069 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1070 free(dp, M_IP6NDP);
1071 dp = NULL;
1072 ifa->ifa_refcnt--;
1073 }
1074 }
1075
1076 done:
1077 splx(s);
1078 }
1079
1080 void
1081 nd6_dad_duplicated(ifa)
1082 struct ifaddr *ifa;
1083 {
1084 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1085 struct dadq *dp;
1086
1087 dp = nd6_dad_find(ifa);
1088 if (dp == NULL) {
1089 printf("nd6_dad_duplicated: DAD structure not found\n");
1090 return;
1091 }
1092
1093 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: %d NS, "
1094 "%d NA\n", ifa->ifa_ifp->if_xname,
1095 ip6_sprintf(&ia->ia_addr.sin6_addr),
1096 dp->dad_ns_icount, dp->dad_na_icount);
1097
1098 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1099 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1100
1101 /* We are done with DAD, with duplicated address found. (failure) */
1102 untimeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa
1103 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1104 , dp->dad_timer
1105 #endif
1106 );
1107
1108 /* XXXJRT This is probably a purely debugging message. */
1109 printf("%s: DAD complete for %s - duplicate found\n",
1110 ifa->ifa_ifp->if_xname, ip6_sprintf(&ia->ia_addr.sin6_addr));
1111 printf("%s: manual intervention required\n", ifa->ifa_ifp->if_xname);
1112
1113 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1114 free(dp, M_IP6NDP);
1115 dp = NULL;
1116 ifa->ifa_refcnt--;
1117 }
1118
1119 void
1120 nd6_dad_ns_input(ifa)
1121 struct ifaddr *ifa;
1122 {
1123 struct in6_ifaddr *ia;
1124 struct ifnet *ifp;
1125 struct in6_addr *taddr6;
1126 struct dadq *dp;
1127 int duplicate;
1128
1129 if (!ifa)
1130 panic("ifa == NULL in nd6_dad_ns_input");
1131
1132 ia = (struct in6_ifaddr *)ifa;
1133 ifp = ifa->ifa_ifp;
1134 taddr6 = &ia->ia_addr.sin6_addr;
1135 duplicate = 0;
1136 dp = nd6_dad_find(ifa);
1137
1138 /*
1139 * If it is from myself, ignore this.
1140 */
1141 if (ifp && (ifp->if_flags & IFF_LOOPBACK))
1142 return;
1143
1144 /* Quickhack - completely ignore DAD NS packets */
1145 if (dad_ignore_ns) {
1146 log(LOG_INFO, "nd6_dad_ns_input: ignoring DAD NS packet for "
1147 "address %s(%s)\n", ip6_sprintf(taddr6),
1148 ifa->ifa_ifp->if_xname);
1149 return;
1150 }
1151
1152 /*
1153 * if I'm yet to start DAD, someone else started using this address
1154 * first. I have a duplicate and you win.
1155 */
1156 if (!dp || dp->dad_ns_ocount == 0)
1157 duplicate++;
1158
1159 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1160
1161 if (duplicate) {
1162 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1163 nd6_dad_duplicated(ifa);
1164 } else {
1165 /*
1166 * not sure if I got a duplicate.
1167 * increment ns count and see what happens.
1168 */
1169 if (dp)
1170 dp->dad_ns_icount++;
1171 }
1172 }
1173
1174 void
1175 nd6_dad_na_input(ifa)
1176 struct ifaddr *ifa;
1177 {
1178 struct dadq *dp;
1179
1180 if (!ifa)
1181 panic("ifa == NULL in nd6_dad_na_input");
1182
1183 dp = nd6_dad_find(ifa);
1184 if (dp)
1185 dp->dad_na_icount++;
1186
1187 /* remove the address. */
1188 nd6_dad_duplicated(ifa);
1189 }
1190