[BACK]Return to nd6_nbr.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet6

Annotation of src/sys/netinet6/nd6_nbr.c, Revision 1.22.4.1

1.22.4.1! he          1: /*     $NetBSD: nd6_nbr.c,v 1.22 2000/05/19 01:40:19 itojun Exp $      */
        !             2: /*     $KAME: nd6_nbr.c,v 1.51 2001/01/20 17:27:00 itojun Exp $        */
1.3       thorpej     3:
1.2       itojun      4: /*
                      5:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      6:  * All rights reserved.
1.22      itojun      7:  *
1.2       itojun      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the project nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
1.22      itojun     19:  *
1.2       itojun     20:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include "opt_inet.h"
1.5       thorpej    34: #include "opt_ipsec.h"
1.2       itojun     35:
                     36: #include <sys/param.h>
                     37: #include <sys/systm.h>
                     38: #include <sys/malloc.h>
                     39: #include <sys/mbuf.h>
                     40: #include <sys/socket.h>
                     41: #include <sys/sockio.h>
                     42: #include <sys/time.h>
                     43: #include <sys/kernel.h>
                     44: #include <sys/errno.h>
                     45: #include <sys/ioctl.h>
                     46: #include <sys/syslog.h>
                     47: #include <sys/queue.h>
1.21      itojun     48: #include <sys/callout.h>
1.2       itojun     49:
                     50: #include <net/if.h>
                     51: #include <net/if_types.h>
                     52: #include <net/if_dl.h>
                     53: #include <net/route.h>
                     54:
                     55: #include <netinet/in.h>
                     56: #include <netinet/in_var.h>
                     57: #include <netinet6/in6_var.h>
1.14      itojun     58: #include <netinet/ip6.h>
1.2       itojun     59: #include <netinet6/ip6_var.h>
                     60: #include <netinet6/nd6.h>
1.14      itojun     61: #include <netinet/icmp6.h>
1.2       itojun     62:
1.18      itojun     63: #ifdef IPSEC
                     64: #include <netinet6/ipsec.h>
                     65: #endif
                     66:
1.9       itojun     67: #include <net/net_osdep.h>
                     68:
1.2       itojun     69: #define SDL(s) ((struct sockaddr_dl *)s)
                     70:
                     71: struct dadq;
                     72: static struct dadq *nd6_dad_find __P((struct ifaddr *));
                     73: static void nd6_dad_timer __P((struct ifaddr *));
1.9       itojun     74: static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
1.2       itojun     75: static void nd6_dad_ns_input __P((struct ifaddr *));
                     76: static void nd6_dad_na_input __P((struct ifaddr *));
                     77:
1.9       itojun     78: static int dad_ignore_ns = 0;  /* ignore NS in DAD - specwise incorrect*/
                     79: static int dad_maxtry = 15;    /* max # of *tries* to transmit DAD packet */
1.2       itojun     80:
                     81: /*
                     82:  * Input an Neighbor Solicitation Message.
                     83:  *
                     84:  * Based on RFC 2461
                     85:  * Based on RFC 2462 (duplicated address detection)
                     86:  */
                     87: void
                     88: nd6_ns_input(m, off, icmp6len)
                     89:        struct mbuf *m;
                     90:        int off, icmp6len;
                     91: {
                     92:        struct ifnet *ifp = m->m_pkthdr.rcvif;
                     93:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1.16      itojun     94:        struct nd_neighbor_solicit *nd_ns;
1.2       itojun     95:        struct in6_addr saddr6 = ip6->ip6_src;
                     96:        struct in6_addr daddr6 = ip6->ip6_dst;
1.16      itojun     97:        struct in6_addr taddr6;
1.2       itojun     98:        struct in6_addr myaddr6;
                     99:        char *lladdr = NULL;
                    100:        struct ifaddr *ifa;
                    101:        int lladdrlen = 0;
                    102:        int anycast = 0, proxy = 0, tentative = 0;
                    103:        int tlladdr;
                    104:        union nd_opts ndopts;
1.16      itojun    105:        struct sockaddr_dl *proxydl = NULL;
1.2       itojun    106:
                    107:        if (ip6->ip6_hlim != 255) {
                    108:                log(LOG_ERR,
                    109:                    "nd6_ns_input: invalid hlim %d\n", ip6->ip6_hlim);
1.16      itojun    110:                goto freeit;
1.2       itojun    111:        }
                    112:
                    113:        if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
                    114:                /* dst has to be solicited node multicast address. */
                    115:                if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
                    116:                    /*don't check ifindex portion*/
                    117:                    && daddr6.s6_addr32[1] == 0
                    118:                    && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
                    119:                    && daddr6.s6_addr8[12] == 0xff) {
                    120:                        ; /*good*/
                    121:                } else {
                    122:                        log(LOG_INFO, "nd6_ns_input: bad DAD packet "
                    123:                                "(wrong ip6 dst)\n");
                    124:                        goto bad;
                    125:                }
                    126:        }
                    127:
1.16      itojun    128: #ifndef PULLDOWN_TEST
                    129:        IP6_EXTHDR_CHECK(m, off, icmp6len,);
                    130:        nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
                    131: #else
                    132:        IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
                    133:        if (nd_ns == NULL) {
                    134:                icmp6stat.icp6s_tooshort++;
                    135:                return;
                    136:        }
                    137: #endif
                    138:        taddr6 = nd_ns->nd_ns_target;
                    139:
1.2       itojun    140:        if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
                    141:                log(LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n");
                    142:                goto bad;
                    143:        }
                    144:
                    145:        if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
                    146:                taddr6.s6_addr16[1] = htons(ifp->if_index);
                    147:
                    148:        icmp6len -= sizeof(*nd_ns);
                    149:        nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
                    150:        if (nd6_options(&ndopts) < 0) {
                    151:                log(LOG_INFO, "nd6_ns_input: invalid ND option, ignored\n");
                    152:                goto bad;
                    153:        }
                    154:
                    155:        if (ndopts.nd_opts_src_lladdr) {
                    156:                lladdr = (char *)(ndopts.nd_opts_src_lladdr +1);
                    157:                lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
                    158:        }
                    159:
                    160:        if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
                    161:                log(LOG_INFO, "nd6_ns_input: bad DAD packet "
                    162:                        "(link-layer address option)\n");
                    163:                goto bad;
                    164:        }
                    165:
                    166:        /*
                    167:         * Attaching target link-layer address to the NA?
                    168:         * (RFC 2461 7.2.4)
                    169:         *
                    170:         * NS IP dst is unicast/anycast                 MUST NOT add
                    171:         * NS IP dst is solicited-node multicast        MUST add
                    172:         *
                    173:         * In implementation, we add target link-layer address by default.
                    174:         * We do not add one in MUST NOT cases.
                    175:         */
                    176: #if 0 /* too much! */
                    177:        ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
                    178:        if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
                    179:                tlladdr = 0;
                    180:        else
                    181: #endif
                    182:        if (!IN6_IS_ADDR_MULTICAST(&daddr6))
                    183:                tlladdr = 0;
                    184:        else
                    185:                tlladdr = 1;
                    186:
                    187:        /*
                    188:         * Target address (taddr6) must be either:
                    189:         * (1) Valid unicast/anycast address for my receiving interface,
                    190:         * (2) Unicast address for which I'm offering proxy service, or
                    191:         * (3) "tentative" address on which DAD is being performed.
                    192:         */
                    193:        /* (1) and (3) check. */
                    194:        ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
                    195:
                    196:        /* (2) check. */
