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