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

Cache object: e954afd0f0ec41a131869749cf151115


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