]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/nd6_nbr.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6_nbr.c
CommitLineData
b0d623f7 1/*
d1ecb069 2 * Copyright (c) 2009 Apple Inc. All rights reserved.
b0d623f7
A
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 */
9bccf70c
A
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 $ */
1c79356b
A
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
1c79356b
A
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>
1c79356b
A
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
1c79356b
A
85#if IPSEC
86#include <netinet6/ipsec.h>
9bccf70c
A
87#if INET6
88#include <netinet6/ipsec6.h>
89#endif
90extern int ipsec_bypass;
1c79356b
A
91#endif
92
93#include <net/net_osdep.h>
94
95#define SDL(s) ((struct sockaddr_dl *)s)
96
97struct dadq;
91447636 98static struct dadq *nd6_dad_find(struct ifaddr *);
9bccf70c 99#ifndef __APPLE__
91447636
A
100static void nd6_dad_starttimer(struct dadq *, int);
101static void nd6_dad_stoptimer(struct dadq *);
9bccf70c 102#else
91447636 103void nd6_dad_stoptimer(struct ifaddr *);
9bccf70c 104#endif
91447636
A
105static void nd6_dad_timer(struct ifaddr *);
106static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
107static void nd6_dad_ns_input(struct ifaddr *);
108static void nd6_dad_na_input(struct ifaddr *);
1c79356b
A
109
110static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/
111static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
112
91447636
A
113extern lck_mtx_t *dad6_mutex;
114extern lck_mtx_t *nd6_mutex;
1c79356b
A
115/*
116 * Input an Neighbor Solicitation Message.
117 *
118 * Based on RFC 2461
119 * Based on RFC 2462 (duplicated address detection)
120 */
121void
91447636
A
122nd6_ns_input(
123 struct mbuf *m,
124 int off,
125 int icmp6len)
1c79356b
A
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;
b0d623f7 135 struct ifaddr *ifa = NULL;
1c79356b
A
136 int lladdrlen = 0;
137 int anycast = 0, proxy = 0, tentative = 0;
138 int tlladdr;
139 union nd_opts ndopts;
b0d623f7 140 struct sockaddr_dl proxydl;
1c79356b 141
9bccf70c 142#ifndef PULLDOWN_TEST
91447636 143 IP6_EXTHDR_CHECK(m, off, icmp6len, return);
9bccf70c
A
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
1c79356b 155 if (ip6->ip6_hlim != 255) {
9bccf70c
A
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;
1c79356b
A
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
55e303ae 166 /* don't check ifindex portion */
1c79356b
A
167 && daddr6.s6_addr32[1] == 0
168 && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
169 && daddr6.s6_addr8[12] == 0xff) {
55e303ae 170 ; /* good */
1c79356b 171 } else {
9bccf70c
A
172 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
173 "(wrong ip6 dst)\n"));
1c79356b
A
174 goto bad;
175 }
176 }
177
1c79356b 178 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
9bccf70c 179 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
1c79356b
A
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) {
9bccf70c
A
189 nd6log((LOG_INFO,
190 "nd6_ns_input: invalid ND option, ignored\n"));
191 /* nd6_options have incremented stats */
192 goto freeit;
1c79356b
A
193 }
194
195 if (ndopts.nd_opts_src_lladdr) {
55e303ae 196 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
1c79356b
A
197 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
198 }
199
200 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
9bccf70c
A
201 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
202 "(link-layer address option)\n"));
1c79356b
A
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
b0d623f7
A
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 }
1c79356b 260 }
b0d623f7 261 RT_UNLOCK(rt);
1c79356b 262 rtfree(rt);
b0d623f7 263 }
1c79356b
A
264 }
265 if (!ifa) {
266 /*
9bccf70c 267 * We've got an NS packet, and we don't have that adddress
1c79356b
A
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) {
9bccf70c 280 nd6log((LOG_INFO,
1c79356b
A
281 "nd6_ns_input: lladdrlen mismatch for %s "
282 "(if %d, NS packet %d)\n",
9bccf70c
A
283 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
284 goto bad;
1c79356b
A
285 }
286
287 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
55e303ae
A
288 nd6log((LOG_INFO,
289 "nd6_ns_input: duplicate IP6 address %s\n",
290 ip6_sprintf(&saddr6)));
1c79356b
A
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),
b0d623f7 335 tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL);
1c79356b
A
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,
b0d623f7 345 tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL);
1c79356b
A
346 freeit:
347 m_freem(m);
b0d623f7
A
348 if (ifa != NULL)
349 ifafree(ifa);
1c79356b
A
350 return;
351
352 bad:
9bccf70c
A
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++;
1c79356b 357 m_freem(m);
b0d623f7
A
358 if (ifa != NULL)
359 ifafree(ifa);
1c79356b
A
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)
b0d623f7
A
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.
1c79356b
A
373 */
374void
91447636
A
375nd6_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)
1c79356b
A
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) {
9bccf70c 400#if DIAGNOSTIC
1c79356b
A
401 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
402 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
403#endif
404 return;
405 }
406
2d21ac55 407 MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1c79356b
A
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;
9bccf70c 417 m->m_pkthdr.rcvif = NULL;
1c79356b
A
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;
55e303ae 428 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
1c79356b
A
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 */
b0d623f7
A
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 {
1c79356b
A
508 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
509 if (ia == NULL) {
b0d623f7
A
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 }
55e303ae 517 m_freem(m);
1c79356b
A
518 return;
519 }
520 ip6->ip6_src = ia->ia_addr.sin6_addr;
521 }
b0d623f7
A
522 if (ia != NULL) {
523 ifafree(&ia->ia_ifa);
524 ia = NULL;
525 }
1c79356b
A
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 */
9bccf70c
A
577 if (ipsec_bypass == 0)
578 (void)ipsec_setsocket(m, NULL);
1c79356b 579#endif
91447636 580 ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, locked);
1c79356b
A
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 */
598void
91447636
A
599nd6_na_input(
600 struct mbuf *m,
601 int off,
602 int icmp6len)
1c79356b
A
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;
b0d623f7 618 struct ifaddr *ifa = NULL;
1c79356b
A
619 struct llinfo_nd6 *ln;
620 struct rtentry *rt;
621 struct sockaddr_dl *sdl;
622 union nd_opts ndopts;
91447636 623 struct timeval timenow;
1c79356b
A
624
625 if (ip6->ip6_hlim != 255) {
9bccf70c
A
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;
1c79356b
A
631 }
632
633#ifndef PULLDOWN_TEST
91447636 634 IP6_EXTHDR_CHECK(m, off, icmp6len, return);
1c79356b
A
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)) {
9bccf70c 653 nd6log((LOG_ERR,
1c79356b 654 "nd6_na_input: invalid target address %s\n",
9bccf70c
A
655 ip6_sprintf(&taddr6)));
656 goto bad;
1c79356b
A
657 }
658 if (IN6_IS_ADDR_MULTICAST(&daddr6))
659 if (is_solicited) {
9bccf70c
A
660 nd6log((LOG_ERR,
661 "nd6_na_input: a solicited adv is multicasted\n"));
662 goto bad;
1c79356b
A
663 }
664
665 icmp6len -= sizeof(*nd_na);
666 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
667 if (nd6_options(&ndopts) < 0) {
9bccf70c
A
668 nd6log((LOG_INFO,
669 "nd6_na_input: invalid ND option, ignored\n"));
670 /* nd6_options have incremented stats */
1c79356b
A
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
55e303ae 696 /* Just for safety, maybe unnecessary. */
1c79356b
A
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) {
9bccf70c 705 nd6log((LOG_INFO,
1c79356b
A
706 "nd6_na_input: lladdrlen mismatch for %s "
707 "(if %d, NA packet %d)\n",
9bccf70c
A
708 ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
709 goto bad;
1c79356b
A
710 }
711
712 /*
713 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
b0d623f7 714 * Callee returns a locked route upon success.
1c79356b 715 */
b0d623f7
A
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);
1c79356b 724 goto freeit;
b0d623f7 725 }
1c79356b 726
91447636 727 getmicrotime(&timenow);
1c79356b
A
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 */
b0d623f7
A
733 if (ifp->if_addrlen && !lladdr) {
734 RT_REMREF_LOCKED(rt);
735 RT_UNLOCK(rt);
1c79356b 736 goto freeit;
b0d623f7 737 }
1c79356b
A
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;
9bccf70c 746 ln->ln_byhint = 0;
b0d623f7
A
747 if (ln->ln_expire) {
748 lck_rw_lock_shared(nd_if_rwlock);
d1ecb069
A
749 ln->ln_expire = rt_expiry(rt, timenow.tv_sec,
750 nd_ifinfo[rt->rt_ifp->if_index].reachable);
b0d623f7
A
751 lck_rw_done(nd_if_rwlock);
752 }
9bccf70c 753 } else {
1c79356b 754 ln->ln_state = ND6_LLINFO_STALE;
d1ecb069
A
755 ln->ln_expire = rt_expiry(rt, timenow.tv_sec,
756 nd6_gctimer);
9bccf70c
A
757 }
758 if ((ln->ln_router = is_router) != 0) {
759 /*
760 * This means a router's state has changed from
761 * non-reachable to probably reachable, and might
762 * affect the status of associated prefixes..
763 */
b0d623f7 764 RT_UNLOCK(rt);
91447636 765 pfxlist_onlink_check(0);
b0d623f7 766 RT_LOCK(rt);
9bccf70c 767 }
1c79356b
A
768 } else {
769 int llchange;
770
771 /*
772 * Check if the link-layer address has changed or not.
773 */
774 if (!lladdr)
775 llchange = 0;
776 else {
777 if (sdl->sdl_alen) {
778 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
779 llchange = 1;
780 else
781 llchange = 0;
782 } else
783 llchange = 1;
784 }
785
786 /*
787 * This is VERY complex. Look at it with care.
788 *
789 * override solicit lladdr llchange action
790 * (L: record lladdr)
791 *
792 * 0 0 n -- (2c)
793 * 0 0 y n (2b) L
794 * 0 0 y y (1) REACHABLE->STALE
795 * 0 1 n -- (2c) *->REACHABLE
796 * 0 1 y n (2b) L *->REACHABLE
797 * 0 1 y y (1) REACHABLE->STALE
798 * 1 0 n -- (2a)
799 * 1 0 y n (2a) L
800 * 1 0 y y (2a) L *->STALE
801 * 1 1 n -- (2a) *->REACHABLE
802 * 1 1 y n (2a) L *->REACHABLE
803 * 1 1 y y (2a) L *->REACHABLE
804 */
805 if (!is_override && (lladdr && llchange)) { /* (1) */
806 /*
807 * If state is REACHABLE, make it STALE.
808 * no other updates should be done.
809 */
9bccf70c 810 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
1c79356b 811 ln->ln_state = ND6_LLINFO_STALE;
d1ecb069
A
812 ln->ln_expire = rt_expiry(rt, timenow.tv_sec,
813 nd6_gctimer);
9bccf70c 814 }
b0d623f7
A
815 RT_REMREF_LOCKED(rt);
816 RT_UNLOCK(rt);
1c79356b
A
817 goto freeit;
818 } else if (is_override /* (2a) */
819 || (!is_override && (lladdr && !llchange)) /* (2b) */
820 || !lladdr) { /* (2c) */
821 /*
822 * Update link-local address, if any.
823 */
824 if (lladdr) {
825 sdl->sdl_alen = ifp->if_addrlen;
826 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
827 }
828
829 /*
830 * If solicited, make the state REACHABLE.
831 * If not solicited and the link-layer address was
832 * changed, make it STALE.
833 */
834 if (is_solicited) {
835 ln->ln_state = ND6_LLINFO_REACHABLE;
9bccf70c 836 ln->ln_byhint = 0;
1c79356b 837 if (ln->ln_expire) {
b0d623f7 838 lck_rw_lock_shared(nd_if_rwlock);
d1ecb069
A
839 ln->ln_expire =
840 rt_expiry(rt, timenow.tv_sec,
841 nd_ifinfo[ifp->if_index].reachable);
b0d623f7 842 lck_rw_done(nd_if_rwlock);
1c79356b
A
843 }
844 } else {
9bccf70c 845 if (lladdr && llchange) {
1c79356b 846 ln->ln_state = ND6_LLINFO_STALE;
d1ecb069
A
847 ln->ln_expire = rt_expiry(rt,
848 timenow.tv_sec, nd6_gctimer);
9bccf70c 849 }
1c79356b
A
850 }
851 }
852
853 if (ln->ln_router && !is_router) {
854 /*
855 * The peer dropped the router flag.
856 * Remove the sender from the Default Router List and
857 * update the Destination Cache entries.
858 */
859 struct nd_defrouter *dr;
860 struct in6_addr *in6;
b0d623f7 861 struct ifnet *rt_ifp = rt->rt_ifp;
1c79356b
A
862
863 in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
55e303ae
A
864
865 /*
866 * Lock to protect the default router list.
867 * XXX: this might be unnecessary, since this function
868 * is only called under the network software interrupt
869 * context. However, we keep it just for safety.
870 */
b0d623f7 871 RT_UNLOCK(rt);
91447636 872 lck_mtx_lock(nd6_mutex);
b0d623f7 873 dr = defrouter_lookup(in6, rt_ifp);
91447636
A
874 if (dr) {
875 defrtrlist_del(dr, 1);
876 lck_mtx_unlock(nd6_mutex);
877 }
878 else {
879 lck_mtx_unlock(nd6_mutex);
b0d623f7 880 if (!ip6_forwarding && (ip6_accept_rtadv || (rt_ifp->if_eflags & IFEF_ACCEPT_RTADVD))) {
91447636
A
881 /*
882 * Even if the neighbor is not in the default
883 * router list, the neighbor may be used
884 * as a next hop for some destinations
885 * (e.g. redirect case). So we must
886 * call rt6_flush explicitly.
887 */
b0d623f7 888 rt6_flush(&ip6->ip6_src, rt_ifp);
91447636 889 }
1c79356b 890 }
b0d623f7 891 RT_LOCK(rt);
1c79356b
A
892 }
893 ln->ln_router = is_router;
894 }
b0d623f7 895 RT_LOCK_ASSERT_HELD(rt);
1c79356b
A
896 rt->rt_flags &= ~RTF_REJECT;
897 ln->ln_asked = 0;
b0d623f7
A
898 if (ln->ln_hold != NULL) {
899 struct mbuf *n = ln->ln_hold;
900 ln->ln_hold = NULL;
9bccf70c 901 /*
55e303ae 902 * we assume ifp is not a loopback here, so just set the 2nd
9bccf70c
A
903 * argument as the 1st one.
904 */
b0d623f7
A
905 RT_UNLOCK(rt);
906 nd6_output(ifp, ifp, n, (struct sockaddr_in6 *)rt_key(rt),
907 rt, 0);
908 RT_LOCK_SPIN(rt);
1c79356b 909 }
b0d623f7
A
910 RT_REMREF_LOCKED(rt);
911 RT_UNLOCK(rt);
1c79356b
A
912
913 freeit:
914 m_freem(m);
b0d623f7
A
915 if (ifa != NULL)
916 ifafree(ifa);
9bccf70c
A
917 return;
918
919 bad:
920 icmp6stat.icp6s_badna++;
921 m_freem(m);
b0d623f7
A
922 if (ifa != NULL)
923 ifafree(ifa);
1c79356b
A
924}
925
926/*
927 * Neighbor advertisement output handling.
928 *
929 * Based on RFC 2461
930 *
931 * the following items are not implemented yet:
932 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
933 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
934 */
935void
91447636
A
936nd6_na_output(
937 struct ifnet *ifp,
938 const struct in6_addr *daddr6,
939 const struct in6_addr *taddr6,
b0d623f7 940 uint32_t flags,
91447636
A
941 int tlladdr, /* 1 if include target link-layer address */
942 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
1c79356b
A
943{
944 struct mbuf *m;
945 struct ip6_hdr *ip6;
946 struct nd_neighbor_advert *nd_na;
947 struct in6_ifaddr *ia = NULL;
948 struct ip6_moptions im6o;
949 int icmp6len;
950 int maxlen;
55e303ae 951 caddr_t mac = NULL;
1c79356b
A
952 struct ifnet *outif = NULL;
953
954 /* estimate the size of message */
955 maxlen = sizeof(*ip6) + sizeof(*nd_na);
956 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
957 if (max_linkhdr + maxlen >= MCLBYTES) {
9bccf70c 958#if DIAGNOSTIC
1c79356b
A
959 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
960 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
961#endif
962 return;
963 }
964
2d21ac55 965 MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1c79356b
A
966 if (m && max_linkhdr + maxlen >= MHLEN) {
967 MCLGET(m, M_DONTWAIT);
968 if ((m->m_flags & M_EXT) == 0) {
969 m_free(m);
970 m = NULL;
971 }
972 }
973 if (m == NULL)
974 return;
9bccf70c 975 m->m_pkthdr.rcvif = NULL;
1c79356b
A
976
977 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
978 m->m_flags |= M_MCAST;
979 im6o.im6o_multicast_ifp = ifp;
980 im6o.im6o_multicast_hlim = 255;
981 im6o.im6o_multicast_loop = 0;
982 }
983
984 icmp6len = sizeof(*nd_na);
985 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
55e303ae 986 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
1c79356b
A
987
988 /* fill neighbor advertisement packet */
989 ip6 = mtod(m, struct ip6_hdr *);
990 ip6->ip6_flow = 0;
991 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
992 ip6->ip6_vfc |= IPV6_VERSION;
993 ip6->ip6_nxt = IPPROTO_ICMPV6;
994 ip6->ip6_hlim = 255;
995 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
996 /* reply to DAD */
997 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
998 ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
999 ip6->ip6_dst.s6_addr32[1] = 0;
1000 ip6->ip6_dst.s6_addr32[2] = 0;
1001 ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1002 flags &= ~ND_NA_FLAG_SOLICITED;
1003 } else
1004 ip6->ip6_dst = *daddr6;
1005
1006 /*
1007 * Select a source whose scope is the same as that of the dest.
1008 */
1009 ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
1010 if (ia == NULL) {
1011 m_freem(m);
1012 return;
1013 }
1014 ip6->ip6_src = ia->ia_addr.sin6_addr;
b0d623f7
A
1015 ifafree(&ia->ia_ifa);
1016 ia = NULL;
1017
1c79356b
A
1018 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1019 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1020 nd_na->nd_na_code = 0;
1021 nd_na->nd_na_target = *taddr6;
1022 if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
1023 nd_na->nd_na_target.s6_addr16[1] = 0;
1024
1025 /*
1026 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1027 * see nd6_ns_input() for details.
1028 * Basically, if NS packet is sent to unicast/anycast addr,
1029 * target lladdr option SHOULD NOT be included.
1030 */
1031 if (tlladdr) {
1c79356b
A
1032 /*
1033 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1034 * lladdr in sdl0. If we are not proxying (sending NA for
1035 * my address) use lladdr configured for the interface.
1036 */
1037 if (sdl0 == NULL)
1038 mac = nd6_ifptomac(ifp);
1039 else if (sdl0->sa_family == AF_LINK) {
1040 struct sockaddr_dl *sdl;
1041 sdl = (struct sockaddr_dl *)sdl0;
1042 if (sdl->sdl_alen == ifp->if_addrlen)
1043 mac = LLADDR(sdl);
1044 }
1045 }
1046 if (tlladdr && mac) {
1047 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1048 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1049
1050 /* roundup to 8 bytes alignment! */
1051 optlen = (optlen + 7) & ~7;
1052
1053 m->m_pkthdr.len += optlen;
1054 m->m_len += optlen;
1055 icmp6len += optlen;
1056 bzero((caddr_t)nd_opt, optlen);
1057 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1058 nd_opt->nd_opt_len = optlen >> 3;
1059 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1060 } else
1061 flags &= ~ND_NA_FLAG_OVERRIDE;
1062
1063 ip6->ip6_plen = htons((u_short)icmp6len);
1064 nd_na->nd_na_flags_reserved = flags;
1065 nd_na->nd_na_cksum = 0;
1066 nd_na->nd_na_cksum =
1067 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1068
1069#if IPSEC
1070 /* Don't lookup socket */
9bccf70c
A
1071 if (ipsec_bypass == 0)
1072 (void)ipsec_setsocket(m, NULL);
1c79356b 1073#endif
91447636 1074 ip6_output(m, NULL, NULL, 0, &im6o, &outif, 0);
1c79356b
A
1075 if (outif) {
1076 icmp6_ifstat_inc(outif, ifs6_out_msg);
1077 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
1078 }
1079 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1080}
1081
1082caddr_t
91447636
A
1083nd6_ifptomac(
1084 struct ifnet *ifp)
1c79356b 1085{
91447636 1086 return ((caddr_t)ifnet_lladdr(ifp));
1c79356b
A
1087}
1088
1089TAILQ_HEAD(dadq_head, dadq);
1090struct dadq {
1091 TAILQ_ENTRY(dadq) dad_list;
1092 struct ifaddr *dad_ifa;
1093 int dad_count; /* max NS to send */
1094 int dad_ns_tcount; /* # of trials to send NS */
1095 int dad_ns_ocount; /* NS sent so far */
1096 int dad_ns_icount;
1097 int dad_na_icount;
1c79356b
A
1098};
1099
1100static struct dadq_head dadq;
9bccf70c 1101static int dad_init = 0;
1c79356b
A
1102
1103static struct dadq *
91447636
A
1104nd6_dad_find(
1105 struct ifaddr *ifa)
1c79356b
A
1106{
1107 struct dadq *dp;
91447636 1108 lck_mtx_lock(dad6_mutex);
1c79356b 1109 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
91447636
A
1110 if (dp->dad_ifa == ifa) {
1111 lck_mtx_unlock(dad6_mutex);
1c79356b 1112 return dp;
91447636 1113 }
1c79356b 1114 }
91447636 1115 lck_mtx_unlock(dad6_mutex);
1c79356b
A
1116 return NULL;
1117}
1118
9bccf70c
A
1119#ifdef __APPLE__
1120void
91447636
A
1121nd6_dad_stoptimer(
1122 struct ifaddr *ifa)
9bccf70c
A
1123{
1124
91447636 1125 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c
A
1126}
1127#else
1128static void
91447636
A
1129nd6_dad_starttimer(
1130 struct dadq *dp,
1131 int ticks)
9bccf70c
A
1132{
1133
1134 callout_reset(&dp->dad_timer_ch, ticks,
91447636 1135 (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
9bccf70c 1136}
55e303ae 1137
9bccf70c 1138static void
91447636
A
1139nd6_dad_stoptimer(
1140 struct dadq *dp)
9bccf70c
A
1141{
1142
1143 callout_stop(&dp->dad_timer_ch);
1144}
1145#endif
1146
1c79356b
A
1147/*
1148 * Start Duplicated Address Detection (DAD) for specified interface address.
1149 */
1150void
91447636
A
1151nd6_dad_start(
1152 struct ifaddr *ifa,
2d21ac55 1153 int *tick_delay) /* minimum delay ticks for IFF_UP event */
1c79356b
A
1154{
1155 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1156 struct dadq *dp;
1c79356b
A
1157
1158 if (!dad_init) {
1159 TAILQ_INIT(&dadq);
1160 dad_init++;
1161 }
1162
1163 /*
1164 * If we don't need DAD, don't do it.
1165 * There are several cases:
1166 * - DAD is disabled (ip6_dad_count == 0)
1167 * - the interface address is anycast
1168 */
1169 if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1170 log(LOG_DEBUG,
1171 "nd6_dad_start: called with non-tentative address "
1172 "%s(%s)\n",
1173 ip6_sprintf(&ia->ia_addr.sin6_addr),
1174 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1175 return;
1176 }
1177 if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1178 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1179 return;
1180 }
1181 if (!ip6_dad_count) {
1182 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1183 return;
1184 }
1185 if (!ifa->ifa_ifp)
1186 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1187 if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1188 return;
1189 if (nd6_dad_find(ifa) != NULL) {
1190 /* DAD already in progress */
1191 return;
1192 }
1193
1194 dp = _MALLOC(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1195 if (dp == NULL) {
1196 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1197 "%s(%s)\n",
1198 ip6_sprintf(&ia->ia_addr.sin6_addr),
1199 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1200 return;
1201 }
1202 bzero(dp, sizeof(*dp));
91447636 1203 lck_mtx_lock(dad6_mutex);
1c79356b 1204 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
91447636 1205 lck_mtx_unlock(dad6_mutex);
1c79356b 1206
9bccf70c
A
1207 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1208 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1c79356b
A
1209
1210 /*
1211 * Send NS packet for DAD, ip6_dad_count times.
1212 * Note that we must delay the first transmission, if this is the
1213 * first packet to be sent from the interface after interface
1214 * (re)initialization.
1215 */
1216 dp->dad_ifa = ifa;
9bccf70c 1217 ifaref(ifa); /*just for safety*/
1c79356b
A
1218 dp->dad_count = ip6_dad_count;
1219 dp->dad_ns_icount = dp->dad_na_icount = 0;
1220 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
2d21ac55 1221 if (tick_delay == NULL) {
b0d623f7 1222 u_int32_t retrans;
1c79356b 1223 nd6_dad_ns_output(dp, ifa);
b0d623f7
A
1224 lck_rw_lock_shared(nd_if_rwlock);
1225 retrans = nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000;
1226 lck_rw_done(nd_if_rwlock);
1227 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1c79356b
A
1228 } else {
1229 int ntick;
1230
2d21ac55 1231 if (*tick_delay == 0)
1c79356b
A
1232 ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
1233 else
2d21ac55
A
1234 ntick = *tick_delay + random() % (hz / 2);
1235 *tick_delay = ntick;
91447636 1236 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
1c79356b
A
1237 ntick);
1238 }
1239}
1240
9bccf70c
A
1241/*
1242 * terminate DAD unconditionally. used for address removals.
1243 */
1244void
91447636
A
1245nd6_dad_stop(
1246 struct ifaddr *ifa)
9bccf70c
A
1247{
1248 struct dadq *dp;
1249
1250 if (!dad_init)
1251 return;
1252 dp = nd6_dad_find(ifa);
1253 if (!dp) {
1254 /* DAD wasn't started yet */
1255 return;
1256 }
1257
91447636 1258 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c 1259
91447636 1260 lck_mtx_lock(dad6_mutex);
9bccf70c 1261 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
91447636 1262 lck_mtx_unlock(dad6_mutex);
9bccf70c
A
1263 FREE(dp, M_IP6NDP);
1264 dp = NULL;
1265 ifafree(ifa);
1266}
1267
1268
1c79356b 1269static void
91447636
A
1270nd6_dad_timer(
1271 struct ifaddr *ifa)
1c79356b 1272{
1c79356b
A
1273 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1274 struct dadq *dp;
1275
1c79356b
A
1276 /* Sanity check */
1277 if (ia == NULL) {
1278 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1279 goto done;
1280 }
1281 dp = nd6_dad_find(ifa);
1282 if (dp == NULL) {
1283 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1284 goto done;
1285 }
1286 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1287 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1288 "%s(%s)\n",
1289 ip6_sprintf(&ia->ia_addr.sin6_addr),
1290 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1291 goto done;
1292 }
1293 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1294 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1295 "%s(%s)\n",
1296 ip6_sprintf(&ia->ia_addr.sin6_addr),
1297 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1298 goto done;
1299 }
1300
1301 /* timeouted with IFF_{RUNNING,UP} check */
1302 if (dp->dad_ns_tcount > dad_maxtry) {
9bccf70c
A
1303 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1304 if_name(ifa->ifa_ifp)));
1c79356b 1305
91447636 1306 lck_mtx_lock(dad6_mutex);
1c79356b 1307 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
91447636 1308 lck_mtx_unlock(dad6_mutex);
9bccf70c 1309 FREE(dp, M_IP6NDP);
1c79356b 1310 dp = NULL;
9bccf70c 1311 ifafree(ifa);
1c79356b
A
1312 goto done;
1313 }
1314
1315 /* Need more checks? */
1316 if (dp->dad_ns_ocount < dp->dad_count) {
b0d623f7 1317 u_int32_t retrans;
1c79356b
A
1318 /*
1319 * We have more NS to go. Send NS packet for DAD.
1320 */
1321 nd6_dad_ns_output(dp, ifa);
b0d623f7
A
1322 lck_rw_lock_shared(nd_if_rwlock);
1323 retrans = nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000;
1324 lck_rw_done(nd_if_rwlock);
1325 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1c79356b
A
1326 } else {
1327 /*
1328 * We have transmitted sufficient number of DAD packets.
1329 * See what we've got.
1330 */
1331 int duplicate;
1332
1333 duplicate = 0;
1334
1335 if (dp->dad_na_icount) {
1336 /*
1337 * the check is in nd6_dad_na_input(),
1338 * but just in case
1339 */
1340 duplicate++;
1341 }
1342
1343 if (dp->dad_ns_icount) {
55e303ae 1344#if 0 /* heuristics */
1c79356b
A
1345 /*
1346 * if
1347 * - we have sent many(?) DAD NS, and
1348 * - the number of NS we sent equals to the
1349 * number of NS we've got, and
1350 * - we've got no NA
1351 * we may have a faulty network card/driver which
1352 * loops back multicasts to myself.
1353 */
1354 if (3 < dp->dad_count
1355 && dp->dad_ns_icount == dp->dad_count
1356 && dp->dad_na_icount == 0) {
1357 log(LOG_INFO, "DAD questionable for %s(%s): "
1358 "network card loops back multicast?\n",
1359 ip6_sprintf(&ia->ia_addr.sin6_addr),
1360 if_name(ifa->ifa_ifp));
1361 /* XXX consider it a duplicate or not? */
1362 /* duplicate++; */
1363 } else {
1364 /* We've seen NS, means DAD has failed. */
1365 duplicate++;
1366 }
1367#else
1368 /* We've seen NS, means DAD has failed. */
1369 duplicate++;
1370#endif
1371 }
1372
1373 if (duplicate) {
1374 /* (*dp) will be freed in nd6_dad_duplicated() */
1375 dp = NULL;
1376 nd6_dad_duplicated(ifa);
1377 } else {
1378 /*
1379 * We are done with DAD. No NA came, no NS came.
1380 * duplicated address found.
1381 */
1382 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1383
9bccf70c 1384 nd6log((LOG_DEBUG,
1c79356b
A
1385 "%s: DAD complete for %s - no duplicates found\n",
1386 if_name(ifa->ifa_ifp),
9bccf70c 1387 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1c79356b 1388
91447636 1389 lck_mtx_lock(dad6_mutex);
1c79356b 1390 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
91447636
A
1391 lck_mtx_unlock(dad6_mutex);
1392 in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia);
9bccf70c 1393 FREE(dp, M_IP6NDP);
1c79356b 1394 dp = NULL;
9bccf70c 1395 ifafree(ifa);
1c79356b
A
1396 }
1397 }
1398
1399done:
2d21ac55 1400 return;
1c79356b
A
1401}
1402
1403void
91447636
A
1404nd6_dad_duplicated(
1405 struct ifaddr *ifa)
1c79356b
A
1406{
1407 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1408 struct dadq *dp;
1409
1410 dp = nd6_dad_find(ifa);
1411 if (dp == NULL) {
1412 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1413 return;
1414 }
1415
9bccf70c
A
1416 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1417 "NS in/out=%d/%d, NA in=%d\n",
1418 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1419 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1c79356b
A
1420
1421 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1422 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1423
1424 /* We are done with DAD, with duplicated address found. (failure) */
91447636 1425 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
9bccf70c 1426
1c79356b
A
1427
1428 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1429 if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1430 log(LOG_ERR, "%s: manual intervention required\n",
1431 if_name(ifa->ifa_ifp));
1432
91447636 1433 lck_mtx_lock(dad6_mutex);
1c79356b 1434 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
91447636 1435 lck_mtx_unlock(dad6_mutex);
9bccf70c 1436 FREE(dp, M_IP6NDP);
1c79356b 1437 dp = NULL;
9bccf70c 1438 ifafree(ifa);
1c79356b
A
1439}
1440
1441static void
91447636
A
1442nd6_dad_ns_output(
1443 struct dadq *dp,
1444 struct ifaddr *ifa)
1c79356b
A
1445{
1446 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1447 struct ifnet *ifp = ifa->ifa_ifp;
1448
1449 dp->dad_ns_tcount++;
1450 if ((ifp->if_flags & IFF_UP) == 0) {
1451#if 0
1452 printf("%s: interface down?\n", if_name(ifp));
1453#endif
1454 return;
1455 }
1456 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1457#if 0
1458 printf("%s: interface not running?\n", if_name(ifp));
1459#endif
1460 return;
1461 }
1462
1463 dp->dad_ns_ocount++;
91447636 1464 nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1, 0);
1c79356b
A
1465}
1466
1467static void
91447636
A
1468nd6_dad_ns_input(
1469 struct ifaddr *ifa)
1c79356b
A
1470{
1471 struct in6_ifaddr *ia;
9bccf70c 1472 const struct in6_addr *taddr6;
1c79356b
A
1473 struct dadq *dp;
1474 int duplicate;
1475
1476 if (!ifa)
1477 panic("ifa == NULL in nd6_dad_ns_input");
1478
1479 ia = (struct in6_ifaddr *)ifa;
1c79356b
A
1480 taddr6 = &ia->ia_addr.sin6_addr;
1481 duplicate = 0;
1482 dp = nd6_dad_find(ifa);
1483
1c79356b
A
1484 /* Quickhack - completely ignore DAD NS packets */
1485 if (dad_ignore_ns) {
9bccf70c
A
1486 nd6log((LOG_INFO,
1487 "nd6_dad_ns_input: ignoring DAD NS packet for "
1c79356b 1488 "address %s(%s)\n", ip6_sprintf(taddr6),
9bccf70c 1489 if_name(ifa->ifa_ifp)));
1c79356b
A
1490 return;
1491 }
1492
1493 /*
1494 * if I'm yet to start DAD, someone else started using this address
1495 * first. I have a duplicate and you win.
1496 */
1497 if (!dp || dp->dad_ns_ocount == 0)
1498 duplicate++;
1499
1500 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1501
1502 if (duplicate) {
1503 dp = NULL; /* will be freed in nd6_dad_duplicated() */
1504 nd6_dad_duplicated(ifa);
1505 } else {
1506 /*
1507 * not sure if I got a duplicate.
1508 * increment ns count and see what happens.
1509 */
1510 if (dp)
1511 dp->dad_ns_icount++;
1512 }
1513}
1514
1515static void
91447636
A
1516nd6_dad_na_input(
1517 struct ifaddr *ifa)
1c79356b
A
1518{
1519 struct dadq *dp;
1520
1521 if (!ifa)
1522 panic("ifa == NULL in nd6_dad_na_input");
1523
1524 dp = nd6_dad_find(ifa);
1525 if (dp)
1526 dp->dad_na_icount++;
1527
1528 /* remove the address. */
1529 nd6_dad_duplicated(ifa);
1530}