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