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

Cache object: f664000caac92d0cc6692aaf2bfb9aff


[ 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.