xref: /openbsd-src/sys/netinet6/nd6.h (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: nd6.h,v 1.77 2022/07/22 15:34:46 kn 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 struct nd_ifinfo {
47 	u_int32_t basereachable;	/* BaseReachableTime */
48 	u_int32_t reachable;		/* Reachable Time */
49 	u_int32_t retrans;		/* Retrans Timer */
50 	int recalctm;			/* BaseReacable re-calculation timer */
51 	u_int8_t initialized; /* Flag to see the entry is initialized */
52 };
53 
54 struct in6_nbrinfo {
55 	char ifname[IFNAMSIZ];	/* if name, e.g. "en0" */
56 	struct in6_addr addr;	/* IPv6 address of the neighbor */
57 	time_t	expire;		/* lifetime for NDP state transition */
58 	long	asked;		/* number of queries already sent for addr */
59 	int	isrouter;	/* if it acts as a router */
60 	int	state;		/* reachability state */
61 };
62 
63 struct prf_ra {
64 	u_int onlink : 1;
65 	u_int autonomous : 1;
66 	u_int router : 1;
67 	u_int reserved : 5;
68 };
69 
70 struct	in6_ndireq {
71 	char ifname[IFNAMSIZ];
72 	struct nd_ifinfo ndi;
73 };
74 
75 struct	in6_ndifreq {
76 	char ifname[IFNAMSIZ];
77 	u_long ifindex;
78 };
79 
80 /* Prefix status */
81 #define NDPRF_ONLINK		0x1
82 #define NDPRF_DETACHED		0x2
83 #define NDPRF_HOME		0x4
84 
85 /* protocol constants */
86 #define MAX_RTR_SOLICITATION_DELAY	1	/*1sec*/
87 #define RTR_SOLICITATION_INTERVAL	4	/*4sec*/
88 #define MAX_RTR_SOLICITATIONS		3
89 
90 #define ND6_INFINITE_LIFETIME		0xffffffff
91 
92 #ifdef _KERNEL
93 
94 #include <sys/queue.h>
95 
96 #define ND_IFINFO(ifp) \
97 	(((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->nd_ifinfo)
98 
99 #define RS_LHCOOKIE(ifp) \
100 	((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->rs_lhcookie
101 
102 struct	llinfo_nd6 {
103 	TAILQ_ENTRY(llinfo_nd6)	ln_list;
104 	struct	rtentry *ln_rt;
105 	struct	mbuf *ln_hold;	/* last packet until resolved/timeout */
106 	long	ln_asked;	/* number of queries already sent for addr */
107 	int	ln_byhint;	/* # of times we made it reachable by UL hint */
108 	short	ln_state;	/* reachability state */
109 	short	ln_router;	/* 2^0: ND6 router bit */
110 };
111 
112 #define ND6_LLINFO_PERMANENT(n)	((n)->ln_rt->rt_expire == 0)
113 
114 /* node constants */
115 #define REACHABLE_TIME			30000	/* msec */
116 #define RETRANS_TIMER			1000	/* msec */
117 #define MIN_RANDOM_FACTOR		512	/* 1024 * 0.5 */
118 #define MAX_RANDOM_FACTOR		1536	/* 1024 * 1.5 */
119 #define ND_COMPUTE_RTIME(x) \
120 		(((MIN_RANDOM_FACTOR * (x >> 10)) + (arc4random() & \
121 		((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000)
122 
123 extern int nd6_delay;
124 extern int nd6_umaxtries;
125 extern int nd6_mmaxtries;
126 extern int nd6_maxnudhint;
127 extern int nd6_gctimer;
128 extern int nd6_debug;
129 
130 #define nd6log(x)	do { if (nd6_debug) log x; } while (0)
131 
132 union nd_opts {
133 	struct nd_opt_hdr *nd_opt_array[9];
134 	struct {
135 		struct nd_opt_hdr *zero;
136 		struct nd_opt_hdr *src_lladdr;
137 		struct nd_opt_hdr *tgt_lladdr;
138 		struct nd_opt_prefix_info *pi_beg; /* multiple opts, start */
139 		struct nd_opt_rd_hdr *rh;
140 		struct nd_opt_mtu *mtu;
141 		struct nd_opt_hdr *search;	/* multiple opts */
142 		struct nd_opt_hdr *last;	/* multiple opts */
143 		int done;
144 		struct nd_opt_prefix_info *pi_end;/* multiple opts, end */
145 	} nd_opt_each;
146 };
147 #define nd_opts_src_lladdr	nd_opt_each.src_lladdr
148 #define nd_opts_tgt_lladdr	nd_opt_each.tgt_lladdr
149 #define nd_opts_pi		nd_opt_each.pi_beg
150 #define nd_opts_pi_end		nd_opt_each.pi_end
151 #define nd_opts_rh		nd_opt_each.rh
152 #define nd_opts_mtu		nd_opt_each.mtu
153 #define nd_opts_search		nd_opt_each.search
154 #define nd_opts_last		nd_opt_each.last
155 #define nd_opts_done		nd_opt_each.done
156 
157 void nd6_init(void);
158 struct nd_ifinfo *nd6_ifattach(struct ifnet *);
159 void nd6_ifdetach(struct nd_ifinfo *);
160 int nd6_is_addr_neighbor(struct sockaddr_in6 *, struct ifnet *);
161 void nd6_option_init(void *, int, union nd_opts *);
162 struct nd_opt_hdr *nd6_option(union nd_opts *);
163 int nd6_options(union nd_opts *);
164 struct	rtentry *nd6_lookup(struct in6_addr *, int, struct ifnet *, u_int);
165 void nd6_setmtu(struct ifnet *);
166 void nd6_llinfo_settimer(struct llinfo_nd6 *, unsigned int);
167 void nd6_purge(struct ifnet *);
168 void nd6_nud_hint(struct rtentry *);
169 void nd6_rtrequest(struct ifnet *, int, struct rtentry *);
170 int nd6_ioctl(u_long, caddr_t, struct ifnet *);
171 void nd6_cache_lladdr(struct ifnet *, struct in6_addr *, char *, int, int, int);
172 int nd6_resolve(struct ifnet *, struct rtentry *, struct mbuf *,
173 	 struct sockaddr *, u_char *);
174 int nd6_need_cache(struct ifnet *);
175 
176 void nd6_na_input(struct mbuf *, int, int);
177 void nd6_na_output(struct ifnet *, struct in6_addr *,
178 	struct in6_addr *, u_long, int, struct sockaddr *);
179 void nd6_ns_input(struct mbuf *, int, int);
180 void nd6_ns_output(struct ifnet *, struct in6_addr *,
181 	struct in6_addr *, struct llinfo_nd6 *, int);
182 caddr_t nd6_ifptomac(struct ifnet *);
183 void nd6_dad_start(struct ifaddr *);
184 void nd6_dad_stop(struct ifaddr *);
185 
186 void nd6_rtr_cache(struct mbuf *, int, int, int);
187 
188 int in6_ifdel(struct ifnet *, struct in6_addr *);
189 int rt6_flush(struct in6_addr *, struct ifnet *);
190 
191 void nd6_expire_timer_update(struct in6_ifaddr *);
192 #endif /* _KERNEL */
193 
194 #endif /* _NETINET6_ND6_H_ */
195