1 /* $OpenBSD: nd6_nbr.c,v 1.151 2023/07/30 12:52:03 krw 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
nd6_ns_input(struct mbuf * m,int off,int icmp6len)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,
326 0);
327
328 nd6_na_output(ifp, &saddr6, &taddr6,
329 ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
330 (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
331 tlladdr, sdltosa(proxydl));
332 freeit:
333 m_freem(m);
334 if_put(ifp);
335 return;
336
337 bad:
338 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
339 inet_ntop(AF_INET6, &saddr6, addr, sizeof(addr))));
340 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
341 inet_ntop(AF_INET6, &daddr6, addr, sizeof(addr))));
342 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
343 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
344 icmp6stat_inc(icp6s_badns);
345 m_freem(m);
346 if_put(ifp);
347 }
348
349 /*
350 * Output an 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 (duplicated address detection)
357 *
358 * ln - for source address determination
359 * dad - duplicated address detection
360 */
361 void
nd6_ns_output(struct ifnet * ifp,const struct in6_addr * daddr6,const struct in6_addr * taddr6,const struct in6_addr * saddr6,int dad)362 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
363 const struct in6_addr *taddr6, const struct in6_addr *saddr6, int dad)
364 {
365 struct mbuf *m;
366 struct ip6_hdr *ip6;
367 struct nd_neighbor_solicit *nd_ns;
368 struct sockaddr_in6 src_sa, dst_sa;
369 struct ip6_moptions im6o;
370 int icmp6len;
371 int maxlen;
372 caddr_t mac;
373
374 if (IN6_IS_ADDR_MULTICAST(taddr6))
375 return;
376
377 /* estimate the size of message */
378 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
379 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
380 #ifdef DIAGNOSTIC
381 if (max_linkhdr + maxlen >= MCLBYTES) {
382 printf("%s: max_linkhdr + maxlen >= MCLBYTES (%d + %d > %d)\n",
383 __func__, max_linkhdr, maxlen, MCLBYTES);
384 panic("%s: insufficient MCLBYTES", __func__);
385 /* NOTREACHED */
386 }
387 #endif
388
389 MGETHDR(m, M_DONTWAIT, MT_DATA);
390 if (m && max_linkhdr + maxlen >= MHLEN) {
391 MCLGET(m, M_DONTWAIT);
392 if ((m->m_flags & M_EXT) == 0) {
393 m_free(m);
394 m = NULL;
395 }
396 }
397 if (m == NULL)
398 return;
399 m->m_pkthdr.ph_ifidx = 0;
400 m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
401
402 if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
403 m->m_flags |= M_MCAST;
404 im6o.im6o_ifidx = ifp->if_index;
405 im6o.im6o_hlim = 255;
406 im6o.im6o_loop = 0;
407 }
408
409 icmp6len = sizeof(*nd_ns);
410 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
411 m_align(m, maxlen);
412
413 /* fill neighbor solicitation packet */
414 ip6 = mtod(m, struct ip6_hdr *);
415 ip6->ip6_flow = 0;
416 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
417 ip6->ip6_vfc |= IPV6_VERSION;
418 /* ip6->ip6_plen will be set later */
419 ip6->ip6_nxt = IPPROTO_ICMPV6;
420 ip6->ip6_hlim = 255;
421 /* determine the source and destination addresses */
422 bzero(&src_sa, sizeof(src_sa));
423 bzero(&dst_sa, sizeof(dst_sa));
424 src_sa.sin6_family = dst_sa.sin6_family = AF_INET6;
425 src_sa.sin6_len = dst_sa.sin6_len = sizeof(struct sockaddr_in6);
426 if (daddr6 != NULL)
427 dst_sa.sin6_addr = *daddr6;
428 else {
429 dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
430 dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
431 dst_sa.sin6_addr.s6_addr32[1] = 0;
432 dst_sa.sin6_addr.s6_addr32[2] = __IPV6_ADDR_INT32_ONE;
433 dst_sa.sin6_addr.s6_addr32[3] = taddr6->s6_addr32[3];
434 dst_sa.sin6_addr.s6_addr8[12] = 0xff;
435 }
436 ip6->ip6_dst = dst_sa.sin6_addr;
437 if (!dad) {
438 /*
439 * RFC2461 7.2.2:
440 * "If the source address of the packet prompting the
441 * solicitation is the same as one of the addresses assigned
442 * to the outgoing interface, that address SHOULD be placed
443 * in the IP Source Address of the outgoing solicitation.
444 * Otherwise, any one of the addresses assigned to the
445 * interface should be used."
446 *
447 * We use the source address for the prompting packet
448 * (saddr6), if:
449 * - saddr6 is given from the caller (by giving "ln"), and
450 * - saddr6 belongs to the outgoing interface and
451 * - if taddr is link local saddr6 must be link local as well
452 * Otherwise, we perform the source address selection as usual.
453 */
454 if (saddr6 != NULL)
455 src_sa.sin6_addr = *saddr6;
456
457 if (!IN6_IS_ADDR_LINKLOCAL(taddr6) ||
458 IN6_IS_ADDR_UNSPECIFIED(&src_sa.sin6_addr) ||
459 IN6_IS_ADDR_LINKLOCAL(&src_sa.sin6_addr) ||
460 !in6ifa_ifpwithaddr(ifp, &src_sa.sin6_addr)) {
461 struct rtentry *rt;
462
463 rt = rtalloc(sin6tosa(&dst_sa), RT_RESOLVE,
464 m->m_pkthdr.ph_rtableid);
465 if (!rtisvalid(rt)) {
466 char addr[INET6_ADDRSTRLEN];
467
468 nd6log((LOG_DEBUG,
469 "%s: source can't be determined: dst=%s\n",
470 __func__, inet_ntop(AF_INET6,
471 &dst_sa.sin6_addr, addr, sizeof(addr))));
472 rtfree(rt);
473 goto bad;
474 }
475 src_sa.sin6_addr =
476 ifatoia6(rt->rt_ifa)->ia_addr.sin6_addr;
477 rtfree(rt);
478 }
479 } else {
480 /*
481 * Source address for DAD packet must always be IPv6
482 * unspecified address. (0::0)
483 * We actually don't have to 0-clear the address (we did it
484 * above), but we do so here explicitly to make the intention
485 * clearer.
486 */
487 bzero(&src_sa.sin6_addr, sizeof(src_sa.sin6_addr));
488 }
489 ip6->ip6_src = src_sa.sin6_addr;
490 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
491 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
492 nd_ns->nd_ns_code = 0;
493 nd_ns->nd_ns_reserved = 0;
494 nd_ns->nd_ns_target = *taddr6;
495
496 if (IN6_IS_SCOPE_EMBED(&nd_ns->nd_ns_target))
497 nd_ns->nd_ns_target.s6_addr16[1] = 0;
498
499 /*
500 * Add source link-layer address option.
501 *
502 * spec implementation
503 * --- ---
504 * DAD packet MUST NOT do not add the option
505 * there's no link layer address:
506 * impossible do not add the option
507 * there's link layer address:
508 * Multicast NS MUST add one add the option
509 * Unicast NS SHOULD add one add the option
510 */
511 if (!dad && (mac = nd6_ifptomac(ifp))) {
512 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
513 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
514 /* 8 byte alignments... */
515 optlen = (optlen + 7) & ~7;
516
517 m->m_pkthdr.len += optlen;
518 m->m_len += optlen;
519 icmp6len += optlen;
520 bzero((caddr_t)nd_opt, optlen);
521 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
522 nd_opt->nd_opt_len = optlen >> 3;
523 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
524 }
525
526 ip6->ip6_plen = htons((u_short)icmp6len);
527 nd_ns->nd_ns_cksum = 0;
528 m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
529
530 ip6_output(m, NULL, NULL, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL);
531 icmp6stat_inc(icp6s_outhist + ND_NEIGHBOR_SOLICIT);
532 return;
533
534 bad:
535 m_freem(m);
536 }
537
538 /*
539 * Neighbor advertisement input handling.
540 *
541 * Based on RFC 2461
542 * Based on RFC 2462 (duplicated address detection)
543 *
544 * the following items are not implemented yet:
545 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
546 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
547 */
548 void
nd6_na_input(struct mbuf * m,int off,int icmp6len)549 nd6_na_input(struct mbuf *m, int off, int icmp6len)
550 {
551 struct ifnet *ifp;
552 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
553 struct nd_neighbor_advert *nd_na;
554 struct in6_addr daddr6 = ip6->ip6_dst;
555 struct in6_addr taddr6;
556 int flags;
557 int is_router;
558 int is_solicited;
559 int is_override;
560 char *lladdr = NULL;
561 int lladdrlen = 0;
562 struct ifaddr *ifa;
563 struct in6_ifaddr *ifa6;
564 struct llinfo_nd6 *ln;
565 struct rtentry *rt = NULL;
566 struct sockaddr_dl *sdl;
567 struct nd_opts ndopts;
568 char addr[INET6_ADDRSTRLEN], addr0[INET6_ADDRSTRLEN];
569
570 NET_ASSERT_LOCKED_EXCLUSIVE();
571
572 ifp = if_get(m->m_pkthdr.ph_ifidx);
573 if (ifp == NULL)
574 goto freeit;
575
576 if (ip6->ip6_hlim != 255) {
577 nd6log((LOG_ERR,
578 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
579 ip6->ip6_hlim,
580 inet_ntop(AF_INET6, &ip6->ip6_src, addr, sizeof(addr)),
581 inet_ntop(AF_INET6, &ip6->ip6_dst, addr0, sizeof(addr0)),
582 ifp->if_xname));
583 goto bad;
584 }
585
586 IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
587 if (nd_na == NULL) {
588 icmp6stat_inc(icp6s_tooshort);
589 if_put(ifp);
590 return;
591 }
592 taddr6 = nd_na->nd_na_target;
593 flags = nd_na->nd_na_flags_reserved;
594 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
595 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
596 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
597
598 if (IN6_IS_SCOPE_EMBED(&taddr6))
599 taddr6.s6_addr16[1] = htons(ifp->if_index);
600
601 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
602 nd6log((LOG_ERR,
603 "nd6_na_input: invalid target address %s\n",
604 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
605 goto bad;
606 }
607 if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
608 nd6log((LOG_ERR,
609 "nd6_na_input: a solicited adv is multicasted\n"));
610 goto bad;
611 }
612
613 icmp6len -= sizeof(*nd_na);
614 if (nd6_options(nd_na + 1, icmp6len, &ndopts) < 0) {
615 nd6log((LOG_INFO,
616 "nd6_na_input: invalid ND option, ignored\n"));
617 /* nd6_options have incremented stats */
618 goto freeit;
619 }
620
621 if (IN6_IS_ADDR_MULTICAST(&daddr6) && !ndopts.nd_opts_tgt_lladdr) {
622 nd6log((LOG_INFO,
623 "nd6_na_input: multicast adv without TLLA\n"));
624 goto bad;
625 }
626
627 if (ndopts.nd_opts_tgt_lladdr) {
628 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
629 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
630 }
631
632 ifa6 = in6ifa_ifpwithaddr(ifp, &taddr6);
633 ifa = ifa6 ? &ifa6->ia_ifa : NULL;
634
635 /*
636 * Target address matches one of my interface address.
637 *
638 * If my address is tentative, this means that there's somebody
639 * already using the same address as mine. This indicates DAD failure.
640 * This is defined in RFC 2462.
641 *
642 * Otherwise, process as defined in RFC 2461.
643 */
644 if (ifa && (ifatoia6(ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
645 struct dadq *dp;
646
647 dp = nd6_dad_find(ifa);
648 if (dp) {
649 dp->dad_na_icount++;
650
651 /* remove the address. */
652 nd6_dad_duplicated(dp);
653 }
654 goto freeit;
655 }
656
657 if (ifa) {
658 #if NCARP > 0
659 /*
660 * Ignore NAs silently for carp addresses if we're not
661 * the CARP master.
662 */
663 if (ifp->if_type == IFT_CARP && !carp_iamatch(ifp))
664 goto freeit;
665 #endif
666 log(LOG_ERR,
667 "nd6_na_input: duplicate IP6 address %s\n",
668 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)));
669 goto freeit;
670 }
671
672 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
673 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
674 "(if %d, NA packet %d)\n",
675 inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)),
676 ifp->if_addrlen, lladdrlen - 2));
677 goto bad;
678 }
679
680 /* Check if we already have this neighbor in our cache. */
681 rt = nd6_lookup(&taddr6, 0, ifp, ifp->if_rdomain);
682
683 /*
684 * If we are a router, we may create new stale cache entries upon
685 * receiving Unsolicited Neighbor Advertisements.
686 */
687 if (rt == NULL && ip6_forwarding == 1) {
688 rt = nd6_lookup(&taddr6, 1, ifp, ifp->if_rdomain);
689 if (rt == NULL || lladdr == NULL ||
690 ((sdl = satosdl(rt->rt_gateway)) == NULL))
691 goto freeit;
692
693 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
694 sdl->sdl_alen = ifp->if_addrlen;
695 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
696
697 /*
698 * RFC9131 6.1.1
699 *
700 * Routers SHOULD create a new entry for the target address
701 * with the reachability state set to STALE.
702 */
703 ln->ln_state = ND6_LLINFO_STALE;
704 nd6_llinfo_settimer(ln, nd6_gctimer);
705
706 goto freeit;
707 }
708
709 /*
710 * Host:
711 * If no neighbor cache entry is found, NA SHOULD silently be
712 * discarded.
713 */
714 if ((rt == NULL) ||
715 ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
716 ((sdl = satosdl(rt->rt_gateway)) == NULL))
717 goto freeit;
718
719 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
720 /*
721 * If the link-layer has address, and no lladdr option came,
722 * discard the packet.
723 */
724 if (ifp->if_addrlen && !lladdr)
725 goto freeit;
726
727 /*
728 * Record link-layer address, and update the state.
729 */
730 sdl->sdl_alen = ifp->if_addrlen;
731 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
732 if (is_solicited) {
733 ln->ln_state = ND6_LLINFO_REACHABLE;
734 ln->ln_byhint = 0;
735 /* Notify userland that a new ND entry is reachable. */
736 rtm_send(rt, RTM_RESOLVE, 0, ifp->if_rdomain);
737 if (!ND6_LLINFO_PERMANENT(ln)) {
738 nd6_llinfo_settimer(ln,
739 ifp->if_nd->reachable);
740 }
741 } else {
742 ln->ln_state = ND6_LLINFO_STALE;
743 nd6_llinfo_settimer(ln, nd6_gctimer);
744 }
745 if ((ln->ln_router = is_router) != 0) {
746 /*
747 * This means a router's state has changed from
748 * non-reachable to probably reachable, and might
749 * affect the status of associated prefixes..
750 */
751 if ((rt->rt_flags & RTF_LLINFO) == 0)
752 goto freeit; /* ln is gone */
753 }
754 } else {
755 int llchange;
756
757 /*
758 * Check if the link-layer address has changed or not.
759 */
760 if (!lladdr)
761 llchange = 0;
762 else {
763 if (sdl->sdl_alen) {
764 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
765 llchange = 1;
766 else
767 llchange = 0;
768 } else
769 llchange = 1;
770 }
771
772 /*
773 * This is VERY complex. Look at it with care.
774 *
775 * override solicit lladdr llchange action
776 * (L: record lladdr)
777 *
778 * 0 0 n -- (2c)
779 * 0 0 y n (2b) L
780 * 0 0 y y (1) REACHABLE->STALE
781 * 0 1 n -- (2c) *->REACHABLE
782 * 0 1 y n (2b) L *->REACHABLE
783 * 0 1 y y (1) REACHABLE->STALE
784 * 1 0 n -- (2a)
785 * 1 0 y n (2a) L
786 * 1 0 y y (2a) L *->STALE
787 * 1 1 n -- (2a) *->REACHABLE
788 * 1 1 y n (2a) L *->REACHABLE
789 * 1 1 y y (2a) L *->REACHABLE
790 */
791 if (!is_override && (lladdr && llchange)) { /* (1) */
792 /*
793 * If state is REACHABLE, make it STALE.
794 * no other updates should be done.
795 */
796 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
797 ln->ln_state = ND6_LLINFO_STALE;
798 nd6_llinfo_settimer(ln, nd6_gctimer);
799 }
800 goto freeit;
801 } else if (is_override /* (2a) */
802 || (!is_override && (lladdr && !llchange)) /* (2b) */
803 || !lladdr) { /* (2c) */
804 /*
805 * Update link-local address, if any.
806 */
807 if (llchange) {
808 log(LOG_INFO, "ndp info overwritten for %s "
809 "by %s on %s\n",
810 inet_ntop(AF_INET6, &taddr6,
811 addr, sizeof(addr)),
812 ether_sprintf(lladdr), ifp->if_xname);
813 }
814 if (lladdr) {
815 sdl->sdl_alen = ifp->if_addrlen;
816 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
817 }
818
819 /*
820 * If solicited, make the state REACHABLE.
821 * If not solicited and the link-layer address was
822 * changed, make it STALE.
823 */
824 if (is_solicited) {
825 ln->ln_state = ND6_LLINFO_REACHABLE;
826 ln->ln_byhint = 0;
827 if (!ND6_LLINFO_PERMANENT(ln)) {
828 nd6_llinfo_settimer(ln,
829 ifp->if_nd->reachable);
830 }
831 } else {
832 if (lladdr && llchange) {
833 ln->ln_state = ND6_LLINFO_STALE;
834 nd6_llinfo_settimer(ln, nd6_gctimer);
835 }
836 }
837 }
838
839 if (ln->ln_router && !is_router) {
840 if (!ip6_forwarding) {
841 /*
842 * The neighbor may be used
843 * as a next hop for some destinations
844 * (e.g. redirect case). So we must
845 * call rt6_flush explicitly.
846 */
847 rt6_flush(&ip6->ip6_src, ifp);
848 }
849 }
850 ln->ln_router = is_router;
851 }
852 rt->rt_flags &= ~RTF_REJECT;
853 ln->ln_asked = 0;
854 if_output_mq(ifp, &ln->ln_mq, &ln_hold_total, rt_key(rt), rt);
855
856 freeit:
857 rtfree(rt);
858 m_freem(m);
859 if_put(ifp);
860 return;
861
862 bad:
863 icmp6stat_inc(icp6s_badna);
864 m_freem(m);
865 if_put(ifp);
866 }
867
868 /*
869 * Neighbor advertisement output handling.
870 *
871 * Based on RFC 2461
872 *
873 * the following items are not implemented yet:
874 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
875 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
876 *
877 * tlladdr - 1 if include target link-layer address
878 * sdl0 - sockaddr_dl (= proxy NA) or NULL
879 */
880 void
nd6_na_output(struct ifnet * ifp,const struct in6_addr * daddr6,const struct in6_addr * taddr6,u_long flags,int tlladdr,struct sockaddr * sdl0)881 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6,
882 const struct in6_addr *taddr6, u_long flags, int tlladdr,
883 struct sockaddr *sdl0)
884 {
885 struct mbuf *m;
886 struct rtentry *rt = NULL;
887 struct ip6_hdr *ip6;
888 struct nd_neighbor_advert *nd_na;
889 struct ip6_moptions im6o;
890 struct sockaddr_in6 dst_sa;
891 int icmp6len, maxlen;
892 caddr_t mac = NULL;
893
894 #if NCARP > 0
895 /* Do not send NAs for carp addresses if we're not the CARP master. */
896 if (ifp->if_type == IFT_CARP && !carp_iamatch(ifp))
897 return;
898 #endif
899
900 /* estimate the size of message */
901 maxlen = sizeof(*ip6) + sizeof(*nd_na);
902 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
903 #ifdef DIAGNOSTIC
904 if (max_linkhdr + maxlen >= MCLBYTES) {
905 printf("%s: max_linkhdr + maxlen >= MCLBYTES (%d + %d > %d)\n",
906 __func__, max_linkhdr, maxlen, MCLBYTES);
907 panic("%s: insufficient MCLBYTES", __func__);
908 /* NOTREACHED */
909 }
910 #endif
911
912 MGETHDR(m, M_DONTWAIT, MT_DATA);
913 if (m && max_linkhdr + maxlen >= MHLEN) {
914 MCLGET(m, M_DONTWAIT);
915 if ((m->m_flags & M_EXT) == 0) {
916 m_free(m);
917 m = NULL;
918 }
919 }
920 if (m == NULL)
921 return;
922 m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
923
924 if (IN6_IS_ADDR_MULTICAST(daddr6)) {
925 m->m_flags |= M_MCAST;
926 im6o.im6o_ifidx = ifp->if_index;
927 im6o.im6o_hlim = 255;
928 im6o.im6o_loop = 0;
929 }
930
931 icmp6len = sizeof(*nd_na);
932 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
933 m_align(m, maxlen);
934
935 /* fill neighbor advertisement packet */
936 ip6 = mtod(m, struct ip6_hdr *);
937 ip6->ip6_flow = 0;
938 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
939 ip6->ip6_vfc |= IPV6_VERSION;
940 ip6->ip6_nxt = IPPROTO_ICMPV6;
941 ip6->ip6_hlim = 255;
942 bzero(&dst_sa, sizeof(dst_sa));
943 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
944 dst_sa.sin6_family = AF_INET6;
945 dst_sa.sin6_addr = *daddr6;
946 if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
947 /* reply to DAD */
948 dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
949 dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
950 dst_sa.sin6_addr.s6_addr32[1] = 0;
951 dst_sa.sin6_addr.s6_addr32[2] = 0;
952 dst_sa.sin6_addr.s6_addr32[3] = __IPV6_ADDR_INT32_ONE;
953
954 flags &= ~ND_NA_FLAG_SOLICITED;
955 }
956 ip6->ip6_dst = dst_sa.sin6_addr;
957
958 /*
959 * Select a source whose scope is the same as that of the dest.
960 */
961 rt = rtalloc(sin6tosa(&dst_sa), RT_RESOLVE, ifp->if_rdomain);
962 if (!rtisvalid(rt)) {
963 char addr[INET6_ADDRSTRLEN];
964
965 nd6log((LOG_DEBUG, "%s: source can't be determined: dst=%s\n",
966 __func__, inet_ntop(AF_INET6, &dst_sa.sin6_addr, addr,
967 sizeof(addr))));
968 rtfree(rt);
969 goto bad;
970 }
971 ip6->ip6_src = ifatoia6(rt->rt_ifa)->ia_addr.sin6_addr;
972 rtfree(rt);
973 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
974 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
975 nd_na->nd_na_code = 0;
976 nd_na->nd_na_target = *taddr6;
977 if (IN6_IS_SCOPE_EMBED(&nd_na->nd_na_target))
978 nd_na->nd_na_target.s6_addr16[1] = 0;
979
980 /*
981 * "tlladdr" indicates NS's condition for adding tlladdr or not.
982 * see nd6_ns_input() for details.
983 * Basically, if NS packet is sent to unicast/anycast addr,
984 * target lladdr option SHOULD NOT be included.
985 */
986 if (tlladdr) {
987 /*
988 * sdl0 != NULL indicates proxy NA. If we do proxy, use
989 * lladdr in sdl0. If we are not proxying (sending NA for
990 * my address) use lladdr configured for the interface.
991 */
992 if (sdl0 == NULL) {
993 mac = nd6_ifptomac(ifp);
994 } else if (sdl0->sa_family == AF_LINK) {
995 struct sockaddr_dl *sdl;
996 sdl = satosdl(sdl0);
997 if (sdl->sdl_alen == ifp->if_addrlen)
998 mac = LLADDR(sdl);
999 }
1000 }
1001 if (tlladdr && mac) {
1002 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1003 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1004
1005 /* roundup to 8 bytes alignment! */
1006 optlen = (optlen + 7) & ~7;
1007
1008 m->m_pkthdr.len += optlen;
1009 m->m_len += optlen;
1010 icmp6len += optlen;
1011 bzero((caddr_t)nd_opt, optlen);
1012 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1013 nd_opt->nd_opt_len = optlen >> 3;
1014 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1015 } else
1016 flags &= ~ND_NA_FLAG_OVERRIDE;
1017
1018 ip6->ip6_plen = htons((u_short)icmp6len);
1019 nd_na->nd_na_flags_reserved = flags;
1020 nd_na->nd_na_cksum = 0;
1021 m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
1022
1023 ip6_output(m, NULL, NULL, 0, &im6o, NULL);
1024 icmp6stat_inc(icp6s_outhist + ND_NEIGHBOR_ADVERT);
1025 return;
1026
1027 bad:
1028 m_freem(m);
1029 }
1030
1031 caddr_t
nd6_ifptomac(struct ifnet * ifp)1032 nd6_ifptomac(struct ifnet *ifp)
1033 {
1034 switch (ifp->if_type) {
1035 case IFT_ETHER:
1036 case IFT_IEEE1394:
1037 case IFT_PROPVIRTUAL:
1038 case IFT_CARP:
1039 case IFT_IEEE80211:
1040 return ((caddr_t)(ifp + 1));
1041 default:
1042 return NULL;
1043 }
1044 }
1045
1046 struct dadq *
nd6_dad_find(struct ifaddr * ifa)1047 nd6_dad_find(struct ifaddr *ifa)
1048 {
1049 struct dadq *dp;
1050
1051 TAILQ_FOREACH(dp, &dadq, dad_list) {
1052 if (dp->dad_ifa == ifa)
1053 return dp;
1054 }
1055 return NULL;
1056 }
1057
1058 void
nd6_dad_destroy(struct dadq * dp)1059 nd6_dad_destroy(struct dadq *dp)
1060 {
1061 TAILQ_REMOVE(&dadq, dp, dad_list);
1062 ifafree(dp->dad_ifa);
1063 free(dp, M_IP6NDP, sizeof(*dp));
1064 ip6_dad_pending--;
1065 }
1066
1067 void
nd6_dad_starttimer(struct dadq * dp)1068 nd6_dad_starttimer(struct dadq *dp)
1069 {
1070 timeout_set_proc(&dp->dad_timer_ch, nd6_dad_timer, dp->dad_ifa);
1071 timeout_add_msec(&dp->dad_timer_ch, RETRANS_TIMER);
1072 }
1073
1074 void
nd6_dad_stoptimer(struct dadq * dp)1075 nd6_dad_stoptimer(struct dadq *dp)
1076 {
1077 timeout_del(&dp->dad_timer_ch);
1078 }
1079
1080 /*
1081 * Start Duplicated Address Detection (DAD) for specified interface address.
1082 */
1083 void
nd6_dad_start(struct ifaddr * ifa)1084 nd6_dad_start(struct ifaddr *ifa)
1085 {
1086 struct in6_ifaddr *ia6 = ifatoia6(ifa);
1087 struct dadq *dp;
1088 char addr[INET6_ADDRSTRLEN];
1089
1090 NET_ASSERT_LOCKED();
1091
1092 /*
1093 * If we don't need DAD, don't do it.
1094 * There are several cases:
1095 * - DAD is disabled (ip6_dad_count == 0)
1096 * - the interface address is anycast
1097 */
1098 KASSERT(ia6->ia6_flags & IN6_IFF_TENTATIVE);
1099 if ((ia6->ia6_flags & IN6_IFF_ANYCAST) || (!ip6_dad_count)) {
1100 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1101
1102 rtm_addr(RTM_CHGADDRATTR, ifa);
1103
1104 return;
1105 }
1106
1107 /* DAD already in progress */
1108 if (nd6_dad_find(ifa) != NULL)
1109 return;
1110
1111 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1112 if (dp == NULL) {
1113 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1114 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1115 addr, sizeof(addr)),
1116 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1117 return;
1118 }
1119
1120 TAILQ_INSERT_TAIL(&dadq, dp, dad_list);
1121 ip6_dad_pending++;
1122
1123 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", ifa->ifa_ifp->if_xname,
1124 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr))));
1125
1126 /*
1127 * Send NS packet for DAD, ip6_dad_count times.
1128 * Note that we must delay the first transmission, if this is the
1129 * first packet to be sent from the interface after interface
1130 * (re)initialization.
1131 */
1132 dp->dad_ifa = ifaref(ifa);
1133 dp->dad_count = ip6_dad_count;
1134 dp->dad_ns_icount = dp->dad_na_icount = 0;
1135 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1136 nd6_dad_ns_output(dp, ifa);
1137 nd6_dad_starttimer(dp);
1138 }
1139
1140 /*
1141 * terminate DAD unconditionally. used for address removals.
1142 */
1143 void
nd6_dad_stop(struct ifaddr * ifa)1144 nd6_dad_stop(struct ifaddr *ifa)
1145 {
1146 struct dadq *dp;
1147
1148 dp = nd6_dad_find(ifa);
1149 if (!dp) {
1150 /* DAD wasn't started yet */
1151 return;
1152 }
1153
1154 nd6_dad_stoptimer(dp);
1155 nd6_dad_destroy(dp);
1156 }
1157
1158 void
nd6_dad_timer(void * xifa)1159 nd6_dad_timer(void *xifa)
1160 {
1161 struct ifaddr *ifa;
1162 struct in6_ifaddr *ia6;
1163 struct in6_addr daddr6, taddr6;
1164 struct ifnet *ifp;
1165 struct dadq *dp;
1166 char addr[INET6_ADDRSTRLEN];
1167
1168 NET_LOCK();
1169
1170 /* Sanity check */
1171 if (xifa == NULL) {
1172 log(LOG_ERR, "%s: called with null parameter\n", __func__);
1173 goto done;
1174 }
1175 ifa = xifa;
1176 ia6 = ifatoia6(ifa);
1177 taddr6 = ia6->ia_addr.sin6_addr;
1178 ifp = ifa->ifa_ifp;
1179 dp = nd6_dad_find(ifa);
1180 if (dp == NULL) {
1181 log(LOG_ERR, "%s: DAD structure not found\n", __func__);
1182 goto done;
1183 }
1184 if (ia6->ia6_flags & IN6_IFF_DUPLICATED) {
1185 log(LOG_ERR, "%s: called with duplicated address %s(%s)\n",
1186 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1187 addr, sizeof(addr)),
1188 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1189 goto done;
1190 }
1191 if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1192 log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1193 __func__, inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1194 addr, sizeof(addr)),
1195 ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
1196 goto done;
1197 }
1198
1199 /* timeouted with IFF_{RUNNING,UP} check */
1200 if (dp->dad_ns_tcount > dad_maxtry) {
1201 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1202 ifa->ifa_ifp->if_xname));
1203
1204 nd6_dad_destroy(dp);
1205 goto done;
1206 }
1207
1208 /* Need more checks? */
1209 if (dp->dad_ns_ocount < dp->dad_count) {
1210 /*
1211 * We have more NS to go. Send NS packet for DAD.
1212 */
1213 nd6_dad_ns_output(dp, ifa);
1214 nd6_dad_starttimer(dp);
1215 } else {
1216 /*
1217 * We have transmitted sufficient number of DAD packets.
1218 */
1219 if (dp->dad_na_icount || dp->dad_ns_icount) {
1220 /* dp will be freed in nd6_dad_duplicated() */
1221 nd6_dad_duplicated(dp);
1222 } else {
1223 /*
1224 * We are done with DAD. No NA came, no NS came.
1225 */
1226 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1227
1228 rtm_addr(RTM_CHGADDRATTR, ifa);
1229
1230 nd6log((LOG_DEBUG,
1231 "%s: DAD complete for %s - no duplicates found\n",
1232 ifa->ifa_ifp->if_xname,
1233 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
1234 addr, sizeof(addr))));
1235
1236 daddr6 = in6addr_linklocal_allrouters;
1237 daddr6.s6_addr16[1] = htons(ifp->if_index);
1238 /* RFC9131 - inform routers about our new address */
1239 nd6_na_output(ifp, &daddr6, &taddr6, 0, 1, NULL);
1240
1241 nd6_dad_destroy(dp);
1242 }
1243 }
1244
1245 done:
1246 NET_UNLOCK();
1247 }
1248
1249 void
nd6_dad_duplicated(struct dadq * dp)1250 nd6_dad_duplicated(struct dadq *dp)
1251 {
1252 struct in6_ifaddr *ia6 = ifatoia6(dp->dad_ifa);
1253 char addr[INET6_ADDRSTRLEN];
1254
1255 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1256 "NS in/out=%d/%d, NA in=%d\n",
1257 ia6->ia_ifp->if_xname,
1258 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)),
1259 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1260
1261 ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
1262 ia6->ia6_flags |= IN6_IFF_DUPLICATED;
1263
1264 /* We are done with DAD, with duplicated address found. (failure) */
1265 nd6_dad_stoptimer(dp);
1266
1267 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1268 ia6->ia_ifp->if_xname,
1269 inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)));
1270 log(LOG_ERR, "%s: manual intervention required\n",
1271 ia6->ia_ifp->if_xname);
1272
1273 rtm_addr(RTM_CHGADDRATTR, dp->dad_ifa);
1274
1275 nd6_dad_destroy(dp);
1276 }
1277
1278 void
nd6_dad_ns_output(struct dadq * dp,struct ifaddr * ifa)1279 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1280 {
1281 struct in6_ifaddr *ia6 = ifatoia6(ifa);
1282 struct ifnet *ifp = ifa->ifa_ifp;
1283
1284 dp->dad_ns_tcount++;
1285 if ((ifp->if_flags & IFF_UP) == 0) {
1286 #if 0
1287 printf("%s: interface down?\n", ifp->if_xname);
1288 #endif
1289 return;
1290 }
1291 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1292 #if 0
1293 printf("%s: interface not running?\n", ifp->if_xname);
1294 #endif
1295 return;
1296 }
1297
1298 dp->dad_ns_ocount++;
1299 nd6_ns_output(ifp, NULL, &ia6->ia_addr.sin6_addr, NULL, 1);
1300 }
1301
1302 void
nd6_dad_ns_input(struct ifaddr * ifa)1303 nd6_dad_ns_input(struct ifaddr *ifa)
1304 {
1305 struct dadq *dp;
1306
1307 if (!ifa)
1308 panic("%s: ifa == NULL", __func__);
1309
1310 dp = nd6_dad_find(ifa);
1311 if (dp == NULL) {
1312 log(LOG_ERR, "%s: DAD structure not found\n", __func__);
1313 return;
1314 }
1315
1316 /*
1317 * if I'm yet to start DAD, someone else started using this address
1318 * first. I have a duplicate and you win.
1319 */
1320 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1321 if (dp->dad_ns_ocount == 0) {
1322 /* dp will be freed in nd6_dad_duplicated() */
1323 nd6_dad_duplicated(dp);
1324 } else {
1325 /*
1326 * not sure if I got a duplicate.
1327 * increment ns count and see what happens.
1328 */
1329 dp->dad_ns_icount++;
1330 }
1331 }
1332
1333 /*
1334 * Check whether ``addr'' is a neighbor address connected to ``ifp''.
1335 */
1336 int
nd6_isneighbor(const struct ifnet * ifp,const struct in6_addr * addr)1337 nd6_isneighbor(const struct ifnet *ifp, const struct in6_addr *addr)
1338 {
1339 struct rtentry *rt;
1340 struct sockaddr_in6 sin6;
1341 unsigned int tableid = ifp->if_rdomain;
1342 int rv = 0;
1343
1344 memset(&sin6, 0, sizeof(sin6));
1345 sin6.sin6_len = sizeof(struct sockaddr_in6);
1346 sin6.sin6_family = AF_INET6;
1347 sin6.sin6_addr = *addr;
1348 rt = rtalloc(sin6tosa(&sin6), 0, tableid);
1349
1350 if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_CLONING|RTF_CLONED))
1351 rv = if_isconnected(ifp, rt->rt_ifidx);
1352
1353 rtfree(rt);
1354 return (rv);
1355 }
1356