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