]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2011 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 | 69 | #include <sys/syslog.h> |
6d2010ae A |
70 | #include <sys/sysctl.h> |
71 | #include <sys/mcache.h> | |
72 | #include <sys/protosw.h> | |
1c79356b A |
73 | #include <kern/queue.h> |
74 | ||
6d2010ae A |
75 | #include <kern/locks.h> |
76 | #include <kern/zalloc.h> | |
77 | ||
1c79356b | 78 | #include <net/if.h> |
6d2010ae | 79 | #include <net/if_var.h> |
1c79356b A |
80 | #include <net/if_types.h> |
81 | #include <net/if_dl.h> | |
6d2010ae | 82 | #include <net/if_llreach.h> |
1c79356b A |
83 | #include <net/route.h> |
84 | ||
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_var.h> | |
87 | #include <netinet6/in6_var.h> | |
88 | #include <netinet/ip6.h> | |
89 | #include <netinet6/ip6_var.h> | |
90 | #include <netinet6/nd6.h> | |
6d2010ae | 91 | #include <netinet6/scope6_var.h> |
1c79356b A |
92 | #include <netinet/icmp6.h> |
93 | ||
1c79356b A |
94 | #if IPSEC |
95 | #include <netinet6/ipsec.h> | |
9bccf70c A |
96 | #if INET6 |
97 | #include <netinet6/ipsec6.h> | |
98 | #endif | |
99 | extern int ipsec_bypass; | |
1c79356b A |
100 | #endif |
101 | ||
102 | #include <net/net_osdep.h> | |
103 | ||
104 | #define SDL(s) ((struct sockaddr_dl *)s) | |
105 | ||
106 | struct dadq; | |
91447636 | 107 | static struct dadq *nd6_dad_find(struct ifaddr *); |
91447636 | 108 | void nd6_dad_stoptimer(struct ifaddr *); |
91447636 A |
109 | static void nd6_dad_timer(struct ifaddr *); |
110 | static void nd6_dad_ns_output(struct dadq *, struct ifaddr *); | |
111 | static void nd6_dad_ns_input(struct ifaddr *); | |
6d2010ae A |
112 | static void nd6_dad_na_input(struct ifaddr *, caddr_t, int); |
113 | static void dad_addref(struct dadq *, int); | |
114 | static void dad_remref(struct dadq *); | |
115 | static struct dadq *nd6_dad_attach(struct dadq *, struct ifaddr *); | |
116 | static void nd6_dad_detach(struct dadq *, struct ifaddr *); | |
1c79356b A |
117 | |
118 | static int dad_ignore_ns = 0; /* ignore NS in DAD - specwise incorrect*/ | |
119 | static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */ | |
120 | ||
6d2010ae A |
121 | static unsigned int dad_size; /* size of zone element */ |
122 | static struct zone *dad_zone; /* zone for dadq */ | |
123 | ||
124 | #define DAD_ZONE_MAX 64 /* maximum elements in zone */ | |
125 | #define DAD_ZONE_NAME "nd6_dad" /* zone name */ | |
126 | ||
127 | #define DAD_LOCK_ASSERT_HELD(_dp) \ | |
128 | lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED) | |
129 | ||
130 | #define DAD_LOCK_ASSERT_NOTHELD(_dp) \ | |
131 | lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED) | |
132 | ||
133 | #define DAD_LOCK(_dp) \ | |
134 | lck_mtx_lock(&(_dp)->dad_lock) | |
135 | ||
136 | #define DAD_LOCK_SPIN(_dp) \ | |
137 | lck_mtx_lock_spin(&(_dp)->dad_lock) | |
138 | ||
139 | #define DAD_CONVERT_LOCK(_dp) do { \ | |
140 | DAD_LOCK_ASSERT_HELD(_dp); \ | |
141 | lck_mtx_convert_spin(&(_dp)->dad_lock); \ | |
142 | } while (0) | |
143 | ||
144 | #define DAD_UNLOCK(_dp) \ | |
145 | lck_mtx_unlock(&(_dp)->dad_lock) | |
146 | ||
147 | #define DAD_ADDREF(_dp) \ | |
148 | dad_addref(_dp, 0) | |
149 | ||
150 | #define DAD_ADDREF_LOCKED(_dp) \ | |
151 | dad_addref(_dp, 1) | |
152 | ||
153 | #define DAD_REMREF(_dp) \ | |
154 | dad_remref(_dp) | |
155 | ||
91447636 A |
156 | extern lck_mtx_t *dad6_mutex; |
157 | extern lck_mtx_t *nd6_mutex; | |
6d2010ae A |
158 | extern int in6_get_hw_ifid(struct ifnet *, struct in6_addr *); |
159 | ||
160 | static int nd6_llreach_base = (LL_BASE_REACHABLE / 1000); /* seconds */ | |
161 | ||
162 | SYSCTL_DECL(_net_inet6_icmp6); | |
163 | ||
164 | SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_llreach_base, | |
165 | CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_llreach_base, LL_BASE_REACHABLE, | |
166 | "default ND6 link-layer reachability max lifetime (in seconds)"); | |
167 | ||
168 | #define SIN6(s) ((struct sockaddr_in6 *)s) | |
169 | ||
1c79356b | 170 | /* |
6d2010ae A |
171 | * Obtain a link-layer source cache entry for the sender. |
172 | * | |
173 | * NOTE: This is currently only for ND6/Ethernet. | |
174 | */ | |
175 | void | |
176 | nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr, | |
177 | unsigned int alen, boolean_t solicited) | |
178 | { | |
179 | struct llinfo_nd6 *ln = rt->rt_llinfo; | |
180 | ||
181 | if (nd6_llreach_base != 0 && | |
182 | ln->ln_expire != 0 && rt->rt_ifp != lo_ifp && | |
183 | ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */ | |
184 | alen == ifp->if_addrlen) { | |
185 | struct if_llreach *lr; | |
186 | const char *why = NULL, *type = ""; | |
187 | ||
188 | /* Become a regular mutex, just in case */ | |
189 | RT_CONVERT_LOCK(rt); | |
190 | ||
191 | if ((lr = ln->ln_llreach) != NULL) { | |
192 | type = (solicited ? "ND6 advertisement" : | |
193 | "ND6 unsolicited announcement"); | |
194 | /* | |
195 | * If target has changed, create a new record; | |
196 | * otherwise keep existing record. | |
197 | */ | |
198 | IFLR_LOCK(lr); | |
199 | if (bcmp(addr, lr->lr_key.addr, alen) != 0) { | |
200 | IFLR_UNLOCK(lr); | |
201 | /* Purge any link-layer info caching */ | |
202 | VERIFY(rt->rt_llinfo_purge != NULL); | |
203 | rt->rt_llinfo_purge(rt); | |
204 | lr = NULL; | |
205 | why = " for different target HW address; " | |
206 | "using new llreach record"; | |
207 | } else { | |
208 | lr->lr_probes = 0; /* reset probe count */ | |
209 | IFLR_UNLOCK(lr); | |
210 | if (solicited) { | |
211 | why = " for same target HW address; " | |
212 | "keeping existing llreach record"; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | if (lr == NULL) { | |
218 | lr = ln->ln_llreach = ifnet_llreach_alloc(ifp, | |
219 | ETHERTYPE_IPV6, addr, alen, nd6_llreach_base); | |
220 | if (lr != NULL) { | |
221 | lr->lr_probes = 0; /* reset probe count */ | |
222 | if (why == NULL) | |
223 | why = "creating new llreach record"; | |
224 | } | |
225 | } | |
226 | ||
227 | if (nd6_debug && lr != NULL && why != NULL) { | |
228 | char tmp[MAX_IPv6_STR_LEN]; | |
229 | ||
230 | nd6log((LOG_DEBUG, "%s%d: %s%s for %s\n", ifp->if_name, | |
231 | ifp->if_unit, type, why, inet_ntop(AF_INET6, | |
232 | &SIN6(rt_key(rt))->sin6_addr, tmp, sizeof (tmp)))); | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | void | |
238 | nd6_llreach_use(struct llinfo_nd6 *ln) | |
239 | { | |
240 | if (ln->ln_llreach != NULL) | |
241 | ln->ln_lastused = net_uptime(); | |
242 | } | |
243 | ||
244 | /* | |
245 | * Input a Neighbor Solicitation Message. | |
1c79356b A |
246 | * |
247 | * Based on RFC 2461 | |
6d2010ae | 248 | * Based on RFC 2462 (duplicate address detection) |
1c79356b A |
249 | */ |
250 | void | |
91447636 A |
251 | nd6_ns_input( |
252 | struct mbuf *m, | |
253 | int off, | |
254 | int icmp6len) | |
1c79356b A |
255 | { |
256 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
257 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
258 | struct nd_neighbor_solicit *nd_ns; | |
259 | struct in6_addr saddr6 = ip6->ip6_src; | |
260 | struct in6_addr daddr6 = ip6->ip6_dst; | |
261 | struct in6_addr taddr6; | |
262 | struct in6_addr myaddr6; | |
263 | char *lladdr = NULL; | |
b0d623f7 | 264 | struct ifaddr *ifa = NULL; |
1c79356b A |
265 | int lladdrlen = 0; |
266 | int anycast = 0, proxy = 0, tentative = 0; | |
267 | int tlladdr; | |
268 | union nd_opts ndopts; | |
b0d623f7 | 269 | struct sockaddr_dl proxydl; |
1c79356b | 270 | |
9bccf70c | 271 | #ifndef PULLDOWN_TEST |
91447636 | 272 | IP6_EXTHDR_CHECK(m, off, icmp6len, return); |
9bccf70c A |
273 | nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off); |
274 | #else | |
275 | IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len); | |
276 | if (nd_ns == NULL) { | |
277 | icmp6stat.icp6s_tooshort++; | |
278 | return; | |
279 | } | |
280 | #endif | |
281 | ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */ | |
282 | taddr6 = nd_ns->nd_ns_target; | |
6d2010ae A |
283 | if (in6_setscope(&taddr6, ifp, NULL) != 0) |
284 | goto bad; | |
9bccf70c | 285 | |
1c79356b | 286 | if (ip6->ip6_hlim != 255) { |
9bccf70c A |
287 | nd6log((LOG_ERR, |
288 | "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n", | |
289 | ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), | |
290 | ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); | |
291 | goto bad; | |
1c79356b A |
292 | } |
293 | ||
294 | if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { | |
6d2010ae A |
295 | /* dst has to be a solicited node multicast address. */ |
296 | if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL && | |
55e303ae | 297 | /* don't check ifindex portion */ |
6d2010ae A |
298 | daddr6.s6_addr32[1] == 0 && |
299 | daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE && | |
300 | daddr6.s6_addr8[12] == 0xff) { | |
55e303ae | 301 | ; /* good */ |
1c79356b | 302 | } else { |
9bccf70c A |
303 | nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " |
304 | "(wrong ip6 dst)\n")); | |
1c79356b A |
305 | goto bad; |
306 | } | |
6d2010ae A |
307 | } else if (!nd6_onlink_ns_rfc4861) { |
308 | struct sockaddr_in6 src_sa6; | |
309 | ||
310 | /* | |
311 | * According to recent IETF discussions, it is not a good idea | |
312 | * to accept a NS from an address which would not be deemed | |
313 | * to be a neighbor otherwise. This point is expected to be | |
314 | * clarified in future revisions of the specification. | |
315 | */ | |
316 | bzero(&src_sa6, sizeof(src_sa6)); | |
317 | src_sa6.sin6_family = AF_INET6; | |
318 | src_sa6.sin6_len = sizeof(src_sa6); | |
319 | src_sa6.sin6_addr = saddr6; | |
320 | if (!nd6_is_addr_neighbor(&src_sa6, ifp, 0)) { | |
321 | nd6log((LOG_INFO, "nd6_ns_input: " | |
322 | "NS packet from non-neighbor\n")); | |
323 | goto bad; | |
324 | } | |
1c79356b A |
325 | } |
326 | ||
1c79356b | 327 | if (IN6_IS_ADDR_MULTICAST(&taddr6)) { |
9bccf70c | 328 | nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n")); |
1c79356b A |
329 | goto bad; |
330 | } | |
331 | ||
1c79356b A |
332 | icmp6len -= sizeof(*nd_ns); |
333 | nd6_option_init(nd_ns + 1, icmp6len, &ndopts); | |
334 | if (nd6_options(&ndopts) < 0) { | |
9bccf70c A |
335 | nd6log((LOG_INFO, |
336 | "nd6_ns_input: invalid ND option, ignored\n")); | |
337 | /* nd6_options have incremented stats */ | |
338 | goto freeit; | |
1c79356b A |
339 | } |
340 | ||
341 | if (ndopts.nd_opts_src_lladdr) { | |
55e303ae | 342 | lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); |
1c79356b A |
343 | lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; |
344 | } | |
6d2010ae | 345 | |
1c79356b | 346 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) { |
9bccf70c A |
347 | nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet " |
348 | "(link-layer address option)\n")); | |
1c79356b A |
349 | goto bad; |
350 | } | |
351 | ||
352 | /* | |
353 | * Attaching target link-layer address to the NA? | |
354 | * (RFC 2461 7.2.4) | |
355 | * | |
356 | * NS IP dst is unicast/anycast MUST NOT add | |
357 | * NS IP dst is solicited-node multicast MUST add | |
358 | * | |
359 | * In implementation, we add target link-layer address by default. | |
360 | * We do not add one in MUST NOT cases. | |
361 | */ | |
1c79356b A |
362 | if (!IN6_IS_ADDR_MULTICAST(&daddr6)) |
363 | tlladdr = 0; | |
364 | else | |
365 | tlladdr = 1; | |
366 | ||
367 | /* | |
368 | * Target address (taddr6) must be either: | |
369 | * (1) Valid unicast/anycast address for my receiving interface, | |
370 | * (2) Unicast address for which I'm offering proxy service, or | |
371 | * (3) "tentative" address on which DAD is being performed. | |
372 | */ | |
373 | /* (1) and (3) check. */ | |
374 | ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); | |
375 | ||
376 | /* (2) check. */ | |
6d2010ae | 377 | if (ifa == NULL) { |
1c79356b A |
378 | struct rtentry *rt; |
379 | struct sockaddr_in6 tsin6; | |
380 | ||
6d2010ae | 381 | bzero(&tsin6, sizeof tsin6); |
1c79356b A |
382 | tsin6.sin6_len = sizeof(struct sockaddr_in6); |
383 | tsin6.sin6_family = AF_INET6; | |
384 | tsin6.sin6_addr = taddr6; | |
385 | ||
6d2010ae A |
386 | rt = rtalloc1_scoped((struct sockaddr *)&tsin6, 0, 0, |
387 | ifp->if_index); | |
388 | ||
b0d623f7 A |
389 | if (rt != NULL) { |
390 | RT_LOCK(rt); | |
391 | if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && | |
392 | rt->rt_gateway->sa_family == AF_LINK) { | |
393 | /* | |
394 | * proxy NDP for single entry | |
395 | */ | |
396 | ifa = (struct ifaddr *)in6ifa_ifpforlinklocal( | |
397 | ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST); | |
398 | if (ifa) { | |
399 | proxy = 1; | |
400 | proxydl = *SDL(rt->rt_gateway); | |
401 | } | |
1c79356b | 402 | } |
b0d623f7 | 403 | RT_UNLOCK(rt); |
1c79356b | 404 | rtfree(rt); |
b0d623f7 | 405 | } |
1c79356b | 406 | } |
6d2010ae | 407 | if (ifa == NULL) { |
1c79356b | 408 | /* |
9bccf70c | 409 | * We've got an NS packet, and we don't have that adddress |
1c79356b A |
410 | * assigned for us. We MUST silently ignore it. |
411 | * See RFC2461 7.2.3. | |
412 | */ | |
413 | goto freeit; | |
414 | } | |
6d2010ae | 415 | IFA_LOCK(ifa); |
1c79356b A |
416 | myaddr6 = *IFA_IN6(ifa); |
417 | anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST; | |
418 | tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE; | |
6d2010ae A |
419 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED) { |
420 | IFA_UNLOCK(ifa); | |
1c79356b | 421 | goto freeit; |
6d2010ae A |
422 | } |
423 | IFA_UNLOCK(ifa); | |
1c79356b A |
424 | |
425 | if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { | |
9bccf70c | 426 | nd6log((LOG_INFO, |
1c79356b A |
427 | "nd6_ns_input: lladdrlen mismatch for %s " |
428 | "(if %d, NS packet %d)\n", | |
9bccf70c A |
429 | ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2)); |
430 | goto bad; | |
1c79356b A |
431 | } |
432 | ||
433 | if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) { | |
55e303ae A |
434 | nd6log((LOG_INFO, |
435 | "nd6_ns_input: duplicate IP6 address %s\n", | |
436 | ip6_sprintf(&saddr6))); | |
1c79356b A |
437 | goto freeit; |
438 | } | |
439 | ||
440 | /* | |
441 | * We have neighbor solicitation packet, with target address equals to | |
442 | * one of my tentative address. | |
443 | * | |
444 | * src addr how to process? | |
445 | * --- --- | |
446 | * multicast of course, invalid (rejected in ip6_input) | |
447 | * unicast somebody is doing address resolution -> ignore | |
448 | * unspec dup address detection | |
449 | * | |
450 | * The processing is defined in RFC 2462. | |
451 | */ | |
452 | if (tentative) { | |
453 | /* | |
454 | * If source address is unspecified address, it is for | |
6d2010ae | 455 | * duplicate address detection. |
1c79356b A |
456 | * |
457 | * If not, the packet is for addess resolution; | |
458 | * silently ignore it. | |
459 | */ | |
460 | if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) | |
461 | nd6_dad_ns_input(ifa); | |
462 | ||
463 | goto freeit; | |
464 | } | |
465 | ||
466 | /* | |
467 | * If the source address is unspecified address, entries must not | |
468 | * be created or updated. | |
469 | * It looks that sender is performing DAD. Output NA toward | |
470 | * all-node multicast address, to tell the sender that I'm using | |
471 | * the address. | |
472 | * S bit ("solicited") must be zero. | |
473 | */ | |
474 | if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) { | |
475 | saddr6 = in6addr_linklocal_allnodes; | |
6d2010ae A |
476 | if (in6_setscope(&saddr6, ifp, NULL) != 0) |
477 | goto bad; | |
1c79356b A |
478 | nd6_na_output(ifp, &saddr6, &taddr6, |
479 | ((anycast || proxy || !tlladdr) | |
480 | ? 0 : ND_NA_FLAG_OVERRIDE) | |
481 | | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0), | |
b0d623f7 | 482 | tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL); |
1c79356b A |
483 | goto freeit; |
484 | } | |
485 | ||
486 | nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0); | |
487 | ||
488 | nd6_na_output(ifp, &saddr6, &taddr6, | |
489 | ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) | |
490 | | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | |
491 | | ND_NA_FLAG_SOLICITED, | |
b0d623f7 | 492 | tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL); |
1c79356b A |
493 | freeit: |
494 | m_freem(m); | |
b0d623f7 | 495 | if (ifa != NULL) |
6d2010ae | 496 | IFA_REMREF(ifa); |
1c79356b A |
497 | return; |
498 | ||
499 | bad: | |
9bccf70c A |
500 | nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6))); |
501 | nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6))); | |
502 | nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6))); | |
503 | icmp6stat.icp6s_badns++; | |
1c79356b | 504 | m_freem(m); |
b0d623f7 | 505 | if (ifa != NULL) |
6d2010ae | 506 | IFA_REMREF(ifa); |
1c79356b A |
507 | } |
508 | ||
509 | /* | |
6d2010ae | 510 | * Output a Neighbor Solicitation Message. Caller specifies: |
1c79356b A |
511 | * - ICMP6 header source IP6 address |
512 | * - ND6 header target IP6 address | |
513 | * - ND6 header source datalink address | |
514 | * | |
515 | * Based on RFC 2461 | |
6d2010ae | 516 | * Based on RFC 2462 (duplicate address detection) |
b0d623f7 A |
517 | * |
518 | * Caller must bump up ln->ln_rt refcnt to make sure 'ln' doesn't go | |
519 | * away if there is a llinfo_nd6 passed in. | |
1c79356b A |
520 | */ |
521 | void | |
91447636 A |
522 | nd6_ns_output( |
523 | struct ifnet *ifp, | |
524 | const struct in6_addr *daddr6, | |
525 | const struct in6_addr *taddr6, | |
526 | struct llinfo_nd6 *ln, /* for source address determination */ | |
6d2010ae | 527 | int dad) /* duplicated address detection */ |
1c79356b A |
528 | { |
529 | struct mbuf *m; | |
530 | struct ip6_hdr *ip6; | |
531 | struct nd_neighbor_solicit *nd_ns; | |
532 | struct in6_ifaddr *ia = NULL; | |
6d2010ae A |
533 | struct in6_addr *src, src_in, src_storage; |
534 | struct ip6_moptions *im6o = NULL; | |
535 | struct ifnet *outif = NULL; | |
1c79356b A |
536 | int icmp6len; |
537 | int maxlen; | |
6d2010ae | 538 | int flags; |
1c79356b | 539 | caddr_t mac; |
6d2010ae A |
540 | struct route_in6 ro; |
541 | struct ip6_out_args ip6oa = { IFSCOPE_NONE, 0 }; | |
542 | ||
543 | bzero(&ro, sizeof(ro)); | |
544 | ||
1c79356b A |
545 | if (IN6_IS_ADDR_MULTICAST(taddr6)) |
546 | return; | |
547 | ||
6d2010ae A |
548 | ip6oa.ip6oa_boundif = ifp->if_index; |
549 | ||
1c79356b A |
550 | /* estimate the size of message */ |
551 | maxlen = sizeof(*ip6) + sizeof(*nd_ns); | |
552 | maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; | |
553 | if (max_linkhdr + maxlen >= MCLBYTES) { | |
9bccf70c | 554 | #if DIAGNOSTIC |
1c79356b A |
555 | printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES " |
556 | "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); | |
557 | #endif | |
558 | return; | |
559 | } | |
560 | ||
2d21ac55 | 561 | MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ |
1c79356b A |
562 | if (m && max_linkhdr + maxlen >= MHLEN) { |
563 | MCLGET(m, M_DONTWAIT); | |
564 | if ((m->m_flags & M_EXT) == 0) { | |
565 | m_free(m); | |
566 | m = NULL; | |
567 | } | |
568 | } | |
569 | if (m == NULL) | |
570 | return; | |
9bccf70c | 571 | m->m_pkthdr.rcvif = NULL; |
1c79356b A |
572 | |
573 | if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) { | |
574 | m->m_flags |= M_MCAST; | |
6d2010ae A |
575 | |
576 | im6o = ip6_allocmoptions(M_DONTWAIT); | |
577 | if (im6o == NULL) { | |
578 | m_freem(m); | |
579 | return; | |
580 | } | |
581 | ||
582 | im6o->im6o_multicast_ifp = ifp; | |
583 | im6o->im6o_multicast_hlim = 255; | |
584 | im6o->im6o_multicast_loop = 0; | |
1c79356b A |
585 | } |
586 | ||
587 | icmp6len = sizeof(*nd_ns); | |
588 | m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len; | |
55e303ae | 589 | m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ |
1c79356b A |
590 | |
591 | /* fill neighbor solicitation packet */ | |
592 | ip6 = mtod(m, struct ip6_hdr *); | |
593 | ip6->ip6_flow = 0; | |
594 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
595 | ip6->ip6_vfc |= IPV6_VERSION; | |
596 | /* ip6->ip6_plen will be set later */ | |
597 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
598 | ip6->ip6_hlim = 255; | |
599 | if (daddr6) | |
600 | ip6->ip6_dst = *daddr6; | |
601 | else { | |
602 | ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; | |
6d2010ae | 603 | ip6->ip6_dst.s6_addr16[1] = 0; |
1c79356b A |
604 | ip6->ip6_dst.s6_addr32[1] = 0; |
605 | ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE; | |
606 | ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3]; | |
607 | ip6->ip6_dst.s6_addr8[12] = 0xff; | |
6d2010ae A |
608 | if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) |
609 | goto bad; | |
1c79356b A |
610 | } |
611 | if (!dad) { | |
1c79356b A |
612 | /* |
613 | * RFC2461 7.2.2: | |
614 | * "If the source address of the packet prompting the | |
615 | * solicitation is the same as one of the addresses assigned | |
616 | * to the outgoing interface, that address SHOULD be placed | |
617 | * in the IP Source Address of the outgoing solicitation. | |
618 | * Otherwise, any one of the addresses assigned to the | |
619 | * interface should be used." | |
620 | * | |
621 | * We use the source address for the prompting packet | |
622 | * (saddr6), if: | |
623 | * - saddr6 is given from the caller (by giving "ln"), and | |
624 | * - saddr6 belongs to the outgoing interface. | |
6d2010ae | 625 | * Otherwise, we perform the source address selection as usual. |
1c79356b | 626 | */ |
6d2010ae A |
627 | struct ip6_hdr *hip6; /* hold ip6 */ |
628 | struct in6_addr *hsrc = NULL; | |
b0d623f7 A |
629 | |
630 | /* Caller holds ref on this route */ | |
631 | if (ln != NULL) { | |
632 | RT_LOCK(ln->ln_rt); | |
6d2010ae A |
633 | /* |
634 | * assuming every packet in ln_hold has the same IP | |
635 | * header | |
636 | */ | |
b0d623f7 A |
637 | if (ln->ln_hold != NULL) { |
638 | hip6 = mtod(ln->ln_hold, struct ip6_hdr *); | |
639 | /* XXX pullup? */ | |
640 | if (sizeof (*hip6) < ln->ln_hold->m_len) | |
6d2010ae | 641 | hsrc = &hip6->ip6_src; |
b0d623f7 | 642 | else |
6d2010ae A |
643 | hsrc = NULL; |
644 | } | |
645 | /* Update probe count, if applicable */ | |
646 | if (ln->ln_llreach != NULL) { | |
647 | IFLR_LOCK_SPIN(ln->ln_llreach); | |
648 | ln->ln_llreach->lr_probes++; | |
649 | IFLR_UNLOCK(ln->ln_llreach); | |
b0d623f7 | 650 | } |
b0d623f7 | 651 | RT_UNLOCK(ln->ln_rt); |
b0d623f7 | 652 | |
1c79356b | 653 | } |
b0d623f7 | 654 | if (ia != NULL) { |
6d2010ae | 655 | IFA_REMREF(&ia->ia_ifa); |
b0d623f7 A |
656 | ia = NULL; |
657 | } | |
6d2010ae A |
658 | if (hsrc != NULL && (ia = in6ifa_ifpwithaddr(ifp, hsrc))) { |
659 | src = hsrc; | |
660 | IFA_REMREF(&ia->ia_ifa); | |
661 | ia = NULL; | |
662 | } else { | |
663 | int error; | |
664 | struct sockaddr_in6 dst_sa; | |
665 | ||
666 | bzero(&dst_sa, sizeof(dst_sa)); | |
667 | dst_sa.sin6_family = AF_INET6; | |
668 | dst_sa.sin6_len = sizeof(dst_sa); | |
669 | dst_sa.sin6_addr = ip6->ip6_dst; | |
670 | ||
671 | src = in6_selectsrc(&dst_sa, NULL, | |
672 | NULL, &ro, NULL, &src_storage, ip6oa.ip6oa_boundif, | |
673 | &error); | |
674 | if (src == NULL) { | |
675 | nd6log((LOG_DEBUG, | |
676 | "nd6_ns_output: source can't be " | |
677 | "determined: dst=%s, error=%d\n", | |
678 | ip6_sprintf(&dst_sa.sin6_addr), | |
679 | error)); | |
680 | goto bad; | |
681 | } | |
682 | } | |
1c79356b A |
683 | } else { |
684 | /* | |
685 | * Source address for DAD packet must always be IPv6 | |
686 | * unspecified address. (0::0) | |
6d2010ae A |
687 | * We actually don't have to 0-clear the address (we did it |
688 | * above), but we do so here explicitly to make the intention | |
689 | * clearer. | |
1c79356b | 690 | */ |
6d2010ae A |
691 | bzero(&src_in, sizeof(src_in)); |
692 | src = &src_in; | |
1c79356b | 693 | } |
6d2010ae | 694 | ip6->ip6_src = *src; |
1c79356b A |
695 | nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1); |
696 | nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT; | |
697 | nd_ns->nd_ns_code = 0; | |
698 | nd_ns->nd_ns_reserved = 0; | |
699 | nd_ns->nd_ns_target = *taddr6; | |
6d2010ae | 700 | in6_clearscope(&nd_ns->nd_ns_target); /* XXX */ |
1c79356b A |
701 | |
702 | /* | |
703 | * Add source link-layer address option. | |
704 | * | |
705 | * spec implementation | |
706 | * --- --- | |
707 | * DAD packet MUST NOT do not add the option | |
708 | * there's no link layer address: | |
709 | * impossible do not add the option | |
710 | * there's link layer address: | |
711 | * Multicast NS MUST add one add the option | |
712 | * Unicast NS SHOULD add one add the option | |
713 | */ | |
714 | if (!dad && (mac = nd6_ifptomac(ifp))) { | |
715 | int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; | |
716 | struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1); | |
717 | /* 8 byte alignments... */ | |
718 | optlen = (optlen + 7) & ~7; | |
719 | ||
720 | m->m_pkthdr.len += optlen; | |
721 | m->m_len += optlen; | |
722 | icmp6len += optlen; | |
723 | bzero((caddr_t)nd_opt, optlen); | |
724 | nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; | |
725 | nd_opt->nd_opt_len = optlen >> 3; | |
726 | bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); | |
727 | } | |
728 | ||
729 | ip6->ip6_plen = htons((u_short)icmp6len); | |
730 | nd_ns->nd_ns_cksum = 0; | |
731 | nd_ns->nd_ns_cksum | |
732 | = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len); | |
733 | ||
734 | #if IPSEC | |
735 | /* Don't lookup socket */ | |
9bccf70c A |
736 | if (ipsec_bypass == 0) |
737 | (void)ipsec_setsocket(m, NULL); | |
1c79356b | 738 | #endif |
6d2010ae A |
739 | flags = dad ? IPV6_UNSPECSRC : 0; |
740 | flags |= IPV6_OUTARGS; | |
741 | ||
742 | ip6_output(m, NULL, NULL, flags, im6o, &outif, &ip6oa); | |
1c79356b A |
743 | if (outif) { |
744 | icmp6_ifstat_inc(outif, ifs6_out_msg); | |
745 | icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit); | |
6d2010ae | 746 | ifnet_release(outif); |
1c79356b A |
747 | } |
748 | icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++; | |
6d2010ae A |
749 | |
750 | if (im6o != NULL) | |
751 | IM6O_REMREF(im6o); | |
752 | if (ro.ro_rt) { /* we don't cache this route. */ | |
753 | rtfree(ro.ro_rt); | |
754 | } | |
755 | if (ia != NULL) | |
756 | IFA_REMREF(&ia->ia_ifa); | |
757 | return; | |
758 | ||
759 | bad: | |
760 | if (im6o != NULL) | |
761 | IM6O_REMREF(im6o); | |
762 | if (ro.ro_rt) { | |
763 | rtfree(ro.ro_rt); | |
764 | } | |
765 | m_freem(m); | |
766 | if (ia != NULL) | |
767 | IFA_REMREF(&ia->ia_ifa); | |
768 | return; | |
1c79356b A |
769 | } |
770 | ||
771 | /* | |
772 | * Neighbor advertisement input handling. | |
773 | * | |
774 | * Based on RFC 2461 | |
6d2010ae | 775 | * Based on RFC 2462 (duplicate address detection) |
1c79356b A |
776 | * |
777 | * the following items are not implemented yet: | |
778 | * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) | |
779 | * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) | |
780 | */ | |
781 | void | |
91447636 A |
782 | nd6_na_input( |
783 | struct mbuf *m, | |
784 | int off, | |
785 | int icmp6len) | |
1c79356b A |
786 | { |
787 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
788 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
789 | struct nd_neighbor_advert *nd_na; | |
1c79356b A |
790 | struct in6_addr daddr6 = ip6->ip6_dst; |
791 | struct in6_addr taddr6; | |
792 | int flags; | |
793 | int is_router; | |
794 | int is_solicited; | |
795 | int is_override; | |
796 | char *lladdr = NULL; | |
797 | int lladdrlen = 0; | |
b0d623f7 | 798 | struct ifaddr *ifa = NULL; |
1c79356b A |
799 | struct llinfo_nd6 *ln; |
800 | struct rtentry *rt; | |
801 | struct sockaddr_dl *sdl; | |
802 | union nd_opts ndopts; | |
91447636 | 803 | struct timeval timenow; |
1c79356b A |
804 | |
805 | if (ip6->ip6_hlim != 255) { | |
9bccf70c A |
806 | nd6log((LOG_ERR, |
807 | "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n", | |
808 | ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), | |
809 | ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); | |
810 | goto bad; | |
1c79356b A |
811 | } |
812 | ||
813 | #ifndef PULLDOWN_TEST | |
91447636 | 814 | IP6_EXTHDR_CHECK(m, off, icmp6len, return); |
1c79356b A |
815 | nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off); |
816 | #else | |
817 | IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len); | |
818 | if (nd_na == NULL) { | |
819 | icmp6stat.icp6s_tooshort++; | |
820 | return; | |
821 | } | |
822 | #endif | |
6d2010ae | 823 | |
1c79356b A |
824 | flags = nd_na->nd_na_flags_reserved; |
825 | is_router = ((flags & ND_NA_FLAG_ROUTER) != 0); | |
826 | is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0); | |
827 | is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0); | |
828 | ||
6d2010ae A |
829 | taddr6 = nd_na->nd_na_target; |
830 | if (in6_setscope(&taddr6, ifp, NULL)) | |
831 | goto bad; /* XXX: impossible */ | |
1c79356b A |
832 | |
833 | if (IN6_IS_ADDR_MULTICAST(&taddr6)) { | |
9bccf70c | 834 | nd6log((LOG_ERR, |
1c79356b | 835 | "nd6_na_input: invalid target address %s\n", |
9bccf70c A |
836 | ip6_sprintf(&taddr6))); |
837 | goto bad; | |
1c79356b A |
838 | } |
839 | if (IN6_IS_ADDR_MULTICAST(&daddr6)) | |
840 | if (is_solicited) { | |
9bccf70c A |
841 | nd6log((LOG_ERR, |
842 | "nd6_na_input: a solicited adv is multicasted\n")); | |
843 | goto bad; | |
1c79356b A |
844 | } |
845 | ||
846 | icmp6len -= sizeof(*nd_na); | |
847 | nd6_option_init(nd_na + 1, icmp6len, &ndopts); | |
848 | if (nd6_options(&ndopts) < 0) { | |
9bccf70c A |
849 | nd6log((LOG_INFO, |
850 | "nd6_na_input: invalid ND option, ignored\n")); | |
851 | /* nd6_options have incremented stats */ | |
1c79356b A |
852 | goto freeit; |
853 | } | |
854 | ||
855 | if (ndopts.nd_opts_tgt_lladdr) { | |
856 | lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); | |
857 | lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; | |
858 | } | |
859 | ||
860 | ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6); | |
861 | ||
862 | /* | |
863 | * Target address matches one of my interface address. | |
864 | * | |
865 | * If my address is tentative, this means that there's somebody | |
866 | * already using the same address as mine. This indicates DAD failure. | |
867 | * This is defined in RFC 2462. | |
868 | * | |
869 | * Otherwise, process as defined in RFC 2461. | |
870 | */ | |
6d2010ae A |
871 | if (ifa != NULL) { |
872 | IFA_LOCK(ifa); | |
873 | if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE) { | |
874 | IFA_UNLOCK(ifa); | |
875 | nd6_dad_na_input(ifa, lladdr, lladdrlen); | |
876 | goto freeit; | |
877 | } | |
878 | IFA_UNLOCK(ifa); | |
1c79356b A |
879 | } |
880 | ||
55e303ae | 881 | /* Just for safety, maybe unnecessary. */ |
1c79356b A |
882 | if (ifa) { |
883 | log(LOG_ERR, | |
884 | "nd6_na_input: duplicate IP6 address %s\n", | |
885 | ip6_sprintf(&taddr6)); | |
886 | goto freeit; | |
887 | } | |
888 | ||
889 | if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { | |
9bccf70c | 890 | nd6log((LOG_INFO, |
1c79356b A |
891 | "nd6_na_input: lladdrlen mismatch for %s " |
892 | "(if %d, NA packet %d)\n", | |
9bccf70c A |
893 | ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2)); |
894 | goto bad; | |
1c79356b A |
895 | } |
896 | ||
897 | /* | |
6d2010ae A |
898 | * If no neighbor cache entry is found, NA SHOULD silently be |
899 | * discarded. | |
1c79356b | 900 | */ |
b0d623f7 A |
901 | if ((rt = nd6_lookup(&taddr6, 0, ifp, 0)) == NULL) |
902 | goto freeit; | |
903 | ||
904 | RT_LOCK_ASSERT_HELD(rt); | |
905 | if ((ln = rt->rt_llinfo) == NULL || | |
906 | (sdl = SDL(rt->rt_gateway)) == NULL) { | |
907 | RT_REMREF_LOCKED(rt); | |
908 | RT_UNLOCK(rt); | |
1c79356b | 909 | goto freeit; |
b0d623f7 | 910 | } |
1c79356b | 911 | |
91447636 | 912 | getmicrotime(&timenow); |
1c79356b A |
913 | if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { |
914 | /* | |
915 | * If the link-layer has address, and no lladdr option came, | |
916 | * discard the packet. | |
917 | */ | |
b0d623f7 A |
918 | if (ifp->if_addrlen && !lladdr) { |
919 | RT_REMREF_LOCKED(rt); | |
920 | RT_UNLOCK(rt); | |
1c79356b | 921 | goto freeit; |
b0d623f7 | 922 | } |
1c79356b A |
923 | |
924 | /* | |
925 | * Record link-layer address, and update the state. | |
926 | */ | |
927 | sdl->sdl_alen = ifp->if_addrlen; | |
928 | bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); | |
929 | if (is_solicited) { | |
930 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
9bccf70c | 931 | ln->ln_byhint = 0; |
b0d623f7 A |
932 | if (ln->ln_expire) { |
933 | lck_rw_lock_shared(nd_if_rwlock); | |
d1ecb069 A |
934 | ln->ln_expire = rt_expiry(rt, timenow.tv_sec, |
935 | nd_ifinfo[rt->rt_ifp->if_index].reachable); | |
b0d623f7 A |
936 | lck_rw_done(nd_if_rwlock); |
937 | } | |
9bccf70c | 938 | } else { |
1c79356b | 939 | ln->ln_state = ND6_LLINFO_STALE; |
d1ecb069 A |
940 | ln->ln_expire = rt_expiry(rt, timenow.tv_sec, |
941 | nd6_gctimer); | |
9bccf70c A |
942 | } |
943 | if ((ln->ln_router = is_router) != 0) { | |
944 | /* | |
945 | * This means a router's state has changed from | |
946 | * non-reachable to probably reachable, and might | |
947 | * affect the status of associated prefixes.. | |
948 | */ | |
b0d623f7 | 949 | RT_UNLOCK(rt); |
6d2010ae A |
950 | lck_mtx_lock(nd6_mutex); |
951 | pfxlist_onlink_check(); | |
952 | lck_mtx_unlock(nd6_mutex); | |
b0d623f7 | 953 | RT_LOCK(rt); |
9bccf70c | 954 | } |
1c79356b A |
955 | } else { |
956 | int llchange; | |
957 | ||
958 | /* | |
959 | * Check if the link-layer address has changed or not. | |
960 | */ | |
961 | if (!lladdr) | |
962 | llchange = 0; | |
963 | else { | |
964 | if (sdl->sdl_alen) { | |
965 | if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) | |
966 | llchange = 1; | |
967 | else | |
968 | llchange = 0; | |
969 | } else | |
970 | llchange = 1; | |
971 | } | |
972 | ||
973 | /* | |
974 | * This is VERY complex. Look at it with care. | |
975 | * | |
976 | * override solicit lladdr llchange action | |
977 | * (L: record lladdr) | |
978 | * | |
979 | * 0 0 n -- (2c) | |
980 | * 0 0 y n (2b) L | |
981 | * 0 0 y y (1) REACHABLE->STALE | |
982 | * 0 1 n -- (2c) *->REACHABLE | |
983 | * 0 1 y n (2b) L *->REACHABLE | |
984 | * 0 1 y y (1) REACHABLE->STALE | |
985 | * 1 0 n -- (2a) | |
986 | * 1 0 y n (2a) L | |
987 | * 1 0 y y (2a) L *->STALE | |
988 | * 1 1 n -- (2a) *->REACHABLE | |
989 | * 1 1 y n (2a) L *->REACHABLE | |
990 | * 1 1 y y (2a) L *->REACHABLE | |
991 | */ | |
6d2010ae | 992 | if (!is_override && (lladdr != NULL && llchange)) { /* (1) */ |
1c79356b A |
993 | /* |
994 | * If state is REACHABLE, make it STALE. | |
995 | * no other updates should be done. | |
996 | */ | |
9bccf70c | 997 | if (ln->ln_state == ND6_LLINFO_REACHABLE) { |
1c79356b | 998 | ln->ln_state = ND6_LLINFO_STALE; |
d1ecb069 A |
999 | ln->ln_expire = rt_expiry(rt, timenow.tv_sec, |
1000 | nd6_gctimer); | |
9bccf70c | 1001 | } |
b0d623f7 A |
1002 | RT_REMREF_LOCKED(rt); |
1003 | RT_UNLOCK(rt); | |
1c79356b A |
1004 | goto freeit; |
1005 | } else if (is_override /* (2a) */ | |
1006 | || (!is_override && (lladdr && !llchange)) /* (2b) */ | |
1007 | || !lladdr) { /* (2c) */ | |
1008 | /* | |
1009 | * Update link-local address, if any. | |
1010 | */ | |
1011 | if (lladdr) { | |
1012 | sdl->sdl_alen = ifp->if_addrlen; | |
1013 | bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * If solicited, make the state REACHABLE. | |
1018 | * If not solicited and the link-layer address was | |
1019 | * changed, make it STALE. | |
1020 | */ | |
1021 | if (is_solicited) { | |
1022 | ln->ln_state = ND6_LLINFO_REACHABLE; | |
9bccf70c | 1023 | ln->ln_byhint = 0; |
1c79356b | 1024 | if (ln->ln_expire) { |
b0d623f7 | 1025 | lck_rw_lock_shared(nd_if_rwlock); |
d1ecb069 A |
1026 | ln->ln_expire = |
1027 | rt_expiry(rt, timenow.tv_sec, | |
1028 | nd_ifinfo[ifp->if_index].reachable); | |
b0d623f7 | 1029 | lck_rw_done(nd_if_rwlock); |
1c79356b A |
1030 | } |
1031 | } else { | |
9bccf70c | 1032 | if (lladdr && llchange) { |
1c79356b | 1033 | ln->ln_state = ND6_LLINFO_STALE; |
d1ecb069 A |
1034 | ln->ln_expire = rt_expiry(rt, |
1035 | timenow.tv_sec, nd6_gctimer); | |
9bccf70c | 1036 | } |
1c79356b A |
1037 | } |
1038 | } | |
1039 | ||
1040 | if (ln->ln_router && !is_router) { | |
1041 | /* | |
1042 | * The peer dropped the router flag. | |
1043 | * Remove the sender from the Default Router List and | |
1044 | * update the Destination Cache entries. | |
1045 | */ | |
1046 | struct nd_defrouter *dr; | |
1047 | struct in6_addr *in6; | |
b0d623f7 | 1048 | struct ifnet *rt_ifp = rt->rt_ifp; |
1c79356b A |
1049 | |
1050 | in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr; | |
55e303ae A |
1051 | |
1052 | /* | |
1053 | * Lock to protect the default router list. | |
1054 | * XXX: this might be unnecessary, since this function | |
1055 | * is only called under the network software interrupt | |
6d2010ae | 1056 | * context. However, we keep it just for safety. |
55e303ae | 1057 | */ |
b0d623f7 | 1058 | RT_UNLOCK(rt); |
91447636 | 1059 | lck_mtx_lock(nd6_mutex); |
b0d623f7 | 1060 | dr = defrouter_lookup(in6, rt_ifp); |
91447636 | 1061 | if (dr) { |
6d2010ae A |
1062 | defrtrlist_del(dr); |
1063 | NDDR_REMREF(dr); | |
91447636 A |
1064 | lck_mtx_unlock(nd6_mutex); |
1065 | } | |
1066 | else { | |
1067 | lck_mtx_unlock(nd6_mutex); | |
b0d623f7 | 1068 | if (!ip6_forwarding && (ip6_accept_rtadv || (rt_ifp->if_eflags & IFEF_ACCEPT_RTADVD))) { |
91447636 A |
1069 | /* |
1070 | * Even if the neighbor is not in the default | |
1071 | * router list, the neighbor may be used | |
1072 | * as a next hop for some destinations | |
1073 | * (e.g. redirect case). So we must | |
1074 | * call rt6_flush explicitly. | |
1075 | */ | |
b0d623f7 | 1076 | rt6_flush(&ip6->ip6_src, rt_ifp); |
91447636 | 1077 | } |
1c79356b | 1078 | } |
b0d623f7 | 1079 | RT_LOCK(rt); |
1c79356b A |
1080 | } |
1081 | ln->ln_router = is_router; | |
1082 | } | |
b0d623f7 | 1083 | RT_LOCK_ASSERT_HELD(rt); |
1c79356b | 1084 | rt->rt_flags &= ~RTF_REJECT; |
6d2010ae A |
1085 | |
1086 | /* cache the gateway (sender HW) address */ | |
1087 | nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, TRUE); | |
1088 | ||
1089 | /* update the llinfo, send a queued packet if there is one */ | |
1c79356b | 1090 | ln->ln_asked = 0; |
b0d623f7 | 1091 | if (ln->ln_hold != NULL) { |
6d2010ae A |
1092 | struct mbuf *m_hold, *m_hold_next; |
1093 | struct sockaddr_in6 sin6; | |
1094 | ||
1095 | rtkey_to_sa6(rt, &sin6); | |
9bccf70c | 1096 | /* |
6d2010ae A |
1097 | * reset the ln_hold in advance, to explicitly |
1098 | * prevent a ln_hold lookup in nd6_output() | |
1099 | * (wouldn't happen, though...) | |
9bccf70c | 1100 | */ |
6d2010ae A |
1101 | for (m_hold = ln->ln_hold; |
1102 | m_hold; m_hold = m_hold_next) { | |
1103 | m_hold_next = m_hold->m_nextpkt; | |
1104 | m_hold->m_nextpkt = NULL; | |
1105 | /* | |
1106 | * we assume ifp is not a loopback here, so just set | |
1107 | * the 2nd argument as the 1st one. | |
1108 | */ | |
1109 | RT_UNLOCK(rt); | |
1110 | nd6_output(ifp, ifp, m_hold, &sin6, rt); | |
1111 | RT_LOCK_SPIN(rt); | |
1112 | } | |
1113 | ln->ln_hold = NULL; | |
1114 | ||
1c79356b | 1115 | } |
b0d623f7 A |
1116 | RT_REMREF_LOCKED(rt); |
1117 | RT_UNLOCK(rt); | |
1c79356b | 1118 | |
6d2010ae | 1119 | freeit: |
1c79356b | 1120 | m_freem(m); |
b0d623f7 | 1121 | if (ifa != NULL) |
6d2010ae | 1122 | IFA_REMREF(ifa); |
9bccf70c A |
1123 | return; |
1124 | ||
6d2010ae | 1125 | bad: |
9bccf70c A |
1126 | icmp6stat.icp6s_badna++; |
1127 | m_freem(m); | |
b0d623f7 | 1128 | if (ifa != NULL) |
6d2010ae | 1129 | IFA_REMREF(ifa); |
1c79356b A |
1130 | } |
1131 | ||
1132 | /* | |
1133 | * Neighbor advertisement output handling. | |
1134 | * | |
1135 | * Based on RFC 2461 | |
1136 | * | |
1137 | * the following items are not implemented yet: | |
1138 | * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD) | |
1139 | * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD) | |
6d2010ae A |
1140 | * |
1141 | * tlladdr - 1 if include target link-layer address | |
1142 | * sdl0 - sockaddr_dl (= proxy NA) or NULL | |
1c79356b A |
1143 | */ |
1144 | void | |
91447636 A |
1145 | nd6_na_output( |
1146 | struct ifnet *ifp, | |
6d2010ae | 1147 | const struct in6_addr *daddr6_0, |
91447636 | 1148 | const struct in6_addr *taddr6, |
b0d623f7 | 1149 | uint32_t flags, |
91447636 A |
1150 | int tlladdr, /* 1 if include target link-layer address */ |
1151 | struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */ | |
1c79356b A |
1152 | { |
1153 | struct mbuf *m; | |
1154 | struct ip6_hdr *ip6; | |
1155 | struct nd_neighbor_advert *nd_na; | |
6d2010ae | 1156 | struct ip6_moptions *im6o = NULL; |
55e303ae | 1157 | caddr_t mac = NULL; |
6d2010ae A |
1158 | struct route_in6 ro; |
1159 | struct in6_addr *src, src_storage, daddr6; | |
1160 | struct sockaddr_in6 dst_sa; | |
1161 | int icmp6len, maxlen, error; | |
1162 | struct ifnet *outif = NULL; | |
1163 | struct ip6_out_args ip6oa = { IFSCOPE_NONE, 0 }; | |
1164 | ||
1165 | bzero(&ro, sizeof(ro)); | |
1166 | ||
1167 | daddr6 = *daddr6_0; /* make a local copy for modification */ | |
1168 | ||
1169 | ip6oa.ip6oa_boundif = ifp->if_index; | |
1c79356b A |
1170 | |
1171 | /* estimate the size of message */ | |
1172 | maxlen = sizeof(*ip6) + sizeof(*nd_na); | |
1173 | maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7; | |
1174 | if (max_linkhdr + maxlen >= MCLBYTES) { | |
9bccf70c | 1175 | #if DIAGNOSTIC |
1c79356b A |
1176 | printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES " |
1177 | "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES); | |
1178 | #endif | |
1179 | return; | |
1180 | } | |
1181 | ||
2d21ac55 | 1182 | MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */ |
1c79356b A |
1183 | if (m && max_linkhdr + maxlen >= MHLEN) { |
1184 | MCLGET(m, M_DONTWAIT); | |
1185 | if ((m->m_flags & M_EXT) == 0) { | |
1186 | m_free(m); | |
1187 | m = NULL; | |
1188 | } | |
1189 | } | |
1190 | if (m == NULL) | |
1191 | return; | |
9bccf70c | 1192 | m->m_pkthdr.rcvif = NULL; |
1c79356b | 1193 | |
6d2010ae | 1194 | if (IN6_IS_ADDR_MULTICAST(&daddr6)) { |
1c79356b | 1195 | m->m_flags |= M_MCAST; |
6d2010ae A |
1196 | |
1197 | im6o = ip6_allocmoptions(M_DONTWAIT); | |
1198 | if (im6o == NULL) { | |
1199 | m_freem(m); | |
1200 | return; | |
1201 | } | |
1202 | ||
1203 | im6o->im6o_multicast_ifp = ifp; | |
1204 | im6o->im6o_multicast_hlim = 255; | |
1205 | im6o->im6o_multicast_loop = 0; | |
1c79356b A |
1206 | } |
1207 | ||
1208 | icmp6len = sizeof(*nd_na); | |
1209 | m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len; | |
55e303ae | 1210 | m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */ |
1c79356b A |
1211 | |
1212 | /* fill neighbor advertisement packet */ | |
1213 | ip6 = mtod(m, struct ip6_hdr *); | |
1214 | ip6->ip6_flow = 0; | |
1215 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
1216 | ip6->ip6_vfc |= IPV6_VERSION; | |
1217 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
1218 | ip6->ip6_hlim = 255; | |
6d2010ae | 1219 | if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) { |
1c79356b | 1220 | /* reply to DAD */ |
6d2010ae A |
1221 | daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL; |
1222 | daddr6.s6_addr16[1] = 0; | |
1223 | daddr6.s6_addr32[1] = 0; | |
1224 | daddr6.s6_addr32[2] = 0; | |
1225 | daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE; | |
1226 | if (in6_setscope(&daddr6, ifp, NULL)) | |
1227 | goto bad; | |
1228 | ||
1c79356b A |
1229 | flags &= ~ND_NA_FLAG_SOLICITED; |
1230 | } else | |
6d2010ae A |
1231 | ip6->ip6_dst = daddr6; |
1232 | ||
1233 | bzero(&dst_sa, sizeof(struct sockaddr_in6)); | |
1234 | dst_sa.sin6_family = AF_INET6; | |
1235 | dst_sa.sin6_len = sizeof(struct sockaddr_in6); | |
1236 | dst_sa.sin6_addr = daddr6; | |
1c79356b A |
1237 | |
1238 | /* | |
1239 | * Select a source whose scope is the same as that of the dest. | |
1240 | */ | |
6d2010ae A |
1241 | bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa)); |
1242 | src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &src_storage, | |
1243 | ip6oa.ip6oa_boundif, &error); | |
1244 | if (src == NULL) { | |
1245 | nd6log((LOG_DEBUG, "nd6_na_output: source can't be " | |
1246 | "determined: dst=%s, error=%d\n", | |
1247 | ip6_sprintf(&dst_sa.sin6_addr), error)); | |
1248 | goto bad; | |
1c79356b | 1249 | } |
6d2010ae | 1250 | ip6->ip6_src = *src; |
b0d623f7 | 1251 | |
1c79356b A |
1252 | nd_na = (struct nd_neighbor_advert *)(ip6 + 1); |
1253 | nd_na->nd_na_type = ND_NEIGHBOR_ADVERT; | |
1254 | nd_na->nd_na_code = 0; | |
1255 | nd_na->nd_na_target = *taddr6; | |
6d2010ae | 1256 | in6_clearscope(&nd_na->nd_na_target); /* XXX */ |
1c79356b A |
1257 | |
1258 | /* | |
1259 | * "tlladdr" indicates NS's condition for adding tlladdr or not. | |
1260 | * see nd6_ns_input() for details. | |
1261 | * Basically, if NS packet is sent to unicast/anycast addr, | |
1262 | * target lladdr option SHOULD NOT be included. | |
1263 | */ | |
1264 | if (tlladdr) { | |
1c79356b A |
1265 | /* |
1266 | * sdl0 != NULL indicates proxy NA. If we do proxy, use | |
1267 | * lladdr in sdl0. If we are not proxying (sending NA for | |
1268 | * my address) use lladdr configured for the interface. | |
1269 | */ | |
1270 | if (sdl0 == NULL) | |
1271 | mac = nd6_ifptomac(ifp); | |
1272 | else if (sdl0->sa_family == AF_LINK) { | |
1273 | struct sockaddr_dl *sdl; | |
1274 | sdl = (struct sockaddr_dl *)sdl0; | |
1275 | if (sdl->sdl_alen == ifp->if_addrlen) | |
1276 | mac = LLADDR(sdl); | |
1277 | } | |
1278 | } | |
1279 | if (tlladdr && mac) { | |
1280 | int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen; | |
1281 | struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1); | |
6d2010ae | 1282 | |
1c79356b A |
1283 | /* roundup to 8 bytes alignment! */ |
1284 | optlen = (optlen + 7) & ~7; | |
1285 | ||
1286 | m->m_pkthdr.len += optlen; | |
1287 | m->m_len += optlen; | |
1288 | icmp6len += optlen; | |
1289 | bzero((caddr_t)nd_opt, optlen); | |
1290 | nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; | |
1291 | nd_opt->nd_opt_len = optlen >> 3; | |
1292 | bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen); | |
1293 | } else | |
1294 | flags &= ~ND_NA_FLAG_OVERRIDE; | |
1295 | ||
1296 | ip6->ip6_plen = htons((u_short)icmp6len); | |
1297 | nd_na->nd_na_flags_reserved = flags; | |
1298 | nd_na->nd_na_cksum = 0; | |
1299 | nd_na->nd_na_cksum = | |
1300 | in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len); | |
1301 | ||
1302 | #if IPSEC | |
1303 | /* Don't lookup socket */ | |
9bccf70c A |
1304 | if (ipsec_bypass == 0) |
1305 | (void)ipsec_setsocket(m, NULL); | |
1c79356b | 1306 | #endif |
6d2010ae | 1307 | ip6_output(m, NULL, NULL, IPV6_OUTARGS, im6o, &outif, &ip6oa); |
1c79356b A |
1308 | if (outif) { |
1309 | icmp6_ifstat_inc(outif, ifs6_out_msg); | |
1310 | icmp6_ifstat_inc(outif, ifs6_out_neighboradvert); | |
6d2010ae | 1311 | ifnet_release(outif); |
1c79356b A |
1312 | } |
1313 | icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++; | |
6d2010ae A |
1314 | |
1315 | if (im6o != NULL) | |
1316 | IM6O_REMREF(im6o); | |
1317 | if (ro.ro_rt) { | |
1318 | rtfree(ro.ro_rt); | |
1319 | } | |
1320 | return; | |
1321 | ||
1322 | bad: | |
1323 | if (im6o != NULL) | |
1324 | IM6O_REMREF(im6o); | |
1325 | if (ro.ro_rt) { | |
1326 | rtfree(ro.ro_rt); | |
1327 | } | |
1328 | m_freem(m); | |
1329 | return; | |
1c79356b A |
1330 | } |
1331 | ||
1332 | caddr_t | |
91447636 A |
1333 | nd6_ifptomac( |
1334 | struct ifnet *ifp) | |
1c79356b | 1335 | { |
6d2010ae A |
1336 | switch (ifp->if_type) { |
1337 | case IFT_ARCNET: | |
1338 | case IFT_ETHER: | |
1339 | case IFT_IEEE8023ADLAG: | |
1340 | case IFT_FDDI: | |
1341 | case IFT_IEEE1394: | |
1342 | #ifdef IFT_L2VLAN | |
1343 | case IFT_L2VLAN: | |
1344 | #endif | |
1345 | #ifdef IFT_IEEE80211 | |
1346 | case IFT_IEEE80211: | |
1347 | #endif | |
1348 | #ifdef IFT_CARP | |
1349 | case IFT_CARP: | |
1350 | #endif | |
1351 | case IFT_BRIDGE: | |
1352 | case IFT_ISO88025: | |
1353 | return ((caddr_t)ifnet_lladdr(ifp)); | |
1354 | default: | |
1355 | return NULL; | |
1356 | } | |
1c79356b A |
1357 | } |
1358 | ||
1359 | TAILQ_HEAD(dadq_head, dadq); | |
1360 | struct dadq { | |
6d2010ae A |
1361 | decl_lck_mtx_data(, dad_lock); |
1362 | u_int32_t dad_refcount; /* reference count */ | |
1363 | int dad_attached; | |
1c79356b A |
1364 | TAILQ_ENTRY(dadq) dad_list; |
1365 | struct ifaddr *dad_ifa; | |
1366 | int dad_count; /* max NS to send */ | |
1367 | int dad_ns_tcount; /* # of trials to send NS */ | |
1368 | int dad_ns_ocount; /* NS sent so far */ | |
1369 | int dad_ns_icount; | |
1370 | int dad_na_icount; | |
6d2010ae | 1371 | int dad_na_ixcount; /* Count of IFDISABLED eligible NA rx'd */ |
1c79356b A |
1372 | }; |
1373 | ||
1374 | static struct dadq_head dadq; | |
6d2010ae A |
1375 | |
1376 | void | |
1377 | nd6_nbr_init(void) | |
1378 | { | |
1379 | TAILQ_INIT(&dadq); | |
1380 | ||
1381 | dad_size = sizeof (struct dadq); | |
1382 | dad_zone = zinit(dad_size, DAD_ZONE_MAX * dad_size, 0, DAD_ZONE_NAME); | |
1383 | if (dad_zone == NULL) { | |
1384 | panic("%s: failed allocating %s", __func__, DAD_ZONE_NAME); | |
1385 | /* NOTREACHED */ | |
1386 | } | |
1387 | zone_change(dad_zone, Z_EXPAND, TRUE); | |
1388 | zone_change(dad_zone, Z_CALLERACCT, FALSE); | |
1389 | } | |
1c79356b A |
1390 | |
1391 | static struct dadq * | |
6d2010ae | 1392 | nd6_dad_find(struct ifaddr *ifa) |
1c79356b A |
1393 | { |
1394 | struct dadq *dp; | |
6d2010ae | 1395 | |
91447636 | 1396 | lck_mtx_lock(dad6_mutex); |
1c79356b | 1397 | for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) { |
6d2010ae | 1398 | DAD_LOCK_SPIN(dp); |
91447636 | 1399 | if (dp->dad_ifa == ifa) { |
6d2010ae A |
1400 | DAD_ADDREF_LOCKED(dp); |
1401 | DAD_UNLOCK(dp); | |
91447636 | 1402 | lck_mtx_unlock(dad6_mutex); |
6d2010ae | 1403 | return (dp); |
91447636 | 1404 | } |
6d2010ae | 1405 | DAD_UNLOCK(dp); |
1c79356b | 1406 | } |
91447636 | 1407 | lck_mtx_unlock(dad6_mutex); |
6d2010ae | 1408 | return (NULL); |
1c79356b A |
1409 | } |
1410 | ||
9bccf70c | 1411 | void |
91447636 A |
1412 | nd6_dad_stoptimer( |
1413 | struct ifaddr *ifa) | |
9bccf70c A |
1414 | { |
1415 | ||
91447636 | 1416 | untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa); |
9bccf70c | 1417 | } |
9bccf70c | 1418 | |
1c79356b | 1419 | /* |
6d2010ae | 1420 | * Start Duplicate Address Detection (DAD) for specified interface address. |
1c79356b A |
1421 | */ |
1422 | void | |
91447636 A |
1423 | nd6_dad_start( |
1424 | struct ifaddr *ifa, | |
2d21ac55 | 1425 | int *tick_delay) /* minimum delay ticks for IFF_UP event */ |
1c79356b A |
1426 | { |
1427 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; | |
1428 | struct dadq *dp; | |
1c79356b | 1429 | |
1c79356b A |
1430 | /* |
1431 | * If we don't need DAD, don't do it. | |
1432 | * There are several cases: | |
1433 | * - DAD is disabled (ip6_dad_count == 0) | |
1434 | * - the interface address is anycast | |
1435 | */ | |
6d2010ae | 1436 | IFA_LOCK(&ia->ia_ifa); |
1c79356b A |
1437 | if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) { |
1438 | log(LOG_DEBUG, | |
1439 | "nd6_dad_start: called with non-tentative address " | |
1440 | "%s(%s)\n", | |
1441 | ip6_sprintf(&ia->ia_addr.sin6_addr), | |
1442 | ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); | |
6d2010ae | 1443 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1444 | return; |
1445 | } | |
1446 | if (ia->ia6_flags & IN6_IFF_ANYCAST) { | |
1447 | ia->ia6_flags &= ~IN6_IFF_TENTATIVE; | |
6d2010ae | 1448 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1449 | return; |
1450 | } | |
1451 | if (!ip6_dad_count) { | |
1452 | ia->ia6_flags &= ~IN6_IFF_TENTATIVE; | |
6d2010ae | 1453 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1454 | return; |
1455 | } | |
6d2010ae A |
1456 | IFA_UNLOCK(&ia->ia_ifa); |
1457 | if (ifa->ifa_ifp == NULL) | |
1c79356b | 1458 | panic("nd6_dad_start: ifa->ifa_ifp == NULL"); |
6d2010ae | 1459 | if (!(ifa->ifa_ifp->if_flags & IFF_UP)) { |
1c79356b | 1460 | return; |
6d2010ae A |
1461 | } |
1462 | if ((dp = nd6_dad_find(ifa)) != NULL) { | |
1463 | DAD_REMREF(dp); | |
1c79356b A |
1464 | /* DAD already in progress */ |
1465 | return; | |
1466 | } | |
1467 | ||
6d2010ae | 1468 | dp = zalloc(dad_zone); |
1c79356b A |
1469 | if (dp == NULL) { |
1470 | log(LOG_ERR, "nd6_dad_start: memory allocation failed for " | |
1471 | "%s(%s)\n", | |
1472 | ip6_sprintf(&ia->ia_addr.sin6_addr), | |
1473 | ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); | |
1474 | return; | |
1475 | } | |
6d2010ae A |
1476 | bzero(dp, dad_size); |
1477 | lck_mtx_init(&dp->dad_lock, ifa_mtx_grp, ifa_mtx_attr); | |
1478 | ||
1479 | /* Callee adds one reference for us */ | |
1480 | dp = nd6_dad_attach(dp, ifa); | |
1c79356b | 1481 | |
9bccf70c A |
1482 | nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp), |
1483 | ip6_sprintf(&ia->ia_addr.sin6_addr))); | |
1c79356b A |
1484 | |
1485 | /* | |
1486 | * Send NS packet for DAD, ip6_dad_count times. | |
1487 | * Note that we must delay the first transmission, if this is the | |
1488 | * first packet to be sent from the interface after interface | |
1489 | * (re)initialization. | |
1490 | */ | |
2d21ac55 | 1491 | if (tick_delay == NULL) { |
b0d623f7 | 1492 | u_int32_t retrans; |
1c79356b | 1493 | nd6_dad_ns_output(dp, ifa); |
b0d623f7 A |
1494 | lck_rw_lock_shared(nd_if_rwlock); |
1495 | retrans = nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000; | |
1496 | lck_rw_done(nd_if_rwlock); | |
1497 | timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans); | |
1c79356b A |
1498 | } else { |
1499 | int ntick; | |
1500 | ||
2d21ac55 | 1501 | if (*tick_delay == 0) |
1c79356b A |
1502 | ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz); |
1503 | else | |
2d21ac55 A |
1504 | ntick = *tick_delay + random() % (hz / 2); |
1505 | *tick_delay = ntick; | |
91447636 | 1506 | timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, |
1c79356b A |
1507 | ntick); |
1508 | } | |
6d2010ae A |
1509 | |
1510 | DAD_REMREF(dp); /* drop our reference */ | |
1511 | } | |
1512 | ||
1513 | static struct dadq * | |
1514 | nd6_dad_attach(struct dadq *dp, struct ifaddr *ifa) | |
1515 | { | |
1516 | lck_mtx_lock(dad6_mutex); | |
1517 | DAD_LOCK(dp); | |
1518 | dp->dad_ifa = ifa; | |
1519 | IFA_ADDREF(ifa); /* for dad_ifa */ | |
1520 | dp->dad_count = ip6_dad_count; | |
1521 | dp->dad_ns_icount = dp->dad_na_icount = 0; | |
1522 | dp->dad_ns_ocount = dp->dad_ns_tcount = 0; | |
1523 | dp->dad_na_ixcount = 0; | |
1524 | VERIFY(!dp->dad_attached); | |
1525 | dp->dad_attached = 1; | |
1526 | DAD_ADDREF_LOCKED(dp); /* for caller */ | |
1527 | DAD_ADDREF_LOCKED(dp); /* for dadq_head list */ | |
1528 | TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list); | |
1529 | DAD_UNLOCK(dp); | |
1530 | lck_mtx_unlock(dad6_mutex); | |
1531 | ||
1532 | return (dp); | |
1533 | } | |
1534 | ||
1535 | static void | |
1536 | nd6_dad_detach(struct dadq *dp, struct ifaddr *ifa) | |
1537 | { | |
1538 | int detached; | |
1539 | ||
1540 | lck_mtx_lock(dad6_mutex); | |
1541 | DAD_LOCK(dp); | |
1542 | if ((detached = dp->dad_attached)) { | |
1543 | VERIFY(dp->dad_ifa == ifa); | |
1544 | TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list); | |
1545 | dp->dad_list.tqe_next = NULL; | |
1546 | dp->dad_list.tqe_prev = NULL; | |
1547 | dp->dad_attached = 0; | |
1548 | } | |
1549 | DAD_UNLOCK(dp); | |
1550 | lck_mtx_unlock(dad6_mutex); | |
1551 | if (detached) { | |
1552 | DAD_REMREF(dp); /* drop dadq_head reference */ | |
1553 | } | |
1c79356b A |
1554 | } |
1555 | ||
9bccf70c A |
1556 | /* |
1557 | * terminate DAD unconditionally. used for address removals. | |
1558 | */ | |
1559 | void | |
6d2010ae | 1560 | nd6_dad_stop(struct ifaddr *ifa) |
9bccf70c A |
1561 | { |
1562 | struct dadq *dp; | |
1563 | ||
9bccf70c A |
1564 | dp = nd6_dad_find(ifa); |
1565 | if (!dp) { | |
1566 | /* DAD wasn't started yet */ | |
1567 | return; | |
1568 | } | |
1569 | ||
91447636 | 1570 | untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa); |
9bccf70c | 1571 | |
6d2010ae A |
1572 | nd6_dad_detach(dp, ifa); |
1573 | DAD_REMREF(dp); /* drop our reference */ | |
9bccf70c A |
1574 | } |
1575 | ||
1576 | ||
1c79356b | 1577 | static void |
6d2010ae | 1578 | nd6_unsol_na_output(struct ifaddr *ifa) |
1c79356b | 1579 | { |
1c79356b | 1580 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; |
6d2010ae A |
1581 | struct ifnet *ifp = ifa->ifa_ifp; |
1582 | struct in6_addr saddr6, taddr6; | |
1583 | ||
1584 | if ((ifp->if_flags & IFF_UP) == 0 || | |
1585 | (ifp->if_flags & IFF_RUNNING) == 0) | |
1586 | return; | |
1587 | ||
1588 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
1589 | taddr6 = ia->ia_addr.sin6_addr; | |
1590 | IFA_UNLOCK(&ia->ia_ifa); | |
1591 | if (in6_setscope(&taddr6, ifp, NULL) != 0) | |
1592 | return; | |
1593 | saddr6 = in6addr_linklocal_allnodes; | |
1594 | if (in6_setscope(&saddr6, ifp, NULL) != 0) | |
1595 | return; | |
1596 | ||
1597 | nd6log((LOG_INFO, "%s: sending unsolicited NA\n", | |
1598 | if_name(ifa->ifa_ifp))); | |
1599 | ||
1600 | nd6_na_output(ifp, &saddr6, &taddr6, ND_NA_FLAG_OVERRIDE, 1, NULL); | |
1601 | } | |
1602 | ||
1603 | static void | |
1604 | nd6_dad_timer(struct ifaddr *ifa) | |
1605 | { | |
1606 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; | |
1607 | struct dadq *dp = NULL; | |
1c79356b | 1608 | |
1c79356b A |
1609 | /* Sanity check */ |
1610 | if (ia == NULL) { | |
1611 | log(LOG_ERR, "nd6_dad_timer: called with null parameter\n"); | |
1612 | goto done; | |
1613 | } | |
1614 | dp = nd6_dad_find(ifa); | |
1615 | if (dp == NULL) { | |
1616 | log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n"); | |
1617 | goto done; | |
1618 | } | |
6d2010ae | 1619 | IFA_LOCK(&ia->ia_ifa); |
1c79356b A |
1620 | if (ia->ia6_flags & IN6_IFF_DUPLICATED) { |
1621 | log(LOG_ERR, "nd6_dad_timer: called with duplicated address " | |
1622 | "%s(%s)\n", | |
1623 | ip6_sprintf(&ia->ia_addr.sin6_addr), | |
1624 | ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); | |
6d2010ae | 1625 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1626 | goto done; |
1627 | } | |
1628 | if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) { | |
1629 | log(LOG_ERR, "nd6_dad_timer: called with non-tentative address " | |
1630 | "%s(%s)\n", | |
1631 | ip6_sprintf(&ia->ia_addr.sin6_addr), | |
1632 | ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???"); | |
6d2010ae | 1633 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1634 | goto done; |
1635 | } | |
6d2010ae | 1636 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1637 | |
1638 | /* timeouted with IFF_{RUNNING,UP} check */ | |
6d2010ae | 1639 | DAD_LOCK(dp); |
1c79356b | 1640 | if (dp->dad_ns_tcount > dad_maxtry) { |
6d2010ae | 1641 | DAD_UNLOCK(dp); |
9bccf70c A |
1642 | nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n", |
1643 | if_name(ifa->ifa_ifp))); | |
1c79356b | 1644 | |
6d2010ae | 1645 | nd6_dad_detach(dp, ifa); |
1c79356b A |
1646 | goto done; |
1647 | } | |
1648 | ||
1649 | /* Need more checks? */ | |
1650 | if (dp->dad_ns_ocount < dp->dad_count) { | |
b0d623f7 | 1651 | u_int32_t retrans; |
6d2010ae | 1652 | DAD_UNLOCK(dp); |
1c79356b A |
1653 | /* |
1654 | * We have more NS to go. Send NS packet for DAD. | |
1655 | */ | |
1656 | nd6_dad_ns_output(dp, ifa); | |
b0d623f7 A |
1657 | lck_rw_lock_shared(nd_if_rwlock); |
1658 | retrans = nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000; | |
1659 | lck_rw_done(nd_if_rwlock); | |
1660 | timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans); | |
1c79356b A |
1661 | } else { |
1662 | /* | |
1663 | * We have transmitted sufficient number of DAD packets. | |
1664 | * See what we've got. | |
1665 | */ | |
1666 | int duplicate; | |
1667 | ||
1668 | duplicate = 0; | |
1669 | ||
1670 | if (dp->dad_na_icount) { | |
1671 | /* | |
1672 | * the check is in nd6_dad_na_input(), | |
1673 | * but just in case | |
1674 | */ | |
1675 | duplicate++; | |
1676 | } | |
1677 | ||
1678 | if (dp->dad_ns_icount) { | |
1c79356b A |
1679 | /* We've seen NS, means DAD has failed. */ |
1680 | duplicate++; | |
1c79356b | 1681 | } |
6d2010ae | 1682 | DAD_UNLOCK(dp); |
1c79356b A |
1683 | |
1684 | if (duplicate) { | |
1685 | /* (*dp) will be freed in nd6_dad_duplicated() */ | |
6d2010ae | 1686 | nd6_dad_duplicated(ifa, TRUE); |
1c79356b A |
1687 | } else { |
1688 | /* | |
1689 | * We are done with DAD. No NA came, no NS came. | |
6d2010ae | 1690 | * No duplicate address found. |
1c79356b | 1691 | */ |
6d2010ae | 1692 | IFA_LOCK_SPIN(&ia->ia_ifa); |
1c79356b | 1693 | ia->ia6_flags &= ~IN6_IFF_TENTATIVE; |
6d2010ae | 1694 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b | 1695 | |
9bccf70c | 1696 | nd6log((LOG_DEBUG, |
1c79356b A |
1697 | "%s: DAD complete for %s - no duplicates found\n", |
1698 | if_name(ifa->ifa_ifp), | |
9bccf70c | 1699 | ip6_sprintf(&ia->ia_addr.sin6_addr))); |
6d2010ae A |
1700 | /* |
1701 | * Send an Unsolicited Neighbor Advertisement so that | |
1702 | * other machines on the network are aware of us | |
1703 | * (important when we are waking from sleep). | |
1704 | */ | |
1705 | nd6_unsol_na_output(ifa); | |
91447636 | 1706 | in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia); |
6d2010ae | 1707 | nd6_dad_detach(dp, ifa); |
1c79356b A |
1708 | } |
1709 | } | |
1710 | ||
1711 | done: | |
6d2010ae A |
1712 | if (dp != NULL) |
1713 | DAD_REMREF(dp); /* drop our reference */ | |
1c79356b A |
1714 | } |
1715 | ||
1716 | void | |
6d2010ae | 1717 | nd6_dad_duplicated(struct ifaddr *ifa, boolean_t dontignhwdup) |
1c79356b A |
1718 | { |
1719 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; | |
1720 | struct dadq *dp; | |
6d2010ae A |
1721 | struct ifnet *ifp = ifa->ifa_ifp; |
1722 | int hwdupposs; | |
1c79356b A |
1723 | |
1724 | dp = nd6_dad_find(ifa); | |
1725 | if (dp == NULL) { | |
1726 | log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n"); | |
1727 | return; | |
1728 | } | |
1729 | ||
6d2010ae A |
1730 | hwdupposs = 0; |
1731 | IFA_LOCK(&ia->ia_ifa); | |
1732 | DAD_LOCK(dp); | |
9bccf70c | 1733 | log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: " |
6d2010ae A |
1734 | "NS in/out=%d/%d, NA in=%d inx=%d\n", |
1735 | if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr), | |
1736 | dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount, | |
1737 | dp->dad_na_ixcount); | |
1738 | hwdupposs = dp->dad_na_ixcount; | |
1739 | DAD_UNLOCK(dp); | |
1c79356b A |
1740 | ia->ia6_flags &= ~IN6_IFF_TENTATIVE; |
1741 | ia->ia6_flags |= IN6_IFF_DUPLICATED; | |
6d2010ae | 1742 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1743 | |
1744 | /* We are done with DAD, with duplicated address found. (failure) */ | |
91447636 | 1745 | untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa); |
9bccf70c | 1746 | |
6d2010ae | 1747 | IFA_LOCK(&ia->ia_ifa); |
1c79356b | 1748 | log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n", |
6d2010ae | 1749 | if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr)); |
1c79356b | 1750 | log(LOG_ERR, "%s: manual intervention required\n", |
6d2010ae A |
1751 | if_name(ifp)); |
1752 | IFA_UNLOCK(&ia->ia_ifa); | |
1753 | ||
1754 | if (hwdupposs || | |
1755 | (dontignhwdup && IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))) { | |
1756 | log(LOG_ERR, "%s: possible hardware address duplication " | |
1757 | "detected, disable IPv6\n", if_name(ifp)); | |
1758 | ||
1759 | lck_rw_lock_shared(nd_if_rwlock); | |
1760 | nd_ifinfo[ifp->if_index].flags |= | |
1761 | ND6_IFF_IFDISABLED; | |
1762 | lck_rw_done(nd_if_rwlock); | |
1763 | } | |
1764 | ||
1765 | /* Send an event to the configuration agent so that the | |
1766 | * duplicate address will be notified to the user and will | |
1767 | * be removed. | |
1768 | */ | |
1769 | in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia); | |
1770 | nd6_dad_detach(dp, ifa); | |
1771 | DAD_REMREF(dp); /* drop our reference */ | |
1c79356b A |
1772 | } |
1773 | ||
1774 | static void | |
6d2010ae | 1775 | nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa) |
1c79356b A |
1776 | { |
1777 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; | |
1778 | struct ifnet *ifp = ifa->ifa_ifp; | |
6d2010ae | 1779 | struct in6_addr taddr6; |
1c79356b | 1780 | |
6d2010ae | 1781 | DAD_LOCK(dp); |
1c79356b A |
1782 | dp->dad_ns_tcount++; |
1783 | if ((ifp->if_flags & IFF_UP) == 0) { | |
6d2010ae | 1784 | DAD_UNLOCK(dp); |
1c79356b A |
1785 | return; |
1786 | } | |
1787 | if ((ifp->if_flags & IFF_RUNNING) == 0) { | |
6d2010ae | 1788 | DAD_UNLOCK(dp); |
1c79356b A |
1789 | return; |
1790 | } | |
1791 | ||
1792 | dp->dad_ns_ocount++; | |
6d2010ae A |
1793 | DAD_UNLOCK(dp); |
1794 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
1795 | taddr6 = ia->ia_addr.sin6_addr; | |
1796 | IFA_UNLOCK(&ia->ia_ifa); | |
1797 | nd6_ns_output(ifp, NULL, &taddr6, NULL, 1); | |
1c79356b A |
1798 | } |
1799 | ||
1800 | static void | |
6d2010ae | 1801 | nd6_dad_ns_input(struct ifaddr *ifa) |
1c79356b | 1802 | { |
1c79356b A |
1803 | struct dadq *dp; |
1804 | int duplicate; | |
6d2010ae | 1805 | struct ifnet *ifp; |
1c79356b | 1806 | |
6d2010ae | 1807 | if (ifa == NULL) |
1c79356b A |
1808 | panic("ifa == NULL in nd6_dad_ns_input"); |
1809 | ||
6d2010ae | 1810 | ifp = ifa->ifa_ifp; |
1c79356b A |
1811 | duplicate = 0; |
1812 | dp = nd6_dad_find(ifa); | |
1813 | ||
1c79356b A |
1814 | /* Quickhack - completely ignore DAD NS packets */ |
1815 | if (dad_ignore_ns) { | |
6d2010ae A |
1816 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; |
1817 | IFA_LOCK(&ia->ia_ifa); | |
9bccf70c A |
1818 | nd6log((LOG_INFO, |
1819 | "nd6_dad_ns_input: ignoring DAD NS packet for " | |
6d2010ae | 1820 | "address %s(%s)\n", ip6_sprintf(&ia->ia_addr.sin6_addr), |
9bccf70c | 1821 | if_name(ifa->ifa_ifp))); |
6d2010ae | 1822 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
1823 | return; |
1824 | } | |
1825 | ||
1826 | /* | |
1827 | * if I'm yet to start DAD, someone else started using this address | |
1828 | * first. I have a duplicate and you win. | |
1829 | */ | |
6d2010ae A |
1830 | if (dp != NULL) |
1831 | DAD_LOCK(dp); | |
1832 | if (dp == NULL || dp->dad_ns_ocount == 0) | |
1c79356b A |
1833 | duplicate++; |
1834 | ||
1835 | /* XXX more checks for loopback situation - see nd6_dad_timer too */ | |
1836 | ||
1837 | if (duplicate) { | |
6d2010ae A |
1838 | if (dp != NULL) { |
1839 | DAD_UNLOCK(dp); | |
1840 | DAD_REMREF(dp); | |
1841 | dp = NULL; | |
1842 | } | |
1843 | nd6_dad_duplicated(ifa, TRUE); | |
1844 | } else if (dp != NULL) { | |
1c79356b A |
1845 | /* |
1846 | * not sure if I got a duplicate. | |
1847 | * increment ns count and see what happens. | |
1848 | */ | |
6d2010ae A |
1849 | dp->dad_ns_icount++; |
1850 | DAD_UNLOCK(dp); | |
1851 | DAD_REMREF(dp); | |
1c79356b A |
1852 | } |
1853 | } | |
1854 | ||
1855 | static void | |
6d2010ae | 1856 | nd6_dad_na_input(struct ifaddr *ifa, caddr_t lladdr, int lladdrlen) |
1c79356b | 1857 | { |
6d2010ae | 1858 | struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; |
1c79356b | 1859 | struct dadq *dp; |
6d2010ae | 1860 | int hwdupposs; |
1c79356b | 1861 | |
6d2010ae | 1862 | if (ifa == NULL) |
1c79356b A |
1863 | panic("ifa == NULL in nd6_dad_na_input"); |
1864 | ||
1865 | dp = nd6_dad_find(ifa); | |
6d2010ae A |
1866 | if (dp == NULL) { |
1867 | log(LOG_ERR, "nd6_dad_na_input: DAD structure not found\n"); | |
1868 | return; | |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * If the address is a link-local address formed from an interface | |
1873 | * identifier based on the hardware address which is supposed to be | |
1874 | * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP | |
1875 | * operation on the interface SHOULD be disabled according to RFC 4862, | |
1876 | * section 5.4.5, but here we decide not to disable if the target | |
1877 | * hardware address is not also ours, which is a transitory possibility | |
1878 | * in the presence of network-resident sleep proxies on the local link. | |
1879 | */ | |
1880 | hwdupposs = 0; | |
1881 | IFA_LOCK(ifa); | |
1882 | if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { | |
1883 | struct ifnet *ifp; | |
1884 | struct in6_addr in6; | |
1885 | ||
1886 | IFA_UNLOCK(ifa); | |
1887 | ifp = ifa->ifa_ifp; | |
1888 | ||
1889 | /* | |
1890 | * To avoid over-reaction, we only apply this logic when we are | |
1891 | * very sure that hardware addresses are supposed to be unique. | |
1892 | */ | |
1893 | switch (ifp->if_type) { | |
1894 | case IFT_BRIDGE: | |
1895 | case IFT_ETHER: | |
1896 | case IFT_FDDI: | |
1897 | case IFT_ATM: | |
1898 | case IFT_IEEE1394: | |
1899 | #ifdef IFT_IEEE80211 | |
1900 | case IFT_IEEE80211: | |
1901 | #endif | |
1902 | /* Check if our hardware address matches the target */ | |
1903 | if (lladdr != NULL && lladdrlen > 0) { | |
1904 | struct ifaddr *llifa; | |
1905 | struct sockaddr_dl *sdl; | |
1906 | ||
1907 | llifa = ifp->if_lladdr; | |
1908 | IFA_LOCK(llifa); | |
1909 | sdl = (struct sockaddr_dl *)llifa->ifa_addr; | |
1910 | if (lladdrlen == sdl->sdl_alen || | |
1911 | bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) | |
1912 | hwdupposs = 1; | |
1913 | IFA_UNLOCK(llifa); | |
1914 | } | |
1915 | in6 = ia->ia_addr.sin6_addr; | |
1916 | if (in6_get_hw_ifid(ifp, &in6) != 0) | |
1917 | break; | |
1918 | /* | |
1919 | * Apply this logic only to the EUI-64 form of | |
1920 | * link-local interface identifiers. | |
1921 | */ | |
1922 | IFA_LOCK(ifa); | |
1923 | if (hwdupposs && | |
1924 | !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { | |
1925 | hwdupposs = 0; | |
1926 | } else if (lladdr == NULL && | |
1927 | IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) { | |
1928 | /* | |
1929 | * We received a NA with no target link-layer | |
1930 | * address option. This means that someone else | |
1931 | * has our address. Mark it as a hardware | |
1932 | * duplicate so we disable IPv6 later on. | |
1933 | */ | |
1934 | hwdupposs = 1; | |
1935 | } | |
1936 | IFA_UNLOCK(ifa); | |
1937 | break; | |
1938 | default: | |
1939 | break; | |
1940 | } | |
1941 | } else { | |
1942 | IFA_UNLOCK(ifa); | |
1943 | } | |
1944 | ||
1945 | DAD_LOCK_SPIN(dp); | |
1946 | dp->dad_na_icount++; | |
1947 | if (hwdupposs) | |
1948 | dp->dad_na_ixcount++; | |
1949 | DAD_UNLOCK(dp); | |
1950 | DAD_REMREF(dp); | |
1951 | ||
1c79356b | 1952 | /* remove the address. */ |
6d2010ae A |
1953 | nd6_dad_duplicated(ifa, FALSE); |
1954 | } | |
1955 | ||
1956 | static void | |
1957 | dad_addref(struct dadq *dp, int locked) | |
1958 | { | |
1959 | if (!locked) | |
1960 | DAD_LOCK_SPIN(dp); | |
1961 | else | |
1962 | DAD_LOCK_ASSERT_HELD(dp); | |
1963 | ||
1964 | if (++dp->dad_refcount == 0) { | |
1965 | panic("%s: dad %p wraparound refcnt\n", __func__, dp); | |
1966 | /* NOTREACHED */ | |
1967 | } | |
1968 | if (!locked) | |
1969 | DAD_UNLOCK(dp); | |
1970 | } | |
1971 | ||
1972 | static void | |
1973 | dad_remref(struct dadq *dp) | |
1974 | { | |
1975 | struct ifaddr *ifa; | |
1976 | ||
1977 | DAD_LOCK_SPIN(dp); | |
1978 | if (dp->dad_refcount == 0) | |
1979 | panic("%s: dad %p negative refcnt\n", __func__, dp); | |
1980 | --dp->dad_refcount; | |
1981 | if (dp->dad_refcount > 0) { | |
1982 | DAD_UNLOCK(dp); | |
1983 | return; | |
1984 | } | |
1985 | DAD_UNLOCK(dp); | |
1986 | ||
1987 | if (dp->dad_attached || | |
1988 | dp->dad_list.tqe_next != NULL || dp->dad_list.tqe_prev != NULL) { | |
1989 | panic("%s: attached dad=%p is being freed", __func__, dp); | |
1990 | /* NOTREACHED */ | |
1991 | } | |
1992 | ||
1993 | if ((ifa = dp->dad_ifa) != NULL) { | |
1994 | IFA_REMREF(ifa); /* drop dad_ifa reference */ | |
1995 | dp->dad_ifa = NULL; | |
1996 | } | |
1997 | ||
1998 | lck_mtx_destroy(&dp->dad_lock, ifa_mtx_grp); | |
1999 | zfree(dad_zone, dp); | |
2000 | } | |
2001 | ||
2002 | void | |
2003 | nd6_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen) | |
2004 | { | |
2005 | /* Nothing more to do if it's disabled */ | |
2006 | if (nd6_llreach_base == 0) | |
2007 | return; | |
2008 | ||
2009 | ifnet_llreach_set_reachable(ifp, ETHERTYPE_IPV6, addr, alen); | |
1c79356b | 2010 | } |