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