1.16      itojun    197:        if (!ifa) {
1.2       itojun    198:                struct rtentry *rt;
                    199:                struct sockaddr_in6 tsin6;
                    200:
                    201:                bzero(&tsin6, sizeof tsin6);
                    202:                tsin6.sin6_len = sizeof(struct sockaddr_in6);
                    203:                tsin6.sin6_family = AF_INET6;
                    204:                tsin6.sin6_addr = taddr6;
                    205:
1.17      itojun    206:                rt = rtalloc1((struct sockaddr *)&tsin6, 0);
1.16      itojun    207:                if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
                    208:                    rt->rt_gateway->sa_family == AF_LINK) {
1.2       itojun    209:                        /*
1.16      itojun    210:                         * proxy NDP for single entry
1.2       itojun    211:                         */
1.16      itojun    212:                        ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
                    213:                                IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
                    214:                        if (ifa) {
1.2       itojun    215:                                proxy = 1;
1.16      itojun    216:                                proxydl = SDL(rt->rt_gateway);
                    217:                        }
1.2       itojun    218:                }
1.16      itojun    219:                if (rt)
                    220:                        rtfree(rt);
1.2       itojun    221:        }
                    222:        if (!ifa) {
                    223:                /*
                    224:                 * We've got a NS packet, and we don't have that adddress
                    225:                 * assigned for us.  We MUST silently ignore it.
                    226:                 * See RFC2461 7.2.3.
                    227:                 */
1.16      itojun    228:                goto freeit;
1.2       itojun    229:        }
1.9       itojun    230:        myaddr6 = *IFA_IN6(ifa);
1.2       itojun    231:        anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
                    232:        tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
                    233:        if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
1.16      itojun    234:                goto freeit;
1.2       itojun    235:
                    236:        if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
                    237:                log(LOG_INFO,
                    238:                    "nd6_ns_input: lladdrlen mismatch for %s "
                    239:                    "(if %d, NS packet %d)\n",
                    240:                        ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
                    241:        }
                    242:
                    243:        if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
                    244:                log(LOG_INFO,
                    245:                    "nd6_ns_input: duplicate IP6 address %s\n",
                    246:                    ip6_sprintf(&saddr6));
1.16      itojun    247:                goto freeit;
1.2       itojun    248:        }
                    249:
                    250:        /*
                    251:         * We have neighbor solicitation packet, with target address equals to
                    252:         * one of my tentative address.
                    253:         *
                    254:         * src addr     how to process?
                    255:         * ---          ---
                    256:         * multicast    of course, invalid (rejected in ip6_input)
                    257:         * unicast      somebody is doing address resolution -> ignore
                    258:         * unspec       dup address detection
                    259:         *
                    260:         * The processing is defined in RFC 2462.
                    261:         */
                    262:        if (tentative) {
                    263:                /*
                    264:                 * If source address is unspecified address, it is for
                    265:                 * duplicated address detection.
                    266:                 *
                    267:                 * If not, the packet is for addess resolution;
                    268:                 * silently ignore it.
                    269:                 */
                    270:                if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
                    271:                        nd6_dad_ns_input(ifa);
                    272:
1.16      itojun    273:                goto freeit;
1.2       itojun    274:        }
                    275:
                    276:        /*
                    277:         * If the source address is unspecified address, entries must not
                    278:         * be created or updated.
                    279:         * It looks that sender is performing DAD.  Output NA toward
                    280:         * all-node multicast address, to tell the sender that I'm using
                    281:         * the address.
                    282:         * S bit ("solicited") must be zero.
                    283:         */
                    284:        if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
                    285:                saddr6 = in6addr_linklocal_allnodes;
                    286:                saddr6.s6_addr16[1] = htons(ifp->if_index);
                    287:                nd6_na_output(ifp, &saddr6, &taddr6,
                    288:                              ((anycast || proxy || !tlladdr)
                    289:                                      ? 0 : ND_NA_FLAG_OVERRIDE)
                    290:                                | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
1.16      itojun    291:                              tlladdr, (struct sockaddr *)proxydl);
                    292:                goto freeit;
1.2       itojun    293:        }
                    294:
1.7       itojun    295:        nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
1.2       itojun    296:
                    297:        nd6_na_output(ifp, &saddr6, &taddr6,
                    298:                      ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
                    299:                        | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
                    300:                        | ND_NA_FLAG_SOLICITED,
1.16      itojun    301:                      tlladdr, (struct sockaddr *)proxydl);
                    302:  freeit:
                    303:        m_freem(m);
