xref: /openbsd/sys/netinet6/nd6.h (revision e298277c)
1 /*	$OpenBSD: nd6.h,v 1.99 2023/05/04 06:56:56 bluhm Exp $	*/
2 /*	$KAME: nd6.h,v 1.95 2002/06/08 11:31:06 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef _NETINET6_ND6_H_
34 #define _NETINET6_ND6_H_
35 
36 #include <sys/task.h>
37 
38 #define ND6_LLINFO_PURGE	-3
39 #define ND6_LLINFO_NOSTATE	-2
40 #define ND6_LLINFO_INCOMPLETE	0
41 #define ND6_LLINFO_REACHABLE	1
42 #define ND6_LLINFO_STALE	2
43 #define ND6_LLINFO_DELAY	3
44 #define ND6_LLINFO_PROBE	4
45 
46 /*
47  * Locks used to protect struct members in this file:
48  *	I	immutable after creation
49  *	K	kernel lock
50  *	m	nd6 mutex, needed when net lock is shared
51  *	N	net lock
52  */
53 
54 struct nd_ifinfo {
55 	u_int32_t reachable;		/* [N] Reachable Time */
56 	int recalctm;			/* [N] BaseReachable recalc timer */
57 };
58 
59 struct in6_nbrinfo {
60 	char ifname[IFNAMSIZ];	/* if name, e.g. "en0" */
61 	struct in6_addr addr;	/* IPv6 address of the neighbor */
62 	time_t	expire;		/* lifetime for NDP state transition */
63 	long	asked;		/* number of queries already sent for addr */
64 	int	isrouter;	/* if it acts as a router */
65 	int	state;		/* reachability state */
66 };
67 
68 struct	in6_ndireq {
69 	char ifname[IFNAMSIZ];
70 	struct nd_ifinfo ndi;
71 };
72 
73 /* protocol constants */
74 #define MAX_RTR_SOLICITATION_DELAY	1	/*1sec*/
75 #define RTR_SOLICITATION_INTERVAL	4	/*4sec*/
76 #define MAX_RTR_SOLICITATIONS		3
77 
78 #define ND6_INFINITE_LIFETIME		0xffffffff
79 
80 #ifdef _KERNEL
81 
82 #include <sys/queue.h>
83 
84 struct	llinfo_nd6 {
85 	TAILQ_ENTRY(llinfo_nd6)	 ln_list;	/* [mN] global nd6_list */
86 	struct	rtentry		*ln_rt;		/* [I] backpointer to rtentry */
87 	struct	mbuf_queue ln_mq;	/* hold packets until resolved */
88 	struct	in6_addr ln_saddr6;	/* source of prompting packet */
89 	long	ln_asked;	/* number of queries already sent for addr */
90 	int	ln_byhint;	/* # of times we made it reachable by UL hint */
91 	short	ln_state;	/* reachability state */
92 	short	ln_router;	/* 2^0: ND6 router bit */
93 };
94 #define LN_HOLD_QUEUE 10
95 #define LN_HOLD_TOTAL 100
96 
97 extern unsigned int ln_hold_total;
98 
99 #define ND6_LLINFO_PERMANENT(n)	((n)->ln_rt->rt_expire == 0)
100 
101 /* node constants */
102 #define REACHABLE_TIME			30000	/* msec */
103 #define RETRANS_TIMER			1000	/* msec */
104 #define MIN_RANDOM_FACTOR		512	/* 1024 * 0.5 */
105 #define MAX_RANDOM_FACTOR		1536	/* 1024 * 1.5 */
106 #define ND_COMPUTE_RTIME(x) \
107 		(((MIN_RANDOM_FACTOR * (x >> 10)) + (arc4random() & \
108 		((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000)
109 
110 extern int nd6_delay;
111 extern int nd6_umaxtries;
112 extern int nd6_mmaxtries;
113 extern int nd6_maxnudhint;
114 extern int nd6_gctimer;
115 extern int nd6_debug;
116 
117 #define nd6log(x)	do { if (nd6_debug) log x; } while (0)
118 
119 struct nd_opts {
120 	struct nd_opt_hdr *nd_opts_src_lladdr;
121 	struct nd_opt_hdr *nd_opts_tgt_lladdr;
122 };
123 
124 void nd6_init(void);
125 void nd6_ifattach(struct ifnet *);
126 void nd6_ifdetach(struct ifnet *);
127 int nd6_is_addr_neighbor(const struct sockaddr_in6 *, struct ifnet *);
128 int nd6_options(void *, int, struct nd_opts *);
129 struct	rtentry *nd6_lookup(const struct in6_addr *, int, struct ifnet *,
130     u_int);
131 void nd6_llinfo_settimer(const struct llinfo_nd6 *, unsigned int);
132 void nd6_purge(struct ifnet *);
133 void nd6_nud_hint(struct rtentry *);
134 void nd6_rtrequest(struct ifnet *, int, struct rtentry *);
135 int nd6_ioctl(u_long, caddr_t, struct ifnet *);
136 void nd6_cache_lladdr(struct ifnet *, const struct in6_addr *, char *,
137     int, int, int);
138 int nd6_resolve(struct ifnet *, struct rtentry *, struct mbuf *,
139 	 struct sockaddr *, u_char *);
140 int nd6_need_cache(struct ifnet *);
141 
142 void nd6_na_input(struct mbuf *, int, int);
143 void nd6_na_output(struct ifnet *, const struct in6_addr *,
144 	const struct in6_addr *, u_long, int, struct sockaddr *);
145 void nd6_ns_input(struct mbuf *, int, int);
146 void nd6_ns_output(struct ifnet *, const struct in6_addr *,
147 	const struct in6_addr *, const struct in6_addr *, int);
148 caddr_t nd6_ifptomac(struct ifnet *);
149 void nd6_dad_start(struct ifaddr *);
150 void nd6_dad_stop(struct ifaddr *);
151 
152 void nd6_rtr_cache(struct mbuf *, int, int, int);
153 
154 int rt6_flush(struct in6_addr *, struct ifnet *);
155 
156 void nd6_expire_timer_update(struct in6_ifaddr *);
157 #endif /* _KERNEL */
158 
159 #endif /* _NETINET6_ND6_H_ */
160