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