1.2       itojun    304:        return;
                    305:
                    306:  bad:
                    307:        log(LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
                    308:        log(LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
                    309:        log(LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
1.16      itojun    310:        m_freem(m);
1.2       itojun    311: }
                    312:
                    313: /*
                    314:  * Output an Neighbor Solicitation Message. Caller specifies:
                    315:  *     - ICMP6 header source IP6 address
                    316:  *     - ND6 header target IP6 address
                    317:  *     - ND6 header source datalink address
                    318:  *
                    319:  * Based on RFC 2461
                    320:  * Based on RFC 2462 (duplicated address detection)
                    321:  */
                    322: void
                    323: nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
                    324:        struct ifnet *ifp;
                    325:        struct in6_addr *daddr6, *taddr6;
                    326:        struct llinfo_nd6 *ln;  /* for source address determination */
                    327:        int dad;        /* duplicated address detection */
                    328: {
                    329:        struct mbuf *m;
                    330:        struct ip6_hdr *ip6;
                    331:        struct nd_neighbor_solicit *nd_ns;
                    332:        struct in6_ifaddr *ia = NULL;
                    333:        struct ip6_moptions im6o;
                    334:        int icmp6len;
1.15      itojun    335:        int maxlen;
1.2       itojun    336:        caddr_t mac;
1.9       itojun    337:        struct ifnet *outif = NULL;
1.2       itojun    338:
                    339:        if (IN6_IS_ADDR_MULTICAST(taddr6))
                    340:                return;
                    341:
1.15      itojun    342:        /* estimate the size of message */
                    343:        maxlen = sizeof(*ip6) + sizeof(*nd_ns);
                    344:        maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
                    345:        if (max_linkhdr + maxlen >= MCLBYTES) {
                    346: #ifdef DIAGNOSTIC
                    347:                printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
                    348:                    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
                    349: #endif
                    350:                return;
                    351:        }
                    352:
                    353:        MGETHDR(m, M_DONTWAIT, MT_DATA);
                    354:        if (m && max_linkhdr + maxlen >= MHLEN) {
                    355:                MCLGET(m, M_DONTWAIT);
                    356:                if ((m->m_flags & M_EXT) == 0) {
                    357:                        m_free(m);
                    358:                        m = NULL;
                    359:                }
                    360:        }
                    361:        if (m == NULL)
1.2       itojun    362:                return;
                    363:
                    364:        if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
                    365:                m->m_flags |= M_MCAST;
                    366:                im6o.im6o_multicast_ifp = ifp;
                    367:                im6o.im6o_multicast_hlim = 255;
                    368:                im6o.im6o_multicast_loop = 0;
                    369:        }
                    370:
                    371:        icmp6len = sizeof(*nd_ns);
                    372:        m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
1.15      itojun    373:        m->m_data += max_linkhdr;       /*or MH_ALIGN() equivalent?*/
1.2       itojun    374:
                    375:        /* fill neighbor solicitation packet */
                    376:        ip6 = mtod(m, struct ip6_hdr *);
                    377:        ip6->ip6_flow = 0;
1.10      itojun    378:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    379:        ip6->ip6_vfc |= IPV6_VERSION;
1.2       itojun    380:        /* ip6->ip6_plen will be set later */
                    381:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    382:        ip6->ip6_hlim = 255;
                    383:        if (daddr6)
                    384:                ip6->ip6_dst = *daddr6;
                    385:        else {
                    386:                ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
                    387:                ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
                    388:                ip6->ip6_dst.s6_addr32[1] = 0;
                    389:                ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
                    390:                ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
                    391:                ip6->ip6_dst.s6_addr8[12] = 0xff;
                    392:        }
                    393:        if (!dad) {
                    394: #if 0  /* KAME way, exact address scope match */
                    395:                /*
                    396:                 * Select a source whose scope is the same as that of the dest.
                    397:                 * Typically, the dest is link-local solicitation multicast
                    398:                 * (i.e. neighbor discovery) or link-local/global unicast
                    399:                 * (i.e. neighbor un-reachability detection).
                    400:                 */
                    401:                ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
                    402:                if (ia == NULL) {
                    403:                        m_freem(m);
                    404:                        return;
                    405:                }
                    406:                ip6->ip6_src = ia->ia_addr.sin6_addr;
                    407: #else  /* spec-wise correct */
                    408:                /*
                    409:                 * RFC2461 7.2.2:
                    410:                 * "If the source address of the packet prompting the
                    411:                 * solicitation is the same as one of the addresses assigned
                    412:                 * to the outgoing interface, that address SHOULD be placed
                    413:                 * in the IP Source Address of the outgoing solicitation.
                    414:                 * Otherwise, any one of the addresses assigned to the
                    415:                 * interface should be used."
                    416:                 *
                    417:                 * We use the source address for the prompting packet
                    418:                 * (saddr6), if:
                    419:                 * - saddr6 is given from the caller (by giving "ln"), and
                    420:                 * - saddr6 belongs to the outgoing interface.
                    421:                 * Otherwise, we perform a scope-wise match.
                    422:                 */
                    423:                struct ip6_hdr *hip6;           /*hold ip6*/
                    424:                struct in6_addr *saddr6;
                    425:
                    426:                if (ln && ln->ln_hold) {
                    427:                        hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
                    428:                        /* XXX pullup? */
                    429:                        if (sizeof(*hip6) < ln->ln_hold->m_len)
                    430:                                saddr6 = &hip6->ip6_src;
                    431:                        else
                    432:                                saddr6 = NULL;
                    433:                } else
                    434:                        saddr6 = NULL;
                    435:                if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
                    436:                        bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
                    437:                else {
                    438:                        ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
                    439:                        if (ia == NULL) {
                    440:                                m_freem(m);     /*XXX*/
                    441:                                return;
                    442:                        }
                    443:                        ip6->ip6_src = ia->ia_addr.sin6_addr;
                    444:                }
                    445: #endif
                    446:        } else {
                    447:                /*
                    448:                 * Source address for DAD packet must always be IPv6
                    449:                 * unspecified address. (0::0)
                    450:                 */
                    451:                bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
                    452:        }
                    453:        nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
                    454:        nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
                    455:        nd_ns->nd_ns_code = 0;
                    456:        nd_ns->nd_ns_reserved = 0;
                    457:        nd_ns->nd_ns_target = *taddr6;
                    458:
                    459:        if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
                    460:                nd_ns->nd_ns_target.s6_addr16[1] = 0;
                    461:
                    462:        /*
                    463:         * Add source link-layer address option.
                    464:         *
                    465:         *                              spec            implementation
                    466:         *                              ---             ---
                    467:         * DAD packet                   MUST NOT        do not add the option
                    468:         * there's no link layer address:
                    469:         *                              impossible      do not add the option
                    470:         * there's link layer address:
                    471:         *      Multicast NS            MUST add one    add the option
                    472:         *      Unicast NS              SHOULD add one  add the option
                    473:         */
                    474:        if (!dad && (mac = nd6_ifptomac(ifp))) {
                    475:                int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
                    476:                struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
1.8       is        477:                /* 8 byte alignments... */
                    478:                optlen = (optlen + 7) & ~7;
1.2       itojun    479:
                    480:                m->m_pkthdr.len += optlen;
                    481:                m->m_len += optlen;
                    482:                icmp6len += optlen;
1.8       is        483:                bzero((caddr_t)nd_opt, optlen);
1.2       itojun    484:                nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
                    485:                nd_opt->nd_opt_len = optlen >> 3;
                    486:                bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
                    487:        }
                    488:
                    489:        ip6->ip6_plen = htons((u_short)icmp6len);
                    490:        nd_ns->nd_ns_cksum = 0;
                    491:        nd_ns->nd_ns_cksum
                    492:                = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
                    493:
                    494: #ifdef IPSEC
1.18      itojun    495:        /* Don't lookup socket */
1.22.4.1! he        496:        (void)ipsec_setsocket(m, NULL);
1.18      itojun    497: #endif
1.9       itojun    498:        ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
                    499:        if (outif) {
                    500:                icmp6_ifstat_inc(outif, ifs6_out_msg);
                    501:                icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
                    502:        }
1.2       itojun    503:        icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
                    504: }
                    505:
                    506: /*
                    507:  * Neighbor advertisement input handling.
                    508:  *
                    509:  * Based on RFC 2461
                    510:  * Based on RFC 2462 (duplicated address detection)
1.16      itojun    511:  *
                    512:  * the following items are not implemented yet:
                    513:  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
                    514:  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
1.2       itojun    515:  */
                    516: void
                    517: nd6_na_input(m, off, icmp6len)
                    518:        struct mbuf *m;
                    519:        int off, icmp6len;
                    520: {
                    521:        struct ifnet *ifp = m->m_pkthdr.rcvif;
                    522:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1.16      itojun    523:        struct nd_neighbor_advert *nd_na;
1.2       itojun    524: #if 0
                    525:        struct in6_addr saddr6 = ip6->ip6_src;
                    526: #endif
                    527:        struct in6_addr daddr6 = ip6->ip6_dst;
1.16      itojun    528:        struct in6_addr taddr6;
                    529:        int flags;
                    530:        int is_router;
                    531:        int is_solicited;
                    532:        int is_override;
1.2       itojun    533:        char *lladdr = NULL;
                    534:        int lladdrlen = 0;
                    535:        struct ifaddr *ifa;
                    536:        struct llinfo_nd6 *ln;
                    537:        struct rtentry *rt;
                    538:        struct sockaddr_dl *sdl;
                    539:        union nd_opts ndopts;
                    540:
                    541:        if (ip6->ip6_hlim != 255) {
                    542:                log(LOG_ERR,
                    543:                    "nd6_na_input: invalid hlim %d\n", ip6->ip6_hlim);
1.16      itojun    544:                goto freeit;
                    545:        }
                    546:
                    547: #ifndef PULLDOWN_TEST
                    548:        IP6_EXTHDR_CHECK(m, off, icmp6len,);
                    549:        nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
                    550: #else
                    551:        IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
                    552:        if (nd_na == NULL) {
                    553:                icmp6stat.icp6s_tooshort++;
1.2       itojun    554:                return;
                    555:        }
1.16      itojun    556: #endif
                    557:        taddr6 = nd_na->nd_na_target;
                    558:        flags = nd_na->nd_na_flags_reserved;
                    559:        is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
                    560:        is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
                    561:        is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
1.2       itojun    562:
                    563:        if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
                    564:                taddr6.s6_addr16[1] = htons(ifp->if_index);
                    565:
                    566:        if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
                    567:                log(LOG_ERR,
                    568:                    "nd6_na_input: invalid target address %s\n",
                    569:                    ip6_sprintf(&taddr6));
1.16      itojun    570:                goto freeit;
1.2       itojun    571:        }
                    572:        if (IN6_IS_ADDR_MULTICAST(&daddr6))
                    573:                if (is_solicited) {
                    574:                        log(LOG_ERR,
                    575:                            "nd6_na_input: a solicited adv is multicasted\n");
1.16      itojun    576:                        goto freeit;
1.2       itojun    577:                }
                    578:
                    579:        icmp6len -= sizeof(*nd_na);
                    580:        nd6_option_init(nd_na + 1, icmp6len, &ndopts);
                    581:        if (nd6_options(&ndopts) < 0) {
                    582:                log(LOG_INFO, "nd6_na_input: invalid ND option, ignored\n");
1.16      itojun    583:                goto freeit;
1.2       itojun    584:        }
                    585:
                    586:        if (ndopts.nd_opts_tgt_lladdr) {
                    587:                lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
                    588:                lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
                    589:        }
                    590:
                    591:        ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
                    592:
                    593:        /*
                    594:         * Target address matches one of my interface address.
                    595:         *
                    596:         * If my address is tentative, this means that there's somebody
                    597:         * already using the same address as mine.  This indicates DAD failure.
                    598:         * This is defined in RFC 2462.
                    599:         *
                    600:         * Otherwise, process as defined in RFC 2461.
                    601:         */
                    602:        if (ifa
                    603:         && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
                    604:                nd6_dad_na_input(ifa);
1.16      itojun    605:                goto freeit;
1.2       itojun    606:        }
                    607:
                    608:        /* Just for safety, maybe unnecessery. */
                    609:        if (ifa) {
                    610:                log(LOG_ERR,
                    611:                    "nd6_na_input: duplicate IP6 address %s\n",
                    612:                    ip6_sprintf(&taddr6));
1.16      itojun    613:                goto freeit;
1.2       itojun    614:        }
                    615:
                    616:        if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
                    617:                log(LOG_INFO,
                    618:                    "nd6_na_input: lladdrlen mismatch for %s "
                    619:                    "(if %d, NA packet %d)\n",
                    620:                        ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
                    621:        }
                    622:
                    623:        /*
                    624:         * If no neighbor cache entry is found, NA SHOULD silently be discarded.
                    625:         */
                    626:        rt = nd6_lookup(&taddr6, 0, ifp);
                    627:        if ((rt == NULL) ||
                    628:           ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
                    629:           ((sdl = SDL(rt->rt_gateway)) == NULL))
1.16      itojun    630:                goto freeit;
1.2       itojun    631:
                    632:        if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
                    633:                /*
                    634:                 * If the link-layer has address, and no lladdr option came,
                    635:                 * discard the packet.
                    636:                 */
                    637:                if (ifp->if_addrlen && !lladdr)
1.16      itojun    638:                        goto freeit;
1.2       itojun    639:
                    640:                /*
                    641:                 * Record link-layer address, and update the state.
                    642:                 */
                    643:                sdl->sdl_alen = ifp->if_addrlen;
                    644:                bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
                    645:                if (is_solicited) {
                    646:                        ln->ln_state = ND6_LLINFO_REACHABLE;
                    647:                        if (ln->ln_expire)
                    648:                                ln->ln_expire = time.tv_sec +
                    649:                                        nd_ifinfo[rt->rt_ifp->if_index].reachable;
                    650:                } else
                    651:                        ln->ln_state = ND6_LLINFO_STALE;
                    652:                ln->ln_router = is_router;
                    653:        } else {
                    654:                int llchange;
                    655:
                    656:                /*
                    657:                 * Check if the link-layer address has changed or not.
                    658:                 */
                    659:                if (!lladdr)
                    660:                        llchange = 0;
                    661:                else {
                    662:                        if (sdl->sdl_alen) {
                    663:                                if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
                    664:                                        llchange = 1;
                    665:                                else
                    666:                                        llchange = 0;
                    667:                        } else
                    668:                                llchange = 1;
                    669:                }
                    670:
                    671:                /*
                    672:                 * This is VERY complex.  Look at it with care.
                    673:                 *
                    674:                 * override solicit lladdr llchange     action
                    675:                 *                                      (L: record lladdr)
                    676:                 *
                    677:                 *      0       0       n       --      (2c)
                    678:                 *      0       0       y       n       (2b) L
                    679:                 *      0       0       y       y       (1)    REACHABLE->STALE
                    680:                 *      0       1       n       --      (2c)   *->REACHABLE
                    681:                 *      0       1       y       n       (2b) L *->REACHABLE
                    682:                 *      0       1       y       y       (1)    REACHABLE->STALE
                    683:                 *      1       0       n       --      (2a)
                    684:                 *      1       0       y       n       (2a) L
                    685:                 *      1       0       y       y       (2a) L *->STALE
                    686:                 *      1       1       n       --      (2a)   *->REACHABLE
                    687:                 *      1       1       y       n       (2a) L *->REACHABLE
                    688:                 *      1       1       y       y       (2a) L *->REACHABLE
                    689:                 */
                    690:                if (!is_override && (lladdr && llchange)) {        /* (1) */
                    691:                        /*
                    692:                         * If state is REACHABLE, make it STALE.
                    693:                         * no other updates should be done.
                    694:                         */
                    695:                        if (ln->ln_state == ND6_LLINFO_REACHABLE)
                    696:                                ln->ln_state = ND6_LLINFO_STALE;
1.16      itojun    697:                        goto freeit;
1.2       itojun    698:                } else if (is_override                             /* (2a) */
                    699:                        || (!is_override && (lladdr && !llchange)) /* (2b) */
                    700:                        || !lladdr) {                              /* (2c) */
                    701:                        /*
                    702:                         * Update link-local address, if any.
                    703:                         */
                    704:                        if (lladdr) {
                    705:                                sdl->sdl_alen = ifp->if_addrlen;
                    706:                                bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
                    707:                        }
                    708:
                    709:                        /*
                    710:                         * If solicited, make the state REACHABLE.
                    711:                         * If not solicited and the link-layer address was
                    712:                         * changed, make it STALE.
                    713:                         */
                    714:                        if (is_solicited) {
                    715:                                ln->ln_state = ND6_LLINFO_REACHABLE;
                    716:                                if (ln->ln_expire) {
                    717:                                        ln->ln_expire = time.tv_sec +
                    718:                                                nd_ifinfo[ifp->if_index].reachable;
                    719:                                }
                    720:                        } else {
                    721:                                if (lladdr && llchange)
                    722:                                        ln->ln_state = ND6_LLINFO_STALE;
                    723:                        }
                    724:                }
                    725:
                    726:                if (ln->ln_router && !is_router) {
                    727:                        /*
                    728:                         * The peer dropped the router flag.
                    729:                         * Remove the sender from the Default Router List and
                    730:                         * update the Destination Cache entries.
                    731:                         */
                    732:                        struct nd_defrouter *dr;
                    733:                        struct in6_addr *in6;
                    734:                        int s;
                    735:
                    736:                        in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
1.4       itojun    737:                        s = splsoftnet();
1.2       itojun    738:                        dr = defrouter_lookup(in6, rt->rt_ifp);
                    739:                        if (dr)
                    740:                                defrtrlist_del(dr);
                    741:                        else if (!ip6_forwarding && ip6_accept_rtadv) {
                    742:                                /*
                    743:                                 * Even if the neighbor is not in the default
                    744:                                 * router list, the neighbor may be used
                    745:                                 * as a next hop for some destinations
                    746:                                 * (e.g. redirect case). So we must
                    747:                                 * call rt6_flush explicitly.
                    748:                                 */
                    749:                                rt6_flush(&ip6->ip6_src, rt->rt_ifp);
                    750:                        }
                    751:                        splx(s);
                    752:                }
                    753:                ln->ln_router = is_router;
                    754:        }
                    755:        rt->rt_flags &= ~RTF_REJECT;
                    756:        ln->ln_asked = 0;
                    757:        if (ln->ln_hold) {
1.9       itojun    758: #ifdef OLDIP6OUTPUT
1.2       itojun    759:                (*ifp->if_output)(ifp, ln->ln_hold, rt_key(rt), rt);
1.9       itojun    760: #else
1.22      itojun    761:                /*
                    762:                 * we assume ifp is not a p2p here, so just set the 2nd
                    763:                 * argument as the 1st one.
                    764:                 */
                    765:                nd6_output(ifp, ifp, ln->ln_hold,
1.9       itojun    766:                           (struct sockaddr_in6 *)rt_key(rt), rt);
1.22      itojun    767: #endif
1.2       itojun    768:                ln->ln_hold = 0;
                    769:        }
