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

Cache object: 8990a1b66d740f768cf3a3fbe351229a


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