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