1.16      itojun    770:
                    771:  freeit:
                    772:        m_freem(m);
1.2       itojun    773: }
                    774:
                    775: /*
                    776:  * Neighbor advertisement output handling.
                    777:  *
                    778:  * Based on RFC 2461
                    779:  *
1.16      itojun    780:  * the following items are not implemented yet:
                    781:  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
                    782:  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
1.2       itojun    783:  */
                    784: void
1.16      itojun    785: nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
1.2       itojun    786:        struct ifnet *ifp;
                    787:        struct in6_addr *daddr6, *taddr6;
                    788:        u_long flags;
1.16      itojun    789:        int tlladdr;            /* 1 if include target link-layer address */
                    790:        struct sockaddr *sdl0;  /* sockaddr_dl (= proxy NA) or NULL */
1.2       itojun    791: {
                    792:        struct mbuf *m;
                    793:        struct ip6_hdr *ip6;
                    794:        struct nd_neighbor_advert *nd_na;
                    795:        struct in6_ifaddr *ia = NULL;
                    796:        struct ip6_moptions im6o;
                    797:        int icmp6len;
1.15      itojun    798:        int maxlen;
1.2       itojun    799:        caddr_t mac;
1.9       itojun    800:        struct ifnet *outif = NULL;
1.15      itojun    801:
                    802:        /* estimate the size of message */
                    803:        maxlen = sizeof(*ip6) + sizeof(*nd_na);
                    804:        maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
                    805:        if (max_linkhdr + maxlen >= MCLBYTES) {
                    806: #ifdef DIAGNOSTIC
                    807:                printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
                    808:                    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
                    809: #endif
                    810:                return;
                    811:        }
                    812:
                    813:        MGETHDR(m, M_DONTWAIT, MT_DATA);
                    814:        if (m && max_linkhdr + maxlen >= MHLEN) {
                    815:                MCLGET(m, M_DONTWAIT);
                    816:                if ((m->m_flags & M_EXT) == 0) {
                    817:                        m_free(m);
                    818:                        m = NULL;
                    819:                }
                    820:        }
                    821:        if (m == NULL)
1.2       itojun    822:                return;
                    823:
                    824:        if (IN6_IS_ADDR_MULTICAST(daddr6)) {
                    825:                m->m_flags |= M_MCAST;
                    826:                im6o.im6o_multicast_ifp = ifp;
                    827:                im6o.im6o_multicast_hlim = 255;
                    828:                im6o.im6o_multicast_loop = 0;
                    829:        }
                    830:
                    831:        icmp6len = sizeof(*nd_na);
                    832:        m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
1.15      itojun    833:        m->m_data += max_linkhdr;       /*or MH_ALIGN() equivalent?*/
1.2       itojun    834:
                    835:        /* fill neighbor advertisement packet */
                    836:        ip6 = mtod(m, struct ip6_hdr *);
                    837:        ip6->ip6_flow = 0;
1.10      itojun    838:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    839:        ip6->ip6_vfc |= IPV6_VERSION;
1.2       itojun    840:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    841:        ip6->ip6_hlim = 255;
                    842:        if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
                    843:                /* reply to DAD */
                    844:                ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
                    845:                ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
                    846:                ip6->ip6_dst.s6_addr32[1] = 0;
                    847:                ip6->ip6_dst.s6_addr32[2] = 0;
                    848:                ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
                    849:                flags &= ~ND_NA_FLAG_SOLICITED;
                    850:        } else
                    851:                ip6->ip6_dst = *daddr6;
                    852:
                    853:        /*
                    854:         * Select a source whose scope is the same as that of the dest.
                    855:         */
                    856:        ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
                    857:        if (ia == NULL) {
                    858:                m_freem(m);
                    859:                return;
                    860:        }
                    861:        ip6->ip6_src = ia->ia_addr.sin6_addr;
                    862:        nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
                    863:        nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
                    864:        nd_na->nd_na_code = 0;
                    865:        nd_na->nd_na_target = *taddr6;
                    866:        if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
                    867:                nd_na->nd_na_target.s6_addr16[1] = 0;
                    868:
                    869:        /*
                    870:         * "tlladdr" indicates NS's condition for adding tlladdr or not.
                    871:         * see nd6_ns_input() for details.
                    872:         * Basically, if NS packet is sent to unicast/anycast addr,
                    873:         * target lladdr option SHOULD NOT be included.
                    874:         */
