nd6.c revision 215418
118316Swollman/*- 218316Swollman * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 318316Swollman * All rights reserved. 418316Swollman * 518316Swollman * Redistribution and use in source and binary forms, with or without 618316Swollman * modification, are permitted provided that the following conditions 718316Swollman * are met: 818316Swollman * 1. Redistributions of source code must retain the above copyright 918316Swollman * notice, this list of conditions and the following disclaimer. 1018316Swollman * 2. Redistributions in binary form must reproduce the above copyright 1118316Swollman * notice, this list of conditions and the following disclaimer in the 1218316Swollman * documentation and/or other materials provided with the distribution. 1318316Swollman * 3. Neither the name of the project nor the names of its contributors 1418316Swollman * may be used to endorse or promote products derived from this software 1518316Swollman * without specific prior written permission. 1618316Swollman * 1718316Swollman * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1818316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1918316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2018316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2118316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2218316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2318316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2418316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2518316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2618316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2718316Swollman * SUCH DAMAGE. 2818316Swollman * 2918316Swollman * $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ 3018316Swollman */ 3118316Swollman 3218316Swollman#include <sys/cdefs.h> 3318316Swollman__FBSDID("$FreeBSD: head/sys/netinet6/nd6.c 215418 2010-11-17 09:25:08Z bz $"); 3418316Swollman 3518316Swollman#include "opt_inet.h" 3618316Swollman#include "opt_inet6.h" 3718316Swollman 3818316Swollman#include <sys/param.h> 3918316Swollman#include <sys/systm.h> 4018316Swollman#include <sys/callout.h> 4118316Swollman#include <sys/malloc.h> 4218316Swollman#include <sys/mbuf.h> 4318316Swollman#include <sys/socket.h> 4418316Swollman#include <sys/sockio.h> 4518316Swollman#include <sys/time.h> 4618316Swollman#include <sys/kernel.h> 4718316Swollman#include <sys/protosw.h> 4818316Swollman#include <sys/errno.h> 4918316Swollman#include <sys/syslog.h> 5018316Swollman#include <sys/lock.h> 5118316Swollman#include <sys/rwlock.h> 5218316Swollman#include <sys/queue.h> 5318316Swollman#include <sys/sysctl.h> 5418316Swollman 5518316Swollman#include <net/if.h> 5618316Swollman#include <net/if_arc.h> 5718316Swollman#include <net/if_dl.h> 5818316Swollman#include <net/if_types.h> 5918316Swollman#include <net/iso88025.h> 6018316Swollman#include <net/fddi.h> 6118316Swollman#include <net/route.h> 6218316Swollman#include <net/vnet.h> 6318316Swollman 6418316Swollman#include <netinet/in.h> 6518316Swollman#include <net/if_llatbl.h> 6618316Swollman#define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le)) 6718316Swollman#include <netinet/if_ether.h> 6818316Swollman#include <netinet6/in6_var.h> 6918316Swollman#include <netinet/ip6.h> 7018316Swollman#include <netinet6/ip6_var.h> 7118316Swollman#include <netinet6/scope6_var.h> 7218316Swollman#include <netinet6/nd6.h> 7318316Swollman#include <netinet6/in6_ifattach.h> 7418316Swollman#include <netinet/icmp6.h> 7518316Swollman#include <netinet6/send.h> 7618316Swollman 7718316Swollman#include <sys/limits.h> 7818316Swollman 7918316Swollman#include <security/mac/mac_framework.h> 8018316Swollman 8118316Swollman#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 8218316Swollman#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 8318316Swollman 8418316Swollman#define SIN6(s) ((struct sockaddr_in6 *)s) 8518316Swollman 8618316Swollman/* timer values */ 8718316SwollmanVNET_DEFINE(int, nd6_prune) = 1; /* walk list every 1 seconds */ 8818316SwollmanVNET_DEFINE(int, nd6_delay) = 5; /* delay first probe time 5 second */ 8918316SwollmanVNET_DEFINE(int, nd6_umaxtries) = 3; /* maximum unicast query */ 9018316SwollmanVNET_DEFINE(int, nd6_mmaxtries) = 3; /* maximum multicast query */ 9118316SwollmanVNET_DEFINE(int, nd6_useloopback) = 1; /* use loopback interface for 9218316Swollman * local traffic */ 9318316SwollmanVNET_DEFINE(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage 9418316Swollman * collection timer */ 9518316Swollman 9618316Swollman/* preventing too many loops in ND option parsing */ 9718316SwollmanSTATIC_VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */ 9818316Swollman 9918316SwollmanVNET_DEFINE(int, nd6_maxnudhint) = 0; /* max # of subsequent upper 10018316Swollman * layer hints */ 10118316SwollmanSTATIC_VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved 10218316Swollman * ND entries */ 10318316Swollman#define V_nd6_maxndopt VNET(nd6_maxndopt) 10418316Swollman#define V_nd6_maxqueuelen VNET(nd6_maxqueuelen) 10518316Swollman 10618316Swollman#ifdef ND6_DEBUG 10718316SwollmanVNET_DEFINE(int, nd6_debug) = 1; 10818316Swollman#else 10918316SwollmanVNET_DEFINE(int, nd6_debug) = 0; 11018316Swollman#endif 11118316Swollman 11218316Swollman/* for debugging? */ 11318316Swollman#if 0 11418316Swollmanstatic int nd6_inuse, nd6_allocated; 11518316Swollman#endif 11618316Swollman 11718316SwollmanVNET_DEFINE(struct nd_drhead, nd_defrouter); 11818316SwollmanVNET_DEFINE(struct nd_prhead, nd_prefix); 11918316Swollman 12018316SwollmanVNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL; 12118316Swollman#define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval) 12218316Swollman 12318316Swollmanstatic struct sockaddr_in6 all1_sa; 12418316Swollman 12518316Swollmanint (*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int); 12618316Swollman 12718316Swollmanstatic int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *, 12818316Swollman struct ifnet *)); 12918316Swollmanstatic void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 13018316Swollmanstatic void nd6_slowtimo(void *); 13118316Swollmanstatic int regen_tmpaddr(struct in6_ifaddr *); 13218316Swollmanstatic struct llentry *nd6_free(struct llentry *, int); 13318316Swollmanstatic void nd6_llinfo_timer(void *); 13418316Swollmanstatic void clear_llinfo_pqueue(struct llentry *); 13518316Swollman 13618316SwollmanSTATIC_VNET_DEFINE(struct callout, nd6_slowtimo_ch); 13718316Swollman#define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch) 13818316Swollman 13918316SwollmanVNET_DEFINE(struct callout, nd6_timer_ch); 14018316Swollman 14118316Swollmanvoid 14218316Swollmannd6_init(void) 14318316Swollman{ 14418316Swollman int i; 14518316Swollman 14618316Swollman LIST_INIT(&V_nd_prefix); 14718316Swollman 14818316Swollman all1_sa.sin6_family = AF_INET6; 14918316Swollman all1_sa.sin6_len = sizeof(struct sockaddr_in6); 15018316Swollman for (i = 0; i < sizeof(all1_sa.sin6_addr); i++) 15118316Swollman all1_sa.sin6_addr.s6_addr[i] = 0xff; 15218316Swollman 15318316Swollman /* initialization of the default router list */ 15418316Swollman TAILQ_INIT(&V_nd_defrouter); 15518316Swollman 15618316Swollman /* start timer */ 15718316Swollman callout_init(&V_nd6_slowtimo_ch, 0); 15818316Swollman callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 15918316Swollman nd6_slowtimo, curvnet); 16018316Swollman} 16118316Swollman 16218316Swollman#ifdef VIMAGE 16318316Swollmanvoid 16418316Swollmannd6_destroy() 16518316Swollman{ 16618316Swollman 16718316Swollman callout_drain(&V_nd6_slowtimo_ch); 16818316Swollman callout_drain(&V_nd6_timer_ch); 16918316Swollman} 17018316Swollman#endif 17118316Swollman 17218316Swollmanstruct nd_ifinfo * 17318316Swollmannd6_ifattach(struct ifnet *ifp) 17418316Swollman{ 17518316Swollman struct nd_ifinfo *nd; 17618316Swollman 17718316Swollman nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK); 17818316Swollman bzero(nd, sizeof(*nd)); 17918316Swollman 18018316Swollman nd->initialized = 1; 18118316Swollman 18218316Swollman nd->chlim = IPV6_DEFHLIM; 18318316Swollman nd->basereachable = REACHABLE_TIME; 18418316Swollman nd->reachable = ND_COMPUTE_RTIME(nd->basereachable); 18518316Swollman nd->retrans = RETRANS_TIMER; 18618316Swollman 18718316Swollman nd->flags = ND6_IFF_PERFORMNUD; 18818316Swollman 18918316Swollman /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL. */ 19018316Swollman if (V_ip6_auto_linklocal || (ifp->if_flags & IFF_LOOPBACK)) 19118316Swollman nd->flags |= ND6_IFF_AUTO_LINKLOCAL; 19218316Swollman 19318316Swollman /* A loopback interface does not need to accept RTADV. */ 19418316Swollman if (V_ip6_accept_rtadv && !(ifp->if_flags & IFF_LOOPBACK)) 19518316Swollman nd->flags |= ND6_IFF_ACCEPT_RTADV; 19618316Swollman 19718316Swollman /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */ 19818316Swollman nd6_setmtu0(ifp, nd); 19918316Swollman 20018316Swollman return nd; 20118316Swollman} 20218316Swollman 20318316Swollmanvoid 20418316Swollmannd6_ifdetach(struct nd_ifinfo *nd) 20518316Swollman{ 20618316Swollman 20718316Swollman free(nd, M_IP6NDP); 20818316Swollman} 20918316Swollman 21018316Swollman/* 21118316Swollman * Reset ND level link MTU. This function is called when the physical MTU 21218316Swollman * changes, which means we might have to adjust the ND level MTU. 21318316Swollman */ 21418316Swollmanvoid 21518316Swollmannd6_setmtu(struct ifnet *ifp) 21618316Swollman{ 21718316Swollman 21818316Swollman nd6_setmtu0(ifp, ND_IFINFO(ifp)); 21918316Swollman} 22018316Swollman 22118316Swollman/* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */ 22218316Swollmanvoid 22318316Swollmannd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi) 22418316Swollman{ 22518316Swollman u_int32_t omaxmtu; 22618316Swollman 22718316Swollman omaxmtu = ndi->maxmtu; 22818316Swollman 22918316Swollman switch (ifp->if_type) { 23018316Swollman case IFT_ARCNET: 23118316Swollman ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */ 23218316Swollman break; 23318316Swollman case IFT_FDDI: 23418316Swollman ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */ 23518316Swollman break; 23618316Swollman case IFT_ISO88025: 23718316Swollman ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu); 23818316Swollman break; 23918316Swollman default: 24018316Swollman ndi->maxmtu = ifp->if_mtu; 24118316Swollman break; 24218316Swollman } 24318316Swollman 24418316Swollman /* 24518316Swollman * Decreasing the interface MTU under IPV6 minimum MTU may cause 24618316Swollman * undesirable situation. We thus notify the operator of the change 24718316Swollman * explicitly. The check for omaxmtu is necessary to restrict the 24818316Swollman * log to the case of changing the MTU, not initializing it. 24918316Swollman */ 25018316Swollman if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) { 25118316Swollman log(LOG_NOTICE, "nd6_setmtu0: " 25218316Swollman "new link MTU on %s (%lu) is too small for IPv6\n", 25318316Swollman if_name(ifp), (unsigned long)ndi->maxmtu); 25418316Swollman } 25518316Swollman 25618316Swollman if (ndi->maxmtu > V_in6_maxmtu) 25718316Swollman in6_setmaxmtu(); /* check all interfaces just in case */ 25818316Swollman 25918316Swollman} 26018316Swollman 26118316Swollmanvoid 26218316Swollmannd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts) 26318316Swollman{ 26418316Swollman 26518316Swollman bzero(ndopts, sizeof(*ndopts)); 26618316Swollman ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; 26718316Swollman ndopts->nd_opts_last 26818316Swollman = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); 26918316Swollman 27018316Swollman if (icmp6len == 0) { 27118316Swollman ndopts->nd_opts_done = 1; 27218316Swollman ndopts->nd_opts_search = NULL; 27318316Swollman } 27418316Swollman} 27518316Swollman 27618316Swollman/* 27718316Swollman * Take one ND option. 27818316Swollman */ 27918316Swollmanstruct nd_opt_hdr * 28018316Swollmannd6_option(union nd_opts *ndopts) 28118316Swollman{ 28218316Swollman struct nd_opt_hdr *nd_opt; 28318316Swollman int olen; 28418316Swollman 28518316Swollman if (ndopts == NULL) 28618316Swollman panic("ndopts == NULL in nd6_option"); 28718316Swollman if (ndopts->nd_opts_last == NULL) 28818316Swollman panic("uninitialized ndopts in nd6_option"); 28918316Swollman if (ndopts->nd_opts_search == NULL) 29018316Swollman return NULL; 29118316Swollman if (ndopts->nd_opts_done) 29218316Swollman return NULL; 29318316Swollman 29418316Swollman nd_opt = ndopts->nd_opts_search; 29518316Swollman 29618316Swollman /* make sure nd_opt_len is inside the buffer */ 29718316Swollman if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) { 29818316Swollman bzero(ndopts, sizeof(*ndopts)); 29918316Swollman return NULL; 30018316Swollman } 30118316Swollman 30218316Swollman olen = nd_opt->nd_opt_len << 3; 30318316Swollman if (olen == 0) { 30418316Swollman /* 30518316Swollman * Message validation requires that all included 30618316Swollman * options have a length that is greater than zero. 30718316Swollman */ 30818316Swollman bzero(ndopts, sizeof(*ndopts)); 30918316Swollman return NULL; 31018316Swollman } 31118316Swollman 31218316Swollman ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen); 31318316Swollman if (ndopts->nd_opts_search > ndopts->nd_opts_last) { 31418316Swollman /* option overruns the end of buffer, invalid */ 31518316Swollman bzero(ndopts, sizeof(*ndopts)); 31618316Swollman return NULL; 31718316Swollman } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { 31818316Swollman /* reached the end of options chain */ 31918316Swollman ndopts->nd_opts_done = 1; 32018316Swollman ndopts->nd_opts_search = NULL; 32118316Swollman } 32218316Swollman return nd_opt; 32318316Swollman} 32418316Swollman 32518316Swollman/* 32618316Swollman * Parse multiple ND options. 32718316Swollman * This function is much easier to use, for ND routines that do not need 32818316Swollman * multiple options of the same type. 32918316Swollman */ 33018316Swollmanint 33118316Swollmannd6_options(union nd_opts *ndopts) 33218316Swollman{ 33318316Swollman struct nd_opt_hdr *nd_opt; 33418316Swollman int i = 0; 33518316Swollman 33618316Swollman if (ndopts == NULL) 33718316Swollman panic("ndopts == NULL in nd6_options"); 33818316Swollman if (ndopts->nd_opts_last == NULL) 33918316Swollman panic("uninitialized ndopts in nd6_options"); 34018316Swollman if (ndopts->nd_opts_search == NULL) 34118316Swollman return 0; 34218316Swollman 34318316Swollman while (1) { 34418316Swollman nd_opt = nd6_option(ndopts); 34518316Swollman if (nd_opt == NULL && ndopts->nd_opts_last == NULL) { 34618316Swollman /* 34718316Swollman * Message validation requires that all included 34818316Swollman * options have a length that is greater than zero. 34918316Swollman */ 35018316Swollman ICMP6STAT_INC(icp6s_nd_badopt); 35118316Swollman bzero(ndopts, sizeof(*ndopts)); 35218316Swollman return -1; 35318316Swollman } 35418316Swollman 35518316Swollman if (nd_opt == NULL) 35618316Swollman goto skip1; 35718316Swollman 35818316Swollman switch (nd_opt->nd_opt_type) { 35918316Swollman case ND_OPT_SOURCE_LINKADDR: 36018316Swollman case ND_OPT_TARGET_LINKADDR: 36118316Swollman case ND_OPT_MTU: 36218316Swollman case ND_OPT_REDIRECTED_HEADER: 36318316Swollman if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { 36418316Swollman nd6log((LOG_INFO, 36518316Swollman "duplicated ND6 option found (type=%d)\n", 36618316Swollman nd_opt->nd_opt_type)); 36718316Swollman /* XXX bark? */ 36818316Swollman } else { 36918316Swollman ndopts->nd_opt_array[nd_opt->nd_opt_type] 37018316Swollman = nd_opt; 37118316Swollman } 37218316Swollman break; 37318316Swollman case ND_OPT_PREFIX_INFORMATION: 37418316Swollman if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) { 37518316Swollman ndopts->nd_opt_array[nd_opt->nd_opt_type] 37618316Swollman = nd_opt; 37718316Swollman } 37818316Swollman ndopts->nd_opts_pi_end = 37918316Swollman (struct nd_opt_prefix_info *)nd_opt; 38018316Swollman break; 38118316Swollman default: 38218316Swollman /* 38318316Swollman * Unknown options must be silently ignored, 38418316Swollman * to accomodate future extension to the protocol. 38518316Swollman */ 38618316Swollman nd6log((LOG_DEBUG, 38718316Swollman "nd6_options: unsupported option %d - " 38818316Swollman "option ignored\n", nd_opt->nd_opt_type)); 38918316Swollman } 39018316Swollman 39118316Swollmanskip1: 39218316Swollman i++; 39318316Swollman if (i > V_nd6_maxndopt) { 39418316Swollman ICMP6STAT_INC(icp6s_nd_toomanyopt); 39518316Swollman nd6log((LOG_INFO, "too many loop in nd opt\n")); 39618316Swollman break; 39718316Swollman } 39818316Swollman 39918316Swollman if (ndopts->nd_opts_done) 40018316Swollman break; 40118316Swollman } 40218316Swollman 40318316Swollman return 0; 40418316Swollman} 40518316Swollman 40618316Swollman/* 40718316Swollman * ND6 timer routine to handle ND6 entries 40818316Swollman */ 40918316Swollmanvoid 41018316Swollmannd6_llinfo_settimer_locked(struct llentry *ln, long tick) 41118316Swollman{ 41218316Swollman int canceled; 41318316Swollman 41418316Swollman if (tick < 0) { 41518316Swollman ln->la_expire = 0; 41618316Swollman ln->ln_ntick = 0; 41718316Swollman canceled = callout_stop(&ln->ln_timer_ch); 41818316Swollman } else { 41918316Swollman ln->la_expire = time_second + tick / hz; 42018316Swollman LLE_ADDREF(ln); 42118316Swollman if (tick > INT_MAX) { 42218316Swollman ln->ln_ntick = tick - INT_MAX; 42318316Swollman canceled = callout_reset(&ln->ln_timer_ch, INT_MAX, 42418316Swollman nd6_llinfo_timer, ln); 42518316Swollman } else { 42618316Swollman ln->ln_ntick = 0; 42718316Swollman canceled = callout_reset(&ln->ln_timer_ch, tick, 42818316Swollman nd6_llinfo_timer, ln); 42918316Swollman } 43018316Swollman } 43118316Swollman if (canceled) 43218316Swollman LLE_REMREF(ln); 43318316Swollman} 43418316Swollman 43518316Swollmanvoid 43618316Swollmannd6_llinfo_settimer(struct llentry *ln, long tick) 43718316Swollman{ 43818316Swollman 43918316Swollman LLE_WLOCK(ln); 44018316Swollman nd6_llinfo_settimer_locked(ln, tick); 44118316Swollman LLE_WUNLOCK(ln); 44218316Swollman} 44318316Swollman 44418316Swollmanstatic void 44518316Swollmannd6_llinfo_timer(void *arg) 44618316Swollman{ 44718316Swollman struct llentry *ln; 44818316Swollman struct in6_addr *dst; 44918316Swollman struct ifnet *ifp; 45018316Swollman struct nd_ifinfo *ndi = NULL; 45118316Swollman 45218316Swollman KASSERT(arg != NULL, ("%s: arg NULL", __func__)); 45318316Swollman ln = (struct llentry *)arg; 45418316Swollman ifp = ln->lle_tbl->llt_ifp; 45518316Swollman 45618316Swollman CURVNET_SET(ifp->if_vnet); 45718316Swollman 45818316Swollman if (ln->ln_ntick > 0) { 45918316Swollman if (ln->ln_ntick > INT_MAX) { 46018316Swollman ln->ln_ntick -= INT_MAX; 46118316Swollman nd6_llinfo_settimer(ln, INT_MAX); 46218316Swollman } else { 46318316Swollman ln->ln_ntick = 0; 46418316Swollman nd6_llinfo_settimer(ln, ln->ln_ntick); 46518316Swollman } 46618316Swollman goto done; 46718316Swollman } 46818316Swollman 46918316Swollman ndi = ND_IFINFO(ifp); 47018316Swollman dst = &L3_ADDR_SIN6(ln)->sin6_addr; 47118316Swollman if (ln->la_flags & LLE_STATIC) { 47218316Swollman goto done; 47318316Swollman } 47418316Swollman 47518316Swollman if (ln->la_flags & LLE_DELETED) { 47618316Swollman (void)nd6_free(ln, 0); 47718316Swollman ln = NULL; 47818316Swollman goto done; 47918316Swollman } 48018316Swollman 48118316Swollman switch (ln->ln_state) { 48218316Swollman case ND6_LLINFO_INCOMPLETE: 48318316Swollman if (ln->la_asked < V_nd6_mmaxtries) { 48418316Swollman ln->la_asked++; 48518316Swollman nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 48618316Swollman nd6_ns_output(ifp, NULL, dst, ln, 0); 48718316Swollman } else { 48818316Swollman struct mbuf *m = ln->la_hold; 48918316Swollman if (m) { 49018316Swollman struct mbuf *m0; 49118316Swollman 49218316Swollman /* 49318316Swollman * assuming every packet in la_hold has the 49418316Swollman * same IP header 49518316Swollman */ 49618316Swollman m0 = m->m_nextpkt; 49718316Swollman m->m_nextpkt = NULL; 49818316Swollman icmp6_error2(m, ICMP6_DST_UNREACH, 49918316Swollman ICMP6_DST_UNREACH_ADDR, 0, ifp); 50018316Swollman 50118316Swollman ln->la_hold = m0; 50218316Swollman clear_llinfo_pqueue(ln); 50318316Swollman } 50418316Swollman (void)nd6_free(ln, 0); 50518316Swollman ln = NULL; 50618316Swollman } 50718316Swollman break; 50818316Swollman case ND6_LLINFO_REACHABLE: 50918316Swollman if (!ND6_LLINFO_PERMANENT(ln)) { 51018316Swollman ln->ln_state = ND6_LLINFO_STALE; 51118316Swollman nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz); 51218316Swollman } 51318316Swollman break; 51418316Swollman 51518316Swollman case ND6_LLINFO_STALE: 51618316Swollman /* Garbage Collection(RFC 2461 5.3) */ 51718316Swollman if (!ND6_LLINFO_PERMANENT(ln)) { 51818316Swollman (void)nd6_free(ln, 1); 51918316Swollman ln = NULL; 52018316Swollman } 52118316Swollman break; 52218316Swollman 52318316Swollman case ND6_LLINFO_DELAY: 52418316Swollman if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { 52518316Swollman /* We need NUD */ 52618316Swollman ln->la_asked = 1; 52718316Swollman ln->ln_state = ND6_LLINFO_PROBE; 52818316Swollman nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 52918316Swollman nd6_ns_output(ifp, dst, dst, ln, 0); 53018316Swollman } else { 53118316Swollman ln->ln_state = ND6_LLINFO_STALE; /* XXX */ 53218316Swollman nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz); 53318316Swollman } 53418316Swollman break; 53518316Swollman case ND6_LLINFO_PROBE: 53618316Swollman if (ln->la_asked < V_nd6_umaxtries) { 53718316Swollman ln->la_asked++; 53818316Swollman nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 53918316Swollman nd6_ns_output(ifp, dst, dst, ln, 0); 54018316Swollman } else { 54118316Swollman (void)nd6_free(ln, 0); 54218316Swollman ln = NULL; 54318316Swollman } 54418316Swollman break; 54518316Swollman } 54618316Swollmandone: 54718316Swollman if (ln != NULL) 54818316Swollman LLE_FREE(ln); 54918316Swollman CURVNET_RESTORE(); 55018316Swollman} 55118316Swollman 55218316Swollman 55318316Swollman/* 55418316Swollman * ND6 timer routine to expire default route list and prefix list 55518316Swollman */ 55618316Swollmanvoid 55718316Swollmannd6_timer(void *arg) 55818316Swollman{ 55918316Swollman CURVNET_SET((struct vnet *) arg); 56018316Swollman int s; 56118316Swollman struct nd_defrouter *dr; 56218316Swollman struct nd_prefix *pr; 56318316Swollman struct in6_ifaddr *ia6, *nia6; 56418316Swollman struct in6_addrlifetime *lt6; 56518316Swollman 56618316Swollman callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz, 56718316Swollman nd6_timer, curvnet); 56818316Swollman 56918316Swollman /* expire default router list */ 57018316Swollman s = splnet(); 57118316Swollman dr = TAILQ_FIRST(&V_nd_defrouter); 57218316Swollman while (dr) { 57318316Swollman if (dr->expire && dr->expire < time_second) { 57418316Swollman struct nd_defrouter *t; 57518316Swollman t = TAILQ_NEXT(dr, dr_entry); 57618316Swollman defrtrlist_del(dr); 57718316Swollman dr = t; 57818316Swollman } else { 57918316Swollman dr = TAILQ_NEXT(dr, dr_entry); 58018316Swollman } 58118316Swollman } 58218316Swollman 58318316Swollman /* 58418316Swollman * expire interface addresses. 58518316Swollman * in the past the loop was inside prefix expiry processing. 58618316Swollman * However, from a stricter speci-confrmance standpoint, we should 58718316Swollman * rather separate address lifetimes and prefix lifetimes. 58818316Swollman * 58918316Swollman * XXXRW: in6_ifaddrhead locking. 59018316Swollman */ 59118316Swollman addrloop: 59218316Swollman TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) { 59318316Swollman /* check address lifetime */ 59418316Swollman lt6 = &ia6->ia6_lifetime; 59518316Swollman if (IFA6_IS_INVALID(ia6)) { 59618316Swollman int regen = 0; 59718316Swollman 59818316Swollman /* 59918316Swollman * If the expiring address is temporary, try 60018316Swollman * regenerating a new one. This would be useful when 60118316Swollman * we suspended a laptop PC, then turned it on after a 60218316Swollman * period that could invalidate all temporary 60318316Swollman * addresses. Although we may have to restart the 60418316Swollman * loop (see below), it must be after purging the 60518316Swollman * address. Otherwise, we'd see an infinite loop of 60618316Swollman * regeneration. 60718316Swollman */ 60818316Swollman if (V_ip6_use_tempaddr && 60918316Swollman (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 61018316Swollman if (regen_tmpaddr(ia6) == 0) 61118316Swollman regen = 1; 61218316Swollman } 61318316Swollman 61418316Swollman in6_purgeaddr(&ia6->ia_ifa); 61518316Swollman 61618316Swollman if (regen) 61718316Swollman goto addrloop; /* XXX: see below */ 61818316Swollman } else if (IFA6_IS_DEPRECATED(ia6)) { 61918316Swollman int oldflags = ia6->ia6_flags; 62018316Swollman 62118316Swollman ia6->ia6_flags |= IN6_IFF_DEPRECATED; 62218316Swollman 62318316Swollman /* 62418316Swollman * If a temporary address has just become deprecated, 62518316Swollman * regenerate a new one if possible. 62618316Swollman */ 62718316Swollman if (V_ip6_use_tempaddr && 62818316Swollman (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 62918316Swollman (oldflags & IN6_IFF_DEPRECATED) == 0) { 63018316Swollman 63118316Swollman if (regen_tmpaddr(ia6) == 0) { 63218316Swollman /* 63318316Swollman * A new temporary address is 63418316Swollman * generated. 63518316Swollman * XXX: this means the address chain 63618316Swollman * has changed while we are still in 63718316Swollman * the loop. Although the change 63818316Swollman * would not cause disaster (because 63918316Swollman * it's not a deletion, but an 64018316Swollman * addition,) we'd rather restart the 64118316Swollman * loop just for safety. Or does this 64218316Swollman * significantly reduce performance?? 64318316Swollman */ 64418316Swollman goto addrloop; 64518316Swollman } 64618316Swollman } 64718316Swollman } else { 64818316Swollman /* 64918316Swollman * A new RA might have made a deprecated address 65018316Swollman * preferred. 65118316Swollman */ 65218316Swollman ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; 65318316Swollman } 65418316Swollman } 65518316Swollman 65618316Swollman /* expire prefix list */ 65718316Swollman pr = V_nd_prefix.lh_first; 65818316Swollman while (pr) { 65918316Swollman /* 66018316Swollman * check prefix lifetime. 66118316Swollman * since pltime is just for autoconf, pltime processing for 66218316Swollman * prefix is not necessary. 66318316Swollman */ 66418316Swollman if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME && 66518316Swollman time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) { 66618316Swollman struct nd_prefix *t; 66718316Swollman t = pr->ndpr_next; 66818316Swollman 66918316Swollman /* 67018316Swollman * address expiration and prefix expiration are 67118316Swollman * separate. NEVER perform in6_purgeaddr here. 67218316Swollman */ 67318316Swollman 67418316Swollman prelist_remove(pr); 67518316Swollman pr = t; 67618316Swollman } else 67718316Swollman pr = pr->ndpr_next; 67818316Swollman } 67918316Swollman splx(s); 68018316Swollman CURVNET_RESTORE(); 68118316Swollman} 68218316Swollman 68318316Swollman/* 68418316Swollman * ia6 - deprecated/invalidated temporary address 68518316Swollman */ 68618316Swollmanstatic int 68718316Swollmanregen_tmpaddr(struct in6_ifaddr *ia6) 68818316Swollman{ 68918316Swollman struct ifaddr *ifa; 69018316Swollman struct ifnet *ifp; 69118316Swollman struct in6_ifaddr *public_ifa6 = NULL; 69218316Swollman 69318316Swollman ifp = ia6->ia_ifa.ifa_ifp; 69418316Swollman IF_ADDR_LOCK(ifp); 69518316Swollman TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 69618316Swollman struct in6_ifaddr *it6; 69718316Swollman 69818316Swollman if (ifa->ifa_addr->sa_family != AF_INET6) 69918316Swollman continue; 70018316Swollman 70118316Swollman it6 = (struct in6_ifaddr *)ifa; 70218316Swollman 70318316Swollman /* ignore no autoconf addresses. */ 70418316Swollman if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 70518316Swollman continue; 70618316Swollman 70718316Swollman /* ignore autoconf addresses with different prefixes. */ 70818316Swollman if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 70918316Swollman continue; 71018316Swollman 71118316Swollman /* 71218316Swollman * Now we are looking at an autoconf address with the same 71318316Swollman * prefix as ours. If the address is temporary and is still 71418316Swollman * preferred, do not create another one. It would be rare, but 71518316Swollman * could happen, for example, when we resume a laptop PC after 71618316Swollman * a long period. 71718316Swollman */ 71818316Swollman if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 71918316Swollman !IFA6_IS_DEPRECATED(it6)) { 72018316Swollman public_ifa6 = NULL; 72118316Swollman break; 72218316Swollman } 72318316Swollman 72418316Swollman /* 72518316Swollman * This is a public autoconf address that has the same prefix 72618316Swollman * as ours. If it is preferred, keep it. We can't break the 72718316Swollman * loop here, because there may be a still-preferred temporary 72818316Swollman * address with the prefix. 72918316Swollman */ 73018316Swollman if (!IFA6_IS_DEPRECATED(it6)) 73118316Swollman public_ifa6 = it6; 73218316Swollman 73318316Swollman if (public_ifa6 != NULL) 73418316Swollman ifa_ref(&public_ifa6->ia_ifa); 73518316Swollman } 73618316Swollman IF_ADDR_UNLOCK(ifp); 73718316Swollman 73818316Swollman if (public_ifa6 != NULL) { 73918316Swollman int e; 74018316Swollman 74118316Swollman if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) { 742 ifa_free(&public_ifa6->ia_ifa); 743 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 744 " tmp addr,errno=%d\n", e); 745 return (-1); 746 } 747 ifa_free(&public_ifa6->ia_ifa); 748 return (0); 749 } 750 751 return (-1); 752} 753 754/* 755 * Nuke neighbor cache/prefix/default router management table, right before 756 * ifp goes away. 757 */ 758void 759nd6_purge(struct ifnet *ifp) 760{ 761 struct nd_defrouter *dr, *ndr; 762 struct nd_prefix *pr, *npr; 763 764 /* 765 * Nuke default router list entries toward ifp. 766 * We defer removal of default router list entries that is installed 767 * in the routing table, in order to keep additional side effects as 768 * small as possible. 769 */ 770 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) { 771 ndr = TAILQ_NEXT(dr, dr_entry); 772 if (dr->installed) 773 continue; 774 775 if (dr->ifp == ifp) 776 defrtrlist_del(dr); 777 } 778 779 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) { 780 ndr = TAILQ_NEXT(dr, dr_entry); 781 if (!dr->installed) 782 continue; 783 784 if (dr->ifp == ifp) 785 defrtrlist_del(dr); 786 } 787 788 /* Nuke prefix list entries toward ifp */ 789 for (pr = V_nd_prefix.lh_first; pr; pr = npr) { 790 npr = pr->ndpr_next; 791 if (pr->ndpr_ifp == ifp) { 792 /* 793 * Because if_detach() does *not* release prefixes 794 * while purging addresses the reference count will 795 * still be above zero. We therefore reset it to 796 * make sure that the prefix really gets purged. 797 */ 798 pr->ndpr_refcnt = 0; 799 800 /* 801 * Previously, pr->ndpr_addr is removed as well, 802 * but I strongly believe we don't have to do it. 803 * nd6_purge() is only called from in6_ifdetach(), 804 * which removes all the associated interface addresses 805 * by itself. 806 * (jinmei@kame.net 20010129) 807 */ 808 prelist_remove(pr); 809 } 810 } 811 812 /* cancel default outgoing interface setting */ 813 if (V_nd6_defifindex == ifp->if_index) 814 nd6_setdefaultiface(0); 815 816 if (!V_ip6_forwarding && ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) { 817 /* Refresh default router list. */ 818 defrouter_select(); 819 } 820 821 /* XXXXX 822 * We do not nuke the neighbor cache entries here any more 823 * because the neighbor cache is kept in if_afdata[AF_INET6]. 824 * nd6_purge() is invoked by in6_ifdetach() which is called 825 * from if_detach() where everything gets purged. So let 826 * in6_domifdetach() do the actual L2 table purging work. 827 */ 828} 829 830/* 831 * the caller acquires and releases the lock on the lltbls 832 * Returns the llentry locked 833 */ 834struct llentry * 835nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp) 836{ 837 struct sockaddr_in6 sin6; 838 struct llentry *ln; 839 int llflags = 0; 840 841 bzero(&sin6, sizeof(sin6)); 842 sin6.sin6_len = sizeof(struct sockaddr_in6); 843 sin6.sin6_family = AF_INET6; 844 sin6.sin6_addr = *addr6; 845 846 IF_AFDATA_LOCK_ASSERT(ifp); 847 848 if (flags & ND6_CREATE) 849 llflags |= LLE_CREATE; 850 if (flags & ND6_EXCLUSIVE) 851 llflags |= LLE_EXCLUSIVE; 852 853 ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6); 854 if ((ln != NULL) && (flags & LLE_CREATE)) 855 ln->ln_state = ND6_LLINFO_NOSTATE; 856 857 return (ln); 858} 859 860/* 861 * Test whether a given IPv6 address is a neighbor or not, ignoring 862 * the actual neighbor cache. The neighbor cache is ignored in order 863 * to not reenter the routing code from within itself. 864 */ 865static int 866nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp) 867{ 868 struct nd_prefix *pr; 869 struct ifaddr *dstaddr; 870 871 /* 872 * A link-local address is always a neighbor. 873 * XXX: a link does not necessarily specify a single interface. 874 */ 875 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 876 struct sockaddr_in6 sin6_copy; 877 u_int32_t zone; 878 879 /* 880 * We need sin6_copy since sa6_recoverscope() may modify the 881 * content (XXX). 882 */ 883 sin6_copy = *addr; 884 if (sa6_recoverscope(&sin6_copy)) 885 return (0); /* XXX: should be impossible */ 886 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 887 return (0); 888 if (sin6_copy.sin6_scope_id == zone) 889 return (1); 890 else 891 return (0); 892 } 893 894 /* 895 * If the address matches one of our addresses, 896 * it should be a neighbor. 897 * If the address matches one of our on-link prefixes, it should be a 898 * neighbor. 899 */ 900 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 901 if (pr->ndpr_ifp != ifp) 902 continue; 903 904 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) { 905 struct rtentry *rt; 906 rt = rtalloc1((struct sockaddr *)&pr->ndpr_prefix, 0, 0); 907 if (rt == NULL) 908 continue; 909 /* 910 * This is the case where multiple interfaces 911 * have the same prefix, but only one is installed 912 * into the routing table and that prefix entry 913 * is not the one being examined here. In the case 914 * where RADIX_MPATH is enabled, multiple route 915 * entries (of the same rt_key value) will be 916 * installed because the interface addresses all 917 * differ. 918 */ 919 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 920 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) { 921 RTFREE_LOCKED(rt); 922 continue; 923 } 924 RTFREE_LOCKED(rt); 925 } 926 927 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 928 &addr->sin6_addr, &pr->ndpr_mask)) 929 return (1); 930 } 931 932 /* 933 * If the address is assigned on the node of the other side of 934 * a p2p interface, the address should be a neighbor. 935 */ 936 dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr); 937 if (dstaddr != NULL) { 938 if (dstaddr->ifa_ifp == ifp) { 939 ifa_free(dstaddr); 940 return (1); 941 } 942 ifa_free(dstaddr); 943 } 944 945 /* 946 * If the default router list is empty, all addresses are regarded 947 * as on-link, and thus, as a neighbor. 948 * XXX: we restrict the condition to hosts, because routers usually do 949 * not have the "default router list". 950 */ 951 if (!V_ip6_forwarding && TAILQ_FIRST(&V_nd_defrouter) == NULL && 952 V_nd6_defifindex == ifp->if_index) { 953 return (1); 954 } 955 956 return (0); 957} 958 959 960/* 961 * Detect if a given IPv6 address identifies a neighbor on a given link. 962 * XXX: should take care of the destination of a p2p link? 963 */ 964int 965nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp) 966{ 967 struct llentry *lle; 968 int rc = 0; 969 970 IF_AFDATA_UNLOCK_ASSERT(ifp); 971 if (nd6_is_new_addr_neighbor(addr, ifp)) 972 return (1); 973 974 /* 975 * Even if the address matches none of our addresses, it might be 976 * in the neighbor cache. 977 */ 978 IF_AFDATA_LOCK(ifp); 979 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) { 980 LLE_RUNLOCK(lle); 981 rc = 1; 982 } 983 IF_AFDATA_UNLOCK(ifp); 984 return (rc); 985} 986 987/* 988 * Free an nd6 llinfo entry. 989 * Since the function would cause significant changes in the kernel, DO NOT 990 * make it global, unless you have a strong reason for the change, and are sure 991 * that the change is safe. 992 */ 993static struct llentry * 994nd6_free(struct llentry *ln, int gc) 995{ 996 struct llentry *next; 997 struct nd_defrouter *dr; 998 struct ifnet *ifp=NULL; 999 1000 /* 1001 * we used to have pfctlinput(PRC_HOSTDEAD) here. 1002 * even though it is not harmful, it was not really necessary. 1003 */ 1004 1005 /* cancel timer */ 1006 nd6_llinfo_settimer(ln, -1); 1007 1008 if (!V_ip6_forwarding) { 1009 int s; 1010 s = splnet(); 1011 dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp); 1012 1013 if (dr != NULL && dr->expire && 1014 ln->ln_state == ND6_LLINFO_STALE && gc) { 1015 /* 1016 * If the reason for the deletion is just garbage 1017 * collection, and the neighbor is an active default 1018 * router, do not delete it. Instead, reset the GC 1019 * timer using the router's lifetime. 1020 * Simply deleting the entry would affect default 1021 * router selection, which is not necessarily a good 1022 * thing, especially when we're using router preference 1023 * values. 1024 * XXX: the check for ln_state would be redundant, 1025 * but we intentionally keep it just in case. 1026 */ 1027 if (dr->expire > time_second) 1028 nd6_llinfo_settimer(ln, 1029 (dr->expire - time_second) * hz); 1030 else 1031 nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz); 1032 splx(s); 1033 LLE_WLOCK(ln); 1034 LLE_REMREF(ln); 1035 LLE_WUNLOCK(ln); 1036 return (LIST_NEXT(ln, lle_next)); 1037 } 1038 1039 if (ln->ln_router || dr) { 1040 /* 1041 * rt6_flush must be called whether or not the neighbor 1042 * is in the Default Router List. 1043 * See a corresponding comment in nd6_na_input(). 1044 */ 1045 rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp); 1046 } 1047 1048 if (dr) { 1049 /* 1050 * Unreachablity of a router might affect the default 1051 * router selection and on-link detection of advertised 1052 * prefixes. 1053 */ 1054 1055 /* 1056 * Temporarily fake the state to choose a new default 1057 * router and to perform on-link determination of 1058 * prefixes correctly. 1059 * Below the state will be set correctly, 1060 * or the entry itself will be deleted. 1061 */ 1062 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1063 1064 /* 1065 * Since defrouter_select() does not affect the 1066 * on-link determination and MIP6 needs the check 1067 * before the default router selection, we perform 1068 * the check now. 1069 */ 1070 pfxlist_onlink_check(); 1071 1072 /* 1073 * refresh default router list 1074 */ 1075 defrouter_select(); 1076 } 1077 splx(s); 1078 } 1079 1080 /* 1081 * Before deleting the entry, remember the next entry as the 1082 * return value. We need this because pfxlist_onlink_check() above 1083 * might have freed other entries (particularly the old next entry) as 1084 * a side effect (XXX). 1085 */ 1086 next = LIST_NEXT(ln, lle_next); 1087 1088 ifp = ln->lle_tbl->llt_ifp; 1089 IF_AFDATA_LOCK(ifp); 1090 LLE_WLOCK(ln); 1091 LLE_REMREF(ln); 1092 llentry_free(ln); 1093 IF_AFDATA_UNLOCK(ifp); 1094 1095 return (next); 1096} 1097 1098/* 1099 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1100 * 1101 * XXX cost-effective methods? 1102 */ 1103void 1104nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force) 1105{ 1106 struct llentry *ln; 1107 struct ifnet *ifp; 1108 1109 if ((dst6 == NULL) || (rt == NULL)) 1110 return; 1111 1112 ifp = rt->rt_ifp; 1113 IF_AFDATA_LOCK(ifp); 1114 ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL); 1115 IF_AFDATA_UNLOCK(ifp); 1116 if (ln == NULL) 1117 return; 1118 1119 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1120 goto done; 1121 1122 /* 1123 * if we get upper-layer reachability confirmation many times, 1124 * it is possible we have false information. 1125 */ 1126 if (!force) { 1127 ln->ln_byhint++; 1128 if (ln->ln_byhint > V_nd6_maxnudhint) { 1129 goto done; 1130 } 1131 } 1132 1133 ln->ln_state = ND6_LLINFO_REACHABLE; 1134 if (!ND6_LLINFO_PERMANENT(ln)) { 1135 nd6_llinfo_settimer_locked(ln, 1136 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz); 1137 } 1138done: 1139 LLE_WUNLOCK(ln); 1140} 1141 1142 1143int 1144nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp) 1145{ 1146 struct in6_drlist *drl = (struct in6_drlist *)data; 1147 struct in6_oprlist *oprl = (struct in6_oprlist *)data; 1148 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1149 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1150 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1151 struct nd_defrouter *dr; 1152 struct nd_prefix *pr; 1153 int i = 0, error = 0; 1154 int s; 1155 1156 switch (cmd) { 1157 case SIOCGDRLST_IN6: 1158 /* 1159 * obsolete API, use sysctl under net.inet6.icmp6 1160 */ 1161 bzero(drl, sizeof(*drl)); 1162 s = splnet(); 1163 dr = TAILQ_FIRST(&V_nd_defrouter); 1164 while (dr && i < DRLSTSIZ) { 1165 drl->defrouter[i].rtaddr = dr->rtaddr; 1166 in6_clearscope(&drl->defrouter[i].rtaddr); 1167 1168 drl->defrouter[i].flags = dr->flags; 1169 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1170 drl->defrouter[i].expire = dr->expire; 1171 drl->defrouter[i].if_index = dr->ifp->if_index; 1172 i++; 1173 dr = TAILQ_NEXT(dr, dr_entry); 1174 } 1175 splx(s); 1176 break; 1177 case SIOCGPRLST_IN6: 1178 /* 1179 * obsolete API, use sysctl under net.inet6.icmp6 1180 * 1181 * XXX the structure in6_prlist was changed in backward- 1182 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6, 1183 * in6_prlist is used for nd6_sysctl() - fill_prlist(). 1184 */ 1185 /* 1186 * XXX meaning of fields, especialy "raflags", is very 1187 * differnet between RA prefix list and RR/static prefix list. 1188 * how about separating ioctls into two? 1189 */ 1190 bzero(oprl, sizeof(*oprl)); 1191 s = splnet(); 1192 pr = V_nd_prefix.lh_first; 1193 while (pr && i < PRLSTSIZ) { 1194 struct nd_pfxrouter *pfr; 1195 int j; 1196 1197 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr; 1198 oprl->prefix[i].raflags = pr->ndpr_raf; 1199 oprl->prefix[i].prefixlen = pr->ndpr_plen; 1200 oprl->prefix[i].vltime = pr->ndpr_vltime; 1201 oprl->prefix[i].pltime = pr->ndpr_pltime; 1202 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1203 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1204 oprl->prefix[i].expire = 0; 1205 else { 1206 time_t maxexpire; 1207 1208 /* XXX: we assume time_t is signed. */ 1209 maxexpire = (-1) & 1210 ~((time_t)1 << 1211 ((sizeof(maxexpire) * 8) - 1)); 1212 if (pr->ndpr_vltime < 1213 maxexpire - pr->ndpr_lastupdate) { 1214 oprl->prefix[i].expire = 1215 pr->ndpr_lastupdate + 1216 pr->ndpr_vltime; 1217 } else 1218 oprl->prefix[i].expire = maxexpire; 1219 } 1220 1221 pfr = pr->ndpr_advrtrs.lh_first; 1222 j = 0; 1223 while (pfr) { 1224 if (j < DRLSTSIZ) { 1225#define RTRADDR oprl->prefix[i].advrtr[j] 1226 RTRADDR = pfr->router->rtaddr; 1227 in6_clearscope(&RTRADDR); 1228#undef RTRADDR 1229 } 1230 j++; 1231 pfr = pfr->pfr_next; 1232 } 1233 oprl->prefix[i].advrtrs = j; 1234 oprl->prefix[i].origin = PR_ORIG_RA; 1235 1236 i++; 1237 pr = pr->ndpr_next; 1238 } 1239 splx(s); 1240 1241 break; 1242 case OSIOCGIFINFO_IN6: 1243#define ND ndi->ndi 1244 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1245 bzero(&ND, sizeof(ND)); 1246 ND.linkmtu = IN6_LINKMTU(ifp); 1247 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1248 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1249 ND.reachable = ND_IFINFO(ifp)->reachable; 1250 ND.retrans = ND_IFINFO(ifp)->retrans; 1251 ND.flags = ND_IFINFO(ifp)->flags; 1252 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1253 ND.chlim = ND_IFINFO(ifp)->chlim; 1254 break; 1255 case SIOCGIFINFO_IN6: 1256 ND = *ND_IFINFO(ifp); 1257 break; 1258 case SIOCSIFINFO_IN6: 1259 /* 1260 * used to change host variables from userland. 1261 * intented for a use on router to reflect RA configurations. 1262 */ 1263 /* 0 means 'unspecified' */ 1264 if (ND.linkmtu != 0) { 1265 if (ND.linkmtu < IPV6_MMTU || 1266 ND.linkmtu > IN6_LINKMTU(ifp)) { 1267 error = EINVAL; 1268 break; 1269 } 1270 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1271 } 1272 1273 if (ND.basereachable != 0) { 1274 int obasereachable = ND_IFINFO(ifp)->basereachable; 1275 1276 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1277 if (ND.basereachable != obasereachable) 1278 ND_IFINFO(ifp)->reachable = 1279 ND_COMPUTE_RTIME(ND.basereachable); 1280 } 1281 if (ND.retrans != 0) 1282 ND_IFINFO(ifp)->retrans = ND.retrans; 1283 if (ND.chlim != 0) 1284 ND_IFINFO(ifp)->chlim = ND.chlim; 1285 /* FALLTHROUGH */ 1286 case SIOCSIFINFO_FLAGS: 1287 { 1288 struct ifaddr *ifa; 1289 struct in6_ifaddr *ia; 1290 1291 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1292 !(ND.flags & ND6_IFF_IFDISABLED)) { 1293 /* ifdisabled 1->0 transision */ 1294 1295 /* 1296 * If the interface is marked as ND6_IFF_IFDISABLED and 1297 * has an link-local address with IN6_IFF_DUPLICATED, 1298 * do not clear ND6_IFF_IFDISABLED. 1299 * See RFC 4862, Section 5.4.5. 1300 */ 1301 int duplicated_linklocal = 0; 1302 1303 IF_ADDR_LOCK(ifp); 1304 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1305 if (ifa->ifa_addr->sa_family != AF_INET6) 1306 continue; 1307 ia = (struct in6_ifaddr *)ifa; 1308 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1309 IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 1310 duplicated_linklocal = 1; 1311 break; 1312 } 1313 } 1314 IF_ADDR_UNLOCK(ifp); 1315 1316 if (duplicated_linklocal) { 1317 ND.flags |= ND6_IFF_IFDISABLED; 1318 log(LOG_ERR, "Cannot enable an interface" 1319 " with a link-local address marked" 1320 " duplicate.\n"); 1321 } else { 1322 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED; 1323 in6_if_up(ifp); 1324 } 1325 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1326 (ND.flags & ND6_IFF_IFDISABLED)) { 1327 /* ifdisabled 0->1 transision */ 1328 /* Mark all IPv6 address as tentative. */ 1329 1330 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1331 IF_ADDR_LOCK(ifp); 1332 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1333 if (ifa->ifa_addr->sa_family != AF_INET6) 1334 continue; 1335 ia = (struct in6_ifaddr *)ifa; 1336 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1337 } 1338 IF_ADDR_UNLOCK(ifp); 1339 } 1340 1341 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) && 1342 (ND.flags & ND6_IFF_AUTO_LINKLOCAL)) { 1343 /* auto_linklocal 0->1 transision */ 1344 1345 /* If no link-local address on ifp, configure */ 1346 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL; 1347 in6_ifattach(ifp, NULL); 1348 } 1349 } 1350 ND_IFINFO(ifp)->flags = ND.flags; 1351 break; 1352#undef ND 1353 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1354 /* sync kernel routing table with the default router list */ 1355 defrouter_reset(); 1356 defrouter_select(); 1357 break; 1358 case SIOCSPFXFLUSH_IN6: 1359 { 1360 /* flush all the prefix advertised by routers */ 1361 struct nd_prefix *pr, *next; 1362 1363 s = splnet(); 1364 for (pr = V_nd_prefix.lh_first; pr; pr = next) { 1365 struct in6_ifaddr *ia, *ia_next; 1366 1367 next = pr->ndpr_next; 1368 1369 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1370 continue; /* XXX */ 1371 1372 /* do we really have to remove addresses as well? */ 1373 /* XXXRW: in6_ifaddrhead locking. */ 1374 TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link, 1375 ia_next) { 1376 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1377 continue; 1378 1379 if (ia->ia6_ndpr == pr) 1380 in6_purgeaddr(&ia->ia_ifa); 1381 } 1382 prelist_remove(pr); 1383 } 1384 splx(s); 1385 break; 1386 } 1387 case SIOCSRTRFLUSH_IN6: 1388 { 1389 /* flush all the default routers */ 1390 struct nd_defrouter *dr, *next; 1391 1392 s = splnet(); 1393 defrouter_reset(); 1394 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) { 1395 next = TAILQ_NEXT(dr, dr_entry); 1396 defrtrlist_del(dr); 1397 } 1398 defrouter_select(); 1399 splx(s); 1400 break; 1401 } 1402 case SIOCGNBRINFO_IN6: 1403 { 1404 struct llentry *ln; 1405 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1406 1407 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1408 return (error); 1409 1410 IF_AFDATA_LOCK(ifp); 1411 ln = nd6_lookup(&nb_addr, 0, ifp); 1412 IF_AFDATA_UNLOCK(ifp); 1413 1414 if (ln == NULL) { 1415 error = EINVAL; 1416 break; 1417 } 1418 nbi->state = ln->ln_state; 1419 nbi->asked = ln->la_asked; 1420 nbi->isrouter = ln->ln_router; 1421 nbi->expire = ln->la_expire; 1422 LLE_RUNLOCK(ln); 1423 break; 1424 } 1425 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1426 ndif->ifindex = V_nd6_defifindex; 1427 break; 1428 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1429 return (nd6_setdefaultiface(ndif->ifindex)); 1430 } 1431 return (error); 1432} 1433 1434/* 1435 * Create neighbor cache entry and cache link-layer address, 1436 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1437 * 1438 * type - ICMP6 type 1439 * code - type dependent information 1440 * 1441 * XXXXX 1442 * The caller of this function already acquired the ndp 1443 * cache table lock because the cache entry is returned. 1444 */ 1445struct llentry * 1446nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr, 1447 int lladdrlen, int type, int code) 1448{ 1449 struct llentry *ln = NULL; 1450 int is_newentry; 1451 int do_update; 1452 int olladdr; 1453 int llchange; 1454 int flags = 0; 1455 int newstate = 0; 1456 uint16_t router = 0; 1457 struct sockaddr_in6 sin6; 1458 struct mbuf *chain = NULL; 1459 int static_route = 0; 1460 1461 IF_AFDATA_UNLOCK_ASSERT(ifp); 1462 1463 if (ifp == NULL) 1464 panic("ifp == NULL in nd6_cache_lladdr"); 1465 if (from == NULL) 1466 panic("from == NULL in nd6_cache_lladdr"); 1467 1468 /* nothing must be updated for unspecified address */ 1469 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1470 return NULL; 1471 1472 /* 1473 * Validation about ifp->if_addrlen and lladdrlen must be done in 1474 * the caller. 1475 * 1476 * XXX If the link does not have link-layer adderss, what should 1477 * we do? (ifp->if_addrlen == 0) 1478 * Spec says nothing in sections for RA, RS and NA. There's small 1479 * description on it in NS section (RFC 2461 7.2.3). 1480 */ 1481 flags |= lladdr ? ND6_EXCLUSIVE : 0; 1482 IF_AFDATA_LOCK(ifp); 1483 ln = nd6_lookup(from, flags, ifp); 1484 1485 if (ln == NULL) { 1486 flags |= LLE_EXCLUSIVE; 1487 ln = nd6_lookup(from, flags |ND6_CREATE, ifp); 1488 IF_AFDATA_UNLOCK(ifp); 1489 is_newentry = 1; 1490 } else { 1491 IF_AFDATA_UNLOCK(ifp); 1492 /* do nothing if static ndp is set */ 1493 if (ln->la_flags & LLE_STATIC) { 1494 static_route = 1; 1495 goto done; 1496 } 1497 is_newentry = 0; 1498 } 1499 if (ln == NULL) 1500 return (NULL); 1501 1502 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 1503 if (olladdr && lladdr) { 1504 llchange = bcmp(lladdr, &ln->ll_addr, 1505 ifp->if_addrlen); 1506 } else 1507 llchange = 0; 1508 1509 /* 1510 * newentry olladdr lladdr llchange (*=record) 1511 * 0 n n -- (1) 1512 * 0 y n -- (2) 1513 * 0 n y -- (3) * STALE 1514 * 0 y y n (4) * 1515 * 0 y y y (5) * STALE 1516 * 1 -- n -- (6) NOSTATE(= PASSIVE) 1517 * 1 -- y -- (7) * STALE 1518 */ 1519 1520 if (lladdr) { /* (3-5) and (7) */ 1521 /* 1522 * Record source link-layer address 1523 * XXX is it dependent to ifp->if_type? 1524 */ 1525 bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen); 1526 ln->la_flags |= LLE_VALID; 1527 } 1528 1529 if (!is_newentry) { 1530 if ((!olladdr && lladdr != NULL) || /* (3) */ 1531 (olladdr && lladdr != NULL && llchange)) { /* (5) */ 1532 do_update = 1; 1533 newstate = ND6_LLINFO_STALE; 1534 } else /* (1-2,4) */ 1535 do_update = 0; 1536 } else { 1537 do_update = 1; 1538 if (lladdr == NULL) /* (6) */ 1539 newstate = ND6_LLINFO_NOSTATE; 1540 else /* (7) */ 1541 newstate = ND6_LLINFO_STALE; 1542 } 1543 1544 if (do_update) { 1545 /* 1546 * Update the state of the neighbor cache. 1547 */ 1548 ln->ln_state = newstate; 1549 1550 if (ln->ln_state == ND6_LLINFO_STALE) { 1551 /* 1552 * XXX: since nd6_output() below will cause 1553 * state tansition to DELAY and reset the timer, 1554 * we must set the timer now, although it is actually 1555 * meaningless. 1556 */ 1557 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 1558 1559 if (ln->la_hold) { 1560 struct mbuf *m_hold, *m_hold_next; 1561 1562 /* 1563 * reset the la_hold in advance, to explicitly 1564 * prevent a la_hold lookup in nd6_output() 1565 * (wouldn't happen, though...) 1566 */ 1567 for (m_hold = ln->la_hold, ln->la_hold = NULL; 1568 m_hold; m_hold = m_hold_next) { 1569 m_hold_next = m_hold->m_nextpkt; 1570 m_hold->m_nextpkt = NULL; 1571 1572 /* 1573 * we assume ifp is not a p2p here, so 1574 * just set the 2nd argument as the 1575 * 1st one. 1576 */ 1577 nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain); 1578 } 1579 /* 1580 * If we have mbufs in the chain we need to do 1581 * deferred transmit. Copy the address from the 1582 * llentry before dropping the lock down below. 1583 */ 1584 if (chain != NULL) 1585 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6)); 1586 } 1587 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 1588 /* probe right away */ 1589 nd6_llinfo_settimer_locked((void *)ln, 0); 1590 } 1591 } 1592 1593 /* 1594 * ICMP6 type dependent behavior. 1595 * 1596 * NS: clear IsRouter if new entry 1597 * RS: clear IsRouter 1598 * RA: set IsRouter if there's lladdr 1599 * redir: clear IsRouter if new entry 1600 * 1601 * RA case, (1): 1602 * The spec says that we must set IsRouter in the following cases: 1603 * - If lladdr exist, set IsRouter. This means (1-5). 1604 * - If it is old entry (!newentry), set IsRouter. This means (7). 1605 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1606 * A quetion arises for (1) case. (1) case has no lladdr in the 1607 * neighbor cache, this is similar to (6). 1608 * This case is rare but we figured that we MUST NOT set IsRouter. 1609 * 1610 * newentry olladdr lladdr llchange NS RS RA redir 1611 * D R 1612 * 0 n n -- (1) c ? s 1613 * 0 y n -- (2) c s s 1614 * 0 n y -- (3) c s s 1615 * 0 y y n (4) c s s 1616 * 0 y y y (5) c s s 1617 * 1 -- n -- (6) c c c s 1618 * 1 -- y -- (7) c c s c s 1619 * 1620 * (c=clear s=set) 1621 */ 1622 switch (type & 0xff) { 1623 case ND_NEIGHBOR_SOLICIT: 1624 /* 1625 * New entry must have is_router flag cleared. 1626 */ 1627 if (is_newentry) /* (6-7) */ 1628 ln->ln_router = 0; 1629 break; 1630 case ND_REDIRECT: 1631 /* 1632 * If the icmp is a redirect to a better router, always set the 1633 * is_router flag. Otherwise, if the entry is newly created, 1634 * clear the flag. [RFC 2461, sec 8.3] 1635 */ 1636 if (code == ND_REDIRECT_ROUTER) 1637 ln->ln_router = 1; 1638 else if (is_newentry) /* (6-7) */ 1639 ln->ln_router = 0; 1640 break; 1641 case ND_ROUTER_SOLICIT: 1642 /* 1643 * is_router flag must always be cleared. 1644 */ 1645 ln->ln_router = 0; 1646 break; 1647 case ND_ROUTER_ADVERT: 1648 /* 1649 * Mark an entry with lladdr as a router. 1650 */ 1651 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 1652 (is_newentry && lladdr)) { /* (7) */ 1653 ln->ln_router = 1; 1654 } 1655 break; 1656 } 1657 1658 if (ln != NULL) { 1659 static_route = (ln->la_flags & LLE_STATIC); 1660 router = ln->ln_router; 1661 1662 if (flags & ND6_EXCLUSIVE) 1663 LLE_WUNLOCK(ln); 1664 else 1665 LLE_RUNLOCK(ln); 1666 if (static_route) 1667 ln = NULL; 1668 } 1669 if (chain) 1670 nd6_output_flush(ifp, ifp, chain, &sin6, NULL); 1671 1672 /* 1673 * When the link-layer address of a router changes, select the 1674 * best router again. In particular, when the neighbor entry is newly 1675 * created, it might affect the selection policy. 1676 * Question: can we restrict the first condition to the "is_newentry" 1677 * case? 1678 * XXX: when we hear an RA from a new router with the link-layer 1679 * address option, defrouter_select() is called twice, since 1680 * defrtrlist_update called the function as well. However, I believe 1681 * we can compromise the overhead, since it only happens the first 1682 * time. 1683 * XXX: although defrouter_select() should not have a bad effect 1684 * for those are not autoconfigured hosts, we explicitly avoid such 1685 * cases for safety. 1686 */ 1687 if (do_update && router && !V_ip6_forwarding && 1688 ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) { 1689 /* 1690 * guaranteed recursion 1691 */ 1692 defrouter_select(); 1693 } 1694 1695 return (ln); 1696done: 1697 if (ln != NULL) { 1698 if (flags & ND6_EXCLUSIVE) 1699 LLE_WUNLOCK(ln); 1700 else 1701 LLE_RUNLOCK(ln); 1702 if (static_route) 1703 ln = NULL; 1704 } 1705 return (ln); 1706} 1707 1708static void 1709nd6_slowtimo(void *arg) 1710{ 1711 CURVNET_SET((struct vnet *) arg); 1712 struct nd_ifinfo *nd6if; 1713 struct ifnet *ifp; 1714 1715 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 1716 nd6_slowtimo, curvnet); 1717 IFNET_RLOCK_NOSLEEP(); 1718 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 1719 ifp = TAILQ_NEXT(ifp, if_list)) { 1720 nd6if = ND_IFINFO(ifp); 1721 if (nd6if->basereachable && /* already initialized */ 1722 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 1723 /* 1724 * Since reachable time rarely changes by router 1725 * advertisements, we SHOULD insure that a new random 1726 * value gets recomputed at least once every few hours. 1727 * (RFC 2461, 6.3.4) 1728 */ 1729 nd6if->recalctm = V_nd6_recalc_reachtm_interval; 1730 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 1731 } 1732 } 1733 IFNET_RUNLOCK_NOSLEEP(); 1734 CURVNET_RESTORE(); 1735} 1736 1737int 1738nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1739 struct sockaddr_in6 *dst, struct rtentry *rt0) 1740{ 1741 1742 return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL)); 1743} 1744 1745 1746/* 1747 * Note that I'm not enforcing any global serialization 1748 * lle state or asked changes here as the logic is too 1749 * complicated to avoid having to always acquire an exclusive 1750 * lock 1751 * KMM 1752 * 1753 */ 1754#define senderr(e) { error = (e); goto bad;} 1755 1756int 1757nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1758 struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle, 1759 struct mbuf **chain) 1760{ 1761 struct mbuf *m = m0; 1762 struct m_tag *mtag; 1763 struct llentry *ln = lle; 1764 struct ip6_hdr *ip6; 1765 int error = 0; 1766 int flags = 0; 1767 int ip6len; 1768 1769#ifdef INVARIANTS 1770 if (lle != NULL) { 1771 1772 LLE_WLOCK_ASSERT(lle); 1773 1774 KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed")); 1775 } 1776#endif 1777 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 1778 goto sendpkt; 1779 1780 if (nd6_need_cache(ifp) == 0) 1781 goto sendpkt; 1782 1783 /* 1784 * next hop determination. This routine is derived from ether_output. 1785 */ 1786 1787 /* 1788 * Address resolution or Neighbor Unreachability Detection 1789 * for the next hop. 1790 * At this point, the destination of the packet must be a unicast 1791 * or an anycast address(i.e. not a multicast). 1792 */ 1793 1794 flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0; 1795 if (ln == NULL) { 1796 retry: 1797 IF_AFDATA_LOCK(ifp); 1798 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst); 1799 IF_AFDATA_UNLOCK(ifp); 1800 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 1801 /* 1802 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 1803 * the condition below is not very efficient. But we believe 1804 * it is tolerable, because this should be a rare case. 1805 */ 1806 flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0); 1807 IF_AFDATA_LOCK(ifp); 1808 ln = nd6_lookup(&dst->sin6_addr, flags, ifp); 1809 IF_AFDATA_UNLOCK(ifp); 1810 } 1811 } 1812 if (ln == NULL) { 1813 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 1814 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 1815 char ip6buf[INET6_ADDRSTRLEN]; 1816 log(LOG_DEBUG, 1817 "nd6_output: can't allocate llinfo for %s " 1818 "(ln=%p)\n", 1819 ip6_sprintf(ip6buf, &dst->sin6_addr), ln); 1820 senderr(EIO); /* XXX: good error? */ 1821 } 1822 goto sendpkt; /* send anyway */ 1823 } 1824 1825 /* We don't have to do link-layer address resolution on a p2p link. */ 1826 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 1827 ln->ln_state < ND6_LLINFO_REACHABLE) { 1828 if ((flags & LLE_EXCLUSIVE) == 0) { 1829 flags |= LLE_EXCLUSIVE; 1830 goto retry; 1831 } 1832 ln->ln_state = ND6_LLINFO_STALE; 1833 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 1834 } 1835 1836 /* 1837 * The first time we send a packet to a neighbor whose entry is 1838 * STALE, we have to change the state to DELAY and a sets a timer to 1839 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 1840 * neighbor unreachability detection on expiration. 1841 * (RFC 2461 7.3.3) 1842 */ 1843 if (ln->ln_state == ND6_LLINFO_STALE) { 1844 if ((flags & LLE_EXCLUSIVE) == 0) { 1845 flags |= LLE_EXCLUSIVE; 1846 LLE_RUNLOCK(ln); 1847 goto retry; 1848 } 1849 ln->la_asked = 0; 1850 ln->ln_state = ND6_LLINFO_DELAY; 1851 nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz); 1852 } 1853 1854 /* 1855 * If the neighbor cache entry has a state other than INCOMPLETE 1856 * (i.e. its link-layer address is already resolved), just 1857 * send the packet. 1858 */ 1859 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1860 goto sendpkt; 1861 1862 /* 1863 * There is a neighbor cache entry, but no ethernet address 1864 * response yet. Append this latest packet to the end of the 1865 * packet queue in the mbuf, unless the number of the packet 1866 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen, 1867 * the oldest packet in the queue will be removed. 1868 */ 1869 if (ln->ln_state == ND6_LLINFO_NOSTATE) 1870 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1871 1872 if ((flags & LLE_EXCLUSIVE) == 0) { 1873 flags |= LLE_EXCLUSIVE; 1874 LLE_RUNLOCK(ln); 1875 goto retry; 1876 } 1877 if (ln->la_hold) { 1878 struct mbuf *m_hold; 1879 int i; 1880 1881 i = 0; 1882 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) { 1883 i++; 1884 if (m_hold->m_nextpkt == NULL) { 1885 m_hold->m_nextpkt = m; 1886 break; 1887 } 1888 } 1889 while (i >= V_nd6_maxqueuelen) { 1890 m_hold = ln->la_hold; 1891 ln->la_hold = ln->la_hold->m_nextpkt; 1892 m_freem(m_hold); 1893 i--; 1894 } 1895 } else { 1896 ln->la_hold = m; 1897 } 1898 /* 1899 * We did the lookup (no lle arg) so we 1900 * need to do the unlock here 1901 */ 1902 if (lle == NULL) { 1903 if (flags & LLE_EXCLUSIVE) 1904 LLE_WUNLOCK(ln); 1905 else 1906 LLE_RUNLOCK(ln); 1907 } 1908 1909 /* 1910 * If there has been no NS for the neighbor after entering the 1911 * INCOMPLETE state, send the first solicitation. 1912 */ 1913 if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) { 1914 ln->la_asked++; 1915 1916 nd6_llinfo_settimer(ln, 1917 (long)ND_IFINFO(ifp)->retrans * hz / 1000); 1918 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 1919 } 1920 return (0); 1921 1922 sendpkt: 1923 /* discard the packet if IPv6 operation is disabled on the interface */ 1924 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 1925 error = ENETDOWN; /* better error? */ 1926 goto bad; 1927 } 1928 /* 1929 * ln is valid and the caller did not pass in 1930 * an llentry 1931 */ 1932 if ((ln != NULL) && (lle == NULL)) { 1933 if (flags & LLE_EXCLUSIVE) 1934 LLE_WUNLOCK(ln); 1935 else 1936 LLE_RUNLOCK(ln); 1937 } 1938 1939#ifdef MAC 1940 mac_netinet6_nd6_send(ifp, m); 1941#endif 1942 1943 /* 1944 * If called from nd6_ns_output() (NS), nd6_na_output() (NA), 1945 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA 1946 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND 1947 * to be diverted to user space. When re-injected into the kernel, 1948 * send_output() will directly dispatch them to the outgoing interface. 1949 */ 1950 if (send_sendso_input_hook != NULL) { 1951 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL); 1952 if (mtag != NULL) { 1953 ip6 = mtod(m, struct ip6_hdr *); 1954 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 1955 /* Use the SEND socket */ 1956 error = send_sendso_input_hook(m, ifp, SND_OUT, 1957 ip6len); 1958 /* -1 == no app on SEND socket */ 1959 if (error == 0 || error != -1) 1960 return (error); 1961 } 1962 } 1963 1964 /* 1965 * We were passed in a pointer to an lle with the lock held 1966 * this means that we can't call if_output as we will 1967 * recurse on the lle lock - so what we do is we create 1968 * a list of mbufs to send and transmit them in the caller 1969 * after the lock is dropped 1970 */ 1971 if (lle != NULL) { 1972 if (*chain == NULL) 1973 *chain = m; 1974 else { 1975 struct mbuf *m = *chain; 1976 1977 /* 1978 * append mbuf to end of deferred chain 1979 */ 1980 while (m->m_nextpkt != NULL) 1981 m = m->m_nextpkt; 1982 m->m_nextpkt = m; 1983 } 1984 return (error); 1985 } 1986 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 1987 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst, 1988 NULL)); 1989 } 1990 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL); 1991 return (error); 1992 1993 bad: 1994 /* 1995 * ln is valid and the caller did not pass in 1996 * an llentry 1997 */ 1998 if ((ln != NULL) && (lle == NULL)) { 1999 if (flags & LLE_EXCLUSIVE) 2000 LLE_WUNLOCK(ln); 2001 else 2002 LLE_RUNLOCK(ln); 2003 } 2004 if (m) 2005 m_freem(m); 2006 return (error); 2007} 2008#undef senderr 2009 2010 2011int 2012nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain, 2013 struct sockaddr_in6 *dst, struct route *ro) 2014{ 2015 struct mbuf *m, *m_head; 2016 struct ifnet *outifp; 2017 int error = 0; 2018 2019 m_head = chain; 2020 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2021 outifp = origifp; 2022 else 2023 outifp = ifp; 2024 2025 while (m_head) { 2026 m = m_head; 2027 m_head = m_head->m_nextpkt; 2028 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro); 2029 } 2030 2031 /* 2032 * XXX 2033 * note that intermediate errors are blindly ignored - but this is 2034 * the same convention as used with nd6_output when called by 2035 * nd6_cache_lladdr 2036 */ 2037 return (error); 2038} 2039 2040 2041int 2042nd6_need_cache(struct ifnet *ifp) 2043{ 2044 /* 2045 * XXX: we currently do not make neighbor cache on any interface 2046 * other than ARCnet, Ethernet, FDDI and GIF. 2047 * 2048 * RFC2893 says: 2049 * - unidirectional tunnels needs no ND 2050 */ 2051 switch (ifp->if_type) { 2052 case IFT_ARCNET: 2053 case IFT_ETHER: 2054 case IFT_FDDI: 2055 case IFT_IEEE1394: 2056#ifdef IFT_L2VLAN 2057 case IFT_L2VLAN: 2058#endif 2059#ifdef IFT_IEEE80211 2060 case IFT_IEEE80211: 2061#endif 2062#ifdef IFT_CARP 2063 case IFT_CARP: 2064#endif 2065 case IFT_GIF: /* XXX need more cases? */ 2066 case IFT_PPP: 2067 case IFT_TUNNEL: 2068 case IFT_BRIDGE: 2069 case IFT_PROPVIRTUAL: 2070 return (1); 2071 default: 2072 return (0); 2073 } 2074} 2075 2076/* 2077 * the callers of this function need to be re-worked to drop 2078 * the lle lock, drop here for now 2079 */ 2080int 2081nd6_storelladdr(struct ifnet *ifp, struct mbuf *m, 2082 struct sockaddr *dst, u_char *desten, struct llentry **lle) 2083{ 2084 struct llentry *ln; 2085 2086 *lle = NULL; 2087 IF_AFDATA_UNLOCK_ASSERT(ifp); 2088 if (m->m_flags & M_MCAST) { 2089 int i; 2090 2091 switch (ifp->if_type) { 2092 case IFT_ETHER: 2093 case IFT_FDDI: 2094#ifdef IFT_L2VLAN 2095 case IFT_L2VLAN: 2096#endif 2097#ifdef IFT_IEEE80211 2098 case IFT_IEEE80211: 2099#endif 2100 case IFT_BRIDGE: 2101 case IFT_ISO88025: 2102 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, 2103 desten); 2104 return (0); 2105 case IFT_IEEE1394: 2106 /* 2107 * netbsd can use if_broadcastaddr, but we don't do so 2108 * to reduce # of ifdef. 2109 */ 2110 for (i = 0; i < ifp->if_addrlen; i++) 2111 desten[i] = ~0; 2112 return (0); 2113 case IFT_ARCNET: 2114 *desten = 0; 2115 return (0); 2116 default: 2117 m_freem(m); 2118 return (EAFNOSUPPORT); 2119 } 2120 } 2121 2122 2123 /* 2124 * the entry should have been created in nd6_store_lladdr 2125 */ 2126 IF_AFDATA_LOCK(ifp); 2127 ln = lla_lookup(LLTABLE6(ifp), 0, dst); 2128 IF_AFDATA_UNLOCK(ifp); 2129 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) { 2130 if (ln != NULL) 2131 LLE_RUNLOCK(ln); 2132 /* this could happen, if we could not allocate memory */ 2133 m_freem(m); 2134 return (1); 2135 } 2136 2137 bcopy(&ln->ll_addr, desten, ifp->if_addrlen); 2138 *lle = ln; 2139 LLE_RUNLOCK(ln); 2140 /* 2141 * A *small* use after free race exists here 2142 */ 2143 return (0); 2144} 2145 2146static void 2147clear_llinfo_pqueue(struct llentry *ln) 2148{ 2149 struct mbuf *m_hold, *m_hold_next; 2150 2151 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) { 2152 m_hold_next = m_hold->m_nextpkt; 2153 m_hold->m_nextpkt = NULL; 2154 m_freem(m_hold); 2155 } 2156 2157 ln->la_hold = NULL; 2158 return; 2159} 2160 2161static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS); 2162static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS); 2163#ifdef SYSCTL_DECL 2164SYSCTL_DECL(_net_inet6_icmp6); 2165#endif 2166SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, 2167 CTLFLAG_RD, nd6_sysctl_drlist, ""); 2168SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2169 CTLFLAG_RD, nd6_sysctl_prlist, ""); 2170SYSCTL_VNET_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen, 2171 CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, ""); 2172 2173static int 2174nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS) 2175{ 2176 int error; 2177 char buf[1024] __aligned(4); 2178 struct in6_defrouter *d, *de; 2179 struct nd_defrouter *dr; 2180 2181 if (req->newptr) 2182 return EPERM; 2183 error = 0; 2184 2185 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; 2186 dr = TAILQ_NEXT(dr, dr_entry)) { 2187 d = (struct in6_defrouter *)buf; 2188 de = (struct in6_defrouter *)(buf + sizeof(buf)); 2189 2190 if (d + 1 <= de) { 2191 bzero(d, sizeof(*d)); 2192 d->rtaddr.sin6_family = AF_INET6; 2193 d->rtaddr.sin6_len = sizeof(d->rtaddr); 2194 d->rtaddr.sin6_addr = dr->rtaddr; 2195 error = sa6_recoverscope(&d->rtaddr); 2196 if (error != 0) 2197 return (error); 2198 d->flags = dr->flags; 2199 d->rtlifetime = dr->rtlifetime; 2200 d->expire = dr->expire; 2201 d->if_index = dr->ifp->if_index; 2202 } else 2203 panic("buffer too short"); 2204 2205 error = SYSCTL_OUT(req, buf, sizeof(*d)); 2206 if (error) 2207 break; 2208 } 2209 2210 return (error); 2211} 2212 2213static int 2214nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2215{ 2216 int error; 2217 char buf[1024] __aligned(4); 2218 struct in6_prefix *p, *pe; 2219 struct nd_prefix *pr; 2220 char ip6buf[INET6_ADDRSTRLEN]; 2221 2222 if (req->newptr) 2223 return EPERM; 2224 error = 0; 2225 2226 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 2227 u_short advrtrs; 2228 size_t advance; 2229 struct sockaddr_in6 *sin6, *s6; 2230 struct nd_pfxrouter *pfr; 2231 2232 p = (struct in6_prefix *)buf; 2233 pe = (struct in6_prefix *)(buf + sizeof(buf)); 2234 2235 if (p + 1 <= pe) { 2236 bzero(p, sizeof(*p)); 2237 sin6 = (struct sockaddr_in6 *)(p + 1); 2238 2239 p->prefix = pr->ndpr_prefix; 2240 if (sa6_recoverscope(&p->prefix)) { 2241 log(LOG_ERR, 2242 "scope error in prefix list (%s)\n", 2243 ip6_sprintf(ip6buf, &p->prefix.sin6_addr)); 2244 /* XXX: press on... */ 2245 } 2246 p->raflags = pr->ndpr_raf; 2247 p->prefixlen = pr->ndpr_plen; 2248 p->vltime = pr->ndpr_vltime; 2249 p->pltime = pr->ndpr_pltime; 2250 p->if_index = pr->ndpr_ifp->if_index; 2251 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2252 p->expire = 0; 2253 else { 2254 time_t maxexpire; 2255 2256 /* XXX: we assume time_t is signed. */ 2257 maxexpire = (-1) & 2258 ~((time_t)1 << 2259 ((sizeof(maxexpire) * 8) - 1)); 2260 if (pr->ndpr_vltime < 2261 maxexpire - pr->ndpr_lastupdate) { 2262 p->expire = pr->ndpr_lastupdate + 2263 pr->ndpr_vltime; 2264 } else 2265 p->expire = maxexpire; 2266 } 2267 p->refcnt = pr->ndpr_refcnt; 2268 p->flags = pr->ndpr_stateflags; 2269 p->origin = PR_ORIG_RA; 2270 advrtrs = 0; 2271 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; 2272 pfr = pfr->pfr_next) { 2273 if ((void *)&sin6[advrtrs + 1] > (void *)pe) { 2274 advrtrs++; 2275 continue; 2276 } 2277 s6 = &sin6[advrtrs]; 2278 bzero(s6, sizeof(*s6)); 2279 s6->sin6_family = AF_INET6; 2280 s6->sin6_len = sizeof(*sin6); 2281 s6->sin6_addr = pfr->router->rtaddr; 2282 if (sa6_recoverscope(s6)) { 2283 log(LOG_ERR, 2284 "scope error in " 2285 "prefix list (%s)\n", 2286 ip6_sprintf(ip6buf, 2287 &pfr->router->rtaddr)); 2288 } 2289 advrtrs++; 2290 } 2291 p->advrtrs = advrtrs; 2292 } else 2293 panic("buffer too short"); 2294 2295 advance = sizeof(*p) + sizeof(*sin6) * advrtrs; 2296 error = SYSCTL_OUT(req, buf, advance); 2297 if (error) 2298 break; 2299 } 2300 2301 return (error); 2302} 2303