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