xref: /freebsd/sys/netinet6/nd6.c (revision 3e7a2321e36754ec10eb4b83a434197b896c0a45)
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);
1359233d8f3SDavid E. O'Brien static void nd6_llinfo_timer(void *);
1366e6b3f7cSQing Li static void clear_llinfo_pqueue(struct llentry *);
137f6b84910SAlexander V. Chernikov static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
138d7968c29SAlexander V. Chernikov static int nd6_output_lle(struct ifnet *, struct ifnet *, struct mbuf *,
139d7968c29SAlexander V. Chernikov 	struct sockaddr_in6 *);
140abc1be90SAlexander V. Chernikov static int nd6_output_ifp(struct ifnet *, struct ifnet *, struct mbuf *,
141abc1be90SAlexander V. Chernikov     struct sockaddr_in6 *);
14233841545SHajimu UMEMOTO 
1433e288e62SDimitry Andric static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
1441e77c105SRobert Watson #define	V_nd6_slowtimo_ch		VNET(nd6_slowtimo_ch)
145eddfbb76SRobert Watson 
146eddfbb76SRobert Watson VNET_DEFINE(struct callout, nd6_timer_ch);
147eddfbb76SRobert Watson 
14830aee131SAndrey V. Elsukov static void
14930aee131SAndrey V. Elsukov nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
15030aee131SAndrey V. Elsukov {
15130aee131SAndrey V. Elsukov 	struct rt_addrinfo rtinfo;
152314294deSAlexander V. Chernikov 	struct sockaddr_in6 dst;
15330aee131SAndrey V. Elsukov 	struct sockaddr_dl gw;
15430aee131SAndrey V. Elsukov 	struct ifnet *ifp;
15530aee131SAndrey V. Elsukov 	int type;
15630aee131SAndrey V. Elsukov 
15730aee131SAndrey V. Elsukov 	LLE_WLOCK_ASSERT(lle);
15830aee131SAndrey V. Elsukov 
159314294deSAlexander V. Chernikov 	if (lltable_get_af(lle->lle_tbl) != AF_INET6)
160314294deSAlexander V. Chernikov 		return;
161314294deSAlexander V. Chernikov 
16230aee131SAndrey V. Elsukov 	switch (evt) {
16330aee131SAndrey V. Elsukov 	case LLENTRY_RESOLVED:
16430aee131SAndrey V. Elsukov 		type = RTM_ADD;
16530aee131SAndrey V. Elsukov 		KASSERT(lle->la_flags & LLE_VALID,
16630aee131SAndrey V. Elsukov 		    ("%s: %p resolved but not valid?", __func__, lle));
16730aee131SAndrey V. Elsukov 		break;
16830aee131SAndrey V. Elsukov 	case LLENTRY_EXPIRED:
16930aee131SAndrey V. Elsukov 		type = RTM_DELETE;
17030aee131SAndrey V. Elsukov 		break;
17130aee131SAndrey V. Elsukov 	default:
17230aee131SAndrey V. Elsukov 		return;
17330aee131SAndrey V. Elsukov 	}
17430aee131SAndrey V. Elsukov 
175314294deSAlexander V. Chernikov 	ifp = lltable_get_ifp(lle->lle_tbl);
17630aee131SAndrey V. Elsukov 
17730aee131SAndrey V. Elsukov 	bzero(&dst, sizeof(dst));
17830aee131SAndrey V. Elsukov 	bzero(&gw, sizeof(gw));
17930aee131SAndrey V. Elsukov 	bzero(&rtinfo, sizeof(rtinfo));
180314294deSAlexander V. Chernikov 	lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
18130aee131SAndrey V. Elsukov 	dst.sin6_scope_id = in6_getscopezone(ifp,
182314294deSAlexander V. Chernikov 	    in6_addrscope(&dst.sin6_addr));
18330aee131SAndrey V. Elsukov 	gw.sdl_len = sizeof(struct sockaddr_dl);
18430aee131SAndrey V. Elsukov 	gw.sdl_family = AF_LINK;
18530aee131SAndrey V. Elsukov 	gw.sdl_alen = ifp->if_addrlen;
18630aee131SAndrey V. Elsukov 	gw.sdl_index = ifp->if_index;
18730aee131SAndrey V. Elsukov 	gw.sdl_type = ifp->if_type;
18830aee131SAndrey V. Elsukov 	if (evt == LLENTRY_RESOLVED)
18930aee131SAndrey V. Elsukov 		bcopy(&lle->ll_addr, gw.sdl_data, ifp->if_addrlen);
19030aee131SAndrey V. Elsukov 	rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst;
19130aee131SAndrey V. Elsukov 	rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw;
19230aee131SAndrey V. Elsukov 	rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY;
19330aee131SAndrey V. Elsukov 	rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | (
19430aee131SAndrey V. Elsukov 	    type == RTM_ADD ? RTF_UP: 0), 0, RT_DEFAULT_FIB);
19530aee131SAndrey V. Elsukov }
19630aee131SAndrey V. Elsukov 
19782cd038dSYoshinobu Inoue void
1981272577eSXin LI nd6_init(void)
19982cd038dSYoshinobu Inoue {
20082cd038dSYoshinobu Inoue 
20144e33a07SMarko Zec 	LIST_INIT(&V_nd_prefix);
20244e33a07SMarko Zec 
203686cdd19SJun-ichiro itojun Hagino 	/* initialization of the default router list */
204603724d3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd_defrouter);
20582cea7e6SBjoern A. Zeeb 
20682cd038dSYoshinobu Inoue 	/* start timer */
207603724d3SBjoern A. Zeeb 	callout_init(&V_nd6_slowtimo_ch, 0);
208603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
209f6dfe47aSMarko Zec 	    nd6_slowtimo, curvnet);
210d6ad6a86SMark Johnston 
211d6ad6a86SMark Johnston 	nd6_dad_init();
21230aee131SAndrey V. Elsukov 	if (IS_DEFAULT_VNET(curvnet))
21330aee131SAndrey V. Elsukov 		lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
21430aee131SAndrey V. Elsukov 		    NULL, EVENTHANDLER_PRI_ANY);
21582cd038dSYoshinobu Inoue }
21682cd038dSYoshinobu Inoue 
217bc29160dSMarko Zec #ifdef VIMAGE
218bc29160dSMarko Zec void
219bc29160dSMarko Zec nd6_destroy()
220bc29160dSMarko Zec {
221bc29160dSMarko Zec 
222bc29160dSMarko Zec 	callout_drain(&V_nd6_slowtimo_ch);
223bc29160dSMarko Zec 	callout_drain(&V_nd6_timer_ch);
22430aee131SAndrey V. Elsukov 	if (IS_DEFAULT_VNET(curvnet))
22530aee131SAndrey V. Elsukov 		EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
226bc29160dSMarko Zec }
227bc29160dSMarko Zec #endif
228bc29160dSMarko Zec 
22931b1bfe1SHajimu UMEMOTO struct nd_ifinfo *
2301272577eSXin LI nd6_ifattach(struct ifnet *ifp)
23182cd038dSYoshinobu Inoue {
23231b1bfe1SHajimu UMEMOTO 	struct nd_ifinfo *nd;
23382cd038dSYoshinobu Inoue 
23439e19560SBjoern A. Zeeb 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
23531b1bfe1SHajimu UMEMOTO 	nd->initialized = 1;
23682cd038dSYoshinobu Inoue 
23731b1bfe1SHajimu UMEMOTO 	nd->chlim = IPV6_DEFHLIM;
23831b1bfe1SHajimu UMEMOTO 	nd->basereachable = REACHABLE_TIME;
23931b1bfe1SHajimu UMEMOTO 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
24031b1bfe1SHajimu UMEMOTO 	nd->retrans = RETRANS_TIMER;
241a283298cSHiroki Sato 
242a283298cSHiroki Sato 	nd->flags = ND6_IFF_PERFORMNUD;
243a283298cSHiroki Sato 
244af805644SHiroki Sato 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
245af805644SHiroki Sato 	 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
246af805644SHiroki Sato 	 * default regardless of the V_ip6_auto_linklocal configuration to
247af805644SHiroki Sato 	 * give a reasonable default behavior.
248af805644SHiroki Sato 	 */
249af805644SHiroki Sato 	if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
250af805644SHiroki Sato 	    (ifp->if_flags & IFF_LOOPBACK))
251a283298cSHiroki Sato 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
252af805644SHiroki Sato 	/*
253af805644SHiroki Sato 	 * A loopback interface does not need to accept RTADV.
254af805644SHiroki Sato 	 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
255af805644SHiroki Sato 	 * default regardless of the V_ip6_accept_rtadv configuration to
256af805644SHiroki Sato 	 * prevent the interface from accepting RA messages arrived
257af805644SHiroki Sato 	 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
258af805644SHiroki Sato 	 */
259af805644SHiroki Sato 	if (V_ip6_accept_rtadv &&
260af805644SHiroki Sato 	    !(ifp->if_flags & IFF_LOOPBACK) &&
261af805644SHiroki Sato 	    (ifp->if_type != IFT_BRIDGE))
262a283298cSHiroki Sato 			nd->flags |= ND6_IFF_ACCEPT_RTADV;
263e7fa8d0aSHiroki Sato 	if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
264e7fa8d0aSHiroki Sato 		nd->flags |= ND6_IFF_NO_RADR;
26531b1bfe1SHajimu UMEMOTO 
26631b1bfe1SHajimu UMEMOTO 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
26731b1bfe1SHajimu UMEMOTO 	nd6_setmtu0(ifp, nd);
26831b1bfe1SHajimu UMEMOTO 
26931b1bfe1SHajimu UMEMOTO 	return nd;
27031b1bfe1SHajimu UMEMOTO }
27131b1bfe1SHajimu UMEMOTO 
27231b1bfe1SHajimu UMEMOTO void
2731272577eSXin LI nd6_ifdetach(struct nd_ifinfo *nd)
27431b1bfe1SHajimu UMEMOTO {
27531b1bfe1SHajimu UMEMOTO 
27631b1bfe1SHajimu UMEMOTO 	free(nd, M_IP6NDP);
27782cd038dSYoshinobu Inoue }
27882cd038dSYoshinobu Inoue 
27982cd038dSYoshinobu Inoue /*
28082cd038dSYoshinobu Inoue  * Reset ND level link MTU. This function is called when the physical MTU
28182cd038dSYoshinobu Inoue  * changes, which means we might have to adjust the ND level MTU.
28282cd038dSYoshinobu Inoue  */
28382cd038dSYoshinobu Inoue void
2841272577eSXin LI nd6_setmtu(struct ifnet *ifp)
28582cd038dSYoshinobu Inoue {
28631b1bfe1SHajimu UMEMOTO 
28731b1bfe1SHajimu UMEMOTO 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
28831b1bfe1SHajimu UMEMOTO }
28931b1bfe1SHajimu UMEMOTO 
29031b1bfe1SHajimu UMEMOTO /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
29131b1bfe1SHajimu UMEMOTO void
2921272577eSXin LI nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
29331b1bfe1SHajimu UMEMOTO {
29431b3783cSHajimu UMEMOTO 	u_int32_t omaxmtu;
29531b1bfe1SHajimu UMEMOTO 
29631b3783cSHajimu UMEMOTO 	omaxmtu = ndi->maxmtu;
29782cd038dSYoshinobu Inoue 
29882cd038dSYoshinobu Inoue 	switch (ifp->if_type) {
29931b3783cSHajimu UMEMOTO 	case IFT_ARCNET:
30031b3783cSHajimu UMEMOTO 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
30182cd038dSYoshinobu Inoue 		break;
30282cd038dSYoshinobu Inoue 	case IFT_FDDI:
30331b3783cSHajimu UMEMOTO 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
30482cd038dSYoshinobu Inoue 		break;
3052049fdeeSMatthew N. Dodd 	case IFT_ISO88025:
3062049fdeeSMatthew N. Dodd 		 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
3072049fdeeSMatthew N. Dodd 		 break;
30882cd038dSYoshinobu Inoue 	default:
30982cd038dSYoshinobu Inoue 		ndi->maxmtu = ifp->if_mtu;
31082cd038dSYoshinobu Inoue 		break;
31182cd038dSYoshinobu Inoue 	}
31282cd038dSYoshinobu Inoue 
31382cd038dSYoshinobu Inoue 	/*
31431b3783cSHajimu UMEMOTO 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
31531b3783cSHajimu UMEMOTO 	 * undesirable situation.  We thus notify the operator of the change
31631b3783cSHajimu UMEMOTO 	 * explicitly.  The check for omaxmtu is necessary to restrict the
31731b3783cSHajimu UMEMOTO 	 * log to the case of changing the MTU, not initializing it.
31882cd038dSYoshinobu Inoue 	 */
31931b3783cSHajimu UMEMOTO 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
32031b3783cSHajimu UMEMOTO 		log(LOG_NOTICE, "nd6_setmtu0: "
32131b3783cSHajimu UMEMOTO 		    "new link MTU on %s (%lu) is too small for IPv6\n",
32231b3783cSHajimu UMEMOTO 		    if_name(ifp), (unsigned long)ndi->maxmtu);
32382cd038dSYoshinobu Inoue 	}
32431b3783cSHajimu UMEMOTO 
325603724d3SBjoern A. Zeeb 	if (ndi->maxmtu > V_in6_maxmtu)
32631b3783cSHajimu UMEMOTO 		in6_setmaxmtu(); /* check all interfaces just in case */
32731b3783cSHajimu UMEMOTO 
32882cd038dSYoshinobu Inoue }
32982cd038dSYoshinobu Inoue 
33082cd038dSYoshinobu Inoue void
3311272577eSXin LI nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
33282cd038dSYoshinobu Inoue {
33307eb2995SHajimu UMEMOTO 
33482cd038dSYoshinobu Inoue 	bzero(ndopts, sizeof(*ndopts));
33582cd038dSYoshinobu Inoue 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
33682cd038dSYoshinobu Inoue 	ndopts->nd_opts_last
33782cd038dSYoshinobu Inoue 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
33882cd038dSYoshinobu Inoue 
33982cd038dSYoshinobu Inoue 	if (icmp6len == 0) {
34082cd038dSYoshinobu Inoue 		ndopts->nd_opts_done = 1;
34182cd038dSYoshinobu Inoue 		ndopts->nd_opts_search = NULL;
34282cd038dSYoshinobu Inoue 	}
34382cd038dSYoshinobu Inoue }
34482cd038dSYoshinobu Inoue 
34582cd038dSYoshinobu Inoue /*
34682cd038dSYoshinobu Inoue  * Take one ND option.
34782cd038dSYoshinobu Inoue  */
34882cd038dSYoshinobu Inoue struct nd_opt_hdr *
3491272577eSXin LI nd6_option(union nd_opts *ndopts)
35082cd038dSYoshinobu Inoue {
35182cd038dSYoshinobu Inoue 	struct nd_opt_hdr *nd_opt;
35282cd038dSYoshinobu Inoue 	int olen;
35382cd038dSYoshinobu Inoue 
35439e19560SBjoern A. Zeeb 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
35539e19560SBjoern A. Zeeb 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
35639e19560SBjoern A. Zeeb 	    __func__));
3572ce62dceSSUZUKI Shinsuke 	if (ndopts->nd_opts_search == NULL)
35882cd038dSYoshinobu Inoue 		return NULL;
35982cd038dSYoshinobu Inoue 	if (ndopts->nd_opts_done)
36082cd038dSYoshinobu Inoue 		return NULL;
36182cd038dSYoshinobu Inoue 
36282cd038dSYoshinobu Inoue 	nd_opt = ndopts->nd_opts_search;
36382cd038dSYoshinobu Inoue 
36419391949SKris Kennaway 	/* make sure nd_opt_len is inside the buffer */
36519391949SKris Kennaway 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
36619391949SKris Kennaway 		bzero(ndopts, sizeof(*ndopts));
36719391949SKris Kennaway 		return NULL;
36819391949SKris Kennaway 	}
36919391949SKris Kennaway 
37082cd038dSYoshinobu Inoue 	olen = nd_opt->nd_opt_len << 3;
37182cd038dSYoshinobu Inoue 	if (olen == 0) {
37282cd038dSYoshinobu Inoue 		/*
37382cd038dSYoshinobu Inoue 		 * Message validation requires that all included
37482cd038dSYoshinobu Inoue 		 * options have a length that is greater than zero.
37582cd038dSYoshinobu Inoue 		 */
37682cd038dSYoshinobu Inoue 		bzero(ndopts, sizeof(*ndopts));
37782cd038dSYoshinobu Inoue 		return NULL;
37882cd038dSYoshinobu Inoue 	}
37982cd038dSYoshinobu Inoue 
38082cd038dSYoshinobu Inoue 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
38119391949SKris Kennaway 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
38219391949SKris Kennaway 		/* option overruns the end of buffer, invalid */
38319391949SKris Kennaway 		bzero(ndopts, sizeof(*ndopts));
38419391949SKris Kennaway 		return NULL;
38519391949SKris Kennaway 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
38619391949SKris Kennaway 		/* reached the end of options chain */
38782cd038dSYoshinobu Inoue 		ndopts->nd_opts_done = 1;
38882cd038dSYoshinobu Inoue 		ndopts->nd_opts_search = NULL;
38982cd038dSYoshinobu Inoue 	}
39082cd038dSYoshinobu Inoue 	return nd_opt;
39182cd038dSYoshinobu Inoue }
39282cd038dSYoshinobu Inoue 
39382cd038dSYoshinobu Inoue /*
39482cd038dSYoshinobu Inoue  * Parse multiple ND options.
39582cd038dSYoshinobu Inoue  * This function is much easier to use, for ND routines that do not need
39682cd038dSYoshinobu Inoue  * multiple options of the same type.
39782cd038dSYoshinobu Inoue  */
39882cd038dSYoshinobu Inoue int
3991272577eSXin LI nd6_options(union nd_opts *ndopts)
40082cd038dSYoshinobu Inoue {
40182cd038dSYoshinobu Inoue 	struct nd_opt_hdr *nd_opt;
40282cd038dSYoshinobu Inoue 	int i = 0;
40382cd038dSYoshinobu Inoue 
40439e19560SBjoern A. Zeeb 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
40539e19560SBjoern A. Zeeb 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
40639e19560SBjoern A. Zeeb 	    __func__));
4072ce62dceSSUZUKI Shinsuke 	if (ndopts->nd_opts_search == NULL)
40882cd038dSYoshinobu Inoue 		return 0;
40982cd038dSYoshinobu Inoue 
41082cd038dSYoshinobu Inoue 	while (1) {
41182cd038dSYoshinobu Inoue 		nd_opt = nd6_option(ndopts);
4122ce62dceSSUZUKI Shinsuke 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
41382cd038dSYoshinobu Inoue 			/*
41482cd038dSYoshinobu Inoue 			 * Message validation requires that all included
41582cd038dSYoshinobu Inoue 			 * options have a length that is greater than zero.
41682cd038dSYoshinobu Inoue 			 */
417e27b0c87SRobert Watson 			ICMP6STAT_INC(icp6s_nd_badopt);
41882cd038dSYoshinobu Inoue 			bzero(ndopts, sizeof(*ndopts));
41982cd038dSYoshinobu Inoue 			return -1;
42082cd038dSYoshinobu Inoue 		}
42182cd038dSYoshinobu Inoue 
4222ce62dceSSUZUKI Shinsuke 		if (nd_opt == NULL)
42382cd038dSYoshinobu Inoue 			goto skip1;
42482cd038dSYoshinobu Inoue 
42582cd038dSYoshinobu Inoue 		switch (nd_opt->nd_opt_type) {
42682cd038dSYoshinobu Inoue 		case ND_OPT_SOURCE_LINKADDR:
42782cd038dSYoshinobu Inoue 		case ND_OPT_TARGET_LINKADDR:
42882cd038dSYoshinobu Inoue 		case ND_OPT_MTU:
42982cd038dSYoshinobu Inoue 		case ND_OPT_REDIRECTED_HEADER:
43011d8451dSHiroki Sato 		case ND_OPT_NONCE:
43182cd038dSYoshinobu Inoue 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
43233841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
43333841545SHajimu UMEMOTO 				    "duplicated ND6 option found (type=%d)\n",
43433841545SHajimu UMEMOTO 				    nd_opt->nd_opt_type));
43582cd038dSYoshinobu Inoue 				/* XXX bark? */
43682cd038dSYoshinobu Inoue 			} else {
43782cd038dSYoshinobu Inoue 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
43882cd038dSYoshinobu Inoue 					= nd_opt;
43982cd038dSYoshinobu Inoue 			}
44082cd038dSYoshinobu Inoue 			break;
44182cd038dSYoshinobu Inoue 		case ND_OPT_PREFIX_INFORMATION:
44282cd038dSYoshinobu Inoue 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
44382cd038dSYoshinobu Inoue 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
44482cd038dSYoshinobu Inoue 					= nd_opt;
44582cd038dSYoshinobu Inoue 			}
44682cd038dSYoshinobu Inoue 			ndopts->nd_opts_pi_end =
44782cd038dSYoshinobu Inoue 				(struct nd_opt_prefix_info *)nd_opt;
44882cd038dSYoshinobu Inoue 			break;
449abbe8356SBjoern A. Zeeb 		/* What about ND_OPT_ROUTE_INFO? RFC 4191 */
450abbe8356SBjoern A. Zeeb 		case ND_OPT_RDNSS:	/* RFC 6106 */
451abbe8356SBjoern A. Zeeb 		case ND_OPT_DNSSL:	/* RFC 6106 */
452abbe8356SBjoern A. Zeeb 			/*
453abbe8356SBjoern A. Zeeb 			 * Silently ignore options we know and do not care about
454abbe8356SBjoern A. Zeeb 			 * in the kernel.
455abbe8356SBjoern A. Zeeb 			 */
456abbe8356SBjoern A. Zeeb 			break;
45782cd038dSYoshinobu Inoue 		default:
45882cd038dSYoshinobu Inoue 			/*
45982cd038dSYoshinobu Inoue 			 * Unknown options must be silently ignored,
46082cd038dSYoshinobu Inoue 			 * to accomodate future extension to the protocol.
46182cd038dSYoshinobu Inoue 			 */
46233841545SHajimu UMEMOTO 			nd6log((LOG_DEBUG,
46382cd038dSYoshinobu Inoue 			    "nd6_options: unsupported option %d - "
46433841545SHajimu UMEMOTO 			    "option ignored\n", nd_opt->nd_opt_type));
46582cd038dSYoshinobu Inoue 		}
46682cd038dSYoshinobu Inoue 
46782cd038dSYoshinobu Inoue skip1:
46882cd038dSYoshinobu Inoue 		i++;
469603724d3SBjoern A. Zeeb 		if (i > V_nd6_maxndopt) {
470e27b0c87SRobert Watson 			ICMP6STAT_INC(icp6s_nd_toomanyopt);
47133841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
47282cd038dSYoshinobu Inoue 			break;
47382cd038dSYoshinobu Inoue 		}
47482cd038dSYoshinobu Inoue 
47582cd038dSYoshinobu Inoue 		if (ndopts->nd_opts_done)
47682cd038dSYoshinobu Inoue 			break;
47782cd038dSYoshinobu Inoue 	}
47882cd038dSYoshinobu Inoue 
47982cd038dSYoshinobu Inoue 	return 0;
48082cd038dSYoshinobu Inoue }
48182cd038dSYoshinobu Inoue 
48282cd038dSYoshinobu Inoue /*
483743eee66SSUZUKI Shinsuke  * ND6 timer routine to handle ND6 entries
48482cd038dSYoshinobu Inoue  */
48582cd038dSYoshinobu Inoue void
4866e6b3f7cSQing Li nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
487743eee66SSUZUKI Shinsuke {
488ef8d671cSHajimu UMEMOTO 	int canceled;
489ef8d671cSHajimu UMEMOTO 
490e6950476SBjoern A. Zeeb 	LLE_WLOCK_ASSERT(ln);
491e6950476SBjoern A. Zeeb 
492743eee66SSUZUKI Shinsuke 	if (tick < 0) {
4936e6b3f7cSQing Li 		ln->la_expire = 0;
494743eee66SSUZUKI Shinsuke 		ln->ln_ntick = 0;
4950447c136SAlexander V. Chernikov 		canceled = callout_stop(&ln->lle_timer);
496743eee66SSUZUKI Shinsuke 	} else {
4977d26db17SHiroki Sato 		ln->la_expire = time_uptime + tick / hz;
4986e6b3f7cSQing Li 		LLE_ADDREF(ln);
499743eee66SSUZUKI Shinsuke 		if (tick > INT_MAX) {
500743eee66SSUZUKI Shinsuke 			ln->ln_ntick = tick - INT_MAX;
5010447c136SAlexander V. Chernikov 			canceled = callout_reset(&ln->lle_timer, INT_MAX,
502743eee66SSUZUKI Shinsuke 			    nd6_llinfo_timer, ln);
503743eee66SSUZUKI Shinsuke 		} else {
504743eee66SSUZUKI Shinsuke 			ln->ln_ntick = 0;
5050447c136SAlexander V. Chernikov 			canceled = callout_reset(&ln->lle_timer, tick,
506743eee66SSUZUKI Shinsuke 			    nd6_llinfo_timer, ln);
507743eee66SSUZUKI Shinsuke 		}
508743eee66SSUZUKI Shinsuke 	}
509ef8d671cSHajimu UMEMOTO 	if (canceled)
510ef8d671cSHajimu UMEMOTO 		LLE_REMREF(ln);
511743eee66SSUZUKI Shinsuke }
51282cd038dSYoshinobu Inoue 
51326deb882SAlexander V. Chernikov /*
51426deb882SAlexander V. Chernikov * Gets source address of the first packet in hold queue
51526deb882SAlexander V. Chernikov * and stores it in @src.
51626deb882SAlexander V. Chernikov * Returns pointer to @src (if hold queue is not empty) or NULL.
51726deb882SAlexander V. Chernikov *
51826deb882SAlexander V. Chernikov */
51926deb882SAlexander V. Chernikov static struct in6_addr *
52026deb882SAlexander V. Chernikov nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
52126deb882SAlexander V. Chernikov {
52226deb882SAlexander V. Chernikov 	struct ip6_hdr hdr;
52326deb882SAlexander V. Chernikov 	struct mbuf *m;
52426deb882SAlexander V. Chernikov 
52526deb882SAlexander V. Chernikov 	if (ln->la_hold == NULL)
52626deb882SAlexander V. Chernikov 		return (NULL);
52726deb882SAlexander V. Chernikov 
52826deb882SAlexander V. Chernikov 	/*
52926deb882SAlexander V. Chernikov 	 * assume every packet in la_hold has the same IP header
53026deb882SAlexander V. Chernikov 	 */
53126deb882SAlexander V. Chernikov 	m = ln->la_hold;
53226deb882SAlexander V. Chernikov 	if (sizeof(hdr) < m->m_len)
53326deb882SAlexander V. Chernikov 		return (NULL);
53426deb882SAlexander V. Chernikov 
53526deb882SAlexander V. Chernikov 	m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
53626deb882SAlexander V. Chernikov 	*src = hdr.ip6_src;
53726deb882SAlexander V. Chernikov 
53826deb882SAlexander V. Chernikov 	return (src);
53926deb882SAlexander V. Chernikov }
54026deb882SAlexander V. Chernikov 
5416e6b3f7cSQing Li void
5426e6b3f7cSQing Li nd6_llinfo_settimer(struct llentry *ln, long tick)
5436e6b3f7cSQing Li {
5446e6b3f7cSQing Li 
5456e6b3f7cSQing Li 	LLE_WLOCK(ln);
5466e6b3f7cSQing Li 	nd6_llinfo_settimer_locked(ln, tick);
5476e6b3f7cSQing Li 	LLE_WUNLOCK(ln);
5486e6b3f7cSQing Li }
5496e6b3f7cSQing Li 
550743eee66SSUZUKI Shinsuke static void
5511272577eSXin LI nd6_llinfo_timer(void *arg)
552743eee66SSUZUKI Shinsuke {
5536e6b3f7cSQing Li 	struct llentry *ln;
55426deb882SAlexander V. Chernikov 	struct in6_addr *dst, *pdst, *psrc, src;
555743eee66SSUZUKI Shinsuke 	struct ifnet *ifp;
556686cdd19SJun-ichiro itojun Hagino 	struct nd_ifinfo *ndi = NULL;
55726deb882SAlexander V. Chernikov 	int send_ns;
55882cd038dSYoshinobu Inoue 
55919291ab3SBjoern A. Zeeb 	KASSERT(arg != NULL, ("%s: arg NULL", __func__));
5606e6b3f7cSQing Li 	ln = (struct llentry *)arg;
5612575fbb8SRandall Stewart 	LLE_WLOCK(ln);
5620447c136SAlexander V. Chernikov 	if (callout_pending(&ln->lle_timer)) {
5632575fbb8SRandall Stewart 		/*
5642575fbb8SRandall Stewart 		 * Here we are a bit odd here in the treatment of
5652575fbb8SRandall Stewart 		 * active/pending. If the pending bit is set, it got
5662575fbb8SRandall Stewart 		 * rescheduled before I ran. The active
5672575fbb8SRandall Stewart 		 * bit we ignore, since if it was stopped
5682575fbb8SRandall Stewart 		 * in ll_tablefree() and was currently running
5692575fbb8SRandall Stewart 		 * it would have return 0 so the code would
5702575fbb8SRandall Stewart 		 * not have deleted it since the callout could
5712575fbb8SRandall Stewart 		 * not be stopped so we want to go through
5722575fbb8SRandall Stewart 		 * with the delete here now. If the callout
5732575fbb8SRandall Stewart 		 * was restarted, the pending bit will be back on and
5742575fbb8SRandall Stewart 		 * we just want to bail since the callout_reset would
5752575fbb8SRandall Stewart 		 * return 1 and our reference would have been removed
5762575fbb8SRandall Stewart 		 * by nd6_llinfo_settimer_locked above since canceled
5772575fbb8SRandall Stewart 		 * would have been 1.
5782575fbb8SRandall Stewart 		 */
5792575fbb8SRandall Stewart 		LLE_WUNLOCK(ln);
5802575fbb8SRandall Stewart 		return;
5812575fbb8SRandall Stewart 	}
58219291ab3SBjoern A. Zeeb 	ifp = ln->lle_tbl->llt_ifp;
5836e6b3f7cSQing Li 	CURVNET_SET(ifp->if_vnet);
58426deb882SAlexander V. Chernikov 	ndi = ND_IFINFO(ifp);
58526deb882SAlexander V. Chernikov 	send_ns = 0;
58626deb882SAlexander V. Chernikov 	dst = &ln->r_l3addr.addr6;
58726deb882SAlexander V. Chernikov 	pdst = dst;
58882cd038dSYoshinobu Inoue 
589743eee66SSUZUKI Shinsuke 	if (ln->ln_ntick > 0) {
590743eee66SSUZUKI Shinsuke 		if (ln->ln_ntick > INT_MAX) {
591743eee66SSUZUKI Shinsuke 			ln->ln_ntick -= INT_MAX;
592e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, INT_MAX);
593743eee66SSUZUKI Shinsuke 		} else {
594743eee66SSUZUKI Shinsuke 			ln->ln_ntick = 0;
595e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
59682cd038dSYoshinobu Inoue 		}
5976e6b3f7cSQing Li 		goto done;
598743eee66SSUZUKI Shinsuke 	}
599743eee66SSUZUKI Shinsuke 
600287e3cb4SHajimu UMEMOTO 	if (ln->la_flags & LLE_STATIC) {
6016e6b3f7cSQing Li 		goto done;
6026e6b3f7cSQing Li 	}
60382cd038dSYoshinobu Inoue 
6046e6b3f7cSQing Li 	if (ln->la_flags & LLE_DELETED) {
6056e6b3f7cSQing Li 		(void)nd6_free(ln, 0);
6062eb10edcSHajimu UMEMOTO 		ln = NULL;
6076e6b3f7cSQing Li 		goto done;
6086e6b3f7cSQing Li 	}
60982cd038dSYoshinobu Inoue 
61082cd038dSYoshinobu Inoue 	switch (ln->ln_state) {
61182cd038dSYoshinobu Inoue 	case ND6_LLINFO_INCOMPLETE:
6126e6b3f7cSQing Li 		if (ln->la_asked < V_nd6_mmaxtries) {
6136e6b3f7cSQing Li 			ln->la_asked++;
61426deb882SAlexander V. Chernikov 			send_ns = 1;
61526deb882SAlexander V. Chernikov 			/* Send NS to multicast address */
61626deb882SAlexander V. Chernikov 			pdst = NULL;
61782cd038dSYoshinobu Inoue 		} else {
6186e6b3f7cSQing Li 			struct mbuf *m = ln->la_hold;
61982cd038dSYoshinobu Inoue 			if (m) {
62031d4137bSSUZUKI Shinsuke 				struct mbuf *m0;
62131d4137bSSUZUKI Shinsuke 
62282cd038dSYoshinobu Inoue 				/*
6236e6b3f7cSQing Li 				 * assuming every packet in la_hold has the
624e6950476SBjoern A. Zeeb 				 * same IP header.  Send error after unlock.
62582cd038dSYoshinobu Inoue 				 */
62631d4137bSSUZUKI Shinsuke 				m0 = m->m_nextpkt;
62731d4137bSSUZUKI Shinsuke 				m->m_nextpkt = NULL;
6286e6b3f7cSQing Li 				ln->la_hold = m0;
62931d4137bSSUZUKI Shinsuke 				clear_llinfo_pqueue(ln);
63082cd038dSYoshinobu Inoue 			}
63163a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_TIMEDOUT);
6326e6b3f7cSQing Li 			(void)nd6_free(ln, 0);
633743eee66SSUZUKI Shinsuke 			ln = NULL;
634e6950476SBjoern A. Zeeb 			if (m != NULL)
635e6950476SBjoern A. Zeeb 				icmp6_error2(m, ICMP6_DST_UNREACH,
636e6950476SBjoern A. Zeeb 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
63782cd038dSYoshinobu Inoue 		}
63882cd038dSYoshinobu Inoue 		break;
63982cd038dSYoshinobu Inoue 	case ND6_LLINFO_REACHABLE:
640743eee66SSUZUKI Shinsuke 		if (!ND6_LLINFO_PERMANENT(ln)) {
64182cd038dSYoshinobu Inoue 			ln->ln_state = ND6_LLINFO_STALE;
642e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
64333841545SHajimu UMEMOTO 		}
64482cd038dSYoshinobu Inoue 		break;
64533841545SHajimu UMEMOTO 
64633841545SHajimu UMEMOTO 	case ND6_LLINFO_STALE:
64733841545SHajimu UMEMOTO 		/* Garbage Collection(RFC 2461 5.3) */
648743eee66SSUZUKI Shinsuke 		if (!ND6_LLINFO_PERMANENT(ln)) {
64963a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
6506e6b3f7cSQing Li 			(void)nd6_free(ln, 1);
651743eee66SSUZUKI Shinsuke 			ln = NULL;
652743eee66SSUZUKI Shinsuke 		}
65333841545SHajimu UMEMOTO 		break;
65433841545SHajimu UMEMOTO 
65582cd038dSYoshinobu Inoue 	case ND6_LLINFO_DELAY:
656686cdd19SJun-ichiro itojun Hagino 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
657686cdd19SJun-ichiro itojun Hagino 			/* We need NUD */
6586e6b3f7cSQing Li 			ln->la_asked = 1;
65982cd038dSYoshinobu Inoue 			ln->ln_state = ND6_LLINFO_PROBE;
66026deb882SAlexander V. Chernikov 			send_ns = 1;
66133841545SHajimu UMEMOTO 		} else {
662686cdd19SJun-ichiro itojun Hagino 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
663e6950476SBjoern A. Zeeb 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
66433841545SHajimu UMEMOTO 		}
66582cd038dSYoshinobu Inoue 		break;
66682cd038dSYoshinobu Inoue 	case ND6_LLINFO_PROBE:
6676e6b3f7cSQing Li 		if (ln->la_asked < V_nd6_umaxtries) {
6686e6b3f7cSQing Li 			ln->la_asked++;
66926deb882SAlexander V. Chernikov 			send_ns = 1;
67082cd038dSYoshinobu Inoue 		} else {
67163a97a40SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
6726e6b3f7cSQing Li 			(void)nd6_free(ln, 0);
673743eee66SSUZUKI Shinsuke 			ln = NULL;
67482cd038dSYoshinobu Inoue 		}
67582cd038dSYoshinobu Inoue 		break;
676e6950476SBjoern A. Zeeb 	default:
677e6950476SBjoern A. Zeeb 		panic("%s: paths in a dark night can be confusing: %d",
678e6950476SBjoern A. Zeeb 		    __func__, ln->ln_state);
67982cd038dSYoshinobu Inoue 	}
6806e6b3f7cSQing Li done:
68126deb882SAlexander V. Chernikov 	if (send_ns != 0) {
68226deb882SAlexander V. Chernikov 		nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
68326deb882SAlexander V. Chernikov 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
68426deb882SAlexander V. Chernikov 		LLE_FREE_LOCKED(ln);
68526deb882SAlexander V. Chernikov 		ln = NULL;
68626deb882SAlexander V. Chernikov 		nd6_ns_output(ifp, psrc, pdst, dst, NULL);
68726deb882SAlexander V. Chernikov 	}
68826deb882SAlexander V. Chernikov 
6896e6b3f7cSQing Li 	if (ln != NULL)
690e6950476SBjoern A. Zeeb 		LLE_FREE_LOCKED(ln);
691f0c0b143SHajimu UMEMOTO 	CURVNET_RESTORE();
69282cd038dSYoshinobu Inoue }
69382cd038dSYoshinobu Inoue 
694743eee66SSUZUKI Shinsuke 
695743eee66SSUZUKI Shinsuke /*
696743eee66SSUZUKI Shinsuke  * ND6 timer routine to expire default route list and prefix list
697743eee66SSUZUKI Shinsuke  */
698743eee66SSUZUKI Shinsuke void
6998b615593SMarko Zec nd6_timer(void *arg)
700743eee66SSUZUKI Shinsuke {
70121ca7b57SMarko Zec 	CURVNET_SET((struct vnet *) arg);
7023b0b2840SJohn Baldwin 	struct nd_defrouter *dr, *ndr;
7033b0b2840SJohn Baldwin 	struct nd_prefix *pr, *npr;
704743eee66SSUZUKI Shinsuke 	struct in6_ifaddr *ia6, *nia6;
705743eee66SSUZUKI Shinsuke 
706603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
707f6dfe47aSMarko Zec 	    nd6_timer, curvnet);
708743eee66SSUZUKI Shinsuke 
70933841545SHajimu UMEMOTO 	/* expire default router list */
7103b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
7117d26db17SHiroki Sato 		if (dr->expire && dr->expire < time_uptime)
71282cd038dSYoshinobu Inoue 			defrtrlist_del(dr);
713686cdd19SJun-ichiro itojun Hagino 	}
71482cd038dSYoshinobu Inoue 
71533841545SHajimu UMEMOTO 	/*
71633841545SHajimu UMEMOTO 	 * expire interface addresses.
71733841545SHajimu UMEMOTO 	 * in the past the loop was inside prefix expiry processing.
71833841545SHajimu UMEMOTO 	 * However, from a stricter speci-confrmance standpoint, we should
71933841545SHajimu UMEMOTO 	 * rather separate address lifetimes and prefix lifetimes.
720d1da0a06SRobert Watson 	 *
721d1da0a06SRobert Watson 	 * XXXRW: in6_ifaddrhead locking.
72233841545SHajimu UMEMOTO 	 */
72333841545SHajimu UMEMOTO   addrloop:
72480af0152SRobert Watson 	TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
72582cd038dSYoshinobu Inoue 		/* check address lifetime */
72633841545SHajimu UMEMOTO 		if (IFA6_IS_INVALID(ia6)) {
72733841545SHajimu UMEMOTO 			int regen = 0;
72833841545SHajimu UMEMOTO 
72933841545SHajimu UMEMOTO 			/*
73033841545SHajimu UMEMOTO 			 * If the expiring address is temporary, try
73133841545SHajimu UMEMOTO 			 * regenerating a new one.  This would be useful when
73288ff5695SSUZUKI Shinsuke 			 * we suspended a laptop PC, then turned it on after a
73333841545SHajimu UMEMOTO 			 * period that could invalidate all temporary
73433841545SHajimu UMEMOTO 			 * addresses.  Although we may have to restart the
73533841545SHajimu UMEMOTO 			 * loop (see below), it must be after purging the
73633841545SHajimu UMEMOTO 			 * address.  Otherwise, we'd see an infinite loop of
73733841545SHajimu UMEMOTO 			 * regeneration.
73833841545SHajimu UMEMOTO 			 */
739603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr &&
74033841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
74133841545SHajimu UMEMOTO 				if (regen_tmpaddr(ia6) == 0)
74233841545SHajimu UMEMOTO 					regen = 1;
74333841545SHajimu UMEMOTO 			}
74433841545SHajimu UMEMOTO 
74533841545SHajimu UMEMOTO 			in6_purgeaddr(&ia6->ia_ifa);
74633841545SHajimu UMEMOTO 
74733841545SHajimu UMEMOTO 			if (regen)
74833841545SHajimu UMEMOTO 				goto addrloop; /* XXX: see below */
74943068328SHajimu UMEMOTO 		} else if (IFA6_IS_DEPRECATED(ia6)) {
75033841545SHajimu UMEMOTO 			int oldflags = ia6->ia6_flags;
75133841545SHajimu UMEMOTO 
75282cd038dSYoshinobu Inoue 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
75333841545SHajimu UMEMOTO 
75433841545SHajimu UMEMOTO 			/*
75533841545SHajimu UMEMOTO 			 * If a temporary address has just become deprecated,
75633841545SHajimu UMEMOTO 			 * regenerate a new one if possible.
75733841545SHajimu UMEMOTO 			 */
758603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr &&
75933841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
76033841545SHajimu UMEMOTO 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
76133841545SHajimu UMEMOTO 
76233841545SHajimu UMEMOTO 				if (regen_tmpaddr(ia6) == 0) {
76333841545SHajimu UMEMOTO 					/*
76433841545SHajimu UMEMOTO 					 * A new temporary address is
76533841545SHajimu UMEMOTO 					 * generated.
76633841545SHajimu UMEMOTO 					 * XXX: this means the address chain
76733841545SHajimu UMEMOTO 					 * has changed while we are still in
76833841545SHajimu UMEMOTO 					 * the loop.  Although the change
76933841545SHajimu UMEMOTO 					 * would not cause disaster (because
77088ff5695SSUZUKI Shinsuke 					 * it's not a deletion, but an
77188ff5695SSUZUKI Shinsuke 					 * addition,) we'd rather restart the
77233841545SHajimu UMEMOTO 					 * loop just for safety.  Or does this
77333841545SHajimu UMEMOTO 					 * significantly reduce performance??
77433841545SHajimu UMEMOTO 					 */
77533841545SHajimu UMEMOTO 					goto addrloop;
77633841545SHajimu UMEMOTO 				}
77733841545SHajimu UMEMOTO 			}
77883649f07SHajimu UMEMOTO 		} else {
77933841545SHajimu UMEMOTO 			/*
78033841545SHajimu UMEMOTO 			 * A new RA might have made a deprecated address
78133841545SHajimu UMEMOTO 			 * preferred.
78233841545SHajimu UMEMOTO 			 */
78333841545SHajimu UMEMOTO 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
78482cd038dSYoshinobu Inoue 		}
78582cd038dSYoshinobu Inoue 	}
78682cd038dSYoshinobu Inoue 
78733841545SHajimu UMEMOTO 	/* expire prefix list */
7883b0b2840SJohn Baldwin 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
78982cd038dSYoshinobu Inoue 		/*
79082cd038dSYoshinobu Inoue 		 * check prefix lifetime.
79182cd038dSYoshinobu Inoue 		 * since pltime is just for autoconf, pltime processing for
79282cd038dSYoshinobu Inoue 		 * prefix is not necessary.
79382cd038dSYoshinobu Inoue 		 */
794743eee66SSUZUKI Shinsuke 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
7957d26db17SHiroki Sato 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
79682cd038dSYoshinobu Inoue 
79782cd038dSYoshinobu Inoue 			/*
79882cd038dSYoshinobu Inoue 			 * address expiration and prefix expiration are
79933841545SHajimu UMEMOTO 			 * separate.  NEVER perform in6_purgeaddr here.
80082cd038dSYoshinobu Inoue 			 */
80182cd038dSYoshinobu Inoue 			prelist_remove(pr);
8023b0b2840SJohn Baldwin 		}
80382cd038dSYoshinobu Inoue 	}
8048b615593SMarko Zec 	CURVNET_RESTORE();
80582cd038dSYoshinobu Inoue }
80682cd038dSYoshinobu Inoue 
8071272577eSXin LI /*
8081272577eSXin LI  * ia6 - deprecated/invalidated temporary address
8091272577eSXin LI  */
81033841545SHajimu UMEMOTO static int
8111272577eSXin LI regen_tmpaddr(struct in6_ifaddr *ia6)
81233841545SHajimu UMEMOTO {
81333841545SHajimu UMEMOTO 	struct ifaddr *ifa;
81433841545SHajimu UMEMOTO 	struct ifnet *ifp;
81533841545SHajimu UMEMOTO 	struct in6_ifaddr *public_ifa6 = NULL;
81633841545SHajimu UMEMOTO 
81733841545SHajimu UMEMOTO 	ifp = ia6->ia_ifa.ifa_ifp;
818137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
819c4dd3fe1SRobert Watson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
82033841545SHajimu UMEMOTO 		struct in6_ifaddr *it6;
82133841545SHajimu UMEMOTO 
82233841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
82333841545SHajimu UMEMOTO 			continue;
82433841545SHajimu UMEMOTO 
82533841545SHajimu UMEMOTO 		it6 = (struct in6_ifaddr *)ifa;
82633841545SHajimu UMEMOTO 
82733841545SHajimu UMEMOTO 		/* ignore no autoconf addresses. */
82833841545SHajimu UMEMOTO 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
82933841545SHajimu UMEMOTO 			continue;
83033841545SHajimu UMEMOTO 
83133841545SHajimu UMEMOTO 		/* ignore autoconf addresses with different prefixes. */
83233841545SHajimu UMEMOTO 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
83333841545SHajimu UMEMOTO 			continue;
83433841545SHajimu UMEMOTO 
83533841545SHajimu UMEMOTO 		/*
83633841545SHajimu UMEMOTO 		 * Now we are looking at an autoconf address with the same
83733841545SHajimu UMEMOTO 		 * prefix as ours.  If the address is temporary and is still
83833841545SHajimu UMEMOTO 		 * preferred, do not create another one.  It would be rare, but
83933841545SHajimu UMEMOTO 		 * could happen, for example, when we resume a laptop PC after
84033841545SHajimu UMEMOTO 		 * a long period.
84133841545SHajimu UMEMOTO 		 */
84233841545SHajimu UMEMOTO 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
84333841545SHajimu UMEMOTO 		    !IFA6_IS_DEPRECATED(it6)) {
84433841545SHajimu UMEMOTO 			public_ifa6 = NULL;
84533841545SHajimu UMEMOTO 			break;
84633841545SHajimu UMEMOTO 		}
84733841545SHajimu UMEMOTO 
84833841545SHajimu UMEMOTO 		/*
84933841545SHajimu UMEMOTO 		 * This is a public autoconf address that has the same prefix
85033841545SHajimu UMEMOTO 		 * as ours.  If it is preferred, keep it.  We can't break the
85133841545SHajimu UMEMOTO 		 * loop here, because there may be a still-preferred temporary
85233841545SHajimu UMEMOTO 		 * address with the prefix.
85333841545SHajimu UMEMOTO 		 */
85433841545SHajimu UMEMOTO 		if (!IFA6_IS_DEPRECATED(it6))
85533841545SHajimu UMEMOTO 			public_ifa6 = it6;
856dff78447SMark Johnston 	}
857c1752bcdSQing Li 	if (public_ifa6 != NULL)
858c1752bcdSQing Li 		ifa_ref(&public_ifa6->ia_ifa);
859137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
86033841545SHajimu UMEMOTO 
86133841545SHajimu UMEMOTO 	if (public_ifa6 != NULL) {
86233841545SHajimu UMEMOTO 		int e;
86333841545SHajimu UMEMOTO 
864743eee66SSUZUKI Shinsuke 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
865c1752bcdSQing Li 			ifa_free(&public_ifa6->ia_ifa);
86633841545SHajimu UMEMOTO 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
86733841545SHajimu UMEMOTO 			    " tmp addr,errno=%d\n", e);
86833841545SHajimu UMEMOTO 			return (-1);
86933841545SHajimu UMEMOTO 		}
870c1752bcdSQing Li 		ifa_free(&public_ifa6->ia_ifa);
87133841545SHajimu UMEMOTO 		return (0);
87233841545SHajimu UMEMOTO 	}
87333841545SHajimu UMEMOTO 
87433841545SHajimu UMEMOTO 	return (-1);
87533841545SHajimu UMEMOTO }
87633841545SHajimu UMEMOTO 
877686cdd19SJun-ichiro itojun Hagino /*
878686cdd19SJun-ichiro itojun Hagino  * Nuke neighbor cache/prefix/default router management table, right before
879686cdd19SJun-ichiro itojun Hagino  * ifp goes away.
880686cdd19SJun-ichiro itojun Hagino  */
881686cdd19SJun-ichiro itojun Hagino void
8821272577eSXin LI nd6_purge(struct ifnet *ifp)
883686cdd19SJun-ichiro itojun Hagino {
884743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr, *ndr;
885686cdd19SJun-ichiro itojun Hagino 	struct nd_prefix *pr, *npr;
886686cdd19SJun-ichiro itojun Hagino 
887686cdd19SJun-ichiro itojun Hagino 	/*
888743eee66SSUZUKI Shinsuke 	 * Nuke default router list entries toward ifp.
889743eee66SSUZUKI Shinsuke 	 * We defer removal of default router list entries that is installed
890743eee66SSUZUKI Shinsuke 	 * in the routing table, in order to keep additional side effects as
891743eee66SSUZUKI Shinsuke 	 * small as possible.
892686cdd19SJun-ichiro itojun Hagino 	 */
8933b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
894743eee66SSUZUKI Shinsuke 		if (dr->installed)
895743eee66SSUZUKI Shinsuke 			continue;
896743eee66SSUZUKI Shinsuke 
897686cdd19SJun-ichiro itojun Hagino 		if (dr->ifp == ifp)
898686cdd19SJun-ichiro itojun Hagino 			defrtrlist_del(dr);
899686cdd19SJun-ichiro itojun Hagino 	}
900743eee66SSUZUKI Shinsuke 
9013b0b2840SJohn Baldwin 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
902743eee66SSUZUKI Shinsuke 		if (!dr->installed)
903743eee66SSUZUKI Shinsuke 			continue;
904743eee66SSUZUKI Shinsuke 
905686cdd19SJun-ichiro itojun Hagino 		if (dr->ifp == ifp)
906686cdd19SJun-ichiro itojun Hagino 			defrtrlist_del(dr);
907686cdd19SJun-ichiro itojun Hagino 	}
908686cdd19SJun-ichiro itojun Hagino 
909686cdd19SJun-ichiro itojun Hagino 	/* Nuke prefix list entries toward ifp */
9103b0b2840SJohn Baldwin 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
911686cdd19SJun-ichiro itojun Hagino 		if (pr->ndpr_ifp == ifp) {
91233841545SHajimu UMEMOTO 			/*
913743eee66SSUZUKI Shinsuke 			 * Because if_detach() does *not* release prefixes
914743eee66SSUZUKI Shinsuke 			 * while purging addresses the reference count will
915743eee66SSUZUKI Shinsuke 			 * still be above zero. We therefore reset it to
916743eee66SSUZUKI Shinsuke 			 * make sure that the prefix really gets purged.
917743eee66SSUZUKI Shinsuke 			 */
918743eee66SSUZUKI Shinsuke 			pr->ndpr_refcnt = 0;
919743eee66SSUZUKI Shinsuke 
920743eee66SSUZUKI Shinsuke 			/*
92133841545SHajimu UMEMOTO 			 * Previously, pr->ndpr_addr is removed as well,
92233841545SHajimu UMEMOTO 			 * but I strongly believe we don't have to do it.
92333841545SHajimu UMEMOTO 			 * nd6_purge() is only called from in6_ifdetach(),
92433841545SHajimu UMEMOTO 			 * which removes all the associated interface addresses
92533841545SHajimu UMEMOTO 			 * by itself.
92633841545SHajimu UMEMOTO 			 * (jinmei@kame.net 20010129)
92733841545SHajimu UMEMOTO 			 */
928686cdd19SJun-ichiro itojun Hagino 			prelist_remove(pr);
929686cdd19SJun-ichiro itojun Hagino 		}
930686cdd19SJun-ichiro itojun Hagino 	}
931686cdd19SJun-ichiro itojun Hagino 
932686cdd19SJun-ichiro itojun Hagino 	/* cancel default outgoing interface setting */
933603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex == ifp->if_index)
934686cdd19SJun-ichiro itojun Hagino 		nd6_setdefaultiface(0);
935686cdd19SJun-ichiro itojun Hagino 
936e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
937a283298cSHiroki Sato 		/* Refresh default router list. */
938686cdd19SJun-ichiro itojun Hagino 		defrouter_select();
9396e6b3f7cSQing Li 	}
9406e6b3f7cSQing Li 
9416e6b3f7cSQing Li 	/* XXXXX
9426e6b3f7cSQing Li 	 * We do not nuke the neighbor cache entries here any more
9436e6b3f7cSQing Li 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
9446e6b3f7cSQing Li 	 * nd6_purge() is invoked by in6_ifdetach() which is called
9456e6b3f7cSQing Li 	 * from if_detach() where everything gets purged. So let
9466e6b3f7cSQing Li 	 * in6_domifdetach() do the actual L2 table purging work.
9476e6b3f7cSQing Li 	 */
9481026ccc4SHajimu UMEMOTO }
949686cdd19SJun-ichiro itojun Hagino 
950686cdd19SJun-ichiro itojun Hagino /*
9516e6b3f7cSQing Li  * the caller acquires and releases the lock on the lltbls
9526e6b3f7cSQing Li  * Returns the llentry locked
953686cdd19SJun-ichiro itojun Hagino  */
9546e6b3f7cSQing Li struct llentry *
9556e6b3f7cSQing Li nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
95682cd038dSYoshinobu Inoue {
95782cd038dSYoshinobu Inoue 	struct sockaddr_in6 sin6;
9586e6b3f7cSQing Li 	struct llentry *ln;
95968352503SBjoern A. Zeeb 	int llflags;
96082cd038dSYoshinobu Inoue 
96182cd038dSYoshinobu Inoue 	bzero(&sin6, sizeof(sin6));
96282cd038dSYoshinobu Inoue 	sin6.sin6_len = sizeof(struct sockaddr_in6);
96382cd038dSYoshinobu Inoue 	sin6.sin6_family = AF_INET6;
96482cd038dSYoshinobu Inoue 	sin6.sin6_addr = *addr6;
965686cdd19SJun-ichiro itojun Hagino 
9666e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
96782cd038dSYoshinobu Inoue 
968b4b1367aSAlexander V. Chernikov 	llflags = (flags & ND6_EXCLUSIVE) ? LLE_EXCLUSIVE : 0;
9696e6b3f7cSQing Li 	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
970b4b1367aSAlexander V. Chernikov 
971b4b1367aSAlexander V. Chernikov 	return (ln);
972b4b1367aSAlexander V. Chernikov }
973b4b1367aSAlexander V. Chernikov 
974b4b1367aSAlexander V. Chernikov struct llentry *
9755a255516SAlexander V. Chernikov nd6_alloc(struct in6_addr *addr6, int flags, struct ifnet *ifp)
976b4b1367aSAlexander V. Chernikov {
977b4b1367aSAlexander V. Chernikov 	struct sockaddr_in6 sin6;
978b4b1367aSAlexander V. Chernikov 	struct llentry *ln;
979b4b1367aSAlexander V. Chernikov 
980b4b1367aSAlexander V. Chernikov 	bzero(&sin6, sizeof(sin6));
981b4b1367aSAlexander V. Chernikov 	sin6.sin6_len = sizeof(struct sockaddr_in6);
982b4b1367aSAlexander V. Chernikov 	sin6.sin6_family = AF_INET6;
983b4b1367aSAlexander V. Chernikov 	sin6.sin6_addr = *addr6;
984b4b1367aSAlexander V. Chernikov 
9855a255516SAlexander V. Chernikov 	ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
986b4b1367aSAlexander V. Chernikov 	if (ln != NULL)
98782cd038dSYoshinobu Inoue 		ln->ln_state = ND6_LLINFO_NOSTATE;
9886e6b3f7cSQing Li 
9896e6b3f7cSQing Li 	return (ln);
99082cd038dSYoshinobu Inoue }
99182cd038dSYoshinobu Inoue 
99282cd038dSYoshinobu Inoue /*
99377b691e0SBrian Feldman  * Test whether a given IPv6 address is a neighbor or not, ignoring
99477b691e0SBrian Feldman  * the actual neighbor cache.  The neighbor cache is ignored in order
99577b691e0SBrian Feldman  * to not reenter the routing code from within itself.
99682cd038dSYoshinobu Inoue  */
99777b691e0SBrian Feldman static int
9981272577eSXin LI nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
99982cd038dSYoshinobu Inoue {
10004208ea14SHajimu UMEMOTO 	struct nd_prefix *pr;
1001743eee66SSUZUKI Shinsuke 	struct ifaddr *dstaddr;
100282cd038dSYoshinobu Inoue 
1003686cdd19SJun-ichiro itojun Hagino 	/*
1004686cdd19SJun-ichiro itojun Hagino 	 * A link-local address is always a neighbor.
10054208ea14SHajimu UMEMOTO 	 * XXX: a link does not necessarily specify a single interface.
1006686cdd19SJun-ichiro itojun Hagino 	 */
1007a1f7e5f8SHajimu UMEMOTO 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1008a1f7e5f8SHajimu UMEMOTO 		struct sockaddr_in6 sin6_copy;
1009a1f7e5f8SHajimu UMEMOTO 		u_int32_t zone;
1010a1f7e5f8SHajimu UMEMOTO 
1011a1f7e5f8SHajimu UMEMOTO 		/*
1012a1f7e5f8SHajimu UMEMOTO 		 * We need sin6_copy since sa6_recoverscope() may modify the
1013a1f7e5f8SHajimu UMEMOTO 		 * content (XXX).
1014a1f7e5f8SHajimu UMEMOTO 		 */
1015a1f7e5f8SHajimu UMEMOTO 		sin6_copy = *addr;
1016a1f7e5f8SHajimu UMEMOTO 		if (sa6_recoverscope(&sin6_copy))
1017a1f7e5f8SHajimu UMEMOTO 			return (0); /* XXX: should be impossible */
1018a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1019a1f7e5f8SHajimu UMEMOTO 			return (0);
1020a1f7e5f8SHajimu UMEMOTO 		if (sin6_copy.sin6_scope_id == zone)
102182cd038dSYoshinobu Inoue 			return (1);
1022a1f7e5f8SHajimu UMEMOTO 		else
1023a1f7e5f8SHajimu UMEMOTO 			return (0);
1024a1f7e5f8SHajimu UMEMOTO 	}
102582cd038dSYoshinobu Inoue 
102682cd038dSYoshinobu Inoue 	/*
102782cd038dSYoshinobu Inoue 	 * If the address matches one of our addresses,
102882cd038dSYoshinobu Inoue 	 * it should be a neighbor.
10294208ea14SHajimu UMEMOTO 	 * If the address matches one of our on-link prefixes, it should be a
10304208ea14SHajimu UMEMOTO 	 * neighbor.
103182cd038dSYoshinobu Inoue 	 */
10323b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
10334208ea14SHajimu UMEMOTO 		if (pr->ndpr_ifp != ifp)
10344208ea14SHajimu UMEMOTO 			continue;
103582cd038dSYoshinobu Inoue 
1036baf7c373SQing Li 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1037baf7c373SQing Li 			struct rtentry *rt;
103881d5d46bSBjoern A. Zeeb 
103981d5d46bSBjoern A. Zeeb 			/* Always use the default FIB here. */
104081d5d46bSBjoern A. Zeeb 			rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
104181d5d46bSBjoern A. Zeeb 			    0, 0, RT_DEFAULT_FIB);
1042baf7c373SQing Li 			if (rt == NULL)
10434208ea14SHajimu UMEMOTO 				continue;
1044baf7c373SQing Li 			/*
1045baf7c373SQing Li 			 * This is the case where multiple interfaces
1046baf7c373SQing Li 			 * have the same prefix, but only one is installed
1047baf7c373SQing Li 			 * into the routing table and that prefix entry
1048baf7c373SQing Li 			 * is not the one being examined here. In the case
1049baf7c373SQing Li 			 * where RADIX_MPATH is enabled, multiple route
1050baf7c373SQing Li 			 * entries (of the same rt_key value) will be
1051baf7c373SQing Li 			 * installed because the interface addresses all
1052baf7c373SQing Li 			 * differ.
1053baf7c373SQing Li 			 */
1054baf7c373SQing Li 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1055baf7c373SQing Li 			       &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
1056baf7c373SQing Li 				RTFREE_LOCKED(rt);
1057baf7c373SQing Li 				continue;
1058baf7c373SQing Li 			}
1059baf7c373SQing Li 			RTFREE_LOCKED(rt);
1060baf7c373SQing Li 		}
10614208ea14SHajimu UMEMOTO 
10624208ea14SHajimu UMEMOTO 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
10634208ea14SHajimu UMEMOTO 		    &addr->sin6_addr, &pr->ndpr_mask))
10644208ea14SHajimu UMEMOTO 			return (1);
106582cd038dSYoshinobu Inoue 	}
10664208ea14SHajimu UMEMOTO 
10674208ea14SHajimu UMEMOTO 	/*
1068743eee66SSUZUKI Shinsuke 	 * If the address is assigned on the node of the other side of
1069743eee66SSUZUKI Shinsuke 	 * a p2p interface, the address should be a neighbor.
1070743eee66SSUZUKI Shinsuke 	 */
10714f8585e0SAlan Somers 	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr, RT_ALL_FIBS);
10728c0fec80SRobert Watson 	if (dstaddr != NULL) {
10738c0fec80SRobert Watson 		if (dstaddr->ifa_ifp == ifp) {
10748c0fec80SRobert Watson 			ifa_free(dstaddr);
1075743eee66SSUZUKI Shinsuke 			return (1);
10768c0fec80SRobert Watson 		}
10778c0fec80SRobert Watson 		ifa_free(dstaddr);
10788c0fec80SRobert Watson 	}
1079743eee66SSUZUKI Shinsuke 
1080743eee66SSUZUKI Shinsuke 	/*
10814208ea14SHajimu UMEMOTO 	 * If the default router list is empty, all addresses are regarded
10824208ea14SHajimu UMEMOTO 	 * as on-link, and thus, as a neighbor.
10834208ea14SHajimu UMEMOTO 	 */
1084e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
10853b0b2840SJohn Baldwin 	    TAILQ_EMPTY(&V_nd_defrouter) &&
1086603724d3SBjoern A. Zeeb 	    V_nd6_defifindex == ifp->if_index) {
108782cd038dSYoshinobu Inoue 		return (1);
108882cd038dSYoshinobu Inoue 	}
108982cd038dSYoshinobu Inoue 
109077b691e0SBrian Feldman 	return (0);
109177b691e0SBrian Feldman }
109277b691e0SBrian Feldman 
109377b691e0SBrian Feldman 
109477b691e0SBrian Feldman /*
109577b691e0SBrian Feldman  * Detect if a given IPv6 address identifies a neighbor on a given link.
109677b691e0SBrian Feldman  * XXX: should take care of the destination of a p2p link?
109777b691e0SBrian Feldman  */
109877b691e0SBrian Feldman int
10991272577eSXin LI nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
110077b691e0SBrian Feldman {
11016e6b3f7cSQing Li 	struct llentry *lle;
11026e6b3f7cSQing Li 	int rc = 0;
110377b691e0SBrian Feldman 
11046e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
110577b691e0SBrian Feldman 	if (nd6_is_new_addr_neighbor(addr, ifp))
110677b691e0SBrian Feldman 		return (1);
110777b691e0SBrian Feldman 
110882cd038dSYoshinobu Inoue 	/*
110982cd038dSYoshinobu Inoue 	 * Even if the address matches none of our addresses, it might be
111082cd038dSYoshinobu Inoue 	 * in the neighbor cache.
111182cd038dSYoshinobu Inoue 	 */
111273cb2f38SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
11136e6b3f7cSQing Li 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
11146e6b3f7cSQing Li 		LLE_RUNLOCK(lle);
11156e6b3f7cSQing Li 		rc = 1;
11166e6b3f7cSQing Li 	}
111773cb2f38SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
11186e6b3f7cSQing Li 	return (rc);
111982cd038dSYoshinobu Inoue }
112082cd038dSYoshinobu Inoue 
112182cd038dSYoshinobu Inoue /*
112282cd038dSYoshinobu Inoue  * Free an nd6 llinfo entry.
1123743eee66SSUZUKI Shinsuke  * Since the function would cause significant changes in the kernel, DO NOT
1124743eee66SSUZUKI Shinsuke  * make it global, unless you have a strong reason for the change, and are sure
1125743eee66SSUZUKI Shinsuke  * that the change is safe.
112682cd038dSYoshinobu Inoue  */
11276e6b3f7cSQing Li static struct llentry *
11286e6b3f7cSQing Li nd6_free(struct llentry *ln, int gc)
112982cd038dSYoshinobu Inoue {
11306e6b3f7cSQing Li         struct llentry *next;
113182cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
1132e6950476SBjoern A. Zeeb 	struct ifnet *ifp;
1133e6950476SBjoern A. Zeeb 
1134e6950476SBjoern A. Zeeb 	LLE_WLOCK_ASSERT(ln);
113582cd038dSYoshinobu Inoue 
113682cd038dSYoshinobu Inoue 	/*
113733841545SHajimu UMEMOTO 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
113833841545SHajimu UMEMOTO 	 * even though it is not harmful, it was not really necessary.
113982cd038dSYoshinobu Inoue 	 */
1140686cdd19SJun-ichiro itojun Hagino 
1141743eee66SSUZUKI Shinsuke 	/* cancel timer */
1142e6950476SBjoern A. Zeeb 	nd6_llinfo_settimer_locked(ln, -1);
1143e6950476SBjoern A. Zeeb 
1144e6950476SBjoern A. Zeeb 	ifp = ln->lle_tbl->llt_ifp;
1145743eee66SSUZUKI Shinsuke 
1146e7fa8d0aSHiroki Sato 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1147314294deSAlexander V. Chernikov 		dr = defrouter_lookup(&ln->r_l3addr.addr6, ifp);
114833841545SHajimu UMEMOTO 
1149743eee66SSUZUKI Shinsuke 		if (dr != NULL && dr->expire &&
1150743eee66SSUZUKI Shinsuke 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1151743eee66SSUZUKI Shinsuke 			/*
1152743eee66SSUZUKI Shinsuke 			 * If the reason for the deletion is just garbage
1153743eee66SSUZUKI Shinsuke 			 * collection, and the neighbor is an active default
1154743eee66SSUZUKI Shinsuke 			 * router, do not delete it.  Instead, reset the GC
1155743eee66SSUZUKI Shinsuke 			 * timer using the router's lifetime.
1156743eee66SSUZUKI Shinsuke 			 * Simply deleting the entry would affect default
1157743eee66SSUZUKI Shinsuke 			 * router selection, which is not necessarily a good
1158743eee66SSUZUKI Shinsuke 			 * thing, especially when we're using router preference
1159743eee66SSUZUKI Shinsuke 			 * values.
1160743eee66SSUZUKI Shinsuke 			 * XXX: the check for ln_state would be redundant,
1161743eee66SSUZUKI Shinsuke 			 *      but we intentionally keep it just in case.
1162743eee66SSUZUKI Shinsuke 			 */
11637d26db17SHiroki Sato 			if (dr->expire > time_uptime)
1164e6950476SBjoern A. Zeeb 				nd6_llinfo_settimer_locked(ln,
11657d26db17SHiroki Sato 				    (dr->expire - time_uptime) * hz);
1166743eee66SSUZUKI Shinsuke 			else
1167e6950476SBjoern A. Zeeb 				nd6_llinfo_settimer_locked(ln,
1168e6950476SBjoern A. Zeeb 				    (long)V_nd6_gctimer * hz);
1169e6950476SBjoern A. Zeeb 
1170e6950476SBjoern A. Zeeb 			next = LIST_NEXT(ln, lle_next);
1171ef8d671cSHajimu UMEMOTO 			LLE_REMREF(ln);
1172ef8d671cSHajimu UMEMOTO 			LLE_WUNLOCK(ln);
1173e6950476SBjoern A. Zeeb 			return (next);
1174743eee66SSUZUKI Shinsuke 		}
1175743eee66SSUZUKI Shinsuke 
1176686cdd19SJun-ichiro itojun Hagino 		if (dr) {
1177686cdd19SJun-ichiro itojun Hagino 			/*
1178686cdd19SJun-ichiro itojun Hagino 			 * Unreachablity of a router might affect the default
1179686cdd19SJun-ichiro itojun Hagino 			 * router selection and on-link detection of advertised
1180686cdd19SJun-ichiro itojun Hagino 			 * prefixes.
1181686cdd19SJun-ichiro itojun Hagino 			 */
1182686cdd19SJun-ichiro itojun Hagino 
1183686cdd19SJun-ichiro itojun Hagino 			/*
1184686cdd19SJun-ichiro itojun Hagino 			 * Temporarily fake the state to choose a new default
1185686cdd19SJun-ichiro itojun Hagino 			 * router and to perform on-link determination of
118688ff5695SSUZUKI Shinsuke 			 * prefixes correctly.
1187686cdd19SJun-ichiro itojun Hagino 			 * Below the state will be set correctly,
1188686cdd19SJun-ichiro itojun Hagino 			 * or the entry itself will be deleted.
1189686cdd19SJun-ichiro itojun Hagino 			 */
1190686cdd19SJun-ichiro itojun Hagino 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
11911d5089c2SBjoern A. Zeeb 		}
1192686cdd19SJun-ichiro itojun Hagino 
11931d5089c2SBjoern A. Zeeb 		if (ln->ln_router || dr) {
11941d5089c2SBjoern A. Zeeb 
11951d5089c2SBjoern A. Zeeb 			/*
11961d5089c2SBjoern A. Zeeb 			 * We need to unlock to avoid a LOR with rt6_flush() with the
11971d5089c2SBjoern A. Zeeb 			 * rnh and for the calls to pfxlist_onlink_check() and
11981d5089c2SBjoern A. Zeeb 			 * defrouter_select() in the block further down for calls
11991d5089c2SBjoern A. Zeeb 			 * into nd6_lookup().  We still hold a ref.
12001d5089c2SBjoern A. Zeeb 			 */
12011d5089c2SBjoern A. Zeeb 			LLE_WUNLOCK(ln);
12021d5089c2SBjoern A. Zeeb 
12031d5089c2SBjoern A. Zeeb 			/*
12041d5089c2SBjoern A. Zeeb 			 * rt6_flush must be called whether or not the neighbor
12051d5089c2SBjoern A. Zeeb 			 * is in the Default Router List.
12061d5089c2SBjoern A. Zeeb 			 * See a corresponding comment in nd6_na_input().
12071d5089c2SBjoern A. Zeeb 			 */
1208314294deSAlexander V. Chernikov 			rt6_flush(&ln->r_l3addr.addr6, ifp);
12091d5089c2SBjoern A. Zeeb 		}
12101d5089c2SBjoern A. Zeeb 
12111d5089c2SBjoern A. Zeeb 		if (dr) {
121233841545SHajimu UMEMOTO 			/*
121333841545SHajimu UMEMOTO 			 * Since defrouter_select() does not affect the
121433841545SHajimu UMEMOTO 			 * on-link determination and MIP6 needs the check
121533841545SHajimu UMEMOTO 			 * before the default router selection, we perform
121633841545SHajimu UMEMOTO 			 * the check now.
121733841545SHajimu UMEMOTO 			 */
121833841545SHajimu UMEMOTO 			pfxlist_onlink_check();
121933841545SHajimu UMEMOTO 
1220686cdd19SJun-ichiro itojun Hagino 			/*
12211d5089c2SBjoern A. Zeeb 			 * Refresh default router list.
1222686cdd19SJun-ichiro itojun Hagino 			 */
1223686cdd19SJun-ichiro itojun Hagino 			defrouter_select();
1224686cdd19SJun-ichiro itojun Hagino 		}
12251d5089c2SBjoern A. Zeeb 
12261d5089c2SBjoern A. Zeeb 		if (ln->ln_router || dr)
12271d5089c2SBjoern A. Zeeb 			LLE_WLOCK(ln);
122882cd038dSYoshinobu Inoue 	}
122982cd038dSYoshinobu Inoue 
123033841545SHajimu UMEMOTO 	/*
123133841545SHajimu UMEMOTO 	 * Before deleting the entry, remember the next entry as the
123233841545SHajimu UMEMOTO 	 * return value.  We need this because pfxlist_onlink_check() above
123333841545SHajimu UMEMOTO 	 * might have freed other entries (particularly the old next entry) as
123433841545SHajimu UMEMOTO 	 * a side effect (XXX).
123533841545SHajimu UMEMOTO 	 */
12366e6b3f7cSQing Li 	next = LIST_NEXT(ln, lle_next);
1237686cdd19SJun-ichiro itojun Hagino 
1238e6950476SBjoern A. Zeeb 	/*
1239e6950476SBjoern A. Zeeb 	 * Save to unlock. We still hold an extra reference and will not
1240e6950476SBjoern A. Zeeb 	 * free(9) in llentry_free() if someone else holds one as well.
1241e6950476SBjoern A. Zeeb 	 */
1242e6950476SBjoern A. Zeeb 	LLE_WUNLOCK(ln);
12436e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
12446e6b3f7cSQing Li 	LLE_WLOCK(ln);
1245b1ec2940SGleb Smirnoff 
1246b1ec2940SGleb Smirnoff 	/* Guard against race with other llentry_free(). */
1247b1ec2940SGleb Smirnoff 	if (ln->la_flags & LLE_LINKED) {
1248becba438SBjoern A. Zeeb 		LLE_REMREF(ln);
12496e6b3f7cSQing Li 		llentry_free(ln);
1250b1ec2940SGleb Smirnoff 	} else
1251b1ec2940SGleb Smirnoff 		LLE_FREE_LOCKED(ln);
1252b1ec2940SGleb Smirnoff 
12536e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
125433841545SHajimu UMEMOTO 
125533841545SHajimu UMEMOTO 	return (next);
125682cd038dSYoshinobu Inoue }
125782cd038dSYoshinobu Inoue 
125882cd038dSYoshinobu Inoue /*
125982cd038dSYoshinobu Inoue  * Upper-layer reachability hint for Neighbor Unreachability Detection.
126082cd038dSYoshinobu Inoue  *
1261cd0fdcf7SHajimu UMEMOTO  * XXX cost-effective methods?
126282cd038dSYoshinobu Inoue  */
126382cd038dSYoshinobu Inoue void
12641272577eSXin LI nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
126582cd038dSYoshinobu Inoue {
12666e6b3f7cSQing Li 	struct llentry *ln;
12676e6b3f7cSQing Li 	struct ifnet *ifp;
126882cd038dSYoshinobu Inoue 
12696e6b3f7cSQing Li 	if ((dst6 == NULL) || (rt == NULL))
127082cd038dSYoshinobu Inoue 		return;
127182cd038dSYoshinobu Inoue 
12726e6b3f7cSQing Li 	ifp = rt->rt_ifp;
1273ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
12746e6b3f7cSQing Li 	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1275ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
12766e6b3f7cSQing Li 	if (ln == NULL)
127782cd038dSYoshinobu Inoue 		return;
127882cd038dSYoshinobu Inoue 
1279ad8d5711SMunechika SUMIKAWA 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
12806e6b3f7cSQing Li 		goto done;
128182cd038dSYoshinobu Inoue 
1282686cdd19SJun-ichiro itojun Hagino 	/*
1283686cdd19SJun-ichiro itojun Hagino 	 * if we get upper-layer reachability confirmation many times,
1284686cdd19SJun-ichiro itojun Hagino 	 * it is possible we have false information.
1285686cdd19SJun-ichiro itojun Hagino 	 */
1286686cdd19SJun-ichiro itojun Hagino 	if (!force) {
1287686cdd19SJun-ichiro itojun Hagino 		ln->ln_byhint++;
12886e6b3f7cSQing Li 		if (ln->ln_byhint > V_nd6_maxnudhint) {
12896e6b3f7cSQing Li 			goto done;
12906e6b3f7cSQing Li 		}
1291686cdd19SJun-ichiro itojun Hagino 	}
1292686cdd19SJun-ichiro itojun Hagino 
129382cd038dSYoshinobu Inoue  	ln->ln_state = ND6_LLINFO_REACHABLE;
1294743eee66SSUZUKI Shinsuke 	if (!ND6_LLINFO_PERMANENT(ln)) {
1295d715e397SBjoern A. Zeeb 		nd6_llinfo_settimer_locked(ln,
1296743eee66SSUZUKI Shinsuke 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1297743eee66SSUZUKI Shinsuke 	}
12986e6b3f7cSQing Li done:
12996e6b3f7cSQing Li 	LLE_WUNLOCK(ln);
130082cd038dSYoshinobu Inoue }
130182cd038dSYoshinobu Inoue 
130282cd038dSYoshinobu Inoue 
13030f1aca65SQing Li /*
13040f1aca65SQing Li  * Rejuvenate this function for routing operations related
13050f1aca65SQing Li  * processing.
13060f1aca65SQing Li  */
13070f1aca65SQing Li void
13080f1aca65SQing Li nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
13090f1aca65SQing Li {
131039e19560SBjoern A. Zeeb 	struct sockaddr_in6 *gateway;
13110f1aca65SQing Li 	struct nd_defrouter *dr;
131239e19560SBjoern A. Zeeb 	struct ifnet *ifp;
13130f1aca65SQing Li 
131439e19560SBjoern A. Zeeb 	gateway = (struct sockaddr_in6 *)rt->rt_gateway;
131539e19560SBjoern A. Zeeb 	ifp = rt->rt_ifp;
13160f1aca65SQing Li 
13170f1aca65SQing Li 	switch (req) {
13180f1aca65SQing Li 	case RTM_ADD:
13190f1aca65SQing Li 		break;
13200f1aca65SQing Li 
13210f1aca65SQing Li 	case RTM_DELETE:
13220f1aca65SQing Li 		if (!ifp)
13230f1aca65SQing Li 			return;
13240f1aca65SQing Li 		/*
13250f1aca65SQing Li 		 * Only indirect routes are interesting.
13260f1aca65SQing Li 		 */
13270f1aca65SQing Li 		if ((rt->rt_flags & RTF_GATEWAY) == 0)
13280f1aca65SQing Li 			return;
13290f1aca65SQing Li 		/*
13300f1aca65SQing Li 		 * check for default route
13310f1aca65SQing Li 		 */
13320f1aca65SQing Li 		if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
13330f1aca65SQing Li 				       &SIN6(rt_key(rt))->sin6_addr)) {
13340f1aca65SQing Li 
13350f1aca65SQing Li 			dr = defrouter_lookup(&gateway->sin6_addr, ifp);
13360f1aca65SQing Li 			if (dr != NULL)
13370f1aca65SQing Li 				dr->installed = 0;
13380f1aca65SQing Li 		}
13390f1aca65SQing Li 		break;
13400f1aca65SQing Li 	}
13410f1aca65SQing Li }
13420f1aca65SQing Li 
13430f1aca65SQing Li 
134482cd038dSYoshinobu Inoue int
13451272577eSXin LI nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
134682cd038dSYoshinobu Inoue {
134782cd038dSYoshinobu Inoue 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
134882cd038dSYoshinobu Inoue 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1349686cdd19SJun-ichiro itojun Hagino 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1350d0bec2c5SHiroki Sato 	int error = 0;
135182cd038dSYoshinobu Inoue 
13520de0dd9bSHiroki Sato 	if (ifp->if_afdata[AF_INET6] == NULL)
13530de0dd9bSHiroki Sato 		return (EPFNOSUPPORT);
135482cd038dSYoshinobu Inoue 	switch (cmd) {
135533841545SHajimu UMEMOTO 	case OSIOCGIFINFO_IN6:
13562ce62dceSSUZUKI Shinsuke #define ND	ndi->ndi
135731b1bfe1SHajimu UMEMOTO 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
13582ce62dceSSUZUKI Shinsuke 		bzero(&ND, sizeof(ND));
13592ce62dceSSUZUKI Shinsuke 		ND.linkmtu = IN6_LINKMTU(ifp);
13602ce62dceSSUZUKI Shinsuke 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
13612ce62dceSSUZUKI Shinsuke 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
13622ce62dceSSUZUKI Shinsuke 		ND.reachable = ND_IFINFO(ifp)->reachable;
13632ce62dceSSUZUKI Shinsuke 		ND.retrans = ND_IFINFO(ifp)->retrans;
13642ce62dceSSUZUKI Shinsuke 		ND.flags = ND_IFINFO(ifp)->flags;
13652ce62dceSSUZUKI Shinsuke 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
13662ce62dceSSUZUKI Shinsuke 		ND.chlim = ND_IFINFO(ifp)->chlim;
136733841545SHajimu UMEMOTO 		break;
136882cd038dSYoshinobu Inoue 	case SIOCGIFINFO_IN6:
13692ce62dceSSUZUKI Shinsuke 		ND = *ND_IFINFO(ifp);
137082cd038dSYoshinobu Inoue 		break;
1371b9204379SSUZUKI Shinsuke 	case SIOCSIFINFO_IN6:
1372b9204379SSUZUKI Shinsuke 		/*
1373b9204379SSUZUKI Shinsuke 		 * used to change host variables from userland.
1374b9204379SSUZUKI Shinsuke 		 * intented for a use on router to reflect RA configurations.
1375b9204379SSUZUKI Shinsuke 		 */
1376b9204379SSUZUKI Shinsuke 		/* 0 means 'unspecified' */
1377b9204379SSUZUKI Shinsuke 		if (ND.linkmtu != 0) {
1378b9204379SSUZUKI Shinsuke 			if (ND.linkmtu < IPV6_MMTU ||
1379b9204379SSUZUKI Shinsuke 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1380b9204379SSUZUKI Shinsuke 				error = EINVAL;
1381b9204379SSUZUKI Shinsuke 				break;
1382b9204379SSUZUKI Shinsuke 			}
1383b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1384b9204379SSUZUKI Shinsuke 		}
1385b9204379SSUZUKI Shinsuke 
1386b9204379SSUZUKI Shinsuke 		if (ND.basereachable != 0) {
1387b9204379SSUZUKI Shinsuke 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1388b9204379SSUZUKI Shinsuke 
1389b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1390b9204379SSUZUKI Shinsuke 			if (ND.basereachable != obasereachable)
1391b9204379SSUZUKI Shinsuke 				ND_IFINFO(ifp)->reachable =
1392b9204379SSUZUKI Shinsuke 				    ND_COMPUTE_RTIME(ND.basereachable);
1393b9204379SSUZUKI Shinsuke 		}
1394b9204379SSUZUKI Shinsuke 		if (ND.retrans != 0)
1395b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->retrans = ND.retrans;
1396b9204379SSUZUKI Shinsuke 		if (ND.chlim != 0)
1397b9204379SSUZUKI Shinsuke 			ND_IFINFO(ifp)->chlim = ND.chlim;
1398b9204379SSUZUKI Shinsuke 		/* FALLTHROUGH */
1399686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFINFO_FLAGS:
1400a283298cSHiroki Sato 	{
1401a283298cSHiroki Sato 		struct ifaddr *ifa;
1402a283298cSHiroki Sato 		struct in6_ifaddr *ia;
1403a283298cSHiroki Sato 
1404a283298cSHiroki Sato 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1405a283298cSHiroki Sato 		    !(ND.flags & ND6_IFF_IFDISABLED)) {
1406a283298cSHiroki Sato 			/* ifdisabled 1->0 transision */
1407a283298cSHiroki Sato 
1408a283298cSHiroki Sato 			/*
1409a283298cSHiroki Sato 			 * If the interface is marked as ND6_IFF_IFDISABLED and
1410a283298cSHiroki Sato 			 * has an link-local address with IN6_IFF_DUPLICATED,
1411a283298cSHiroki Sato 			 * do not clear ND6_IFF_IFDISABLED.
1412a283298cSHiroki Sato 			 * See RFC 4862, Section 5.4.5.
1413a283298cSHiroki Sato 			 */
1414137f91e8SJohn Baldwin 			IF_ADDR_RLOCK(ifp);
1415a283298cSHiroki Sato 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1416a283298cSHiroki Sato 				if (ifa->ifa_addr->sa_family != AF_INET6)
1417a283298cSHiroki Sato 					continue;
1418a283298cSHiroki Sato 				ia = (struct in6_ifaddr *)ifa;
1419a283298cSHiroki Sato 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1420d0bec2c5SHiroki Sato 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1421a283298cSHiroki Sato 					break;
1422a283298cSHiroki Sato 			}
1423137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
1424a283298cSHiroki Sato 
1425d0bec2c5SHiroki Sato 			if (ifa != NULL) {
1426d0bec2c5SHiroki Sato 				/* LLA is duplicated. */
1427a283298cSHiroki Sato 				ND.flags |= ND6_IFF_IFDISABLED;
1428a283298cSHiroki Sato 				log(LOG_ERR, "Cannot enable an interface"
1429a283298cSHiroki Sato 				    " with a link-local address marked"
1430a283298cSHiroki Sato 				    " duplicate.\n");
1431a283298cSHiroki Sato 			} else {
1432a283298cSHiroki Sato 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1433154d5f73SHiroki Sato 				if (ifp->if_flags & IFF_UP)
1434a283298cSHiroki Sato 					in6_if_up(ifp);
1435a283298cSHiroki Sato 			}
1436a283298cSHiroki Sato 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1437a283298cSHiroki Sato 			    (ND.flags & ND6_IFF_IFDISABLED)) {
1438a283298cSHiroki Sato 			/* ifdisabled 0->1 transision */
1439a283298cSHiroki Sato 			/* Mark all IPv6 address as tentative. */
1440a283298cSHiroki Sato 
1441a283298cSHiroki Sato 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1442d0bec2c5SHiroki Sato 			if ((ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD) == 0) {
1443137f91e8SJohn Baldwin 				IF_ADDR_RLOCK(ifp);
1444d0bec2c5SHiroki Sato 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1445d0bec2c5SHiroki Sato 				    ifa_link) {
1446d0bec2c5SHiroki Sato 					if (ifa->ifa_addr->sa_family !=
1447d0bec2c5SHiroki Sato 					    AF_INET6)
1448a283298cSHiroki Sato 						continue;
1449a283298cSHiroki Sato 					ia = (struct in6_ifaddr *)ifa;
1450a283298cSHiroki Sato 					ia->ia6_flags |= IN6_IFF_TENTATIVE;
1451a283298cSHiroki Sato 				}
1452137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
1453a283298cSHiroki Sato 			}
1454d0bec2c5SHiroki Sato 		}
1455a283298cSHiroki Sato 
1456154d5f73SHiroki Sato 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1457154d5f73SHiroki Sato 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1458a283298cSHiroki Sato 				/* auto_linklocal 0->1 transision */
1459a283298cSHiroki Sato 
1460a283298cSHiroki Sato 				/* If no link-local address on ifp, configure */
1461a283298cSHiroki Sato 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1462a283298cSHiroki Sato 				in6_ifattach(ifp, NULL);
1463154d5f73SHiroki Sato 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1464154d5f73SHiroki Sato 			    ifp->if_flags & IFF_UP) {
146577bc4985SHiroki Sato 				/*
146677bc4985SHiroki Sato 				 * When the IF already has
1467154d5f73SHiroki Sato 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1468154d5f73SHiroki Sato 				 * address is assigned, and IFF_UP, try to
1469154d5f73SHiroki Sato 				 * assign one.
147077bc4985SHiroki Sato 				 */
1471137f91e8SJohn Baldwin 				IF_ADDR_RLOCK(ifp);
1472d0bec2c5SHiroki Sato 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1473d0bec2c5SHiroki Sato 				    ifa_link) {
1474d0bec2c5SHiroki Sato 					if (ifa->ifa_addr->sa_family !=
1475d0bec2c5SHiroki Sato 					    AF_INET6)
147677bc4985SHiroki Sato 						continue;
147777bc4985SHiroki Sato 					ia = (struct in6_ifaddr *)ifa;
1478d0bec2c5SHiroki Sato 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
147977bc4985SHiroki Sato 						break;
148077bc4985SHiroki Sato 				}
1481137f91e8SJohn Baldwin 				IF_ADDR_RUNLOCK(ifp);
1482d0bec2c5SHiroki Sato 				if (ifa != NULL)
1483d0bec2c5SHiroki Sato 					/* No LLA is configured. */
148477bc4985SHiroki Sato 					in6_ifattach(ifp, NULL);
1485a283298cSHiroki Sato 			}
1486a283298cSHiroki Sato 		}
1487154d5f73SHiroki Sato 	}
14882ce62dceSSUZUKI Shinsuke 		ND_IFINFO(ifp)->flags = ND.flags;
1489686cdd19SJun-ichiro itojun Hagino 		break;
14902ce62dceSSUZUKI Shinsuke #undef ND
1491686cdd19SJun-ichiro itojun Hagino 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1492743eee66SSUZUKI Shinsuke 		/* sync kernel routing table with the default router list */
1493743eee66SSUZUKI Shinsuke 		defrouter_reset();
1494686cdd19SJun-ichiro itojun Hagino 		defrouter_select();
149582cd038dSYoshinobu Inoue 		break;
149682cd038dSYoshinobu Inoue 	case SIOCSPFXFLUSH_IN6:
149782cd038dSYoshinobu Inoue 	{
149882cd038dSYoshinobu Inoue 		/* flush all the prefix advertised by routers */
149982cd038dSYoshinobu Inoue 		struct nd_prefix *pr, *next;
150082cd038dSYoshinobu Inoue 
15013b0b2840SJohn Baldwin 		LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
150233841545SHajimu UMEMOTO 			struct in6_ifaddr *ia, *ia_next;
150333841545SHajimu UMEMOTO 
150433841545SHajimu UMEMOTO 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
150533841545SHajimu UMEMOTO 				continue; /* XXX */
150633841545SHajimu UMEMOTO 
150733841545SHajimu UMEMOTO 			/* do we really have to remove addresses as well? */
1508d1da0a06SRobert Watson 			/* XXXRW: in6_ifaddrhead locking. */
150980af0152SRobert Watson 			TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
151080af0152SRobert Watson 			    ia_next) {
151133841545SHajimu UMEMOTO 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
151233841545SHajimu UMEMOTO 					continue;
151333841545SHajimu UMEMOTO 
151433841545SHajimu UMEMOTO 				if (ia->ia6_ndpr == pr)
151533841545SHajimu UMEMOTO 					in6_purgeaddr(&ia->ia_ifa);
151633841545SHajimu UMEMOTO 			}
151782cd038dSYoshinobu Inoue 			prelist_remove(pr);
151882cd038dSYoshinobu Inoue 		}
151982cd038dSYoshinobu Inoue 		break;
152082cd038dSYoshinobu Inoue 	}
152182cd038dSYoshinobu Inoue 	case SIOCSRTRFLUSH_IN6:
152282cd038dSYoshinobu Inoue 	{
152382cd038dSYoshinobu Inoue 		/* flush all the default routers */
152482cd038dSYoshinobu Inoue 		struct nd_defrouter *dr, *next;
152582cd038dSYoshinobu Inoue 
1526743eee66SSUZUKI Shinsuke 		defrouter_reset();
15273b0b2840SJohn Baldwin 		TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
152882cd038dSYoshinobu Inoue 			defrtrlist_del(dr);
152982cd038dSYoshinobu Inoue 		}
1530743eee66SSUZUKI Shinsuke 		defrouter_select();
153182cd038dSYoshinobu Inoue 		break;
153282cd038dSYoshinobu Inoue 	}
153382cd038dSYoshinobu Inoue 	case SIOCGNBRINFO_IN6:
153482cd038dSYoshinobu Inoue 	{
15356e6b3f7cSQing Li 		struct llentry *ln;
153682cd038dSYoshinobu Inoue 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
153782cd038dSYoshinobu Inoue 
1538a1f7e5f8SHajimu UMEMOTO 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1539a1f7e5f8SHajimu UMEMOTO 			return (error);
154082cd038dSYoshinobu Inoue 
154173cb2f38SAndrey V. Elsukov 		IF_AFDATA_RLOCK(ifp);
15426e6b3f7cSQing Li 		ln = nd6_lookup(&nb_addr, 0, ifp);
154373cb2f38SAndrey V. Elsukov 		IF_AFDATA_RUNLOCK(ifp);
15446e6b3f7cSQing Li 
15456e6b3f7cSQing Li 		if (ln == NULL) {
154682cd038dSYoshinobu Inoue 			error = EINVAL;
154782cd038dSYoshinobu Inoue 			break;
154882cd038dSYoshinobu Inoue 		}
154982cd038dSYoshinobu Inoue 		nbi->state = ln->ln_state;
15506e6b3f7cSQing Li 		nbi->asked = ln->la_asked;
155182cd038dSYoshinobu Inoue 		nbi->isrouter = ln->ln_router;
15525a041915SHiroki Sato 		if (ln->la_expire == 0)
15535a041915SHiroki Sato 			nbi->expire = 0;
15545a041915SHiroki Sato 		else
15555a041915SHiroki Sato 			nbi->expire = ln->la_expire +
15565a041915SHiroki Sato 			    (time_second - time_uptime);
15576e6b3f7cSQing Li 		LLE_RUNLOCK(ln);
155882cd038dSYoshinobu Inoue 		break;
155982cd038dSYoshinobu Inoue 	}
1560686cdd19SJun-ichiro itojun Hagino 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1561603724d3SBjoern A. Zeeb 		ndif->ifindex = V_nd6_defifindex;
1562686cdd19SJun-ichiro itojun Hagino 		break;
1563686cdd19SJun-ichiro itojun Hagino 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1564686cdd19SJun-ichiro itojun Hagino 		return (nd6_setdefaultiface(ndif->ifindex));
156582cd038dSYoshinobu Inoue 	}
156682cd038dSYoshinobu Inoue 	return (error);
156782cd038dSYoshinobu Inoue }
156882cd038dSYoshinobu Inoue 
156982cd038dSYoshinobu Inoue /*
157082cd038dSYoshinobu Inoue  * Create neighbor cache entry and cache link-layer address,
157182cd038dSYoshinobu Inoue  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
15721272577eSXin LI  *
15731272577eSXin LI  * type - ICMP6 type
15741272577eSXin LI  * code - type dependent information
15756e6b3f7cSQing Li  *
15766e6b3f7cSQing Li  * XXXXX
15776e6b3f7cSQing Li  *  The caller of this function already acquired the ndp
15786e6b3f7cSQing Li  *  cache table lock because the cache entry is returned.
157982cd038dSYoshinobu Inoue  */
15806e6b3f7cSQing Li struct llentry *
15811272577eSXin LI nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
15821272577eSXin LI     int lladdrlen, int type, int code)
158382cd038dSYoshinobu Inoue {
15845a255516SAlexander V. Chernikov 	struct llentry *ln = NULL, *ln_tmp;
158582cd038dSYoshinobu Inoue 	int is_newentry;
158682cd038dSYoshinobu Inoue 	int do_update;
158782cd038dSYoshinobu Inoue 	int olladdr;
158882cd038dSYoshinobu Inoue 	int llchange;
158968352503SBjoern A. Zeeb 	int flags;
159082cd038dSYoshinobu Inoue 	int newstate = 0;
15913d3728e9SQing Li 	uint16_t router = 0;
15926e6b3f7cSQing Li 	struct sockaddr_in6 sin6;
1593fd14c50bSKip Macy 	struct mbuf *chain = NULL;
15943d3728e9SQing Li 	int static_route = 0;
15956e6b3f7cSQing Li 
15966e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
159782cd038dSYoshinobu Inoue 
159839e19560SBjoern A. Zeeb 	KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
159939e19560SBjoern A. Zeeb 	KASSERT(from != NULL, ("%s: from == NULL", __func__));
160082cd038dSYoshinobu Inoue 
160182cd038dSYoshinobu Inoue 	/* nothing must be updated for unspecified address */
160282cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(from))
160382cd038dSYoshinobu Inoue 		return NULL;
160482cd038dSYoshinobu Inoue 
160582cd038dSYoshinobu Inoue 	/*
160682cd038dSYoshinobu Inoue 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
160782cd038dSYoshinobu Inoue 	 * the caller.
160882cd038dSYoshinobu Inoue 	 *
160982cd038dSYoshinobu Inoue 	 * XXX If the link does not have link-layer adderss, what should
161082cd038dSYoshinobu Inoue 	 * we do? (ifp->if_addrlen == 0)
161182cd038dSYoshinobu Inoue 	 * Spec says nothing in sections for RA, RS and NA.  There's small
161282cd038dSYoshinobu Inoue 	 * description on it in NS section (RFC 2461 7.2.3).
161382cd038dSYoshinobu Inoue 	 */
161468352503SBjoern A. Zeeb 	flags = lladdr ? ND6_EXCLUSIVE : 0;
1615ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
16166e6b3f7cSQing Li 	ln = nd6_lookup(from, flags, ifp);
1617ea0c3776SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
16185a255516SAlexander V. Chernikov 	is_newentry = 0;
16196e6b3f7cSQing Li 	if (ln == NULL) {
162068352503SBjoern A. Zeeb 		flags |= ND6_EXCLUSIVE;
16215a255516SAlexander V. Chernikov 		ln = nd6_alloc(from, 0, ifp);
16225a255516SAlexander V. Chernikov 		if (ln == NULL)
16235a255516SAlexander V. Chernikov 			return (NULL);
16245a255516SAlexander V. Chernikov 		IF_AFDATA_WLOCK(ifp);
16255a255516SAlexander V. Chernikov 		LLE_WLOCK(ln);
16265a255516SAlexander V. Chernikov 		/* Prefer any existing lle over newly-created one */
16275a255516SAlexander V. Chernikov 		ln_tmp = nd6_lookup(from, ND6_EXCLUSIVE, ifp);
16285a255516SAlexander V. Chernikov 		if (ln_tmp == NULL)
16295a255516SAlexander V. Chernikov 			lltable_link_entry(LLTABLE6(ifp), ln);
16305a255516SAlexander V. Chernikov 		IF_AFDATA_WUNLOCK(ifp);
16315a255516SAlexander V. Chernikov 		if (ln_tmp == NULL)
16325a255516SAlexander V. Chernikov 			/* No existing lle, mark as new entry */
163382cd038dSYoshinobu Inoue 			is_newentry = 1;
16345a255516SAlexander V. Chernikov 		else {
16355a255516SAlexander V. Chernikov 			lltable_free_entry(LLTABLE6(ifp), ln);
16365a255516SAlexander V. Chernikov 			ln = ln_tmp;
16375a255516SAlexander V. Chernikov 			ln_tmp = NULL;
16385a255516SAlexander V. Chernikov 		}
16395a255516SAlexander V. Chernikov 	}
164033841545SHajimu UMEMOTO 	/* do nothing if static ndp is set */
16415a255516SAlexander V. Chernikov 	if ((ln->la_flags & LLE_STATIC)) {
16423d3728e9SQing Li 			static_route = 1;
16436e6b3f7cSQing Li 			goto done;
16443d3728e9SQing Li 	}
164582cd038dSYoshinobu Inoue 
16466e6b3f7cSQing Li 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
164782cd038dSYoshinobu Inoue 	if (olladdr && lladdr) {
16486e6b3f7cSQing Li 		llchange = bcmp(lladdr, &ln->ll_addr,
16496e6b3f7cSQing Li 		    ifp->if_addrlen);
165082cd038dSYoshinobu Inoue 	} else
165182cd038dSYoshinobu Inoue 		llchange = 0;
165282cd038dSYoshinobu Inoue 
165382cd038dSYoshinobu Inoue 	/*
165482cd038dSYoshinobu Inoue 	 * newentry olladdr  lladdr  llchange	(*=record)
165582cd038dSYoshinobu Inoue 	 *	0	n	n	--	(1)
165682cd038dSYoshinobu Inoue 	 *	0	y	n	--	(2)
165782cd038dSYoshinobu Inoue 	 *	0	n	y	--	(3) * STALE
165882cd038dSYoshinobu Inoue 	 *	0	y	y	n	(4) *
165982cd038dSYoshinobu Inoue 	 *	0	y	y	y	(5) * STALE
166082cd038dSYoshinobu Inoue 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
166182cd038dSYoshinobu Inoue 	 *	1	--	y	--	(7) * STALE
166282cd038dSYoshinobu Inoue 	 */
166382cd038dSYoshinobu Inoue 
166482cd038dSYoshinobu Inoue 	if (lladdr) {		/* (3-5) and (7) */
166582cd038dSYoshinobu Inoue 		/*
166682cd038dSYoshinobu Inoue 		 * Record source link-layer address
166782cd038dSYoshinobu Inoue 		 * XXX is it dependent to ifp->if_type?
166882cd038dSYoshinobu Inoue 		 */
16696e6b3f7cSQing Li 		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
16706e6b3f7cSQing Li 		ln->la_flags |= LLE_VALID;
167163a97a40SNavdeep Parhar 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
167282cd038dSYoshinobu Inoue 	}
167382cd038dSYoshinobu Inoue 
167482cd038dSYoshinobu Inoue 	if (!is_newentry) {
16752ce62dceSSUZUKI Shinsuke 		if ((!olladdr && lladdr != NULL) ||	/* (3) */
16762ce62dceSSUZUKI Shinsuke 		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
167782cd038dSYoshinobu Inoue 			do_update = 1;
167882cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_STALE;
167982cd038dSYoshinobu Inoue 		} else					/* (1-2,4) */
168082cd038dSYoshinobu Inoue 			do_update = 0;
168182cd038dSYoshinobu Inoue 	} else {
168282cd038dSYoshinobu Inoue 		do_update = 1;
16832ce62dceSSUZUKI Shinsuke 		if (lladdr == NULL)			/* (6) */
168482cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_NOSTATE;
168582cd038dSYoshinobu Inoue 		else					/* (7) */
168682cd038dSYoshinobu Inoue 			newstate = ND6_LLINFO_STALE;
168782cd038dSYoshinobu Inoue 	}
168882cd038dSYoshinobu Inoue 
168982cd038dSYoshinobu Inoue 	if (do_update) {
169082cd038dSYoshinobu Inoue 		/*
169182cd038dSYoshinobu Inoue 		 * Update the state of the neighbor cache.
169282cd038dSYoshinobu Inoue 		 */
169382cd038dSYoshinobu Inoue 		ln->ln_state = newstate;
169482cd038dSYoshinobu Inoue 
169582cd038dSYoshinobu Inoue 		if (ln->ln_state == ND6_LLINFO_STALE) {
1696d7968c29SAlexander V. Chernikov 			if (ln->la_hold != NULL)
1697d7968c29SAlexander V. Chernikov 				nd6_grab_holdchain(ln, &chain, &sin6);
169882cd038dSYoshinobu Inoue 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
169982cd038dSYoshinobu Inoue 			/* probe right away */
17006e6b3f7cSQing Li 			nd6_llinfo_settimer_locked((void *)ln, 0);
170182cd038dSYoshinobu Inoue 		}
170282cd038dSYoshinobu Inoue 	}
170382cd038dSYoshinobu Inoue 
170482cd038dSYoshinobu Inoue 	/*
170582cd038dSYoshinobu Inoue 	 * ICMP6 type dependent behavior.
170682cd038dSYoshinobu Inoue 	 *
170782cd038dSYoshinobu Inoue 	 * NS: clear IsRouter if new entry
170882cd038dSYoshinobu Inoue 	 * RS: clear IsRouter
170982cd038dSYoshinobu Inoue 	 * RA: set IsRouter if there's lladdr
171082cd038dSYoshinobu Inoue 	 * redir: clear IsRouter if new entry
171182cd038dSYoshinobu Inoue 	 *
171282cd038dSYoshinobu Inoue 	 * RA case, (1):
171382cd038dSYoshinobu Inoue 	 * The spec says that we must set IsRouter in the following cases:
171482cd038dSYoshinobu Inoue 	 * - If lladdr exist, set IsRouter.  This means (1-5).
171582cd038dSYoshinobu Inoue 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
171682cd038dSYoshinobu Inoue 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
171782cd038dSYoshinobu Inoue 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
171882cd038dSYoshinobu Inoue 	 * neighbor cache, this is similar to (6).
171982cd038dSYoshinobu Inoue 	 * This case is rare but we figured that we MUST NOT set IsRouter.
172082cd038dSYoshinobu Inoue 	 *
172182cd038dSYoshinobu Inoue 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
172282cd038dSYoshinobu Inoue 	 *							D R
172382cd038dSYoshinobu Inoue 	 *	0	n	n	--	(1)	c   ?     s
172482cd038dSYoshinobu Inoue 	 *	0	y	n	--	(2)	c   s     s
172582cd038dSYoshinobu Inoue 	 *	0	n	y	--	(3)	c   s     s
172682cd038dSYoshinobu Inoue 	 *	0	y	y	n	(4)	c   s     s
172782cd038dSYoshinobu Inoue 	 *	0	y	y	y	(5)	c   s     s
172882cd038dSYoshinobu Inoue 	 *	1	--	n	--	(6) c	c	c s
172982cd038dSYoshinobu Inoue 	 *	1	--	y	--	(7) c	c   s	c s
173082cd038dSYoshinobu Inoue 	 *
173182cd038dSYoshinobu Inoue 	 *					(c=clear s=set)
173282cd038dSYoshinobu Inoue 	 */
173382cd038dSYoshinobu Inoue 	switch (type & 0xff) {
173482cd038dSYoshinobu Inoue 	case ND_NEIGHBOR_SOLICIT:
173582cd038dSYoshinobu Inoue 		/*
173682cd038dSYoshinobu Inoue 		 * New entry must have is_router flag cleared.
173782cd038dSYoshinobu Inoue 		 */
173882cd038dSYoshinobu Inoue 		if (is_newentry)	/* (6-7) */
173982cd038dSYoshinobu Inoue 			ln->ln_router = 0;
174082cd038dSYoshinobu Inoue 		break;
174182cd038dSYoshinobu Inoue 	case ND_REDIRECT:
174282cd038dSYoshinobu Inoue 		/*
174382cd038dSYoshinobu Inoue 		 * If the icmp is a redirect to a better router, always set the
174482cd038dSYoshinobu Inoue 		 * is_router flag.  Otherwise, if the entry is newly created,
174582cd038dSYoshinobu Inoue 		 * clear the flag.  [RFC 2461, sec 8.3]
174682cd038dSYoshinobu Inoue 		 */
174782cd038dSYoshinobu Inoue 		if (code == ND_REDIRECT_ROUTER)
174882cd038dSYoshinobu Inoue 			ln->ln_router = 1;
174982cd038dSYoshinobu Inoue 		else if (is_newentry) /* (6-7) */
175082cd038dSYoshinobu Inoue 			ln->ln_router = 0;
175182cd038dSYoshinobu Inoue 		break;
175282cd038dSYoshinobu Inoue 	case ND_ROUTER_SOLICIT:
175382cd038dSYoshinobu Inoue 		/*
175482cd038dSYoshinobu Inoue 		 * is_router flag must always be cleared.
175582cd038dSYoshinobu Inoue 		 */
175682cd038dSYoshinobu Inoue 		ln->ln_router = 0;
175782cd038dSYoshinobu Inoue 		break;
175882cd038dSYoshinobu Inoue 	case ND_ROUTER_ADVERT:
175982cd038dSYoshinobu Inoue 		/*
176082cd038dSYoshinobu Inoue 		 * Mark an entry with lladdr as a router.
176182cd038dSYoshinobu Inoue 		 */
176207eb2995SHajimu UMEMOTO 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
176307eb2995SHajimu UMEMOTO 		    (is_newentry && lladdr)) {			/* (7) */
176482cd038dSYoshinobu Inoue 			ln->ln_router = 1;
176582cd038dSYoshinobu Inoue 		}
176682cd038dSYoshinobu Inoue 		break;
176782cd038dSYoshinobu Inoue 	}
176882cd038dSYoshinobu Inoue 
176923ee1bfaSKip Macy 	if (ln != NULL) {
17703d3728e9SQing Li 		static_route = (ln->la_flags & LLE_STATIC);
1771c2b3a02bSKip Macy 		router = ln->ln_router;
1772c2b3a02bSKip Macy 
17736e6b3f7cSQing Li 		if (flags & ND6_EXCLUSIVE)
17746e6b3f7cSQing Li 			LLE_WUNLOCK(ln);
17756e6b3f7cSQing Li 		else
17766e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
1777c2b3a02bSKip Macy 		if (static_route)
17786e6b3f7cSQing Li 			ln = NULL;
17796e6b3f7cSQing Li 	}
1780d7968c29SAlexander V. Chernikov 	if (chain != NULL)
1781d7968c29SAlexander V. Chernikov 		nd6_flush_holdchain(ifp, ifp, chain, &sin6);
17826e6b3f7cSQing Li 
1783554bf4aaSHajimu UMEMOTO 	/*
1784554bf4aaSHajimu UMEMOTO 	 * When the link-layer address of a router changes, select the
1785554bf4aaSHajimu UMEMOTO 	 * best router again.  In particular, when the neighbor entry is newly
1786554bf4aaSHajimu UMEMOTO 	 * created, it might affect the selection policy.
1787554bf4aaSHajimu UMEMOTO 	 * Question: can we restrict the first condition to the "is_newentry"
1788554bf4aaSHajimu UMEMOTO 	 * case?
1789554bf4aaSHajimu UMEMOTO 	 * XXX: when we hear an RA from a new router with the link-layer
1790554bf4aaSHajimu UMEMOTO 	 * address option, defrouter_select() is called twice, since
1791554bf4aaSHajimu UMEMOTO 	 * defrtrlist_update called the function as well.  However, I believe
1792554bf4aaSHajimu UMEMOTO 	 * we can compromise the overhead, since it only happens the first
1793554bf4aaSHajimu UMEMOTO 	 * time.
1794554bf4aaSHajimu UMEMOTO 	 * XXX: although defrouter_select() should not have a bad effect
1795554bf4aaSHajimu UMEMOTO 	 * for those are not autoconfigured hosts, we explicitly avoid such
1796554bf4aaSHajimu UMEMOTO 	 * cases for safety.
1797554bf4aaSHajimu UMEMOTO 	 */
1798e7fa8d0aSHiroki Sato 	if (do_update && router &&
1799a283298cSHiroki Sato 	    ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
18006e6b3f7cSQing Li 		/*
18016e6b3f7cSQing Li 		 * guaranteed recursion
18026e6b3f7cSQing Li 		 */
1803554bf4aaSHajimu UMEMOTO 		defrouter_select();
18046e6b3f7cSQing Li 	}
1805554bf4aaSHajimu UMEMOTO 
18066e6b3f7cSQing Li 	return (ln);
18076e6b3f7cSQing Li done:
1808aba53ef0SKip Macy 	if (ln != NULL) {
18096e6b3f7cSQing Li 		if (flags & ND6_EXCLUSIVE)
18106e6b3f7cSQing Li 			LLE_WUNLOCK(ln);
18116e6b3f7cSQing Li 		else
18126e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
18133d3728e9SQing Li 		if (static_route)
18146e6b3f7cSQing Li 			ln = NULL;
18156e6b3f7cSQing Li 	}
18166e6b3f7cSQing Li 	return (ln);
181782cd038dSYoshinobu Inoue }
181882cd038dSYoshinobu Inoue 
181982cd038dSYoshinobu Inoue static void
18208b615593SMarko Zec nd6_slowtimo(void *arg)
182182cd038dSYoshinobu Inoue {
18228b615593SMarko Zec 	CURVNET_SET((struct vnet *) arg);
182333841545SHajimu UMEMOTO 	struct nd_ifinfo *nd6if;
182431b1bfe1SHajimu UMEMOTO 	struct ifnet *ifp;
182582cd038dSYoshinobu Inoue 
1826603724d3SBjoern A. Zeeb 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1827f6dfe47aSMarko Zec 	    nd6_slowtimo, curvnet);
182877dfcdc4SRobert Watson 	IFNET_RLOCK_NOSLEEP();
1829e6abaf91SGleb Smirnoff 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
18300de0dd9bSHiroki Sato 		if (ifp->if_afdata[AF_INET6] == NULL)
18310de0dd9bSHiroki Sato 			continue;
183231b1bfe1SHajimu UMEMOTO 		nd6if = ND_IFINFO(ifp);
183382cd038dSYoshinobu Inoue 		if (nd6if->basereachable && /* already initialized */
183482cd038dSYoshinobu Inoue 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
183582cd038dSYoshinobu Inoue 			/*
183682cd038dSYoshinobu Inoue 			 * Since reachable time rarely changes by router
183782cd038dSYoshinobu Inoue 			 * advertisements, we SHOULD insure that a new random
183882cd038dSYoshinobu Inoue 			 * value gets recomputed at least once every few hours.
183982cd038dSYoshinobu Inoue 			 * (RFC 2461, 6.3.4)
184082cd038dSYoshinobu Inoue 			 */
1841603724d3SBjoern A. Zeeb 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
184282cd038dSYoshinobu Inoue 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
184382cd038dSYoshinobu Inoue 		}
184482cd038dSYoshinobu Inoue 	}
184577dfcdc4SRobert Watson 	IFNET_RUNLOCK_NOSLEEP();
18468b615593SMarko Zec 	CURVNET_RESTORE();
184782cd038dSYoshinobu Inoue }
184882cd038dSYoshinobu Inoue 
1849d7968c29SAlexander V. Chernikov void
1850d7968c29SAlexander V. Chernikov nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
1851d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *sin6)
1852d7968c29SAlexander V. Chernikov {
1853d7968c29SAlexander V. Chernikov 
1854d7968c29SAlexander V. Chernikov 	LLE_WLOCK_ASSERT(ln);
1855d7968c29SAlexander V. Chernikov 
1856d7968c29SAlexander V. Chernikov 	*chain = ln->la_hold;
1857d7968c29SAlexander V. Chernikov 	ln->la_hold = NULL;
1858314294deSAlexander V. Chernikov 	lltable_fill_sa_entry(ln, (struct sockaddr *)sin6);
1859d7968c29SAlexander V. Chernikov 
1860d7968c29SAlexander V. Chernikov 	if (ln->ln_state == ND6_LLINFO_STALE) {
1861d7968c29SAlexander V. Chernikov 
1862d7968c29SAlexander V. Chernikov 		/*
1863d7968c29SAlexander V. Chernikov 		 * The first time we send a packet to a
1864d7968c29SAlexander V. Chernikov 		 * neighbor whose entry is STALE, we have
1865d7968c29SAlexander V. Chernikov 		 * to change the state to DELAY and a sets
1866d7968c29SAlexander V. Chernikov 		 * a timer to expire in DELAY_FIRST_PROBE_TIME
1867d7968c29SAlexander V. Chernikov 		 * seconds to ensure do neighbor unreachability
1868d7968c29SAlexander V. Chernikov 		 * detection on expiration.
1869d7968c29SAlexander V. Chernikov 		 * (RFC 2461 7.3.3)
1870d7968c29SAlexander V. Chernikov 		 */
1871d7968c29SAlexander V. Chernikov 		ln->la_asked = 0;
1872d7968c29SAlexander V. Chernikov 		ln->ln_state = ND6_LLINFO_DELAY;
1873d7968c29SAlexander V. Chernikov 		nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1874d7968c29SAlexander V. Chernikov 	}
1875d7968c29SAlexander V. Chernikov }
1876d7968c29SAlexander V. Chernikov 
1877d7968c29SAlexander V. Chernikov static int
1878d7968c29SAlexander V. Chernikov nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1879d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *dst)
1880d7968c29SAlexander V. Chernikov {
1881d7968c29SAlexander V. Chernikov 	int error;
1882d7968c29SAlexander V. Chernikov 	int ip6len;
1883d7968c29SAlexander V. Chernikov 	struct ip6_hdr *ip6;
1884d7968c29SAlexander V. Chernikov 	struct m_tag *mtag;
1885d7968c29SAlexander V. Chernikov 
1886d7968c29SAlexander V. Chernikov #ifdef MAC
1887d7968c29SAlexander V. Chernikov 	mac_netinet6_nd6_send(ifp, m);
1888d7968c29SAlexander V. Chernikov #endif
1889d7968c29SAlexander V. Chernikov 
1890d7968c29SAlexander V. Chernikov 	/*
1891d7968c29SAlexander V. Chernikov 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
1892d7968c29SAlexander V. Chernikov 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
1893d7968c29SAlexander V. Chernikov 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
1894d7968c29SAlexander V. Chernikov 	 * to be diverted to user space.  When re-injected into the kernel,
1895d7968c29SAlexander V. Chernikov 	 * send_output() will directly dispatch them to the outgoing interface.
1896d7968c29SAlexander V. Chernikov 	 */
1897d7968c29SAlexander V. Chernikov 	if (send_sendso_input_hook != NULL) {
1898d7968c29SAlexander V. Chernikov 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
1899d7968c29SAlexander V. Chernikov 		if (mtag != NULL) {
1900d7968c29SAlexander V. Chernikov 			ip6 = mtod(m, struct ip6_hdr *);
1901d7968c29SAlexander V. Chernikov 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
1902d7968c29SAlexander V. Chernikov 			/* Use the SEND socket */
1903d7968c29SAlexander V. Chernikov 			error = send_sendso_input_hook(m, ifp, SND_OUT,
1904d7968c29SAlexander V. Chernikov 			    ip6len);
1905d7968c29SAlexander V. Chernikov 			/* -1 == no app on SEND socket */
1906d7968c29SAlexander V. Chernikov 			if (error == 0 || error != -1)
1907d7968c29SAlexander V. Chernikov 			    return (error);
1908d7968c29SAlexander V. Chernikov 		}
1909d7968c29SAlexander V. Chernikov 	}
1910d7968c29SAlexander V. Chernikov 
1911d7968c29SAlexander V. Chernikov 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
1912d7968c29SAlexander V. Chernikov 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
1913d7968c29SAlexander V. Chernikov 	    mtod(m, struct ip6_hdr *));
1914d7968c29SAlexander V. Chernikov 
1915d7968c29SAlexander V. Chernikov 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1916d7968c29SAlexander V. Chernikov 		origifp = ifp;
1917d7968c29SAlexander V. Chernikov 
1918d7968c29SAlexander V. Chernikov 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
1919d7968c29SAlexander V. Chernikov 	return (error);
1920d7968c29SAlexander V. Chernikov }
1921d7968c29SAlexander V. Chernikov 
1922f6990c4eSAlexander V. Chernikov /*
1923f6990c4eSAlexander V. Chernikov  * IPv6 packet output - light version.
1924f6990c4eSAlexander V. Chernikov  * Checks if destination LLE exists and is in proper state
1925f6990c4eSAlexander V. Chernikov  * (e.g no modification required). If not true, fall back to
1926f6990c4eSAlexander V. Chernikov  * "heavy" version.
1927f6990c4eSAlexander V. Chernikov  */
192882cd038dSYoshinobu Inoue int
1929f6990c4eSAlexander V. Chernikov nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
19301272577eSXin LI     struct sockaddr_in6 *dst, struct rtentry *rt0)
193182cd038dSYoshinobu Inoue {
1932f6990c4eSAlexander V. Chernikov 	struct llentry *ln = NULL;
19336e6b3f7cSQing Li 
1934f6990c4eSAlexander V. Chernikov 	/* discard the packet if IPv6 operation is disabled on the interface */
1935f6990c4eSAlexander V. Chernikov 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1936f6990c4eSAlexander V. Chernikov 		m_freem(m);
1937f6990c4eSAlexander V. Chernikov 		return (ENETDOWN); /* better error? */
1938f6990c4eSAlexander V. Chernikov 	}
1939f6990c4eSAlexander V. Chernikov 
1940f6990c4eSAlexander V. Chernikov 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1941f6990c4eSAlexander V. Chernikov 		goto sendpkt;
1942f6990c4eSAlexander V. Chernikov 
1943f6990c4eSAlexander V. Chernikov 	if (nd6_need_cache(ifp) == 0)
1944f6990c4eSAlexander V. Chernikov 		goto sendpkt;
1945f6990c4eSAlexander V. Chernikov 
1946f6990c4eSAlexander V. Chernikov 	IF_AFDATA_RLOCK(ifp);
1947f6990c4eSAlexander V. Chernikov 	ln = nd6_lookup(&dst->sin6_addr, 0, ifp);
1948f6990c4eSAlexander V. Chernikov 	IF_AFDATA_RUNLOCK(ifp);
1949f6990c4eSAlexander V. Chernikov 
1950f6990c4eSAlexander V. Chernikov 	/*
1951f6990c4eSAlexander V. Chernikov 	 * Perform fast path for the following cases:
1952f6990c4eSAlexander V. Chernikov 	 * 1) lle state is REACHABLE
1953f6990c4eSAlexander V. Chernikov 	 * 2) lle state is DELAY (NS message sentNS message sent)
1954f6990c4eSAlexander V. Chernikov 	 *
1955f6990c4eSAlexander V. Chernikov 	 * Every other case involves lle modification, so we handle
1956f6990c4eSAlexander V. Chernikov 	 * them separately.
1957f6990c4eSAlexander V. Chernikov 	 */
1958f6990c4eSAlexander V. Chernikov 	if (ln == NULL || (ln->ln_state != ND6_LLINFO_REACHABLE &&
1959f6990c4eSAlexander V. Chernikov 	    ln->ln_state != ND6_LLINFO_DELAY)) {
1960f6990c4eSAlexander V. Chernikov 		/* Fall back to slow processing path */
1961f6990c4eSAlexander V. Chernikov 		if (ln != NULL)
1962f6990c4eSAlexander V. Chernikov 			LLE_RUNLOCK(ln);
1963d7968c29SAlexander V. Chernikov 		return (nd6_output_lle(ifp, origifp, m, dst));
1964f6990c4eSAlexander V. Chernikov 	}
1965f6990c4eSAlexander V. Chernikov 
1966f6990c4eSAlexander V. Chernikov sendpkt:
1967f6990c4eSAlexander V. Chernikov 	if (ln != NULL)
1968f6990c4eSAlexander V. Chernikov 		LLE_RUNLOCK(ln);
1969f6990c4eSAlexander V. Chernikov 
1970d7968c29SAlexander V. Chernikov 	return (nd6_output_ifp(ifp, origifp, m, dst));
19716e6b3f7cSQing Li }
19726e6b3f7cSQing Li 
19736e6b3f7cSQing Li 
19746e6b3f7cSQing Li /*
1975f6990c4eSAlexander V. Chernikov  * Output IPv6 packet - heavy version.
1976f6990c4eSAlexander V. Chernikov  * Function assume that either
1977f6990c4eSAlexander V. Chernikov  * 1) destination LLE does not exist, is invalid or stale, so
1978f6990c4eSAlexander V. Chernikov  *   ND6_EXCLUSIVE lock needs to be acquired
1979f6990c4eSAlexander V. Chernikov  * 2) destination lle is provided (with ND6_EXCLUSIVE lock),
1980f6990c4eSAlexander V. Chernikov  *   in that case packets are queued in &chain.
19816e6b3f7cSQing Li  *
19826e6b3f7cSQing Li  */
1983d7968c29SAlexander V. Chernikov static int
1984f6990c4eSAlexander V. Chernikov nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1985d7968c29SAlexander V. Chernikov     struct sockaddr_in6 *dst)
19866e6b3f7cSQing Li {
19875a255516SAlexander V. Chernikov 	struct llentry *lle = NULL, *lle_tmp;
198882cd038dSYoshinobu Inoue 
19899dffa6a3SAlexander V. Chernikov 	KASSERT(m != NULL, ("NULL mbuf, nothing to send"));
19909dffa6a3SAlexander V. Chernikov 	/* discard the packet if IPv6 operation is disabled on the interface */
19919dffa6a3SAlexander V. Chernikov 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
19929dffa6a3SAlexander V. Chernikov 		m_freem(m);
19939dffa6a3SAlexander V. Chernikov 		return (ENETDOWN); /* better error? */
19949dffa6a3SAlexander V. Chernikov 	}
19959dffa6a3SAlexander V. Chernikov 
199682cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
199782cd038dSYoshinobu Inoue 		goto sendpkt;
199882cd038dSYoshinobu Inoue 
199933841545SHajimu UMEMOTO 	if (nd6_need_cache(ifp) == 0)
200082cd038dSYoshinobu Inoue 		goto sendpkt;
200182cd038dSYoshinobu Inoue 
200282cd038dSYoshinobu Inoue 	/*
200382cd038dSYoshinobu Inoue 	 * Address resolution or Neighbor Unreachability Detection
200482cd038dSYoshinobu Inoue 	 * for the next hop.
200582cd038dSYoshinobu Inoue 	 * At this point, the destination of the packet must be a unicast
200682cd038dSYoshinobu Inoue 	 * or an anycast address(i.e. not a multicast).
200782cd038dSYoshinobu Inoue 	 */
2008f6990c4eSAlexander V. Chernikov 	if (lle == NULL) {
2009c445d252SAdrian Chadd 		IF_AFDATA_RLOCK(ifp);
2010f6990c4eSAlexander V. Chernikov 		lle = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
2011c445d252SAdrian Chadd 		IF_AFDATA_RUNLOCK(ifp);
2012f6990c4eSAlexander V. Chernikov 		if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
2013686cdd19SJun-ichiro itojun Hagino 			/*
2014686cdd19SJun-ichiro itojun Hagino 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2015686cdd19SJun-ichiro itojun Hagino 			 * the condition below is not very efficient.  But we believe
2016686cdd19SJun-ichiro itojun Hagino 			 * it is tolerable, because this should be a rare case.
2017686cdd19SJun-ichiro itojun Hagino 			 */
20185a255516SAlexander V. Chernikov 			lle = nd6_alloc(&dst->sin6_addr, 0, ifp);
2019f6990c4eSAlexander V. Chernikov 			if (lle == NULL) {
20201d54aa3bSBjoern A. Zeeb 				char ip6buf[INET6_ADDRSTRLEN];
2021686cdd19SJun-ichiro itojun Hagino 				log(LOG_DEBUG,
2022686cdd19SJun-ichiro itojun Hagino 				    "nd6_output: can't allocate llinfo for %s "
2023bbd8aebaSQing Li 				    "(ln=%p)\n",
2024f6990c4eSAlexander V. Chernikov 				    ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
20259dffa6a3SAlexander V. Chernikov 				m_freem(m);
20269dffa6a3SAlexander V. Chernikov 				return (ENOBUFS);
202782cd038dSYoshinobu Inoue 			}
20285a255516SAlexander V. Chernikov 			lle->ln_state = ND6_LLINFO_NOSTATE;
20295a255516SAlexander V. Chernikov 
20305a255516SAlexander V. Chernikov 			IF_AFDATA_WLOCK(ifp);
20315a255516SAlexander V. Chernikov 			LLE_WLOCK(lle);
20325a255516SAlexander V. Chernikov 			/* Prefer any existing entry over newly-created one */
20335a255516SAlexander V. Chernikov 			lle_tmp = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
20345a255516SAlexander V. Chernikov 			if (lle_tmp == NULL)
20355a255516SAlexander V. Chernikov 				lltable_link_entry(LLTABLE6(ifp), lle);
20365a255516SAlexander V. Chernikov 			IF_AFDATA_WUNLOCK(ifp);
20375a255516SAlexander V. Chernikov 			if (lle_tmp != NULL) {
20385a255516SAlexander V. Chernikov 				lltable_free_entry(LLTABLE6(ifp), lle);
20395a255516SAlexander V. Chernikov 				lle = lle_tmp;
20405a255516SAlexander V. Chernikov 				lle_tmp = NULL;
20415a255516SAlexander V. Chernikov 			}
20425a255516SAlexander V. Chernikov 		}
20435a255516SAlexander V. Chernikov 	}
20445a255516SAlexander V. Chernikov 	if (lle == NULL) {
20455a255516SAlexander V. Chernikov 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
20465a255516SAlexander V. Chernikov 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
20475a255516SAlexander V. Chernikov 			m_freem(m);
20485a255516SAlexander V. Chernikov 			return (ENOBUFS);
20495a255516SAlexander V. Chernikov 		}
2050686cdd19SJun-ichiro itojun Hagino 		goto sendpkt;	/* send anyway */
2051686cdd19SJun-ichiro itojun Hagino 	}
2052686cdd19SJun-ichiro itojun Hagino 
2053f6990c4eSAlexander V. Chernikov 	LLE_WLOCK_ASSERT(lle);
2054f6990c4eSAlexander V. Chernikov 
2055686cdd19SJun-ichiro itojun Hagino 	/* We don't have to do link-layer address resolution on a p2p link. */
2056686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2057f6990c4eSAlexander V. Chernikov 	    lle->ln_state < ND6_LLINFO_REACHABLE) {
2058f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_STALE;
2059f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_gctimer * hz);
206033841545SHajimu UMEMOTO 	}
206182cd038dSYoshinobu Inoue 
206282cd038dSYoshinobu Inoue 	/*
206382cd038dSYoshinobu Inoue 	 * The first time we send a packet to a neighbor whose entry is
206482cd038dSYoshinobu Inoue 	 * STALE, we have to change the state to DELAY and a sets a timer to
206582cd038dSYoshinobu Inoue 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
206682cd038dSYoshinobu Inoue 	 * neighbor unreachability detection on expiration.
206782cd038dSYoshinobu Inoue 	 * (RFC 2461 7.3.3)
206882cd038dSYoshinobu Inoue 	 */
2069f6990c4eSAlexander V. Chernikov 	if (lle->ln_state == ND6_LLINFO_STALE) {
2070f6990c4eSAlexander V. Chernikov 		lle->la_asked = 0;
2071f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_DELAY;
2072f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_delay * hz);
207382cd038dSYoshinobu Inoue 	}
207482cd038dSYoshinobu Inoue 
207582cd038dSYoshinobu Inoue 	/*
207682cd038dSYoshinobu Inoue 	 * If the neighbor cache entry has a state other than INCOMPLETE
207788ff5695SSUZUKI Shinsuke 	 * (i.e. its link-layer address is already resolved), just
207882cd038dSYoshinobu Inoue 	 * send the packet.
207982cd038dSYoshinobu Inoue 	 */
2080f6990c4eSAlexander V. Chernikov 	if (lle->ln_state > ND6_LLINFO_INCOMPLETE)
208182cd038dSYoshinobu Inoue 		goto sendpkt;
208282cd038dSYoshinobu Inoue 
208382cd038dSYoshinobu Inoue 	/*
208482cd038dSYoshinobu Inoue 	 * There is a neighbor cache entry, but no ethernet address
2085743eee66SSUZUKI Shinsuke 	 * response yet.  Append this latest packet to the end of the
2086743eee66SSUZUKI Shinsuke 	 * packet queue in the mbuf, unless the number of the packet
2087743eee66SSUZUKI Shinsuke 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2088743eee66SSUZUKI Shinsuke 	 * the oldest packet in the queue will be removed.
208982cd038dSYoshinobu Inoue 	 */
2090f6990c4eSAlexander V. Chernikov 	if (lle->ln_state == ND6_LLINFO_NOSTATE)
2091f6990c4eSAlexander V. Chernikov 		lle->ln_state = ND6_LLINFO_INCOMPLETE;
20926e6b3f7cSQing Li 
2093f6990c4eSAlexander V. Chernikov 	if (lle->la_hold != NULL) {
2094743eee66SSUZUKI Shinsuke 		struct mbuf *m_hold;
2095743eee66SSUZUKI Shinsuke 		int i;
2096743eee66SSUZUKI Shinsuke 
2097743eee66SSUZUKI Shinsuke 		i = 0;
2098f6990c4eSAlexander V. Chernikov 		for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2099743eee66SSUZUKI Shinsuke 			i++;
2100743eee66SSUZUKI Shinsuke 			if (m_hold->m_nextpkt == NULL) {
2101743eee66SSUZUKI Shinsuke 				m_hold->m_nextpkt = m;
2102743eee66SSUZUKI Shinsuke 				break;
210382cd038dSYoshinobu Inoue 			}
210482cd038dSYoshinobu Inoue 		}
2105603724d3SBjoern A. Zeeb 		while (i >= V_nd6_maxqueuelen) {
2106f6990c4eSAlexander V. Chernikov 			m_hold = lle->la_hold;
2107f6990c4eSAlexander V. Chernikov 			lle->la_hold = lle->la_hold->m_nextpkt;
210831d4137bSSUZUKI Shinsuke 			m_freem(m_hold);
2109743eee66SSUZUKI Shinsuke 			i--;
2110743eee66SSUZUKI Shinsuke 		}
2111743eee66SSUZUKI Shinsuke 	} else {
2112f6990c4eSAlexander V. Chernikov 		lle->la_hold = m;
21136e6b3f7cSQing Li 	}
2114743eee66SSUZUKI Shinsuke 
2115743eee66SSUZUKI Shinsuke 	/*
2116743eee66SSUZUKI Shinsuke 	 * If there has been no NS for the neighbor after entering the
2117743eee66SSUZUKI Shinsuke 	 * INCOMPLETE state, send the first solicitation.
2118743eee66SSUZUKI Shinsuke 	 */
2119f6990c4eSAlexander V. Chernikov 	if (!ND6_LLINFO_PERMANENT(lle) && lle->la_asked == 0) {
212026deb882SAlexander V. Chernikov 		struct in6_addr src, *psrc;
2121f6990c4eSAlexander V. Chernikov 		lle->la_asked++;
21226e6b3f7cSQing Li 
2123f6990c4eSAlexander V. Chernikov 		nd6_llinfo_settimer_locked(lle,
2124743eee66SSUZUKI Shinsuke 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
212526deb882SAlexander V. Chernikov 		psrc = nd6_llinfo_get_holdsrc(lle, &src);
2126f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
212726deb882SAlexander V. Chernikov 		nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL);
2128d7968c29SAlexander V. Chernikov 	} else {
2129d7968c29SAlexander V. Chernikov 		/* We did the lookup so we need to do the unlock here. */
2130f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
2131743eee66SSUZUKI Shinsuke 	}
2132e6950476SBjoern A. Zeeb 
213382cd038dSYoshinobu Inoue 	return (0);
213482cd038dSYoshinobu Inoue 
213582cd038dSYoshinobu Inoue   sendpkt:
2136d7968c29SAlexander V. Chernikov 	if (lle != NULL)
2137f6990c4eSAlexander V. Chernikov 		LLE_WUNLOCK(lle);
21385b27b045SSUZUKI Shinsuke 
2139d7968c29SAlexander V. Chernikov 	return (nd6_output_ifp(ifp, origifp, m, dst));
214082cd038dSYoshinobu Inoue }
214182cd038dSYoshinobu Inoue 
21426e6b3f7cSQing Li 
21436e6b3f7cSQing Li int
2144d7968c29SAlexander V. Chernikov nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
21459c9bde01SAlexander V. Chernikov     struct sockaddr_in6 *dst)
21466e6b3f7cSQing Li {
21476e6b3f7cSQing Li 	struct mbuf *m, *m_head;
21486e6b3f7cSQing Li 	struct ifnet *outifp;
21496e6b3f7cSQing Li 	int error = 0;
21506e6b3f7cSQing Li 
21516e6b3f7cSQing Li 	m_head = chain;
21526e6b3f7cSQing Li 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
21536e6b3f7cSQing Li 		outifp = origifp;
21546e6b3f7cSQing Li 	else
21556e6b3f7cSQing Li 		outifp = ifp;
21566e6b3f7cSQing Li 
21576e6b3f7cSQing Li 	while (m_head) {
21586e6b3f7cSQing Li 		m = m_head;
21596e6b3f7cSQing Li 		m_head = m_head->m_nextpkt;
2160d7968c29SAlexander V. Chernikov 		error = nd6_output_ifp(ifp, origifp, m, dst);
21616e6b3f7cSQing Li 	}
21626e6b3f7cSQing Li 
21636e6b3f7cSQing Li 	/*
21646e6b3f7cSQing Li 	 * XXX
21656e6b3f7cSQing Li 	 * note that intermediate errors are blindly ignored - but this is
21666e6b3f7cSQing Li 	 * the same convention as used with nd6_output when called by
21676e6b3f7cSQing Li 	 * nd6_cache_lladdr
21686e6b3f7cSQing Li 	 */
21696e6b3f7cSQing Li 	return (error);
21706e6b3f7cSQing Li }
21716e6b3f7cSQing Li 
21726e6b3f7cSQing Li 
217382cd038dSYoshinobu Inoue int
21741272577eSXin LI nd6_need_cache(struct ifnet *ifp)
217533841545SHajimu UMEMOTO {
217633841545SHajimu UMEMOTO 	/*
217733841545SHajimu UMEMOTO 	 * XXX: we currently do not make neighbor cache on any interface
217833841545SHajimu UMEMOTO 	 * other than ARCnet, Ethernet, FDDI and GIF.
217933841545SHajimu UMEMOTO 	 *
218033841545SHajimu UMEMOTO 	 * RFC2893 says:
218133841545SHajimu UMEMOTO 	 * - unidirectional tunnels needs no ND
218233841545SHajimu UMEMOTO 	 */
218333841545SHajimu UMEMOTO 	switch (ifp->if_type) {
218433841545SHajimu UMEMOTO 	case IFT_ARCNET:
218533841545SHajimu UMEMOTO 	case IFT_ETHER:
218633841545SHajimu UMEMOTO 	case IFT_FDDI:
218733841545SHajimu UMEMOTO 	case IFT_IEEE1394:
218805b6760dSMunechika SUMIKAWA 	case IFT_L2VLAN:
218933841545SHajimu UMEMOTO 	case IFT_IEEE80211:
2190e4cd31ddSJeff Roberson 	case IFT_INFINIBAND:
219159280079SAndrew Thompson 	case IFT_BRIDGE:
2192c57086ceSHajimu UMEMOTO 	case IFT_PROPVIRTUAL:
219333841545SHajimu UMEMOTO 		return (1);
219433841545SHajimu UMEMOTO 	default:
219533841545SHajimu UMEMOTO 		return (0);
219633841545SHajimu UMEMOTO 	}
219733841545SHajimu UMEMOTO }
219833841545SHajimu UMEMOTO 
21996e6b3f7cSQing Li /*
2200f6b84910SAlexander V. Chernikov  * Add pernament ND6 link-layer record for given
2201f6b84910SAlexander V. Chernikov  * interface address.
2202f6b84910SAlexander V. Chernikov  *
2203f6b84910SAlexander V. Chernikov  * Very similar to IPv4 arp_ifinit(), but:
2204f6b84910SAlexander V. Chernikov  * 1) IPv6 DAD is performed in different place
2205f6b84910SAlexander V. Chernikov  * 2) It is called by IPv6 protocol stack in contrast to
2206f6b84910SAlexander V. Chernikov  * arp_ifinit() which is typically called in SIOCSIFADDR
2207f6b84910SAlexander V. Chernikov  * driver ioctl handler.
2208f6b84910SAlexander V. Chernikov  *
2209f6b84910SAlexander V. Chernikov  */
2210f6b84910SAlexander V. Chernikov int
2211f6b84910SAlexander V. Chernikov nd6_add_ifa_lle(struct in6_ifaddr *ia)
2212f6b84910SAlexander V. Chernikov {
2213f6b84910SAlexander V. Chernikov 	struct ifnet *ifp;
22145a255516SAlexander V. Chernikov 	struct llentry *ln, *ln_tmp;
22155a255516SAlexander V. Chernikov 	struct sockaddr *dst;
2216f6b84910SAlexander V. Chernikov 
2217f6b84910SAlexander V. Chernikov 	ifp = ia->ia_ifa.ifa_ifp;
2218af6209a1SAndrey V. Elsukov 	if (nd6_need_cache(ifp) == 0)
2219af6209a1SAndrey V. Elsukov 		return (0);
22205a255516SAlexander V. Chernikov 
2221f6b84910SAlexander V. Chernikov 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
22225a255516SAlexander V. Chernikov 	dst = (struct sockaddr *)&ia->ia_addr;
22235a255516SAlexander V. Chernikov 	ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst);
22245a255516SAlexander V. Chernikov 	if (ln == NULL)
22255a255516SAlexander V. Chernikov 		return (ENOBUFS);
22265a255516SAlexander V. Chernikov 
22275a255516SAlexander V. Chernikov 	IF_AFDATA_WLOCK(ifp);
22285a255516SAlexander V. Chernikov 	LLE_WLOCK(ln);
22295a255516SAlexander V. Chernikov 	/* Unlink any entry if exists */
22305a255516SAlexander V. Chernikov 	ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_EXCLUSIVE, dst);
22315a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22325a255516SAlexander V. Chernikov 		lltable_unlink_entry(LLTABLE6(ifp), ln_tmp);
22335a255516SAlexander V. Chernikov 	lltable_link_entry(LLTABLE6(ifp), ln);
22345a255516SAlexander V. Chernikov 	IF_AFDATA_WUNLOCK(ifp);
22355a255516SAlexander V. Chernikov 
22365a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22375a255516SAlexander V. Chernikov 		EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED);
22385a255516SAlexander V. Chernikov 	EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
22395a255516SAlexander V. Chernikov 
22405a255516SAlexander V. Chernikov 	LLE_WUNLOCK(ln);
22415a255516SAlexander V. Chernikov 	if (ln_tmp != NULL)
22425a255516SAlexander V. Chernikov 		llentry_free(ln_tmp);
22435a255516SAlexander V. Chernikov 
22445a255516SAlexander V. Chernikov 	return (0);
2245f6b84910SAlexander V. Chernikov }
2246f6b84910SAlexander V. Chernikov 
2247f6b84910SAlexander V. Chernikov /*
2248*3e7a2321SAlexander V. Chernikov  * Removes either all lle entries for given @ia, or lle
2249*3e7a2321SAlexander V. Chernikov  * corresponding to @ia address.
2250f6b84910SAlexander V. Chernikov  */
2251f6b84910SAlexander V. Chernikov void
2252*3e7a2321SAlexander V. Chernikov nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all)
2253f6b84910SAlexander V. Chernikov {
2254f6b84910SAlexander V. Chernikov 	struct sockaddr_in6 mask, addr;
2255*3e7a2321SAlexander V. Chernikov 	struct sockaddr *saddr, *smask;
2256f6b84910SAlexander V. Chernikov 	struct ifnet *ifp;
2257f6b84910SAlexander V. Chernikov 
2258f6b84910SAlexander V. Chernikov 	ifp = ia->ia_ifa.ifa_ifp;
2259f6b84910SAlexander V. Chernikov 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2260f6b84910SAlexander V. Chernikov 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2261*3e7a2321SAlexander V. Chernikov 	saddr = (struct sockaddr *)&addr;
2262*3e7a2321SAlexander V. Chernikov 	smask = (struct sockaddr *)&mask;
2263*3e7a2321SAlexander V. Chernikov 
2264*3e7a2321SAlexander V. Chernikov 	if (all != 0)
2265*3e7a2321SAlexander V. Chernikov 		lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC);
2266*3e7a2321SAlexander V. Chernikov 	else
2267*3e7a2321SAlexander V. Chernikov 		lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr);
2268f6b84910SAlexander V. Chernikov }
2269f6b84910SAlexander V. Chernikov 
2270f6b84910SAlexander V. Chernikov /*
22716e6b3f7cSQing Li  * the callers of this function need to be re-worked to drop
22726e6b3f7cSQing Li  * the lle lock, drop here for now
22736e6b3f7cSQing Li  */
227433841545SHajimu UMEMOTO int
2275bbd8aebaSQing Li nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
227674860d4fSAlexander V. Chernikov     const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
227782cd038dSYoshinobu Inoue {
22786e6b3f7cSQing Li 	struct llentry *ln;
227982cd038dSYoshinobu Inoue 
228074860d4fSAlexander V. Chernikov 	if (pflags != NULL)
228174860d4fSAlexander V. Chernikov 		*pflags = 0;
22826e6b3f7cSQing Li 	IF_AFDATA_UNLOCK_ASSERT(ifp);
2283f31b83e1SNavdeep Parhar 	if (m != NULL && m->m_flags & M_MCAST) {
228482cd038dSYoshinobu Inoue 		switch (ifp->if_type) {
228582cd038dSYoshinobu Inoue 		case IFT_ETHER:
228682cd038dSYoshinobu Inoue 		case IFT_FDDI:
228705b6760dSMunechika SUMIKAWA 		case IFT_L2VLAN:
228833841545SHajimu UMEMOTO 		case IFT_IEEE80211:
228959280079SAndrew Thompson 		case IFT_BRIDGE:
22902049fdeeSMatthew N. Dodd 		case IFT_ISO88025:
229182cd038dSYoshinobu Inoue 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
229282cd038dSYoshinobu Inoue 						 desten);
2293354c3d34SLuigi Rizzo 			return (0);
229482cd038dSYoshinobu Inoue 		default:
2295fef5fd23SBosko Milekic 			m_freem(m);
2296354c3d34SLuigi Rizzo 			return (EAFNOSUPPORT);
229782cd038dSYoshinobu Inoue 		}
229882cd038dSYoshinobu Inoue 	}
229982cd038dSYoshinobu Inoue 
23006e6b3f7cSQing Li 
23016e6b3f7cSQing Li 	/*
23026e6b3f7cSQing Li 	 * the entry should have been created in nd6_store_lladdr
23036e6b3f7cSQing Li 	 */
230473cb2f38SAndrey V. Elsukov 	IF_AFDATA_RLOCK(ifp);
23056e6b3f7cSQing Li 	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
230673cb2f38SAndrey V. Elsukov 	IF_AFDATA_RUNLOCK(ifp);
23076e6b3f7cSQing Li 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
230815209fb6SKip Macy 		if (ln != NULL)
23096e6b3f7cSQing Li 			LLE_RUNLOCK(ln);
2310530f95fcSGleb Smirnoff 		/* this could happen, if we could not allocate memory */
2311530f95fcSGleb Smirnoff 		m_freem(m);
23126e6b3f7cSQing Li 		return (1);
2313530f95fcSGleb Smirnoff 	}
2314530f95fcSGleb Smirnoff 
23156e6b3f7cSQing Li 	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
231674860d4fSAlexander V. Chernikov 	if (pflags != NULL)
231774860d4fSAlexander V. Chernikov 		*pflags = ln->la_flags;
23186e6b3f7cSQing Li 	LLE_RUNLOCK(ln);
23196e6b3f7cSQing Li 	/*
23206e6b3f7cSQing Li 	 * A *small* use after free race exists here
23216e6b3f7cSQing Li 	 */
2322cd46a114SLuigi Rizzo 	return (0);
232382cd038dSYoshinobu Inoue }
232433841545SHajimu UMEMOTO 
232531d4137bSSUZUKI Shinsuke static void
23266e6b3f7cSQing Li clear_llinfo_pqueue(struct llentry *ln)
232731d4137bSSUZUKI Shinsuke {
232831d4137bSSUZUKI Shinsuke 	struct mbuf *m_hold, *m_hold_next;
232931d4137bSSUZUKI Shinsuke 
23306e6b3f7cSQing Li 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
233131d4137bSSUZUKI Shinsuke 		m_hold_next = m_hold->m_nextpkt;
233231d4137bSSUZUKI Shinsuke 		m_freem(m_hold);
233331d4137bSSUZUKI Shinsuke 	}
233431d4137bSSUZUKI Shinsuke 
23356e6b3f7cSQing Li 	ln->la_hold = NULL;
233631d4137bSSUZUKI Shinsuke 	return;
233731d4137bSSUZUKI Shinsuke }
233831d4137bSSUZUKI Shinsuke 
233933841545SHajimu UMEMOTO static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
234033841545SHajimu UMEMOTO static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
234133841545SHajimu UMEMOTO #ifdef SYSCTL_DECL
234233841545SHajimu UMEMOTO SYSCTL_DECL(_net_inet6_icmp6);
234333841545SHajimu UMEMOTO #endif
234433841545SHajimu UMEMOTO SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
234533841545SHajimu UMEMOTO 	CTLFLAG_RD, nd6_sysctl_drlist, "");
234633841545SHajimu UMEMOTO SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
234733841545SHajimu UMEMOTO 	CTLFLAG_RD, nd6_sysctl_prlist, "");
23486df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
23496df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
23506df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
23516df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
235233841545SHajimu UMEMOTO 
235333841545SHajimu UMEMOTO static int
235433841545SHajimu UMEMOTO nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
235533841545SHajimu UMEMOTO {
23567195094cSMarius Strobl 	struct in6_defrouter d;
235733841545SHajimu UMEMOTO 	struct nd_defrouter *dr;
23587195094cSMarius Strobl 	int error;
235933841545SHajimu UMEMOTO 
236033841545SHajimu UMEMOTO 	if (req->newptr)
23617195094cSMarius Strobl 		return (EPERM);
236233841545SHajimu UMEMOTO 
23637195094cSMarius Strobl 	bzero(&d, sizeof(d));
23647195094cSMarius Strobl 	d.rtaddr.sin6_family = AF_INET6;
23657195094cSMarius Strobl 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
23667195094cSMarius Strobl 
23677195094cSMarius Strobl 	/*
23687195094cSMarius Strobl 	 * XXX locking
23697195094cSMarius Strobl 	 */
23703b0b2840SJohn Baldwin 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
23717195094cSMarius Strobl 		d.rtaddr.sin6_addr = dr->rtaddr;
23727195094cSMarius Strobl 		error = sa6_recoverscope(&d.rtaddr);
2373edbb8b46SPeter Wemm 		if (error != 0)
2374b2630c29SGeorge V. Neville-Neil 			return (error);
23757195094cSMarius Strobl 		d.flags = dr->flags;
23767195094cSMarius Strobl 		d.rtlifetime = dr->rtlifetime;
2377ffa0165aSHiroki Sato 		d.expire = dr->expire + (time_second - time_uptime);
23787195094cSMarius Strobl 		d.if_index = dr->ifp->if_index;
23797195094cSMarius Strobl 		error = SYSCTL_OUT(req, &d, sizeof(d));
23807195094cSMarius Strobl 		if (error != 0)
238107eb2995SHajimu UMEMOTO 			return (error);
238233841545SHajimu UMEMOTO 	}
23837195094cSMarius Strobl 	return (0);
23847195094cSMarius Strobl }
238533841545SHajimu UMEMOTO 
238633841545SHajimu UMEMOTO static int
238733841545SHajimu UMEMOTO nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
238833841545SHajimu UMEMOTO {
23897195094cSMarius Strobl 	struct in6_prefix p;
23907195094cSMarius Strobl 	struct sockaddr_in6 s6;
239133841545SHajimu UMEMOTO 	struct nd_prefix *pr;
23927195094cSMarius Strobl 	struct nd_pfxrouter *pfr;
23937195094cSMarius Strobl 	time_t maxexpire;
23947195094cSMarius Strobl 	int error;
23951d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
239633841545SHajimu UMEMOTO 
239733841545SHajimu UMEMOTO 	if (req->newptr)
23987195094cSMarius Strobl 		return (EPERM);
239933841545SHajimu UMEMOTO 
24007195094cSMarius Strobl 	bzero(&p, sizeof(p));
24017195094cSMarius Strobl 	p.origin = PR_ORIG_RA;
24027195094cSMarius Strobl 	bzero(&s6, sizeof(s6));
24037195094cSMarius Strobl 	s6.sin6_family = AF_INET6;
24047195094cSMarius Strobl 	s6.sin6_len = sizeof(s6);
24057195094cSMarius Strobl 
24067195094cSMarius Strobl 	/*
24077195094cSMarius Strobl 	 * XXX locking
24087195094cSMarius Strobl 	 */
24093b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
24107195094cSMarius Strobl 		p.prefix = pr->ndpr_prefix;
24117195094cSMarius Strobl 		if (sa6_recoverscope(&p.prefix)) {
24127195094cSMarius Strobl 			log(LOG_ERR, "scope error in prefix list (%s)\n",
24137195094cSMarius Strobl 			    ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2414a1f7e5f8SHajimu UMEMOTO 			/* XXX: press on... */
2415a1f7e5f8SHajimu UMEMOTO 		}
24167195094cSMarius Strobl 		p.raflags = pr->ndpr_raf;
24177195094cSMarius Strobl 		p.prefixlen = pr->ndpr_plen;
24187195094cSMarius Strobl 		p.vltime = pr->ndpr_vltime;
24197195094cSMarius Strobl 		p.pltime = pr->ndpr_pltime;
24207195094cSMarius Strobl 		p.if_index = pr->ndpr_ifp->if_index;
2421743eee66SSUZUKI Shinsuke 		if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
24227195094cSMarius Strobl 			p.expire = 0;
2423743eee66SSUZUKI Shinsuke 		else {
2424743eee66SSUZUKI Shinsuke 			/* XXX: we assume time_t is signed. */
2425743eee66SSUZUKI Shinsuke 			maxexpire = (-1) &
24267195094cSMarius Strobl 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
24277195094cSMarius Strobl 			if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
24287195094cSMarius Strobl 				p.expire = pr->ndpr_lastupdate +
2429ffa0165aSHiroki Sato 				    pr->ndpr_vltime +
2430ffa0165aSHiroki Sato 				    (time_second - time_uptime);
24317195094cSMarius Strobl 			else
24327195094cSMarius Strobl 				p.expire = maxexpire;
2433743eee66SSUZUKI Shinsuke 		}
24347195094cSMarius Strobl 		p.refcnt = pr->ndpr_refcnt;
24357195094cSMarius Strobl 		p.flags = pr->ndpr_stateflags;
24367195094cSMarius Strobl 		p.advrtrs = 0;
24377195094cSMarius Strobl 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
24387195094cSMarius Strobl 			p.advrtrs++;
24397195094cSMarius Strobl 		error = SYSCTL_OUT(req, &p, sizeof(p));
24407195094cSMarius Strobl 		if (error != 0)
244107eb2995SHajimu UMEMOTO 			return (error);
24427195094cSMarius Strobl 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
24437195094cSMarius Strobl 			s6.sin6_addr = pfr->router->rtaddr;
24447195094cSMarius Strobl 			if (sa6_recoverscope(&s6))
24457195094cSMarius Strobl 				log(LOG_ERR,
24467195094cSMarius Strobl 				    "scope error in prefix list (%s)\n",
24477195094cSMarius Strobl 				    ip6_sprintf(ip6buf, &pfr->router->rtaddr));
24487195094cSMarius Strobl 			error = SYSCTL_OUT(req, &s6, sizeof(s6));
24497195094cSMarius Strobl 			if (error != 0)
24507195094cSMarius Strobl 				return (error);
24517195094cSMarius Strobl 		}
24527195094cSMarius Strobl 	}
24537195094cSMarius Strobl 	return (0);
245433841545SHajimu UMEMOTO }
2455