xref: /freebsd/sys/netinet6/nd6.c (revision 17a036563d991f6f1cd398551f0806be3e2a0f11)
1caf43b02SWarner Losh /*-
282cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
382cd038dSYoshinobu Inoue  * All rights reserved.
482cd038dSYoshinobu Inoue  *
582cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
682cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
782cd038dSYoshinobu Inoue  * are met:
882cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
982cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1082cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1282cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1382cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1482cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1582cd038dSYoshinobu Inoue  *    without specific prior written permission.
1682cd038dSYoshinobu Inoue  *
1782cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1882cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1982cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2082cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2182cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2282cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2382cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2482cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2582cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2682cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2782cd038dSYoshinobu Inoue  * SUCH DAMAGE.
28b48287a3SDavid E. O'Brien  *
29b48287a3SDavid E. O'Brien  *	$KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
3082cd038dSYoshinobu Inoue  */
3182cd038dSYoshinobu Inoue 
32b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
33b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
34b48287a3SDavid E. O'Brien 
35686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
36686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
37686cdd19SJun-ichiro itojun Hagino 
3882cd038dSYoshinobu Inoue #include <sys/param.h>
3982cd038dSYoshinobu Inoue #include <sys/systm.h>
4033841545SHajimu UMEMOTO #include <sys/callout.h>
4182cd038dSYoshinobu Inoue #include <sys/malloc.h>
4282cd038dSYoshinobu Inoue #include <sys/mbuf.h>
4382cd038dSYoshinobu Inoue #include <sys/socket.h>
4482cd038dSYoshinobu Inoue #include <sys/sockio.h>
4582cd038dSYoshinobu Inoue #include <sys/time.h>
4682cd038dSYoshinobu Inoue #include <sys/kernel.h>
47686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
4882cd038dSYoshinobu Inoue #include <sys/errno.h>
4982cd038dSYoshinobu Inoue #include <sys/syslog.h>
506e6b3f7cSQing Li #include <sys/lock.h>
516e6b3f7cSQing Li #include <sys/rwlock.h>
5282cd038dSYoshinobu Inoue #include <sys/queue.h>
5357f60867SMark Johnston #include <sys/sdt.h>
5433841545SHajimu UMEMOTO #include <sys/sysctl.h>
5582cd038dSYoshinobu Inoue 
5682cd038dSYoshinobu Inoue #include <net/if.h>
5776039bc8SGleb Smirnoff #include <net/if_var.h>
5831b3783cSHajimu UMEMOTO #include <net/if_arc.h>
5982cd038dSYoshinobu Inoue #include <net/if_dl.h>
6082cd038dSYoshinobu Inoue #include <net/if_types.h>
612049fdeeSMatthew N. Dodd #include <net/iso88025.h>
628e5fd461SMatthew N. Dodd #include <net/fddi.h>
6382cd038dSYoshinobu Inoue #include <net/route.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
6582cd038dSYoshinobu Inoue 
6682cd038dSYoshinobu Inoue #include <netinet/in.h>
6757f60867SMark Johnston #include <netinet/in_kdtrace.h>
686e6b3f7cSQing Li #include <net/if_llatbl.h>
6982cd038dSYoshinobu Inoue #include <netinet/if_ether.h>
7082cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7282cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
73a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
7482cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
75a283298cSHiroki Sato #include <netinet6/in6_ifattach.h>
76686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
771db8d1f8SAna Kukec #include <netinet6/send.h>
7882cd038dSYoshinobu Inoue 
79743eee66SSUZUKI Shinsuke #include <sys/limits.h>
80743eee66SSUZUKI Shinsuke 
81aed55708SRobert Watson #include <security/mac/mac_framework.h>
82aed55708SRobert Watson 
8382cd038dSYoshinobu Inoue #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
8482cd038dSYoshinobu Inoue #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
8582cd038dSYoshinobu Inoue 
8647e8d432SGleb Smirnoff #define SIN6(s) ((const struct sockaddr_in6 *)(s))
8782cd038dSYoshinobu Inoue 
8882cea7e6SBjoern A. Zeeb /* timer values */
8982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_prune)	= 1;	/* walk list every 1 seconds */
9082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_delay)	= 5;	/* delay first probe time 5 second */
9182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_umaxtries)	= 3;	/* maximum unicast query */
9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_mmaxtries)	= 3;	/* maximum multicast query */
9382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_useloopback) = 1;	/* use loopback interface for
9482cea7e6SBjoern A. Zeeb 					 * local traffic */
9582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_gctimer)	= (60 * 60 * 24); /* 1 day: garbage
9682cea7e6SBjoern A. Zeeb 					 * collection timer */
97686cdd19SJun-ichiro itojun Hagino 
98686cdd19SJun-ichiro itojun Hagino /* preventing too many loops in ND option parsing */
993e288e62SDimitry Andric static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
10082cea7e6SBjoern A. Zeeb 
10182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_maxnudhint) = 0;	/* max # of subsequent upper
10282cea7e6SBjoern A. Zeeb 					 * layer hints */
1033e288e62SDimitry Andric static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
10482cea7e6SBjoern A. Zeeb 					 * ND entries */
1051e77c105SRobert Watson #define	V_nd6_maxndopt			VNET(nd6_maxndopt)
1061e77c105SRobert Watson #define	V_nd6_maxqueuelen		VNET(nd6_maxqueuelen)
107686cdd19SJun-ichiro itojun Hagino 
10882cea7e6SBjoern A. Zeeb #ifdef ND6_DEBUG
10982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_debug) = 1;
11082cea7e6SBjoern A. Zeeb #else
11182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_debug) = 0;
11282cea7e6SBjoern A. Zeeb #endif
11333841545SHajimu UMEMOTO 
11430aee131SAndrey V. Elsukov static eventhandler_tag lle_event_eh;
11530aee131SAndrey V. Elsukov 
11682cd038dSYoshinobu Inoue /* for debugging? */
1176e6b3f7cSQing Li #if 0
11882cd038dSYoshinobu Inoue static int nd6_inuse, nd6_allocated;
1196e6b3f7cSQing Li #endif
12082cd038dSYoshinobu Inoue 
121eddfbb76SRobert Watson VNET_DEFINE(struct nd_drhead, nd_defrouter);
122eddfbb76SRobert Watson VNET_DEFINE(struct nd_prhead, nd_prefix);
12382cd038dSYoshinobu Inoue 
12482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
1251e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
12644e33a07SMarko Zec 
1271db8d1f8SAna Kukec int	(*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
1281db8d1f8SAna Kukec 
1296f56329aSXin LI static int nd6_is_new_addr_neighbor(struct sockaddr_in6 *,
1306f56329aSXin LI 	struct ifnet *);
1319233d8f3SDavid E. O'Brien static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
1329233d8f3SDavid E. O'Brien static void nd6_slowtimo(void *);
1339233d8f3SDavid E. O'Brien static int regen_tmpaddr(struct in6_ifaddr *);
1346e6b3f7cSQing Li static struct llentry *nd6_free(struct llentry *, int);
135*17a03656SEric van Gyzen static void nd6_free_redirect(const struct llentry *);
1369233d8f3SDavid E. O'Brien static void nd6_llinfo_timer(void *);
1376e6b3f7cSQing Li static void clear_llinfo_pqueue(struct llentry *);
138f6b84910SAlexander V. Chernikov static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
139d7968c29SAlexander V. Chernikov static int nd6_output_lle(struct ifnet *, struct ifnet *, struct mbuf *,
140d7968c29SAlexander V. Chernikov 	struct sockaddr_in6 *);
141abc1be90SAlexander V. Chernikov static int nd6_output_ifp(struct ifnet *, struct ifnet *, struct mbuf *,
142abc1be90SAlexander V. Chernikov     struct sockaddr_in6 *);
14333841545SHajimu UMEMOTO 
1443e288e62SDimitry Andric static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
1451e77c105SRobert Watson #define	V_nd6_slowtimo_ch		VNET(nd6_slowtimo_ch)
146eddfbb76SRobert Watson 
147eddfbb76SRobert Watson VNET_DEFINE(struct callout, nd6_timer_ch);
148eddfbb76SRobert Watson 
14930aee131SAndrey V. Elsukov static void
15030aee131SAndrey V. Elsukov nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
15130aee131SAndrey V. Elsukov {
15230aee131SAndrey V. Elsukov 	struct rt_addrinfo rtinfo;
153314294deSAlexander V. Chernikov 	struct sockaddr_in6 dst;
15430aee131SAndrey V. Elsukov 	struct sockaddr_dl gw;
15530aee131SAndrey V. Elsukov 	struct ifnet *ifp;
15630aee131SAndrey V. Elsukov 	int type;
15730aee131SAndrey V. Elsukov 
15830aee131SAndrey V. Elsukov 	LLE_WLOCK_ASSERT(lle);
15930aee131SAndrey V. Elsukov 
160314294deSAlexander V. Chernikov 	if (lltable_get_af(lle->lle_tbl) != AF_INET6)
161314294deSAlexander V. Chernikov 		return;
162314294deSAlexander V. Chernikov 
16330aee131SAndrey V. Elsukov 	switch (evt) {
16430aee131SAndrey V. Elsukov 	case LLENTRY_RESOLVED:
16530aee131SAndrey V. Elsukov 		type = RTM_ADD;
16630aee131SAndrey V. Elsukov 		KASSERT(lle->la_flags & LLE_VALID,
16730aee131SAndrey V. Elsukov 		    ("%s: %p resolved but not valid?", __func__, lle));
16830aee131SAndrey V. Elsukov 		break;
16930aee131SAndrey V. Elsukov 	case LLENTRY_EXPIRED:
17030aee131SAndrey V. Elsukov 		type = RTM_DELETE;
17130aee131SAndrey V. Elsukov 		break;
17230aee131SAndrey V. Elsukov 	default:
17330aee131SAndrey V. Elsukov 		return;
17430aee131SAndrey V. Elsukov 	}
17530aee131SAndrey V. Elsukov 
176314294deSAlexander V. Chernikov 	ifp = lltable_get_ifp(lle->lle_tbl);
17730aee131SAndrey V. Elsukov 
17830aee131SAndrey V. Elsukov 	bzero(&dst, sizeof(dst));
17930aee131SAndrey V. Elsukov 	bzero(&gw, sizeof(gw));
18030aee131SAndrey V. Elsukov 	bzero(&rtinfo, sizeof(rtinfo));
181314294deSAlexander V. Chernikov 	lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
18230aee131SAndrey V. Elsukov 	dst.sin6_scope_id = in6_getscopezone(ifp,
183314294deSAlexander V. Chernikov 	    in6_addrscope(&dst.sin6_addr));
18430aee131SAndrey V. Elsukov 	gw.sdl_len = sizeof(struct sockaddr_dl);
18530aee131SAndrey V. Elsukov 	gw.sdl_family = AF_LINK;
18630aee131SAndrey V. Elsukov 	gw.sdl_alen = ifp->if_addrlen;
18730aee131SAndrey V. Elsukov 	gw.sdl_index = ifp->if_index;
18830aee131SAndrey V. Elsukov 	gw.sdl_type = ifp->if_type;
18930aee131SAndrey V. Elsukov 	if (evt == LLENTRY_RESOLVED)
19030aee131SAndrey V. Elsukov 		bcopy(&lle->ll_addr, gw.sdl_data, ifp->if_addrlen);
19130aee131SAndrey V. Elsukov 	rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst;
19230aee131SAndrey V. Elsukov 	rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw;
19330aee131SAndrey V. Elsukov 	rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY;
19430aee131SAndrey V. Elsukov 	rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | (
19530aee131SAndrey V. Elsukov 	    type == RTM_ADD ? RTF_UP: 0), 0, RT_DEFAULT_FIB);
19630aee131SAndrey V. Elsukov }
19730aee131SAndrey V. Elsukov 
19882cd038dSYoshinobu Inoue void
1991272577eSXin LI nd6_init(void)
20082cd038dSYoshinobu Inoue {
20182cd038dSYoshinobu Inoue 
20244e33a07SMarko Zec 	LIST_INIT(&V_nd_prefix);
20344e33a07SMarko Zec 
204686cdd19SJun-ichiro itojun Hagino 	/* initialization of the default router list */
205603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd_defrouter);
20682cea7e6SBjoern A. Zeeb 
20782cd038dSYoshinobu Inoue 	/* start timer */
208603724d3SBjoern A. Zeeb 	callout_init(&V_nd6_slowtimo_ch, 0);
209603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
210f6dfe47aSMarko Zec 	    nd6_slowtimo, curvnet);
211d6ad6a86SMark Johnston 
212d6ad6a86SMark Johnston 	nd6_dad_init();
21330aee131SAndrey V. Elsukov 	if (IS_DEFAULT_VNET(curvnet))
21430aee131SAndrey V. Elsukov 		lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
21530aee131SAndrey V. Elsukov 		    NULL, EVENTHANDLER_PRI_ANY);
21682cd038dSYoshinobu Inoue }
21782cd038dSYoshinobu Inoue 
218bc29160dSMarko Zec #ifdef VIMAGE
219bc29160dSMarko Zec void
220bc29160dSMarko Zec nd6_destroy()
221bc29160dSMarko Zec {
222bc29160dSMarko Zec 
223bc29160dSMarko Zec 	callout_drain(&V_nd6_slowtimo_ch);
224bc29160dSMarko Zec 	callout_drain(&V_nd6_timer_ch);
22530aee131SAndrey V. Elsukov 	if (IS_DEFAULT_VNET(curvnet))
22630aee131SAndrey V. Elsukov 		EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
227bc29160dSMarko Zec }
228bc29160dSMarko Zec #endif
229bc29160dSMarko Zec 
23031b1bfe1SHajimu UMEMOTO struct nd_ifinfo *
2311272577eSXin LI nd6_ifattach(struct ifnet *ifp)
23282cd038dSYoshinobu Inoue {
23331b1bfe1SHajimu UMEMOTO 	struct nd_ifinfo *nd;
23482cd038dSYoshinobu Inoue 
23539e19560SBjoern A. Zeeb 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
23631b1bfe1SHajimu UMEMOTO 	nd->initialized = 1;
23782cd038dSYoshinobu Inoue 
23831b1bfe1SHajimu UMEMOTO 	nd->chlim = IPV6_DEFHLIM;
23931b1bfe1SHajimu UMEMOTO 	nd->basereachable = REACHABLE_TIME;
24031b1bfe1SHajimu UMEMOTO 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
24131b1bfe1SHajimu UMEMOTO 	nd->retrans = RETRANS_TIMER;
242a283298cSHiroki Sato 
243a283298cSHiroki Sato 	nd->flags = ND6_IFF_PERFORMNUD;
244a283298cSHiroki Sato 
245af805644SHiroki Sato 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
246af805644SHiroki Sato 	 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
247af805644SHiroki Sato 	 * default regardless of the V_ip6_auto_linklocal configuration to
248af805644SHiroki Sato 	 * give a reasonable default behavior.
249af805644SHiroki Sato 	 */
250af805644SHiroki Sato 	if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
251af805644SHiroki Sato 	    (ifp->if_flags & IFF_LOOPBACK))
252a283298cSHiroki Sato 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
253af805644SHiroki Sato 	/*
254af805644SHiroki Sato 	 * A loopback interface does not need to accept RTADV.
255af805644SHiroki Sato 	 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
256af805644SHiroki Sato 	 * default regardless of the V_ip6_accept_rtadv configuration to
257af805644SHiroki Sato 	 * prevent the interface from accepting RA messages arrived
258af805644SHiroki Sato 	 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
259af805644SHiroki Sato 	 */
260af805644SHiroki Sato 	if (V_ip6_accept_rtadv &&
261af805644SHiroki Sato 	    !(ifp->if_flags & IFF_LOOPBACK) &&
262af805644SHiroki Sato 	    (ifp->if_type != IFT_BRIDGE))
263a283298cSHiroki Sato 			nd->flags |= ND6_IFF_ACCEPT_RTADV;
264e7fa8d0aSHiroki Sato 	if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
265e7fa8d0aSHiroki Sato 		nd->flags |= ND6_IFF_NO_RADR;
26631b1bfe1SHajimu UMEMOTO 
26731b1bfe1SHajimu UMEMOTO 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
26831b1bfe1SHajimu UMEMOTO 	nd6_setmtu0(ifp, nd);
26931b1bfe1SHajimu UMEMOTO 
27031b1bfe1SHajimu UMEMOTO 	return nd;
27131b1bfe1SHajimu UMEMOTO }
27231b1bfe1SHajimu UMEMOTO 
27331b1bfe1SHajimu UMEMOTO void
2741272577eSXin LI nd6_ifdetach(struct nd_ifinfo *nd)
27531b1bfe1SHajimu UMEMOTO {
27631b1bfe1SHajimu UMEMOTO 
27731b1bfe1SHajimu UMEMOTO 	free(nd, M_IP6NDP);
27882cd038dSYoshinobu Inoue }
27982cd038dSYoshinobu Inoue 
28082cd038dSYoshinobu Inoue /*
28182cd038dSYoshinobu Inoue  * Reset ND level link MTU. This function is called when the physical MTU
28282cd038dSYoshinobu Inoue  * changes, which means we might have to adjust the ND level MTU.
28382cd038dSYoshinobu Inoue  */
28482cd038dSYoshinobu Inoue void
2851272577eSXin LI nd6_setmtu(struct ifnet *ifp)
28682cd038dSYoshinobu Inoue {
28731b1bfe1SHajimu UMEMOTO 
28831b1bfe1SHajimu UMEMOTO 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
28931b1bfe1SHajimu UMEMOTO }
29031b1bfe1SHajimu UMEMOTO 
29131b1bfe1SHajimu UMEMOTO /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
29231b1bfe1SHajimu UMEMOTO void
2931272577eSXin LI nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
29431b1bfe1SHajimu UMEMOTO {
29531b3783cSHajimu UMEMOTO 	u_int32_t omaxmtu;
29631b1bfe1SHajimu UMEMOTO 
29731b3783cSHajimu UMEMOTO 	omaxmtu = ndi->maxmtu;
29882cd038dSYoshinobu Inoue 
29982cd038dSYoshinobu Inoue 	switch (ifp->if_type) {
30031b3783cSHajimu UMEMOTO 	case IFT_ARCNET:
30131b3783cSHajimu UMEMOTO 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
30282cd038dSYoshinobu Inoue 		break;
30382cd038dSYoshinobu Inoue 	case IFT_FDDI:
30431b3783cSHajimu UMEMOTO 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
30582cd038dSYoshinobu Inoue 		break;
3062049fdeeSMatthew N. Dodd 	case IFT_ISO88025:
3072049fdeeSMatthew N. Dodd 		 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
3082049fdeeSMatthew N. Dodd 		 break;
30982cd038dSYoshinobu Inoue 	default:
31082cd038dSYoshinobu Inoue 		ndi->maxmtu = ifp->if_mtu;
31182cd038dSYoshinobu Inoue 		break;
31282cd038dSYoshinobu Inoue 	}
31382cd038dSYoshinobu Inoue 
31482cd038dSYoshinobu Inoue 	/*
31531b3783cSHajimu UMEMOTO 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
31631b3783cSHajimu UMEMOTO 	 * undesirable situation.  We thus notify the operator of the change
31731b3783cSHajimu UMEMOTO 	 * explicitly.  The check for omaxmtu is necessary to restrict the
31831b3783cSHajimu UMEMOTO 	 * log to the case of changing the MTU, not initializing it.
31982cd038dSYoshinobu Inoue 	 */
32031b3783cSHajimu UMEMOTO 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
32131b3783cSHajimu UMEMOTO 		log(LOG_NOTICE, "nd6_setmtu0: "
32231b3783cSHajimu UMEMOTO 		    "new link MTU on %s (%lu) is too small for IPv6\n",
32331b3783cSHajimu UMEMOTO 		    if_name(ifp), (unsigned long)ndi->maxmtu);
32482cd038dSYoshinobu Inoue 	}
32531b3783cSHajimu UMEMOTO 
326603724d3SBjoern A. Zeeb 	if (ndi->maxmtu > V_in6_maxmtu)
32731b3783cSHajimu UMEMOTO 		in6_setmaxmtu(); /* check all interfaces just in case */
32831b3783cSHajimu UMEMOTO 
32982cd038dSYoshinobu Inoue }
33082cd038dSYoshinobu Inoue 
33182cd038dSYoshinobu Inoue void
3321272577eSXin LI nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
33382cd038dSYoshinobu Inoue {
33407eb2995SHajimu UMEMOTO 
33582cd038dSYoshinobu Inoue 	bzero(ndopts, sizeof(*ndopts));
33682cd038dSYoshinobu Inoue 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
33782cd038dSYoshinobu Inoue 	ndopts->nd_opts_last
33882cd038dSYoshinobu Inoue 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
33982cd038dSYoshinobu Inoue 
34082cd038dSYoshinobu Inoue 	if (icmp6len == 0) {
34182cd038dSYoshinobu Inoue 		ndopts->nd_opts_done = 1;
34282cd038dSYoshinobu Inoue 		ndopts->nd_opts_search = NULL;
34382cd038dSYoshinobu Inoue 	}
34482cd038dSYoshinobu Inoue }
34582cd038dSYoshinobu Inoue 
34682cd038dSYoshinobu Inoue /*
34782cd038dSYoshinobu Inoue  * Take one ND option.
34882cd038dSYoshinobu Inoue  */
34982cd038dSYoshinobu Inoue struct nd_opt_hdr *
3501272577eSXin LI nd6_option(union nd_opts *ndopts)
35182cd038dSYoshinobu Inoue {
35282cd038dSYoshinobu Inoue 	struct nd_opt_hdr *nd_opt;
35382cd038dSYoshinobu Inoue 	int olen;
35482cd038dSYoshinobu Inoue 
35539e19560SBjoern A. Zeeb 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
35639e19560SBjoern A. Zeeb 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
35739e19560SBjoern A. Zeeb 	    __func__));
3582ce62dceSSUZUKI Shinsuke 	if (ndopts->nd_opts_search == NULL)
35982cd038dSYoshinobu Inoue 		return NULL;
36082cd038dSYoshinobu Inoue 	if (ndopts->nd_opts_done)
36182cd038dSYoshinobu Inoue 		return NULL;
36282cd038dSYoshinobu Inoue 
36382cd038dSYoshinobu Inoue 	nd_opt = ndopts->nd_opts_search;
36482cd038dSYoshinobu Inoue 
36519391949SKris Kennaway 	/* make sure nd_opt_len is inside the buffer */
36619391949SKris Kennaway 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
36719391949SKris Kennaway 		bzero(ndopts, sizeof(*ndopts));
36819391949SKris Kennaway 		return NULL;
36919391949SKris Kennaway 	}
37019391949SKris Kennaway 
37182cd038dSYoshinobu Inoue 	olen = nd_opt->nd_opt_len << 3;
37282cd038dSYoshinobu Inoue 	if (olen == 0) {
37382cd038dSYoshinobu Inoue 		/*
37482cd038dSYoshinobu Inoue 		 * Message validation requires that all included
37582cd038dSYoshinobu Inoue 		 * options have a length that is greater than zero.
37682cd038dSYoshinobu Inoue 		 */
37782cd038dSYoshinobu Inoue 		bzero(ndopts, sizeof(*ndopts));
37882cd038dSYoshinobu Inoue 		return NULL;
37982cd038dSYoshinobu Inoue 	}
38082cd038dSYoshinobu Inoue 
38182cd038dSYoshinobu Inoue 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
38219391949SKris Kennaway 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
38319391949SKris Kennaway 		/* option overruns the end of buffer, invalid */
38419391949SKris Kennaway 		bzero(ndopts, sizeof(*ndopts));
38519391949SKris Kennaway 		return NULL;
38619391949SKris Kennaway 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
38719391949SKris Kennaway 		/* reached the end of options chain */
38882cd038dSYoshinobu Inoue 		ndopts->nd_opts_done = 1;
38982cd038dSYoshinobu Inoue 		ndopts->nd_opts_search = NULL;
39082cd038dSYoshinobu Inoue 	}
39182cd038dSYoshinobu Inoue 	return nd_opt;
39282cd038dSYoshinobu Inoue }
39382cd038dSYoshinobu Inoue 
39482cd038dSYoshinobu Inoue /*
39582cd038dSYoshinobu Inoue  * Parse multiple ND options.
39682cd038dSYoshinobu Inoue  * This function is much easier to use, for ND routines that do not need
39782cd038dSYoshinobu Inoue  * multiple options of the same type.
39882cd038dSYoshinobu Inoue  */
39982cd038dSYoshinobu Inoue int
4001272577eSXin LI nd6_options(union nd_opts *ndopts)
40182cd038dSYoshinobu Inoue {
40282cd038dSYoshinobu Inoue 	struct nd_opt_hdr *nd_opt;
40382cd038dSYoshinobu Inoue 	int i = 0;
40482cd038dSYoshinobu Inoue 
40539e19560SBjoern A. Zeeb 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
40639e19560SBjoern A. Zeeb 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
40739e19560SBjoern A. Zeeb 	    __func__));
4082ce62dceSSUZUKI Shinsuke 	if (ndopts->nd_opts_search == NULL)
40982cd038dSYoshinobu Inoue 		return 0;
41082cd038dSYoshinobu Inoue 
41182cd038dSYoshinobu Inoue 	while (1) {
41282cd038dSYoshinobu Inoue 		nd_opt = nd6_option(ndopts);
4132ce62dceSSUZUKI Shinsuke 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
41482cd038dSYoshinobu Inoue 			/*
41582cd038dSYoshinobu Inoue 			 * Message validation requires that all included
41682cd038dSYoshinobu Inoue 			 * options have a length that is greater than zero.
41782cd038dSYoshinobu Inoue 			 */
418e27b0c87SRobert Watson 			ICMP6STAT_INC(icp6s_nd_badopt);
41982cd038dSYoshinobu Inoue 			bzero(ndopts, sizeof(*ndopts));
42082cd038dSYoshinobu Inoue 			return -1;
42182cd038dSYoshinobu Inoue 		}
42282cd038dSYoshinobu Inoue 
4232ce62dceSSUZUKI Shinsuke 		if (nd_opt == NULL)
42482cd038dSYoshinobu Inoue 			goto skip1;
42582cd038dSYoshinobu Inoue 
42682cd038dSYoshinobu Inoue 		switch (nd_opt->nd_opt_type) {
42782cd038dSYoshinobu Inoue 		case ND_OPT_SOURCE_LINKADDR:
42882cd038dSYoshinobu Inoue 		case ND_OPT_TARGET_LINKADDR:
42982cd038dSYoshinobu Inoue 		case ND_OPT_MTU:
43082cd038dSYoshinobu Inoue 		case ND_OPT_REDIRECTED_HEADER:
43111d8451dSHiroki Sato 		case ND_OPT_NONCE:
43282cd038dSYoshinobu Inoue 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
43333841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
43433841545SHajimu UMEMOTO 				    "duplicated ND6 option found (type=%d)\n",
43533841545SHajimu UMEMOTO 				    nd_opt->nd_opt_type));
43682cd038dSYoshinobu Inoue 				/* XXX bark? */
43782cd038dSYoshinobu Inoue 			} else {
43882cd038dSYoshinobu Inoue 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
43982cd038dSYoshinobu Inoue 					= nd_opt;
44082cd038dSYoshinobu Inoue 			}
44182cd038dSYoshinobu Inoue 			break;
44282cd038dSYoshinobu Inoue 		case ND_OPT_PREFIX_INFORMATION:
44382cd038dSYoshinobu Inoue 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
44482cd038dSYoshinobu Inoue 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
44582cd038dSYoshinobu Inoue 					= nd_opt;
44682cd038dSYoshinobu Inoue 			}
44782cd038dSYoshinobu Inoue 			ndopts->nd_opts_pi_end =
44882cd038dSYoshinobu Inoue 				(struct nd_opt_prefix_info *)nd_opt;
44982cd038dSYoshinobu Inoue 			break;
450abbe8356SBjoern A. Zeeb 		/* What about ND_OPT_ROUTE_INFO? RFC 4191 */
451abbe8356SBjoern A. Zeeb 		case ND_OPT_RDNSS:	/* RFC 6106 */
452abbe8356SBjoern A. Zeeb 		case ND_OPT_DNSSL:	/* RFC 6106 */
453abbe8356SBjoern A. Zeeb 			/*
454abbe8356SBjoern A. Zeeb 			 * Silently ignore options we know and do not care about
455abbe8356SBjoern A. Zeeb 			 * in the kernel.
456abbe8356SBjoern A. Zeeb 			 */
457abbe8356SBjoern A. Zeeb 			break;
45882cd038dSYoshinobu Inoue 		default:
45982cd038dSYoshinobu Inoue 			/*
46082cd038dSYoshinobu Inoue 			 * Unknown options must be silently ignored,
46182cd038dSYoshinobu Inoue 			 * to accomodate future extension to the protocol.
46282cd038dSYoshinobu Inoue 			 */
46333841545SHajimu UMEMOTO 			nd6log((LOG_DEBUG,
46482cd038dSYoshinobu Inoue 			    "nd6_options: unsupported option %d - "
46533841545SHajimu UMEMOTO 			    "option ignored\n", nd_opt->nd_opt_type));
46682cd038dSYoshinobu Inoue 		}
46782cd038dSYoshinobu Inoue 
46882cd038dSYoshinobu Inoue skip1:
46982cd038dSYoshinobu Inoue 		i++;
470603724d3SBjoern A. Zeeb 		if (i > V_nd6_maxndopt) {
471e27b0c87SRobert Watson 			ICMP6STAT_INC(icp6s_nd_toomanyopt);
47233841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
47382cd038dSYoshinobu Inoue 			break;
47482cd038dSYoshinobu Inoue 		}
47582cd038dSYoshinobu Inoue 
47682cd038dSYoshinobu Inoue 		if (ndopts->nd_opts_done)
47782cd038dSYoshinobu Inoue 			break;
47882cd038dSYoshinobu Inoue 	}
47982cd038dSYoshinobu Inoue 
48082cd038dSYoshinobu Inoue 	return 0;
48182cd038dSYoshinobu Inoue }
48282cd038dSYoshinobu Inoue 
48382cd038dSYoshinobu Inoue /*
484743eee66SSUZUKI Shinsuke  * ND6 timer routine to handle ND6 entries
48582cd038dSYoshinobu Inoue  */
48682cd038dSYoshinobu Inoue void
4876e6b3f7cSQing Li nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
488743eee66SSUZUKI Shinsuke {
489ef8d671cSHajimu UMEMOTO 	int canceled;
490ef8d671cSHajimu UMEMOTO 
491e6950476SBjoern A. Zeeb 	LLE_WLOCK_ASSERT(ln);
492e6950476SBjoern A. Zeeb 
493743eee66SSUZUKI Shinsuke 	if (tick < 0) {
4946e6b3f7cSQing Li 		ln->la_expire = 0;
495743eee66SSUZUKI Shinsuke 		ln->ln_ntick = 0;
4960447c136SAlexander V. Chernikov 		canceled = callout_stop(&ln->lle_timer);
497743eee66SSUZUKI Shinsuke 	} else {
4987d26db17SHiroki Sato 		ln->la_expire = time_uptime + tick / hz;
4996e6b3f7cSQing Li 		LLE_ADDREF(ln);
500743eee66SSUZUKI Shinsuke 		if (tick > INT_MAX) {
501743eee66SSUZUKI Shinsuke 			ln->ln_ntick = tick - INT_MAX;
5020447c136SAlexander V. Chernikov 			canceled = callout_reset(&ln->lle_timer, INT_MAX,
503743eee66SSUZUKI Shinsuke 			    nd6_llinfo_timer, ln);
504743eee66SSUZUKI Shinsuke 		} else {
505743eee66SSUZUKI Shinsuke 			ln->ln_ntick = 0;
5060447c136SAlexander V. Chernikov 			canceled = callout_reset(&ln->lle_timer, tick,
507743eee66SSUZUKI Shinsuke 			    nd6_llinfo_timer, ln);
508743eee66SSUZUKI Shinsuke 		}
509743eee66SSUZUKI Shinsuke 	}
510ef8d671cSHajimu UMEMOTO 	if (canceled)
511ef8d671cSHajimu UMEMOTO 		LLE_REMREF(ln);
512743eee66SSUZUKI Shinsuke }
51382cd038dSYoshinobu Inoue 
51426deb882SAlexander V. Chernikov /*
51526deb882SAlexander V. Chernikov * Gets source address of the first packet in hold queue
51626deb882SAlexander V. Chernikov * and stores it in @src.
51726deb882SAlexander V. Chernikov * Returns pointer to @src (if hold queue is not empty) or NULL.
51826deb882SAlexander V. Chernikov *
51926deb882SAlexander V. Chernikov */
52026deb882SAlexander V. Chernikov static struct in6_addr *
52126deb882SAlexander V. Chernikov nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
52226deb882SAlexander V. Chernikov {
52326deb882SAlexander V. Chernikov 	struct ip6_hdr hdr;
52426deb882SAlexander V. Chernikov 	struct mbuf *m;
52526deb882SAlexander V. Chernikov 
52626deb882SAlexander V. Chernikov 	if (ln->la_hold == NULL)
52726deb882SAlexander V. Chernikov 		return (NULL);
52826deb882SAlexander V. Chernikov 
52926deb882SAlexander V. Chernikov 	/*
53026deb882SAlexander V. Chernikov 	 * assume every packet in la_hold has the same IP header
53126deb882SAlexander V. Chernikov 	 */
53226deb882SAlexander V. Chernikov 	m = ln->la_hold;
53326deb882SAlexander V. Chernikov 	if (sizeof(hdr) < m->m_len)
53426deb882SAlexander V. Chernikov 		return (NULL);
53526deb882SAlexander V. Chernikov 
53626deb882SAlexander V. Chernikov 	m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
53726deb882SAlexander V. Chernikov 	*src = hdr.ip6_src;
53826deb882SAlexander V. Chernikov 
53926deb882SAlexander V. Chernikov 	return (src);
54026deb882SAlexander V. Chernikov }
54126deb882SAlexander V. Chernikov 
5426e6b3f7cSQing Li void
5436e6b3f7cSQing Li nd6_llinfo_settimer(struct llentry *ln, long tick)
5446e6b3f7cSQing Li {
5456e6b3f7cSQing Li 
5466e6b3f7cSQing Li 	LLE_WLOCK(ln);
5476e6b3f7cSQing Li 	nd6_llinfo_settimer_locked(ln, tick);
5486e6b3f7cSQing Li 	LLE_WUNLOCK(ln);
5496e6b3f7cSQing Li }
5506e6b3f7cSQing Li 
551743eee66SSUZUKI Shinsuke static void
5521272577eSXin LI nd6_llinfo_timer(void *arg)
553743eee66SSUZUKI Shinsuke {
5546e6b3f7cSQing Li 	struct llentry *ln;
55526deb882SAlexander V. Chernikov 	struct in6_addr *dst, *pdst, *psrc, src;
556743eee66SSUZUKI Shinsuke 	struct ifnet *ifp;
557686cdd19SJun-ichiro itojun Hagino 	struct nd_ifinfo *ndi = NULL;
55826deb882SAlexander V. Chernikov 	int send_ns;
55982cd038dSYoshinobu Inoue 
56019291ab3SBjoern A. Zeeb 	KASSERT(arg != NULL, ("%s: arg NULL", __func__));
5616e6b3f7cSQing Li 	ln = (struct llentry *)arg;
5622575fbb8SRandall Stewart 	LLE_WLOCK(ln);
5630447c136SAlexander V. Chernikov 	if (callout_pending(&ln->lle_timer)) {
5642575fbb8SRandall Stewart 		/*
5652575fbb8SRandall Stewart 		 * Here we are a bit odd here in the treatment of
5662575fbb8SRandall Stewart 		 * active/pending. If the pending bit is set, it got
5672575fbb8SRandall Stewart 		 * rescheduled before I ran. The active
5682575fbb8SRandall Stewart 		 * bit we ignore, since if it was stopped
5692575fbb8SRandall Stewart 		 * in ll_tablefree() and was currently running
5702575fbb8SRandall Stewart 		 * it would have return 0 so the code would
5712575fbb8SRandall Stewart 		 * not have deleted it since the callout could
5722575fbb8SRandall Stewart 		 * not be stopped so we want to go through
5732575fbb8SRandall Stewart 		 * with the delete here now. If the callout
5742575fbb8SRandall Stewart 		 * was restarted, the pending bit will be back on and
5752575fbb8SRandall Stewart 		 * we just want to bail since the callout_reset would
5762575fbb8SRandall Stewart 		 * return 1 and our reference would have been removed
5772575fbb8SRandall Stewart 		 * by nd6_llinfo_settimer_locked above since canceled
5782575fbb8SRandall Stewart 		 * would have been 1.
5792575fbb8SRandall Stewart 		 */
5802575fbb8SRandall Stewart 		LLE_WUNLOCK(ln);
5812575fbb8SRandall Stewart 		return;
5822575fbb8SRandall Stewart 	}
58319291ab3SBjoern A. Zeeb 	ifp = ln->lle_tbl->llt_ifp;
5846e6b3f7cSQing Li 	CURVNET_SET(ifp->if_vnet);
58526deb882SAlexander V. Chernikov 	ndi = ND_IFINFO(ifp);
58626deb882SAlexander V. Chernikov 	send_ns = 0;
58726deb882SAlexander V. Chernikov 	dst = &ln->r_l3addr.addr6;
58826deb882SAlexander V. Chernikov 	pdst = dst;
58982cd038dSYoshinobu Inoue 
590743eee66SSUZUKI Shinsuke 	if (ln->ln_ntick > 0) {
591743eee66SSUZUKI Shinsuke 		if (ln->ln_ntick > INT_MAX) {
592743eee66SSUZUKI Shinsuke 			ln->ln_ntick -= INT_MAX;
593e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, INT_MAX);
594743eee66SSUZUKI Shinsuke 		} else {
595743eee66SSUZUKI Shinsuke 			ln->ln_ntick = 0;
596e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
59782cd038dSYoshinobu Inoue 		}
5986e6b3f7cSQing Li 		goto done;
599743eee66SSUZUKI Shinsuke 	}
600743eee66SSUZUKI Shinsuke 
601287e3cb4SHajimu UMEMOTO 	if (ln->la_flags & LLE_STATIC) {
6026e6b3f7cSQing Li 		goto done;
6036e6b3f7cSQing Li 	}
60482cd038dSYoshinobu Inoue 
6056e6b3f7cSQing Li 	if (ln->la_flags & LLE_DELETED) {
6066e6b3f7cSQing Li 		(void)nd6_free(ln, 0);
6072eb10edcSHajimu UMEMOTO 		ln = NULL;
6086e6b3f7cSQing Li 		goto done;
6096e6b3f7cSQing Li 	}
61082cd038dSYoshinobu Inoue 
61182cd038dSYoshinobu Inoue 	switch (ln->ln_state) {
61282cd038dSYoshinobu Inoue 	case ND6_LLINFO_INCOMPLETE:
6136e6b3f7cSQing Li 		if (ln->la_asked < V_nd6_mmaxtries) {
6146e6b3f7cSQing Li 			ln->la_asked++;
61526deb882SAlexander V. Chernikov 			send_ns = 1;
61626deb882SAlexander V. Chernikov 			/* Send NS to multicast address */
61726deb882SAlexander V. Chernikov 			pdst = NULL;
61882cd038dSYoshinobu Inoue 		} else {
6196e6b3f7cSQing Li 			struct mbuf *m = ln->la_hold;
62082cd038dSYoshinobu Inoue 			if (m) {
62131d4137bSSUZUKI Shinsuke 				struct mbuf *m0;
62231d4137bSSUZUKI Shinsuke 
62382cd038dSYoshinobu Inoue 				/*
6246e6b3f7cSQing Li 				 * assuming every packet in la_hold has the
625e6950476SBjoern A. Zeeb 				 * same IP header.  Send error after unlock.
62682cd038dSYoshinobu Inoue 				 */
62731d4137bSSUZUKI Shinsuke 				m0 = m->m_nextpkt;
62831d4137bSSUZUKI Shinsuke 				m->m_nextpkt = NULL;
6296e6b3f7cSQing Li 				ln->la_hold = m0;
63031d4137bSSUZUKI Shinsuke 				clear_llinfo_pqueue(ln);
63182cd038dSYoshinobu Inoue 			}
63263a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_TIMEDOUT);
6336e6b3f7cSQing Li 			(void)nd6_free(ln, 0);
634743eee66SSUZUKI Shinsuke 			ln = NULL;
635e6950476SBjoern A. Zeeb 			if (m != NULL)
636e6950476SBjoern A. Zeeb 				icmp6_error2(m, ICMP6_DST_UNREACH,
637e6950476SBjoern A. Zeeb 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
63882cd038dSYoshinobu Inoue 		}
63982cd038dSYoshinobu Inoue 		break;
64082cd038dSYoshinobu Inoue 	case ND6_LLINFO_REACHABLE:
641743eee66SSUZUKI Shinsuke 		if (!ND6_LLINFO_PERMANENT(ln)) {
64282cd038dSYoshinobu Inoue 			ln->ln_state = ND6_LLINFO_STALE;
643e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
64433841545SHajimu UMEMOTO 		}
64582cd038dSYoshinobu Inoue 		break;
64633841545SHajimu UMEMOTO 
64733841545SHajimu UMEMOTO 	case ND6_LLINFO_STALE:
64833841545SHajimu UMEMOTO 		/* Garbage Collection(RFC 2461 5.3) */
649743eee66SSUZUKI Shinsuke 		if (!ND6_LLINFO_PERMANENT(ln)) {
65063a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
6516e6b3f7cSQing Li 			(void)nd6_free(ln, 1);
652743eee66SSUZUKI Shinsuke 			ln = NULL;
653743eee66SSUZUKI Shinsuke 		}
65433841545SHajimu UMEMOTO 		break;
65533841545SHajimu UMEMOTO 
65682cd038dSYoshinobu Inoue 	case ND6_LLINFO_DELAY:
657686cdd19SJun-ichiro itojun Hagino 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
658686cdd19SJun-ichiro itojun Hagino 			/* We need NUD */
6596e6b3f7cSQing Li 			ln->la_asked = 1;
66082cd038dSYoshinobu Inoue 			ln->ln_state = ND6_LLINFO_PROBE;
66126deb882SAlexander V. Chernikov 			send_ns = 1;
66233841545SHajimu UMEMOTO 		} else {
663686cdd19SJun-ichiro itojun Hagino 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
664e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
66533841545SHajimu UMEMOTO 		}
66682cd038dSYoshinobu Inoue 		break;
66782cd038dSYoshinobu Inoue 	case ND6_LLINFO_PROBE:
6686e6b3f7cSQing Li 		if (ln->la_asked < V_nd6_umaxtries) {
6696e6b3f7cSQing Li 			ln->la_asked++;
67026deb882SAlexander V. Chernikov 			send_ns = 1;
67182cd038dSYoshinobu Inoue 		} else {
67263a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
6736e6b3f7cSQing Li 			(void)nd6_free(ln, 0);
674743eee66SSUZUKI Shinsuke 			ln = NULL;
67582cd038dSYoshinobu Inoue 		}
67682cd038dSYoshinobu Inoue 		break;
677e6950476SBjoern A. Zeeb 	default:
678e6950476SBjoern A. Zeeb 		panic("%s: paths in a dark night can be confusing: %d",
679e6950476SBjoern A. Zeeb 		    __func__, ln->ln_state);
68082cd038dSYoshinobu Inoue 	}
6816e6b3f7cSQing Li done:
68226deb882SAlexander V. Chernikov 	if (send_ns != 0) {
68326deb882SAlexander V. Chernikov 		nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
68426deb882SAlexander V. Chernikov 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
68526deb882SAlexander V. Chernikov 		LLE_FREE_LOCKED(ln);
68626deb882SAlexander V. Chernikov 		ln = NULL;
68726deb882SAlexander V. Chernikov 		nd6_ns_output(ifp, psrc, pdst, dst, NULL);
68826deb882SAlexander V. Chernikov 	}
68926deb882SAlexander V. Chernikov 
6906e6b3f7cSQing Li 	if (ln != NULL)
691e6950476SBjoern A. Zeeb 		LLE_FREE_LOCKED(ln);
692f0c0b143SHajimu UMEMOTO 	CURVNET_RESTORE();
69382cd038dSYoshinobu Inoue }
69482cd038dSYoshinobu Inoue 
695743eee66SSUZUKI Shinsuke 
696743eee66SSUZUKI Shinsuke /*
697743eee66SSUZUKI Shinsuke  * ND6 timer routine to expire default route list and prefix list
698743eee66SSUZUKI Shinsuke  */
699743eee66SSUZUKI Shinsuke void
7008b615593SMarko Zec nd6_timer(void *arg)
701743eee66SSUZUKI Shinsuke {
70221ca7b57SMarko Zec 	CURVNET_SET((struct vnet *) arg);
7033b0b2840SJohn Baldwin 	struct nd_defrouter *dr, *ndr;
7043b0b2840SJohn Baldwin 	struct nd_prefix *pr, *npr;
705743eee66SSUZUKI Shinsuke 	struct in6_ifaddr *ia6, *nia6;
706743eee66SSUZUKI Shinsuke 
707603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
708f6dfe47aSMarko Zec 	    nd6_timer, curvnet);
709743eee66SSUZUKI Shinsuke 
71033841545SHajimu UMEMOTO 	/* expire default router list */
7113b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
7127d26db17SHiroki Sato 		if (dr->expire && dr->expire < time_uptime)
71382cd038dSYoshinobu Inoue 			defrtrlist_del(dr);
714686cdd19SJun-ichiro itojun Hagino 	}
71582cd038dSYoshinobu Inoue 
71633841545SHajimu UMEMOTO 	/*
71733841545SHajimu UMEMOTO 	 * expire interface addresses.
71833841545SHajimu UMEMOTO 	 * in the past the loop was inside prefix expiry processing.
71933841545SHajimu UMEMOTO 	 * However, from a stricter speci-confrmance standpoint, we should
72033841545SHajimu UMEMOTO 	 * rather separate address lifetimes and prefix lifetimes.
721d1da0a06SRobert Watson 	 *
722d1da0a06SRobert Watson 	 * XXXRW: in6_ifaddrhead locking.
72333841545SHajimu UMEMOTO 	 */
72433841545SHajimu UMEMOTO   addrloop:
72580af0152SRobert Watson 	TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
72682cd038dSYoshinobu Inoue 		/* check address lifetime */
72733841545SHajimu UMEMOTO 		if (IFA6_IS_INVALID(ia6)) {
72833841545SHajimu UMEMOTO 			int regen = 0;
72933841545SHajimu UMEMOTO 
73033841545SHajimu UMEMOTO 			/*
73133841545SHajimu UMEMOTO 			 * If the expiring address is temporary, try
73233841545SHajimu UMEMOTO 			 * regenerating a new one.  This would be useful when
73388ff5695SSUZUKI Shinsuke 			 * we suspended a laptop PC, then turned it on after a
73433841545SHajimu UMEMOTO 			 * period that could invalidate all temporary
73533841545SHajimu UMEMOTO 			 * addresses.  Although we may have to restart the
73633841545SHajimu UMEMOTO 			 * loop (see below), it must be after purging the
73733841545SHajimu UMEMOTO 			 * address.  Otherwise, we'd see an infinite loop of
73833841545SHajimu UMEMOTO 			 * regeneration.
73933841545SHajimu UMEMOTO 			 */
740603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr &&
74133841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
74233841545SHajimu UMEMOTO 				if (regen_tmpaddr(ia6) == 0)
74333841545SHajimu UMEMOTO 					regen = 1;
74433841545SHajimu UMEMOTO 			}
74533841545SHajimu UMEMOTO 
74633841545SHajimu UMEMOTO 			in6_purgeaddr(&ia6->ia_ifa);
74733841545SHajimu UMEMOTO 
74833841545SHajimu UMEMOTO 			if (regen)
74933841545SHajimu UMEMOTO 				goto addrloop; /* XXX: see below */
75043068328SHajimu UMEMOTO 		} else if (IFA6_IS_DEPRECATED(ia6)) {
75133841545SHajimu UMEMOTO 			int oldflags = ia6->ia6_flags;
75233841545SHajimu UMEMOTO 
75382cd038dSYoshinobu Inoue 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
75433841545SHajimu UMEMOTO 
75533841545SHajimu UMEMOTO 			/*
75633841545SHajimu UMEMOTO 			 * If a temporary address has just become deprecated,
75733841545SHajimu UMEMOTO 			 * regenerate a new one if possible.
75833841545SHajimu UMEMOTO 			 */
759603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr &&
76033841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
76133841545SHajimu UMEMOTO 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
76233841545SHajimu UMEMOTO 
76333841545SHajimu UMEMOTO 				if (regen_tmpaddr(ia6) == 0) {
76433841545SHajimu UMEMOTO 					/*
76533841545SHajimu UMEMOTO 					 * A new temporary address is
76633841545SHajimu UMEMOTO 					 * generated.
76733841545SHajimu UMEMOTO 					 * XXX: this means the address chain
76833841545SHajimu UMEMOTO 					 * has changed while we are still in
76933841545SHajimu UMEMOTO 					 * the loop.  Although the change
77033841545SHajimu UMEMOTO 					 * would not cause disaster (because
77188ff5695SSUZUKI Shinsuke 					 * it's not a deletion, but an
77288ff5695SSUZUKI Shinsuke 					 * addition,) we'd rather restart the
77333841545SHajimu UMEMOTO 					 * loop just for safety.  Or does this
77433841545SHajimu UMEMOTO 					 * significantly reduce performance??
77533841545SHajimu UMEMOTO 					 */
77633841545SHajimu UMEMOTO 					goto addrloop;
77733841545SHajimu UMEMOTO 				}
77833841545SHajimu UMEMOTO 			}
77983649f07SHajimu UMEMOTO 		} else {
78033841545SHajimu UMEMOTO 			/*
78133841545SHajimu UMEMOTO 			 * A new RA might have made a deprecated address
78233841545SHajimu UMEMOTO 			 * preferred.
78333841545SHajimu UMEMOTO 			 */
78433841545SHajimu UMEMOTO 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
78582cd038dSYoshinobu Inoue 		}
78682cd038dSYoshinobu Inoue 	}
78782cd038dSYoshinobu Inoue 
78833841545SHajimu UMEMOTO 	/* expire prefix list */
7893b0b2840SJohn Baldwin 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
79082cd038dSYoshinobu Inoue 		/*
79182cd038dSYoshinobu Inoue 		 * check prefix lifetime.
79282cd038dSYoshinobu Inoue 		 * since pltime is just for autoconf, pltime processing for
79382cd038dSYoshinobu Inoue 		 * prefix is not necessary.
79482cd038dSYoshinobu Inoue 		 */
795743eee66SSUZUKI Shinsuke 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
7967d26db17SHiroki Sato 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
79782cd038dSYoshinobu Inoue 
79882cd038dSYoshinobu Inoue 			/*
79982cd038dSYoshinobu Inoue 			 * address expiration and prefix expiration are
80033841545SHajimu UMEMOTO 			 * separate.  NEVER perform in6_purgeaddr here.
80182cd038dSYoshinobu Inoue 			 */
80282cd038dSYoshinobu Inoue 			prelist_remove(pr);
8033b0b2840SJohn Baldwin 		}
80482cd038dSYoshinobu Inoue 	}
8058b615593SMarko Zec 	CURVNET_RESTORE();
80682cd038dSYoshinobu Inoue }
80782cd038dSYoshinobu Inoue 
8081272577eSXin LI /*
8091272577eSXin LI  * ia6 - deprecated/invalidated temporary address
8101272577eSXin LI  */
81133841545SHajimu UMEMOTO static int
8121272577eSXin LI regen_tmpaddr(struct in6_ifaddr *ia6)
81333841545SHajimu UMEMOTO {
81433841545SHajimu UMEMOTO 	struct ifaddr *ifa;
81533841545SHajimu UMEMOTO 	struct ifnet *ifp;
81633841545SHajimu UMEMOTO 	struct in6_ifaddr *public_ifa6 = NULL;
81733841545SHajimu UMEMOTO 
81833841545SHajimu UMEMOTO 	ifp = ia6->ia_ifa.ifa_ifp;
819137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
820c4dd3fe1SRobert Watson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
82133841545SHajimu UMEMOTO 		struct in6_ifaddr *it6;
82233841545SHajimu UMEMOTO 
82333841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
82433841545SHajimu UMEMOTO 			continue;
82533841545SHajimu UMEMOTO 
82633841545SHajimu UMEMOTO 		it6 = (struct in6_ifaddr *)ifa;
82733841545SHajimu UMEMOTO 
82833841545SHajimu UMEMOTO 		/* ignore no autoconf addresses. */
82933841545SHajimu UMEMOTO 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
83033841545SHajimu UMEMOTO 			continue;
83133841545SHajimu UMEMOTO 
83233841545SHajimu UMEMOTO 		/* ignore autoconf addresses with different prefixes. */
83333841545SHajimu UMEMOTO 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
83433841545SHajimu UMEMOTO 			continue;
83533841545SHajimu UMEMOTO 
83633841545SHajimu UMEMOTO 		/*
83733841545SHajimu UMEMOTO 		 * Now we are looking at an autoconf address with the same
83833841545SHajimu UMEMOTO 		 * prefix as ours.  If the address is temporary and is still
83933841545SHajimu UMEMOTO 		 * preferred, do not create another one.  It would be rare, but
84033841545SHajimu UMEMOTO 		 * could happen, for example, when we resume a laptop PC after
84133841545SHajimu UMEMOTO 		 * a long period.
84233841545SHajimu UMEMOTO 		 */
84333841545SHajimu UMEMOTO 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
84433841545SHajimu UMEMOTO 		    !IFA6_IS_DEPRECATED(it6)) {
84533841545SHajimu UMEMOTO 			public_ifa6 = NULL;
84633841545SHajimu UMEMOTO 			break;
84733841545SHajimu UMEMOTO 		}
84833841545SHajimu UMEMOTO 
84933841545SHajimu UMEMOTO 		/*
85033841545SHajimu UMEMOTO 		 * This is a public autoconf address that has the same prefix
85133841545SHajimu UMEMOTO 		 * as ours.  If it is preferred, keep it.  We can't break the
85233841545SHajimu UMEMOTO 		 * loop here, because there may be a still-preferred temporary
85333841545SHajimu UMEMOTO 		 * address with the prefix.
85433841545SHajimu UMEMOTO 		 */
85533841545SHajimu UMEMOTO 		if (!IFA6_IS_DEPRECATED(it6))
85633841545SHajimu UMEMOTO 			public_ifa6 = it6;
857dff78447SMark Johnston 	}
858c1752bcdSQing Li 	if (public_ifa6 != NULL)
859c1752bcdSQing Li 		ifa_ref(&public_ifa6->ia_ifa);
860137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
86133841545SHajimu UMEMOTO 
86233841545SHajimu UMEMOTO 	if (public_ifa6 != NULL) {
86333841545SHajimu UMEMOTO 		int e;
86433841545SHajimu UMEMOTO 
865743eee66SSUZUKI Shinsuke 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
866c1752bcdSQing Li 			ifa_free(&public_ifa6->ia_ifa);
86733841545SHajimu UMEMOTO 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
86833841545SHajimu UMEMOTO 			    " tmp addr,errno=%d\n", e);
86933841545SHajimu UMEMOTO 			return (-1);
87033841545SHajimu UMEMOTO 		}
871c1752bcdSQing Li 		ifa_free(&public_ifa6->ia_ifa);
87233841545SHajimu UMEMOTO 		return (0);
87333841545SHajimu UMEMOTO 	}
87433841545SHajimu UMEMOTO 
87533841545SHajimu UMEMOTO 	return (-1);
87633841545SHajimu UMEMOTO }
87733841545SHajimu UMEMOTO 
878686cdd19SJun-ichiro itojun Hagino /*
879686cdd19SJun-ichiro itojun Hagino  * Nuke neighbor cache/prefix/default router management table, right before
880686cdd19SJun-ichiro itojun Hagino  * ifp goes away.
881686cdd19SJun-ichiro itojun Hagino  */
882686cdd19SJun-ichiro itojun Hagino void
8831272577eSXin LI nd6_purge(struct ifnet *ifp)
884686cdd19SJun-ichiro itojun Hagino {
885743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr, *ndr;
886686cdd19SJun-ichiro itojun Hagino 	struct nd_prefix *pr, *npr;
887686cdd19SJun-ichiro itojun Hagino 
888686cdd19SJun-ichiro itojun Hagino 	/*
889743eee66SSUZUKI Shinsuke 	 * Nuke default router list entries toward ifp.
890743eee66SSUZUKI Shinsuke 	 * We defer removal of default router list entries that is installed
891743eee66SSUZUKI Shinsuke 	 * in the routing table, in order to keep additional side effects as
892743eee66SSUZUKI Shinsuke 	 * small as possible.
893686cdd19SJun-ichiro itojun Hagino 	 */
8943b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
895743eee66SSUZUKI Shinsuke 		if (dr->installed)
896743eee66SSUZUKI Shinsuke 			continue;
897743eee66SSUZUKI Shinsuke 
898686cdd19SJun-ichiro itojun Hagino 		if (dr->ifp == ifp)
899686cdd19SJun-ichiro itojun Hagino 			defrtrlist_del(dr);
900686cdd19SJun-ichiro itojun Hagino 	}
901743eee66SSUZUKI Shinsuke 
9023b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
903743eee66SSUZUKI Shinsuke 		if (!dr->installed)
904743eee66SSUZUKI Shinsuke 			continue;
905743eee66SSUZUKI Shinsuke 
906686cdd19SJun-ichiro itojun Hagino 		if (dr->ifp == ifp)
907686cdd19SJun-ichiro itojun Hagino 			defrtrlist_del(dr);
908686cdd19SJun-ichiro itojun Hagino 	}
909686cdd19SJun-ichiro itojun Hagino 
910686cdd19SJun-ichiro itojun Hagino 	/* Nuke prefix list entries toward ifp */
9113b0b2840SJohn Baldwin 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
912686cdd19SJun-ichiro itojun Hagino 		if (pr->ndpr_ifp == ifp) {
91333841545SHajimu UMEMOTO 			/*
914743eee66SSUZUKI Shinsuke 			 * Because if_detach() does *not* release prefixes
915743eee66SSUZUKI Shinsuke 			 * while purging addresses the reference count will
916743eee66SSUZUKI Shinsuke 			 * still be above zero. We therefore reset it to
917743eee66SSUZUKI Shinsuke 			 * make sure that the prefix really gets purged.
918743eee66SSUZUKI Shinsuke 			 */
919743eee66SSUZUKI Shinsuke 			pr->ndpr_refcnt = 0;
920743eee66SSUZUKI Shinsuke 
921743eee66SSUZUKI Shinsuke 			/*
92233841545SHajimu UMEMOTO 			 * Previously, pr->ndpr_addr is removed as well,
92333841545SHajimu UMEMOTO 			 * but I strongly believe we don't have to do it.
92433841545SHajimu UMEMOTO 			 * nd6_purge() is only called from in6_ifdetach(),
92533841545SHajimu UMEMOTO 			 * which removes all the associated interface addresses
92633841545SHajimu UMEMOTO 			 * by itself.
92733841545SHajimu UMEMOTO 			 * (jinmei@kame.net 20010129)
92833841545SHajimu UMEMOTO 			 */
929686cdd19SJun-ichiro itojun Hagino 			prelist_remove(pr);
930686cdd19SJun-ichiro itojun Hagino 		}
931686cdd19SJun-ichiro itojun Hagino 	}
932686cdd19SJun-ichiro itojun Hagino 
933686cdd19SJun-ichiro itojun Hagino 	/* cancel default outgoing interface setting */
934603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex == ifp->if_index)
935686cdd19SJun-ichiro itojun Hagino 		nd6_setdefaultiface(0);
936686cdd19SJun-ichiro itojun Hagino 
937e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
938a283298cSHiroki Sato 		/* Refresh default router list. */
939686cdd19SJun-ichiro itojun Hagino 		defrouter_select();
9406e6b3f7cSQing Li 	}
9416e6b3f7cSQing Li 
9426e6b3f7cSQing Li 	/* XXXXX
9436e6b3f7cSQing Li 	 * We do not nuke the neighbor cache entries here any more
9446e6b3f7cSQing Li 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
9456e6b3f7cSQing Li 	 * nd6_purge() is invoked by in6_ifdetach() which is called
9466e6b3f7cSQing Li 	 * from if_detach() where everything gets purged. So let
9476e6b3f7cSQing Li 	 * in6_domifdetach() do the actual L2 table purging work.
9486e6b3f7cSQing Li 	 */
9491026ccc4SHajimu UMEMOTO }
950686cdd19SJun-ichiro itojun Hagino 
951686cdd19SJun-ichiro itojun Hagino /*
9526e6b3f7cSQing Li  * the caller acquires and releases the lock on the lltbls
9536e6b3f7cSQing Li  * Returns the llentry locked
954686cdd19SJun-ichiro itojun Hagino  */
9556e6b3f7cSQing Li struct llentry *
9566e6b3f7cSQing Li nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
95782cd038dSYoshinobu Inoue {
95882cd038dSYoshinobu Inoue 	struct sockaddr_in6 sin6;
9596e6b3f7cSQing Li 	struct llentry *ln;
96068352503SBjoern A. Zeeb 	int llflags;
96182cd038dSYoshinobu Inoue 
96282cd038dSYoshinobu Inoue 	bzero(&sin6, sizeof(sin6));
96382cd038dSYoshinobu Inoue 	sin6.sin6_len = sizeof(struct sockaddr_in6);
96482cd038dSYoshinobu Inoue 	sin6.sin6_family = AF_INET6;
96582cd038dSYoshinobu Inoue 	sin6.sin6_addr = *addr6;
966686cdd19SJun-ichiro itojun Hagino 
9676e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
96882cd038dSYoshinobu Inoue 
969b4b1367aSAlexander V. Chernikov 	llflags = (flags & ND6_EXCLUSIVE) ? LLE_EXCLUSIVE : 0;
9706e6b3f7cSQing Li 	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
971b4b1367aSAlexander V. Chernikov 
972b4b1367aSAlexander V. Chernikov 	return (ln);
973b4b1367aSAlexander V. Chernikov }
974b4b1367aSAlexander V. Chernikov 
975b4b1367aSAlexander V. Chernikov struct llentry *
9765a255516SAlexander V. Chernikov nd6_alloc(struct in6_addr *addr6, int flags, struct ifnet *ifp)
977b4b1367aSAlexander V. Chernikov {
978b4b1367aSAlexander V. Chernikov 	struct sockaddr_in6 sin6;
979b4b1367aSAlexander V. Chernikov 	struct llentry *ln;
980b4b1367aSAlexander V. Chernikov 
981b4b1367aSAlexander V. Chernikov 	bzero(&sin6, sizeof(sin6));
982b4b1367aSAlexander V. Chernikov 	sin6.sin6_len = sizeof(struct sockaddr_in6);
983b4b1367aSAlexander V. Chernikov 	sin6.sin6_family = AF_INET6;
984b4b1367aSAlexander V. Chernikov 	sin6.sin6_addr = *addr6;
985b4b1367aSAlexander V. Chernikov 
9865a255516SAlexander V. Chernikov 	ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
987b4b1367aSAlexander V. Chernikov 	if (ln != NULL)
98882cd038dSYoshinobu Inoue 		ln->ln_state = ND6_LLINFO_NOSTATE;
9896e6b3f7cSQing Li 
9906e6b3f7cSQing Li 	return (ln);
99182cd038dSYoshinobu Inoue }
99282cd038dSYoshinobu Inoue 
99382cd038dSYoshinobu Inoue /*
99477b691e0SBrian Feldman  * Test whether a given IPv6 address is a neighbor or not, ignoring
99577b691e0SBrian Feldman  * the actual neighbor cache.  The neighbor cache is ignored in order
99677b691e0SBrian Feldman  * to not reenter the routing code from within itself.
99782cd038dSYoshinobu Inoue  */
99877b691e0SBrian Feldman static int
9991272577eSXin LI nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
100082cd038dSYoshinobu Inoue {
10014208ea14SHajimu UMEMOTO 	struct nd_prefix *pr;
1002743eee66SSUZUKI Shinsuke 	struct ifaddr *dstaddr;
100382cd038dSYoshinobu Inoue 
1004686cdd19SJun-ichiro itojun Hagino 	/*
1005686cdd19SJun-ichiro itojun Hagino 	 * A link-local address is always a neighbor.
10064208ea14SHajimu UMEMOTO 	 * XXX: a link does not necessarily specify a single interface.
1007686cdd19SJun-ichiro itojun Hagino 	 */
1008a1f7e5f8SHajimu UMEMOTO 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1009a1f7e5f8SHajimu UMEMOTO 		struct sockaddr_in6 sin6_copy;
1010a1f7e5f8SHajimu UMEMOTO 		u_int32_t zone;
1011a1f7e5f8SHajimu UMEMOTO 
1012a1f7e5f8SHajimu UMEMOTO 		/*
1013a1f7e5f8SHajimu UMEMOTO 		 * We need sin6_copy since sa6_recoverscope() may modify the
1014a1f7e5f8SHajimu UMEMOTO 		 * content (XXX).
1015a1f7e5f8SHajimu UMEMOTO 		 */
1016a1f7e5f8SHajimu UMEMOTO 		sin6_copy = *addr;
1017a1f7e5f8SHajimu UMEMOTO 		if (sa6_recoverscope(&sin6_copy))
1018a1f7e5f8SHajimu UMEMOTO 			return (0); /* XXX: should be impossible */
1019a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1020a1f7e5f8SHajimu UMEMOTO 			return (0);
1021a1f7e5f8SHajimu UMEMOTO 		if (sin6_copy.sin6_scope_id == zone)
102282cd038dSYoshinobu Inoue 			return (1);
1023a1f7e5f8SHajimu UMEMOTO 		else
1024a1f7e5f8SHajimu UMEMOTO 			return (0);
1025a1f7e5f8SHajimu UMEMOTO 	}
102682cd038dSYoshinobu Inoue 
102782cd038dSYoshinobu Inoue 	/*
102882cd038dSYoshinobu Inoue 	 * If the address matches one of our addresses,
102982cd038dSYoshinobu Inoue 	 * it should be a neighbor.
10304208ea14SHajimu UMEMOTO 	 * If the address matches one of our on-link prefixes, it should be a
10314208ea14SHajimu UMEMOTO 	 * neighbor.
103282cd038dSYoshinobu Inoue 	 */
10333b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
10344208ea14SHajimu UMEMOTO 		if (pr->ndpr_ifp != ifp)
10354208ea14SHajimu UMEMOTO 			continue;
103682cd038dSYoshinobu Inoue 
1037baf7c373SQing Li 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1038baf7c373SQing Li 			struct rtentry *rt;
103981d5d46bSBjoern A. Zeeb 
104081d5d46bSBjoern A. Zeeb 			/* Always use the default FIB here. */
104181d5d46bSBjoern A. Zeeb 			rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
104281d5d46bSBjoern A. Zeeb 			    0, 0, RT_DEFAULT_FIB);
1043baf7c373SQing Li 			if (rt == NULL)
10444208ea14SHajimu UMEMOTO 				continue;
1045baf7c373SQing Li 			/*
1046baf7c373SQing Li 			 * This is the case where multiple interfaces
1047baf7c373SQing Li 			 * have the same prefix, but only one is installed
1048baf7c373SQing Li 			 * into the routing table and that prefix entry
1049baf7c373SQing Li 			 * is not the one being examined here. In the case
1050baf7c373SQing Li 			 * where RADIX_MPATH is enabled, multiple route
1051baf7c373SQing Li 			 * entries (of the same rt_key value) will be
1052baf7c373SQing Li 			 * installed because the interface addresses all
1053baf7c373SQing Li 			 * differ.
1054baf7c373SQing Li 			 */
1055baf7c373SQing Li 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1056baf7c373SQing Li 			       &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
1057baf7c373SQing Li 				RTFREE_LOCKED(rt);
1058baf7c373SQing Li 				continue;
1059baf7c373SQing Li 			}
1060baf7c373SQing Li 			RTFREE_LOCKED(rt);
1061baf7c373SQing Li 		}
10624208ea14SHajimu UMEMOTO 
10634208ea14SHajimu UMEMOTO 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
10644208ea14SHajimu UMEMOTO 		    &addr->sin6_addr, &pr->ndpr_mask))
10654208ea14SHajimu UMEMOTO 			return (1);
106682cd038dSYoshinobu Inoue 	}
10674208ea14SHajimu UMEMOTO 
10684208ea14SHajimu UMEMOTO 	/*
1069743eee66SSUZUKI Shinsuke 	 * If the address is assigned on the node of the other side of
1070743eee66SSUZUKI Shinsuke 	 * a p2p interface, the address should be a neighbor.
1071743eee66SSUZUKI Shinsuke 	 */
10724f8585e0SAlan Somers 	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr, RT_ALL_FIBS);
10738c0fec80SRobert Watson 	if (dstaddr != NULL) {
10748c0fec80SRobert Watson 		if (dstaddr->ifa_ifp == ifp) {
10758c0fec80SRobert Watson 			ifa_free(dstaddr);
1076743eee66SSUZUKI Shinsuke 			return (1);
10778c0fec80SRobert Watson 		}
10788c0fec80SRobert Watson 		ifa_free(dstaddr);
10798c0fec80SRobert Watson 	}
1080743eee66SSUZUKI Shinsuke 
1081743eee66SSUZUKI Shinsuke 	/*
10824208ea14SHajimu UMEMOTO 	 * If the default router list is empty, all addresses are regarded
10834208ea14SHajimu UMEMOTO 	 * as on-link, and thus, as a neighbor.
10844208ea14SHajimu UMEMOTO 	 */
1085e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
10863b0b2840SJohn Baldwin 	    TAILQ_EMPTY(&V_nd_defrouter) &&
1087603724d3SBjoern A. Zeeb 	    V_nd6_defifindex == ifp->if_index) {
108882cd038dSYoshinobu Inoue 		return (1);
108982cd038dSYoshinobu Inoue 	}
109082cd038dSYoshinobu Inoue 
109177b691e0SBrian Feldman 	return (0);
109277b691e0SBrian Feldman }
109377b691e0SBrian Feldman 
109477b691e0SBrian Feldman 
109577b691e0SBrian Feldman /*
109677b691e0SBrian Feldman  * Detect if a given IPv6 address identifies a neighbor on a given link.
109777b691e0SBrian Feldman  * XXX: should take care of the destination of a p2p link?
109877b691e0SBrian Feldman  */
109977b691e0SBrian Feldman int
11001272577eSXin LI nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
110177b691e0SBrian Feldman {
11026e6b3f7cSQing Li 	struct llentry *lle;
11036e6b3f7cSQing Li 	int rc = 0;
110477b691e0SBrian Feldman 
11056e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
110677b691e0SBrian Feldman 	if (nd6_is_new_addr_neighbor(addr, ifp))
110777b691e0SBrian Feldman 		return (1);
110877b691e0SBrian Feldman 
110982cd038dSYoshinobu Inoue 	/*
111082cd038dSYoshinobu Inoue 	 * Even if the address matches none of our addresses, it might be
111182cd038dSYoshinobu Inoue 	 * in the neighbor cache.
111282cd038dSYoshinobu Inoue 	 */
111373cb2f38SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
11146e6b3f7cSQing Li 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
11156e6b3f7cSQing Li 		LLE_RUNLOCK(lle);
11166e6b3f7cSQing Li 		rc = 1;
11176e6b3f7cSQing Li 	}
111873cb2f38SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
11196e6b3f7cSQing Li 	return (rc);
112082cd038dSYoshinobu Inoue }
112182cd038dSYoshinobu Inoue 
112282cd038dSYoshinobu Inoue /*
112382cd038dSYoshinobu Inoue  * Free an nd6 llinfo entry.
1124743eee66SSUZUKI Shinsuke  * Since the function would cause significant changes in the kernel, DO NOT
1125743eee66SSUZUKI Shinsuke  * make it global, unless you have a strong reason for the change, and are sure
1126743eee66SSUZUKI Shinsuke  * that the change is safe.
112782cd038dSYoshinobu Inoue  */
11286e6b3f7cSQing Li static struct llentry *
11296e6b3f7cSQing Li nd6_free(struct llentry *ln, int gc)
113082cd038dSYoshinobu Inoue {
11316e6b3f7cSQing Li         struct llentry *next;
113282cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
1133e6950476SBjoern A. Zeeb 	struct ifnet *ifp;
1134e6950476SBjoern A. Zeeb 
1135e6950476SBjoern A. Zeeb 	LLE_WLOCK_ASSERT(ln);
113682cd038dSYoshinobu Inoue 
113782cd038dSYoshinobu Inoue 	/*
113833841545SHajimu UMEMOTO 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
113933841545SHajimu UMEMOTO 	 * even though it is not harmful, it was not really necessary.
114082cd038dSYoshinobu Inoue 	 */
1141686cdd19SJun-ichiro itojun Hagino 
1142743eee66SSUZUKI Shinsuke 	/* cancel timer */
1143e6950476SBjoern A. Zeeb 	nd6_llinfo_settimer_locked(ln, -1);
1144e6950476SBjoern A. Zeeb 
1145e6950476SBjoern A. Zeeb 	ifp = ln->lle_tbl->llt_ifp;
1146743eee66SSUZUKI Shinsuke 
1147e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1148314294deSAlexander V. Chernikov 		dr = defrouter_lookup(&ln->r_l3addr.addr6, ifp);
114933841545SHajimu UMEMOTO 
1150743eee66SSUZUKI Shinsuke 		if (dr != NULL && dr->expire &&
1151743eee66SSUZUKI Shinsuke 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1152743eee66SSUZUKI Shinsuke 			/*
1153743eee66SSUZUKI Shinsuke 			 * If the reason for the deletion is just garbage
1154743eee66SSUZUKI Shinsuke 			 * collection, and the neighbor is an active default
1155743eee66SSUZUKI Shinsuke 			 * router, do not delete it.  Instead, reset the GC
1156743eee66SSUZUKI Shinsuke 			 * timer using the router's lifetime.
1157743eee66SSUZUKI Shinsuke 			 * Simply deleting the entry would affect default
1158743eee66SSUZUKI Shinsuke 			 * router selection, which is not necessarily a good
1159743eee66SSUZUKI Shinsuke 			 * thing, especially when we're using router preference
1160743eee66SSUZUKI Shinsuke 			 * values.
1161743eee66SSUZUKI Shinsuke 			 * XXX: the check for ln_state would be redundant,
1162743eee66SSUZUKI Shinsuke 			 *      but we intentionally keep it just in case.
1163743eee66SSUZUKI Shinsuke 			 */
11647d26db17SHiroki Sato 			if (dr->expire > time_uptime)
1165e6950476SBjoern A. Zeeb 				nd6_llinfo_settimer_locked(ln,
11667d26db17SHiroki Sato 				    (dr->expire - time_uptime) * hz);
1167743eee66SSUZUKI Shinsuke 			else
1168e6950476SBjoern A. Zeeb 				nd6_llinfo_settimer_locked(ln,
1169e6950476SBjoern A. Zeeb 				    (long)V_nd6_gctimer * hz);
1170e6950476SBjoern A. Zeeb 
1171e6950476SBjoern A. Zeeb 			next = LIST_NEXT(ln, lle_next);
1172ef8d671cSHajimu UMEMOTO 			LLE_REMREF(ln);
1173ef8d671cSHajimu UMEMOTO 			LLE_WUNLOCK(ln);
1174e6950476SBjoern A. Zeeb 			return (next);
1175743eee66SSUZUKI Shinsuke 		}
1176743eee66SSUZUKI Shinsuke 
1177686cdd19SJun-ichiro itojun Hagino 		if (dr) {
1178686cdd19SJun-ichiro itojun Hagino 			/*
1179686cdd19SJun-ichiro itojun Hagino 			 * Unreachablity of a router might affect the default
1180686cdd19SJun-ichiro itojun Hagino 			 * router selection and on-link detection of advertised
1181686cdd19SJun-ichiro itojun Hagino 			 * prefixes.
1182686cdd19SJun-ichiro itojun Hagino 			 */
1183686cdd19SJun-ichiro itojun Hagino 
1184686cdd19SJun-ichiro itojun Hagino 			/*
1185686cdd19SJun-ichiro itojun Hagino 			 * Temporarily fake the state to choose a new default
1186686cdd19SJun-ichiro itojun Hagino 			 * router and to perform on-link determination of
118788ff5695SSUZUKI Shinsuke 			 * prefixes correctly.
1188686cdd19SJun-ichiro itojun Hagino 			 * Below the state will be set correctly,
1189686cdd19SJun-ichiro itojun Hagino 			 * or the entry itself will be deleted.
1190686cdd19SJun-ichiro itojun Hagino 			 */
1191686cdd19SJun-ichiro itojun Hagino 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
11921d5089c2SBjoern A. Zeeb 		}
1193686cdd19SJun-ichiro itojun Hagino 
11941d5089c2SBjoern A. Zeeb 		if (ln->ln_router || dr) {
11951d5089c2SBjoern A. Zeeb 
11961d5089c2SBjoern A. Zeeb 			/*
11971d5089c2SBjoern A. Zeeb 			 * We need to unlock to avoid a LOR with rt6_flush() with the
11981d5089c2SBjoern A. Zeeb 			 * rnh and for the calls to pfxlist_onlink_check() and
11991d5089c2SBjoern A. Zeeb 			 * defrouter_select() in the block further down for calls
12001d5089c2SBjoern A. Zeeb 			 * into nd6_lookup().  We still hold a ref.
12011d5089c2SBjoern A. Zeeb 			 */
12021d5089c2SBjoern A. Zeeb 			LLE_WUNLOCK(ln);
12031d5089c2SBjoern A. Zeeb 
12041d5089c2SBjoern A. Zeeb 			/*
12051d5089c2SBjoern A. Zeeb 			 * rt6_flush must be called whether or not the neighbor
12061d5089c2SBjoern A. Zeeb 			 * is in the Default Router List.
12071d5089c2SBjoern A. Zeeb 			 * See a corresponding comment in nd6_na_input().
12081d5089c2SBjoern A. Zeeb 			 */
1209314294deSAlexander V. Chernikov 			rt6_flush(&ln->r_l3addr.addr6, ifp);
12101d5089c2SBjoern A. Zeeb 		}
12111d5089c2SBjoern A. Zeeb 
12121d5089c2SBjoern A. Zeeb 		if (dr) {
121333841545SHajimu UMEMOTO 			/*
121433841545SHajimu UMEMOTO 			 * Since defrouter_select() does not affect the
121533841545SHajimu UMEMOTO 			 * on-link determination and MIP6 needs the check
121633841545SHajimu UMEMOTO 			 * before the default router selection, we perform
121733841545SHajimu UMEMOTO 			 * the check now.
121833841545SHajimu UMEMOTO 			 */
121933841545SHajimu UMEMOTO 			pfxlist_onlink_check();
122033841545SHajimu UMEMOTO 
1221686cdd19SJun-ichiro itojun Hagino 			/*
12221d5089c2SBjoern A. Zeeb 			 * Refresh default router list.
1223686cdd19SJun-ichiro itojun Hagino 			 */
1224686cdd19SJun-ichiro itojun Hagino 			defrouter_select();
1225686cdd19SJun-ichiro itojun Hagino 		}
12261d5089c2SBjoern A. Zeeb 
1227*17a03656SEric van Gyzen 		/*
1228*17a03656SEric van Gyzen 		 * If this entry was added by an on-link redirect, remove the
1229*17a03656SEric van Gyzen 		 * corresponding host route.
1230*17a03656SEric van Gyzen 		 */
1231*17a03656SEric van Gyzen 		if (ln->la_flags & LLE_REDIRECT)
1232*17a03656SEric van Gyzen 			nd6_free_redirect(ln);
1233*17a03656SEric van Gyzen 
12341d5089c2SBjoern A. Zeeb 		if (ln->ln_router || dr)
12351d5089c2SBjoern A. Zeeb 			LLE_WLOCK(ln);
123682cd038dSYoshinobu Inoue 	}
123782cd038dSYoshinobu Inoue 
123833841545SHajimu UMEMOTO 	/*
123933841545SHajimu UMEMOTO 	 * Before deleting the entry, remember the next entry as the
124033841545SHajimu UMEMOTO 	 * return value.  We need this because pfxlist_onlink_check() above
124133841545SHajimu UMEMOTO 	 * might have freed other entries (particularly the old next entry) as
124233841545SHajimu UMEMOTO 	 * a side effect (XXX).
124333841545SHajimu UMEMOTO 	 */
12446e6b3f7cSQing Li 	next = LIST_NEXT(ln, lle_next);
1245686cdd19SJun-ichiro itojun Hagino 
1246e6950476SBjoern A. Zeeb 	/*
1247e6950476SBjoern A. Zeeb 	 * Save to unlock. We still hold an extra reference and will not
1248e6950476SBjoern A. Zeeb 	 * free(9) in llentry_free() if someone else holds one as well.
1249e6950476SBjoern A. Zeeb 	 */
1250e6950476SBjoern A. Zeeb 	LLE_WUNLOCK(ln);
12516e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
12526e6b3f7cSQing Li 	LLE_WLOCK(ln);
1253b1ec2940SGleb Smirnoff 
1254b1ec2940SGleb Smirnoff 	/* Guard against race with other llentry_free(). */
1255b1ec2940SGleb Smirnoff 	if (ln->la_flags & LLE_LINKED) {
1256becba438SBjoern A. Zeeb 		LLE_REMREF(ln);
12576e6b3f7cSQing Li 		llentry_free(ln);
1258b1ec2940SGleb Smirnoff 	} else
1259b1ec2940SGleb Smirnoff 		LLE_FREE_LOCKED(ln);
1260b1ec2940SGleb Smirnoff 
12616e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
126233841545SHajimu UMEMOTO 
126333841545SHajimu UMEMOTO 	return (next);
126482cd038dSYoshinobu Inoue }
126582cd038dSYoshinobu Inoue 
126682cd038dSYoshinobu Inoue /*
1267*17a03656SEric van Gyzen  * Remove the rtentry for the given llentry,
1268*17a03656SEric van Gyzen  * both of which were installed by a redirect.
1269*17a03656SEric van Gyzen  */
1270*17a03656SEric van Gyzen static void
1271*17a03656SEric van Gyzen nd6_free_redirect(const struct llentry *ln)
1272*17a03656SEric van Gyzen {
1273*17a03656SEric van Gyzen 	int fibnum;
1274*17a03656SEric van Gyzen 	struct rtentry *rt;
1275*17a03656SEric van Gyzen 	struct radix_node_head *rnh;
1276*17a03656SEric van Gyzen 	struct sockaddr_in6 sin6;
1277*17a03656SEric van Gyzen 
1278*17a03656SEric van Gyzen 	lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6);
1279*17a03656SEric van Gyzen 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1280*17a03656SEric van Gyzen 		rnh = rt_tables_get_rnh(fibnum, AF_INET6);
1281*17a03656SEric van Gyzen 		if (rnh == NULL)
1282*17a03656SEric van Gyzen 			continue;
1283*17a03656SEric van Gyzen 
1284*17a03656SEric van Gyzen 		RADIX_NODE_HEAD_LOCK(rnh);
1285*17a03656SEric van Gyzen 		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0,
1286*17a03656SEric van Gyzen 		    RTF_RNH_LOCKED, fibnum);
1287*17a03656SEric van Gyzen 		if (rt) {
1288*17a03656SEric van Gyzen 			if (rt->rt_flags == (RTF_UP | RTF_HOST | RTF_DYNAMIC))
1289*17a03656SEric van Gyzen 				rt_expunge(rnh, rt);
1290*17a03656SEric van Gyzen 			RTFREE_LOCKED(rt);
1291*17a03656SEric van Gyzen 		}
1292*17a03656SEric van Gyzen 		RADIX_NODE_HEAD_UNLOCK(rnh);
1293*17a03656SEric van Gyzen 	}
1294*17a03656SEric van Gyzen }
1295*17a03656SEric van Gyzen 
1296*17a03656SEric van Gyzen /*
129782cd038dSYoshinobu Inoue  * Upper-layer reachability hint for Neighbor Unreachability Detection.
129882cd038dSYoshinobu Inoue  *
1299cd0fdcf7SHajimu UMEMOTO  * XXX cost-effective methods?
130082cd038dSYoshinobu Inoue  */
130182cd038dSYoshinobu Inoue void
13021272577eSXin LI nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
130382cd038dSYoshinobu Inoue {
13046e6b3f7cSQing Li 	struct llentry *ln;
13056e6b3f7cSQing Li 	struct ifnet *ifp;
130682cd038dSYoshinobu Inoue 
13076e6b3f7cSQing Li 	if ((dst6 == NULL) || (rt == NULL))
130882cd038dSYoshinobu Inoue 		return;
130982cd038dSYoshinobu Inoue 
13106e6b3f7cSQing Li 	ifp = rt->rt_ifp;
1311ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
13126e6b3f7cSQing Li 	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1313ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
13146e6b3f7cSQing Li 	if (ln == NULL)
131582cd038dSYoshinobu Inoue 		return;
131682cd038dSYoshinobu Inoue 
1317ad8d5711SMunechika SUMIKAWA 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
13186e6b3f7cSQing Li 		goto done;
131982cd038dSYoshinobu Inoue 
1320686cdd19SJun-ichiro itojun Hagino 	/*
1321686cdd19SJun-ichiro itojun Hagino 	 * if we get upper-layer reachability confirmation many times,
1322686cdd19SJun-ichiro itojun Hagino 	 * it is possible we have false information.
1323686cdd19SJun-ichiro itojun Hagino 	 */
1324686cdd19SJun-ichiro itojun Hagino 	if (!force) {
1325686cdd19SJun-ichiro itojun Hagino 		ln->ln_byhint++;
13266e6b3f7cSQing Li 		if (ln->ln_byhint > V_nd6_maxnudhint) {
13276e6b3f7cSQing Li 			goto done;
13286e6b3f7cSQing Li 		}
1329686cdd19SJun-ichiro itojun Hagino 	}
1330686cdd19SJun-ichiro itojun Hagino 
133182cd038dSYoshinobu Inoue  	ln->ln_state = ND6_LLINFO_REACHABLE;
1332743eee66SSUZUKI Shinsuke 	if (!ND6_LLINFO_PERMANENT(ln)) {
1333d715e397SBjoern A. Zeeb 		nd6_llinfo_settimer_locked(ln,
1334743eee66SSUZUKI Shinsuke 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1335743eee66SSUZUKI Shinsuke 	}
13366e6b3f7cSQing Li done:
13376e6b3f7cSQing Li 	LLE_WUNLOCK(ln);
133882cd038dSYoshinobu Inoue }
133982cd038dSYoshinobu Inoue 
134082cd038dSYoshinobu Inoue 
13410f1aca65SQing Li /*
13420f1aca65SQing Li  * Rejuvenate this function for routing operations related
13430f1aca65SQing Li  * processing.
13440f1aca65SQing Li  */
13450f1aca65SQing Li void
13460f1aca65SQing Li nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
13470f1aca65SQing Li {
134839e19560SBjoern A. Zeeb 	struct sockaddr_in6 *gateway;
13490f1aca65SQing Li 	struct nd_defrouter *dr;
135039e19560SBjoern A. Zeeb 	struct ifnet *ifp;
13510f1aca65SQing Li 
135239e19560SBjoern A. Zeeb 	gateway = (struct sockaddr_in6 *)rt->rt_gateway;
135339e19560SBjoern A. Zeeb 	ifp = rt->rt_ifp;
13540f1aca65SQing Li 
13550f1aca65SQing Li 	switch (req) {
13560f1aca65SQing Li 	case RTM_ADD:
13570f1aca65SQing Li 		break;
13580f1aca65SQing Li 
13590f1aca65SQing Li 	case RTM_DELETE:
13600f1aca65SQing Li 		if (!ifp)
13610f1aca65SQing Li 			return;
13620f1aca65SQing Li 		/*
13630f1aca65SQing Li 		 * Only indirect routes are interesting.
13640f1aca65SQing Li 		 */
13650f1aca65SQing Li 		if ((rt->rt_flags & RTF_GATEWAY) == 0)
13660f1aca65SQing Li 			return;
13670f1aca65SQing Li 		/*
13680f1aca65SQing Li 		 * check for default route
13690f1aca65SQing Li 		 */
13700f1aca65SQing Li 		if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
13710f1aca65SQing Li 				       &SIN6(rt_key(rt))->sin6_addr)) {
13720f1aca65SQing Li 
13730f1aca65SQing Li 			dr = defrouter_lookup(&gateway->sin6_addr, ifp);
13740f1aca65SQing Li 			if (dr != NULL)
13750f1aca65SQing Li 				dr->installed = 0;
13760f1aca65SQing Li 		}
13770f1aca65SQing Li 		break;
13780f1aca65SQing Li 	}
13790f1aca65SQing Li }
13800f1aca65SQing Li 
13810f1aca65SQing Li 
138282cd038dSYoshinobu Inoue int
13831272577eSXin LI nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
138482cd038dSYoshinobu Inoue {
138582cd038dSYoshinobu Inoue 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
138682cd038dSYoshinobu Inoue 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1387686cdd19SJun-ichiro itojun Hagino 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1388d0bec2c5SHiroki Sato 	int error = 0;
138982cd038dSYoshinobu Inoue 
13900de0dd9bSHiroki Sato 	if (ifp->if_afdata[AF_INET6] == NULL)
13910de0dd9bSHiroki Sato 		return (EPFNOSUPPORT);
139282cd038dSYoshinobu Inoue 	switch (cmd) {
139333841545SHajimu UMEMOTO 	case OSIOCGIFINFO_IN6:
13942ce62dceSSUZUKI Shinsuke #define ND	ndi->ndi
139531b1bfe1SHajimu UMEMOTO 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
13962ce62dceSSUZUKI Shinsuke 		bzero(&ND, sizeof(ND));
13972ce62dceSSUZUKI Shinsuke 		ND.linkmtu = IN6_LINKMTU(ifp);
13982ce62dceSSUZUKI Shinsuke 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
13992ce62dceSSUZUKI Shinsuke 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
14002ce62dceSSUZUKI Shinsuke 		ND.reachable = ND_IFINFO(ifp)->reachable;
14012ce62dceSSUZUKI Shinsuke 		ND.retrans = ND_IFINFO(ifp)->retrans;
14022ce62dceSSUZUKI Shinsuke 		ND.flags = ND_IFINFO(ifp)->flags;
14032ce62dceSSUZUKI Shinsuke 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
14042ce62dceSSUZUKI Shinsuke 		ND.chlim = ND_IFINFO(ifp)->chlim;
140533841545SHajimu UMEMOTO 		break;
140682cd038dSYoshinobu Inoue 	case SIOCGIFINFO_IN6:
14072ce62dceSSUZUKI Shinsuke 		ND = *ND_IFINFO(ifp);
140882cd038dSYoshinobu Inoue 		break;
1409b9204379SSUZUKI Shinsuke 	case SIOCSIFINFO_IN6:
1410b9204379SSUZUKI Shinsuke 		/*
1411b9204379SSUZUKI Shinsuke 		 * used to change host variables from userland.
1412b9204379SSUZUKI Shinsuke 		 * intented for a use on router to reflect RA configurations.
1413b9204379SSUZUKI Shinsuke 		 */
1414b9204379SSUZUKI Shinsuke 		/* 0 means 'unspecified' */
1415b9204379SSUZUKI Shinsuke 		if (ND.linkmtu != 0) {
1416b9204379SSUZUKI Shinsuke 			if (ND.linkmtu < IPV6_MMTU ||
1417b9204379SSUZUKI Shinsuke 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1418b9204379SSUZUKI Shinsuke 				error = EINVAL;
1419b9204379SSUZUKI Shinsuke 				break;
1420b9204379SSUZUKI Shinsuke 			}
1421b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1422b9204379SSUZUKI Shinsuke 		}
1423b9204379SSUZUKI Shinsuke 
1424b9204379SSUZUKI Shinsuke 		if (ND.basereachable != 0) {
1425b9204379SSUZUKI Shinsuke 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1426b9204379SSUZUKI Shinsuke 
1427b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1428b9204379SSUZUKI Shinsuke 			if (ND.basereachable != obasereachable)
1429b9204379SSUZUKI Shinsuke 				ND_IFINFO(ifp)->reachable =
1430b9204379SSUZUKI Shinsuke 				    ND_COMPUTE_RTIME(ND.basereachable);
1431b9204379SSUZUKI Shinsuke 		}
1432b9204379SSUZUKI Shinsuke 		if (ND.retrans != 0)
1433b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->retrans = ND.retrans;
1434b9204379SSUZUKI Shinsuke 		if (ND.chlim != 0)
1435b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->chlim = ND.chlim;
1436b9204379SSUZUKI Shinsuke 		/* FALLTHROUGH */
1437686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFINFO_FLAGS:
1438a283298cSHiroki Sato 	{
1439a283298cSHiroki Sato 		struct ifaddr *ifa;
1440a283298cSHiroki Sato 		struct in6_ifaddr *ia;
1441a283298cSHiroki Sato 
1442a283298cSHiroki Sato 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1443a283298cSHiroki Sato 		    !(ND.flags & ND6_IFF_IFDISABLED)) {
1444a283298cSHiroki Sato 			/* ifdisabled 1->0 transision */
1445a283298cSHiroki Sato 
1446a283298cSHiroki Sato 			/*
1447a283298cSHiroki Sato 			 * If the interface is marked as ND6_IFF_IFDISABLED and
1448a283298cSHiroki Sato 			 * has an link-local address with IN6_IFF_DUPLICATED,
1449a283298cSHiroki Sato 			 * do not clear ND6_IFF_IFDISABLED.
1450a283298cSHiroki Sato 			 * See RFC 4862, Section 5.4.5.
1451a283298cSHiroki Sato 			 */
1452137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
1453a283298cSHiroki Sato 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1454a283298cSHiroki Sato 				if (ifa->ifa_addr->sa_family != AF_INET6)
1455a283298cSHiroki Sato 					continue;
1456a283298cSHiroki Sato 				ia = (struct in6_ifaddr *)ifa;
1457a283298cSHiroki Sato 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1458d0bec2c5SHiroki Sato 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1459a283298cSHiroki Sato 					break;
1460a283298cSHiroki Sato 			}
1461137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
1462a283298cSHiroki Sato 
1463d0bec2c5SHiroki Sato 			if (ifa != NULL) {
1464d0bec2c5SHiroki Sato 				/* LLA is duplicated. */
1465a283298cSHiroki Sato 				ND.flags |= ND6_IFF_IFDISABLED;
1466a283298cSHiroki Sato 				log(LOG_ERR, "Cannot enable an interface"
1467a283298cSHiroki Sato 				    " with a link-local address marked"
1468a283298cSHiroki Sato 				    " duplicate.\n");
1469a283298cSHiroki Sato 			} else {
1470a283298cSHiroki Sato 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1471154d5f73SHiroki Sato 				if (ifp->if_flags & IFF_UP)
1472a283298cSHiroki Sato 					in6_if_up(ifp);
1473a283298cSHiroki Sato 			}
1474a283298cSHiroki Sato 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1475a283298cSHiroki Sato 			    (ND.flags & ND6_IFF_IFDISABLED)) {
1476a283298cSHiroki Sato 			/* ifdisabled 0->1 transision */
1477a283298cSHiroki Sato 			/* Mark all IPv6 address as tentative. */
1478a283298cSHiroki Sato 
1479a283298cSHiroki Sato 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1480d0bec2c5SHiroki Sato 			if ((ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD) == 0) {
1481137f91e8SJohn Baldwin 				IF_ADDR_RLOCK(ifp);
1482d0bec2c5SHiroki Sato 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1483d0bec2c5SHiroki Sato 				    ifa_link) {
1484d0bec2c5SHiroki Sato 					if (ifa->ifa_addr->sa_family !=
1485d0bec2c5SHiroki Sato 					    AF_INET6)
1486a283298cSHiroki Sato 						continue;
1487a283298cSHiroki Sato 					ia = (struct in6_ifaddr *)ifa;
1488a283298cSHiroki Sato 					ia->ia6_flags |= IN6_IFF_TENTATIVE;
1489a283298cSHiroki Sato 				}
1490137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
1491a283298cSHiroki Sato 			}
1492d0bec2c5SHiroki Sato 		}
1493a283298cSHiroki Sato 
1494154d5f73SHiroki Sato 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1495154d5f73SHiroki Sato 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1496a283298cSHiroki Sato 				/* auto_linklocal 0->1 transision */
1497a283298cSHiroki Sato 
1498a283298cSHiroki Sato 				/* If no link-local address on ifp, configure */
1499a283298cSHiroki Sato 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1500a283298cSHiroki Sato 				in6_ifattach(ifp, NULL);
1501154d5f73SHiroki Sato 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1502154d5f73SHiroki Sato 			    ifp->if_flags & IFF_UP) {
150377bc4985SHiroki Sato 				/*
150477bc4985SHiroki Sato 				 * When the IF already has
1505154d5f73SHiroki Sato 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1506154d5f73SHiroki Sato 				 * address is assigned, and IFF_UP, try to
1507154d5f73SHiroki Sato 				 * assign one.
150877bc4985SHiroki Sato 				 */
1509137f91e8SJohn Baldwin 				IF_ADDR_RLOCK(ifp);
1510d0bec2c5SHiroki Sato 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1511d0bec2c5SHiroki Sato 				    ifa_link) {
1512d0bec2c5SHiroki Sato 					if (ifa->ifa_addr->sa_family !=
1513d0bec2c5SHiroki Sato 					    AF_INET6)
151477bc4985SHiroki Sato 						continue;
151577bc4985SHiroki Sato 					ia = (struct in6_ifaddr *)ifa;
1516d0bec2c5SHiroki Sato 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
151777bc4985SHiroki Sato 						break;
151877bc4985SHiroki Sato 				}
1519137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
1520d0bec2c5SHiroki Sato 				if (ifa != NULL)
1521d0bec2c5SHiroki Sato 					/* No LLA is configured. */
152277bc4985SHiroki Sato 					in6_ifattach(ifp, NULL);
1523a283298cSHiroki Sato 			}
1524a283298cSHiroki Sato 		}
1525154d5f73SHiroki Sato 	}
15262ce62dceSSUZUKI Shinsuke 		ND_IFINFO(ifp)->flags = ND.flags;
1527686cdd19SJun-ichiro itojun Hagino 		break;
15282ce62dceSSUZUKI Shinsuke #undef ND
1529686cdd19SJun-ichiro itojun Hagino 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1530743eee66SSUZUKI Shinsuke 		/* sync kernel routing table with the default router list */
1531743eee66SSUZUKI Shinsuke 		defrouter_reset();
1532686cdd19SJun-ichiro itojun Hagino 		defrouter_select();
153382cd038dSYoshinobu Inoue 		break;
153482cd038dSYoshinobu Inoue 	case SIOCSPFXFLUSH_IN6:
153582cd038dSYoshinobu Inoue 	{
153682cd038dSYoshinobu Inoue 		/* flush all the prefix advertised by routers */
153782cd038dSYoshinobu Inoue 		struct nd_prefix *pr, *next;
153882cd038dSYoshinobu Inoue 
15393b0b2840SJohn Baldwin 		LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
154033841545SHajimu UMEMOTO 			struct in6_ifaddr *ia, *ia_next;
154133841545SHajimu UMEMOTO 
154233841545SHajimu UMEMOTO 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
154333841545SHajimu UMEMOTO 				continue; /* XXX */
154433841545SHajimu UMEMOTO 
154533841545SHajimu UMEMOTO 			/* do we really have to remove addresses as well? */
1546d1da0a06SRobert Watson 			/* XXXRW: in6_ifaddrhead locking. */
154780af0152SRobert Watson 			TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
154880af0152SRobert Watson 			    ia_next) {
154933841545SHajimu UMEMOTO 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
155033841545SHajimu UMEMOTO 					continue;
155133841545SHajimu UMEMOTO 
155233841545SHajimu UMEMOTO 				if (ia->ia6_ndpr == pr)
155333841545SHajimu UMEMOTO 					in6_purgeaddr(&ia->ia_ifa);
155433841545SHajimu UMEMOTO 			}
155582cd038dSYoshinobu Inoue 			prelist_remove(pr);
155682cd038dSYoshinobu Inoue 		}
155782cd038dSYoshinobu Inoue 		break;
155882cd038dSYoshinobu Inoue 	}
155982cd038dSYoshinobu Inoue 	case SIOCSRTRFLUSH_IN6:
156082cd038dSYoshinobu Inoue 	{
156182cd038dSYoshinobu Inoue 		/* flush all the default routers */
156282cd038dSYoshinobu Inoue 		struct nd_defrouter *dr, *next;
156382cd038dSYoshinobu Inoue 
1564743eee66SSUZUKI Shinsuke 		defrouter_reset();
15653b0b2840SJohn Baldwin 		TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
156682cd038dSYoshinobu Inoue 			defrtrlist_del(dr);
156782cd038dSYoshinobu Inoue 		}
1568743eee66SSUZUKI Shinsuke 		defrouter_select();
156982cd038dSYoshinobu Inoue 		break;
157082cd038dSYoshinobu Inoue 	}
157182cd038dSYoshinobu Inoue 	case SIOCGNBRINFO_IN6:
157282cd038dSYoshinobu Inoue 	{
15736e6b3f7cSQing Li 		struct llentry *ln;
157482cd038dSYoshinobu Inoue 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
157582cd038dSYoshinobu Inoue 
1576a1f7e5f8SHajimu UMEMOTO 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1577a1f7e5f8SHajimu UMEMOTO 			return (error);
157882cd038dSYoshinobu Inoue 
157973cb2f38SAndrey V. Elsukov 		IF_AFDATA_RLOCK(ifp);
15806e6b3f7cSQing Li 		ln = nd6_lookup(&nb_addr, 0, ifp);
158173cb2f38SAndrey V. Elsukov 		IF_AFDATA_RUNLOCK(ifp);
15826e6b3f7cSQing Li 
15836e6b3f7cSQing Li 		if (ln == NULL) {
158482cd038dSYoshinobu Inoue 			error = EINVAL;
158582cd038dSYoshinobu Inoue 			break;
158682cd038dSYoshinobu Inoue 		}
158782cd038dSYoshinobu Inoue 		nbi->state = ln->ln_state;
15886e6b3f7cSQing Li 		nbi->asked = ln->la_asked;
158982cd038dSYoshinobu Inoue 		nbi->isrouter = ln->ln_router;
15905a041915SHiroki Sato 		if (ln->la_expire == 0)
15915a041915SHiroki Sato 			nbi->expire = 0;
15925a041915SHiroki Sato 		else
15935a041915SHiroki Sato 			nbi->expire = ln->la_expire +
15945a041915SHiroki Sato 			    (time_second - time_uptime);
15956e6b3f7cSQing Li 		LLE_RUNLOCK(ln);
159682cd038dSYoshinobu Inoue 		break;
159782cd038dSYoshinobu Inoue 	}
1598686cdd19SJun-ichiro itojun Hagino 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1599603724d3SBjoern A. Zeeb 		ndif->ifindex = V_nd6_defifindex;
1600686cdd19SJun-ichiro itojun Hagino 		break;
1601686cdd19SJun-ichiro itojun Hagino 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1602686cdd19SJun-ichiro itojun Hagino 		return (nd6_setdefaultiface(ndif->ifindex));
160382cd038dSYoshinobu Inoue 	}
160482cd038dSYoshinobu Inoue 	return (error);
160582cd038dSYoshinobu Inoue }
160682cd038dSYoshinobu Inoue 
160782cd038dSYoshinobu Inoue /*
160882cd038dSYoshinobu Inoue  * Create neighbor cache entry and cache link-layer address,
160982cd038dSYoshinobu Inoue  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
16101272577eSXin LI  *
16111272577eSXin LI  * type - ICMP6 type
16121272577eSXin LI  * code - type dependent information
16136e6b3f7cSQing Li  *
16146e6b3f7cSQing Li  * XXXXX
16156e6b3f7cSQing Li  *  The caller of this function already acquired the ndp
16166e6b3f7cSQing Li  *  cache table lock because the cache entry is returned.
161782cd038dSYoshinobu Inoue  */
16186e6b3f7cSQing Li struct llentry *
16191272577eSXin LI nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
16201272577eSXin LI     int lladdrlen, int type, int code)
162182cd038dSYoshinobu Inoue {
16225a255516SAlexander V. Chernikov 	struct llentry *ln = NULL, *ln_tmp;
162382cd038dSYoshinobu Inoue 	int is_newentry;
162482cd038dSYoshinobu Inoue 	int do_update;
162582cd038dSYoshinobu Inoue 	int olladdr;
162682cd038dSYoshinobu Inoue 	int llchange;
162768352503SBjoern A. Zeeb 	int flags;
162882cd038dSYoshinobu Inoue 	int newstate = 0;
16293d3728e9SQing Li 	uint16_t router = 0;
16306e6b3f7cSQing Li 	struct sockaddr_in6 sin6;
1631fd14c50bSKip Macy 	struct mbuf *chain = NULL;
16323d3728e9SQing Li 	int static_route = 0;
16336e6b3f7cSQing Li 
16346e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
163582cd038dSYoshinobu Inoue 
163639e19560SBjoern A. Zeeb 	KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
163739e19560SBjoern A. Zeeb 	KASSERT(from != NULL, ("%s: from == NULL", __func__));
163882cd038dSYoshinobu Inoue 
163982cd038dSYoshinobu Inoue 	/* nothing must be updated for unspecified address */
164082cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(from))
164182cd038dSYoshinobu Inoue 		return NULL;
164282cd038dSYoshinobu Inoue 
164382cd038dSYoshinobu Inoue 	/*
164482cd038dSYoshinobu Inoue 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
164582cd038dSYoshinobu Inoue 	 * the caller.
164682cd038dSYoshinobu Inoue 	 *
164782cd038dSYoshinobu Inoue 	 * XXX If the link does not have link-layer adderss, what should
164882cd038dSYoshinobu Inoue 	 * we do? (ifp->if_addrlen == 0)
164982cd038dSYoshinobu Inoue 	 * Spec says nothing in sections for RA, RS and NA.  There's small
165082cd038dSYoshinobu Inoue 	 * description on it in NS section (RFC 2461 7.2.3).
165182cd038dSYoshinobu Inoue 	 */
165268352503SBjoern A. Zeeb 	flags = lladdr ? ND6_EXCLUSIVE : 0;
1653ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
16546e6b3f7cSQing Li 	ln = nd6_lookup(from, flags, ifp);
1655ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
16565a255516SAlexander V. Chernikov 	is_newentry = 0;
16576e6b3f7cSQing Li 	if (ln == NULL) {
165868352503SBjoern A. Zeeb 		flags |= ND6_EXCLUSIVE;
16595a255516SAlexander V. Chernikov 		ln = nd6_alloc(from, 0, ifp);
16605a255516SAlexander V. Chernikov 		if (ln == NULL)
16615a255516SAlexander V. Chernikov 			return (NULL);
16625a255516SAlexander V. Chernikov 		IF_AFDATA_WLOCK(ifp);
16635a255516SAlexander V. Chernikov 		LLE_WLOCK(ln);
16645a255516SAlexander V. Chernikov 		/* Prefer any existing lle over newly-created one */
16655a255516SAlexander V. Chernikov 		ln_tmp = nd6_lookup(from, ND6_EXCLUSIVE, ifp);
16665a255516SAlexander V. Chernikov 		if (ln_tmp == NULL)
16675a255516SAlexander V. Chernikov 			lltable_link_entry(LLTABLE6(ifp), ln);
16685a255516SAlexander V. Chernikov 		IF_AFDATA_WUNLOCK(ifp);
16695a255516SAlexander V. Chernikov 		if (ln_tmp == NULL)
16705a255516SAlexander V. Chernikov 			/* No existing lle, mark as new entry */
167182cd038dSYoshinobu Inoue 			is_newentry = 1;
16725a255516SAlexander V. Chernikov 		else {
16735a255516SAlexander V. Chernikov 			lltable_free_entry(LLTABLE6(ifp), ln);
16745a255516SAlexander V. Chernikov 			ln = ln_tmp;
16755a255516SAlexander V. Chernikov 			ln_tmp = NULL;
16765a255516SAlexander V. Chernikov 		}
16775a255516SAlexander V. Chernikov 	}
167833841545SHajimu UMEMOTO 	/* do nothing if static ndp is set */
16795a255516SAlexander V. Chernikov 	if ((ln->la_flags & LLE_STATIC)) {
16803d3728e9SQing Li 			static_route = 1;
16816e6b3f7cSQing Li 			goto done;
16823d3728e9SQing Li 	}
168382cd038dSYoshinobu Inoue 
16846e6b3f7cSQing Li 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
168582cd038dSYoshinobu Inoue 	if (olladdr && lladdr) {
16866e6b3f7cSQing Li 		llchange = bcmp(lladdr, &ln->ll_addr,
16876e6b3f7cSQing Li 		    ifp->if_addrlen);
168882cd038dSYoshinobu Inoue 	} else
168982cd038dSYoshinobu Inoue 		llchange = 0;
169082cd038dSYoshinobu Inoue 
169182cd038dSYoshinobu Inoue 	/*
169282cd038dSYoshinobu Inoue 	 * newentry olladdr  lladdr  llchange	(*=record)
169382cd038dSYoshinobu Inoue 	 *	0	n	n	--	(1)
169482cd038dSYoshinobu Inoue 	 *	0	y	n	--	(2)
169582cd038dSYoshinobu Inoue 	 *	0	n	y	--	(3) * STALE
169682cd038dSYoshinobu Inoue 	 *	0	y	y	n	(4) *
169782cd038dSYoshinobu Inoue 	 *	0	y	y	y	(5) * STALE
169882cd038dSYoshinobu Inoue 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
169982cd038dSYoshinobu Inoue 	 *	1	--	y	--	(7) * STALE
170082cd038dSYoshinobu Inoue 	 */
170182cd038dSYoshinobu Inoue 
170282cd038dSYoshinobu Inoue 	if (lladdr) {		/* (3-5) and (7) */
170382cd038dSYoshinobu Inoue 		/*
170482cd038dSYoshinobu Inoue 		 * Record source link-layer address
170582cd038dSYoshinobu Inoue 		 * XXX is it dependent to ifp->if_type?
170682cd038dSYoshinobu Inoue 		 */
17076e6b3f7cSQing Li 		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
17086e6b3f7cSQing Li 		ln->la_flags |= LLE_VALID;
170963a97a40SNavdeep Parhar 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
171082cd038dSYoshinobu Inoue 	}
171182cd038dSYoshinobu Inoue 
171282cd038dSYoshinobu Inoue 	if (!is_newentry) {
17132ce62dceSSUZUKI Shinsuke 		if ((!olladdr && lladdr != NULL) ||	/* (3) */
17142ce62dceSSUZUKI Shinsuke 		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
171582cd038dSYoshinobu Inoue 			do_update = 1;
171682cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_STALE;
171782cd038dSYoshinobu Inoue 		} else					/* (1-2,4) */
171882cd038dSYoshinobu Inoue 			do_update = 0;
171982cd038dSYoshinobu Inoue 	} else {
172082cd038dSYoshinobu Inoue 		do_update = 1;
17212ce62dceSSUZUKI Shinsuke 		if (lladdr == NULL)			/* (6) */
172282cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_NOSTATE;
172382cd038dSYoshinobu Inoue 		else					/* (7) */
172482cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_STALE;
172582cd038dSYoshinobu Inoue 	}
172682cd038dSYoshinobu Inoue 
172782cd038dSYoshinobu Inoue 	if (do_update) {
172882cd038dSYoshinobu Inoue 		/*
172982cd038dSYoshinobu Inoue 		 * Update the state of the neighbor cache.
173082cd038dSYoshinobu Inoue 		 */
173182cd038dSYoshinobu Inoue 		ln->ln_state = newstate;
173282cd038dSYoshinobu Inoue 
173382cd038dSYoshinobu Inoue 		if (ln->ln_state == ND6_LLINFO_STALE) {
1734d7968c29SAlexander V. Chernikov 			if (ln->la_hold != NULL)
1735d7968c29SAlexander V. Chernikov 				nd6_grab_holdchain(ln, &chain, &sin6);
173682cd038dSYoshinobu Inoue 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
173782cd038dSYoshinobu Inoue 			/* probe right away */
17386e6b3f7cSQing Li 			nd6_llinfo_settimer_locked((void *)ln, 0);
173982cd038dSYoshinobu Inoue 		}
174082cd038dSYoshinobu Inoue 	}
174182cd038dSYoshinobu Inoue 
174282cd038dSYoshinobu Inoue 	/*
174382cd038dSYoshinobu Inoue 	 * ICMP6 type dependent behavior.
174482cd038dSYoshinobu Inoue 	 *
174582cd038dSYoshinobu Inoue 	 * NS: clear IsRouter if new entry
174682cd038dSYoshinobu Inoue 	 * RS: clear IsRouter
174782cd038dSYoshinobu Inoue 	 * RA: set IsRouter if there's lladdr
174882cd038dSYoshinobu Inoue 	 * redir: clear IsRouter if new entry
174982cd038dSYoshinobu Inoue 	 *
175082cd038dSYoshinobu Inoue 	 * RA case, (1):
175182cd038dSYoshinobu Inoue 	 * The spec says that we must set IsRouter in the following cases:
175282cd038dSYoshinobu Inoue 	 * - If lladdr exist, set IsRouter.  This means (1-5).
175382cd038dSYoshinobu Inoue 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
175482cd038dSYoshinobu Inoue 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
175582cd038dSYoshinobu Inoue 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
175682cd038dSYoshinobu Inoue 	 * neighbor cache, this is similar to (6).
175782cd038dSYoshinobu Inoue 	 * This case is rare but we figured that we MUST NOT set IsRouter.
175882cd038dSYoshinobu Inoue 	 *
175982cd038dSYoshinobu Inoue 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
176082cd038dSYoshinobu Inoue 	 *							D R
176182cd038dSYoshinobu Inoue 	 *	0	n	n	--	(1)	c   ?     s
176282cd038dSYoshinobu Inoue 	 *	0	y	n	--	(2)	c   s     s
176382cd038dSYoshinobu Inoue 	 *	0	n	y	--	(3)	c   s     s
176482cd038dSYoshinobu Inoue 	 *	0	y	y	n	(4)	c   s     s
176582cd038dSYoshinobu Inoue 	 *	0	y	y	y	(5)	c   s     s
176682cd038dSYoshinobu Inoue 	 *	1	--	n	--	(6) c	c	c s
176782cd038dSYoshinobu Inoue 	 *	1	--	y	--	(7) c	c   s	c s
176882cd038dSYoshinobu Inoue 	 *
176982cd038dSYoshinobu Inoue 	 *					(c=clear s=set)
177082cd038dSYoshinobu Inoue 	 */
177182cd038dSYoshinobu Inoue 	switch (type & 0xff) {
177282cd038dSYoshinobu Inoue 	case ND_NEIGHBOR_SOLICIT:
177382cd038dSYoshinobu Inoue 		/*
177482cd038dSYoshinobu Inoue 		 * New entry must have is_router flag cleared.
177582cd038dSYoshinobu Inoue 		 */
177682cd038dSYoshinobu Inoue 		if (is_newentry)	/* (6-7) */
177782cd038dSYoshinobu Inoue 			ln->ln_router = 0;
177882cd038dSYoshinobu Inoue 		break;
177982cd038dSYoshinobu Inoue 	case ND_REDIRECT:
178082cd038dSYoshinobu Inoue 		/*
178182cd038dSYoshinobu Inoue 		 * If the icmp is a redirect to a better router, always set the
178282cd038dSYoshinobu Inoue 		 * is_router flag.  Otherwise, if the entry is newly created,
178382cd038dSYoshinobu Inoue 		 * clear the flag.  [RFC 2461, sec 8.3]
178482cd038dSYoshinobu Inoue 		 */
178582cd038dSYoshinobu Inoue 		if (code == ND_REDIRECT_ROUTER)
178682cd038dSYoshinobu Inoue 			ln->ln_router = 1;
1787*17a03656SEric van Gyzen 		else {
1788*17a03656SEric van Gyzen 			if (is_newentry) /* (6-7) */
178982cd038dSYoshinobu Inoue 				ln->ln_router = 0;
1790*17a03656SEric van Gyzen 			ln->la_flags |= LLE_REDIRECT;
1791*17a03656SEric van Gyzen 		}
179282cd038dSYoshinobu Inoue 		break;
179382cd038dSYoshinobu Inoue 	case ND_ROUTER_SOLICIT:
179482cd038dSYoshinobu Inoue 		/*
179582cd038dSYoshinobu Inoue 		 * is_router flag must always be cleared.
179682cd038dSYoshinobu Inoue 		 */
179782cd038dSYoshinobu Inoue 		ln->ln_router = 0;
179882cd038dSYoshinobu Inoue 		break;
179982cd038dSYoshinobu Inoue 	case ND_ROUTER_ADVERT:
180082cd038dSYoshinobu Inoue 		/*
180182cd038dSYoshinobu Inoue 		 * Mark an entry with lladdr as a router.
180282cd038dSYoshinobu Inoue 		 */
180307eb2995SHajimu UMEMOTO 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
180407eb2995SHajimu UMEMOTO 		    (is_newentry && lladdr)) {			/* (7) */
180582cd038dSYoshinobu Inoue 			ln->ln_router = 1;
180682cd038dSYoshinobu Inoue 		}
180782cd038dSYoshinobu Inoue 		break;
180882cd038dSYoshinobu Inoue 	}
180982cd038dSYoshinobu Inoue 
181023ee1bfaSKip Macy 	if (ln != NULL) {
18113d3728e9SQing Li 		static_route = (ln->la_flags & LLE_STATIC);
1812c2b3a02bSKip Macy 		router = ln->ln_router;
1813c2b3a02bSKip Macy 
18146e6b3f7cSQing Li 		if (flags & ND6_EXCLUSIVE)
18156e6b3f7cSQing Li 			LLE_WUNLOCK(ln);
18166e6b3f7cSQing Li 		else
18176e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
1818c2b3a02bSKip Macy 		if (static_route)
18196e6b3f7cSQing Li 			ln = NULL;
18206e6b3f7cSQing Li 	}
1821d7968c29SAlexander V. Chernikov 	if (chain != NULL)
1822d7968c29SAlexander V. Chernikov 		nd6_flush_holdchain(ifp, ifp, chain, &sin6);
18236e6b3f7cSQing Li 
1824554bf4aaSHajimu UMEMOTO 	/*
1825554bf4aaSHajimu UMEMOTO 	 * When the link-layer address of a router changes, select the
1826554bf4aaSHajimu UMEMOTO 	 * best router again.  In particular, when the neighbor entry is newly
1827554bf4aaSHajimu UMEMOTO 	 * created, it might affect the selection policy.
1828554bf4aaSHajimu UMEMOTO 	 * Question: can we restrict the first condition to the "is_newentry"
1829554bf4aaSHajimu UMEMOTO 	 * case?
1830554bf4aaSHajimu UMEMOTO 	 * XXX: when we hear an RA from a new router with the link-layer
1831554bf4aaSHajimu UMEMOTO 	 * address option, defrouter_select() is called twice, since
1832554bf4aaSHajimu UMEMOTO 	 * defrtrlist_update called the function as well.  However, I believe
1833554bf4aaSHajimu UMEMOTO 	 * we can compromise the overhead, since it only happens the first
1834554bf4aaSHajimu UMEMOTO 	 * time.
1835554bf4aaSHajimu UMEMOTO 	 * XXX: although defrouter_select() should not have a bad effect
1836554bf4aaSHajimu UMEMOTO 	 * for those are not autoconfigured hosts, we explicitly avoid such
1837554bf4aaSHajimu UMEMOTO 	 * cases for safety.
1838554bf4aaSHajimu UMEMOTO 	 */
1839e7fa8d0aSHiroki Sato 	if (do_update && router &&
1840a283298cSHiroki Sato 	    ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
18416e6b3f7cSQing Li 		/*
18426e6b3f7cSQing Li 		 * guaranteed recursion
18436e6b3f7cSQing Li 		 */
1844554bf4aaSHajimu UMEMOTO 		defrouter_select();
18456e6b3f7cSQing Li 	}
1846554bf4aaSHajimu UMEMOTO 
18476e6b3f7cSQing Li 	return (ln);
18486e6b3f7cSQing Li done:
1849aba53ef0SKip Macy 	if (ln != NULL) {
18506e6b3f7cSQing Li 		if (flags & ND6_EXCLUSIVE)
18516e6b3f7cSQing Li 			LLE_WUNLOCK(ln);
18526e6b3f7cSQing Li 		else
18536e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
18543d3728e9SQing Li 		if (static_route)
18556e6b3f7cSQing Li 			ln = NULL;
18566e6b3f7cSQing Li 	}
18576e6b3f7cSQing Li 	return (ln);
185882cd038dSYoshinobu Inoue }
185982cd038dSYoshinobu Inoue 
186082cd038dSYoshinobu Inoue static void
18618b615593SMarko Zec nd6_slowtimo(void *arg)
186282cd038dSYoshinobu Inoue {
18638b615593SMarko Zec 	CURVNET_SET((struct vnet *) arg);
186433841545SHajimu UMEMOTO 	struct nd_ifinfo *nd6if;
186531b1bfe1SHajimu UMEMOTO 	struct ifnet *ifp;
186682cd038dSYoshinobu Inoue 
1867603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1868f6dfe47aSMarko Zec 	    nd6_slowtimo, curvnet);
186977dfcdc4SRobert Watson 	IFNET_RLOCK_NOSLEEP();
1870e6abaf91SGleb Smirnoff 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
18710de0dd9bSHiroki Sato 		if (ifp->if_afdata[AF_INET6] == NULL)
18720de0dd9bSHiroki Sato 			continue;
187331b1bfe1SHajimu UMEMOTO 		nd6if = ND_IFINFO(ifp);
187482cd038dSYoshinobu Inoue 		if (nd6if->basereachable && /* already initialized */
187582cd038dSYoshinobu Inoue 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
187682cd038dSYoshinobu Inoue 			/*
187782cd038dSYoshinobu Inoue 			 * Since reachable time rarely changes by router
187882cd038dSYoshinobu Inoue 			 * advertisements, we SHOULD insure that a new random
187982cd038dSYoshinobu Inoue 			 * value gets recomputed at least once every few hours.
188082cd038dSYoshinobu Inoue 			 * (RFC 2461, 6.3.4)
188182cd038dSYoshinobu Inoue 			 */
1882603724d3SBjoern A. Zeeb 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
188382cd038dSYoshinobu Inoue 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
188482cd038dSYoshinobu Inoue 		}
188582cd038dSYoshinobu Inoue 	}
188677dfcdc4SRobert Watson 	IFNET_RUNLOCK_NOSLEEP();
18878b615593SMarko Zec 	CURVNET_RESTORE();
188882cd038dSYoshinobu Inoue }
188982cd038dSYoshinobu Inoue 
1890d7968c29SAlexander V. Chernikov void
1891d7968c29SAlexander V. Chernikov nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
1892d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *sin6)
1893d7968c29SAlexander V. Chernikov {
1894d7968c29SAlexander V. Chernikov 
1895d7968c29SAlexander V. Chernikov 	LLE_WLOCK_ASSERT(ln);
1896d7968c29SAlexander V. Chernikov 
1897d7968c29SAlexander V. Chernikov 	*chain = ln->la_hold;
1898d7968c29SAlexander V. Chernikov 	ln->la_hold = NULL;
1899314294deSAlexander V. Chernikov 	lltable_fill_sa_entry(ln, (struct sockaddr *)sin6);
1900d7968c29SAlexander V. Chernikov 
1901d7968c29SAlexander V. Chernikov 	if (ln->ln_state == ND6_LLINFO_STALE) {
1902d7968c29SAlexander V. Chernikov 
1903d7968c29SAlexander V. Chernikov 		/*
1904d7968c29SAlexander V. Chernikov 		 * The first time we send a packet to a
1905d7968c29SAlexander V. Chernikov 		 * neighbor whose entry is STALE, we have
1906d7968c29SAlexander V. Chernikov 		 * to change the state to DELAY and a sets
1907d7968c29SAlexander V. Chernikov 		 * a timer to expire in DELAY_FIRST_PROBE_TIME
1908d7968c29SAlexander V. Chernikov 		 * seconds to ensure do neighbor unreachability
1909d7968c29SAlexander V. Chernikov 		 * detection on expiration.
1910d7968c29SAlexander V. Chernikov 		 * (RFC 2461 7.3.3)
1911d7968c29SAlexander V. Chernikov 		 */
1912d7968c29SAlexander V. Chernikov 		ln->la_asked = 0;
1913d7968c29SAlexander V. Chernikov 		ln->ln_state = ND6_LLINFO_DELAY;
1914d7968c29SAlexander V. Chernikov 		nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1915d7968c29SAlexander V. Chernikov 	}
1916d7968c29SAlexander V. Chernikov }
1917d7968c29SAlexander V. Chernikov 
1918d7968c29SAlexander V. Chernikov static int
1919d7968c29SAlexander V. Chernikov nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1920d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *dst)
1921d7968c29SAlexander V. Chernikov {
1922d7968c29SAlexander V. Chernikov 	int error;
1923d7968c29SAlexander V. Chernikov 	int ip6len;
1924d7968c29SAlexander V. Chernikov 	struct ip6_hdr *ip6;
1925d7968c29SAlexander V. Chernikov 	struct m_tag *mtag;
1926d7968c29SAlexander V. Chernikov 
1927d7968c29SAlexander V. Chernikov #ifdef MAC
1928d7968c29SAlexander V. Chernikov 	mac_netinet6_nd6_send(ifp, m);
1929d7968c29SAlexander V. Chernikov #endif
1930d7968c29SAlexander V. Chernikov 
1931d7968c29SAlexander V. Chernikov 	/*
1932d7968c29SAlexander V. Chernikov 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
1933d7968c29SAlexander V. Chernikov 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
1934d7968c29SAlexander V. Chernikov 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
1935d7968c29SAlexander V. Chernikov 	 * to be diverted to user space.  When re-injected into the kernel,
1936d7968c29SAlexander V. Chernikov 	 * send_output() will directly dispatch them to the outgoing interface.
1937d7968c29SAlexander V. Chernikov 	 */
1938d7968c29SAlexander V. Chernikov 	if (send_sendso_input_hook != NULL) {
1939d7968c29SAlexander V. Chernikov 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
1940d7968c29SAlexander V. Chernikov 		if (mtag != NULL) {
1941d7968c29SAlexander V. Chernikov 			ip6 = mtod(m, struct ip6_hdr *);
1942d7968c29SAlexander V. Chernikov 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
1943d7968c29SAlexander V. Chernikov 			/* Use the SEND socket */
1944d7968c29SAlexander V. Chernikov 			error = send_sendso_input_hook(m, ifp, SND_OUT,
1945d7968c29SAlexander V. Chernikov 			    ip6len);
1946d7968c29SAlexander V. Chernikov 			/* -1 == no app on SEND socket */
1947d7968c29SAlexander V. Chernikov 			if (error == 0 || error != -1)
1948d7968c29SAlexander V. Chernikov 			    return (error);
1949d7968c29SAlexander V. Chernikov 		}
1950d7968c29SAlexander V. Chernikov 	}
1951d7968c29SAlexander V. Chernikov 
1952d7968c29SAlexander V. Chernikov 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
1953d7968c29SAlexander V. Chernikov 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
1954d7968c29SAlexander V. Chernikov 	    mtod(m, struct ip6_hdr *));
1955d7968c29SAlexander V. Chernikov 
1956d7968c29SAlexander V. Chernikov 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1957d7968c29SAlexander V. Chernikov 		origifp = ifp;
1958d7968c29SAlexander V. Chernikov 
1959d7968c29SAlexander V. Chernikov 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
1960d7968c29SAlexander V. Chernikov 	return (error);
1961d7968c29SAlexander V. Chernikov }
1962d7968c29SAlexander V. Chernikov 
1963f6990c4eSAlexander V. Chernikov /*
1964f6990c4eSAlexander V. Chernikov  * IPv6 packet output - light version.
1965f6990c4eSAlexander V. Chernikov  * Checks if destination LLE exists and is in proper state
1966f6990c4eSAlexander V. Chernikov  * (e.g no modification required). If not true, fall back to
1967f6990c4eSAlexander V. Chernikov  * "heavy" version.
1968f6990c4eSAlexander V. Chernikov  */
196982cd038dSYoshinobu Inoue int
1970f6990c4eSAlexander V. Chernikov nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
19711272577eSXin LI     struct sockaddr_in6 *dst, struct rtentry *rt0)
197282cd038dSYoshinobu Inoue {
1973f6990c4eSAlexander V. Chernikov 	struct llentry *ln = NULL;
19746e6b3f7cSQing Li 
1975f6990c4eSAlexander V. Chernikov 	/* discard the packet if IPv6 operation is disabled on the interface */
1976f6990c4eSAlexander V. Chernikov 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1977f6990c4eSAlexander V. Chernikov 		m_freem(m);
1978f6990c4eSAlexander V. Chernikov 		return (ENETDOWN); /* better error? */
1979f6990c4eSAlexander V. Chernikov 	}
1980f6990c4eSAlexander V. Chernikov 
1981f6990c4eSAlexander V. Chernikov 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1982f6990c4eSAlexander V. Chernikov 		goto sendpkt;
1983f6990c4eSAlexander V. Chernikov 
1984f6990c4eSAlexander V. Chernikov 	if (nd6_need_cache(ifp) == 0)
1985f6990c4eSAlexander V. Chernikov 		goto sendpkt;
1986f6990c4eSAlexander V. Chernikov 
1987f6990c4eSAlexander V. Chernikov 	IF_AFDATA_RLOCK(ifp);
1988f6990c4eSAlexander V. Chernikov 	ln = nd6_lookup(&dst->sin6_addr, 0, ifp);
1989f6990c4eSAlexander V. Chernikov 	IF_AFDATA_RUNLOCK(ifp);
1990f6990c4eSAlexander V. Chernikov 
1991f6990c4eSAlexander V. Chernikov 	/*
1992f6990c4eSAlexander V. Chernikov 	 * Perform fast path for the following cases:
1993f6990c4eSAlexander V. Chernikov 	 * 1) lle state is REACHABLE
1994f6990c4eSAlexander V. Chernikov 	 * 2) lle state is DELAY (NS message sentNS message sent)
1995f6990c4eSAlexander V. Chernikov 	 *
1996f6990c4eSAlexander V. Chernikov 	 * Every other case involves lle modification, so we handle
1997f6990c4eSAlexander V. Chernikov 	 * them separately.
1998f6990c4eSAlexander V. Chernikov 	 */
1999f6990c4eSAlexander V. Chernikov 	if (ln == NULL || (ln->ln_state != ND6_LLINFO_REACHABLE &&
2000f6990c4eSAlexander V. Chernikov 	    ln->ln_state != ND6_LLINFO_DELAY)) {
2001f6990c4eSAlexander V. Chernikov 		/* Fall back to slow processing path */
2002f6990c4eSAlexander V. Chernikov 		if (ln != NULL)
2003f6990c4eSAlexander V. Chernikov 			LLE_RUNLOCK(ln);
2004d7968c29SAlexander V. Chernikov 		return (nd6_output_lle(ifp, origifp, m, dst));
2005f6990c4eSAlexander V. Chernikov 	}
2006f6990c4eSAlexander V. Chernikov 
2007f6990c4eSAlexander V. Chernikov sendpkt:
2008f6990c4eSAlexander V. Chernikov 	if (ln != NULL)
2009f6990c4eSAlexander V. Chernikov 		LLE_RUNLOCK(ln);
2010f6990c4eSAlexander V. Chernikov 
2011d7968c29SAlexander V. Chernikov 	return (nd6_output_ifp(ifp, origifp, m, dst));
20126e6b3f7cSQing Li }
20136e6b3f7cSQing Li 
20146e6b3f7cSQing Li 
20156e6b3f7cSQing Li /*
2016f6990c4eSAlexander V. Chernikov  * Output IPv6 packet - heavy version.
2017f6990c4eSAlexander V. Chernikov  * Function assume that either
2018f6990c4eSAlexander V. Chernikov  * 1) destination LLE does not exist, is invalid or stale, so
2019f6990c4eSAlexander V. Chernikov  *   ND6_EXCLUSIVE lock needs to be acquired
2020f6990c4eSAlexander V. Chernikov  * 2) destination lle is provided (with ND6_EXCLUSIVE lock),
2021f6990c4eSAlexander V. Chernikov  *   in that case packets are queued in &chain.
20226e6b3f7cSQing Li  *
20236e6b3f7cSQing Li  */
2024d7968c29SAlexander V. Chernikov static int
2025f6990c4eSAlexander V. Chernikov nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2026d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *dst)
20276e6b3f7cSQing Li {
20285a255516SAlexander V. Chernikov 	struct llentry *lle = NULL, *lle_tmp;
202982cd038dSYoshinobu Inoue 
20309dffa6a3SAlexander V. Chernikov 	KASSERT(m != NULL, ("NULL mbuf, nothing to send"));
20319dffa6a3SAlexander V. Chernikov 	/* discard the packet if IPv6 operation is disabled on the interface */
20329dffa6a3SAlexander V. Chernikov 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
20339dffa6a3SAlexander V. Chernikov 		m_freem(m);
20349dffa6a3SAlexander V. Chernikov 		return (ENETDOWN); /* better error? */
20359dffa6a3SAlexander V. Chernikov 	}
20369dffa6a3SAlexander V. Chernikov 
203782cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
203882cd038dSYoshinobu Inoue 		goto sendpkt;
203982cd038dSYoshinobu Inoue 
204033841545SHajimu UMEMOTO 	if (nd6_need_cache(ifp) == 0)
204182cd038dSYoshinobu Inoue 		goto sendpkt;
204282cd038dSYoshinobu Inoue 
204382cd038dSYoshinobu Inoue 	/*
204482cd038dSYoshinobu Inoue 	 * Address resolution or Neighbor Unreachability Detection
204582cd038dSYoshinobu Inoue 	 * for the next hop.
204682cd038dSYoshinobu Inoue 	 * At this point, the destination of the packet must be a unicast
204782cd038dSYoshinobu Inoue 	 * or an anycast address(i.e. not a multicast).
204882cd038dSYoshinobu Inoue 	 */
2049f6990c4eSAlexander V. Chernikov 	if (lle == NULL) {
2050c445d252SAdrian Chadd 		IF_AFDATA_RLOCK(ifp);
2051f6990c4eSAlexander V. Chernikov 		lle = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
2052c445d252SAdrian Chadd 		IF_AFDATA_RUNLOCK(ifp);
2053f6990c4eSAlexander V. Chernikov 		if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
2054686cdd19SJun-ichiro itojun Hagino 			/*
2055686cdd19SJun-ichiro itojun Hagino 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2056686cdd19SJun-ichiro itojun Hagino 			 * the condition below is not very efficient.  But we believe
2057686cdd19SJun-ichiro itojun Hagino 			 * it is tolerable, because this should be a rare case.
2058686cdd19SJun-ichiro itojun Hagino 			 */
20595a255516SAlexander V. Chernikov 			lle = nd6_alloc(&dst->sin6_addr, 0, ifp);
2060f6990c4eSAlexander V. Chernikov 			if (lle == NULL) {
20611d54aa3bSBjoern A. Zeeb 				char ip6buf[INET6_ADDRSTRLEN];
2062686cdd19SJun-ichiro itojun Hagino 				log(LOG_DEBUG,
2063686cdd19SJun-ichiro itojun Hagino 				    "nd6_output: can't allocate llinfo for %s "
2064bbd8aebaSQing Li 				    "(ln=%p)\n",
2065f6990c4eSAlexander V. Chernikov 				    ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
20669dffa6a3SAlexander V. Chernikov 				m_freem(m);
20679dffa6a3SAlexander V. Chernikov 				return (ENOBUFS);
206882cd038dSYoshinobu Inoue 			}
20695a255516SAlexander V. Chernikov 			lle->ln_state = ND6_LLINFO_NOSTATE;
20705a255516SAlexander V. Chernikov 
20715a255516SAlexander V. Chernikov 			IF_AFDATA_WLOCK(ifp);
20725a255516SAlexander V. Chernikov 			LLE_WLOCK(lle);
20735a255516SAlexander V. Chernikov 			/* Prefer any existing entry over newly-created one */
20745a255516SAlexander V. Chernikov 			lle_tmp = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
20755a255516SAlexander V. Chernikov 			if (lle_tmp == NULL)
20765a255516SAlexander V. Chernikov 				lltable_link_entry(LLTABLE6(ifp), lle);
20775a255516SAlexander V. Chernikov 			IF_AFDATA_WUNLOCK(ifp);
20785a255516SAlexander V. Chernikov 			if (lle_tmp != NULL) {
20795a255516SAlexander V. Chernikov 				lltable_free_entry(LLTABLE6(ifp), lle);
20805a255516SAlexander V. Chernikov 				lle = lle_tmp;
20815a255516SAlexander V. Chernikov 				lle_tmp = NULL;
20825a255516SAlexander V. Chernikov 			}
20835a255516SAlexander V. Chernikov 		}
20845a255516SAlexander V. Chernikov 	}
20855a255516SAlexander V. Chernikov 	if (lle == NULL) {
20865a255516SAlexander V. Chernikov 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
20875a255516SAlexander V. Chernikov 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
20885a255516SAlexander V. Chernikov 			m_freem(m);
20895a255516SAlexander V. Chernikov 			return (ENOBUFS);
20905a255516SAlexander V. Chernikov 		}
2091686cdd19SJun-ichiro itojun Hagino 		goto sendpkt;	/* send anyway */
2092686cdd19SJun-ichiro itojun Hagino 	}
2093686cdd19SJun-ichiro itojun Hagino 
2094f6990c4eSAlexander V. Chernikov 	LLE_WLOCK_ASSERT(lle);
2095f6990c4eSAlexander V. Chernikov 
2096686cdd19SJun-ichiro itojun Hagino 	/* We don't have to do link-layer address resolution on a p2p link. */
2097686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2098f6990c4eSAlexander V. Chernikov 	    lle->ln_state < ND6_LLINFO_REACHABLE) {
2099f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_STALE;
2100f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_gctimer * hz);
210133841545SHajimu UMEMOTO 	}
210282cd038dSYoshinobu Inoue 
210382cd038dSYoshinobu Inoue 	/*
210482cd038dSYoshinobu Inoue 	 * The first time we send a packet to a neighbor whose entry is
210582cd038dSYoshinobu Inoue 	 * STALE, we have to change the state to DELAY and a sets a timer to
210682cd038dSYoshinobu Inoue 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
210782cd038dSYoshinobu Inoue 	 * neighbor unreachability detection on expiration.
210882cd038dSYoshinobu Inoue 	 * (RFC 2461 7.3.3)
210982cd038dSYoshinobu Inoue 	 */
2110f6990c4eSAlexander V. Chernikov 	if (lle->ln_state == ND6_LLINFO_STALE) {
2111f6990c4eSAlexander V. Chernikov 		lle->la_asked = 0;
2112f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_DELAY;
2113f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_delay * hz);
211482cd038dSYoshinobu Inoue 	}
211582cd038dSYoshinobu Inoue 
211682cd038dSYoshinobu Inoue 	/*
211782cd038dSYoshinobu Inoue 	 * If the neighbor cache entry has a state other than INCOMPLETE
211888ff5695SSUZUKI Shinsuke 	 * (i.e. its link-layer address is already resolved), just
211982cd038dSYoshinobu Inoue 	 * send the packet.
212082cd038dSYoshinobu Inoue 	 */
2121f6990c4eSAlexander V. Chernikov 	if (lle->ln_state > ND6_LLINFO_INCOMPLETE)
212282cd038dSYoshinobu Inoue 		goto sendpkt;
212382cd038dSYoshinobu Inoue 
212482cd038dSYoshinobu Inoue 	/*
212582cd038dSYoshinobu Inoue 	 * There is a neighbor cache entry, but no ethernet address
2126743eee66SSUZUKI Shinsuke 	 * response yet.  Append this latest packet to the end of the
2127743eee66SSUZUKI Shinsuke 	 * packet queue in the mbuf, unless the number of the packet
2128743eee66SSUZUKI Shinsuke 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2129743eee66SSUZUKI Shinsuke 	 * the oldest packet in the queue will be removed.
213082cd038dSYoshinobu Inoue 	 */
2131f6990c4eSAlexander V. Chernikov 	if (lle->ln_state == ND6_LLINFO_NOSTATE)
2132f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_INCOMPLETE;
21336e6b3f7cSQing Li 
2134f6990c4eSAlexander V. Chernikov 	if (lle->la_hold != NULL) {
2135743eee66SSUZUKI Shinsuke 		struct mbuf *m_hold;
2136743eee66SSUZUKI Shinsuke 		int i;
2137743eee66SSUZUKI Shinsuke 
2138743eee66SSUZUKI Shinsuke 		i = 0;
2139f6990c4eSAlexander V. Chernikov 		for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2140743eee66SSUZUKI Shinsuke 			i++;
2141743eee66SSUZUKI Shinsuke 			if (m_hold->m_nextpkt == NULL) {
2142743eee66SSUZUKI Shinsuke 				m_hold->m_nextpkt = m;
2143743eee66SSUZUKI Shinsuke 				break;
214482cd038dSYoshinobu Inoue 			}
214582cd038dSYoshinobu Inoue 		}
2146603724d3SBjoern A. Zeeb 		while (i >= V_nd6_maxqueuelen) {
2147f6990c4eSAlexander V. Chernikov 			m_hold = lle->la_hold;
2148f6990c4eSAlexander V. Chernikov 			lle->la_hold = lle->la_hold->m_nextpkt;
214931d4137bSSUZUKI Shinsuke 			m_freem(m_hold);
2150743eee66SSUZUKI Shinsuke 			i--;
2151743eee66SSUZUKI Shinsuke 		}
2152743eee66SSUZUKI Shinsuke 	} else {
2153f6990c4eSAlexander V. Chernikov 		lle->la_hold = m;
21546e6b3f7cSQing Li 	}
2155743eee66SSUZUKI Shinsuke 
2156743eee66SSUZUKI Shinsuke 	/*
2157743eee66SSUZUKI Shinsuke 	 * If there has been no NS for the neighbor after entering the
2158743eee66SSUZUKI Shinsuke 	 * INCOMPLETE state, send the first solicitation.
2159743eee66SSUZUKI Shinsuke 	 */
2160f6990c4eSAlexander V. Chernikov 	if (!ND6_LLINFO_PERMANENT(lle) && lle->la_asked == 0) {
216126deb882SAlexander V. Chernikov 		struct in6_addr src, *psrc;
2162f6990c4eSAlexander V. Chernikov 		lle->la_asked++;
21636e6b3f7cSQing Li 
2164f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle,
2165743eee66SSUZUKI Shinsuke 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
216626deb882SAlexander V. Chernikov 		psrc = nd6_llinfo_get_holdsrc(lle, &src);
2167f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
216826deb882SAlexander V. Chernikov 		nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL);
2169d7968c29SAlexander V. Chernikov 	} else {
2170d7968c29SAlexander V. Chernikov 		/* We did the lookup so we need to do the unlock here. */
2171f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
2172743eee66SSUZUKI Shinsuke 	}
2173e6950476SBjoern A. Zeeb 
217482cd038dSYoshinobu Inoue 	return (0);
217582cd038dSYoshinobu Inoue 
217682cd038dSYoshinobu Inoue   sendpkt:
2177d7968c29SAlexander V. Chernikov 	if (lle != NULL)
2178f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
21795b27b045SSUZUKI Shinsuke 
2180d7968c29SAlexander V. Chernikov 	return (nd6_output_ifp(ifp, origifp, m, dst));
218182cd038dSYoshinobu Inoue }
218282cd038dSYoshinobu Inoue 
21836e6b3f7cSQing Li 
21846e6b3f7cSQing Li int
2185d7968c29SAlexander V. Chernikov nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
21869c9bde01SAlexander V. Chernikov     struct sockaddr_in6 *dst)
21876e6b3f7cSQing Li {
21886e6b3f7cSQing Li 	struct mbuf *m, *m_head;
21896e6b3f7cSQing Li 	struct ifnet *outifp;
21906e6b3f7cSQing Li 	int error = 0;
21916e6b3f7cSQing Li 
21926e6b3f7cSQing Li 	m_head = chain;
21936e6b3f7cSQing Li 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
21946e6b3f7cSQing Li 		outifp = origifp;
21956e6b3f7cSQing Li 	else
21966e6b3f7cSQing Li 		outifp = ifp;
21976e6b3f7cSQing Li 
21986e6b3f7cSQing Li 	while (m_head) {
21996e6b3f7cSQing Li 		m = m_head;
22006e6b3f7cSQing Li 		m_head = m_head->m_nextpkt;
2201d7968c29SAlexander V. Chernikov 		error = nd6_output_ifp(ifp, origifp, m, dst);
22026e6b3f7cSQing Li 	}
22036e6b3f7cSQing Li 
22046e6b3f7cSQing Li 	/*
22056e6b3f7cSQing Li 	 * XXX
22066e6b3f7cSQing Li 	 * note that intermediate errors are blindly ignored - but this is
22076e6b3f7cSQing Li 	 * the same convention as used with nd6_output when called by
22086e6b3f7cSQing Li 	 * nd6_cache_lladdr
22096e6b3f7cSQing Li 	 */
22106e6b3f7cSQing Li 	return (error);
22116e6b3f7cSQing Li }
22126e6b3f7cSQing Li 
22136e6b3f7cSQing Li 
221482cd038dSYoshinobu Inoue int
22151272577eSXin LI nd6_need_cache(struct ifnet *ifp)
221633841545SHajimu UMEMOTO {
221733841545SHajimu UMEMOTO 	/*
221833841545SHajimu UMEMOTO 	 * XXX: we currently do not make neighbor cache on any interface
221933841545SHajimu UMEMOTO 	 * other than ARCnet, Ethernet, FDDI and GIF.
222033841545SHajimu UMEMOTO 	 *
222133841545SHajimu UMEMOTO 	 * RFC2893 says:
222233841545SHajimu UMEMOTO 	 * - unidirectional tunnels needs no ND
222333841545SHajimu UMEMOTO 	 */
222433841545SHajimu UMEMOTO 	switch (ifp->if_type) {
222533841545SHajimu UMEMOTO 	case IFT_ARCNET:
222633841545SHajimu UMEMOTO 	case IFT_ETHER:
222733841545SHajimu UMEMOTO 	case IFT_FDDI:
222833841545SHajimu UMEMOTO 	case IFT_IEEE1394:
222905b6760dSMunechika SUMIKAWA 	case IFT_L2VLAN:
223033841545SHajimu UMEMOTO 	case IFT_IEEE80211:
2231e4cd31ddSJeff Roberson 	case IFT_INFINIBAND:
223259280079SAndrew Thompson 	case IFT_BRIDGE:
2233c57086ceSHajimu UMEMOTO 	case IFT_PROPVIRTUAL:
223433841545SHajimu UMEMOTO 		return (1);
223533841545SHajimu UMEMOTO 	default:
223633841545SHajimu UMEMOTO 		return (0);
223733841545SHajimu UMEMOTO 	}
223833841545SHajimu UMEMOTO }
223933841545SHajimu UMEMOTO 
22406e6b3f7cSQing Li /*
2241f6b84910SAlexander V. Chernikov  * Add pernament ND6 link-layer record for given
2242f6b84910SAlexander V. Chernikov  * interface address.
2243f6b84910SAlexander V. Chernikov  *
2244f6b84910SAlexander V. Chernikov  * Very similar to IPv4 arp_ifinit(), but:
2245f6b84910SAlexander V. Chernikov  * 1) IPv6 DAD is performed in different place
2246f6b84910SAlexander V. Chernikov  * 2) It is called by IPv6 protocol stack in contrast to
2247f6b84910SAlexander V. Chernikov  * arp_ifinit() which is typically called in SIOCSIFADDR
2248f6b84910SAlexander V. Chernikov  * driver ioctl handler.
2249f6b84910SAlexander V. Chernikov  *
2250f6b84910SAlexander V. Chernikov  */
2251f6b84910SAlexander V. Chernikov int
2252f6b84910SAlexander V. Chernikov nd6_add_ifa_lle(struct in6_ifaddr *ia)
2253f6b84910SAlexander V. Chernikov {
2254f6b84910SAlexander V. Chernikov 	struct ifnet *ifp;
22555a255516SAlexander V. Chernikov 	struct llentry *ln, *ln_tmp;
22565a255516SAlexander V. Chernikov 	struct sockaddr *dst;
2257f6b84910SAlexander V. Chernikov 
2258f6b84910SAlexander V. Chernikov 	ifp = ia->ia_ifa.ifa_ifp;
2259af6209a1SAndrey V. Elsukov 	if (nd6_need_cache(ifp) == 0)
2260af6209a1SAndrey V. Elsukov 		return (0);
22615a255516SAlexander V. Chernikov 
2262f6b84910SAlexander V. Chernikov 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
22635a255516SAlexander V. Chernikov 	dst = (struct sockaddr *)&ia->ia_addr;
22645a255516SAlexander V. Chernikov 	ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst);
22655a255516SAlexander V. Chernikov 	if (ln == NULL)
22665a255516SAlexander V. Chernikov 		return (ENOBUFS);
22675a255516SAlexander V. Chernikov 
22685a255516SAlexander V. Chernikov 	IF_AFDATA_WLOCK(ifp);
22695a255516SAlexander V. Chernikov 	LLE_WLOCK(ln);
22705a255516SAlexander V. Chernikov 	/* Unlink any entry if exists */
22715a255516SAlexander V. Chernikov 	ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_EXCLUSIVE, dst);
22725a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22735a255516SAlexander V. Chernikov 		lltable_unlink_entry(LLTABLE6(ifp), ln_tmp);
22745a255516SAlexander V. Chernikov 	lltable_link_entry(LLTABLE6(ifp), ln);
22755a255516SAlexander V. Chernikov 	IF_AFDATA_WUNLOCK(ifp);
22765a255516SAlexander V. Chernikov 
22775a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22785a255516SAlexander V. Chernikov 		EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED);
22795a255516SAlexander V. Chernikov 	EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
22805a255516SAlexander V. Chernikov 
22815a255516SAlexander V. Chernikov 	LLE_WUNLOCK(ln);
22825a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22835a255516SAlexander V. Chernikov 		llentry_free(ln_tmp);
22845a255516SAlexander V. Chernikov 
22855a255516SAlexander V. Chernikov 	return (0);
2286f6b84910SAlexander V. Chernikov }
2287f6b84910SAlexander V. Chernikov 
2288f6b84910SAlexander V. Chernikov /*
22893e7a2321SAlexander V. Chernikov  * Removes either all lle entries for given @ia, or lle
22903e7a2321SAlexander V. Chernikov  * corresponding to @ia address.
2291f6b84910SAlexander V. Chernikov  */
2292f6b84910SAlexander V. Chernikov void
22933e7a2321SAlexander V. Chernikov nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all)
2294f6b84910SAlexander V. Chernikov {
2295f6b84910SAlexander V. Chernikov 	struct sockaddr_in6 mask, addr;
22963e7a2321SAlexander V. Chernikov 	struct sockaddr *saddr, *smask;
2297f6b84910SAlexander V. Chernikov 	struct ifnet *ifp;
2298f6b84910SAlexander V. Chernikov 
2299f6b84910SAlexander V. Chernikov 	ifp = ia->ia_ifa.ifa_ifp;
2300f6b84910SAlexander V. Chernikov 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2301f6b84910SAlexander V. Chernikov 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
23023e7a2321SAlexander V. Chernikov 	saddr = (struct sockaddr *)&addr;
23033e7a2321SAlexander V. Chernikov 	smask = (struct sockaddr *)&mask;
23043e7a2321SAlexander V. Chernikov 
23053e7a2321SAlexander V. Chernikov 	if (all != 0)
23063e7a2321SAlexander V. Chernikov 		lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC);
23073e7a2321SAlexander V. Chernikov 	else
23083e7a2321SAlexander V. Chernikov 		lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr);
2309f6b84910SAlexander V. Chernikov }
2310f6b84910SAlexander V. Chernikov 
2311f6b84910SAlexander V. Chernikov /*
23126e6b3f7cSQing Li  * the callers of this function need to be re-worked to drop
23136e6b3f7cSQing Li  * the lle lock, drop here for now
23146e6b3f7cSQing Li  */
231533841545SHajimu UMEMOTO int
2316bbd8aebaSQing Li nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
231774860d4fSAlexander V. Chernikov     const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
231882cd038dSYoshinobu Inoue {
23196e6b3f7cSQing Li 	struct llentry *ln;
232082cd038dSYoshinobu Inoue 
232174860d4fSAlexander V. Chernikov 	if (pflags != NULL)
232274860d4fSAlexander V. Chernikov 		*pflags = 0;
23236e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
2324f31b83e1SNavdeep Parhar 	if (m != NULL && m->m_flags & M_MCAST) {
232582cd038dSYoshinobu Inoue 		switch (ifp->if_type) {
232682cd038dSYoshinobu Inoue 		case IFT_ETHER:
232782cd038dSYoshinobu Inoue 		case IFT_FDDI:
232805b6760dSMunechika SUMIKAWA 		case IFT_L2VLAN:
232933841545SHajimu UMEMOTO 		case IFT_IEEE80211:
233059280079SAndrew Thompson 		case IFT_BRIDGE:
23312049fdeeSMatthew N. Dodd 		case IFT_ISO88025:
233282cd038dSYoshinobu Inoue 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
233382cd038dSYoshinobu Inoue 						 desten);
2334354c3d34SLuigi Rizzo 			return (0);
233582cd038dSYoshinobu Inoue 		default:
2336fef5fd23SBosko Milekic 			m_freem(m);
2337354c3d34SLuigi Rizzo 			return (EAFNOSUPPORT);
233882cd038dSYoshinobu Inoue 		}
233982cd038dSYoshinobu Inoue 	}
234082cd038dSYoshinobu Inoue 
23416e6b3f7cSQing Li 
23426e6b3f7cSQing Li 	/*
23436e6b3f7cSQing Li 	 * the entry should have been created in nd6_store_lladdr
23446e6b3f7cSQing Li 	 */
234573cb2f38SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
23466e6b3f7cSQing Li 	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
234773cb2f38SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
23486e6b3f7cSQing Li 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
234915209fb6SKip Macy 		if (ln != NULL)
23506e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
2351530f95fcSGleb Smirnoff 		/* this could happen, if we could not allocate memory */
2352530f95fcSGleb Smirnoff 		m_freem(m);
23536e6b3f7cSQing Li 		return (1);
2354530f95fcSGleb Smirnoff 	}
2355530f95fcSGleb Smirnoff 
23566e6b3f7cSQing Li 	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
235774860d4fSAlexander V. Chernikov 	if (pflags != NULL)
235874860d4fSAlexander V. Chernikov 		*pflags = ln->la_flags;
23596e6b3f7cSQing Li 	LLE_RUNLOCK(ln);
23606e6b3f7cSQing Li 	/*
23616e6b3f7cSQing Li 	 * A *small* use after free race exists here
23626e6b3f7cSQing Li 	 */
2363cd46a114SLuigi Rizzo 	return (0);
236482cd038dSYoshinobu Inoue }
236533841545SHajimu UMEMOTO 
236631d4137bSSUZUKI Shinsuke static void
23676e6b3f7cSQing Li clear_llinfo_pqueue(struct llentry *ln)
236831d4137bSSUZUKI Shinsuke {
236931d4137bSSUZUKI Shinsuke 	struct mbuf *m_hold, *m_hold_next;
237031d4137bSSUZUKI Shinsuke 
23716e6b3f7cSQing Li 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
237231d4137bSSUZUKI Shinsuke 		m_hold_next = m_hold->m_nextpkt;
237331d4137bSSUZUKI Shinsuke 		m_freem(m_hold);
237431d4137bSSUZUKI Shinsuke 	}
237531d4137bSSUZUKI Shinsuke 
23766e6b3f7cSQing Li 	ln->la_hold = NULL;
237731d4137bSSUZUKI Shinsuke 	return;
237831d4137bSSUZUKI Shinsuke }
237931d4137bSSUZUKI Shinsuke 
238033841545SHajimu UMEMOTO static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
238133841545SHajimu UMEMOTO static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
238233841545SHajimu UMEMOTO #ifdef SYSCTL_DECL
238333841545SHajimu UMEMOTO SYSCTL_DECL(_net_inet6_icmp6);
238433841545SHajimu UMEMOTO #endif
238533841545SHajimu UMEMOTO SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
238633841545SHajimu UMEMOTO 	CTLFLAG_RD, nd6_sysctl_drlist, "");
238733841545SHajimu UMEMOTO SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
238833841545SHajimu UMEMOTO 	CTLFLAG_RD, nd6_sysctl_prlist, "");
23896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
23906df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
23916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
23926df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
239333841545SHajimu UMEMOTO 
239433841545SHajimu UMEMOTO static int
239533841545SHajimu UMEMOTO nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
239633841545SHajimu UMEMOTO {
23977195094cSMarius Strobl 	struct in6_defrouter d;
239833841545SHajimu UMEMOTO 	struct nd_defrouter *dr;
23997195094cSMarius Strobl 	int error;
240033841545SHajimu UMEMOTO 
240133841545SHajimu UMEMOTO 	if (req->newptr)
24027195094cSMarius Strobl 		return (EPERM);
240333841545SHajimu UMEMOTO 
24047195094cSMarius Strobl 	bzero(&d, sizeof(d));
24057195094cSMarius Strobl 	d.rtaddr.sin6_family = AF_INET6;
24067195094cSMarius Strobl 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
24077195094cSMarius Strobl 
24087195094cSMarius Strobl 	/*
24097195094cSMarius Strobl 	 * XXX locking
24107195094cSMarius Strobl 	 */
24113b0b2840SJohn Baldwin 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
24127195094cSMarius Strobl 		d.rtaddr.sin6_addr = dr->rtaddr;
24137195094cSMarius Strobl 		error = sa6_recoverscope(&d.rtaddr);
2414edbb8b46SPeter Wemm 		if (error != 0)
2415b2630c29SGeorge V. Neville-Neil 			return (error);
24167195094cSMarius Strobl 		d.flags = dr->flags;
24177195094cSMarius Strobl 		d.rtlifetime = dr->rtlifetime;
2418ffa0165aSHiroki Sato 		d.expire = dr->expire + (time_second - time_uptime);
24197195094cSMarius Strobl 		d.if_index = dr->ifp->if_index;
24207195094cSMarius Strobl 		error = SYSCTL_OUT(req, &d, sizeof(d));
24217195094cSMarius Strobl 		if (error != 0)
242207eb2995SHajimu UMEMOTO 			return (error);
242333841545SHajimu UMEMOTO 	}
24247195094cSMarius Strobl 	return (0);
24257195094cSMarius Strobl }
242633841545SHajimu UMEMOTO 
242733841545SHajimu UMEMOTO static int
242833841545SHajimu UMEMOTO nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
242933841545SHajimu UMEMOTO {
24307195094cSMarius Strobl 	struct in6_prefix p;
24317195094cSMarius Strobl 	struct sockaddr_in6 s6;
243233841545SHajimu UMEMOTO 	struct nd_prefix *pr;
24337195094cSMarius Strobl 	struct nd_pfxrouter *pfr;
24347195094cSMarius Strobl 	time_t maxexpire;
24357195094cSMarius Strobl 	int error;
24361d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
243733841545SHajimu UMEMOTO 
243833841545SHajimu UMEMOTO 	if (req->newptr)
24397195094cSMarius Strobl 		return (EPERM);
244033841545SHajimu UMEMOTO 
24417195094cSMarius Strobl 	bzero(&p, sizeof(p));
24427195094cSMarius Strobl 	p.origin = PR_ORIG_RA;
24437195094cSMarius Strobl 	bzero(&s6, sizeof(s6));
24447195094cSMarius Strobl 	s6.sin6_family = AF_INET6;
24457195094cSMarius Strobl 	s6.sin6_len = sizeof(s6);
24467195094cSMarius Strobl 
24477195094cSMarius Strobl 	/*
24487195094cSMarius Strobl 	 * XXX locking
24497195094cSMarius Strobl 	 */
24503b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
24517195094cSMarius Strobl 		p.prefix = pr->ndpr_prefix;
24527195094cSMarius Strobl 		if (sa6_recoverscope(&p.prefix)) {
24537195094cSMarius Strobl 			log(LOG_ERR, "scope error in prefix list (%s)\n",
24547195094cSMarius Strobl 			    ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2455a1f7e5f8SHajimu UMEMOTO 			/* XXX: press on... */
2456a1f7e5f8SHajimu UMEMOTO 		}
24577195094cSMarius Strobl 		p.raflags = pr->ndpr_raf;
24587195094cSMarius Strobl 		p.prefixlen = pr->ndpr_plen;
24597195094cSMarius Strobl 		p.vltime = pr->ndpr_vltime;
24607195094cSMarius Strobl 		p.pltime = pr->ndpr_pltime;
24617195094cSMarius Strobl 		p.if_index = pr->ndpr_ifp->if_index;
2462743eee66SSUZUKI Shinsuke 		if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
24637195094cSMarius Strobl 			p.expire = 0;
2464743eee66SSUZUKI Shinsuke 		else {
2465743eee66SSUZUKI Shinsuke 			/* XXX: we assume time_t is signed. */
2466743eee66SSUZUKI Shinsuke 			maxexpire = (-1) &
24677195094cSMarius Strobl 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
24687195094cSMarius Strobl 			if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
24697195094cSMarius Strobl 				p.expire = pr->ndpr_lastupdate +
2470ffa0165aSHiroki Sato 				    pr->ndpr_vltime +
2471ffa0165aSHiroki Sato 				    (time_second - time_uptime);
24727195094cSMarius Strobl 			else
24737195094cSMarius Strobl 				p.expire = maxexpire;
2474743eee66SSUZUKI Shinsuke 		}
24757195094cSMarius Strobl 		p.refcnt = pr->ndpr_refcnt;
24767195094cSMarius Strobl 		p.flags = pr->ndpr_stateflags;
24777195094cSMarius Strobl 		p.advrtrs = 0;
24787195094cSMarius Strobl 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
24797195094cSMarius Strobl 			p.advrtrs++;
24807195094cSMarius Strobl 		error = SYSCTL_OUT(req, &p, sizeof(p));
24817195094cSMarius Strobl 		if (error != 0)
248207eb2995SHajimu UMEMOTO 			return (error);
24837195094cSMarius Strobl 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
24847195094cSMarius Strobl 			s6.sin6_addr = pfr->router->rtaddr;
24857195094cSMarius Strobl 			if (sa6_recoverscope(&s6))
24867195094cSMarius Strobl 				log(LOG_ERR,
24877195094cSMarius Strobl 				    "scope error in prefix list (%s)\n",
24887195094cSMarius Strobl 				    ip6_sprintf(ip6buf, &pfr->router->rtaddr));
24897195094cSMarius Strobl 			error = SYSCTL_OUT(req, &s6, sizeof(s6));
24907195094cSMarius Strobl 			if (error != 0)
24917195094cSMarius Strobl 				return (error);
24927195094cSMarius Strobl 		}
24937195094cSMarius Strobl 	}
24947195094cSMarius Strobl 	return (0);
249533841545SHajimu UMEMOTO }
2496