xref: /freebsd/usr.sbin/rtadvd/rtadvd.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*	$KAME: rtadvd.h,v 1.26 2003/08/05 12:34:23 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1998 WIDE Project.
7  * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #define	ELM_MALLOC(p,error_action)					\
36 	do {								\
37 		p = malloc(sizeof(*p));					\
38 		if (p == NULL) {					\
39 			syslog(LOG_ERR, "<%s> malloc failed: %s",	\
40 			    __func__, strerror(errno));			\
41 			error_action;					\
42 		}							\
43 		memset(p, 0, sizeof(*p));				\
44 	} while(0)
45 
46 #define IN6ADDR_LINKLOCAL_ALLNODES_INIT				\
47 	{{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
48 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}
49 
50 #define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT			\
51 	{{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
52 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}}
53 
54 #define IN6ADDR_SITELOCAL_ALLROUTERS_INIT			\
55 	{{{ 0xff, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
56 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}}
57 
58 extern struct sockaddr_in6 sin6_linklocal_allnodes;
59 extern struct sockaddr_in6 sin6_linklocal_allrouters;
60 extern struct sockaddr_in6 sin6_sitelocal_allrouters;
61 
62 /*
63  * RFC 3542 API deprecates IPV6_PKTINFO in favor of
64  * IPV6_RECVPKTINFO
65  */
66 #ifndef IPV6_RECVPKTINFO
67 #ifdef IPV6_PKTINFO
68 #define IPV6_RECVPKTINFO	IPV6_PKTINFO
69 #endif
70 #endif
71 
72 /*
73  * RFC 3542 API deprecates IPV6_HOPLIMIT in favor of
74  * IPV6_RECVHOPLIMIT
75  */
76 #ifndef IPV6_RECVHOPLIMIT
77 #ifdef IPV6_HOPLIMIT
78 #define IPV6_RECVHOPLIMIT	IPV6_HOPLIMIT
79 #endif
80 #endif
81 
82 /* protocol constants and default values */
83 #define DEF_MAXRTRADVINTERVAL 600
84 #define DEF_ADVLINKMTU 0
85 #define DEF_ADVREACHABLETIME 0
86 #define DEF_ADVRETRANSTIMER 0
87 #define DEF_ADVCURHOPLIMIT 64
88 #define DEF_ADVVALIDLIFETIME 2592000
89 #define DEF_ADVPREFERREDLIFETIME 604800
90 
91 #define MAXROUTERLIFETIME 9000
92 #define MIN_MAXINTERVAL 4
93 #define MAX_MAXINTERVAL 1800
94 #define MIN_MININTERVAL 3
95 #define MAXREACHABLETIME 3600000
96 
97 #define MAX_INITIAL_RTR_ADVERT_INTERVAL  16
98 #define MAX_INITIAL_RTR_ADVERTISEMENTS    3
99 #define MAX_FINAL_RTR_ADVERTISEMENTS      3
100 #define MIN_DELAY_BETWEEN_RAS             3
101 #define MAX_RA_DELAY_TIME                 500000 /* usec */
102 
103 #define PREFIX_FROM_KERNEL 1
104 #define PREFIX_FROM_CONFIG 2
105 #define PREFIX_FROM_DYNAMIC 3
106 
107 struct prefix {
108 	TAILQ_ENTRY(prefix)	pfx_next;
109 
110 	struct rainfo *pfx_rainfo;	/* back pointer to the interface */
111 	/*
112 	 * Expiration timer.  This is used when a prefix derived from
113 	 * the kernel is deleted.
114 	 */
115 	struct rtadvd_timer *pfx_timer;
116 
117 	uint32_t	pfx_validlifetime;	/* AdvValidLifetime */
118 	uint32_t       	pfx_vltimeexpire;	/* Expiration of vltime */
119 	uint32_t	pfx_preflifetime;	/* AdvPreferredLifetime */
120 	uint32_t	pfx_pltimeexpire;	/* Expiration of pltime */
121 	int		pfx_onlinkflg;		/* bool: AdvOnLinkFlag */
122 	int		pfx_autoconfflg;	/* bool: AdvAutonomousFlag */
123 	int		pfx_prefixlen;
124 	int		pfx_origin;		/* From kernel or config */
125 
126 	struct in6_addr	pfx_prefix;
127 };
128 
129 struct rtinfo {
130 	TAILQ_ENTRY(rtinfo)	rti_next;
131 
132 	uint32_t	rti_ltime;	/* route lifetime */
133 	int		rti_rtpref;	/* route preference */
134 	int		rti_prefixlen;
135 	struct in6_addr	rti_prefix;
136 };
137 
138 struct rdnss_addr {
139 	TAILQ_ENTRY(rdnss_addr)	ra_next;
140 
141 	struct in6_addr ra_dns;	/* DNS server entry */
142 };
143 
144 struct rdnss {
145 	TAILQ_ENTRY(rdnss) rd_next;
146 
147 	TAILQ_HEAD(, rdnss_addr) rd_list;	/* list of DNS servers */
148 	uint32_t rd_ltime;	/* number of seconds valid */
149 };
150 
151 /*
152  * The maximum length of a domain name in a DNS search list is calculated
153  * by a domain name + length fields per 63 octets + a zero octet at
154  * the tail and adding 8 octet boundary padding.
155  */
156 #define _DNAME_LABELENC_MAXLEN \
157 	(NI_MAXHOST + (NI_MAXHOST / 64 + 1) + 1)
158 
159 #define DNAME_LABELENC_MAXLEN \
160 	(_DNAME_LABELENC_MAXLEN + 8 - _DNAME_LABELENC_MAXLEN % 8)
161 
162 struct dnssl_addr {
163 	TAILQ_ENTRY(dnssl_addr)	da_next;
164 
165 	int da_len;				/* length of entry */
166 	char da_dom[DNAME_LABELENC_MAXLEN];	/* search domain name entry */
167 };
168 
169 struct dnssl {
170 	TAILQ_ENTRY(dnssl)	dn_next;
171 
172 	TAILQ_HEAD(, dnssl_addr) dn_list;	/* list of search domains */
173 	uint32_t dn_ltime;			/* number of seconds valid */
174 };
175 
176 struct soliciter {
177 	TAILQ_ENTRY(soliciter)	sol_next;
178 
179 	struct sockaddr_in6	sol_addr;
180 };
181 
182 struct	rainfo {
183 	/* pointer for list */
184 	TAILQ_ENTRY(rainfo)	rai_next;
185 
186 	/* interface information */
187 	struct ifinfo *rai_ifinfo;
188 
189 	int	rai_advlinkopt;		/* bool: whether include link-layer addr opt */
190 	int	rai_advifprefix;	/* bool: gather IF prefixes? */
191 
192 	/* Router configuration variables */
193 	uint16_t	rai_lifetime;		/* AdvDefaultLifetime */
194 	uint16_t	rai_maxinterval;	/* MaxRtrAdvInterval */
195 	uint16_t	rai_mininterval;	/* MinRtrAdvInterval */
196 	int 	rai_managedflg;		/* AdvManagedFlag */
197 	int	rai_otherflg;		/* AdvOtherConfigFlag */
198 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
199 	int	rai_ipv6onlyflg;	/* AdvIPv6OnlyFlag */
200 #endif
201 
202 	int	rai_rtpref;		/* router preference */
203 	uint32_t	rai_linkmtu;		/* AdvLinkMTU */
204 	uint32_t	rai_reachabletime;	/* AdvReachableTime */
205 	uint32_t	rai_retranstimer;	/* AdvRetransTimer */
206 	uint8_t	rai_hoplimit;		/* AdvCurHopLimit */
207 
208 	TAILQ_HEAD(, prefix) rai_prefix;/* AdvPrefixList(link head) */
209 	int	rai_pfxs;		/* number of prefixes */
210 
211 	uint16_t	rai_clockskew;	/* used for consisitency check of lifetimes */
212 
213 	TAILQ_HEAD(, rdnss) rai_rdnss;	/* DNS server list */
214 	TAILQ_HEAD(, dnssl) rai_dnssl;	/* search domain list */
215 	TAILQ_HEAD(, rtinfo) rai_route;	/* route information option (link head) */
216 	int	rai_routes;		/* number of route information options */
217 	/* actual RA packet data and its length */
218 	size_t	rai_ra_datalen;
219 	char	*rai_ra_data;
220 
221 	/* info about soliciter */
222 	TAILQ_HEAD(, soliciter) rai_soliciter;	/* recent solication source */
223 };
224 
225 /* RA information list */
226 extern TAILQ_HEAD(railist_head_t, rainfo) railist;
227 
228 /*
229  * ifi_state:
230  *
231  *           (INIT)
232  *              |
233  *              | update_ifinfo()
234  *              | update_persist_ifinfo()
235  *              v
236  *         UNCONFIGURED
237  *               |  ^
238  *   loadconfig()|  |rm_ifinfo(), ra_output()
239  *      (MC join)|  |(MC leave)
240  *               |  |
241  *               |  |
242  *               v  |
243  *         TRANSITIVE
244  *               |  ^
245  *    ra_output()|  |getconfig()
246  *               |  |
247  *               |  |
248  *               |  |
249  *               v  |
250  *         CONFIGURED
251  *
252  *
253  */
254 #define	IFI_STATE_UNCONFIGURED	0
255 #define	IFI_STATE_CONFIGURED	1
256 #define	IFI_STATE_TRANSITIVE	2
257 
258 struct	ifinfo {
259 	TAILQ_ENTRY(ifinfo)	ifi_next;
260 
261 	uint16_t	ifi_state;
262 	uint16_t	ifi_persist;
263 	uint16_t	ifi_ifindex;
264 	char	ifi_ifname[IFNAMSIZ];
265 	uint8_t	ifi_type;
266 	uint16_t	ifi_flags;
267 	uint32_t	ifi_nd_flags;
268 	uint32_t	ifi_phymtu;
269 	struct sockaddr_dl	ifi_sdl;
270 
271 	struct rainfo	*ifi_rainfo;
272 	struct rainfo	*ifi_rainfo_trans;
273 	uint16_t	ifi_burstcount;
274 	uint32_t	ifi_burstinterval;
275 	struct rtadvd_timer	*ifi_ra_timer;
276 	/* timestamp when the latest RA was sent */
277 	struct timespec		ifi_ra_lastsent;
278 	uint16_t	ifi_rs_waitcount;
279 
280 	/* statistics */
281 	uint64_t ifi_raoutput;		/* # of RAs sent */
282 	uint64_t ifi_rainput;		/* # of RAs received */
283 	uint64_t ifi_rainconsistent;	/* # of inconsistent recv'd RAs  */
284 	uint64_t ifi_rsinput;		/* # of RSs received */
285 };
286 
287 /* Interface list */
288 extern TAILQ_HEAD(ifilist_head_t, ifinfo) ifilist;
289 
290 extern char *mcastif;
291 
292 struct rtadvd_timer	*ra_timeout(void *);
293 void			ra_timer_update(void *, struct timespec *);
294 void			ra_output(struct ifinfo *);
295 
296 int			prefix_match(struct in6_addr *, int,
297 			    struct in6_addr *, int);
298 struct ifinfo		*if_indextoifinfo(int);
299 struct prefix		*find_prefix(struct rainfo *,
300 			    struct in6_addr *, int);
301 void			rtadvd_set_reload(int);
302 void			rtadvd_set_shutdown(int);
303