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