The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/nd6_nbr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 608e7a72c113886bde810b83de434f71


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.