1.16      itojun    875:        if (tlladdr) {
                    876:                mac = NULL;
                    877:                /*
                    878:                 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
                    879:                 * lladdr in sdl0.  If we are not proxying (sending NA for
                    880:                 * my address) use lladdr configured for the interface.
                    881:                 */
                    882:                if (sdl0 == NULL)
                    883:                        mac = nd6_ifptomac(ifp);
                    884:                else if (sdl0->sa_family == AF_LINK) {
                    885:                        struct sockaddr_dl *sdl;
                    886:                        sdl = (struct sockaddr_dl *)sdl0;
                    887:                        if (sdl->sdl_alen == ifp->if_addrlen)
                    888:                                mac = LLADDR(sdl);
                    889:                }
                    890:        }
                    891:        if (tlladdr && mac) {
1.2       itojun    892:                int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
                    893:                struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
                    894:
1.8       is        895:                /* roundup to 8 bytes alignment! */
                    896:                optlen = (optlen + 7) & ~7;
                    897:
1.2       itojun    898:                m->m_pkthdr.len += optlen;
                    899:                m->m_len += optlen;
                    900:                icmp6len += optlen;
1.8       is        901:                bzero((caddr_t)nd_opt, optlen);
1.2       itojun    902:                nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
                    903:                nd_opt->nd_opt_len = optlen >> 3;
                    904:                bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
                    905:        } else
                    906:                flags &= ~ND_NA_FLAG_OVERRIDE;
                    907:
                    908:        ip6->ip6_plen = htons((u_short)icmp6len);
                    909:        nd_na->nd_na_flags_reserved = flags;
                    910:        nd_na->nd_na_cksum = 0;
                    911:        nd_na->nd_na_cksum =
                    912:                in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
                    913:
                    914: #ifdef IPSEC
