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