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