1.18      itojun    915:        /* Don't lookup socket */
1.22.4.1! he        916:        (void)ipsec_setsocket(m, NULL);
1.18      itojun    917: #endif
1.9       itojun    918:        ip6_output(m, NULL, NULL, 0, &im6o, &outif);
                    919:        if (outif) {
                    920:                icmp6_ifstat_inc(outif, ifs6_out_msg);
                    921:                icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
                    922:        }
1.2       itojun    923:        icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
                    924: }
                    925:
                    926: caddr_t
                    927: nd6_ifptomac(ifp)
                    928:        struct ifnet *ifp;
                    929: {
                    930:        switch (ifp->if_type) {
1.8       is        931:        case IFT_ARCNET:
1.2       itojun    932:        case IFT_ETHER:
                    933:        case IFT_FDDI:
                    934:                return LLADDR(ifp->if_sadl);
                    935:                break;
                    936:        default:
                    937:                return NULL;
                    938:        }
                    939: }
                    940:
                    941: TAILQ_HEAD(dadq_head, dadq);
                    942: struct dadq {
                    943:        TAILQ_ENTRY(dadq) dad_list;
                    944:        struct ifaddr *dad_ifa;
                    945:        int dad_count;          /* max NS to send */
1.9       itojun    946:        int dad_ns_tcount;      /* # of trials to send NS */
1.2       itojun    947:        int dad_ns_ocount;      /* NS sent so far */
                    948:        int dad_ns_icount;
                    949:        int dad_na_icount;
1.21      itojun    950:        struct callout dad_timer_ch;
1.2       itojun    951: };
                    952:
                    953: static struct dadq_head dadq;
                    954:
                    955: static struct dadq *
                    956: nd6_dad_find(ifa)
                    957:        struct ifaddr *ifa;
                    958: {
                    959:        struct dadq *dp;
                    960:
                    961:        for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
                    962:                if (dp->dad_ifa == ifa)
                    963:                        return dp;
                    964:        }
                    965:        return NULL;
                    966: }
                    967:
                    968: /*
                    969:  * Start Duplicated Address Detection (DAD) for specified interface address.
                    970:  */
                    971: void
                    972: nd6_dad_start(ifa, tick)
                    973:        struct ifaddr *ifa;
                    974:        int *tick;      /* minimum delay ticks for IFF_UP event */
                    975: {
                    976:        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
                    977:        struct dadq *dp;
                    978:        static int dad_init = 0;
                    979:
                    980:        if (!dad_init) {
                    981:                TAILQ_INIT(&dadq);
                    982:                dad_init++;
                    983:        }
                    984:
                    985:        /*
                    986:         * If we don't need DAD, don't do it.
                    987:         * There are several cases:
                    988:         * - DAD is disabled (ip6_dad_count == 0)
                    989:         * - the interface address is anycast
                    990:         */
                    991:        if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1.15      itojun    992:                log(LOG_DEBUG,
                    993:                        "nd6_dad_start: called with non-tentative address "
1.2       itojun    994:                        "%s(%s)\n",
                    995:                        ip6_sprintf(&ia->ia_addr.sin6_addr),
1.9       itojun    996:                        ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1.2       itojun    997:                return;
                    998:        }
                    999:        if (ia->ia6_flags & IN6_IFF_ANYCAST) {
                   1000:                ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
                   1001:                return;
                   1002:        }
                   1003:        if (!ip6_dad_count) {
                   1004:                ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
                   1005:                return;
                   1006:        }
                   1007:        if (!ifa->ifa_ifp)
                   1008:                panic("nd6_dad_start: ifa->ifa_ifp == NULL");
                   1009:        if (!(ifa->ifa_ifp->if_flags & IFF_UP))
                   1010:                return;
                   1011:        if (nd6_dad_find(ifa) != NULL) {
                   1012:                /* DAD already in progress */
                   1013:                return;
                   1014:        }
                   1015:
                   1016:        dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
                   1017:        if (dp == NULL) {
1.15      itojun   1018:                log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1.2       itojun   1019:                        "%s(%s)\n",
                   1020:                        ip6_sprintf(&ia->ia_addr.sin6_addr),
1.9       itojun   1021:                        ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1.2       itojun   1022:                return;
                   1023:        }
                   1024:        bzero(dp, sizeof(*dp));
1.21      itojun   1025:        callout_init(&dp->dad_timer_ch);
1.2       itojun   1026:        TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
                   1027:
1.19      thorpej  1028: #ifdef ND6_DEBUG
1.15      itojun   1029:        log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1.6       thorpej  1030:            ip6_sprintf(&ia->ia_addr.sin6_addr));
1.19      thorpej  1031: #endif
1.2       itojun   1032:
                   1033:        /*
                   1034:         * Send NS packet for DAD, ip6_dad_count times.
                   1035:         * Note that we must delay the first transmission, if this is the
                   1036:         * first packet to be sent from the interface after interface
                   1037:         * (re)initialization.
                   1038:         */
                   1039:        dp->dad_ifa = ifa;
