1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
32 */
33
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/counter.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/libkern.h>
44 #include <sys/lock.h>
45 #include <sys/rwlock.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/queue.h>
55 #include <sys/callout.h>
56 #include <sys/refcount.h>
57
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
61 #include <net/if_var.h>
62 #include <net/if_private.h>
63 #include <net/route.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <net/if_llatbl.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/in6_ifattach.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
74 #include <netinet6/nd6.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/ip_carp.h>
77 #include <netinet6/send.h>
78
79 #include <machine/atomic.h>
80
81 #define SDL(s) ((struct sockaddr_dl *)s)
82
83 MALLOC_DECLARE(M_IP6NDP);
84
85 struct dadq;
86 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
87 static void nd6_dad_add(struct dadq *dp);
88 static void nd6_dad_del(struct dadq *dp);
89 static void nd6_dad_rele(struct dadq *);
90 static void nd6_dad_starttimer(struct dadq *, int);
91 static void nd6_dad_stoptimer(struct dadq *);
92 static void nd6_dad_timer(void *);
93 static void nd6_dad_duplicated(struct ifaddr *, struct dadq *);
94 static void nd6_dad_ns_output(struct dadq *);
95 static void nd6_dad_ns_input(struct ifaddr *, struct nd_opt_nonce *);
96 static void nd6_dad_na_input(struct ifaddr *);
97 static void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
98 const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
99 static void nd6_ns_output_fib(struct ifnet *, const struct in6_addr *,
100 const struct in6_addr *, const struct in6_addr *, uint8_t *, u_int);
101 static void nd6_queue_add(struct ifaddr *, struct in6_addr *,
102 struct in6_addr *, struct sockaddr_dl *, int, uint32_t);
103
104 static struct ifaddr *nd6_proxy_fill_sdl(struct ifnet *,
105 const struct in6_addr *, struct sockaddr_dl *);
106
107 VNET_DEFINE_STATIC(int, dad_enhanced) = 1;
108 #define V_dad_enhanced VNET(dad_enhanced)
109
110 SYSCTL_DECL(_net_inet6_ip6);
111 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_VNET | CTLFLAG_RW,
112 &VNET_NAME(dad_enhanced), 0,
113 "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD.");
114
115 VNET_DEFINE_STATIC(int, dad_maxtry) = 15; /* max # of *tries* to
116 transmit DAD packet */
117 #define V_dad_maxtry VNET(dad_maxtry)
118
119 VNET_DEFINE_STATIC(int, nd6_onlink_ns_rfc4861) = 0;
120 #define V_nd6_onlink_ns_rfc4861 VNET(nd6_onlink_ns_rfc4861)
121 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_ONLINKNSRFC4861,
122 nd6_onlink_ns_rfc4861, CTLFLAG_VNET | CTLFLAG_RW,
123 &VNET_NAME(nd6_onlink_ns_rfc4861), 0,
124 "Accept 'on-link' ICMPv6 NS messages in compliance with RFC 4861");
125
126 struct nd_queue {
127 TAILQ_ENTRY(nd_queue) ndq_list;
128 struct ifaddr *ndq_ifa;
129 struct in6_addr ndq_daddr;
130 struct in6_addr ndq_taddr;
131 struct sockaddr_dl ndq_sdl;
132 uint32_t ndq_flags;
133 struct callout ndq_callout;
134 };
135
136 /*
137 * Input a Neighbor Solicitation Message.
138 *
139 * Based on RFC 2461
140 * Based on RFC 2462 (duplicate address detection)
141 */
142 void
nd6_ns_input(struct mbuf * m,int off,int icmp6len)143 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
144 {
145 struct ifnet *ifp;
146 struct ip6_hdr *ip6;
147 struct nd_neighbor_solicit *nd_ns;
148 struct in6_addr daddr6, myaddr6, saddr6, taddr6;
149 struct ifaddr *ifa;
150 struct sockaddr_dl proxydl;
151 union nd_opts ndopts;
152 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
153 char *lladdr;
154 int rflag, tentative, tlladdr;
155 u_int lladdr_pad, lladdrlen;
156 uint32_t dflags;
157
158 ifa = NULL;
159
160 /* RFC 6980: Nodes MUST silently ignore fragments */
161 if(m->m_flags & M_FRAGMENTED)
162 goto freeit;
163
164 ifp = m->m_pkthdr.rcvif;
165 ip6 = mtod(m, struct ip6_hdr *);
166 if (__predict_false(ip6->ip6_hlim != 255)) {
167 ICMP6STAT_INC(icp6s_invlhlim);
168 nd6log((LOG_ERR,
169 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
170 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
171 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
172 goto bads;
173 }
174
175 if (m->m_len < off + icmp6len) {
176 m = m_pullup(m, off + icmp6len);
177 if (m == NULL) {
178 IP6STAT_INC(ip6s_exthdrtoolong);
179 return;
180 }
181 }
182 ip6 = mtod(m, struct ip6_hdr *);
183 nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
184
185 saddr6 = ip6->ip6_src;
186 daddr6 = ip6->ip6_dst;
187 taddr6 = nd_ns->nd_ns_target;
188 if (in6_setscope(&taddr6, ifp, NULL) != 0)
189 goto bad;
190
191 rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
192 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
193 rflag = 0;
194
195 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
196 /* dst has to be a solicited node multicast address. */
197 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
198 /* don't check ifindex portion */
199 daddr6.s6_addr32[1] == 0 &&
200 daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
201 daddr6.s6_addr8[12] == 0xff) {
202 ; /* good */
203 } else {
204 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
205 "(wrong ip6 dst)\n"));
206 goto bad;
207 }
208 } else if (!V_nd6_onlink_ns_rfc4861) {
209 struct sockaddr_in6 src_sa6;
210
211 /*
212 * According to recent IETF discussions, it is not a good idea
213 * to accept a NS from an address which would not be deemed
214 * to be a neighbor otherwise. This point is expected to be
215 * clarified in future revisions of the specification.
216 */
217 bzero(&src_sa6, sizeof(src_sa6));
218 src_sa6.sin6_family = AF_INET6;
219 src_sa6.sin6_len = sizeof(src_sa6);
220 src_sa6.sin6_addr = saddr6;
221 if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
222 nd6log((LOG_INFO, "nd6_ns_input: "
223 "NS packet from non-neighbor\n"));
224 goto bad;
225 }
226 }
227
228 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
229 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
230 goto bad;
231 }
232
233 icmp6len -= sizeof(*nd_ns);
234 nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
235 if (nd6_options(&ndopts) < 0) {
236 nd6log((LOG_INFO,
237 "nd6_ns_input: invalid ND option, ignored\n"));
238 /* nd6_options have incremented stats */
239 goto freeit;
240 }
241
242 lladdr = NULL;
243 lladdrlen = 0;
244 lladdr_pad = nd6_lladdr_opt_pad(ifp);
245 if (ndopts.nd_opts_src_lladdr) {
246 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1) + lladdr_pad;
247 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
248 }
249
250 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
251 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
252 "(link-layer address option)\n"));
253 goto bad;
254 }
255
256 /*
257 * Attaching target link-layer address to the NA?
258 * (RFC 2461 7.2.4)
259 *
260 * NS IP dst is unicast/anycast MUST NOT add
261 * NS IP dst is solicited-node multicast MUST add
262 *
263 * In implementation, we add target link-layer address by default.
264 * We do not add one in MUST NOT cases.
265 */
266 tlladdr = 0;
267 if (IN6_IS_ADDR_MULTICAST(&daddr6))
268 tlladdr |= ND6_NA_OPT_LLA;
269
270 /*
271 * Target address (taddr6) must be either:
272 * (1) Valid unicast/anycast address for my receiving interface,
273 * (2) Unicast address for which I'm offering proxy service, or
274 * (3) "tentative" address on which DAD is being performed.
275 */
276 /* (1) and (3) check. */
277 if (ifp->if_carp) {
278 ifa = (*carp_iamatch6_p)(ifp, &taddr6);
279 if (ifa != NULL)
280 tlladdr |= ND6_NA_CARP_MASTER;
281 } else
282 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
283
284 /* (2) check. */
285 dflags = 0;
286 if (ifa == NULL) {
287 if ((ifa = nd6_proxy_fill_sdl(ifp, &taddr6, &proxydl)) != NULL)
288 dflags |= ND6_QUEUE_FLAG_PROXY;
289 }
290 if (ifa == NULL) {
291 /*
292 * We've got an NS packet, and we don't have that address
293 * assigned for us. We MUST silently ignore it.
294 * See RFC2461 7.2.3.
295 */
296 goto freeit;
297 }
298 if ((((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) != 0)
299 dflags |= ND6_QUEUE_FLAG_ANYCAST;
300 myaddr6 = *IFA_IN6(ifa);
301 tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
302 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
303 goto freeit;
304
305 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
306 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
307 "(if %d, NS packet %u)\n",
308 ip6_sprintf(ip6bufs, &taddr6),
309 ifp->if_addrlen, lladdrlen - 2 - lladdr_pad));
310 goto bad;
311 }
312
313 if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
314 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
315 ip6_sprintf(ip6bufs, &saddr6)));
316 goto freeit;
317 }
318
319 /*
320 * We have neighbor solicitation packet, with target address equals to
321 * one of my tentative address.
322 *
323 * src addr how to process?
324 * --- ---
325 * multicast of course, invalid (rejected in ip6_input)
326 * unicast somebody is doing address resolution -> ignore
327 * unspec dup address detection
328 *
329 * The processing is defined in RFC 2462.
330 */
331 if (tentative) {
332 /*
333 * If source address is unspecified address, it is for
334 * duplicate address detection.
335 *
336 * If not, the packet is for addess resolution;
337 * silently ignore it.
338 */
339 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
340 nd6_dad_ns_input(ifa, ndopts.nd_opts_nonce);
341
342 goto freeit;
343 }
344
345 /*
346 * If the Target Address is either an anycast address or a unicast
347 * address for which the node is providing proxy service, or the Target
348 * Link-Layer Address option is not included, the Override flag SHOULD
349 * be set to zero. Otherwise, the Override flag SHOULD be set to one.
350 */
351 if (dflags == 0 && (tlladdr & ND6_NA_OPT_LLA) != 0)
352 rflag |= ND_NA_FLAG_OVERRIDE;
353 /*
354 * If the source address is unspecified address, entries must not
355 * be created or updated.
356 * It looks that sender is performing DAD. nd6_na_output() will
357 * send NA toward all-node multicast address, to tell the sender
358 * that I'm using the address.
359 * S bit ("solicited") must be zero.
360 */
361 if (!IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
362 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
363 ND_NEIGHBOR_SOLICIT, 0);
364 rflag |= ND_NA_FLAG_SOLICITED;
365 }
366
367 /*
368 * RFC 4861, anycast or proxy NA sent in response to a NS SHOULD
369 * be delayed by a random time between 0 and MAX_ANYCAST_DELAY_TIME
370 * to reduce the probability of network congestion.
371 */
372 if (dflags == 0)
373 nd6_na_output_fib(ifp, &saddr6, &taddr6, rflag, tlladdr, NULL, M_GETFIB(m));
374 else
375 nd6_queue_add(ifa, &saddr6, &taddr6, &proxydl, arc4random() %
376 (MAX_ANYCAST_DELAY_TIME * hz), dflags);
377 freeit:
378 if (ifa != NULL)
379 ifa_free(ifa);
380 m_freem(m);
381 return;
382
383 bad:
384 nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
385 ip6_sprintf(ip6bufs, &saddr6)));
386 nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
387 ip6_sprintf(ip6bufs, &daddr6)));
388 nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
389 ip6_sprintf(ip6bufs, &taddr6)));
390 bads:
391 ICMP6STAT_INC(icp6s_badns);
392 if (ifa != NULL)
393 ifa_free(ifa);
394 m_freem(m);
395 }
396
397 static struct ifaddr *
nd6_proxy_fill_sdl(struct ifnet * ifp,const struct in6_addr * taddr6,struct sockaddr_dl * sdl)398 nd6_proxy_fill_sdl(struct ifnet *ifp, const struct in6_addr *taddr6,
399 struct sockaddr_dl *sdl)
400 {
401 struct ifaddr *ifa;
402 struct llentry *ln;
403
404 ifa = NULL;
405 ln = nd6_lookup(taddr6, LLE_SF(AF_INET6, 0), ifp);
406 if (ln == NULL)
407 return (ifa);
408 if ((ln->la_flags & (LLE_PUB | LLE_VALID)) == (LLE_PUB | LLE_VALID)) {
409 link_init_sdl(ifp, (struct sockaddr *)sdl, ifp->if_type);
410 sdl->sdl_alen = ifp->if_addrlen;
411 bcopy(ln->ll_addr, &sdl->sdl_data, ifp->if_addrlen);
412 LLE_RUNLOCK(ln);
413 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
414 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
415 } else
416 LLE_RUNLOCK(ln);
417
418 return (ifa);
419 }
420
421 /*
422 * Output a Neighbor Solicitation Message. Caller specifies:
423 * - ICMP6 header source IP6 address
424 * - ND6 header target IP6 address
425 * - ND6 header source datalink address
426 *
427 * Based on RFC 2461
428 * Based on RFC 2462 (duplicate address detection)
429 *
430 * ln - for source address determination
431 * nonce - If non-NULL, NS is used for duplicate address detection and
432 * the value (length is ND_OPT_NONCE_LEN) is used as a random nonce.
433 */
434 static void
nd6_ns_output_fib(struct ifnet * ifp,const struct in6_addr * saddr6,const struct in6_addr * daddr6,const struct in6_addr * taddr6,uint8_t * nonce,u_int fibnum)435 nd6_ns_output_fib(struct ifnet *ifp, const struct in6_addr *saddr6,
436 const struct in6_addr *daddr6, const struct in6_addr *taddr6,
437 uint8_t *nonce, u_int fibnum)
438 {
439 struct mbuf *m;
440 struct m_tag *mtag;
441 struct ip6_hdr *ip6;
442 struct nd_neighbor_solicit *nd_ns;
443 struct ip6_moptions im6o;
444 int icmp6len;
445 int maxlen;
446
447 NET_EPOCH_ASSERT();
448
449 if (IN6_IS_ADDR_MULTICAST(taddr6))
450 return;
451
452 /* estimate the size of message */
453 maxlen = sizeof(*ip6) + sizeof(*nd_ns);
454 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
455 KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
456 "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
457 __func__, max_linkhdr, maxlen, MCLBYTES));
458
459 if (max_linkhdr + maxlen > MHLEN)
460 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
461 else
462 m = m_gethdr(M_NOWAIT, MT_DATA);
463 if (m == NULL)
464 return;
465 M_SETFIB(m, fibnum);
466
467 icmp6len = sizeof(*nd_ns);
468 m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
469 m->m_data += max_linkhdr; /* or M_ALIGN() equivalent? */
470
471 /* fill neighbor solicitation packet */
472 ip6 = mtod(m, struct ip6_hdr *);
473 ip6->ip6_flow = 0;
474 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
475 ip6->ip6_vfc |= IPV6_VERSION;
476 /* ip6->ip6_plen will be set later */
477 ip6->ip6_nxt = IPPROTO_ICMPV6;
478 ip6->ip6_hlim = 255;
479 if (daddr6)
480 ip6->ip6_dst = *daddr6;
481 else {
482 ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
483 ip6->ip6_dst.s6_addr16[1] = 0;
484 ip6->ip6_dst.s6_addr32[1] = 0;
485 ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
486 ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
487 ip6->ip6_dst.s6_addr8[12] = 0xff;
488 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
489 goto bad;
490 }
491 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
492 m->m_flags |= M_MCAST;
493 im6o.im6o_multicast_ifp = ifp;
494 im6o.im6o_multicast_hlim = 255;
495 im6o.im6o_multicast_loop = 0;
496 }
497 if (nonce == NULL) {
498 char ip6buf[INET6_ADDRSTRLEN];
499 struct ifaddr *ifa = NULL;
500
501 /*
502 * RFC2461 7.2.2:
503 * "If the source address of the packet prompting the
504 * solicitation is the same as one of the addresses assigned
505 * to the outgoing interface, that address SHOULD be placed
506 * in the IP Source Address of the outgoing solicitation.
507 * Otherwise, any one of the addresses assigned to the
508 * interface should be used."
509 *
510 * We use the source address for the prompting packet
511 * (saddr6), if saddr6 belongs to the outgoing interface.
512 * Otherwise, we perform the source address selection as usual.
513 */
514 if (saddr6 != NULL)
515 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, saddr6);
516 if (ifa == NULL) {
517 int error;
518
519 error = in6_selectsrc_nbr(fibnum, &ip6->ip6_dst, &im6o,
520 ifp, &ip6->ip6_src);
521 if (error) {
522 nd6log((LOG_DEBUG, "%s: source can't be "
523 "determined: dst=%s, error=%d\n", __func__,
524 ip6_sprintf(ip6buf, &ip6->ip6_dst),
525 error));
526 goto bad;
527 }
528 } else
529 ip6->ip6_src = *saddr6;
530
531 if (ifp->if_carp != NULL) {
532 /*
533 * Check that selected source address belongs to
534 * CARP addresses.
535 */
536 if (ifa == NULL)
537 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
538 &ip6->ip6_src);
539 /*
540 * Do not send NS for CARP address if we are not
541 * the CARP master.
542 */
543 if (ifa != NULL && ifa->ifa_carp != NULL &&
544 !(*carp_master_p)(ifa)) {
545 nd6log((LOG_DEBUG,
546 "nd6_ns_output: NS from BACKUP CARP address %s\n",
547 ip6_sprintf(ip6buf, &ip6->ip6_src)));
548 ifa_free(ifa);
549 goto bad;
550 }
551 }
552 if (ifa != NULL)
553 ifa_free(ifa);
554 } else {
555 /*
556 * Source address for DAD packet must always be IPv6
557 * unspecified address. (0::0)
558 * We actually don't have to 0-clear the address (we did it
559 * above), but we do so here explicitly to make the intention
560 * clearer.
561 */
562 bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
563 }
564 nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
565 nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
566 nd_ns->nd_ns_code = 0;
567 nd_ns->nd_ns_reserved = 0;
568 nd_ns->nd_ns_target = *taddr6;
569 in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
570
571 /*
572 * Add source link-layer address option.
573 *
574 * spec implementation
575 * --- ---
576 * DAD packet MUST NOT do not add the option
577 * there's no link layer address:
578 * impossible do not add the option
579 * there's link layer address:
580 * Multicast NS MUST add one add the option
581 * Unicast NS SHOULD add one add the option
582 */
583 if (nonce == NULL) {
584 struct nd_opt_hdr *nd_opt;
585 char *mac;
586 int optlen;
587 u_int pad;
588
589 mac = NULL;
590 if (ifp->if_carp)
591 mac = (*carp_macmatch6_p)(ifp, m, &ip6->ip6_src);
592 if (mac == NULL)
593 mac = nd6_ifptomac(ifp);
594
595 if (mac != NULL) {
596 nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
597 pad = nd6_lladdr_opt_pad(ifp);
598 optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
599 /* 8 byte alignments... */
600 optlen = (optlen + 7) & ~7;
601 m->m_pkthdr.len += optlen;
602 m->m_len += optlen;
603 icmp6len += optlen;
604 bzero(nd_opt, optlen);
605 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
606 nd_opt->nd_opt_len = optlen >> 3;
607 memcpy((char *)(nd_opt + 1) + pad, mac,
608 ifp->if_addrlen);
609 }
610 }
611 /*
612 * Add a Nonce option (RFC 3971) to detect looped back NS messages.
613 * This behavior is documented as Enhanced Duplicate Address
614 * Detection in RFC 7527.
615 * net.inet6.ip6.dad_enhanced=0 disables this.
616 */
617 if (V_dad_enhanced != 0 && nonce != NULL) {
618 int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
619 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
620 /* 8-byte alignment is required. */
621 optlen = (optlen + 7) & ~7;
622
623 m->m_pkthdr.len += optlen;
624 m->m_len += optlen;
625 icmp6len += optlen;
626 bzero((caddr_t)nd_opt, optlen);
627 nd_opt->nd_opt_type = ND_OPT_NONCE;
628 nd_opt->nd_opt_len = optlen >> 3;
629 bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN);
630 }
631 ip6->ip6_plen = htons((u_short)icmp6len);
632 nd_ns->nd_ns_cksum = 0;
633 nd_ns->nd_ns_cksum =
634 in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
635
636 if (send_sendso_input_hook != NULL) {
637 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
638 sizeof(unsigned short), M_NOWAIT);
639 if (mtag == NULL)
640 goto bad;
641 *(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
642 m_tag_prepend(m, mtag);
643 }
644
645 ip6_output(m, NULL, NULL, (nonce != NULL) ? IPV6_UNSPECSRC : 0,
646 &im6o, NULL, NULL);
647 icmp6_ifstat_inc(ifp, ifs6_out_msg);
648 icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
649 ICMP6STAT_INC2(icp6s_outhist, ND_NEIGHBOR_SOLICIT);
650
651 return;
652
653 bad:
654 m_freem(m);
655 }
656
657 #ifndef BURN_BRIDGES
658 void
nd6_ns_output(struct ifnet * ifp,const struct in6_addr * saddr6,const struct in6_addr * daddr6,const struct in6_addr * taddr6,uint8_t * nonce)659 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *saddr6,
660 const struct in6_addr *daddr6, const struct in6_addr *taddr6,uint8_t *nonce)
661 {
662
663 nd6_ns_output_fib(ifp, saddr6, daddr6, taddr6, nonce, RT_DEFAULT_FIB);
664 }
665 #endif
666 /*
667 * Neighbor advertisement input handling.
668 *
669 * Based on RFC 2461
670 * Based on RFC 2462 (duplicate address detection)
671 */
672 void
nd6_na_input(struct mbuf * m,int off,int icmp6len)673 nd6_na_input(struct mbuf *m, int off, int icmp6len)
674 {
675 struct ifnet *ifp;
676 struct ip6_hdr *ip6;
677 struct ifaddr *ifa;
678 struct llentry *ln;
679 struct mbuf *chain;
680 struct nd_neighbor_advert *nd_na;
681 struct in6_addr daddr6, taddr6;
682 union nd_opts ndopts;
683 u_char linkhdr[LLE_MAX_LINKHDR];
684 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
685 char *lladdr;
686 size_t linkhdrsize;
687 int flags, is_override, is_router, is_solicited;
688 int lladdr_off, checklink;
689 u_int lladdr_pad, lladdrlen;
690 bool flush_holdchain = false;
691
692 NET_EPOCH_ASSERT();
693
694 chain = NULL;
695 ln = NULL;
696 checklink = 0;
697
698 /* RFC 6980: Nodes MUST silently ignore fragments */
699 if(m->m_flags & M_FRAGMENTED)
700 goto freeit;
701
702 ifp = m->m_pkthdr.rcvif;
703 ip6 = mtod(m, struct ip6_hdr *);
704 if (__predict_false(ip6->ip6_hlim != 255)) {
705 ICMP6STAT_INC(icp6s_invlhlim);
706 nd6log((LOG_ERR,
707 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
708 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
709 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
710 goto bad;
711 }
712
713 if (m->m_len < off + icmp6len) {
714 m = m_pullup(m, off + icmp6len);
715 if (m == NULL) {
716 IP6STAT_INC(ip6s_exthdrtoolong);
717 return;
718 }
719 }
720 ip6 = mtod(m, struct ip6_hdr *);
721 nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
722
723 flags = nd_na->nd_na_flags_reserved;
724 is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
725 is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
726 is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
727
728 taddr6 = nd_na->nd_na_target;
729 if (in6_setscope(&taddr6, ifp, NULL))
730 goto bad; /* XXX: impossible */
731
732 if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
733 nd6log((LOG_ERR,
734 "nd6_na_input: invalid target address %s\n",
735 ip6_sprintf(ip6bufs, &taddr6)));
736 goto bad;
737 }
738
739 daddr6 = ip6->ip6_dst;
740 if (IN6_IS_ADDR_MULTICAST(&daddr6))
741 if (is_solicited) {
742 nd6log((LOG_ERR,
743 "nd6_na_input: a solicited adv is multicasted\n"));
744 goto bad;
745 }
746
747 icmp6len -= sizeof(*nd_na);
748 nd6_option_init(nd_na + 1, icmp6len, &ndopts);
749 if (nd6_options(&ndopts) < 0) {
750 nd6log((LOG_INFO,
751 "nd6_na_input: invalid ND option, ignored\n"));
752 /* nd6_options have incremented stats */
753 goto freeit;
754 }
755
756 lladdr = NULL;
757 lladdrlen = 0;
758 lladdr_pad = nd6_lladdr_opt_pad(ifp);
759 if (ndopts.nd_opts_tgt_lladdr) {
760 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1) + lladdr_pad;
761 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
762 }
763
764 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
765 if (ifa != NULL && ifa->ifa_carp != NULL) {
766 /*
767 * Silently ignore NAs for CARP addresses if we are not
768 * the CARP master.
769 */
770 if (!(*carp_master_p)(ifa)) {
771 nd6log((LOG_DEBUG,
772 "nd6_na_input: NA for BACKUP CARP address %s\n",
773 ip6_sprintf(ip6bufs, &taddr6)));
774 ifa_free(ifa);
775 goto freeit;
776 }
777 }
778 /*
779 * Target address matches one of my interface address.
780 *
781 * If my address is tentative, this means that there's somebody
782 * already using the same address as mine. This indicates DAD failure.
783 * This is defined in RFC 2462.
784 *
785 * Otherwise, process as defined in RFC 2461.
786 */
787 if (ifa
788 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
789 nd6_dad_na_input(ifa);
790 ifa_free(ifa);
791 goto freeit;
792 }
793
794 /* Just for safety, maybe unnecessary. */
795 if (ifa) {
796 ifa_free(ifa);
797 log(LOG_ERR,
798 "nd6_na_input: duplicate IP6 address %s\n",
799 ip6_sprintf(ip6bufs, &taddr6));
800 goto freeit;
801 }
802
803 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
804 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
805 "(if %d, NA packet %u)\n", ip6_sprintf(ip6bufs, &taddr6),
806 ifp->if_addrlen, lladdrlen - 2 - lladdr_pad));
807 goto bad;
808 }
809
810 /*
811 * If no neighbor cache entry is found, NA SHOULD silently be
812 * discarded.
813 */
814 ln = nd6_lookup(&taddr6, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp);
815 if (ln == NULL) {
816 goto freeit;
817 }
818
819 /*
820 * Do not try to override static entry.
821 */
822 if (ln->la_flags & LLE_STATIC)
823 goto freeit;
824
825 if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
826 /*
827 * If the link-layer has address, and no lladdr option came,
828 * discard the packet.
829 */
830 if (ifp->if_addrlen && lladdr == NULL) {
831 goto freeit;
832 }
833
834 /*
835 * Record link-layer address, and update the state.
836 */
837 if (!nd6_try_set_entry_addr(ifp, ln, lladdr))
838 goto freeit;
839
840 flush_holdchain = true;
841 if (is_solicited)
842 nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
843 else
844 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
845 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
846 if ((ln->ln_router = is_router) != 0) {
847 /*
848 * This means a router's state has changed from
849 * non-reachable to probably reachable, and might
850 * affect the status of associated prefixes..
851 */
852 checklink = 1;
853 }
854 } else {
855 int llchange;
856
857 /*
858 * Check if the link-layer address has changed or not.
859 */
860 if (lladdr == NULL)
861 llchange = 0;
862 else {
863 if (ln->la_flags & LLE_VALID) {
864 if (bcmp(lladdr, ln->ll_addr, ifp->if_addrlen))
865 llchange = 1;
866 else
867 llchange = 0;
868 } else
869 llchange = 1;
870 }
871
872 /*
873 * This is VERY complex. Look at it with care.
874 *
875 * override solicit lladdr llchange action
876 * (L: record lladdr)
877 *
878 * 0 0 n -- (2c)
879 * 0 0 y n (2b) L
880 * 0 0 y y (1) REACHABLE->STALE
881 * 0 1 n -- (2c) *->REACHABLE
882 * 0 1 y n (2b) L *->REACHABLE
883 * 0 1 y y (1) REACHABLE->STALE
884 * 1 0 n -- (2a)
885 * 1 0 y n (2a) L
886 * 1 0 y y (2a) L *->STALE
887 * 1 1 n -- (2a) *->REACHABLE
888 * 1 1 y n (2a) L *->REACHABLE
889 * 1 1 y y (2a) L *->REACHABLE
890 */
891 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
892 /*
893 * If state is REACHABLE, make it STALE.
894 * no other updates should be done.
895 */
896 if (ln->ln_state == ND6_LLINFO_REACHABLE)
897 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
898 goto freeit;
899 } else if (is_override /* (2a) */
900 || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
901 || lladdr == NULL) { /* (2c) */
902 /*
903 * Update link-local address, if any.
904 */
905 if (lladdr != NULL) {
906 linkhdrsize = sizeof(linkhdr);
907 if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
908 linkhdr, &linkhdrsize, &lladdr_off) != 0)
909 goto freeit;
910 if (lltable_try_set_entry_addr(ifp, ln, linkhdr,
911 linkhdrsize, lladdr_off) == 0)
912 goto freeit;
913 EVENTHANDLER_INVOKE(lle_event, ln,
914 LLENTRY_RESOLVED);
915 }
916
917 /*
918 * If solicited, make the state REACHABLE.
919 * If not solicited and the link-layer address was
920 * changed, make it STALE.
921 */
922 if (is_solicited)
923 nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
924 else {
925 if (lladdr != NULL && llchange)
926 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
927 }
928 }
929
930 if (ln->ln_router && !is_router) {
931 /*
932 * The peer dropped the router flag.
933 * Remove the sender from the Default Router List and
934 * update the Destination Cache entries.
935 */
936 struct ifnet *nd6_ifp;
937
938 nd6_ifp = lltable_get_ifp(ln->lle_tbl);
939 if (!defrouter_remove(&ln->r_l3addr.addr6, nd6_ifp) &&
940 (nd6_ifp->if_inet6->nd_flags &
941 ND6_IFF_ACCEPT_RTADV) != 0)
942 /*
943 * Even if the neighbor is not in the default
944 * router list, the neighbor may be used as a
945 * next hop for some destinations (e.g. redirect
946 * case). So we must call rt6_flush explicitly.
947 */
948 rt6_flush(&ip6->ip6_src, ifp);
949 }
950 ln->ln_router = is_router;
951 }
952 /* XXX - QL
953 * Does this matter?
954 * rt->rt_flags &= ~RTF_REJECT;
955 */
956 ln->la_asked = 0;
957 if (ln->la_hold != NULL)
958 chain = nd6_grab_holdchain(ln);
959 freeit:
960 if (ln != NULL)
961 LLE_WUNLOCK(ln);
962
963 if (chain != NULL)
964 nd6_flush_holdchain(ifp, ln, chain);
965 if (flush_holdchain)
966 nd6_flush_children_holdchain(ifp, ln);
967
968 if (checklink)
969 pfxlist_onlink_check();
970
971 m_freem(m);
972 return;
973
974 bad:
975 if (ln != NULL)
976 LLE_WUNLOCK(ln);
977
978 ICMP6STAT_INC(icp6s_badna);
979 m_freem(m);
980 }
981
982 /*
983 * Neighbor advertisement output handling.
984 *
985 * Based on RFC 2461
986 *
987 * tlladdr:
988 * - 0x01 if include target link-layer address
989 * - 0x02 if target address is CARP MASTER
990 * sdl0 - sockaddr_dl (= proxy NA) or NULL
991 */
992 static void
nd6_na_output_fib(struct ifnet * ifp,const struct in6_addr * daddr6_0,const struct in6_addr * taddr6,u_long flags,int tlladdr,struct sockaddr * sdl0,u_int fibnum)993 nd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
994 const struct in6_addr *taddr6, u_long flags, int tlladdr,
995 struct sockaddr *sdl0, u_int fibnum)
996 {
997 struct mbuf *m;
998 struct m_tag *mtag;
999 struct ip6_hdr *ip6;
1000 struct nd_neighbor_advert *nd_na;
1001 struct ip6_moptions im6o;
1002 struct in6_addr daddr6;
1003
1004 NET_EPOCH_ASSERT();
1005
1006 int icmp6len, maxlen, error;
1007 caddr_t mac = NULL;
1008
1009 daddr6 = *daddr6_0; /* make a local copy for modification */
1010
1011 /* estimate the size of message */
1012 maxlen = sizeof(*ip6) + sizeof(*nd_na);
1013 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
1014 KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
1015 "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
1016 __func__, max_linkhdr, maxlen, MCLBYTES));
1017
1018 if (max_linkhdr + maxlen > MHLEN)
1019 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1020 else
1021 m = m_gethdr(M_NOWAIT, MT_DATA);
1022 if (m == NULL)
1023 return;
1024 M_SETFIB(m, fibnum);
1025
1026 icmp6len = sizeof(*nd_na);
1027 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
1028 m->m_data += max_linkhdr; /* or M_ALIGN() equivalent? */
1029
1030 /* fill neighbor advertisement packet */
1031 ip6 = mtod(m, struct ip6_hdr *);
1032 ip6->ip6_flow = 0;
1033 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1034 ip6->ip6_vfc |= IPV6_VERSION;
1035 ip6->ip6_nxt = IPPROTO_ICMPV6;
1036 ip6->ip6_hlim = 255;
1037
1038 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
1039 /* reply to DAD */
1040 daddr6 = in6addr_linklocal_allnodes;
1041 if (in6_setscope(&daddr6, ifp, NULL))
1042 goto bad;
1043
1044 flags &= ~ND_NA_FLAG_SOLICITED;
1045 }
1046 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1047 m->m_flags |= M_MCAST;
1048 im6o.im6o_multicast_ifp = ifp;
1049 im6o.im6o_multicast_hlim = 255;
1050 im6o.im6o_multicast_loop = 0;
1051 }
1052
1053 ip6->ip6_dst = daddr6;
1054 error = in6_selectsrc_nbr(fibnum, &daddr6, &im6o, ifp, &ip6->ip6_src);
1055 if (error) {
1056 char ip6buf[INET6_ADDRSTRLEN];
1057 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1058 "determined: dst=%s, error=%d\n",
1059 ip6_sprintf(ip6buf, &daddr6), error));
1060 goto bad;
1061 }
1062 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1063 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1064 nd_na->nd_na_code = 0;
1065 nd_na->nd_na_target = *taddr6;
1066 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1067
1068 /*
1069 * If we respond from CARP address, we need to prepare mac address
1070 * for carp_output().
1071 */
1072 if (ifp->if_carp && (tlladdr & ND6_NA_CARP_MASTER))
1073 mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1074 /*
1075 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1076 * see nd6_ns_input() for details.
1077 * Basically, if NS packet is sent to unicast/anycast addr,
1078 * target lladdr option SHOULD NOT be included.
1079 */
1080 if (tlladdr & ND6_NA_OPT_LLA) {
1081 /*
1082 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1083 * lladdr in sdl0. If we are not proxying (sending NA for
1084 * my address) use lladdr configured for the interface.
1085 */
1086 if (sdl0 == NULL) {
1087 if (mac == NULL)
1088 mac = nd6_ifptomac(ifp);
1089 } else if (sdl0->sa_family == AF_LINK) {
1090 struct sockaddr_dl *sdl;
1091 sdl = (struct sockaddr_dl *)sdl0;
1092 if (sdl->sdl_alen == ifp->if_addrlen)
1093 mac = LLADDR(sdl);
1094 }
1095 }
1096 if ((tlladdr & ND6_NA_OPT_LLA) && mac != NULL) {
1097 u_int pad = nd6_lladdr_opt_pad(ifp);
1098 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1099 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1100
1101 /* roundup to 8 bytes alignment! */
1102 optlen = (optlen + 7) & ~7;
1103
1104 m->m_pkthdr.len += optlen;
1105 m->m_len += optlen;
1106 icmp6len += optlen;
1107 bzero((caddr_t)nd_opt, optlen);
1108 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1109 nd_opt->nd_opt_len = optlen >> 3;
1110 memcpy((char *)(nd_opt + 1) + pad, mac, ifp->if_addrlen);
1111 } else
1112 flags &= ~ND_NA_FLAG_OVERRIDE;
1113
1114 ip6->ip6_plen = htons((u_short)icmp6len);
1115 nd_na->nd_na_flags_reserved = flags;
1116 nd_na->nd_na_cksum = 0;
1117 nd_na->nd_na_cksum =
1118 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1119
1120 if (send_sendso_input_hook != NULL) {
1121 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1122 sizeof(unsigned short), M_NOWAIT);
1123 if (mtag == NULL)
1124 goto bad;
1125 *(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1126 m_tag_prepend(m, mtag);
1127 }
1128
1129 ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
1130 icmp6_ifstat_inc(ifp, ifs6_out_msg);
1131 icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1132 ICMP6STAT_INC2(icp6s_outhist, ND_NEIGHBOR_ADVERT);
1133
1134 return;
1135
1136 bad:
1137 m_freem(m);
1138 }
1139
1140 #ifndef BURN_BRIDGES
1141 void
nd6_na_output(struct ifnet * ifp,const struct in6_addr * daddr6_0,const struct in6_addr * taddr6,u_long flags,int tlladdr,struct sockaddr * sdl0)1142 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
1143 const struct in6_addr *taddr6, u_long flags, int tlladdr,
1144 struct sockaddr *sdl0)
1145 {
1146
1147 nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
1148 RT_DEFAULT_FIB);
1149 }
1150 #endif
1151
1152 caddr_t
nd6_ifptomac(struct ifnet * ifp)1153 nd6_ifptomac(struct ifnet *ifp)
1154 {
1155 switch (ifp->if_type) {
1156 case IFT_ETHER:
1157 case IFT_IEEE1394:
1158 case IFT_L2VLAN:
1159 case IFT_INFINIBAND:
1160 case IFT_BRIDGE:
1161 return IF_LLADDR(ifp);
1162 default:
1163 return NULL;
1164 }
1165 }
1166
1167 /*
1168 * Number of reserved octets between the header of an ND link-layer
1169 * address option and the link-layer address itself. On IPoIB links
1170 * two zero octets are prepended to the 20-octet link-layer address
1171 * to fill the 24-octet option (RFC 4391, section 9.3); on all other
1172 * link types the address immediately follows the option header.
1173 */
1174 u_int
nd6_lladdr_opt_pad(struct ifnet * ifp)1175 nd6_lladdr_opt_pad(struct ifnet *ifp)
1176 {
1177 switch (ifp->if_type) {
1178 case IFT_INFINIBAND:
1179 case IFT_INFINIBANDLAG:
1180 return (2);
1181 default:
1182 return (0);
1183 }
1184 }
1185
1186 struct dadq {
1187 TAILQ_ENTRY(dadq) dad_list;
1188 struct ifaddr *dad_ifa;
1189 int dad_count; /* max NS to send */
1190 int dad_ns_tcount; /* # of trials to send NS */
1191 int dad_ns_ocount; /* NS sent so far */
1192 int dad_ns_icount;
1193 int dad_na_icount;
1194 int dad_ns_lcount; /* looped back NS */
1195 int dad_loopbackprobe; /* probing state for loopback detection */
1196 struct callout dad_timer_ch;
1197 struct vnet *dad_vnet;
1198 u_int dad_refcnt;
1199 #define ND_OPT_NONCE_LEN32 \
1200 ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
1201 uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
1202 bool dad_ondadq; /* on dadq? Protected by DADQ_WLOCK. */
1203 };
1204
1205 VNET_DEFINE_STATIC(TAILQ_HEAD(, dadq), dadq);
1206 VNET_DEFINE_STATIC(struct rwlock, dad_rwlock);
1207 #define V_dadq VNET(dadq)
1208 #define V_dad_rwlock VNET(dad_rwlock)
1209
1210 #define DADQ_LOCKPTR() (&V_dad_rwlock)
1211 #define DADQ_LOCK_INIT() rw_init(DADQ_LOCKPTR(), "nd6 DAD queue")
1212 #define DADQ_RLOCK() rw_rlock(DADQ_LOCKPTR())
1213 #define DADQ_RUNLOCK() rw_runlock(DADQ_LOCKPTR())
1214 #define DADQ_WLOCK() rw_wlock(DADQ_LOCKPTR())
1215 #define DADQ_WUNLOCK() rw_wunlock(DADQ_LOCKPTR())
1216
1217 #define DADQ_LOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_LOCKED);
1218 #define DADQ_RLOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_RLOCKED);
1219 #define DADQ_WLOCK_ASSERT() rw_assert(DADQ_LOCKPTR(), RA_WLOCKED);
1220
1221 static void
nd6_dad_add(struct dadq * dp)1222 nd6_dad_add(struct dadq *dp)
1223 {
1224 DADQ_WLOCK_ASSERT();
1225
1226 TAILQ_INSERT_TAIL(&V_dadq, dp, dad_list);
1227 dp->dad_ondadq = true;
1228 }
1229
1230 static void
nd6_dad_del(struct dadq * dp)1231 nd6_dad_del(struct dadq *dp)
1232 {
1233 DADQ_WLOCK_ASSERT();
1234
1235 if (dp->dad_ondadq) {
1236 /*
1237 * Remove dp from the dadq and release the dadq's
1238 * reference.
1239 */
1240 TAILQ_REMOVE(&V_dadq, dp, dad_list);
1241 dp->dad_ondadq = false;
1242 nd6_dad_rele(dp);
1243 }
1244 }
1245
1246 static struct dadq *
nd6_dad_find(struct ifaddr * ifa,struct nd_opt_nonce * n)1247 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *n)
1248 {
1249 struct dadq *dp;
1250
1251 DADQ_LOCK_ASSERT();
1252
1253 TAILQ_FOREACH(dp, &V_dadq, dad_list) {
1254 if (dp->dad_ifa != ifa)
1255 continue;
1256
1257 /*
1258 * Skip if the nonce matches the received one.
1259 * +2 in the length is required because of type and
1260 * length fields are included in a header.
1261 */
1262 if (n != NULL &&
1263 n->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
1264 memcmp(&n->nd_opt_nonce[0], &dp->dad_nonce[0],
1265 ND_OPT_NONCE_LEN) == 0) {
1266 dp->dad_ns_lcount++;
1267 continue;
1268 }
1269 break;
1270 }
1271
1272 return (dp);
1273 }
1274
1275 static void
nd6_dad_starttimer(struct dadq * dp,int ticks)1276 nd6_dad_starttimer(struct dadq *dp, int ticks)
1277 {
1278 DADQ_WLOCK_ASSERT();
1279
1280 callout_reset(&dp->dad_timer_ch, ticks, nd6_dad_timer, dp);
1281 }
1282
1283 static void
nd6_dad_stoptimer(struct dadq * dp)1284 nd6_dad_stoptimer(struct dadq *dp)
1285 {
1286 callout_drain(&dp->dad_timer_ch);
1287 }
1288
1289 static void
nd6_dad_rele(struct dadq * dp)1290 nd6_dad_rele(struct dadq *dp)
1291 {
1292 if (refcount_release(&dp->dad_refcnt)) {
1293 KASSERT(!dp->dad_ondadq, ("dp %p still on DAD queue", dp));
1294 ifa_free(dp->dad_ifa);
1295 free(dp, M_IP6NDP);
1296 }
1297 }
1298
1299 void
nd6_dad_init(void)1300 nd6_dad_init(void)
1301 {
1302 DADQ_LOCK_INIT();
1303 TAILQ_INIT(&V_dadq);
1304 }
1305
1306 /*
1307 * Start Duplicate Address Detection (DAD) for specified interface address.
1308 */
1309 void
nd6_dad_start(struct ifaddr * ifa,int delay)1310 nd6_dad_start(struct ifaddr *ifa, int delay)
1311 {
1312 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1313 struct dadq *dp;
1314 char ip6buf[INET6_ADDRSTRLEN];
1315
1316 KASSERT((ia->ia6_flags & IN6_IFF_TENTATIVE) != 0,
1317 ("starting DAD on non-tentative address %p", ifa));
1318
1319 /*
1320 * If we don't need DAD, don't do it.
1321 * There are several cases:
1322 * - DAD is disabled globally or on the interface
1323 * - the interface address is anycast
1324 */
1325 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0 ||
1326 V_ip6_dad_count == 0 ||
1327 (ifa->ifa_ifp->if_inet6->nd_flags & ND6_IFF_NO_DAD) != 0) {
1328 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1329 return;
1330 }
1331 if ((ifa->ifa_ifp->if_flags & IFF_UP) == 0 ||
1332 (ifa->ifa_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1333 (ifa->ifa_ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) != 0)
1334 return;
1335
1336 DADQ_WLOCK();
1337 if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
1338 /*
1339 * DAD is already in progress. Let the existing entry
1340 * finish it.
1341 */
1342 DADQ_WUNLOCK();
1343 return;
1344 }
1345
1346 dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1347 if (dp == NULL) {
1348 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1349 "%s(%s)\n",
1350 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1351 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1352 return;
1353 }
1354 callout_init_rw(&dp->dad_timer_ch, DADQ_LOCKPTR(),
1355 CALLOUT_RETURNUNLOCKED);
1356 #ifdef VIMAGE
1357 dp->dad_vnet = curvnet;
1358 #endif
1359 nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1360 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1361
1362 /*
1363 * Send NS packet for DAD, ip6_dad_count times.
1364 * Note that we must delay the first transmission, if this is the
1365 * first packet to be sent from the interface after interface
1366 * (re)initialization.
1367 */
1368 dp->dad_ifa = ifa;
1369 ifa_ref(dp->dad_ifa);
1370 dp->dad_count = V_ip6_dad_count;
1371 dp->dad_ns_icount = dp->dad_na_icount = 0;
1372 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1373 dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
1374
1375 /* Add this to the dadq and add a reference for the dadq. */
1376 refcount_init(&dp->dad_refcnt, 1);
1377 nd6_dad_add(dp);
1378 nd6_dad_starttimer(dp, delay);
1379 DADQ_WUNLOCK();
1380 }
1381
1382 /*
1383 * terminate DAD unconditionally. used for address removals.
1384 */
1385 void
nd6_dad_stop(struct ifaddr * ifa)1386 nd6_dad_stop(struct ifaddr *ifa)
1387 {
1388 struct dadq *dp;
1389
1390 DADQ_WLOCK();
1391 dp = nd6_dad_find(ifa, NULL);
1392 if (dp == NULL) {
1393 DADQ_WUNLOCK();
1394 /* DAD wasn't started yet */
1395 return;
1396 }
1397
1398 /*
1399 * Acquire a temporary reference so that we can safely stop the callout.
1400 */
1401 (void)refcount_acquire(&dp->dad_refcnt);
1402 nd6_dad_del(dp);
1403 DADQ_WUNLOCK();
1404
1405 nd6_dad_stoptimer(dp);
1406 nd6_dad_rele(dp);
1407 }
1408
1409 static void
nd6_dad_timer(void * arg)1410 nd6_dad_timer(void *arg)
1411 {
1412 struct dadq *dp = arg;
1413 struct ifaddr *ifa = dp->dad_ifa;
1414 struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
1415 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1416 char ip6buf[INET6_ADDRSTRLEN];
1417 struct epoch_tracker et;
1418
1419 CURVNET_SET(dp->dad_vnet);
1420 KASSERT(ia != NULL, ("DAD entry %p with no address", dp));
1421
1422 NET_EPOCH_ENTER(et);
1423 if (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) {
1424 /* Do not need DAD for ifdisabled interface. */
1425 log(LOG_ERR, "nd6_dad_timer: cancel DAD on %s because of "
1426 "ND6_IFF_IFDISABLED.\n", ifp->if_xname);
1427 goto err;
1428 }
1429 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1430 log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1431 "%s(%s)\n",
1432 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1433 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1434 goto err;
1435 }
1436 if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1437 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1438 "%s(%s)\n",
1439 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1440 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1441 goto err;
1442 }
1443
1444 /* Stop DAD if the interface is down even after dad_maxtry attempts. */
1445 if ((dp->dad_ns_tcount > V_dad_maxtry) &&
1446 (((ifp->if_flags & IFF_UP) == 0) ||
1447 ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0))) {
1448 nd6log((LOG_INFO, "%s: could not run DAD "
1449 "because the interface was down or not running.\n",
1450 if_name(ifa->ifa_ifp)));
1451 goto err;
1452 }
1453
1454 /* Need more checks? */
1455 if (dp->dad_ns_ocount < dp->dad_count) {
1456 /*
1457 * We have more NS to go. Send NS packet for DAD.
1458 */
1459 nd6_dad_starttimer(dp,
1460 (long)ifa->ifa_ifp->if_inet6->nd_retrans * hz / 1000);
1461 nd6_dad_ns_output(dp);
1462 goto done;
1463 } else {
1464 /*
1465 * We have transmitted sufficient number of DAD packets.
1466 * See what we've got.
1467 */
1468 if (dp->dad_ns_icount > 0 || dp->dad_na_icount > 0) {
1469 /* We've seen NS or NA, means DAD has failed. */
1470 nd6_dad_duplicated(ifa, dp);
1471 } else if (V_dad_enhanced != 0 &&
1472 dp->dad_ns_lcount > 0 &&
1473 dp->dad_ns_lcount > dp->dad_loopbackprobe) {
1474 /*
1475 * Sec. 4.1 in RFC 7527 requires transmission of
1476 * additional probes until the loopback condition
1477 * becomes clear when a looped back probe is detected.
1478 */
1479 log(LOG_ERR, "%s: a looped back NS message is "
1480 "detected during DAD for %s. "
1481 "Another DAD probes are being sent.\n",
1482 if_name(ifa->ifa_ifp),
1483 ip6_sprintf(ip6buf, IFA_IN6(ifa)));
1484 dp->dad_loopbackprobe = dp->dad_ns_lcount;
1485 /*
1486 * Send an NS immediately and increase dad_count by
1487 * V_nd6_mmaxtries - 1.
1488 */
1489 dp->dad_count =
1490 dp->dad_ns_ocount + V_nd6_mmaxtries - 1;
1491 nd6_dad_starttimer(dp,
1492 (long)ifa->ifa_ifp->if_inet6->nd_retrans * hz / 1000);
1493 nd6_dad_ns_output(dp);
1494 goto done;
1495 } else {
1496 /*
1497 * We are done with DAD. No NA came, no NS came.
1498 * No duplicate address found. Check IFDISABLED flag
1499 * again in case that it is changed between the
1500 * beginning of this function and here.
1501 *
1502 * Reset DAD failures counter if using stable addresses.
1503 */
1504 if ((ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) == 0) {
1505 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1506 if ((ifp->if_inet6->nd_flags & ND6_IFF_STABLEADDR) && !(ia->ia6_flags & IN6_IFF_TEMPORARY))
1507 atomic_store_int(&DAD_FAILURES(ifp), 0);
1508 /*
1509 * RFC 9131 Section 6.1.2: The first advertisement
1510 * SHOULD be sent as soon as an address changes the
1511 * state from tentative to preferred.
1512 */
1513 nd6_grand_start(ifa, ND6_QUEUE_FLAG_NEWGUA);
1514 }
1515
1516 nd6log((LOG_DEBUG,
1517 "%s: DAD complete for %s - no duplicates found\n",
1518 if_name(ifa->ifa_ifp),
1519 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1520 if (dp->dad_ns_lcount > 0)
1521 log(LOG_ERR, "%s: DAD completed while "
1522 "a looped back NS message is detected "
1523 "during DAD for %s.\n",
1524 if_name(ifa->ifa_ifp),
1525 ip6_sprintf(ip6buf, IFA_IN6(ifa)));
1526 }
1527 }
1528 err:
1529 nd6_dad_del(dp);
1530 DADQ_WUNLOCK();
1531 done:
1532 NET_EPOCH_EXIT(et);
1533 CURVNET_RESTORE();
1534 }
1535
1536 static void
nd6_dad_duplicated(struct ifaddr * ifa,struct dadq * dp)1537 nd6_dad_duplicated(struct ifaddr *ifa, struct dadq *dp)
1538 {
1539 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1540 struct ifnet *ifp;
1541 char ip6buf[INET6_ADDRSTRLEN];
1542
1543 ifp = ifa->ifa_ifp;
1544
1545 log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1546 "NS in/out/loopback=%d/%d/%d, NA in=%d\n",
1547 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1548 dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
1549 dp->dad_na_icount);
1550
1551 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1552 ia->ia6_flags |= IN6_IFF_DUPLICATED;
1553
1554 log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1555 if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1556
1557 /*
1558 * For RFC 7217 stable addresses, increment failure counter here if we still have retries.
1559 * More addresses will be generated as long as retries are not exhausted.
1560 */
1561 if ((ifp->if_inet6->nd_flags & ND6_IFF_STABLEADDR) && !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
1562 u_int dad_failures = atomic_load_int(&DAD_FAILURES(ifp));
1563
1564 if (dad_failures <= V_ip6_stableaddr_maxretries) {
1565 atomic_add_int(&DAD_FAILURES(ifp), 1);
1566 /* if retries exhausted, output an informative error message */
1567 if (dad_failures == V_ip6_stableaddr_maxretries)
1568 log(LOG_ERR, "%s: manual intervention required, consider disabling \"stableaddr\" on the interface"
1569 " or checking hostuuid for uniqueness\n",
1570 if_name(ifp));
1571 }
1572 } else {
1573 log(LOG_ERR, "%s: manual intervention required\n",
1574 if_name(ifp));
1575 }
1576
1577 /*
1578 * If the address is a link-local address formed from an interface
1579 * identifier based on the hardware address which is supposed to be
1580 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1581 * operation on the interface SHOULD be disabled.
1582 * [RFC 4862, Section 5.4.5]
1583 */
1584 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1585 struct in6_addr in6;
1586
1587 /*
1588 * To avoid over-reaction, we only apply this logic when we are
1589 * very sure that hardware addresses are supposed to be unique.
1590 */
1591 switch (ifp->if_type) {
1592 case IFT_ETHER:
1593 case IFT_ATM:
1594 case IFT_IEEE1394:
1595 case IFT_INFINIBAND:
1596 in6 = ia->ia_addr.sin6_addr;
1597 if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1598 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1599 ifp->if_inet6->nd_flags |= ND6_IFF_IFDISABLED;
1600 log(LOG_ERR, "%s: possible hardware address "
1601 "duplication detected, disable IPv6\n",
1602 if_name(ifp));
1603 }
1604 break;
1605 }
1606 }
1607 }
1608
1609 /*
1610 * Transmit a neighbour solicitation for the purpose of DAD. Returns with the
1611 * DAD queue unlocked.
1612 */
1613 static void
nd6_dad_ns_output(struct dadq * dp)1614 nd6_dad_ns_output(struct dadq *dp)
1615 {
1616 struct in6_ifaddr *ia = (struct in6_ifaddr *)dp->dad_ifa;
1617 struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
1618 int i;
1619
1620 DADQ_WLOCK_ASSERT();
1621
1622 dp->dad_ns_tcount++;
1623 if ((ifp->if_flags & IFF_UP) == 0) {
1624 DADQ_WUNLOCK();
1625 return;
1626 }
1627 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1628 DADQ_WUNLOCK();
1629 return;
1630 }
1631
1632 dp->dad_ns_ocount++;
1633 if (V_dad_enhanced != 0) {
1634 for (i = 0; i < ND_OPT_NONCE_LEN32; i++)
1635 dp->dad_nonce[i] = arc4random();
1636 /*
1637 * XXXHRS: Note that in the case that
1638 * DupAddrDetectTransmits > 1, multiple NS messages with
1639 * different nonces can be looped back in an unexpected
1640 * order. The current implementation recognizes only
1641 * the latest nonce on the sender side. Practically it
1642 * should work well in almost all cases.
1643 */
1644 }
1645 DADQ_WUNLOCK();
1646 nd6_ns_output(ifp, NULL, NULL, &ia->ia_addr.sin6_addr,
1647 (uint8_t *)&dp->dad_nonce[0]);
1648 }
1649
1650 static void
nd6_dad_ns_input(struct ifaddr * ifa,struct nd_opt_nonce * ndopt_nonce)1651 nd6_dad_ns_input(struct ifaddr *ifa, struct nd_opt_nonce *ndopt_nonce)
1652 {
1653 struct dadq *dp;
1654
1655 if (ifa == NULL)
1656 panic("ifa == NULL in nd6_dad_ns_input");
1657
1658 /* Ignore Nonce option when Enhanced DAD is disabled. */
1659 if (V_dad_enhanced == 0)
1660 ndopt_nonce = NULL;
1661 DADQ_RLOCK();
1662 dp = nd6_dad_find(ifa, ndopt_nonce);
1663 if (dp != NULL)
1664 dp->dad_ns_icount++;
1665 DADQ_RUNLOCK();
1666 }
1667
1668 static void
nd6_dad_na_input(struct ifaddr * ifa)1669 nd6_dad_na_input(struct ifaddr *ifa)
1670 {
1671 struct dadq *dp;
1672
1673 if (ifa == NULL)
1674 panic("ifa == NULL in nd6_dad_na_input");
1675
1676 DADQ_RLOCK();
1677 dp = nd6_dad_find(ifa, NULL);
1678 if (dp != NULL)
1679 dp->dad_na_icount++;
1680 DADQ_RUNLOCK();
1681 }
1682
1683 static void
nd6_queue_rel(void * arg)1684 nd6_queue_rel(void *arg)
1685 {
1686 struct nd_queue *ndq = arg;
1687 struct ifaddr *ifa;
1688
1689 ifa = ndq->ndq_ifa;
1690 IF_ADDR_WLOCK_ASSERT(ifa->ifa_ifp);
1691
1692 /* Remove ndq from the nd_queue list and free it */
1693 TAILQ_REMOVE(&ifa->ifa_ifp->if_inet6->nd_queue, ndq, ndq_list);
1694 IF_ADDR_WUNLOCK(ifa->ifa_ifp);
1695
1696 free(ndq, M_IP6NDP);
1697 ifa_free(ifa);
1698 }
1699
1700 static void
nd6_queue_timer(void * arg)1701 nd6_queue_timer(void *arg)
1702 {
1703 struct nd_queue *ndq = arg;
1704 struct ifaddr *ifa = ndq->ndq_ifa;
1705 struct ifnet *ifp;
1706 struct in6_addr daddr, taddr;
1707 struct sockaddr_dl sdl;
1708 struct epoch_tracker et;
1709 int delay, tlladdr;
1710 u_long flags;
1711 bool proxy;
1712
1713 KASSERT(ifa != NULL, ("ND6 queue entry %p with no address", ndq));
1714
1715 ifp = ifa->ifa_ifp;
1716 CURVNET_SET(ifp->if_vnet);
1717 NET_EPOCH_ENTER(et);
1718
1719 daddr = ndq->ndq_daddr;
1720 taddr = ndq->ndq_taddr;
1721 tlladdr = ND6_NA_OPT_LLA;
1722 flags = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
1723 if ((ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) != 0 && V_ip6_norbit_raif)
1724 flags &= ~ND_NA_FLAG_ROUTER;
1725
1726 /*
1727 * RFC 9131 Section 6.1.2: If the address is preferred,
1728 * then the Override flag SHOULD NOT be set.
1729 */
1730 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_NEWGUA) != 0) {
1731 /*
1732 * XXX: If the address is in the Optimistic state,
1733 * then the Override flag MUST NOT be set.
1734 * We don't support RFC 4429 yet.
1735 */
1736 if ((ifp->if_inet6->nd_flags & ND6_IFF_PREFER_SOURCE) == 0)
1737 flags |= ND_NA_FLAG_OVERRIDE;
1738 }
1739 /*
1740 * RFC 4861 Section 7.2.6: if link-layer address changed,
1741 * The Override flag MAY be set to either zero or one.
1742 */
1743 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_LLADDR) != 0)
1744 flags |= ND_NA_FLAG_OVERRIDE;
1745 /* anycast advertisement delay rule (RFC 4861 7.2.7, SHOULD) */
1746 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_ANYCAST) != 0)
1747 flags |= ND_NA_FLAG_SOLICITED;
1748 /* proxy advertisement delay rule (RFC 4861 7.2.8, SHOULD) */
1749 proxy = false;
1750 if ((ndq->ndq_flags & ND6_QUEUE_FLAG_PROXY) != 0) {
1751 flags |= ND_NA_FLAG_SOLICITED;
1752 sdl = ndq->ndq_sdl;
1753 proxy = true;
1754 }
1755
1756 /*
1757 * if it was GRAND, wait at least a RetransTimer
1758 * before removing from queue.
1759 */
1760 if ((ndq->ndq_flags & ND6_QUEUE_GRAND_MASK) != 0) {
1761 delay = ifp->if_inet6->nd_retrans * hz / 1000;
1762 callout_reset(&ndq->ndq_callout, delay, nd6_queue_rel, ndq);
1763 IF_ADDR_WUNLOCK(ifp);
1764 } else
1765 nd6_queue_rel(ndq);
1766
1767 if (__predict_true(in6_setscope(&daddr, ifp, NULL) == 0))
1768 nd6_na_output_fib(ifp, &daddr, &taddr, flags, tlladdr,
1769 proxy ? (struct sockaddr *)&sdl : NULL, ifp->if_fib);
1770
1771 NET_EPOCH_EXIT(et);
1772 CURVNET_RESTORE();
1773 }
1774
1775 /*
1776 * Queue a delayed IPv6 Neighbor Advertisement.
1777 *
1778 * daddr: destination address (who the NA is sent to)
1779 * taddr: target address being advertised (used for proxy NAs)
1780 * sdl: link-layer address (used for proxy NAs)
1781 */
1782 static void
nd6_queue_add(struct ifaddr * ifa,struct in6_addr * daddr,struct in6_addr * taddr,struct sockaddr_dl * sdl,int delay,uint32_t flags)1783 nd6_queue_add(struct ifaddr *ifa, struct in6_addr *daddr,
1784 struct in6_addr *taddr, struct sockaddr_dl *sdl, int delay, uint32_t flags)
1785 {
1786 struct nd_queue *ndq = NULL;
1787 struct ifnet *ifp;
1788 struct in6_ifextra *ext;
1789 char ip6buf[INET6_ADDRSTRLEN];
1790
1791 NET_EPOCH_ASSERT();
1792
1793 ifp = ifa->ifa_ifp;
1794 ext = ifp->if_inet6;
1795 IF_ADDR_WLOCK(ifp);
1796 /*
1797 * if request comes from GRAND, check whether another delayed
1798 * GRAND NA exists in the queue.
1799 * If it exists, cancel previous one and reuse its ndq.
1800 */
1801 if ((flags & ND6_QUEUE_GRAND_MASK) != 0) {
1802 TAILQ_FOREACH(ndq, &ext->nd_queue, ndq_list) {
1803 if (ndq->ndq_ifa == ifa &&
1804 (flags & ND6_QUEUE_GRAND_MASK) != 0)
1805 break;
1806 }
1807 }
1808 if (ndq == NULL) {
1809 ndq = malloc(sizeof(*ndq), M_IP6NDP, M_NOWAIT | M_ZERO);
1810 if (ndq == NULL) {
1811 log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1812 __func__, ip6_sprintf(ip6buf, IFA_IN6(ifa)),
1813 ifp ? if_name(ifp) : "???");
1814 IF_ADDR_WUNLOCK(ifp);
1815 return;
1816 }
1817
1818 callout_init_mtx(&ndq->ndq_callout, &ifp->if_addr_lock,
1819 CALLOUT_TRYLOCK | CALLOUT_RETURNUNLOCKED);
1820 ifa_ref(ifa);
1821 ndq->ndq_ifa = ifa;
1822 TAILQ_INSERT_TAIL(&ext->nd_queue, ndq, ndq_list);
1823 }
1824
1825 memcpy(&ndq->ndq_daddr, daddr, sizeof(struct in6_addr));
1826 /*
1827 * For proxy NAs, the target address (taddr) being advertised differs from
1828 * the interface address (ifa), so we must explicitly store both the proxy
1829 * target address and its link-layer address (sdl).
1830 * For non-proxy NAs, use the interface address (ifa) itself as the target.
1831 */
1832 if ((flags & ND6_QUEUE_FLAG_PROXY) != 0) {
1833 memcpy(&ndq->ndq_taddr, taddr, sizeof(struct in6_addr));
1834 memcpy(&ndq->ndq_sdl, sdl, sizeof(struct sockaddr_dl));
1835 } else
1836 memcpy(&ndq->ndq_taddr, IFA_IN6(ifa), sizeof(struct in6_addr));
1837 ndq->ndq_flags = flags;
1838
1839 nd6log((LOG_DEBUG, "%s: delay IPv6 NA for %s\n", if_name(ifp),
1840 ip6_sprintf(ip6buf, IFA_IN6(ifa))));
1841 callout_reset(&ndq->ndq_callout, delay, nd6_queue_timer, ndq);
1842 IF_ADDR_WUNLOCK(ifp);
1843 }
1844
1845 /*
1846 * Start Gratuitous Neighbor Discovery (GRAND) for specified address.
1847 * Called after DAD completes and by interface link layer change event.
1848 */
1849 void
nd6_grand_start(struct ifaddr * ifa,uint32_t flags)1850 nd6_grand_start(struct ifaddr *ifa, uint32_t flags)
1851 {
1852 struct nd_queue *ndq;
1853 struct in6_ifextra *ext = ifa->ifa_ifp->if_inet6;
1854 struct in6_addr daddr = IN6ADDR_ANY_INIT;
1855 int delay, count = 0;
1856
1857 NET_EPOCH_ASSERT();
1858 /* If we don't need GRAND, don't do it. */
1859 if (V_ip6_grand_count == 0 ||
1860 ifa->ifa_carp != NULL)
1861 return;
1862
1863 /* Check if new address is global */
1864 if ((flags & ND6_QUEUE_FLAG_NEWGUA) != 0 &&
1865 in6_addrscope(IFA_IN6(ifa)) != IPV6_ADDR_SCOPE_GLOBAL)
1866 return;
1867
1868 /*
1869 * RFC 9131 Section 6.1.2: These advertisements MUST be
1870 * separated by at least RetransTimer seconds.
1871 */
1872 TAILQ_FOREACH(ndq, &ext->nd_queue, ndq_list) {
1873 /*
1874 * RFC 9131 Section 6.1.2: a node SHOULD send
1875 * up to MAX_NEIGHBOR_ADVERTISEMENT Neighbor Advertisement messages.
1876 * Make sure we don't queue GRAND more than V_ip6_grand_count
1877 * per interface.
1878 * Since this limitation only applies to GRAND, don't
1879 * count non-GRAND ndq.
1880 */
1881 if ((ndq->ndq_flags & ND6_QUEUE_GRAND_MASK) == 0)
1882 continue;
1883
1884 count++;
1885 if (count >= V_ip6_grand_count)
1886 return;
1887 }
1888
1889 /*
1890 * RFC 9131 Section 6.1.2: if new global address added,
1891 * use the all-routers multicast address.
1892 */
1893 if ((flags & ND6_QUEUE_FLAG_NEWGUA) != 0)
1894 daddr = in6addr_linklocal_allrouters;
1895
1896 /*
1897 * RFC 4861 Section 7.2.6: if link-layer address changed,
1898 * use the all-nodes multicast address.
1899 */
1900 if ((flags & ND6_QUEUE_FLAG_LLADDR) != 0)
1901 daddr = in6addr_linklocal_allnodes;
1902
1903 delay = ext->nd_retrans * hz / 1000;
1904 nd6_queue_add(ifa, &daddr, NULL, NULL, count * delay, flags);
1905 }
1906
1907 /*
1908 * drain nd6 queue. used for address removals.
1909 */
1910 void
nd6_queue_stop(struct ifaddr * ifa)1911 nd6_queue_stop(struct ifaddr *ifa)
1912 {
1913 struct nd_queue *ndq, *dndq;
1914 struct ifnet *ifp;
1915
1916 ifp = ifa->ifa_ifp;
1917 IF_ADDR_WLOCK(ifp);
1918 TAILQ_FOREACH_SAFE(ndq, &ifp->if_inet6->nd_queue, ndq_list, dndq) {
1919 if (ndq->ndq_ifa != ifa)
1920 continue;
1921
1922 callout_stop(&ndq->ndq_callout);
1923
1924 /* Remove ndq from the nd_queue list and free it */
1925 TAILQ_REMOVE(&ifa->ifa_ifp->if_inet6->nd_queue, ndq, ndq_list);
1926 free(ndq, M_IP6NDP);
1927 ifa_free(ifa);
1928 }
1929 IF_ADDR_WUNLOCK(ifp);
1930 }
1931