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