1.13      thorpej  1040:        IFAREF(ifa);            /* just for safety */
1.2       itojun   1041:        dp->dad_count = ip6_dad_count;
                   1042:        dp->dad_ns_icount = dp->dad_na_icount = 0;
1.9       itojun   1043:        dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1.2       itojun   1044:        if (!tick) {
1.9       itojun   1045:                nd6_dad_ns_output(dp, ifa);
1.21      itojun   1046:                callout_reset(&dp->dad_timer_ch,
1.20      thorpej  1047:                    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000,
                   1048:                    (void (*) __P((void *)))nd6_dad_timer, ifa);
1.2       itojun   1049:        } else {
                   1050:                int ntick;
                   1051:
                   1052:                if (*tick == 0)
                   1053:                        ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
                   1054:                else
                   1055:                        ntick = *tick + random() % (hz / 2);
                   1056:                *tick = ntick;
1.21      itojun   1057:                callout_reset(&dp->dad_timer_ch, ntick,
1.20      thorpej  1058:                    (void (*) __P((void *)))nd6_dad_timer, ifa);
1.2       itojun   1059:        }
                   1060: }
                   1061:
                   1062: static void
                   1063: nd6_dad_timer(ifa)
                   1064:        struct ifaddr *ifa;
                   1065: {
                   1066:        int s;
                   1067:        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
                   1068:        struct dadq *dp;
                   1069:
1.4       itojun   1070:        s = splsoftnet();       /*XXX*/
1.2       itojun   1071:
                   1072:        /* Sanity check */
                   1073:        if (ia == NULL) {
1.15      itojun   1074:                log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1.2       itojun   1075:                goto done;
                   1076:        }
                   1077:        dp = nd6_dad_find(ifa);
                   1078:        if (dp == NULL) {
1.15      itojun   1079:                log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1.2       itojun   1080:                goto done;
                   1081:        }
                   1082:        if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1.15      itojun   1083:                log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1.2       itojun   1084:                        "%s(%s)\n",
                   1085:                        ip6_sprintf(&ia->ia_addr.sin6_addr),
1.9       itojun   1086:                        ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1.2       itojun   1087:                goto done;
                   1088:        }
                   1089:        if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1.15      itojun   1090:                log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1.2       itojun   1091:                        "%s(%s)\n",
                   1092:                        ip6_sprintf(&ia->ia_addr.sin6_addr),
1.9       itojun   1093:                        ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
                   1094:                goto done;
                   1095:        }
                   1096:
                   1097:        /* timeouted with IFF_{RUNNING,UP} check */
                   1098:        if (dp->dad_ns_tcount > dad_maxtry) {
1.15      itojun   1099:                log(LOG_ERR, "%s: could not run DAD, driver problem?\n",
1.9       itojun   1100:                    if_name(ifa->ifa_ifp));
                   1101:
                   1102:                TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
                   1103:                free(dp, M_IP6NDP);
                   1104:                dp = NULL;
1.13      thorpej  1105:                IFAFREE(ifa);
1.2       itojun   1106:                goto done;
                   1107:        }
                   1108:
                   1109:        /* Need more checks? */
                   1110:        if (dp->dad_ns_ocount < dp->dad_count) {
                   1111:                /*
                   1112:                 * We have more NS to go.  Send NS packet for DAD.
                   1113:                 */
1.9       itojun   1114:                nd6_dad_ns_output(dp, ifa);
1.21      itojun   1115:                callout_reset(&dp->dad_timer_ch,
1.20      thorpej  1116:                    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000,
                   1117:                    (void (*) __P((void *)))nd6_dad_timer, ifa);
1.2       itojun   1118:        } else {
                   1119:                /*
                   1120:                 * We have transmitted sufficient number of DAD packets.
                   1121:                 * See what we've got.
                   1122:                 */
                   1123:                int duplicate;
                   1124:
                   1125:                duplicate = 0;
                   1126:
                   1127:                if (dp->dad_na_icount) {
                   1128:                        /*
                   1129:                         * the check is in nd6_dad_na_input(),
                   1130:                         * but just in case
                   1131:                         */
                   1132:                        duplicate++;
                   1133:                }
                   1134:
                   1135:                if (dp->dad_ns_icount) {
                   1136: #if 0 /*heuristics*/
                   1137:                        /*
                   1138:                         * if
                   1139:                         * - we have sent many(?) DAD NS, and
                   1140:                         * - the number of NS we sent equals to the
                   1141:                         *   number of NS we've got, and
                   1142:                         * - we've got no NA
                   1143:                         * we may have a faulty network card/driver which
                   1144:                         * loops back multicasts to myself.
                   1145:                         */
                   1146:                        if (3 < dp->dad_count
                   1147:                         && dp->dad_ns_icount == dp->dad_count
                   1148:                         && dp->dad_na_icount == 0) {
                   1149:                                log(LOG_INFO, "DAD questionable for %s(%s): "
                   1150:                                        "network card loops back multicast?\n",
                   1151:                                        ip6_sprintf(&ia->ia_addr.sin6_addr),
1.9       itojun   1152:                                        if_name(ifa->ifa_ifp));
1.2       itojun   1153:                                /* XXX consider it a duplicate or not? */
                   1154:                                /* duplicate++; */
                   1155:                        } else {
                   1156:                                /* We've seen NS, means DAD has failed. */
                   1157:                                duplicate++;
                   1158:                        }
                   1159: #else
                   1160:                        /* We've seen NS, means DAD has failed. */
                   1161:                        duplicate++;
                   1162: #endif
                   1163:                }
                   1164:
                   1165:                if (duplicate) {
                   1166:                        /* (*dp) will be freed in nd6_dad_duplicated() */
                   1167:                        dp = NULL;
                   1168:                        nd6_dad_duplicated(ifa);
                   1169:                } else {
                   1170:                        /*
                   1171:                         * We are done with DAD.  No NA came, no NS came.
                   1172:                         * duplicated address found.
                   1173:                         */
                   1174:                        ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
                   1175:
1.19      thorpej  1176: #ifdef ND6_DEBUG
                   1177:                        log(LOG_DEBUG,
1.15      itojun   1178:                            "%s: DAD complete for %s - no duplicates found\n",
                   1179:                            if_name(ifa->ifa_ifp),
1.6       thorpej  1180:                            ip6_sprintf(&ia->ia_addr.sin6_addr));
1.19      thorpej  1181: #endif
1.6       thorpej  1182:
1.2       itojun   1183:                        TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
                   1184:                        free(dp, M_IP6NDP);
                   1185:                        dp = NULL;
1.13      thorpej  1186:                        IFAFREE(ifa);
1.2       itojun   1187:                }
                   1188:        }
                   1189:
                   1190: done:
                   1191:        splx(s);
                   1192: }
                   1193:
                   1194: void
                   1195: nd6_dad_duplicated(ifa)
                   1196:        struct ifaddr *ifa;
                   1197: {
                   1198:        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
                   1199:        struct dadq *dp;
                   1200:
                   1201:        dp = nd6_dad_find(ifa);
                   1202:        if (dp == NULL) {
1.15      itojun   1203:                log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1.2       itojun   1204:                return;
                   1205:        }
                   1206:
1.6       thorpej  1207:        log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: %d NS, "
1.9       itojun   1208:            "%d NA\n", if_name(ifa->ifa_ifp),
1.6       thorpej  1209:            ip6_sprintf(&ia->ia_addr.sin6_addr),
                   1210:            dp->dad_ns_icount, dp->dad_na_icount);
