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


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

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

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

    1 /*      $NetBSD: nd6_nbr.c,v 1.65.8.1 2008/10/03 10:31:59 jdc Exp $     */
    2 /*      $KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei Exp $        */
    3 
    4 /*
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: nd6_nbr.c,v 1.65.8.1 2008/10/03 10:31:59 jdc Exp $");
   35 
   36 #include "opt_inet.h"
   37 #include "opt_ipsec.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/socket.h>
   44 #include <sys/sockio.h>
   45 #include <sys/time.h>
   46 #include <sys/kernel.h>
   47 #include <sys/errno.h>
   48 #include <sys/ioctl.h>
   49 #include <sys/syslog.h>
   50 #include <sys/queue.h>
   51 #include <sys/callout.h>
   52 
   53 #include <net/if.h>
   54 #include <net/if_types.h>
   55 #include <net/if_dl.h>
   56 #include <net/route.h>
   57 
   58 #include <netinet/in.h>
   59 #include <netinet/in_var.h>
   60 #include <netinet6/in6_var.h>
   61 #include <netinet6/in6_ifattach.h>
   62 #include <netinet/ip6.h>
   63 #include <netinet6/ip6_var.h>
   64 #include <netinet6/scope6_var.h>
   65 #include <netinet6/nd6.h>
   66 #include <netinet/icmp6.h>
   67 
   68 #ifdef IPSEC
   69 #include <netinet6/ipsec.h>
   70 #endif
   71 
   72 #include "carp.h"
   73 #if NCARP > 0
   74 #include <netinet/ip_carp.h>
   75 #endif
   76 
   77 #include <net/net_osdep.h>
   78 
   79 #define SDL(s) ((struct sockaddr_dl *)s)
   80 
   81 struct dadq;
   82 static struct dadq *nd6_dad_find __P((struct ifaddr *));
   83 static void nd6_dad_starttimer __P((struct dadq *, int));
   84 static void nd6_dad_stoptimer __P((struct dadq *));
   85 static void nd6_dad_timer __P((struct ifaddr *));
   86 static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
   87 static void nd6_dad_ns_input __P((struct ifaddr *));
   88 static void nd6_dad_na_input __P((struct ifaddr *));
   89 
   90 static int dad_ignore_ns = 0;   /* ignore NS in DAD - specwise incorrect*/
   91 static int dad_maxtry = 15;     /* max # of *tries* to transmit DAD packet */
   92 
   93 /*
   94  * Input a Neighbor Solicitation Message.
   95  *
   96  * Based on RFC 2461
   97  * Based on RFC 2462 (duplicate address detection)
   98  */
   99 void
  100 nd6_ns_input(m, off, icmp6len)
  101         struct mbuf *m;
  102         int off, icmp6len;
  103 {
  104         struct ifnet *ifp = m->m_pkthdr.rcvif;
  105         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  106         struct nd_neighbor_solicit *nd_ns;
  107         struct in6_addr saddr6 = ip6->ip6_src;
  108         struct in6_addr daddr6 = ip6->ip6_dst;
  109         struct in6_addr taddr6;
  110         struct in6_addr myaddr6;
  111         char *lladdr = NULL;
  112         struct ifaddr *ifa;
  113         int lladdrlen = 0;
  114         int anycast = 0, proxy = 0, tentative = 0;
  115         int router = ip6_forwarding;
  116         int tlladdr;
  117         union nd_opts ndopts;
  118         struct sockaddr_dl *proxydl = NULL;
  119 
  120         IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
  121         if (nd_ns == NULL) {
  122                 icmp6stat.icp6s_tooshort++;
  123                 return;
  124         }
  125         ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
  126         taddr6 = nd_ns->nd_ns_target;
  127         if (in6_setscope(&taddr6, ifp, NULL) != 0)
  128                 goto bad;
  129 
  130         if (ip6->ip6_hlim != 255) {
  131                 nd6log((LOG_ERR,
  132                     "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
  133                     ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
  134                     ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
  135                 goto bad;
  136         }
  137 
  138         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
  139                 /* dst has to be a solicited node multicast address. */
  140                 /* don't check ifindex portion */
  141                 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
  142                     daddr6.s6_addr32[1] == 0 &&
  143                     daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
  144                     daddr6.s6_addr8[12] == 0xff) {
  145                         ; /* good */
  146                 } else {
  147                         nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  148                             "(wrong ip6 dst)\n"));
  149                         goto bad;
  150                 }
  151         } else {
  152                 /*
  153                  * Make sure the source address is from a neighbor's address.
  154                  */
  155                 if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
  156                         nd6log((LOG_INFO, "nd6_ns_input: "
  157                             "NS packet from non-neighbor\n"));
  158                         goto bad;
  159                 }
  160         }
  161 
  162 
  163         if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  164                 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
  165                 goto bad;
  166         }
  167 
  168         icmp6len -= sizeof(*nd_ns);
  169         nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
  170         if (nd6_options(&ndopts) < 0) {
  171                 nd6log((LOG_INFO,
  172                     "nd6_ns_input: invalid ND option, ignored\n"));
  173                 /* nd6_options have incremented stats */
  174                 goto freeit;
  175         }
  176 
  177         if (ndopts.nd_opts_src_lladdr) {
  178                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  179                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  180         }
  181 
  182         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
  183                 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  184                     "(link-layer address option)\n"));
  185                 goto bad;
  186         }
  187 
  188         /*
  189          * Attaching target link-layer address to the NA?
  190          * (RFC 2461 7.2.4)
  191          *
  192          * NS IP dst is multicast                       MUST add
  193          * Otherwise                                    MAY be omitted
  194          *
  195          * In this implementation, we omit the target link-layer address
  196          * in the "MAY" case. 
  197          */
  198 #if 0 /* too much! */
  199         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
  200         if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
  201                 tlladdr = 0;
  202         else
  203 #endif
  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 #if NCARP > 0
  217         if (ifp->if_carp && ifp->if_type != IFT_CARP)
  218                 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
  219         else
  220                 ifa = NULL;
  221         if (!ifa)
  222                 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
  223 #else
  224         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
  225 #endif
  226 
  227         /* (2) check. */
  228         if (ifa == NULL) {
  229                 struct rtentry *rt;
  230                 struct sockaddr_in6 tsin6;
  231 
  232                 memset(&tsin6, 0, sizeof(tsin6));
  233                 tsin6.sin6_len = sizeof(struct sockaddr_in6);
  234                 tsin6.sin6_family = AF_INET6;
  235                 tsin6.sin6_addr = taddr6;
  236 
  237                 rt = rtalloc1((struct sockaddr *)&tsin6, 0);
  238                 if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
  239                     rt->rt_gateway->sa_family == AF_LINK) {
  240                         /*
  241                          * proxy NDP for single entry
  242                          */
  243                         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
  244                                 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
  245                         if (ifa) {
  246                                 proxy = 1;
  247                                 proxydl = SDL(rt->rt_gateway);
  248                                 router = 0;     /* XXX */
  249                         }
  250                 }
  251                 if (rt)
  252                         rtfree(rt);
  253         }
  254         if (ifa == NULL) {
  255                 /*
  256                  * We've got an NS packet, and we don't have that address
  257                  * assigned for us.  We MUST silently ignore it.
  258                  * See RFC2461 7.2.3.
  259                  */
  260                 goto freeit;
  261         }
  262         myaddr6 = *IFA_IN6(ifa);
  263         anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
  264         tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
  265         if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
  266                 goto freeit;
  267 
  268         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  269                 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
  270                     "(if %d, NS packet %d)\n",
  271                     ip6_sprintf(&taddr6), 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, ND_NEIGHBOR_SOLICIT, 0);
  329 
  330         nd6_na_output(ifp, &saddr6, &taddr6,
  331             ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
  332             (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
  333             tlladdr, (struct sockaddr *)proxydl);
  334  freeit:
  335         m_freem(m);
  336         return;
  337 
  338  bad:
  339         nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
  340         nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
  341         nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
  342         icmp6stat.icp6s_badns++;
  343         m_freem(m);
  344 }
  345 
  346 /*
  347  * Output a Neighbor Solicitation Message. Caller specifies:
  348  *      - ICMP6 header source IP6 address
  349  *      - ND6 header target IP6 address
  350  *      - ND6 header source datalink address
  351  *
  352  * Based on RFC 2461
  353  * Based on RFC 2462 (duplicate address detection)
  354  */
  355 void
  356 nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
  357         struct ifnet *ifp;
  358         const struct in6_addr *daddr6, *taddr6;
  359         struct llinfo_nd6 *ln;  /* for source address determination */
  360         int dad;        /* duplicate address detection */
  361 {
  362         struct mbuf *m;
  363         struct ip6_hdr *ip6;
  364         struct nd_neighbor_solicit *nd_ns;
  365         struct in6_addr *src, src_in;
  366         struct ip6_moptions im6o;
  367         int icmp6len;
  368         int maxlen;
  369         caddr_t mac;
  370         struct route_in6 ro;
  371 
  372         bzero(&ro, sizeof(ro));
  373 
  374         if (IN6_IS_ADDR_MULTICAST(taddr6))
  375                 return;
  376 
  377         /* estimate the size of message */
  378         maxlen = sizeof(*ip6) + sizeof(*nd_ns);
  379         maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  380 #ifdef DIAGNOSTIC
  381         if (max_linkhdr + maxlen >= MCLBYTES) {
  382                 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
  383                     "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
  384                 panic("nd6_ns_output: insufficient MCLBYTES");
  385                 /* NOTREACHED */
  386         }
  387 #endif
  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                  * (hsrc), if:
  444                  * - hsrc is given from the caller (by giving "ln"), and
  445                  * - hsrc 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_int16_t)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 saddr6 = ip6->ip6_src;
  572         struct in6_addr daddr6 = ip6->ip6_dst;
  573         struct in6_addr taddr6;
  574         int flags;
  575         int is_router;
  576         int is_solicited;
  577         int is_override;
  578         char *lladdr = NULL;
  579         int lladdrlen = 0;
  580         struct ifaddr *ifa;
  581         struct llinfo_nd6 *ln;
  582         struct rtentry *rt;
  583         struct sockaddr_dl *sdl;
  584         union nd_opts ndopts;
  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(&ip6->ip6_src),
  590                     ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
  591                 goto bad;
  592         }
  593 
  594         IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
  595         if (nd_na == NULL) {
  596                 icmp6stat.icp6s_tooshort++;
  597                 return;
  598         }
  599 
  600         flags = nd_na->nd_na_flags_reserved;
  601         is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
  602         is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
  603         is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
  604 
  605         taddr6 = nd_na->nd_na_target;
  606         if (in6_setscope(&taddr6, ifp, NULL))
  607                 return;         /* XXX: impossible */
  608 
  609         if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  610                 nd6log((LOG_ERR,
  611                     "nd6_na_input: invalid target address %s\n",
  612                     ip6_sprintf(&taddr6)));
  613                 goto bad;
  614         }
  615         if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
  616                 nd6log((LOG_ERR,
  617                     "nd6_na_input: a solicited adv is multicasted\n"));
  618                 goto bad;
  619         }
  620 
  621         icmp6len -= sizeof(*nd_na);
  622         nd6_option_init(nd_na + 1, icmp6len, &ndopts);
  623         if (nd6_options(&ndopts) < 0) {
  624                 nd6log((LOG_INFO,
  625                     "nd6_na_input: invalid ND option, ignored\n"));
  626                 /* nd6_options have incremented stats */
  627                 goto freeit;
  628         }
  629 
  630         if (ndopts.nd_opts_tgt_lladdr) {
  631                 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
  632                 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
  633         }
  634 
  635         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
  636 
  637         /*
  638          * Target address matches one of my interface address.
  639          *
  640          * If my address is tentative, this means that there's somebody
  641          * already using the same address as mine.  This indicates DAD failure.
  642          * This is defined in RFC 2462.
  643          *
  644          * Otherwise, process as defined in RFC 2461.
  645          */
  646         if (ifa
  647          && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
  648                 nd6_dad_na_input(ifa);
  649                 goto freeit;
  650         }
  651 
  652         /* Just for safety, maybe unnecessary. */
  653         if (ifa) {
  654                 log(LOG_ERR,
  655                     "nd6_na_input: duplicate IP6 address %s\n",
  656                     ip6_sprintf(&taddr6));
  657                 goto freeit;
  658         }
  659         /*
  660          * Make sure the source address is from a neighbor's address.
  661          */
  662         if (in6ifa_ifplocaladdr(ifp, &saddr6) == NULL) {
  663                 nd6log((LOG_INFO, "nd6_ns_input: "
  664                     "ND packet from non-neighbor\n"));
  665                 goto bad;
  666         }
  667 
  668         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  669                 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
  670                     "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
  671                     ifp->if_addrlen, lladdrlen - 2));
  672                 goto bad;
  673         }
  674 
  675         /*
  676          * If no neighbor cache entry is found, NA SHOULD silently be
  677          * discarded.
  678          */
  679         rt = nd6_lookup(&taddr6, 0, ifp);
  680         if ((rt == NULL) ||
  681            ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
  682            ((sdl = SDL(rt->rt_gateway)) == NULL))
  683                 goto freeit;
  684 
  685         if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
  686                 /*
  687                  * If the link-layer has address, and no lladdr option came,
  688                  * discard the packet.
  689                  */
  690                 if (ifp->if_addrlen && !lladdr)
  691                         goto freeit;
  692 
  693                 /*
  694                  * Record link-layer address, and update the state.
  695                  */
  696                 sdl->sdl_alen = ifp->if_addrlen;
  697                 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
  698                 if (is_solicited) {
  699                         ln->ln_state = ND6_LLINFO_REACHABLE;
  700                         ln->ln_byhint = 0;
  701                         if (!ND6_LLINFO_PERMANENT(ln)) {
  702                                 nd6_llinfo_settimer(ln,
  703                                     (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
  704                         }
  705                 } else {
  706                         ln->ln_state = ND6_LLINFO_STALE;
  707                         nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  708                 }
  709                 if ((ln->ln_router = is_router) != 0) {
  710                         /*
  711                          * This means a router's state has changed from
  712                          * non-reachable to probably reachable, and might
  713                          * affect the status of associated prefixes..
  714                          */
  715                         pfxlist_onlink_check();
  716                 }
  717         } else {
  718                 int llchange;
  719 
  720                 /*
  721                  * Check if the link-layer address has changed or not.
  722                  */
  723                 if (lladdr == NULL)
  724                         llchange = 0;
  725                 else {
  726                         if (sdl->sdl_alen) {
  727                                 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
  728                                         llchange = 1;
  729                                 else
  730                                         llchange = 0;
  731                         } else
  732                                 llchange = 1;
  733                 }
  734 
  735                 /*
  736                  * This is VERY complex.  Look at it with care.
  737                  *
  738                  * override solicit lladdr llchange     action
  739                  *                                      (L: record lladdr)
  740                  *
  741                  *      0       0       n       --      (2c)
  742                  *      0       0       y       n       (2b) L
  743                  *      0       0       y       y       (1)    REACHABLE->STALE
  744                  *      0       1       n       --      (2c)   *->REACHABLE
  745                  *      0       1       y       n       (2b) L *->REACHABLE
  746                  *      0       1       y       y       (1)    REACHABLE->STALE
  747                  *      1       0       n       --      (2a)
  748                  *      1       0       y       n       (2a) L
  749                  *      1       0       y       y       (2a) L *->STALE
  750                  *      1       1       n       --      (2a)   *->REACHABLE
  751                  *      1       1       y       n       (2a) L *->REACHABLE
  752                  *      1       1       y       y       (2a) L *->REACHABLE
  753                  */
  754                 if (!is_override && lladdr != NULL && llchange) { /* (1) */
  755                         /*
  756                          * If state is REACHABLE, make it STALE.
  757                          * no other updates should be done.
  758                          */
  759                         if (ln->ln_state == ND6_LLINFO_REACHABLE) {
  760                                 ln->ln_state = ND6_LLINFO_STALE;
  761                                 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  762                         }
  763                         goto freeit;
  764                 } else if (is_override                             /* (2a) */
  765                     || (!is_override && lladdr != NULL && !llchange) /* (2b) */
  766                     || lladdr == NULL) {                           /* (2c) */
  767                         /*
  768                          * Update link-local address, if any.
  769                          */
  770                         if (lladdr != NULL) {
  771                                 sdl->sdl_alen = ifp->if_addrlen;
  772                                 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
  773                         }
  774 
  775                         /*
  776                          * If solicited, make the state REACHABLE.
  777                          * If not solicited and the link-layer address was
  778                          * changed, make it STALE.
  779                          */
  780                         if (is_solicited) {
  781                                 ln->ln_state = ND6_LLINFO_REACHABLE;
  782                                 ln->ln_byhint = 0;
  783                                 if (!ND6_LLINFO_PERMANENT(ln)) {
  784                                         nd6_llinfo_settimer(ln,
  785                                             (long)ND_IFINFO(ifp)->reachable * hz);
  786                                 }
  787                         } else {
  788                                 if (lladdr && llchange) {
  789                                         ln->ln_state = ND6_LLINFO_STALE;
  790                                         nd6_llinfo_settimer(ln,
  791                                             (long)nd6_gctimer * hz);
  792                                 }
  793                         }
  794                 }
  795 
  796                 if (ln->ln_router && !is_router) {
  797                         /*
  798                          * The peer dropped the router flag.
  799                          * Remove the sender from the Default Router List and
  800                          * update the Destination Cache entries.
  801                          */
  802                         struct nd_defrouter *dr;
  803                         struct in6_addr *in6;
  804                         int s;
  805 
  806                         in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
  807 
  808                         /*
  809                          * Lock to protect the default router list.
  810                          * XXX: this might be unnecessary, since this function
  811                          * is only called under the network software interrupt
  812                          * context.  However, we keep it just for safety.
  813                          */
  814                         s = splsoftnet();
  815                         dr = defrouter_lookup(in6, rt->rt_ifp);
  816                         if (dr)
  817                                 defrtrlist_del(dr);
  818                         else if (!ip6_forwarding) {
  819                                 /*
  820                                  * Even if the neighbor is not in the default
  821                                  * router list, the neighbor may be used
  822                                  * as a next hop for some destinations
  823                                  * (e.g. redirect case). So we must
  824                                  * call rt6_flush explicitly.
  825                                  */
  826                                 rt6_flush(&ip6->ip6_src, rt->rt_ifp);
  827                         }
  828                         splx(s);
  829                 }
  830                 ln->ln_router = is_router;
  831         }
  832         rt->rt_flags &= ~RTF_REJECT;
  833         ln->ln_asked = 0;
  834         if (ln->ln_hold) {
  835                 struct mbuf *m_hold, *m_hold_next;
  836 
  837                 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
  838                         struct mbuf *mpkt = NULL;
  839                         
  840                         m_hold_next = m_hold->m_nextpkt;
  841                         mpkt = m_copym(m_hold, 0, M_COPYALL, M_DONTWAIT);
  842                         if (mpkt == NULL) {
  843                                 m_freem(m_hold);
  844                                 break;
  845                         }
  846                         mpkt->m_nextpkt = NULL;
  847                         /*
  848                          * we assume ifp is not a loopback here, so just set
  849                          * the 2nd argument as the 1st one.
  850                          */
  851                         nd6_output(ifp, ifp, mpkt,
  852                             (struct sockaddr_in6 *)rt_key(rt), rt);
  853                 }
  854                 ln->ln_hold = NULL;
  855         }
  856 
  857  freeit:
  858         m_freem(m);
  859         return;
  860 
  861  bad:
  862         icmp6stat.icp6s_badna++;
  863         m_freem(m);
  864 }
  865 
  866 /*
  867  * Neighbor advertisement output handling.
  868  *
  869  * Based on RFC 2461
  870  *
  871  * the following items are not implemented yet:
  872  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
  873  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  874  */
  875 void
  876 nd6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0)
  877         struct ifnet *ifp;
  878         const struct in6_addr *daddr6_0, *taddr6;
  879         u_long flags;
  880         int tlladdr;            /* 1 if include target link-layer address */
  881         struct sockaddr *sdl0;  /* sockaddr_dl (= proxy NA) or NULL */
  882 {
  883         struct mbuf *m;
  884         struct ip6_hdr *ip6;
  885         struct nd_neighbor_advert *nd_na;
  886         struct ip6_moptions im6o;
  887         struct sockaddr_in6 dst_sa;
  888         struct in6_addr *src, daddr6;
  889         int icmp6len, maxlen, error;
  890         caddr_t mac;
  891         struct route_in6 ro;
  892 
  893         mac = NULL;
  894         bzero(&ro, sizeof(ro));
  895 
  896         daddr6 = *daddr6_0;     /* make a local copy for modification */
  897 
  898         /* estimate the size of message */
  899         maxlen = sizeof(*ip6) + sizeof(*nd_na);
  900         maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  901 #ifdef DIAGNOSTIC
  902         if (max_linkhdr + maxlen >= MCLBYTES) {
  903                 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
  904                     "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
  905                 panic("nd6_na_output: insufficient MCLBYTES");
  906                 /* NOTREACHED */
  907         }
  908 #endif
  909 
  910         MGETHDR(m, M_DONTWAIT, MT_DATA);
  911         if (m && max_linkhdr + maxlen >= MHLEN) {
  912                 MCLGET(m, M_DONTWAIT);
  913                 if ((m->m_flags & M_EXT) == 0) {
  914                         m_free(m);
  915                         m = NULL;
  916                 }
  917         }
  918         if (m == NULL)
  919                 return;
  920         m->m_pkthdr.rcvif = NULL;
  921 
  922         if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
  923                 m->m_flags |= M_MCAST;
  924                 im6o.im6o_multicast_ifp = ifp;
  925                 im6o.im6o_multicast_hlim = 255;
  926                 im6o.im6o_multicast_loop = 0;
  927         }
  928 
  929         icmp6len = sizeof(*nd_na);
  930         m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
  931         m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
  932 
  933         /* fill neighbor advertisement packet */
  934         ip6 = mtod(m, struct ip6_hdr *);
  935         ip6->ip6_flow = 0;
  936         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  937         ip6->ip6_vfc |= IPV6_VERSION;
  938         ip6->ip6_nxt = IPPROTO_ICMPV6;
  939         ip6->ip6_hlim = 255;
  940         if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
  941                 /* reply to DAD */
  942                 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
  943                 daddr6.s6_addr16[1] = 0;
  944                 daddr6.s6_addr32[1] = 0;
  945                 daddr6.s6_addr32[2] = 0;
  946                 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
  947                 if (in6_setscope(&daddr6, ifp, NULL))
  948                         goto bad;
  949 
  950                 flags &= ~ND_NA_FLAG_SOLICITED;
  951         }
  952         ip6->ip6_dst = daddr6;
  953         bzero(&dst_sa, sizeof(struct sockaddr_in6));
  954         dst_sa.sin6_family = AF_INET6;
  955         dst_sa.sin6_len = sizeof(struct sockaddr_in6);
  956         dst_sa.sin6_addr = daddr6;
  957 
  958         /*
  959          * Select a source whose scope is the same as that of the dest.
  960          */
  961         bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
  962         src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
  963         if (src == NULL) {
  964                 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
  965                     "determined: dst=%s, error=%d\n",
  966                     ip6_sprintf(&dst_sa.sin6_addr), error));
  967                 goto bad;
  968         }
  969         ip6->ip6_src = *src;
  970         nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
  971         nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
  972         nd_na->nd_na_code = 0;
  973         nd_na->nd_na_target = *taddr6;
  974         in6_clearscope(&nd_na->nd_na_target); /* XXX */
  975 
  976         /*
  977          * "tlladdr" indicates NS's condition for adding tlladdr or not.
  978          * see nd6_ns_input() for details.
  979          * Basically, if NS packet is sent to unicast/anycast addr,
  980          * target lladdr option SHOULD NOT be included.
  981          */
  982         if (tlladdr) {
  983                 /*
  984                  * sdl0 != NULL indicates proxy NA.  If we do proxy, use
  985                  * lladdr in sdl0.  If we are not proxying (sending NA for
  986                  * my address) use lladdr configured for the interface.
  987                  */
  988                 if (sdl0 == NULL)
  989                         mac = nd6_ifptomac(ifp);
  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_int16_t)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, NULL, 0, &im6o, (struct socket *)NULL, NULL);
 1021 
 1022         icmp6_ifstat_inc(ifp, ifs6_out_msg);
 1023         icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
 1024         icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
 1025 
 1026         if (ro.ro_rt) {         /* we don't cache this route. */
 1027                 RTFREE(ro.ro_rt);
 1028         }
 1029         return;
 1030 
 1031   bad:
 1032         if (ro.ro_rt) {
 1033                 RTFREE(ro.ro_rt);
 1034         }
 1035         m_freem(m);
 1036         return;
 1037 }
 1038 
 1039 caddr_t
 1040 nd6_ifptomac(ifp)
 1041         struct ifnet *ifp;
 1042 {
 1043         switch (ifp->if_type) {
 1044         case IFT_ARCNET:
 1045         case IFT_ETHER:
 1046         case IFT_FDDI:
 1047         case IFT_IEEE1394:
 1048         case IFT_PROPVIRTUAL:
 1049         case IFT_CARP:
 1050         case IFT_L2VLAN:
 1051         case IFT_IEEE80211:
 1052                 return LLADDR(ifp->if_sadl);
 1053         default:
 1054                 return NULL;
 1055         }
 1056 }
 1057 
 1058 TAILQ_HEAD(dadq_head, dadq);
 1059 struct dadq {
 1060         TAILQ_ENTRY(dadq) dad_list;
 1061         struct ifaddr *dad_ifa;
 1062         int dad_count;          /* max NS to send */
 1063         int dad_ns_tcount;      /* # of trials to send NS */
 1064         int dad_ns_ocount;      /* NS sent so far */
 1065         int dad_ns_icount;
 1066         int dad_na_icount;
 1067         struct callout dad_timer_ch;
 1068 };
 1069 
 1070 static struct dadq_head dadq;
 1071 static int dad_init = 0;
 1072 
 1073 static struct dadq *
 1074 nd6_dad_find(ifa)
 1075         struct ifaddr *ifa;
 1076 {
 1077         struct dadq *dp;
 1078 
 1079         for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
 1080                 if (dp->dad_ifa == ifa)
 1081                         return dp;
 1082         }
 1083         return NULL;
 1084 }
 1085 
 1086 static void
 1087 nd6_dad_starttimer(dp, ticks)
 1088         struct dadq *dp;
 1089         int ticks;
 1090 {
 1091 
 1092         callout_reset(&dp->dad_timer_ch, ticks,
 1093             (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
 1094 }
 1095 
 1096 static void
 1097 nd6_dad_stoptimer(dp)
 1098         struct dadq *dp;
 1099 {
 1100 
 1101         callout_stop(&dp->dad_timer_ch);
 1102 }
 1103 
 1104 /*
 1105  * Start Duplicate Address Detection (DAD) for specified interface address.
 1106  */
 1107 void
 1108 nd6_dad_start(ifa, xtick)
 1109         struct ifaddr *ifa;
 1110         int xtick;      /* minimum delay ticks for IFF_UP event */
 1111 {
 1112         struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
 1113         struct dadq *dp;
 1114 
 1115         if (!dad_init) {
 1116                 TAILQ_INIT(&dadq);
 1117                 dad_init++;
 1118         }
 1119 
 1120         /*
 1121          * If we don't need DAD, don't do it.
 1122          * There are several cases:
 1123          * - DAD is disabled (ip6_dad_count == 0)
 1124          * - the interface address is anycast
 1125          */
 1126         if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
 1127                 log(LOG_DEBUG,
 1128                         "nd6_dad_start: called with non-tentative address "
 1129                         "%s(%s)\n",
 1130                         ip6_sprintf(&ia->ia_addr.sin6_addr),
 1131                         ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
 1132                 return;
 1133         }
 1134         if (ia->ia6_flags & IN6_IFF_ANYCAST) {
 1135                 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
 1136                 return;
 1137         }
 1138         if (!ip6_dad_count) {
 1139                 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
 1140                 return;
 1141         }
 1142         if (ifa->ifa_ifp == NULL)
 1143                 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
 1144         if (!(ifa->ifa_ifp->if_flags & IFF_UP))
 1145                 return;
 1146         if (nd6_dad_find(ifa) != NULL) {
 1147                 /* DAD already in progress */
 1148                 return;
 1149         }
 1150 
 1151         dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
 1152         if (dp == NULL) {
 1153                 log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
 1154                         "%s(%s)\n",
 1155                         ip6_sprintf(&ia->ia_addr.sin6_addr),
 1156                         ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
 1157                 return;
 1158         }
 1159         bzero(dp, sizeof(*dp));
 1160         callout_init(&dp->dad_timer_ch);
 1161         TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
 1162 
 1163         nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
 1164             ip6_sprintf(&ia->ia_addr.sin6_addr)));
 1165 
 1166         /*
 1167          * Send NS packet for DAD, ip6_dad_count times.
 1168          * Note that we must delay the first transmission, if this is the
 1169          * first packet to be sent from the interface after interface
 1170          * (re)initialization.
 1171          */
 1172         dp->dad_ifa = ifa;
 1173         IFAREF(ifa);    /* just for safety */
 1174         dp->dad_count = ip6_dad_count;
 1175         dp->dad_ns_icount = dp->dad_na_icount = 0;
 1176         dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
 1177         if (xtick == 0) {
 1178                 nd6_dad_ns_output(dp, ifa);
 1179                 nd6_dad_starttimer(dp,
 1180                     (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
 1181         } else
 1182                 nd6_dad_starttimer(dp, xtick);
 1183 }
 1184 
 1185 /*
 1186  * terminate DAD unconditionally.  used for address removals.
 1187  */
 1188 void
 1189 nd6_dad_stop(ifa)
 1190         struct ifaddr *ifa;
 1191 {
 1192         struct dadq *dp;
 1193 
 1194         if (!dad_init)
 1195                 return;
 1196         dp = nd6_dad_find(ifa);
 1197         if (dp == NULL) {
 1198                 /* DAD wasn't started yet */
 1199                 return;
 1200         }
 1201 
 1202         nd6_dad_stoptimer(dp);
 1203 
 1204         TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
 1205         free(dp, M_IP6NDP);
 1206         dp = NULL;
 1207         IFAFREE(ifa);
 1208 }
 1209 
 1210 static void
 1211 nd6_dad_timer(ifa)
 1212         struct ifaddr *ifa;
 1213 {
 1214         int s;
 1215         struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
 1216         struct dadq *dp;
 1217 
 1218         s = splsoftnet();       /* XXX */
 1219 
 1220         /* Sanity check */
 1221         if (ia == NULL) {
 1222                 log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
 1223                 goto done;
 1224         }
 1225         dp = nd6_dad_find(ifa);
 1226         if (dp == NULL) {
 1227                 log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
 1228                 goto done;
 1229         }
 1230         if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
 1231                 log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
 1232                         "%s(%s)\n",
 1233                         ip6_sprintf(&ia->ia_addr.sin6_addr),
 1234                         ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
 1235                 goto done;
 1236         }
 1237         if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
 1238                 log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
 1239                         "%s(%s)\n",
 1240                         ip6_sprintf(&ia->ia_addr.sin6_addr),
 1241                         ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
 1242                 goto done;
 1243         }
 1244 
 1245         /* timeouted with IFF_{RUNNING,UP} check */
 1246         if (dp->dad_ns_tcount > dad_maxtry) {
 1247                 nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
 1248                         if_name(ifa->ifa_ifp)));
 1249 
 1250                 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
 1251                 free(dp, M_IP6NDP);
 1252                 dp = NULL;
 1253                 IFAFREE(ifa);
 1254                 goto done;
 1255         }
 1256 
 1257         /* Need more checks? */
 1258         if (dp->dad_ns_ocount < dp->dad_count) {
 1259                 /*
 1260                  * We have more NS to go.  Send NS packet for DAD.
 1261                  */
 1262                 nd6_dad_ns_output(dp, ifa);
 1263                 nd6_dad_starttimer(dp,
 1264                     (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
 1265         } else {
 1266                 /*
 1267                  * We have transmitted sufficient number of DAD packets.
 1268                  * See what we've got.
 1269                  */
 1270                 int duplicate;
 1271 
 1272                 duplicate = 0;
 1273 
 1274                 if (dp->dad_na_icount) {
 1275                         /*
 1276                          * the check is in nd6_dad_na_input(),
 1277                          * but just in case
 1278                          */
 1279                         duplicate++;
 1280                 }
 1281 
 1282                 if (dp->dad_ns_icount) {
 1283                         /* We've seen NS, means DAD has failed. */
 1284                         duplicate++;
 1285                 }
 1286 
 1287                 if (duplicate) {
 1288                         /* (*dp) will be freed in nd6_dad_duplicated() */
 1289                         dp = NULL;
 1290                         nd6_dad_duplicated(ifa);
 1291                 } else {
 1292                         /*
 1293                          * We are done with DAD.  No NA came, no NS came.
 1294                          * No duplicate address found.
 1295                          */
 1296                         ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
 1297 
 1298                         nd6log((LOG_DEBUG,
 1299                             "%s: DAD complete for %s - no duplicates found\n",
 1300                             if_name(ifa->ifa_ifp),
 1301                             ip6_sprintf(&ia->ia_addr.sin6_addr)));
 1302 
 1303                         TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
 1304                         free(dp, M_IP6NDP);
 1305                         dp = NULL;
 1306                         IFAFREE(ifa);
 1307                 }
 1308         }
 1309 
 1310 done:
 1311         splx(s);
 1312 }
 1313 
 1314 void
 1315 nd6_dad_duplicated(ifa)
 1316         struct ifaddr *ifa;
 1317 {
 1318         struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
 1319         struct ifnet *ifp;
 1320         struct dadq *dp;
 1321 
 1322         dp = nd6_dad_find(ifa);
 1323         if (dp == NULL) {
 1324                 log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
 1325                 return;
 1326         }
 1327 
 1328         ifp = ifa->ifa_ifp;
 1329         log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
 1330             "NS in/out=%d/%d, NA in=%d\n",
 1331             if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
 1332             dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
 1333 
 1334         ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
 1335         ia->ia6_flags |= IN6_IFF_DUPLICATED;
 1336 
 1337         /* We are done with DAD, with duplicated address found. (failure) */
 1338         nd6_dad_stoptimer(dp);
 1339 
 1340         log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
 1341             if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
 1342         log(LOG_ERR, "%s: manual intervention required\n",
 1343             if_name(ifp));
 1344 
 1345         /*
 1346          * If the address is a link-local address formed from an interface
 1347          * identifier based on the hardware address which is supposed to be
 1348          * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
 1349          * operation on the interface SHOULD be disabled.
 1350          * [rfc2462bis-03 Section 5.4.5]
 1351          */
 1352         if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
 1353                 struct in6_addr in6;
 1354 
 1355                 /*
 1356                  * To avoid over-reaction, we only apply this logic when we are
 1357                  * very sure that hardware addresses are supposed to be unique.
 1358                  */
 1359                 switch (ifp->if_type) {
 1360                 case IFT_ETHER:
 1361                 case IFT_FDDI:
 1362                 case IFT_ATM:
 1363                 case IFT_IEEE1394:
 1364 #ifdef IFT_IEEE80211
 1365                 case IFT_IEEE80211:
 1366 #endif
 1367                         in6 = ia->ia_addr.sin6_addr;
 1368                         if (in6_get_hw_ifid(ifp, &in6) == 0 &&
 1369                             IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
 1370                                 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
 1371                                 log(LOG_ERR, "%s: possible hardware address "
 1372                                     "duplication detected, disable IPv6\n",
 1373                                     if_name(ifp));
 1374                         }
 1375                         break;
 1376                 }
 1377         }
 1378 
 1379         TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
 1380         free(dp, M_IP6NDP);
 1381         dp = NULL;
 1382         IFAFREE(ifa);
 1383 }
 1384 
 1385 static void
 1386 nd6_dad_ns_output(dp, ifa)
 1387         struct dadq *dp;
 1388         struct ifaddr *ifa;
 1389 {
 1390         struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
 1391         struct ifnet *ifp = ifa->ifa_ifp;
 1392 
 1393         dp->dad_ns_tcount++;
 1394         if ((ifp->if_flags & IFF_UP) == 0) {
 1395 #if 0
 1396                 printf("%s: interface down?\n", if_name(ifp));
 1397 #endif
 1398                 return;
 1399         }
 1400         if ((ifp->if_flags & IFF_RUNNING) == 0) {
 1401 #if 0
 1402                 printf("%s: interface not running?\n", if_name(ifp));
 1403 #endif
 1404                 return;
 1405         }
 1406 
 1407         dp->dad_ns_tcount = 0;
 1408         dp->dad_ns_ocount++;
 1409         nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
 1410 }
 1411 
 1412 static void
 1413 nd6_dad_ns_input(ifa)
 1414         struct ifaddr *ifa;
 1415 {
 1416         struct in6_ifaddr *ia;
 1417         const struct in6_addr *taddr6;
 1418         struct dadq *dp;
 1419         int duplicate;
 1420 
 1421         if (ifa == NULL)
 1422                 panic("ifa == NULL in nd6_dad_ns_input");
 1423 
 1424         ia = (struct in6_ifaddr *)ifa;
 1425         taddr6 = &ia->ia_addr.sin6_addr;
 1426         duplicate = 0;
 1427         dp = nd6_dad_find(ifa);
 1428 
 1429         /* Quickhack - completely ignore DAD NS packets */
 1430         if (dad_ignore_ns) {
 1431                 nd6log((LOG_INFO,
 1432                     "nd6_dad_ns_input: ignoring DAD NS packet for "
 1433                     "address %s(%s)\n", ip6_sprintf(taddr6),
 1434                     if_name(ifa->ifa_ifp)));
 1435                 return;
 1436         }
 1437 
 1438         /*
 1439          * if I'm yet to start DAD, someone else started using this address
 1440          * first.  I have a duplicate and you win.
 1441          */
 1442         if (dp == NULL || dp->dad_ns_ocount == 0)
 1443                 duplicate++;
 1444 
 1445         /* XXX more checks for loopback situation - see nd6_dad_timer too */
 1446 
 1447         if (duplicate) {
 1448                 dp = NULL;      /* will be freed in nd6_dad_duplicated() */
 1449                 nd6_dad_duplicated(ifa);
 1450         } else {
 1451                 /*
 1452                  * not sure if I got a duplicate.
 1453                  * increment ns count and see what happens.
 1454                  */
 1455                 if (dp)
 1456                         dp->dad_ns_icount++;
 1457         }
 1458 }
 1459 
 1460 static void
 1461 nd6_dad_na_input(ifa)
 1462         struct ifaddr *ifa;
 1463 {
 1464         struct dadq *dp;
 1465 
 1466         if (ifa == NULL)
 1467                 panic("ifa == NULL in nd6_dad_na_input");
 1468 
 1469         dp = nd6_dad_find(ifa);
 1470         if (dp)
 1471                 dp->dad_na_icount++;
 1472 
 1473         /* remove the address. */
 1474         nd6_dad_duplicated(ifa);
 1475 }

Cache object: e0ebfdf73d8d791cc04b347a5cec5057


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