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

Cache object: d0f7b198ada298c05883a35955255fe2


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