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