1 /* $OpenBSD: nd6_nbr.c,v 1.142 2023/01/06 14:17:15 kn Exp $ */
2 /* $KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei Exp $ */
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
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/ioctl.h>
42 #include <sys/syslog.h>
43 #include <sys/queue.h>
44 #include <sys/timeout.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_types.h>
49 #include <net/if_dl.h>
50 #include <net/route.h>
51
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 #include <netinet6/in6_var.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet6/nd6.h>
58 #include <netinet/icmp6.h>
59
60 #include "carp.h"
61 #if NCARP > 0
62 #include <netinet/ip_carp.h>
63 #endif
64
65 static TAILQ_HEAD(, dadq) dadq =
66 TAILQ_HEAD_INITIALIZER(dadq); /* list of addresses to run DAD on */
67 struct dadq {
68 TAILQ_ENTRY(dadq) dad_list;
69 struct ifaddr *dad_ifa;
70 int dad_count; /* max NS to send */
71 int dad_ns_tcount; /* # of trials to send NS */
72 int dad_ns_ocount; /* NS sent so far */
73 int dad_ns_icount;
74 int dad_na_icount;
75 struct timeout dad_timer_ch;
76 };
77
78 struct dadq *nd6_dad_find(struct ifaddr *);
79 void nd6_dad_destroy(struct dadq *);
80 void nd6_dad_starttimer(struct dadq *);
81 void nd6_dad_stoptimer(struct dadq *);
82 void nd6_dad_timer(void *);
83 void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
84 void nd6_dad_ns_input(struct ifaddr *);
85 void nd6_dad_duplicated(struct dadq *);
86
87 int nd6_isneighbor(const struct ifnet *, const struct in6_addr *);
88
89 static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
90
91 /*
92 * Input an Neighbor Solicitation Message.
93 *
94 * Based on RFC 2461
95 * Based on RFC 2462 (duplicated address detection)
96 */
97 void
98 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
99 {
100 struct ifnet *ifp;
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 router = ip6_forwarding;
112 int tlladdr;
113 struct nd_opts ndopts;
114 struct sockaddr_dl *proxydl = NULL;
115 char addr[INET6_ADDRSTRLEN], addr0[INET6_ADDRSTRLEN];
116
117 ifp = if_get(m->m_pkthdr.ph_ifidx);
118 if (ifp == NULL)
119 goto freeit;
120
121 IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
122 if (nd_ns == NULL) {
123 icmp6stat_inc(icp6s_tooshort);
124 if_put(ifp);
125 return;
126 }
127 ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
128 taddr6 = nd_ns->nd_ns_target;
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,
134 inet_ntop(AF_INET6, &ip6->ip6_src, addr, sizeof(addr)),
135 inet_ntop(AF_INET6, &ip6->ip6_dst, addr0, sizeof(addr0)),
136 ifp->if_xname));
137 goto bad;
138 }
139
140 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
141 /* dst has to be solicited node multicast address. */
142 /* don't check ifindex portion */
143 if (daddr6.s6_addr16[0] == __IPV6_ADDR_INT16_MLL &&
144 daddr6.s6_addr32[1] == 0 &&
145 daddr6.s6_addr32[2] == __IPV6_ADDR_INT32_ONE &&
146 daddr6.s6_addr8[12] == 0xff) {
147 ; /*good*/
148 } else {
149 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
150 "(wrong ip6 dst)\n"));
151 goto bad;
152 }
153 } else {
154 /*
155 * Make sure the source address is from a neighbor's address.
156 */
157 if (!nd6_isneighbor(ifp, &saddr6)) {
158 nd6log((LOG_INFO, "nd6_ns_input: "
159 "NS packet from non-neighbor\n"));
160 goto bad;
161 }
162 }
163
164
165 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
166 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
167 goto bad;
168 }
169
170 if (IN6_IS_SCOPE_EMBED(&taddr6))
171 taddr6.s6_addr16[1] = htons(ifp->if_index);
172
173 icmp6len -= sizeof(*nd_ns);
174 if (nd6_options(nd_ns + 1, icmp6len, &ndopts) < 0) {
175 nd6log((LOG_INFO,
176 "nd6_ns_input: invalid ND option, ignored\n"));
177 /* nd6_options have incremented stats */
178 goto freeit;
179 }
180
181 if (ndopts.nd_opts_src_lladdr) {
182 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
183 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
184 }
185
186 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
187 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
188 "(link-layer address option)\n"));
189 goto bad;
190 }
191
192 /*
193 * Attaching target link-layer address to the NA?
194 * (RFC 2461 7.2.4)
195 *
196 * NS IP dst is unicast/anycast MUST NOT add
197 * NS IP dst is solicited-node multicast MUST add
198 *
199 * In implementation, we add target link-layer address by default.
200 * We do not add one in MUST NOT cases.
201 */
202 #if 0 /* too much! */
203 ifa = &in6ifa_ifpwithaddr(ifp, &daddr6)->ia_ifa;
204 if (ifa && (ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST))
205 tlladdr = 0;
206 else
207 #endif
208 if (!IN6_IS_ADDR_MULTICAST(&daddr6))
209 tlladdr = 0;
210 else
211 tlladdr = 1;
212
213 /*
214 * Target address (taddr6) must be either:
215 * (1) Valid unicast/anycast address for my receiving interface,
216 * (2) Unicast address for which I'm offering proxy service, or
217 * (3) "tentative" address on which DAD is being performed.
218 */
219 /* (1) and (3) check. */
220 ifa = &in6ifa_ifpwithaddr(ifp, &taddr6)->ia_ifa;
221 #if NCARP > 0
222 if (ifp->if_type == IFT_CARP && ifa && !carp_iamatch(ifp))
223 ifa = NULL;
224 #endif
225
226 /* (2) check. */
227 if (!ifa) {
228 struct rtentry *rt;
229 struct sockaddr_in6 tsin6;
230
231 bzero(&tsin6, sizeof tsin6);
232 tsin6.sin6_len = sizeof(struct sockaddr_in6);
233 tsin6.sin6_family = AF_INET6;
234 tsin6.sin6_addr = taddr6;
235
236 rt = rtalloc(sin6tosa(&tsin6), 0, m->m_pkthdr.ph_rtableid);
237 if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
238 rt->rt_gateway->sa_family == AF_LINK) {
239 /*
240 * proxy NDP for single entry
241 */
242 ifa = &in6ifa_ifpforlinklocal(ifp, IN6_IFF_TENTATIVE|
243 IN6_IFF_DUPLICATED|IN6_IFF_ANYCAST)->ia_ifa;
244 if (ifa) {
245 proxy = 1;
246 proxydl = satosdl(rt->rt_gateway);
247 router = 0; /* XXX */
248 }
249 }
250 if (rt)
251 rtfree(rt);
252 }
253 if (!ifa) {
254 /*
255 * We've got an NS packet, and we don't have that address
256 * assigned for us. We MUST silently ignore it.
257 * See RFC2461 7.2.3.
258 */
259 goto freeit;
260 }
261 myaddr6 = *IFA_IN6(ifa);
262 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
263 tentative = ifatoia6(ifa)->ia6_flags & IN6_IFF_TENTATIVE;
264 if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DUPLICATED)
265 goto freeit;
266
267 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
268 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
269 "(if %d, NS packet %d)\n",
270 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)),
271 ifp->if_addrlen, lladdrlen - 2));
272 goto bad;
273 }
274
275 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
276 log(LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
277 inet_ntop(AF_INET6, &saddr6, addr, sizeof(addr)));
278 goto freeit;
279 }
280
281 /*
282 * We have neighbor solicitation packet, with target address equals to
283 * one of my tentative address.
284 *
285 * src addr how to process?
286 * --- ---
287 * multicast of course, invalid (rejected in ip6_input)
288 * unicast somebody is doing address resolution -> ignore
289 * unspec dup address detection
290 *
291 * The processing is defined in RFC 2462.
292 */
293 if (tentative) {
294 /*
295 * If source address is unspecified address, it is for
296 * duplicated address detection.
297 *
298 * If not, the packet is for address resolution;
299 * silently ignore it.
300 */
301 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
302 nd6_dad_ns_input(ifa);
303
304 goto freeit;
305 }
306
307 /*
308 * If the source address is unspecified address, entries must not
309 * be created or updated.
310 * It looks that sender is performing DAD. Output NA toward
311 * all-node multicast address, to tell the sender that I'm using
312 * the address.
313 * S bit ("solicited") must be zero.
314 */
315 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
316 saddr6 = in6addr_linklocal_allnodes;
317 saddr6.s6_addr16[1] = htons(ifp->if_index);
318 nd6_na_output(ifp, &saddr6, &taddr6,
319 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
320 (router ? ND_NA_FLAG_ROUTER : 0),
321 tlladdr, sdltosa(proxydl));
322 goto freeit;
323 }
324
325 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
326
327 nd6_na_output(ifp, &saddr6, &taddr6,
328 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
329 (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
330 tlladdr, sdltosa(proxydl));
331 freeit:
332 m_freem(m);
333 if_put(ifp);
334 return;
335
336 bad:
337 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
338 inet_ntop(AF_INET6, &saddr6, addr, sizeof(addr))));
339 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
340 inet_ntop(AF_INET6, &daddr6, addr, sizeof(addr))));
341 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
342 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
343 icmp6stat_inc(icp6s_badns);
344 m_freem(m);
345 if_put(ifp);
346 }
347
348 /*
349 * Output an Neighbor Solicitation Message. Caller specifies:
350 * - ICMP6 header source IP6 address
351 * - ND6 header target IP6 address
352 * - ND6 header source datalink address
353 *
354 * Based on RFC 2461
355 * Based on RFC 2462 (duplicated address detection)
356 *
357 * ln - for source address determination
358 * dad - duplicated address detection
359 */
360 void
361 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
362 const struct in6_addr *taddr6, const struct llinfo_nd6 *ln, int dad)
363 {
364 struct mbuf *m;
365 struct ip6_hdr *ip6;
366 struct nd_neighbor_solicit *nd_ns;
367 struct sockaddr_in6 src_sa, dst_sa;
368 struct ip6_moptions im6o;
369 int icmp6len;
370 int maxlen;
371 caddr_t mac;
372
373 if (IN6_IS_ADDR_MULTICAST(taddr6))
374 return;
375
376 /* estimate the size of message */
377 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
378 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
379 #ifdef DIAGNOSTIC
380 if (max_linkhdr + maxlen >= MCLBYTES) {
381 printf("%s: max_linkhdr + maxlen >= MCLBYTES "
382 "(%d + %d > %d)\n", __func__, max_linkhdr, maxlen, MCLBYTES);
383 panic("%s: insufficient MCLBYTES", __func__);
384 /* NOTREACHED */
385 }
386 #endif
387
388 MGETHDR(m, M_DONTWAIT, MT_DATA);
389 if (m && max_linkhdr + maxlen >= MHLEN) {
390 MCLGET(m, M_DONTWAIT);
391 if ((m->m_flags & M_EXT) == 0) {
392 m_free(m);
393 m = NULL;
394 }
395 }
396 if (m == NULL)
397 return;
398 m->m_pkthdr.ph_ifidx = 0;
399 m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
400
401 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
402 m->m_flags |= M_MCAST;
403 im6o.im6o_ifidx = ifp->if_index;
404 im6o.im6o_hlim = 255;
405 im6o.im6o_loop = 0;
406 }
407
408 icmp6len = sizeof(*nd_ns);
409 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
410 m_align(m, maxlen);
411
412 /* fill neighbor solicitation packet */
413 ip6 = mtod(m, struct ip6_hdr *);
414 ip6->ip6_flow = 0;
415 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
416 ip6->ip6_vfc |= IPV6_VERSION;
417 /* ip6->ip6_plen will be set later */
418 ip6->ip6_nxt = IPPROTO_ICMPV6;
419 ip6->ip6_hlim = 255;
420 /* determine the source and destination addresses */
421 bzero(&src_sa, sizeof(src_sa));
422 bzero(&dst_sa, sizeof(dst_sa));
423 src_sa.sin6_family = dst_sa.sin6_family = AF_INET6;
424 src_sa.sin6_len = dst_sa.sin6_len = sizeof(struct sockaddr_in6);
425 if (daddr6)
426 dst_sa.sin6_addr = *daddr6;
427 else {
428 dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
429 dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
430 dst_sa.sin6_addr.s6_addr32[1] = 0;
431 dst_sa.sin6_addr.s6_addr32[2] = __IPV6_ADDR_INT32_ONE;
432 dst_sa.sin6_addr.s6_addr32[3] = taddr6->s6_addr32[3];
433 dst_sa.sin6_addr.s6_addr8[12] = 0xff;
434 }
435 ip6->ip6_dst = dst_sa.sin6_addr;
436 if (!dad) {
437 /*
438 * RFC2461 7.2.2:
439 * "If the source address of the packet prompting the
440 * solicitation is the same as one of the addresses assigned
441 * to the outgoing interface, that address SHOULD be placed
442 * in the IP Source Address of the outgoing solicitation.
443 * Otherwise, any one of the addresses assigned to the
444 * interface should be used."
445 *
446 * We use the source address for the prompting packet
447 * (saddr6), if:
448 * - saddr6 is given from the caller (by giving "ln"), and
449 * - saddr6 belongs to the outgoing interface and
450 * - if taddr is link local saddr6 must be link local as well
451 * Otherwise, we perform the source address selection as usual.
452 */
453 struct ip6_hdr *hip6; /* hold ip6 */
454 struct in6_addr *saddr6;
455
456 if (ln && ln->ln_hold) {
457 hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
458 if (sizeof(*hip6) <= ln->ln_hold->m_len) {
459 saddr6 = &hip6->ip6_src;
460 if (saddr6 && IN6_IS_ADDR_LINKLOCAL(taddr6) &&
461 !IN6_IS_ADDR_LINKLOCAL(saddr6))
462 saddr6 = NULL;
463 } else
464 saddr6 = NULL;
465 } else
466 saddr6 = NULL;
467 if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
468 src_sa.sin6_addr = *saddr6;
469 else {
470 struct rtentry *rt;
471
472 rt = rtalloc(sin6tosa(&dst_sa), RT_RESOLVE,
473 m->m_pkthdr.ph_rtableid);
474 if (!rtisvalid(rt)) {
475 char addr[INET6_ADDRSTRLEN];
476
477 nd6log((LOG_DEBUG,
478 "%s: source can't be determined: dst=%s\n",
479 __func__, inet_ntop(AF_INET6,
480 &dst_sa.sin6_addr, addr, sizeof(addr))));
481 rtfree(rt);
482 goto bad;
483 }
484 src_sa.sin6_addr =
485 ifatoia6(rt->rt_ifa)->ia_addr.sin6_addr;
486 rtfree(rt);
487 }
488 } else {
489 /*
490 * Source address for DAD packet must always be IPv6
491 * unspecified address. (0::0)
492 * We actually don't have to 0-clear the address (we did it
493 * above), but we do so here explicitly to make the intention
494 * clearer.
495 */
496 bzero(&src_sa.sin6_addr, sizeof(src_sa.sin6_addr));
497 }
498 ip6->ip6_src = src_sa.sin6_addr;
499 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
500 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
501 nd_ns->nd_ns_code = 0;
502 nd_ns->nd_ns_reserved = 0;
503 nd_ns->nd_ns_target = *taddr6;
504
505 if (IN6_IS_SCOPE_EMBED(&nd_ns->nd_ns_target))
506 nd_ns->nd_ns_target.s6_addr16[1] = 0;
507
508 /*
509 * Add source link-layer address option.
510 *
511 * spec implementation
512 * --- ---
513 * DAD packet MUST NOT do not add the option
514 * there's no link layer address:
515 * impossible do not add the option
516 * there's link layer address:
517 * Multicast NS MUST add one add the option
518 * Unicast NS SHOULD add one add the option
519 */
520 if (!dad && (mac = nd6_ifptomac(ifp))) {
521 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
522 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
523 /* 8 byte alignments... */
524 optlen = (optlen + 7) & ~7;
525
526 m->m_pkthdr.len += optlen;
527 m->m_len += optlen;
528 icmp6len += optlen;
529 bzero((caddr_t)nd_opt, optlen);
530 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
531 nd_opt->nd_opt_len = optlen >> 3;
532 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
533 }
534
535 ip6->ip6_plen = htons((u_short)icmp6len);
536 nd_ns->nd_ns_cksum = 0;
537 m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
538
539 ip6_output(m, NULL, NULL, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL);
540 icmp6stat_inc(icp6s_outhist + ND_NEIGHBOR_SOLICIT);
541 return;
542
543 bad:
544 m_freem(m);
545 }
546
547 /*
548 * Neighbor advertisement input handling.
549 *
550 * Based on RFC 2461
551 * Based on RFC 2462 (duplicated address detection)
552 *
553 * the following items are not implemented yet:
554 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
555 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
556 */
557 void
558 nd6_na_input(struct mbuf *m, int off, int icmp6len)
559 {
560 struct ifnet *ifp;
561 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
562 struct nd_neighbor_advert *nd_na;
563 struct in6_addr daddr6 = ip6->ip6_dst;
564 struct in6_addr taddr6;
565 int flags;
566 int is_router;
567 int is_solicited;
568 int is_override;
569 char *lladdr = NULL;
570 int lladdrlen = 0;
571 struct ifaddr *ifa;
572 struct in6_ifaddr *ifa6;
573 struct llinfo_nd6 *ln;
574 struct rtentry *rt = NULL;
575 struct sockaddr_dl *sdl;
576 struct nd_opts ndopts;
577 char addr[INET6_ADDRSTRLEN], addr0[INET6_ADDRSTRLEN];
578
579 NET_ASSERT_LOCKED();
580
581 ifp = if_get(m->m_pkthdr.ph_ifidx);
582 if (ifp == NULL)
583 goto freeit;
584
585 if (ip6->ip6_hlim != 255) {
586 nd6log((LOG_ERR,
587 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
588 ip6->ip6_hlim,
589 inet_ntop(AF_INET6, &ip6->ip6_src, addr, sizeof(addr)),
590 inet_ntop(AF_INET6, &ip6->ip6_dst, addr0, sizeof(addr0)),
591 ifp->if_xname));
592 goto bad;
593 }
594
595 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
596 if (nd_na == NULL) {
597 icmp6stat_inc(icp6s_tooshort);
598 if_put(ifp);
599 return;
600 }
601 taddr6 = nd_na->nd_na_target;
602 flags = nd_na->nd_na_flags_reserved;
603 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
604 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
605 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
606
607 if (IN6_IS_SCOPE_EMBED(&taddr6))
608 taddr6.s6_addr16[1] = htons(ifp->if_index);
609
610 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
611 nd6log((LOG_ERR,
612 "nd6_na_input: invalid target address %s\n",
613 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
614 goto bad;
615 }
616 if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
617 nd6log((LOG_ERR,
618 "nd6_na_input: a solicited adv is multicasted\n"));
619 goto bad;
620 }
621
622 icmp6len -= sizeof(*nd_na);
623 if (nd6_options(nd_na + 1, icmp6len, &ndopts) < 0) {
624 nd6log((LOG_INFO,
625 "nd6_na_input: invalid ND option, ignored\n"));
626 /* nd6_options have incremented stats */
627 goto freeit;
628 }
629
630 if (IN6_IS_ADDR_MULTICAST(&daddr6) && !ndopts.nd_opts_tgt_lladdr) {
631 nd6log((LOG_INFO,
632 "nd6_na_input: multicast adv without TLLA\n"));
633 goto bad;
634 }
635
636 if (ndopts.nd_opts_tgt_lladdr) {
637 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
638 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
639 }
640
641 ifa6 = in6ifa_ifpwithaddr(ifp, &taddr6);
642 ifa = ifa6 ? &ifa6->ia_ifa : NULL;
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 && (ifatoia6(ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
654 struct dadq *dp;
655
656 dp = nd6_dad_find(ifa);
657 if (dp) {
658 dp->dad_na_icount++;
659
660 /* remove the address. */
661 nd6_dad_duplicated(dp);
662 }
663 goto freeit;
664 }
665
666 if (ifa) {
667 #if NCARP > 0
668 /*
669 * Ignore NAs silently for carp addresses if we're not
670 * the CARP master.
671 */
672 if (ifp->if_type == IFT_CARP && !carp_iamatch(ifp))
673 goto freeit;
674 #endif
675 log(LOG_ERR,
676 "nd6_na_input: duplicate IP6 address %s\n",
677 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)));
678 goto freeit;
679 }
680
681 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
682 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
683 "(if %d, NA packet %d)\n",
684 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)),
685 ifp->if_addrlen, lladdrlen - 2));
686 goto bad;
687 }
688
689 /*
690 * If no neighbor cache entry is found, NA SHOULD silently be
691 * discarded.
692 */
693 rt = nd6_lookup(&taddr6, 0, ifp, ifp->if_rdomain);
694 if ((rt == NULL) ||
695 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
696 ((sdl = satosdl(rt->rt_gateway)) == NULL))
697 goto freeit;
698
699 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
700 /*
701 * If the link-layer has address, and no lladdr option came,
702 * discard the packet.
703 */
704 if (ifp->if_addrlen && !lladdr)
705 goto freeit;
706
707 /*
708 * Record link-layer address, and update the state.
709 */
710 sdl->sdl_alen = ifp->if_addrlen;
711 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
712 if (is_solicited) {
713 ln->ln_state = ND6_LLINFO_REACHABLE;
714 ln->ln_byhint = 0;
715 /* Notify userland that a new ND entry is reachable. */
716 rtm_send(rt, RTM_RESOLVE, 0, ifp->if_rdomain);
717 if (!ND6_LLINFO_PERMANENT(ln)) {
718 nd6_llinfo_settimer(ln,
719 ifp->if_nd->reachable);
720 }
721 } else {
722 ln->ln_state = ND6_LLINFO_STALE;
723 nd6_llinfo_settimer(ln, nd6_gctimer);
724 }
725 if ((ln->ln_router = is_router) != 0) {
726 /*
727 * This means a router's state has changed from
728 * non-reachable to probably reachable, and might
729 * affect the status of associated prefixes..
730 */
731 if ((rt->rt_flags & RTF_LLINFO) == 0)
732 goto freeit; /* ln is gone */
733 }
734 } else {
735 int llchange;
736
737 /*
738 * Check if the link-layer address has changed or not.
739 */
740 if (!lladdr)
741 llchange = 0;
742 else {
743 if (sdl->sdl_alen) {
744 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
745 llchange = 1;
746 else
747 llchange = 0;
748 } else
749 llchange = 1;
750 }
751
752 /*
753 * This is VERY complex. Look at it with care.
754 *
755 * override solicit lladdr llchange action
756 * (L: record lladdr)
757 *
758 * 0 0 n -- (2c)
759 * 0 0 y n (2b) L
760 * 0 0 y y (1) REACHABLE->STALE
761 * 0 1 n -- (2c) *->REACHABLE
762 * 0 1 y n (2b) L *->REACHABLE
763 * 0 1 y y (1) REACHABLE->STALE
764 * 1 0 n -- (2a)
765 * 1 0 y n (2a) L
766 * 1 0 y y (2a) L *->STALE
767 * 1 1 n -- (2a) *->REACHABLE
768 * 1 1 y n (2a) L *->REACHABLE
769 * 1 1 y y (2a) L *->REACHABLE
770 */
771 if (!is_override && (lladdr && llchange)) { /* (1) */
772 /*
773 * If state is REACHABLE, make it STALE.
774 * no other updates should be done.
775 */
776 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
777 ln->ln_state = ND6_LLINFO_STALE;
778 nd6_llinfo_settimer(ln, nd6_gctimer);
779 }
780 goto freeit;
781 } else if (is_override /* (2a) */
782 || (!is_override && (lladdr && !llchange)) /* (2b) */
783 || !lladdr) { /* (2c) */
784 /*
785 * Update link-local address, if any.
786 */
787 if (llchange) {
788 log(LOG_INFO, "ndp info overwritten for %s "
789 "by %s on %s\n",
790 inet_ntop(AF_INET6, &taddr6,
791 addr, sizeof(addr)),
792 ether_sprintf(lladdr), ifp->if_xname);
793 }
794 if (lladdr) {
795 sdl->sdl_alen = ifp->if_addrlen;
796 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
797 }
798
799 /*
800 * If solicited, make the state REACHABLE.
801 * If not solicited and the link-layer address was
802 * changed, make it STALE.
803 */
804 if (is_solicited) {
805 ln->ln_state = ND6_LLINFO_REACHABLE;
806 ln->ln_byhint = 0;
807 if (!ND6_LLINFO_PERMANENT(ln)) {
808 nd6_llinfo_settimer(ln,
809 ifp->if_nd->reachable);
810 }
811 } else {
812 if (lladdr && llchange) {
813 ln->ln_state = ND6_LLINFO_STALE;
814 nd6_llinfo_settimer(ln, nd6_gctimer);
815 }
816 }
817 }
818
819 if (ln->ln_router && !is_router) {
820 if (!ip6_forwarding) {
821 /*
822 * The neighbor may be used
823 * as a next hop for some destinations
824 * (e.g. redirect case). So we must
825 * call rt6_flush explicitly.
826 */
827 rt6_flush(&ip6->ip6_src, ifp);
828 }
829 }
830 ln->ln_router = is_router;
831 }
832 rt->rt_flags &= ~RTF_REJECT;
833 ln->ln_asked = 0;
834 if (ln->ln_hold) {
835 struct mbuf *n = ln->ln_hold;
836 ln->ln_hold = NULL;
837 /*
838 * we assume ifp is not a loopback here, so just set the 2nd
839 * argument as the 1st one.
840 */
841 ifp->if_output(ifp, n, rt_key(rt), rt);
842 if (ln->ln_hold == n) {
843 /* n is back in ln_hold. Discard. */
844 m_freem(ln->ln_hold);
845 ln->ln_hold = NULL;
846 }
847 }
848
849 freeit:
850 rtfree(rt);
851 m_freem(m);
852 if_put(ifp);
853 return;
854
855 bad:
856 icmp6stat_inc(icp6s_badna);
857 m_freem(m);
858 if_put(ifp);
859 }
860
861 /*
862 * Neighbor advertisement output handling.
863 *
864 * Based on RFC 2461
865 *
866 * the following items are not implemented yet:
867 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
868 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
869 *
870 * tlladdr - 1 if include target link-layer address
871 * sdl0 - sockaddr_dl (= proxy NA) or NULL
872 */
873 void
874 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
875 const struct in6_addr *taddr6, u_long flags, int tlladdr,
876 struct sockaddr *sdl0)
877 {
878 struct mbuf *m;
879 struct rtentry *rt = NULL;
880 struct ip6_hdr *ip6;
881 struct nd_neighbor_advert *nd_na;
882 struct ip6_moptions im6o;
883 struct sockaddr_in6 dst_sa;
884 int icmp6len, maxlen;
885 caddr_t mac = NULL;
886
887 #if NCARP > 0
888 /* Do not send NAs for carp addresses if we're not the CARP master. */
889 if (ifp->if_type == IFT_CARP && !carp_iamatch(ifp))
890 return;
891 #endif
892
893 /* estimate the size of message */
894 maxlen = sizeof(*ip6) + sizeof(*nd_na);
895 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
896 #ifdef DIAGNOSTIC
897 if (max_linkhdr + maxlen >= MCLBYTES) {
898 printf("%s: max_linkhdr + maxlen >= MCLBYTES "
899 "(%d + %d > %d)\n", __func__, max_linkhdr, maxlen, MCLBYTES);
900 panic("%s: insufficient MCLBYTES", __func__);
901 /* NOTREACHED */
902 }
903 #endif
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.ph_rtableid = ifp->if_rdomain;
916
917 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
918 m->m_flags |= M_MCAST;
919 im6o.im6o_ifidx = ifp->if_index;
920 im6o.im6o_hlim = 255;
921 im6o.im6o_loop = 0;
922 }
923
924 icmp6len = sizeof(*nd_na);
925 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
926 m_align(m, maxlen);
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 bzero(&dst_sa, sizeof(dst_sa));
936 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
937 dst_sa.sin6_family = AF_INET6;
938 dst_sa.sin6_addr = *daddr6;
939 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
940 /* reply to DAD */
941 dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
942 dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
943 dst_sa.sin6_addr.s6_addr32[1] = 0;
944 dst_sa.sin6_addr.s6_addr32[2] = 0;
945 dst_sa.sin6_addr.s6_addr32[3] = __IPV6_ADDR_INT32_ONE;
946
947 flags &= ~ND_NA_FLAG_SOLICITED;
948 }
949 ip6->ip6_dst = dst_sa.sin6_addr;
950
951 /*
952 * Select a source whose scope is the same as that of the dest.
953 */
954 rt = rtalloc(sin6tosa(&dst_sa), RT_RESOLVE, ifp->if_rdomain);
955 if (!rtisvalid(rt)) {
956 char addr[INET6_ADDRSTRLEN];
957
958 nd6log((LOG_DEBUG, "%s: source can't be determined: dst=%s\n",
959 __func__, inet_ntop(AF_INET6, &dst_sa.sin6_addr, addr,
960 sizeof(addr))));
961 rtfree(rt);
962 goto bad;
963 }
964 ip6->ip6_src = ifatoia6(rt->rt_ifa)->ia_addr.sin6_addr;
965 rtfree(rt);
966 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
967 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
968 nd_na->nd_na_code = 0;
969 nd_na->nd_na_target = *taddr6;
970 if (IN6_IS_SCOPE_EMBED(&nd_na->nd_na_target))
971 nd_na->nd_na_target.s6_addr16[1] = 0;
972
973 /*
974 * "tlladdr" indicates NS's condition for adding tlladdr or not.
975 * see nd6_ns_input() for details.
976 * Basically, if NS packet is sent to unicast/anycast addr,
977 * target lladdr option SHOULD NOT be included.
978 */
979 if (tlladdr) {
980 /*
981 * sdl0 != NULL indicates proxy NA. If we do proxy, use
982 * lladdr in sdl0. If we are not proxying (sending NA for
983 * my address) use lladdr configured for the interface.
984 */
985 if (sdl0 == NULL) {
986 mac = nd6_ifptomac(ifp);
987 } else if (sdl0->sa_family == AF_LINK) {
988 struct sockaddr_dl *sdl;
989 sdl = satosdl(sdl0);
990 if (sdl->sdl_alen == ifp->if_addrlen)
991 mac = LLADDR(sdl);
992 }
993 }
994 if (tlladdr && mac) {
995 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
996 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
997
998 /* roundup to 8 bytes alignment! */
999 optlen = (optlen + 7) & ~7;
1000
1001 m->m_pkthdr.len += optlen;
1002 m->m_len += optlen;
1003 icmp6len += optlen;
1004 bzero((caddr_t)nd_opt, optlen);
1005 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1006 nd_opt->nd_opt_len = optlen >> 3;
1007 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1008 } else
1009 flags &= ~ND_NA_FLAG_OVERRIDE;
1010
1011 ip6->ip6_plen = htons((u_short)icmp6len);
1012 nd_na->nd_na_flags_reserved = flags;
1013 nd_na->nd_na_cksum = 0;
1014 m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
1015
1016 ip6_output(m, NULL, NULL, 0, &im6o, NULL);
1017 icmp6stat_inc(icp6s_outhist + ND_NEIGHBOR_ADVERT);
1018 return;
1019
1020 bad:
1021 m_freem(m);
1022 }
1023
1024 caddr_t
1025 nd6_ifptomac(struct ifnet *ifp)
1026 {
1027 switch (ifp->if_type) {
1028 case IFT_ETHER:
1029 case IFT_IEEE1394:
1030 case IFT_PROPVIRTUAL:
1031 case IFT_CARP:
1032 case IFT_IEEE80211:
1033 return ((caddr_t)(ifp + 1));
1034 default:
1035 return NULL;
1036 }
1037 }
1038
1039 struct dadq *
1040 nd6_dad_find(struct ifaddr *ifa)
1041 {
1042 struct dadq *dp;
1043
1044 TAILQ_FOREACH(dp, &dadq, dad_list) {
1045 if (dp->dad_ifa == ifa)
1046 return dp;
1047 }
1048 return NULL;
1049 }
1050
1051 void
1052 nd6_dad_destroy(struct dadq *dp)
1053 {
1054 TAILQ_REMOVE(&dadq, dp, dad_list);
1055 ifafree(dp->dad_ifa);
1056 free(dp, M_IP6NDP, sizeof(*dp));
1057 ip6_dad_pending--;
1058 }
1059
1060 void
1061 nd6_dad_starttimer(struct dadq *dp)
1062 {
1063 timeout_set_proc(&dp->dad_timer_ch, nd6_dad_timer, dp->dad_ifa);
1064 timeout_add_msec(&dp->dad_timer_ch, RETRANS_TIMER);
1065 }
1066
1067 void
1068 nd6_dad_stoptimer(struct dadq *dp)
1069 {
1070 timeout_del(&dp->dad_timer_ch);
1071 }
1072
1073 /*
1074 * Start Duplicated Address Detection (DAD) for specified interface address.
1075 */
1076 void
1077 nd6_dad_start(struct ifaddr *ifa)
1078 {
1079 struct in6_ifaddr *ia6 = ifatoia6(ifa);
1080 struct dadq *dp;
1081 char addr[INET6_ADDRSTRLEN];
1082
1083 NET_ASSERT_LOCKED();
1084
1085 /*
1086 * If we don't need DAD, don't do it.
1087 * There are several cases:
1088 * - DAD is disabled (ip6_dad_count == 0)
1089 * - the interface address is anycast
1090 */
1091 KASSERT(ia6->ia6_flags & IN6_IFF_TENTATIVE);
1092 if ((ia6->ia6_flags & IN6_IFF_ANYCAST) || (!ip6_dad_count)) {
1093 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1094
1095 rtm_addr(RTM_CHGADDRATTR, ifa);
1096
1097 return;
1098 }
1099
1100 /* DAD already in progress */
1101 if (nd6_dad_find(ifa) != NULL)
1102 return;
1103
1104 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1105 if (dp == NULL) {
1106 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1107 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1108 addr, sizeof(addr)),
1109 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1110 return;
1111 }
1112
1113 TAILQ_INSERT_TAIL(&dadq, dp, dad_list);
1114 ip6_dad_pending++;
1115
1116 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", ifa->ifa_ifp->if_xname,
1117 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr))));
1118
1119 /*
1120 * Send NS packet for DAD, ip6_dad_count times.
1121 * Note that we must delay the first transmission, if this is the
1122 * first packet to be sent from the interface after interface
1123 * (re)initialization.
1124 */
1125 dp->dad_ifa = ifaref(ifa);
1126 dp->dad_count = ip6_dad_count;
1127 dp->dad_ns_icount = dp->dad_na_icount = 0;
1128 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1129 nd6_dad_ns_output(dp, ifa);
1130 nd6_dad_starttimer(dp);
1131 }
1132
1133 /*
1134 * terminate DAD unconditionally. used for address removals.
1135 */
1136 void
1137 nd6_dad_stop(struct ifaddr *ifa)
1138 {
1139 struct dadq *dp;
1140
1141 dp = nd6_dad_find(ifa);
1142 if (!dp) {
1143 /* DAD wasn't started yet */
1144 return;
1145 }
1146
1147 nd6_dad_stoptimer(dp);
1148 nd6_dad_destroy(dp);
1149 }
1150
1151 void
1152 nd6_dad_timer(void *xifa)
1153 {
1154 struct ifaddr *ifa = xifa;
1155 struct in6_ifaddr *ia6 = ifatoia6(ifa);
1156 struct dadq *dp;
1157 char addr[INET6_ADDRSTRLEN];
1158
1159 NET_LOCK();
1160
1161 /* Sanity check */
1162 if (ia6 == NULL) {
1163 log(LOG_ERR, "%s: called with null parameter\n", __func__);
1164 goto done;
1165 }
1166 dp = nd6_dad_find(ifa);
1167 if (dp == NULL) {
1168 log(LOG_ERR, "%s: DAD structure not found\n", __func__);
1169 goto done;
1170 }
1171 if (ia6->ia6_flags & IN6_IFF_DUPLICATED) {
1172 log(LOG_ERR, "%s: called with duplicated address %s(%s)\n",
1173 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1174 addr, sizeof(addr)),
1175 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1176 goto done;
1177 }
1178 if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1179 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1180 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1181 addr, sizeof(addr)),
1182 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1183 goto done;
1184 }
1185
1186 /* timeouted with IFF_{RUNNING,UP} check */
1187 if (dp->dad_ns_tcount > dad_maxtry) {
1188 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1189 ifa->ifa_ifp->if_xname));
1190
1191 nd6_dad_destroy(dp);
1192 goto done;
1193 }
1194
1195 /* Need more checks? */
1196 if (dp->dad_ns_ocount < dp->dad_count) {
1197 /*
1198 * We have more NS to go. Send NS packet for DAD.
1199 */
1200 nd6_dad_ns_output(dp, ifa);
1201 nd6_dad_starttimer(dp);
1202 } else {
1203 /*
1204 * We have transmitted sufficient number of DAD packets.
1205 */
1206 if (dp->dad_na_icount || dp->dad_ns_icount) {
1207 /* dp will be freed in nd6_dad_duplicated() */
1208 nd6_dad_duplicated(dp);
1209 } else {
1210 /*
1211 * We are done with DAD. No NA came, no NS came.
1212 */
1213 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1214
1215 rtm_addr(RTM_CHGADDRATTR, ifa);
1216
1217 nd6log((LOG_DEBUG,
1218 "%s: DAD complete for %s - no duplicates found\n",
1219 ifa->ifa_ifp->if_xname,
1220 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1221 addr, sizeof(addr))));
1222
1223 nd6_dad_destroy(dp);
1224 }
1225 }
1226
1227 done:
1228 NET_UNLOCK();
1229 }
1230
1231 void
1232 nd6_dad_duplicated(struct dadq *dp)
1233 {
1234 struct in6_ifaddr *ia6 = ifatoia6(dp->dad_ifa);
1235 char addr[INET6_ADDRSTRLEN];
1236
1237 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1238 "NS in/out=%d/%d, NA in=%d\n",
1239 ia6->ia_ifp->if_xname,
1240 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)),
1241 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1242
1243 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1244 ia6->ia6_flags |= IN6_IFF_DUPLICATED;
1245
1246 /* We are done with DAD, with duplicated address found. (failure) */
1247 nd6_dad_stoptimer(dp);
1248
1249 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1250 ia6->ia_ifp->if_xname,
1251 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)));
1252 log(LOG_ERR, "%s: manual intervention required\n",
1253 ia6->ia_ifp->if_xname);
1254
1255 rtm_addr(RTM_CHGADDRATTR, dp->dad_ifa);
1256
1257 nd6_dad_destroy(dp);
1258 }
1259
1260 void
1261 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1262 {
1263 struct in6_ifaddr *ia6 = ifatoia6(ifa);
1264 struct ifnet *ifp = ifa->ifa_ifp;
1265
1266 dp->dad_ns_tcount++;
1267 if ((ifp->if_flags & IFF_UP) == 0) {
1268 #if 0
1269 printf("%s: interface down?\n", ifp->if_xname);
1270 #endif
1271 return;
1272 }
1273 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1274 #if 0
1275 printf("%s: interface not running?\n", ifp->if_xname);
1276 #endif
1277 return;
1278 }
1279
1280 dp->dad_ns_ocount++;
1281 nd6_ns_output(ifp, NULL, &ia6->ia_addr.sin6_addr, NULL, 1);
1282 }
1283
1284 void
1285 nd6_dad_ns_input(struct ifaddr *ifa)
1286 {
1287 struct dadq *dp;
1288
1289 if (!ifa)
1290 panic("%s: ifa == NULL", __func__);
1291
1292 dp = nd6_dad_find(ifa);
1293 if (dp == NULL) {
1294 log(LOG_ERR, "%s: DAD structure not found\n", __func__);
1295 return;
1296 }
1297
1298 /*
1299 * if I'm yet to start DAD, someone else started using this address
1300 * first. I have a duplicate and you win.
1301 */
1302 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1303 if (dp->dad_ns_ocount == 0) {
1304 /* dp will be freed in nd6_dad_duplicated() */
1305 nd6_dad_duplicated(dp);
1306 } else {
1307 /*
1308 * not sure if I got a duplicate.
1309 * increment ns count and see what happens.
1310 */
1311 dp->dad_ns_icount++;
1312 }
1313 }
1314
1315 /*
1316 * Check whether ``addr'' is a neighbor address connected to ``ifp''.
1317 */
1318 int
1319 nd6_isneighbor(const struct ifnet *ifp, const struct in6_addr *addr)
1320 {
1321 struct rtentry *rt;
1322 struct sockaddr_in6 sin6;
1323 unsigned int tableid = ifp->if_rdomain;
1324 int rv = 0;
1325
1326 memset(&sin6, 0, sizeof(sin6));
1327 sin6.sin6_len = sizeof(struct sockaddr_in6);
1328 sin6.sin6_family = AF_INET6;
1329 sin6.sin6_addr = *addr;
1330 rt = rtalloc(sin6tosa(&sin6), 0, tableid);
1331
1332 if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_CLONING|RTF_CLONED))
1333 rv = if_isconnected(ifp, rt->rt_ifidx);
1334
1335 rtfree(rt);
1336 return (rv);
1337 }
Cache object: 570659b3dece0090bc62acb4cab06897
|