1.2       itojun   1211:
                   1212:        ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
                   1213:        ia->ia6_flags |= IN6_IFF_DUPLICATED;
                   1214:
                   1215:        /* We are done with DAD, with duplicated address found. (failure) */
1.21      itojun   1216:        callout_stop(&dp->dad_timer_ch);
1.2       itojun   1217:
1.15      itojun   1218:        log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1.9       itojun   1219:            if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1.15      itojun   1220:        log(LOG_ERR, "%s: manual intervention required\n",
                   1221:            if_name(ifa->ifa_ifp));
1.6       thorpej  1222:
1.2       itojun   1223:        TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
                   1224:        free(dp, M_IP6NDP);
                   1225:        dp = NULL;
1.13      thorpej  1226:        IFAFREE(ifa);
1.2       itojun   1227: }
                   1228:
1.9       itojun   1229: static void
                   1230: nd6_dad_ns_output(dp, ifa)
                   1231:        struct dadq *dp;
                   1232:        struct ifaddr *ifa;
                   1233: {
                   1234:        struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
                   1235:        struct ifnet *ifp = ifa->ifa_ifp;
                   1236:
                   1237:        dp->dad_ns_tcount++;
                   1238:        if ((ifp->if_flags & IFF_UP) == 0) {
                   1239: #if 0
                   1240:                printf("%s: interface down?\n", if_name(ifp));
                   1241: #endif
                   1242:                return;
                   1243:        }
                   1244:        if ((ifp->if_flags & IFF_RUNNING) == 0) {
                   1245: #if 0
                   1246:                printf("%s: interface not running?\n", if_name(ifp));
                   1247: #endif
                   1248:                return;
                   1249:        }
                   1250:
                   1251:        dp->dad_ns_ocount++;
                   1252:        nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
                   1253: }
                   1254:
                   1255: static void
1.2       itojun   1256: nd6_dad_ns_input(ifa)
                   1257:        struct ifaddr *ifa;
                   1258: {
                   1259:        struct in6_ifaddr *ia;
                   1260:        struct ifnet *ifp;
                   1261:        struct in6_addr *taddr6;
                   1262:        struct dadq *dp;
                   1263:        int duplicate;
                   1264:
                   1265:        if (!ifa)
                   1266:                panic("ifa == NULL in nd6_dad_ns_input");
                   1267:
                   1268:        ia = (struct in6_ifaddr *)ifa;
                   1269:        ifp = ifa->ifa_ifp;
                   1270:        taddr6 = &ia->ia_addr.sin6_addr;
                   1271:        duplicate = 0;
                   1272:        dp = nd6_dad_find(ifa);
                   1273:
                   1274:        /*
                   1275:         * If it is from myself, ignore this.
                   1276:         */
                   1277:        if (ifp && (ifp->if_flags & IFF_LOOPBACK))
                   1278:                return;
                   1279:
                   1280:        /* Quickhack - completely ignore DAD NS packets */
                   1281:        if (dad_ignore_ns) {
                   1282:                log(LOG_INFO, "nd6_dad_ns_input: ignoring DAD NS packet for "
                   1283:                    "address %s(%s)\n", ip6_sprintf(taddr6),
1.9       itojun   1284:                    if_name(ifa->ifa_ifp));
1.2       itojun   1285:                return;
                   1286:        }
                   1287:
                   1288:        /*
                   1289:         * if I'm yet to start DAD, someone else started using this address
                   1290:         * first.  I have a duplicate and you win.
                   1291:         */
                   1292:        if (!dp || dp->dad_ns_ocount == 0)
                   1293:                duplicate++;
                   1294:
                   1295:        /* XXX more checks for loopback situation - see nd6_dad_timer too */
                   1296:
                   1297:        if (duplicate) {
                   1298:                dp = NULL;      /* will be freed in nd6_dad_duplicated() */
                   1299:                nd6_dad_duplicated(ifa);
                   1300:        } else {
                   1301:                /*
                   1302:                 * not sure if I got a duplicate.
                   1303:                 * increment ns count and see what happens.
                   1304:                 */
                   1305:                if (dp)
                   1306:                        dp->dad_ns_icount++;
                   1307:        }
                   1308: }
                   1309:
1.9       itojun   1310: static void
1.2       itojun   1311: nd6_dad_na_input(ifa)
                   1312:        struct ifaddr *ifa;
                   1313: {
                   1314:        struct dadq *dp;
                   1315:
                   1316:        if (!ifa)
                   1317:                panic("ifa == NULL in nd6_dad_na_input");
                   1318:
                   1319:        dp = nd6_dad_find(ifa);
                   1320:        if (dp)
                   1321:                dp->dad_na_icount++;
                   1322:
                   1323:        /* remove the address. */
                   1324:        nd6_dad_duplicated(ifa);
                   1325: }

CVSweb <webmaster@jp.NetBSD.org>