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