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