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