[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/nd6_nbr.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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: src/sys/netinet6/nd6_nbr.c,v 1.57 2008/12/02 21:37:28 bz Exp $");
 34 
 35 #include "opt_inet.h"
 36 #include "opt_inet6.h"
 37 #include "opt_ipsec.h"
 38 #include "opt_carp.h"
 39 #include "opt_mpath.h"
 40 
 41 #include <sys/param.h>
 42 #include <sys/systm.h>
 43 #include <sys/malloc.h>
 44 #include <sys/mbuf.h>
 45 #include <sys/socket.h>
 46 #include <sys/sockio.h>
 47 #include <sys/time.h>
 48 #include <sys/kernel.h>
 49 #include <sys/errno.h>
 50 #include <sys/syslog.h>
 51 #include <sys/queue.h>
 52 #include <sys/callout.h>
 53 #include <sys/vimage.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 <netinet6/in6_var.h>
 67 #include <netinet6/in6_ifattach.h>
 68 #include <netinet/ip6.h>
 69 #include <netinet6/ip6_var.h>
 70 #include <netinet6/scope6_var.h>
 71 #include <netinet6/nd6.h>
 72 #include <netinet/icmp6.h>
 73 #include <netinet6/vinet6.h>
 74 
 75 #ifdef DEV_CARP
 76 #include <netinet/ip_carp.h>
 77 #endif
 78 
 79 #define SDL(s) ((struct sockaddr_dl *)s)
 80 
 81 struct dadq;
 82 static struct dadq *nd6_dad_find(struct ifaddr *);
 83 static void nd6_dad_starttimer(struct dadq *, int);
 84 static void nd6_dad_stoptimer(struct dadq *);
 85 static void nd6_dad_timer(struct ifaddr *);
 86 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
 87 static void nd6_dad_ns_input(struct ifaddr *);
 88 static void nd6_dad_na_input(struct ifaddr *);
 89 
 90 #ifdef VIMAGE_GLOBALS
 91 int dad_ignore_ns;
 92 int dad_maxtry;
 93 #endif
 94 
 95 /*
 96  * Input a Neighbor Solicitation Message.
 97  *
 98  * Based on RFC 2461
 99  * Based on RFC 2462 (duplicate address detection)
100  */
101 void
102 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
103 {
104         INIT_VNET_INET6(curvnet);
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         union nd_opts ndopts;
118         struct sockaddr_dl *proxydl = NULL;
119         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
120 
121 #ifndef PULLDOWN_TEST
122         IP6_EXTHDR_CHECK(m, off, icmp6len,);
123         nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
124 #else
125         IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
126         if (nd_ns == NULL) {
127                 V_icmp6stat.icp6s_tooshort++;
128                 return;
129         }
130 #endif
131         ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
132         taddr6 = nd_ns->nd_ns_target;
133         if (in6_setscope(&taddr6, ifp, NULL) != 0)
134                 goto bad;
135 
136         if (ip6->ip6_hlim != 255) {
137                 nd6log((LOG_ERR,
138                     "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
139                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
140                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
141                 goto bad;
142         }
143 
144         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
145                 /* dst has to be a solicited node multicast address. */
146                 if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
147                     /* don't check ifindex portion */
148                     daddr6.s6_addr32[1] == 0 &&
149                     daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
150                     daddr6.s6_addr8[12] == 0xff) {
151                         ; /* good */
152                 } else {
153                         nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
154                             "(wrong ip6 dst)\n"));
155                         goto bad;
156                 }
157         } else if (!V_nd6_onlink_ns_rfc4861) {
158                 struct sockaddr_in6 src_sa6;
159 
160                 /*
161                  * According to recent IETF discussions, it is not a good idea
162                  * to accept a NS from an address which would not be deemed
163                  * to be a neighbor otherwise.  This point is expected to be
164                  * clarified in future revisions of the specification.
165                  */
166                 bzero(&src_sa6, sizeof(src_sa6));
167                 src_sa6.sin6_family = AF_INET6;
168                 src_sa6.sin6_len = sizeof(src_sa6);
169                 src_sa6.sin6_addr = saddr6;
170                 if (!nd6_is_addr_neighbor(&src_sa6, ifp)) {
171                         nd6log((LOG_INFO, "nd6_ns_input: "
172                                 "NS packet from non-neighbor\n"));
173                         goto bad;
174                 }
175         }
176 
177         if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
178                 nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
179                 goto bad;
180         }
181 
182         icmp6len -= sizeof(*nd_ns);
183         nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
184         if (nd6_options(&ndopts) < 0) {
185                 nd6log((LOG_INFO,
186                     "nd6_ns_input: invalid ND option, ignored\n"));
187                 /* nd6_options have incremented stats */
188                 goto freeit;
189         }
190 
191         if (ndopts.nd_opts_src_lladdr) {
192                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
193                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
194         }
195 
196         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
197                 nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
198                     "(link-layer address option)\n"));
199                 goto bad;
200         }
201 
202         /*
203          * Attaching target link-layer address to the NA?
204          * (RFC 2461 7.2.4)
205          *
206          * NS IP dst is unicast/anycast                 MUST NOT add
207          * NS IP dst is solicited-node multicast        MUST add
208          *
209          * In implementation, we add target link-layer address by default.
210          * We do not add one in MUST NOT cases.
211          */
212         if (!IN6_IS_ADDR_MULTICAST(&daddr6))
213                 tlladdr = 0;
214         else
215                 tlladdr = 1;
216 
217         /*
218          * Target address (taddr6) must be either:
219          * (1) Valid unicast/anycast address for my receiving interface,
220          * (2) Unicast address for which I'm offering proxy service, or
221          * (3) "tentative" address on which DAD is being performed.
222          */
223         /* (1) and (3) check. */
224 #ifdef DEV_CARP
225         if (ifp->if_carp)
226                 ifa = carp_iamatch6(ifp->if_carp, &taddr6);
227         if (ifa == NULL)
228                 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
229 #else
230         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
231 #endif
232 
233         /* (2) check. */
234         if (ifa == NULL) {
235                 struct rtentry *rt;
236                 struct sockaddr_in6 tsin6;
237                 int need_proxy;
238 #ifdef RADIX_MPATH
239                 struct route_in6 ro;
240 #endif
241 
242                 bzero(&tsin6, sizeof tsin6);
243                 tsin6.sin6_len = sizeof(struct sockaddr_in6);
244                 tsin6.sin6_family = AF_INET6;
245                 tsin6.sin6_addr = taddr6;
246 
247 #ifdef RADIX_MPATH
248                 bzero(&ro, sizeof(ro));
249                 ro.ro_dst = tsin6;
250                 rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
251                 rt = ro.ro_rt;
252 #else
253                 rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
254 #endif
255                 need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
256                     rt->rt_gateway->sa_family == AF_LINK);
257                 if (rt)
258                         rtfree(rt);
259                 if (need_proxy) {
260                         /*
261                          * proxy NDP for single entry
262                          */
263                         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
264                                 IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
265                         if (ifa) {
266                                 proxy = 1;
267                                 proxydl = SDL(rt->rt_gateway);
268                         }
269                 }
270         }
271         if (ifa == NULL) {
272                 /*
273                  * We've got an NS packet, and we don't have that adddress
274                  * assigned for us.  We MUST silently ignore it.
275                  * See RFC2461 7.2.3.
276                  */
277                 goto freeit;
278         }
279         myaddr6 = *IFA_IN6(ifa);
280         anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
281         tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
282         if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
283                 goto freeit;
284 
285         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
286                 nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
287                     "(if %d, NS packet %d)\n",
288                     ip6_sprintf(ip6bufs, &taddr6),
289                     ifp->if_addrlen, lladdrlen - 2));
290                 goto bad;
291         }
292 
293         if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
294                 nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
295                     ip6_sprintf(ip6bufs, &saddr6)));
296                 goto freeit;
297         }
298 
299         /*
300          * We have neighbor solicitation packet, with target address equals to
301          * one of my tentative address.
302          *
303          * src addr     how to process?
304          * ---          ---
305          * multicast    of course, invalid (rejected in ip6_input)
306          * unicast      somebody is doing address resolution -> ignore
307          * unspec       dup address detection
308          *
309          * The processing is defined in RFC 2462.
310          */
311         if (tentative) {
312                 /*
313                  * If source address is unspecified address, it is for
314                  * duplicate address detection.
315                  *
316                  * If not, the packet is for addess resolution;
317                  * silently ignore it.
318                  */
319                 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
320                         nd6_dad_ns_input(ifa);
321 
322                 goto freeit;
323         }
324 
325         /*
326          * If the source address is unspecified address, entries must not
327          * be created or updated.
328          * It looks that sender is performing DAD.  Output NA toward
329          * all-node multicast address, to tell the sender that I'm using
330          * the address.
331          * S bit ("solicited") must be zero.
332          */
333         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
334                 struct in6_addr in6_all;
335 
336                 in6_all = in6addr_linklocal_allnodes;
337                 if (in6_setscope(&in6_all, ifp, NULL) != 0)
338                         goto bad;
339                 nd6_na_output(ifp, &in6_all, &taddr6,
340                     ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
341                     (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
342                     tlladdr, (struct sockaddr *)proxydl);
343                 goto freeit;
344         }
345 
346         nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
347             ND_NEIGHBOR_SOLICIT, 0);
348 
349         nd6_na_output(ifp, &saddr6, &taddr6,
350             ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
351             (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
352             tlladdr, (struct sockaddr *)proxydl);
353  freeit:
354         m_freem(m);
355         return;
356 
357  bad:
358         nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
359                 ip6_sprintf(ip6bufs, &saddr6)));
360         nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
361                 ip6_sprintf(ip6bufs, &daddr6)));
362         nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
363                 ip6_sprintf(ip6bufs, &taddr6)));
364         V_icmp6stat.icp6s_badns++;
365         m_freem(m);
366 }
367 
368 /*
369  * Output a Neighbor Solicitation Message. Caller specifies:
370  *      - ICMP6 header source IP6 address
371  *      - ND6 header target IP6 address
372  *      - ND6 header source datalink address
373  *
374  * Based on RFC 2461
375  * Based on RFC 2462 (duplicate address detection)
376  *
377  *   ln - for source address determination
378  *  dad - duplicate address detection
379  */
380 void
381 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
382     const struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
383 {
384         INIT_VNET_INET6(ifp->if_vnet);
385         struct mbuf *m;
386         struct ip6_hdr *ip6;
387         struct nd_neighbor_solicit *nd_ns;
388         struct in6_addr *src, src_in;
389         struct ip6_moptions im6o;
390         int icmp6len;
391         int maxlen;
392         caddr_t mac;
393         struct route_in6 ro;
394 
395         bzero(&ro, sizeof(ro));
396 
397         if (IN6_IS_ADDR_MULTICAST(taddr6))
398                 return;
399 
400         /* estimate the size of message */
401         maxlen = sizeof(*ip6) + sizeof(*nd_ns);
402         maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
403         if (max_linkhdr + maxlen >= MCLBYTES) {
404 #ifdef DIAGNOSTIC
405                 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
406                     "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
407 #endif
408                 return;
409         }
410 
411         MGETHDR(m, M_DONTWAIT, MT_DATA);
412         if (m && max_linkhdr + maxlen >= MHLEN) {
413                 MCLGET(m, M_DONTWAIT);
414                 if ((m->m_flags & M_EXT) == 0) {
415                         m_free(m);
416                         m = NULL;
417                 }
418         }
419         if (m == NULL)
420                 return;
421         m->m_pkthdr.rcvif = NULL;
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                 /*
456                  * RFC2461 7.2.2:
457                  * "If the source address of the packet prompting the
458                  * solicitation is the same as one of the addresses assigned
459                  * to the outgoing interface, that address SHOULD be placed
460                  * in the IP Source Address of the outgoing solicitation.
461                  * Otherwise, any one of the addresses assigned to the
462                  * interface should be used."
463                  *
464                  * We use the source address for the prompting packet
465                  * (saddr6), if:
466                  * - saddr6 is given from the caller (by giving "ln"), and
467                  * - saddr6 belongs to the outgoing interface.
468                  * Otherwise, we perform the source address selection as usual.
469                  */
470                 struct ip6_hdr *hip6;           /* hold ip6 */
471                 struct in6_addr *hsrc = NULL;
472 
473                 if (ln && ln->ln_hold) {
474                         /*
475                          * assuming every packet in ln_hold has the same IP
476                          * header
477                          */
478                         hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
479                         /* XXX pullup? */
480                         if (sizeof(*hip6) < ln->ln_hold->m_len)
481                                 hsrc = &hip6->ip6_src;
482                         else
483                                 hsrc = NULL;
484                 }
485                 if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
486                         src = hsrc;
487                 else {
488                         int error;
489                         struct sockaddr_in6 dst_sa;
490 
491                         bzero(&dst_sa, sizeof(dst_sa));
492                         dst_sa.sin6_family = AF_INET6;
493                         dst_sa.sin6_len = sizeof(dst_sa);
494                         dst_sa.sin6_addr = ip6->ip6_dst;
495 
496                         src = in6_selectsrc(&dst_sa, NULL,
497                             NULL, &ro, NULL, NULL, &error);
498                         if (src == NULL) {
499                                 char ip6buf[INET6_ADDRSTRLEN];
500                                 nd6log((LOG_DEBUG,
501                                     "nd6_ns_output: source can't be "
502                                     "determined: dst=%s, error=%d\n",
503                                     ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
504                                     error));
505                                 goto bad;
506                         }
507                 }
508         } else {
509                 /*
510                  * Source address for DAD packet must always be IPv6
511                  * unspecified address. (0::0)
512                  * We actually don't have to 0-clear the address (we did it
513                  * above), but we do so here explicitly to make the intention
514                  * clearer.
515                  */
516                 bzero(&src_in, sizeof(src_in));
517                 src = &src_in;
518         }
519         ip6->ip6_src = *src;
520         nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
521         nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
522         nd_ns->nd_ns_code = 0;
523         nd_ns->nd_ns_reserved = 0;
524         nd_ns->nd_ns_target = *taddr6;
525         in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
526 
527         /*
528          * Add source link-layer address option.
529          *
530          *                              spec            implementation
531          *                              ---             ---
532          * DAD packet                   MUST NOT        do not add the option
533          * there's no link layer address:
534          *                              impossible      do not add the option
535          * there's link layer address:
536          *      Multicast NS            MUST add one    add the option
537          *      Unicast NS              SHOULD add one  add the option
538          */
539         if (!dad && (mac = nd6_ifptomac(ifp))) {
540                 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
541                 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
542                 /* 8 byte alignments... */
543                 optlen = (optlen + 7) & ~7;
544 
545                 m->m_pkthdr.len += optlen;
546                 m->m_len += optlen;
547                 icmp6len += optlen;
548                 bzero((caddr_t)nd_opt, optlen);
549                 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
550                 nd_opt->nd_opt_len = optlen >> 3;
551                 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
552         }
553 
554         ip6->ip6_plen = htons((u_short)icmp6len);
555         nd_ns->nd_ns_cksum = 0;
556         nd_ns->nd_ns_cksum =
557             in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
558 
559         ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
560         icmp6_ifstat_inc(ifp, ifs6_out_msg);
561         icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
562         V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
563 
564         if (ro.ro_rt) {         /* we don't cache this route. */
565                 RTFREE(ro.ro_rt);
566         }
567         return;
568 
569   bad:
570         if (ro.ro_rt) {
571                 RTFREE(ro.ro_rt);
572         }
573         m_freem(m);
574         return;
575 }
576 
577 /*
578  * Neighbor advertisement input handling.
579  *
580  * Based on RFC 2461
581  * Based on RFC 2462 (duplicate address detection)
582  *
583  * the following items are not implemented yet:
584  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
585  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
586  */
587 void
588 nd6_na_input(struct mbuf *m, int off, int icmp6len)
589 {
590         INIT_VNET_INET6(curvnet);
591         struct ifnet *ifp = m->m_pkthdr.rcvif;
592         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
593         struct nd_neighbor_advert *nd_na;
594         struct in6_addr daddr6 = ip6->ip6_dst;
595         struct in6_addr taddr6;
596         int flags;
597         int is_router;
598         int is_solicited;
599         int is_override;
600         char *lladdr = NULL;
601         int lladdrlen = 0;
602         struct ifaddr *ifa;
603         struct llinfo_nd6 *ln;
604         struct rtentry *rt;
605         struct sockaddr_dl *sdl;
606         union nd_opts ndopts;
607         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
608 
609         if (ip6->ip6_hlim != 255) {
610                 nd6log((LOG_ERR,
611                     "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
612                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
613                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
614                 goto bad;
615         }
616 
617 #ifndef PULLDOWN_TEST
618         IP6_EXTHDR_CHECK(m, off, icmp6len,);
619         nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
620 #else
621         IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
622         if (nd_na == NULL) {
623                 V_icmp6stat.icp6s_tooshort++;
624                 return;
625         }
626 #endif
627 
628         flags = nd_na->nd_na_flags_reserved;
629         is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
630         is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
631         is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
632 
633         taddr6 = nd_na->nd_na_target;
634         if (in6_setscope(&taddr6, ifp, NULL))
635                 goto bad;       /* XXX: impossible */
636 
637         if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
638                 nd6log((LOG_ERR,
639                     "nd6_na_input: invalid target address %s\n",
640                     ip6_sprintf(ip6bufs, &taddr6)));
641                 goto bad;
642         }
643         if (IN6_IS_ADDR_MULTICAST(&daddr6))
644                 if (is_solicited) {
645                         nd6log((LOG_ERR,
646                             "nd6_na_input: a solicited adv is multicasted\n"));
647                         goto bad;
648                 }
649 
650         icmp6len -= sizeof(*nd_na);
651         nd6_option_init(nd_na + 1, icmp6len, &ndopts);
652         if (nd6_options(&ndopts) < 0) {
653                 nd6log((LOG_INFO,
654                     "nd6_na_input: invalid ND option, ignored\n"));
655                 /* nd6_options have incremented stats */
656                 goto freeit;
657         }
658 
659         if (ndopts.nd_opts_tgt_lladdr) {
660                 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
661                 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
662         }
663 
664         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
665 
666         /*
667          * Target address matches one of my interface address.
668          *
669          * If my address is tentative, this means that there's somebody
670          * already using the same address as mine.  This indicates DAD failure.
671          * This is defined in RFC 2462.
672          *
673          * Otherwise, process as defined in RFC 2461.
674          */
675         if (ifa
676          && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
677                 nd6_dad_na_input(ifa);
678                 goto freeit;
679         }
680 
681         /* Just for safety, maybe unnecessary. */
682         if (ifa) {
683                 log(LOG_ERR,
684                     "nd6_na_input: duplicate IP6 address %s\n",
685                     ip6_sprintf(ip6bufs, &taddr6));
686                 goto freeit;
687         }
688 
689         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
690                 nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
691                     "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
692                     ifp->if_addrlen, lladdrlen - 2));
693                 goto bad;
694         }
695 
696         /*
697          * If no neighbor cache entry is found, NA SHOULD silently be
698          * discarded.
699          */
700         rt = nd6_lookup(&taddr6, 0, ifp);
701         if ((rt == NULL) ||
702            ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
703            ((sdl = SDL(rt->rt_gateway)) == NULL))
704                 goto freeit;
705 
706         if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
707                 /*
708                  * If the link-layer has address, and no lladdr option came,
709                  * discard the packet.
710                  */
711                 if (ifp->if_addrlen && lladdr == NULL)
712                         goto freeit;
713 
714                 /*
715                  * Record link-layer address, and update the state.
716                  */
717                 sdl->sdl_alen = ifp->if_addrlen;
718                 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
719                 if (is_solicited) {
720                         ln->ln_state = ND6_LLINFO_REACHABLE;
721                         ln->ln_byhint = 0;
722                         if (!ND6_LLINFO_PERMANENT(ln)) {
723                                 nd6_llinfo_settimer(ln,
724                                     (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
725                         }
726                 } else {
727                         ln->ln_state = ND6_LLINFO_STALE;
728                         nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
729                 }
730                 if ((ln->ln_router = is_router) != 0) {
731                         /*
732                          * This means a router's state has changed from
733                          * non-reachable to probably reachable, and might
734                          * affect the status of associated prefixes..
735                          */
736                         pfxlist_onlink_check();
737                 }
738         } else {
739                 int llchange;
740 
741                 /*
742                  * Check if the link-layer address has changed or not.
743                  */
744                 if (lladdr == NULL)
745                         llchange = 0;
746                 else {
747                         if (sdl->sdl_alen) {
748                                 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
749                                         llchange = 1;
750                                 else
751                                         llchange = 0;
752                         } else
753                                 llchange = 1;
754                 }
755 
756                 /*
757                  * This is VERY complex.  Look at it with care.
758                  *
759                  * override solicit lladdr llchange     action
760                  *                                      (L: record lladdr)
761                  *
762                  *      0       0       n       --      (2c)
763                  *      0       0       y       n       (2b) L
764                  *      0       0       y       y       (1)    REACHABLE->STALE
765                  *      0       1       n       --      (2c)   *->REACHABLE
766                  *      0       1       y       n       (2b) L *->REACHABLE
767                  *      0       1       y       y       (1)    REACHABLE->STALE
768                  *      1       0       n       --      (2a)
769                  *      1       0       y       n       (2a) L
770                  *      1       0       y       y       (2a) L *->STALE
771                  *      1       1       n       --      (2a)   *->REACHABLE
772                  *      1       1       y       n       (2a) L *->REACHABLE
773                  *      1       1       y       y       (2a) L *->REACHABLE
774                  */
775                 if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
776                         /*
777                          * If state is REACHABLE, make it STALE.
778                          * no other updates should be done.
779                          */
780                         if (ln->ln_state == ND6_LLINFO_REACHABLE) {
781                                 ln->ln_state = ND6_LLINFO_STALE;
782                                 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
783                         }
784                         goto freeit;
785                 } else if (is_override                             /* (2a) */
786                         || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
787                         || lladdr == NULL) {                       /* (2c) */
788                         /*
789                          * Update link-local address, if any.
790                          */
791                         if (lladdr != NULL) {
792                                 sdl->sdl_alen = ifp->if_addrlen;
793                                 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
794                         }
795 
796                         /*
797                          * If solicited, make the state REACHABLE.
798                          * If not solicited and the link-layer address was
799                          * changed, make it STALE.
800                          */
801                         if (is_solicited) {
802                                 ln->ln_state = ND6_LLINFO_REACHABLE;
803                                 ln->ln_byhint = 0;
804                                 if (!ND6_LLINFO_PERMANENT(ln)) {
805                                         nd6_llinfo_settimer(ln,
806                                             (long)ND_IFINFO(ifp)->reachable * hz);
807                                 }
808                         } else {
809                                 if (lladdr != NULL && llchange) {
810                                         ln->ln_state = ND6_LLINFO_STALE;
811                                         nd6_llinfo_settimer(ln,
812                                             (long)V_nd6_gctimer * hz);
813                                 }
814                         }
815                 }
816 
817                 if (ln->ln_router && !is_router) {
818                         /*
819                          * The peer dropped the router flag.
820                          * Remove the sender from the Default Router List and
821                          * update the Destination Cache entries.
822                          */
823                         struct nd_defrouter *dr;
824                         struct in6_addr *in6;
825                         int s;
826 
827                         in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
828 
829                         /*
830                          * Lock to protect the default router list.
831                          * XXX: this might be unnecessary, since this function
832                          * is only called under the network software interrupt
833                          * context.  However, we keep it just for safety.
834                          */
835                         s = splnet();
836                         dr = defrouter_lookup(in6, ifp);
837                         if (dr)
838                                 defrtrlist_del(dr);
839                         else if (!V_ip6_forwarding) {
840                                 /*
841                                  * Even if the neighbor is not in the default
842                                  * router list, the neighbor may be used
843                                  * as a next hop for some destinations
844                                  * (e.g. redirect case). So we must
845                                  * call rt6_flush explicitly.
846                                  */
847                                 rt6_flush(&ip6->ip6_src, ifp);
848                         }
849                         splx(s);
850                 }
851                 ln->ln_router = is_router;
852         }
853         rt->rt_flags &= ~RTF_REJECT;
854         ln->ln_asked = 0;
855         if (ln->ln_hold) {
856                 struct mbuf *m_hold, *m_hold_next;
857 
858                 /*
859                  * reset the ln_hold in advance, to explicitly
860                  * prevent a ln_hold lookup in nd6_output()
861                  * (wouldn't happen, though...)
862                  */
863                 for (m_hold = ln->ln_hold;
864                     m_hold; m_hold = m_hold_next) {
865                         m_hold_next = m_hold->m_nextpkt;
866                         m_hold->m_nextpkt = NULL;
867                         /*
868                          * we assume ifp is not a loopback here, so just set
869                          * the 2nd argument as the 1st one.
870                          */
871                         nd6_output(ifp, ifp, m_hold,
872                             (struct sockaddr_in6 *)rt_key(rt), rt);
873                 }
874                 ln->ln_hold = NULL;
875         }
876 
877  freeit:
878         m_freem(m);
879         return;
880 
881  bad:
882         V_icmp6stat.icp6s_badna++;
883         m_freem(m);
884 }
885 
886 /*
887  * Neighbor advertisement output handling.
888  *
889  * Based on RFC 2461
890  *
891  * the following items are not implemented yet:
892  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
893  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
894  *
895  * tlladdr - 1 if include target link-layer address
896  * sdl0 - sockaddr_dl (= proxy NA) or NULL
897  */
898 void
899 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
900     const struct in6_addr *taddr6, u_long flags, int tlladdr,
901     struct sockaddr *sdl0)
902 {
903         INIT_VNET_INET6(ifp->if_vnet);
904         struct mbuf *m;
905         struct ip6_hdr *ip6;
906         struct nd_neighbor_advert *nd_na;
907         struct ip6_moptions im6o;
908         struct in6_addr *src, daddr6;
909         struct sockaddr_in6 dst_sa;
910         int icmp6len, maxlen, error;
911         caddr_t mac = NULL;
912         struct route_in6 ro;
913 
914         bzero(&ro, sizeof(ro));
915 
916         daddr6 = *daddr6_0;     /* make a local copy for modification */
917 
918         /* estimate the size of message */
919         maxlen = sizeof(*ip6) + sizeof(*nd_na);
920         maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
921         if (max_linkhdr + maxlen >= MCLBYTES) {
922 #ifdef DIAGNOSTIC
923                 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
924                     "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
925 #endif
926                 return;
927         }
928 
929         MGETHDR(m, M_DONTWAIT, MT_DATA);
930         if (m && max_linkhdr + maxlen >= MHLEN) {
931                 MCLGET(m, M_DONTWAIT);
932                 if ((m->m_flags & M_EXT) == 0) {
933                         m_free(m);
934                         m = NULL;
935                 }
936         }
937         if (m == NULL)
938                 return;
939         m->m_pkthdr.rcvif = NULL;
940 
941         if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
942                 m->m_flags |= M_MCAST;
943                 im6o.im6o_multicast_ifp = ifp;
944                 im6o.im6o_multicast_hlim = 255;
945                 im6o.im6o_multicast_loop = 0;
946         }
947 
948         icmp6len = sizeof(*nd_na);
949         m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
950         m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
951 
952         /* fill neighbor advertisement packet */
953         ip6 = mtod(m, struct ip6_hdr *);
954         ip6->ip6_flow = 0;
955         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
956         ip6->ip6_vfc |= IPV6_VERSION;
957         ip6->ip6_nxt = IPPROTO_ICMPV6;
958         ip6->ip6_hlim = 255;
959         if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
960                 /* reply to DAD */
961                 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
962                 daddr6.s6_addr16[1] = 0;
963                 daddr6.s6_addr32[1] = 0;
964                 daddr6.s6_addr32[2] = 0;
965                 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
966                 if (in6_setscope(&daddr6, ifp, NULL))
967                         goto bad;
968 
969                 flags &= ~ND_NA_FLAG_SOLICITED;
970         }
971         ip6->ip6_dst = daddr6;
972         bzero(&dst_sa, sizeof(struct sockaddr_in6));
973         dst_sa.sin6_family = AF_INET6;
974         dst_sa.sin6_len = sizeof(struct sockaddr_in6);
975         dst_sa.sin6_addr = daddr6;
976 
977         /*
978          * Select a source whose scope is the same as that of the dest.
979          */
980         bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
981         src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
982         if (src == NULL) {
983                 char ip6buf[INET6_ADDRSTRLEN];
984                 nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
985                     "determined: dst=%s, error=%d\n",
986                     ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
987                 goto bad;
988         }
989         ip6->ip6_src = *src;
990         nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
991         nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
992         nd_na->nd_na_code = 0;
993         nd_na->nd_na_target = *taddr6;
994         in6_clearscope(&nd_na->nd_na_target); /* XXX */
995 
996         /*
997          * "tlladdr" indicates NS's condition for adding tlladdr or not.
998          * see nd6_ns_input() for details.
999          * Basically, if NS packet is sent to unicast/anycast addr,