1 /*	$NetBSD: failover.h,v 1.3 2022/04/03 01:10:58 christos Exp $	*/
2 
3 /* failover.h
4 
5    Definitions for address trees... */
6 
7 /*
8  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 2000-2003 by Internet Software Consortium
10  *
11  * This Source Code Form is subject to the terms of the Mozilla Public
12  * License, v. 2.0. If a copy of the MPL was not distributed with this
13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  *   Internet Systems Consortium, Inc.
24  *   PO Box 360
25  *   Newmarket, NH 03857 USA
26  *   <info@isc.org>
27  *   https://www.isc.org/
28  *
29  */
30 
31 #if defined (FAILOVER_PROTOCOL)
32 struct failover_option_info {
33 	int code;
34 	const char *name;
35 	enum { FT_UINT8, FT_IPADDR, FT_UINT32, FT_BYTES, FT_TEXT_OR_BYTES,
36 	       FT_DDNS, FT_DDNS1, FT_UINT16, FT_TEXT,
37 	       FT_UNDEF, FT_DIGEST } type;
38 	int num_present;
39 	int offset;
40 	u_int32_t bit;
41 };
42 
43 typedef struct {
44 	unsigned count;
45 	u_int8_t *data;
46 } failover_option_t;
47 
48 /* Failover configuration defaults. */
49 #ifndef  DEFAULT_MAX_BALANCE_TIME
50 # define DEFAULT_MAX_BALANCE_TIME	3600
51 #endif
52 
53 #ifndef  DEFAULT_MIN_BALANCE_TIME
54 # define DEFAULT_MIN_BALANCE_TIME	60
55 #endif
56 
57 #ifndef  DEFAULT_MAX_LEASE_MISBALANCE
58 # define DEFAULT_MAX_LEASE_MISBALANCE   15
59 #endif
60 
61 #ifndef  DEFAULT_MAX_LEASE_OWNERSHIP
62 # define DEFAULT_MAX_LEASE_OWNERSHIP    10
63 #endif
64 
65 #ifndef  DEFAULT_MAX_FLYING_UPDATES
66 # define DEFAULT_MAX_FLYING_UPDATES	100
67 #endif
68 
69 #ifndef  DEFAULT_MAX_RESPONSE_DELAY
70 # define DEFAULT_MAX_RESPONSE_DELAY	20
71 #endif
72 
73 /*
74  * IANA has assigned ports 647 ("dhcp-failover") and 847 ("dhcp-failover2").
75  * Of these, only port 647 is mentioned in the -12 draft revision.  We're not
76  * sure if they are supposed to indicate primary and secondary?  No matter,
77  * we'll stick to the -12 draft revision level.
78  */
79 #ifndef  DEFAULT_FAILOVER_PORT
80 # define DEFAULT_FAILOVER_PORT		647
81 #endif
82 
83 #define FM_OFFSET(x) (long)(&(((failover_message_t *)0) -> x))
84 
85 /* All of the below definitions are mandated by draft-ietf-dhc-failover-12.
86  * The Sections referenced are Sections within that document of that
87  * version, and may be different in other documents of other versions.
88  */
89 
90 /* Failover message options from Section 12: */
91 #define FTO_ADDRESSES_TRANSFERRED	1
92 #define FTB_ADDRESSES_TRANSFERRED		0x00000002
93 #define FTO_ASSIGNED_IP_ADDRESS		2
94 #define FTB_ASSIGNED_IP_ADDRESS			0x00000004
95 #define FTO_BINDING_STATUS		3
96 #define FTB_BINDING_STATUS			0x00000008
97 #define FTO_CLIENT_IDENTIFIER		4
98 #define FTB_CLIENT_IDENTIFIER			0x00000010
99 #define FTO_CHADDR			5
100 #define FTB_CHADDR				0x00000020
101 #define FTO_CLTT			6
102 #define FTB_CLTT				0x00000040
103 #define FTO_REPLY_OPTIONS		7
104 #define FTB_REPLY_OPTIONS			0x00000080
105 #define FTO_REQUEST_OPTIONS		8
106 #define FTB_REQUEST_OPTIONS			0x00000100
107 #define FTO_DDNS			9
108 #define FTB_DDNS				0x00000200
109 #define FTO_DELAYED_SERVICE		10
110 #define FTB_DELAYED_SERVICE			0x00000400
111 #define FTO_HBA				11
112 #define FTB_HBA					0x00000800
113 #define FTO_IP_FLAGS			12
114 #define FTB_IP_FLAGS				0x00001000
115 #define FTO_LEASE_EXPIRY		13
116 #define FTB_LEASE_EXPIRY			0x00002000
117 #define FTO_MAX_UNACKED			14
118 #define FTB_MAX_UNACKED				0x00004000
119 #define FTO_MCLT			15
120 #define FTB_MCLT				0x00008000
121 #define FTO_MESSAGE			16
122 #define FTB_MESSAGE				0x00010000
123 #define FTO_MESSAGE_DIGEST		17
124 #define FTB_MESSAGE_DIGEST			0x00020000
125 #define FTO_POTENTIAL_EXPIRY		18
126 #define FTB_POTENTIAL_EXPIRY			0x00040000
127 #define FTO_RECEIVE_TIMER		19
128 #define FTB_RECEIVE_TIMER			0x00080000
129 #define FTO_PROTOCOL_VERSION		20
130 #define FTB_PROTOCOL_VERSION			0x00100000
131 #define FTO_REJECT_REASON		21
132 #define FTB_REJECT_REASON			0x00200000
133 #define FTO_RELATIONSHIP_NAME		22
134 #define FTB_RELATIONSHIP_NAME			0x00400000
135 #define FTO_SERVER_FLAGS		23
136 #define FTB_SERVER_FLAGS			0x00800000
137 #define FTO_SERVER_STATE		24
138 #define FTB_SERVER_STATE			0x01000000
139 #define FTO_STOS			25
140 #define FTB_STOS				0x02000000
141 #define FTO_TLS_REPLY			26
142 #define FTB_TLS_REPLY				0x04000000
143 #define FTO_TLS_REQUEST			27
144 #define FTB_TLS_REQUEST				0x08000000
145 #define FTO_VENDOR_CLASS		28
146 #define FTB_VENDOR_CLASS			0x10000000
147 #define FTO_VENDOR_OPTIONS		29
148 #define FTB_VENDOR_OPTIONS			0x20000000
149 
150 #define FTO_MAX				FTO_VENDOR_OPTIONS
151 
152 /* Failover protocol message types from Section 6.1: */
153 #define FTM_POOLREQ		1
154 #define FTM_POOLRESP		2
155 #define FTM_BNDUPD		3
156 #define FTM_BNDACK		4
157 #define FTM_CONNECT		5
158 #define FTM_CONNECTACK		6
159 #define FTM_UPDREQALL		7
160 #define FTM_UPDDONE		8
161 #define FTM_UPDREQ		9
162 #define FTM_STATE		10
163 #define FTM_CONTACT		11
164 #define FTM_DISCONNECT		12
165 
166 #define FTM_MAX			FTM_DISCONNECT
167 
168 /* Reject reasons from Section 12.21: */
169 #define FTR_ILLEGAL_IP_ADDR	1
170 #define FTR_FATAL_CONFLICT	2
171 #define FTR_MISSING_BINDINFO	3
172 #define FTR_TIMEMISMATCH	4
173 #define FTR_INVALID_MCLT	5
174 #define FTR_MISC_REJECT		6
175 #define FTR_DUP_CONNECTION	7
176 #define FTR_INVALID_PARTNER	8
177 #define FTR_TLS_UNSUPPORTED	9
178 #define FTR_TLS_UNCONFIGURED	10
179 #define FTR_TLS_REQUIRED	11
180 #define FTR_DIGEST_UNSUPPORTED	12
181 #define FTR_DIGEST_UNCONFIGURED	13
182 #define FTR_VERSION_MISMATCH	14
183 #define FTR_OUTDATED_BIND_INFO	15
184 #define FTR_LESS_CRIT_BIND_INFO	16
185 #define FTR_NO_TRAFFIC		17
186 #define FTR_HBA_CONFLICT	18
187 #define FTR_IP_NOT_RESERVED	19
188 #define FTR_IP_DIGEST_FAILURE	20
189 #define FTR_IP_MISSING_DIGEST	21
190 #define FTR_UNKNOWN		254
191 
192 /* Message size limitations defined in Section 6.1: */
193 #define DHCP_FAILOVER_MIN_MESSAGE_SIZE    12
194 #define DHCP_FAILOVER_MAX_MESSAGE_SIZE	2048
195 
196 /* Failover server flags from Section 12.23: */
197 #define FTF_SERVER_STARTUP	1
198 
199 /* DDNS flags from Section 12.9.  These are really their names. */
200 #define FTF_DDNS_C		0x0001
201 #define FTF_DDNS_A		0x0002
202 #define FTF_DDNS_D		0x0004
203 #define FTF_DDNS_P		0x0008
204 
205 /* FTO_IP_FLAGS contents from Section 12.12: */
206 #define FTF_IP_FLAG_RESERVE	0x0001
207 #define FTF_IP_FLAG_BOOTP	0x0002
208 
209 /* FTO_MESSAGE_DIGEST Type Codes from Section 12.17: */
210 #define FTT_MESSAGE_DIGEST_HMAC_MD5	0x01
211 
212 typedef struct failover_message {
213 	int refcnt;
214 	struct failover_message *next;
215 
216 	int options_present;
217 
218 	u_int32_t time;
219 	u_int32_t xid;
220 	u_int8_t type;
221 
222 	/* One-byte options. */
223 	u_int8_t binding_status;
224 	u_int8_t delayed_service;
225 	u_int8_t protocol_version;
226 	u_int8_t reject_reason;
227 	u_int8_t server_flags;
228 	u_int8_t server_state;
229 	u_int8_t tls_reply;
230 	u_int8_t tls_request;
231 
232 	/* Two-byte options. */
233 	u_int16_t ip_flags;
234 
235 	/* Four-byte options. */
236 	u_int32_t addresses_transferred;
237 	u_int32_t assigned_addr;
238 	u_int32_t cltt;
239 	u_int32_t expiry;
240 	u_int32_t max_unacked;
241 	u_int32_t mclt;
242 	u_int32_t potential_expiry;
243 	u_int32_t receive_timer;
244 	u_int32_t stos;
245 
246 	/* Arbitrary field options. */
247 	failover_option_t chaddr;
248 	failover_option_t client_identifier;
249 	failover_option_t hba;
250 	failover_option_t message;
251 	failover_option_t message_digest;
252 	failover_option_t relationship_name;
253 	failover_option_t reply_options;
254 	failover_option_t request_options;
255 	failover_option_t vendor_class;
256 	failover_option_t vendor_options;
257 
258 	/* Special contents options. */
259 	ddns_fqdn_t ddns;
260 } failover_message_t;
261 
262 typedef struct {
263 	OMAPI_OBJECT_PREAMBLE;
264 	struct option_cache *peer_address;
265 	unsigned peer_port;
266 	int options_present;
267 	enum dhcp_flink_state {
268 		dhcp_flink_start,
269 		dhcp_flink_message_length_wait,
270 		dhcp_flink_message_wait,
271 		dhcp_flink_disconnected,
272 		dhcp_flink_state_max
273 	} state;
274 	failover_message_t *imsg;
275 	struct _dhcp_failover_state *state_object;
276 	u_int16_t imsg_len;
277 	unsigned imsg_count;
278 	u_int8_t imsg_payoff; /* Pay*load* offset. :') */
279 	u_int32_t xid;
280 } dhcp_failover_link_t;
281 
282 typedef struct _dhcp_failover_listener {
283 	OMAPI_OBJECT_PREAMBLE;
284 	struct _dhcp_failover_listener *next;
285 	omapi_addr_t address;
286 } dhcp_failover_listener_t;
287 #endif /* FAILOVER_PROTOCOL */
288 
289 /* A failover peer's running state. */
290 enum failover_state {
291 	unknown_state			=  0, /* XXX: Not a standard state. */
292 	startup				=  1,
293 	normal				=  2,
294 	communications_interrupted	=  3,
295 	partner_down			=  4,
296 	potential_conflict		=  5,
297 	recover				=  6,
298 	paused				=  7,
299 	shut_down			=  8,
300 	recover_done			=  9,
301 	resolution_interrupted		= 10,
302 	conflict_done			= 11,
303 
304 	/* Draft revision 12 of the failover protocol documents a RECOVER-WAIT
305 	 * state, but does not enumerate its value in the section 12.24
306 	 * table.  ISC DHCP 3.0.x used value 254 even though the state was
307 	 * not documented at all.  For the time being, we will continue to use
308 	 * this value.
309 	 */
310 	recover_wait			= 254
311 };
312 
313 /* Service states are simplifications of failover states, particularly
314    useful because the startup state isn't actually implementable as a
315    separate failover state without maintaining a state stack. */
316 
317 enum service_state {
318 	unknown_service_state,
319 	cooperating,
320 	not_cooperating,
321 	service_partner_down,
322 	not_responding,
323 	service_startup
324 };
325 
326 #if defined (FAILOVER_PROTOCOL)
327 typedef struct _dhcp_failover_config {
328 	struct option_cache *address;
329 	int port;
330 	u_int32_t max_flying_updates;
331 	enum failover_state state;
332 	TIME stos;
333 	u_int32_t max_response_delay;
334 } dhcp_failover_config_t;
335 
336 typedef struct _dhcp_failover_state {
337 	OMAPI_OBJECT_PREAMBLE;
338 	struct _dhcp_failover_state *next;
339 	char *name;			/* Name of this failover instance. */
340 	dhcp_failover_config_t me;	/* My configuration. */
341 	dhcp_failover_config_t partner;	/* Partner's configuration. */
342 	enum failover_state saved_state; /* Saved state during startup. */
343 	struct data_string server_identifier; /* Server identifier (IP addr) */
344 	u_int32_t mclt;
345 
346 	u_int8_t *hba;	/* Hash bucket array for load balancing. */
347 	int load_balance_max_secs;
348 
349 	u_int32_t max_lease_misbalance, max_lease_ownership;
350 	u_int32_t max_balance, min_balance;
351 	TIME last_balance, sched_balance;
352 
353 	u_int32_t auto_partner_down;
354 
355 	enum service_state service_state;
356 	const char *nrr;	/* Printable reason why we're in the
357 				   not_responding service state (empty
358 				   string if we are responding. */
359 
360 	dhcp_failover_link_t *link_to_peer;	/* Currently-established link
361 						   to peer. */
362 
363 	enum {
364 		primary, secondary
365 	} i_am;		/* We are primary or secondary in this relationship. */
366 
367 	TIME last_packet_sent;		/* Timestamp on last packet we sent. */
368 	TIME last_timestamp_received;	/* The last timestamp we sent that
369 					   has been returned by our partner. */
370 	TIME skew;	/* The skew between our clock and our partner's. */
371 	struct lease *update_queue_head; /* List of leases we haven't sent
372 					    to peer. */
373 	struct lease *update_queue_tail;
374 
375 	struct lease *ack_queue_head;	/* List of lease updates the peer
376 					   hasn't yet acked. */
377 	struct lease *ack_queue_tail;
378 
379 	struct lease *send_update_done;	/* When we get a BNDACK for this
380 					   lease, send an UPDDONE message. */
381 	int cur_unacked_updates;	/* Number of updates we've sent
382 					   that have not yet been acked. */
383 
384 					/* List of messages which we haven't
385 					   acked yet. */
386 	failover_message_t *toack_queue_head;
387 	failover_message_t *toack_queue_tail;
388 	int pending_acks;		/* Number of messages in the toack
389 					   queue. */
390 	int pool_count;			/* Number of pools referencing this
391 					   failover state object. */
392 	int curUPD;			/* If an UPDREQ* message is in motion,
393 					   this value indicates which one. */
394 	u_int32_t updxid;		/* XID of UPDREQ* message in action. */
395 } dhcp_failover_state_t;
396 
397 extern int check_secs_byte_order; /* check byte order of secs field when true */
398 
399 #define DHCP_FAILOVER_VERSION		1
400 #endif /* FAILOVER_PROTOCOL */
401