xref: /openbsd-src/sys/netinet6/nd6.h (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: nd6.h,v 1.76 2020/03/28 16:15:45 florian 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_IS_LLINFO_PROBREACH(n) ((n)->ln_state > ND6_LLINFO_INCOMPLETE)
113 #define ND6_LLINFO_PERMANENT(n)	((n)->ln_rt->rt_expire == 0)
114 
115 /* node constants */
116 #define MAX_REACHABLE_TIME		3600000	/* msec */
117 #define REACHABLE_TIME			30000	/* msec */
118 #define RETRANS_TIMER			1000	/* msec */
119 #define MIN_RANDOM_FACTOR		512	/* 1024 * 0.5 */
120 #define MAX_RANDOM_FACTOR		1536	/* 1024 * 1.5 */
121 #define ND_COMPUTE_RTIME(x) \
122 		(((MIN_RANDOM_FACTOR * (x >> 10)) + (arc4random() & \
123 		((MAX_RANDOM_FACTOR - MIN_RANDOM_FACTOR) * (x >> 10)))) /1000)
124 
125 extern int nd6_delay;
126 extern int nd6_umaxtries;
127 extern int nd6_mmaxtries;
128 extern int nd6_maxnudhint;
129 extern int nd6_gctimer;
130 extern int nd6_debug;
131 
132 #define nd6log(x)	do { if (nd6_debug) log x; } while (0)
133 
134 union nd_opts {
135 	struct nd_opt_hdr *nd_opt_array[9];
136 	struct {
137 		struct nd_opt_hdr *zero;
138 		struct nd_opt_hdr *src_lladdr;
139 		struct nd_opt_hdr *tgt_lladdr;
140 		struct nd_opt_prefix_info *pi_beg; /* multiple opts, start */
141 		struct nd_opt_rd_hdr *rh;
142 		struct nd_opt_mtu *mtu;
143 		struct nd_opt_hdr *search;	/* multiple opts */
144 		struct nd_opt_hdr *last;	/* multiple opts */
145 		int done;
146 		struct nd_opt_prefix_info *pi_end;/* multiple opts, end */
147 	} nd_opt_each;
148 };
149 #define nd_opts_src_lladdr	nd_opt_each.src_lladdr
150 #define nd_opts_tgt_lladdr	nd_opt_each.tgt_lladdr
151 #define nd_opts_pi		nd_opt_each.pi_beg
152 #define nd_opts_pi_end		nd_opt_each.pi_end
153 #define nd_opts_rh		nd_opt_each.rh
154 #define nd_opts_mtu		nd_opt_each.mtu
155 #define nd_opts_search		nd_opt_each.search
156 #define nd_opts_last		nd_opt_each.last
157 #define nd_opts_done		nd_opt_each.done
158 
159 void nd6_init(void);
160 struct nd_ifinfo *nd6_ifattach(struct ifnet *);
161 void nd6_ifdetach(struct nd_ifinfo *);
162 int nd6_is_addr_neighbor(struct sockaddr_in6 *, struct ifnet *);
163 void nd6_option_init(void *, int, union nd_opts *);
164 struct nd_opt_hdr *nd6_option(union nd_opts *);
165 int nd6_options(union nd_opts *);
166 struct	rtentry *nd6_lookup(struct in6_addr *, int, struct ifnet *, u_int);
167 void nd6_setmtu(struct ifnet *);
168 void nd6_llinfo_settimer(struct llinfo_nd6 *, unsigned int);
169 void nd6_purge(struct ifnet *);
170 void nd6_nud_hint(struct rtentry *);
171 void nd6_rtrequest(struct ifnet *, int, struct rtentry *);
172 int nd6_ioctl(u_long, caddr_t, struct ifnet *);
173 void nd6_cache_lladdr(struct ifnet *, struct in6_addr *, char *, int, int, int);
174 int nd6_resolve(struct ifnet *, struct rtentry *, struct mbuf *,
175 	 struct sockaddr *, u_char *);
176 int nd6_need_cache(struct ifnet *);
177 
178 void nd6_na_input(struct mbuf *, int, int);
179 void nd6_na_output(struct ifnet *, struct in6_addr *,
180 	struct in6_addr *, u_long, int, struct sockaddr *);
181 void nd6_ns_input(struct mbuf *, int, int);
182 void nd6_ns_output(struct ifnet *, struct in6_addr *,
183 	struct in6_addr *, struct llinfo_nd6 *, int);
184 caddr_t nd6_ifptomac(struct ifnet *);
185 void nd6_dad_start(struct ifaddr *);
186 void nd6_dad_stop(struct ifaddr *);
187 
188 void nd6_rtr_cache(struct mbuf *, int, int, int);
189 
190 int in6_ifdel(struct ifnet *, struct in6_addr *);
191 int rt6_flush(struct in6_addr *, struct ifnet *);
192 
193 void nd6_expire_timer_update(struct in6_ifaddr *);
194 #endif /* _KERNEL */
195 
196 #endif /* _NETINET6_ND6_H_ */
197