]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet6/nd6_nbr.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6_nbr.c
index a21dfb189e30b7e773fa23581838c6efd3c34179..5e968a96e1707c66d4c668bb06ef372420fbd8cc 100644 (file)
@@ -1,4 +1,5 @@
-/*     $KAME: nd6_nbr.c,v 1.32 2000/03/21 11:37:30 itojun Exp $        */
+/*     $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.4 2001/07/06 05:32:25 sumikawa Exp $       */
+/*     $KAME: nd6_nbr.c,v 1.64 2001/05/17 03:48:30 itojun Exp $        */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  * SUCH DAMAGE.
  */
 
-#if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
-#include "opt_inet.h"
-#if __NetBSD__ /*XXX*/
-#include "opt_ipsec.h"
-#endif
-#endif
-
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/malloc.h>
@@ -45,9 +39,6 @@
 #include <sys/time.h>
 #include <sys/kernel.h>
 #include <sys/errno.h>
-#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
-#include <sys/ioctl.h>
-#endif
 #include <sys/syslog.h>
 #include <kern/queue.h>
 
 #include <netinet6/nd6.h>
 #include <netinet/icmp6.h>
 
-#ifdef __OpenBSD__     /*don't confuse KAME ipsec with OpenBSD ipsec*/
-#undef IPSEC
-#endif
-
 #if IPSEC
 #include <netinet6/ipsec.h>
+#if INET6
+#include <netinet6/ipsec6.h>
+#endif
+extern int ipsec_bypass;
 #endif
 
 #include <net/net_osdep.h>
 #define SDL(s) ((struct sockaddr_dl *)s)
 
 struct dadq;
-static struct dadq *nd6_dad_find __P((struct ifaddr *));
-static void nd6_dad_timer __P((struct ifaddr *));
-static void nd6_dad_timer_funnel __P((struct ifaddr *));
-static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
-static void nd6_dad_ns_input __P((struct ifaddr *));
-static void nd6_dad_na_input __P((struct ifaddr *));
+static struct dadq *nd6_dad_find(struct ifaddr *);
+#ifndef __APPLE__
+static void nd6_dad_starttimer(struct dadq *, int);
+static void nd6_dad_stoptimer(struct dadq *);
+#else
+void nd6_dad_stoptimer(struct ifaddr *);
+#endif
+static void nd6_dad_timer(struct ifaddr *);
+static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
+static void nd6_dad_ns_input(struct ifaddr *);
+static void nd6_dad_na_input(struct ifaddr *);
 
 static int dad_ignore_ns = 0;  /* ignore NS in DAD - specwise incorrect*/
 static int dad_maxtry = 15;    /* max # of *tries* to transmit DAD packet */
 
+extern lck_mtx_t *dad6_mutex;
+extern lck_mtx_t *nd6_mutex;
 /*
  * Input an Neighbor Solicitation Message.
  *
@@ -94,9 +92,10 @@ static int dad_maxtry = 15;  /* max # of *tries* to transmit DAD packet */
  * Based on RFC 2462 (duplicated address detection)
  */
 void
-nd6_ns_input(m, off, icmp6len)
-       struct mbuf *m;
-       int off, icmp6len;
+nd6_ns_input(
+       struct mbuf *m,
+       int off,
+       int icmp6len)
 {
        struct ifnet *ifp = m->m_pkthdr.rcvif;
        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
@@ -113,41 +112,44 @@ nd6_ns_input(m, off, icmp6len)
        union nd_opts ndopts;
        struct sockaddr_dl *proxydl = NULL;
 
+#ifndef PULLDOWN_TEST
+       IP6_EXTHDR_CHECK(m, off, icmp6len, return);
+       nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
+#else
+       IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
+       if (nd_ns == NULL) {
+               icmp6stat.icp6s_tooshort++;
+               return;
+       }
+#endif
+       ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
+       taddr6 = nd_ns->nd_ns_target;
+
        if (ip6->ip6_hlim != 255) {
-               log(LOG_ERR,
-                   "nd6_ns_input: invalid hlim %d\n", ip6->ip6_hlim);
-               goto freeit;
+               nd6log((LOG_ERR,
+                   "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
+                   ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
+                   ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
+               goto bad;
        }
 
        if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
                /* dst has to be solicited node multicast address. */
                if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
-                   /*don't check ifindex portion*/
+                   /* don't check ifindex portion */
                    && daddr6.s6_addr32[1] == 0
                    && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
                    && daddr6.s6_addr8[12] == 0xff) {
-                       ; /*good*/
+                       ; /* good */
                } else {
-                       log(LOG_INFO, "nd6_ns_input: bad DAD packet "
-                               "(wrong ip6 dst)\n");
+                       nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
+                               "(wrong ip6 dst)\n"));
                        goto bad;
                }
        }
 
