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