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