-#ifndef PULLDOWN_TEST
-       IP6_EXTHDR_CHECK(m, off, icmp6len,);
-       nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
-#else
-       IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
-       if (nd_ns == NULL) {
-               icmp6stat.icp6s_tooshort++;
-               return;
-       }
-#endif
-       taddr6 = nd_ns->nd_ns_target;
-
        if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
-               log(LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n");
+               nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
                goto bad;
        }
 
@@ -157,18 +159,20 @@ nd6_ns_input(m, off, icmp6len)
        icmp6len -= sizeof(*nd_ns);
        nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
        if (nd6_options(&ndopts) < 0) {
-               log(LOG_INFO, "nd6_ns_input: invalid ND option, ignored\n");
-               goto bad;
+               nd6log((LOG_INFO,
+                   "nd6_ns_input: invalid ND option, ignored\n"));
+               /* nd6_options have incremented stats */
+               goto freeit;
        }
 
        if (ndopts.nd_opts_src_lladdr) {
-               lladdr = (char *)(ndopts.nd_opts_src_lladdr +1);
+               lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
                lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
        }
        
        if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
-               log(LOG_INFO, "nd6_ns_input: bad DAD packet "
-                       "(link-layer address option)\n");
+               nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
+                   "(link-layer address option)\n"));
                goto bad;
        }
 
@@ -212,11 +216,7 @@ nd6_ns_input(m, off, icmp6len)
                tsin6.sin6_family = AF_INET6;
                tsin6.sin6_addr = taddr6;
 
