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