-               rt = rtalloc1((struct sockaddr *)&tsin6, 0
-#if __FreeBSD__ || defined (__APPLE__)
-                             , 0
-#endif /* __FreeBSD__ */
-                             );
+               rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
                if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
                    rt->rt_gateway->sa_family == AF_LINK) {
                        /*
@@ -234,7 +234,7 @@ nd6_ns_input(m, off, icmp6len)
        }
        if (!ifa) {
                /*
-                * We've got a NS packet, and we don't have that adddress
+                * We've got an NS packet, and we don't have that adddress
                 * assigned for us.  We MUST silently ignore it.
                 * See RFC2461 7.2.3.
                 */
@@ -247,16 +247,17 @@ nd6_ns_input(m, off, icmp6len)
                goto freeit;
 
        if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
-               log(LOG_INFO,
+               nd6log((LOG_INFO,
                    "nd6_ns_input: lladdrlen mismatch for %s "
                    "(if %d, NS packet %d)\n",
-                       ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
+                       ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
+               goto bad;
        }
 
        if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
-               log(LOG_INFO,
-                   "nd6_ns_input: duplicate IP6 address %s\n",
-                   ip6_sprintf(&saddr6));
+               nd6log((LOG_INFO,
+                       "nd6_ns_input: duplicate IP6 address %s\n",
+                       ip6_sprintf(&saddr6)));
                goto freeit;
        }
 
@@ -317,9 +318,10 @@ nd6_ns_input(m, off, icmp6len)
        return;
 
  bad:
-       log(LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
-       log(LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
-       log(LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
+       nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
+       nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
+       nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
+       icmp6stat.icp6s_badns++;
        m_freem(m);
 }
 
@@ -333,11 +335,13 @@ nd6_ns_input(m, off, icmp6len)
  * Based on RFC 2462 (duplicated address detection)
  */
 void
-nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
-       struct ifnet *ifp;
-       struct in6_addr *daddr6, *taddr6;
-       struct llinfo_nd6 *ln;  /* for source address determination */
-       int dad;        /* duplicated address detection */
+nd6_ns_output(
+       struct ifnet *ifp,
+       const struct in6_addr *daddr6,
+       const struct in6_addr *taddr6,
+       struct llinfo_nd6 *ln,  /* for source address determination */
+       int dad,        /* duplicated address detection */
+       int locked)
 {
        struct mbuf *m;
        struct ip6_hdr *ip6;
@@ -356,7 +360,7 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
        maxlen = sizeof(*ip6) + sizeof(*nd_ns);
        maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
        if (max_linkhdr + maxlen >= MCLBYTES) {
-#ifdef DIAGNOSTIC
+#if DIAGNOSTIC
                printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
                    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
 #endif
@@ -373,6 +377,7 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
        }
        if (m == NULL)
                return;
+       m->m_pkthdr.rcvif = NULL;
 
        if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
                m->m_flags |= M_MCAST;
@@ -383,7 +388,7 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
 
        icmp6len = sizeof(*nd_ns);
        m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
-       m->m_data += max_linkhdr;       /*or MH_ALIGN() equivalent?*/
+       m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
 
        /* fill neighbor solicitation packet */
        ip6 = mtod(m, struct ip6_hdr *);
@@ -433,7 +438,7 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
                 * - saddr6 belongs to the outgoing interface.
                 * Otherwise, we perform a scope-wise match.
                 */
-               struct ip6_hdr *hip6;           /*hold ip6*/
+               struct ip6_hdr *hip6;           /* hold ip6 */
                struct in6_addr *saddr6;
 
                if (ln && ln->ln_hold) {
@@ -450,7 +455,10 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
                else {
                        ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
                        if (ia == NULL) {
-                               m_freem(m);     /*XXX*/
+                               if (ln && ln->ln_hold)
+                                       m_freem(ln->ln_hold);
+                               ln->ln_hold = NULL;
+                               m_freem(m);
                                return;
                        }
                        ip6->ip6_src = ia->ia_addr.sin6_addr;
@@ -506,9 +514,10 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
 
 #if IPSEC
        /* Don't lookup socket */
-       ipsec_setsocket(m, NULL);
+       if (ipsec_bypass == 0)
+               (void)ipsec_setsocket(m, NULL);
 #endif
-       ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
+       ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif, locked);
        if (outif) {
                icmp6_ifstat_inc(outif, ifs6_out_msg);
                icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
@@ -527,9 +536,10 @@ nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  */
 void
-nd6_na_input(m, off, icmp6len)
-       struct mbuf *m;
-       int off, icmp6len;
+nd6_na_input(
+       struct mbuf *m,
+       int off, 
+       int icmp6len)
 {
        struct ifnet *ifp = m->m_pkthdr.rcvif;
        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
@@ -550,15 +560,18 @@ nd6_na_input(m, off, icmp6len)
        struct rtentry *rt;
        struct sockaddr_dl *sdl;
        union nd_opts ndopts;
+       struct timeval timenow;
 
        if (ip6->ip6_hlim != 255) {
-               log(LOG_ERR,
-                   "nd6_na_input: invalid hlim %d\n", ip6->ip6_hlim);
-               goto freeit;
+               nd6log((LOG_ERR,
+                   "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
+                   ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
+                   ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
+               goto bad;
        }
 
 #ifndef PULLDOWN_TEST
-       IP6_EXTHDR_CHECK(m, off, icmp6len,);
+       IP6_EXTHDR_CHECK(m, off, icmp6len, return);
        nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
 #else
        IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
@@ -577,22 +590,24 @@ nd6_na_input(m, off, icmp6len)
                taddr6.s6_addr16[1] = htons(ifp->if_index);
 
        if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
-               log(LOG_ERR,
+               nd6log((LOG_ERR,
                    "nd6_na_input: invalid target address %s\n",
-                   ip6_sprintf(&taddr6));
-               goto freeit;
+                   ip6_sprintf(&taddr6)));
+               goto bad;
        }
        if (IN6_IS_ADDR_MULTICAST(&daddr6))
                if (is_solicited) {
-                       log(LOG_ERR,
-                           "nd6_na_input: a solicited adv is multicasted\n");
-                       goto freeit;
+                       nd6log((LOG_ERR,
+                           "nd6_na_input: a solicited adv is multicasted\n"));
+                       goto bad;
                }
 
        icmp6len -= sizeof(*nd_na);
        nd6_option_init(nd_na + 1, icmp6len, &ndopts);
        if (nd6_options(&ndopts) < 0) {
-               log(LOG_INFO, "nd6_na_input: invalid ND option, ignored\n");
+               nd6log((LOG_INFO,
+                   "nd6_na_input: invalid ND option, ignored\n"));
+               /* nd6_options have incremented stats */
                goto freeit;
        }
 
@@ -618,7 +633,7 @@ nd6_na_input(m, off, icmp6len)
                goto freeit;
        }
 
-       /* Just for safety, maybe unnecessery. */
+       /* Just for safety, maybe unnecessary. */
        if (ifa) {
                log(LOG_ERR,
                    "nd6_na_input: duplicate IP6 address %s\n",
@@ -627,21 +642,23 @@ nd6_na_input(m, off, icmp6len)
        }
 
        if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
-               log(LOG_INFO,
+               nd6log((LOG_INFO,
                    "nd6_na_input: lladdrlen mismatch for %s "
                    "(if %d, NA packet %d)\n",
-                       ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
+                       ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
+               goto bad;
        }
 
        /*
         * If no neighbor cache entry is found, NA SHOULD silently be discarded.
         */
-       rt = nd6_lookup(&taddr6, 0, ifp);
+       rt = nd6_lookup(&taddr6, 0, ifp, 0);
        if ((rt == NULL) ||
           ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
           ((sdl = SDL(rt->rt_gateway)) == NULL))
                goto freeit;
 
+       getmicrotime(&timenow);
        if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
                /*
                 * If the link-layer has address, and no lladdr option came,
@@ -657,16 +674,22 @@ nd6_na_input(m, off, icmp6len)
                bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
                if (is_solicited) {
                        ln->ln_state = ND6_LLINFO_REACHABLE;
+                       ln->ln_byhint = 0;
                        if (ln->ln_expire)
-#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
-                               ln->ln_expire = time.tv_sec +
-#else
-                               ln->ln_expire = time_second +
-#endif
-                                       nd_ifinfo[rt->rt_ifp->if_index].reachable;
-               } else
+                               ln->ln_expire = timenow.tv_sec +
+                                   nd_ifinfo[rt->rt_ifp->if_index].reachable;
+               } else {
                        ln->ln_state = ND6_LLINFO_STALE;
-               ln->ln_router = is_router;
+                       ln->ln_expire = timenow.tv_sec + nd6_gctimer;
+               }
+               if ((ln->ln_router = is_router) != 0) {
+                       /*
+                        * This means a router's state has changed from
+                        * non-reachable to probably reachable, and might
+                        * affect the status of associated prefixes..
+                        */
+                       pfxlist_onlink_check(0);
+               }
        } else {
                int llchange;
 
@@ -709,8 +732,10 @@ nd6_na_input(m, off, icmp6len)
                         * If state is REACHABLE, make it STALE.
                         * no other updates should be done.
                         */
-                       if (ln->ln_state == ND6_LLINFO_REACHABLE)
+                       if (ln->ln_state == ND6_LLINFO_REACHABLE) {
                                ln->ln_state = ND6_LLINFO_STALE;
+                               ln->ln_expire = timenow.tv_sec + nd6_gctimer;
+                       }
                        goto freeit;
                } else if (is_override                             /* (2a) */
                        || (!is_override && (lladdr && !llchange)) /* (2b) */
@@ -730,17 +755,16 @@ nd6_na_input(m, off, icmp6len)
                         */
                        if (is_solicited) {
                                ln->ln_state = ND6_LLINFO_REACHABLE;
+                               ln->ln_byhint = 0;
                                if (ln->ln_expire) {
-#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
-                                       ln->ln_expire = time.tv_sec +
-#else
-                                       ln->ln_expire = time_second +
-#endif
-                                               nd_ifinfo[ifp->if_index].reachable;
+                                       ln->ln_expire = timenow.tv_sec +
+                                           nd_ifinfo[ifp->if_index].reachable;
                                }
                        } else {
-                               if (lladdr && llchange)
+                               if (lladdr && llchange) {
                                        ln->ln_state = ND6_LLINFO_STALE;
+                                       ln->ln_expire = timenow.tv_sec + nd6_gctimer;
+                               }
                        }
                }
 
@@ -755,42 +779,54 @@ nd6_na_input(m, off, icmp6len)
                        int s;
 
                        in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
-#if __NetBSD__
-                       s = splsoftnet();
-#else
-                       s = splnet();
-#endif
+
+                       /*
+                        * Lock to protect the default router list.
+                        * XXX: this might be unnecessary, since this function
+                        * is only called under the network software interrupt
+                        * context.  However, we keep it just for safety.  
+                        */
+                       lck_mtx_lock(nd6_mutex);
                        dr = defrouter_lookup(in6, rt->rt_ifp);
-                       if (dr)
-                               defrtrlist_del(dr);
-                       else if (!ip6_forwarding && ip6_accept_rtadv) {
-                               /*
-                                * Even if the neighbor is not in the default
-                                * router list, the neighbor may be used
-                                * as a next hop for some destinations
-                                * (e.g. redirect case). So we must
-                                * call rt6_flush explicitly.
-                                */
-                               rt6_flush(&ip6->ip6_src, rt->rt_ifp);
+                       if (dr) {
+                               defrtrlist_del(dr, 1);
+                               lck_mtx_unlock(nd6_mutex);
+                       }
+                       else {
+                               lck_mtx_unlock(nd6_mutex);
+                               if (!ip6_forwarding && (ip6_accept_rtadv || (rt->rt_ifp->if_eflags & IFEF_ACCEPT_RTADVD))) {
+                                       /*
+                                        * Even if the neighbor is not in the default
+                                        * router list, the neighbor may be used
+                                        * as a next hop for some destinations
+                                        * (e.g. redirect case). So we must
+                                        * call rt6_flush explicitly.
+                                        */
+                                       rt6_flush(&ip6->ip6_src, rt->rt_ifp);
+                               }
                        }
-                       splx(s);
                }
                ln->ln_router = is_router;
        }
        rt->rt_flags &= ~RTF_REJECT;
        ln->ln_asked = 0;
        if (ln->ln_hold) {
-#if OLDIP6OUTPUT
-               (*ifp->if_output)(ifp, ln->ln_hold, rt_key(rt), rt);
-#else
-               nd6_output(ifp, ln->ln_hold,
-                          (struct sockaddr_in6 *)rt_key(rt), rt);
-#endif
+               /*
+                * we assume ifp is not a loopback here, so just set the 2nd
+                * argument as the 1st one.
+                */
+               nd6_output(ifp, ifp, ln->ln_hold,
+                          (struct sockaddr_in6 *)rt_key(rt), rt, 0);
                ln->ln_hold = 0;
        }
 
  freeit:
        m_freem(m);
+       return;
+
+ bad:
+       icmp6stat.icp6s_badna++;
+       m_freem(m);
 }
 
 /*
@@ -803,12 +839,13 @@ nd6_na_input(m, off, icmp6len)
  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  */
 void
-nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
-       struct ifnet *ifp;
-       struct in6_addr *daddr6, *taddr6;
-       u_long flags;
-       int tlladdr;            /* 1 if include target link-layer address */
-       struct sockaddr *sdl0;  /* sockaddr_dl (= proxy NA) or NULL */
+nd6_na_output(
+       struct ifnet *ifp,
+       const struct in6_addr *daddr6,
+       const struct in6_addr *taddr6,
+       u_long flags,
+       int tlladdr,            /* 1 if include target link-layer address */
+       struct sockaddr *sdl0)  /* sockaddr_dl (= proxy NA) or NULL */
 {
        struct mbuf *m;
        struct ip6_hdr *ip6;
@@ -817,14 +854,14 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
        struct ip6_moptions im6o;
        int icmp6len;
        int maxlen;
-       caddr_t mac;
+       caddr_t mac = NULL;
        struct ifnet *outif = NULL;
 
        /* estimate the size of message */
        maxlen = sizeof(*ip6) + sizeof(*nd_na);
        maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
        if (max_linkhdr + maxlen >= MCLBYTES) {
-#ifdef DIAGNOSTIC
+#if DIAGNOSTIC
                printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
                    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
 #endif
@@ -841,6 +878,7 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
        }
        if (m == NULL)
                return;
+       m->m_pkthdr.rcvif = NULL;
 
        if (IN6_IS_ADDR_MULTICAST(daddr6)) {
                m->m_flags |= M_MCAST;
@@ -851,7 +889,7 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
 
        icmp6len = sizeof(*nd_na);
        m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
-       m->m_data += max_linkhdr;       /*or MH_ALIGN() equivalent?*/
+       m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
 
        /* fill neighbor advertisement packet */
        ip6 = mtod(m, struct ip6_hdr *);
@@ -894,7 +932,6 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
         * target lladdr option SHOULD NOT be included.
         */
        if (tlladdr) {
-               mac = NULL;
                /*
                 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
                 * lladdr in sdl0.  If we are not proxying (sending NA for
@@ -934,9 +971,10 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
 
 #if IPSEC
        /* Don't lookup socket */
-       ipsec_setsocket(m, NULL);
+       if (ipsec_bypass == 0)
+               (void)ipsec_setsocket(m, NULL);
 #endif
-       ip6_output(m, NULL, NULL, 0, &im6o, &outif);
+       ip6_output(m, NULL, NULL, 0, &im6o, &outif, 0);
        if (outif) {
                icmp6_ifstat_inc(outif, ifs6_out_msg);
                icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
@@ -945,22 +983,10 @@ nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
 }
 
 caddr_t
-nd6_ifptomac(ifp)
-       struct ifnet *ifp;
+nd6_ifptomac(
+       struct ifnet *ifp)
 {
-       switch (ifp->if_type) {
-       case IFT_ARCNET:
-       case IFT_ETHER:
-       case IFT_FDDI:
-#if __NetBSD__
-               return LLADDR(ifp->if_sadl);
-#else
-               return ((caddr_t)(ifp + 1));
-#endif
-               break;
-       default:
-               return NULL;
-       }
+       return ((caddr_t)ifnet_lladdr(ifp));
 }
 
 TAILQ_HEAD(dadq_head, dadq);
@@ -972,37 +998,65 @@ struct dadq {
        int dad_ns_ocount;      /* NS sent so far */
        int dad_ns_icount;
        int dad_na_icount;
-#if defined(__FreeBSD__) && __FreeBSD__ >= 3
-       struct callout_handle dad_timer;
-#endif
 };
 
 static struct dadq_head dadq;
+static int dad_init = 0;
 
 static struct dadq *
-nd6_dad_find(ifa)
-       struct ifaddr *ifa;
+nd6_dad_find(
+       struct ifaddr *ifa)
 {
        struct dadq *dp;
-
+       lck_mtx_lock(dad6_mutex);
        for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
-               if (dp->dad_ifa == ifa)
+               if (dp->dad_ifa == ifa) {
+                       lck_mtx_unlock(dad6_mutex);
                        return dp;
+               }
        }
+       lck_mtx_unlock(dad6_mutex);
        return NULL;
 }
 
+#ifdef  __APPLE__
+void
+nd6_dad_stoptimer(
+       struct ifaddr *ifa)
+{
+
+       untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
+}
+#else
+static void
+nd6_dad_starttimer(
+       struct dadq *dp,
+       int ticks)
+{
+
+       callout_reset(&dp->dad_timer_ch, ticks,
+           (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
+}
+
+static void
+nd6_dad_stoptimer(
+       struct dadq *dp)
+{
+
+       callout_stop(&dp->dad_timer_ch);
+}
+#endif
+
 /*
  * Start Duplicated Address Detection (DAD) for specified interface address.
  */
 void
-nd6_dad_start(ifa, tick)
-       struct ifaddr *ifa;
-       int *tick;      /* minimum delay ticks for IFF_UP event */
+nd6_dad_start(
+       struct ifaddr *ifa,
+       int *tick)      /* minimum delay ticks for IFF_UP event */
 {
        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
        struct dadq *dp;
-       static int dad_init = 0;
 
        if (!dad_init) {
                TAILQ_INIT(&dadq);
@@ -1049,12 +1103,12 @@ nd6_dad_start(ifa, tick)
                return;
        }
        bzero(dp, sizeof(*dp));
+       lck_mtx_lock(dad6_mutex);
        TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
+       lck_mtx_unlock(dad6_mutex);
 
-#if ND6_DEBUG
-       log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
-           ip6_sprintf(&ia->ia_addr.sin6_addr));
-#endif
+       nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
+           ip6_sprintf(&ia->ia_addr.sin6_addr)));
 
        /*
         * Send NS packet for DAD, ip6_dad_count times.
@@ -1063,16 +1117,13 @@ nd6_dad_start(ifa, tick)
         * (re)initialization.
         */
        dp->dad_ifa = ifa;
-       ifa->ifa_refcnt++;      /*just for safety*/
+       ifaref(ifa);    /*just for safety*/
        dp->dad_count = ip6_dad_count;
        dp->dad_ns_icount = dp->dad_na_icount = 0;
        dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
-       if (!tick) {
+       if (tick == NULL) {
                nd6_dad_ns_output(dp, ifa);
-#if defined(__FreeBSD__) && __FreeBSD__ >= 3
-               dp->dad_timer =
-#endif
-               timeout((void (*) __P((void *)))nd6_dad_timer_funnel, (void *)ifa,
+               timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
                        nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
        } else {
                int ntick;
@@ -1082,43 +1133,48 @@ nd6_dad_start(ifa, tick)
                else
                        ntick = *tick + random() % (hz / 2);
                *tick = ntick;
-#if defined(__FreeBSD__) && __FreeBSD__ >= 3
-               dp->dad_timer =
-#endif
-               timeout((void (*) __P((void *)))nd6_dad_timer_funnel, (void *)ifa,
+               timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
                        ntick);
        }
 }
 
-static void     
-nd6_dad_timer_funnel(ifa)
-       struct ifaddr *ifa;
+/*
+ * terminate DAD unconditionally.  used for address removals.
+ */
+void
+nd6_dad_stop(
+       struct ifaddr *ifa)
 {
-                
-#ifdef __APPLE__
-        boolean_t       funnel_state;
-        funnel_state = thread_funnel_set(network_flock, TRUE);
-#endif 
-       nd6_dad_timer(ifa);
-#ifdef __APPLE__
-        (void) thread_funnel_set(network_flock, FALSE);
-#endif
+       struct dadq *dp;
+
+       if (!dad_init)
+               return;
+       dp = nd6_dad_find(ifa);
+       if (!dp) {
+               /* DAD wasn't started yet */
+               return;
+       }
+
+       untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
+
+       lck_mtx_lock(dad6_mutex);
+       TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
+       lck_mtx_unlock(dad6_mutex);
+       FREE(dp, M_IP6NDP);
+       dp = NULL;
+       ifafree(ifa);
+}
 
-} 
 
 static void
-nd6_dad_timer(ifa)
-       struct ifaddr *ifa;
+nd6_dad_timer(
+       struct ifaddr *ifa)
 {
        int s;
        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
        struct dadq *dp;
 
-#if __NetBSD__
-       s = splsoftnet();       /*XXX*/
-#else
-       s = splnet();           /*XXX*/
-#endif
+       s = splnet();           /* XXX */
 
        /* Sanity check */
        if (ia == NULL) {
@@ -1147,13 +1203,15 @@ nd6_dad_timer(ifa)
 
        /* timeouted with IFF_{RUNNING,UP} check */
        if (dp->dad_ns_tcount > dad_maxtry) {
-               log(LOG_ERR, "%s: could not run DAD, driver problem?\n",
-                   if_name(ifa->ifa_ifp));
+               nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
+                       if_name(ifa->ifa_ifp)));
 
+               lck_mtx_lock(dad6_mutex);
                TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
-               _FREE(dp, M_IP6NDP);
+               lck_mtx_unlock(dad6_mutex);
+               FREE(dp, M_IP6NDP);
                dp = NULL;
-               IFAFREE(ifa);
+               ifafree(ifa);
                goto done;
        }
 
@@ -1163,10 +1221,7 @@ nd6_dad_timer(ifa)
                 * We have more NS to go.  Send NS packet for DAD.
                 */
                nd6_dad_ns_output(dp, ifa);
-#if defined(__FreeBSD__) && __FreeBSD__ >= 3
-               dp->dad_timer =
-#endif
-               timeout((void (*) __P((void *)))nd6_dad_timer_funnel, (void *)ifa,
+               timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
                        nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
        } else {
                /*
@@ -1186,7 +1241,7 @@ nd6_dad_timer(ifa)
                }
 
                if (dp->dad_ns_icount) {
-#if 0 /*heuristics*/
+#if 0 /* heuristics */
                        /*
                         * if
                         * - we have sent many(?) DAD NS, and
@@ -1226,17 +1281,18 @@ nd6_dad_timer(ifa)
                         */
                        ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
 
-#if ND6_DEBUG
-                       log(LOG_INFO,
+                       nd6log((LOG_DEBUG,
                            "%s: DAD complete for %s - no duplicates found\n",
                            if_name(ifa->ifa_ifp),
-                           ip6_sprintf(&ia->ia_addr.sin6_addr));
-#endif
+                           ip6_sprintf(&ia->ia_addr.sin6_addr)));
 
+                       lck_mtx_lock(dad6_mutex);
                        TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
-                       _FREE(dp, M_IP6NDP);
+                       lck_mtx_unlock(dad6_mutex);
+                       in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia);
+                       FREE(dp, M_IP6NDP);
                        dp = NULL;
-                       IFAFREE(ifa);
+                       ifafree(ifa);
                }
        }
 
@@ -1245,8 +1301,8 @@ done:
 }
 
 void
-nd6_dad_duplicated(ifa)
-       struct ifaddr *ifa;
+nd6_dad_duplicated(
+       struct ifaddr *ifa)
 {
        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
        struct dadq *dp;
@@ -1257,36 +1313,35 @@ nd6_dad_duplicated(ifa)
                return;
        }
 
-       log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: %d NS, "
-           "%d NA\n", if_name(ifa->ifa_ifp),
-           ip6_sprintf(&ia->ia_addr.sin6_addr),
-           dp->dad_ns_icount, dp->dad_na_icount);
+       log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
+           "NS in/out=%d/%d, NA in=%d\n",
+           if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
+           dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
 
        ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
        ia->ia6_flags |= IN6_IFF_DUPLICATED;
 
        /* We are done with DAD, with duplicated address found. (failure) */
-       untimeout((void (*) __P((void *)))nd6_dad_timer_funnel, (void *)ifa
-#if defined(__FreeBSD__) && __FreeBSD__ >= 3
-               , dp->dad_timer
-#endif
-               );
+       untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
+
 
        log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
            if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
        log(LOG_ERR, "%s: manual intervention required\n",
            if_name(ifa->ifa_ifp));
 
+       lck_mtx_lock(dad6_mutex);
        TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
-       _FREE(dp, M_IP6NDP);
+       lck_mtx_unlock(dad6_mutex);
+       FREE(dp, M_IP6NDP);
        dp = NULL;
-       IFAFREE(ifa);
+       ifafree(ifa);
 }
 
 static void
-nd6_dad_ns_output(dp, ifa)
-       struct dadq *dp;
-       struct ifaddr *ifa;
+nd6_dad_ns_output(
+       struct dadq *dp,
+       struct ifaddr *ifa)
 {
        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
        struct ifnet *ifp = ifa->ifa_ifp;
@@ -1306,16 +1361,15 @@ nd6_dad_ns_output(dp, ifa)
        }
 
        dp->dad_ns_ocount++;
-       nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
+       nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1, 0);
 }
 
 static void
-nd6_dad_ns_input(ifa)
-       struct ifaddr *ifa;
+nd6_dad_ns_input(
+       struct ifaddr *ifa)
 {
        struct in6_ifaddr *ia;
-       struct ifnet *ifp;
-       struct in6_addr *taddr6;
+       const struct in6_addr *taddr6;
        struct dadq *dp;
        int duplicate;
 
@@ -1323,22 +1377,16 @@ nd6_dad_ns_input(ifa)
                panic("ifa == NULL in nd6_dad_ns_input");
 
        ia = (struct in6_ifaddr *)ifa;
-       ifp = ifa->ifa_ifp;
        taddr6 = &ia->ia_addr.sin6_addr;
        duplicate = 0;
        dp = nd6_dad_find(ifa);
 
-       /*
-        * If it is from myself, ignore this.
-        */
-       if (ifp && (ifp->if_flags & IFF_LOOPBACK))
-               return;
-
        /* Quickhack - completely ignore DAD NS packets */
        if (dad_ignore_ns) {
-               log(LOG_INFO, "nd6_dad_ns_input: ignoring DAD NS packet for "
+               nd6log((LOG_INFO,
+                   "nd6_dad_ns_input: ignoring DAD NS packet for "
                    "address %s(%s)\n", ip6_sprintf(taddr6),
-                   if_name(ifa->ifa_ifp));
+                   if_name(ifa->ifa_ifp)));
                return;
        }
 
@@ -1365,8 +1413,8 @@ nd6_dad_ns_input(ifa)
 }
 
 static void
-nd6_dad_na_input(ifa)
-       struct ifaddr *ifa;
+nd6_dad_na_input(
+       struct ifaddr *ifa)
 {
        struct dadq *dp;