dhcpd.h 131 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1 2 3 4 5
/* dhcpd.h

   Definitions for dhcpd... */

/*
6
 * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7
 * Copyright (c) 1996-2003 by Internet Software Consortium
Ted Lemon's avatar
Ted Lemon committed
8
 *
9 10 11
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
Ted Lemon's avatar
Ted Lemon committed
12
 *
13 14 15 16 17 18 19
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Ted Lemon's avatar
Ted Lemon committed
20
 *
21 22 23 24
 *   Internet Systems Consortium, Inc.
 *   950 Charter Street
 *   Redwood City, CA 94063
 *   <info@isc.org>
25
 *   https://www.isc.org/
Ted Lemon's avatar
Ted Lemon committed
26
 *
Ted Lemon's avatar
Ted Lemon committed
27 28
 */

Shawn Routhier's avatar
Shawn Routhier committed
29 30
/*! \file includes/dhcpd.h */

31
#include "config.h"
David Hankins's avatar
David Hankins committed
32

33
#ifndef __CYGWIN32__
Ted Lemon's avatar
Ted Lemon committed
34 35 36
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
37
#include <sys/un.h>
Ted Lemon's avatar
Ted Lemon committed
38
#include <arpa/inet.h>
39
#include <errno.h>
40

Ted Lemon's avatar
Ted Lemon committed
41
#include <netdb.h>
42 43 44 45
#else
#define fd_set cygwin_fd_set
#include <sys/types.h>
#endif
46
#include <stddef.h>
Ted Lemon's avatar
Ted Lemon committed
47 48
#include <fcntl.h>
#include <stdio.h>
49
#include <unistd.h>
50
#include <string.h>
51 52
#include <stdlib.h>
#include <sys/stat.h>
53
#include <sys/mman.h>
54
#include <ctype.h>
55
#include <time.h>
56

57
#include <net/if.h>
Mark Andrews's avatar
Mark Andrews committed
58
#undef FDDI
59 60
#include <net/route.h>
#include <net/if_arp.h>
61
#if HAVE_NET_IF_DL_H
62 63
# include <net/if_dl.h>
#endif
64 65 66

#include <setjmp.h>

67
#include "cdefs.h"
Ted Lemon's avatar
Ted Lemon committed
68
#include "osdep.h"
69 70

#include "arpa/nameser.h"
71 72

#include "minires.h"
73

74 75 76
struct hash_table;
typedef struct hash_table group_hash_t;
typedef struct hash_table universe_hash_t;
77 78
typedef struct hash_table option_name_hash_t;
typedef struct hash_table option_code_hash_t;
79
typedef struct hash_table dns_zone_hash_t;
80 81
typedef struct hash_table lease_ip_hash_t;
typedef struct hash_table lease_id_hash_t;
82 83 84
typedef struct hash_table host_hash_t;
typedef struct hash_table class_hash_t;

85 86 87 88 89 90
typedef time_t TIME;

#ifndef EOL
#define EOL '\n'
#endif

91 92 93
#include <omapip/isclib.h>
#include <omapip/result.h>

Ted Lemon's avatar
Ted Lemon committed
94
#include "dhcp.h"
David Hankins's avatar
David Hankins committed
95
#include "dhcp6.h"
96
#include "statement.h"
Ted Lemon's avatar
Ted Lemon committed
97
#include "tree.h"
98
#include "inet.h"
Ted Lemon's avatar
Ted Lemon committed
99
#include "dhctoken.h"
Ted Lemon's avatar
Ted Lemon committed
100

101
#include <omapip/omapip_p.h>
Ted Lemon's avatar
Ted Lemon committed
102

103 104 105 106 107
#if defined(LDAP_CONFIGURATION)
# include <ldap.h>
# include <sys/utsname.h> /* for uname() */
#endif

108
#if !defined (BYTE_NAME_HASH_SIZE)
Francis Dupont's avatar
Francis Dupont committed
109
# define BYTE_NAME_HASH_SIZE	401	/* Default would be ridiculous. */
110 111
#endif
#if !defined (BYTE_CODE_HASH_SIZE)
Francis Dupont's avatar
Francis Dupont committed
112
# define BYTE_CODE_HASH_SIZE	254	/* Default would be ridiculous. */
113 114
#endif

115 116 117 118 119
/* Although it is highly improbable that a 16-bit option space might
 * actually use 2^16 actual defined options, it is the worst case
 * scenario we must prepare for.  Having 4 options per bucket in this
 * case is pretty reasonable.
 */
120
#if !defined (WORD_NAME_HASH_SIZE)
121
# define WORD_NAME_HASH_SIZE	20479
122 123
#endif
#if !defined (WORD_CODE_HASH_SIZE)
124
# define WORD_CODE_HASH_SIZE	16384
125 126
#endif

127
/* Not only is it improbable that the 32-bit spaces might actually use 2^32
Francis Dupont's avatar
Francis Dupont committed
128
 * defined options, it is infeasible.  It would be best for this kind of
129 130 131
 * space to be dynamically sized.  Instead we size it at the word hash's
 * level.
 */
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#if !defined (QUAD_NAME_HASH_SIZE)
# define QUAD_NAME_HASH_SIZE	WORD_NAME_HASH_SIZE
#endif
#if !defined (QUAD_CODE_HASH_SIZE)
# define QUAD_CODE_HASH_SIZE	WORD_CODE_HASH_SIZE
#endif

#if !defined (DNS_HASH_SIZE)
# define DNS_HASH_SIZE		0	/* Default. */
#endif

/* Default size to use for name/code hashes on user-defined option spaces. */
#if !defined (DEFAULT_SPACE_HASH_SIZE)
# define DEFAULT_SPACE_HASH_SIZE	11
#endif

#if !defined (NWIP_HASH_SIZE)
149
# define NWIP_HASH_SIZE		17	/* A really small table. */
150 151 152
#endif

#if !defined (FQDN_HASH_SIZE)
Francis Dupont's avatar
Francis Dupont committed
153
# define FQDN_HASH_SIZE		13	/* A ridiculously small table. */
154 155
#endif

156 157 158
/* I really doubt a given installation is going to have more than a few
 * hundred vendors involved.
 */
159
#if !defined (VIVCO_HASH_SIZE)
160
# define VIVCO_HASH_SIZE	127
161 162 163 164 165 166
#endif

#if !defined (VIVSO_HASH_SIZE)
# define VIVSO_HASH_SIZE	VIVCO_HASH_SIZE
#endif

David Hankins's avatar
David Hankins committed
167 168 169 170
#if !defined (VSIO_HASH_SIZE)
# define VSIO_HASH_SIZE         VIVCO_HASH_SIZE
#endif

171 172 173 174 175
#if !defined (VIV_ISC_HASH_SIZE)
# define VIV_ISC_HASH_SIZE	3	/* An incredulously small table. */
#endif

#if !defined (UNIVERSE_HASH_SIZE)
176
# define UNIVERSE_HASH_SIZE	13	/* A really small table. */
177 178 179 180 181 182
#endif

#if !defined (GROUP_HASH_SIZE)
# define GROUP_HASH_SIZE	0	/* Default. */
#endif

183 184
/* At least one person has indicated they use ~20k host records.
 */
185
#if !defined (HOST_HASH_SIZE)
186
# define HOST_HASH_SIZE		22501
187 188
#endif

189 190 191 192 193 194
/* We have user reports of use of ISC DHCP numbering leases in the 200k's.
 *
 * We also have reports of folks using 10.0/8 as a dynamic range.  The
 * following is something of a compromise between the two.  At the ~2-3
 * hundred thousand leases, there's ~2-3 leases to search in each bucket.
 */
195
#if !defined (LEASE_HASH_SIZE)
196
# define LEASE_HASH_SIZE	100003
197 198
#endif

199 200 201
/* It is not known what the worst case subclass hash size is.  We estimate
 * high, I think.
 */
202
#if !defined (SCLASS_HASH_SIZE)
203
# define SCLASS_HASH_SIZE	12007
204 205 206 207 208 209
#endif

#if !defined (AGENT_HASH_SIZE)
# define AGENT_HASH_SIZE	11	/* A really small table. */
#endif

210 211 212 213 214 215
/* The server hash size is used for both names and codes.  There aren't
 * many (roughly 50 at the moment), so we use a smaller table.  If we
 * use a 1:1 table size, then we get name collisions due to poor name
 * hashing.  So we use double the space we need, which drastically
 * reduces collisions.
 */
216
#if !defined (SERVER_HASH_SIZE)
217
# define SERVER_HASH_SIZE (2*(sizeof(server_options) / sizeof(struct option)))
218 219 220
#endif


221
/* How many options are likely to appear in a single packet? */
222 223
#if !defined (OPTION_HASH_SIZE)
# define OPTION_HASH_SIZE 17
224 225
# define OPTION_HASH_PTWO 32	/* Next power of two above option hash. */
# define OPTION_HASH_EXP 5	/* The exponent for that power of two. */
226 227
#endif

228 229 230 231 232
#define compute_option_hash(x) \
	(((x) & (OPTION_HASH_PTWO - 1)) + \
	 (((x) >> OPTION_HASH_EXP) & \
	  (OPTION_HASH_PTWO - 1))) % OPTION_HASH_SIZE;

233 234 235 236 237 238 239
/* Lease queue information.  We have two ways of storing leases.
 * The original is a linear linked list which is slower but uses
 * less memory while the other adds a binary array on top of that
 * list to make insertions faster.  We define several macros
 * based on which is in use to allow the code to be cleaner by
 * avoiding #ifdefs.
 *
240
 * POOL_DESTROYP is used for cleanup
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
 */

#if !defined (BINARY_LEASES)
#define LEASE_STRUCT struct lease *
#define LEASE_STRUCT_PTR struct lease **
#define LEASE_GET_FIRST(LQ) LQ
#define LEASE_GET_FIRSTP(LQ) *(LQ)
#define LEASE_GET_NEXT(LQ, LEASE) LEASE->next
#define LEASE_GET_NEXTP(LQ, LEASE) LEASE->next
#define LEASE_INSERTP(LQ, LEASE) lease_insert(LQ, LEASE)
#define LEASE_REMOVEP(LQ, LEASE) lease_remove(LQ, LEASE)
#define LEASE_NOT_EMPTY(LQ) LQ
#define LEASE_NOT_EMPTYP(LQ) *LQ
#define POOL_DESTROYP(LQ) lease_remove_all(LQ)
#else
#define LEASE_STRUCT struct leasechain
#define LEASE_STRUCT_PTR struct leasechain *
#define LEASE_GET_FIRST(LQ) lc_get_first_lease(&LQ)
#define LEASE_GET_FIRSTP(LQ) lc_get_first_lease(LQ)
#define LEASE_GET_NEXT(LQ, LEASE) lc_get_next(&LQ, LEASE)
#define LEASE_GET_NEXTP(LQ, LEASE) lc_get_next(LQ, LEASE)
#define LEASE_INSERTP(LQ, LEASE) lc_add_sorted_lease(LQ, LEASE)
#define LEASE_REMOVEP(LQ, LEASE) lc_unlink_lease(LQ, LEASE)
#define LEASE_NOT_EMPTY(LQ) lc_not_empty(&LQ)
#define LEASE_NOT_EMPTYP(LQ) lc_not_empty(LQ)
#define POOL_DESTROYP(LQ) lc_delete_all(LQ)
#endif

269 270 271 272 273 274 275 276
enum dhcp_shutdown_state {
	shutdown_listeners,
	shutdown_omapi_connections,
	shutdown_drop_omapi_connections,
	shutdown_dhcp,
	shutdown_done
};

Ted Lemon's avatar
Ted Lemon committed
277 278 279 280 281 282 283
/* Client FQDN option, failover FQDN option, etc. */
typedef struct {
	u_int8_t codes [2];
	unsigned length;
	u_int8_t *data;
} ddns_fqdn_t;

Ted Lemon's avatar
Ted Lemon committed
284
#include "failover.h"
Ted Lemon's avatar
Ted Lemon committed
285

Ted Lemon's avatar
Ted Lemon committed
286 287 288 289 290 291 292 293
/* A parsing context. */

struct parse {
	int lexline;
	int lexchar;
	char *token_line;
	char *prev_line;
	char *cur_line;
294
	const char *tlname;
Ted Lemon's avatar
Ted Lemon committed
295 296
	int eol_token;

Shane Kerr's avatar
Shane Kerr committed
297
	/*
298
	 * In order to give nice output when we have a parsing error
Shane Kerr's avatar
Shane Kerr committed
299 300 301
	 * in our file, we keep track of where we are in the line so
	 * that we can show the user.
	 *
302
	 * We need to keep track of two lines, because we can look
Shane Kerr's avatar
Shane Kerr committed
303 304
	 * ahead, via the "peek" function, to the next line sometimes.
	 *
305
	 * The "line1" and "line2" variables act as buffers for this
Shane Kerr's avatar
Shane Kerr committed
306 307 308 309 310 311 312 313
	 * information. The "lpos" variable tells us where we are in the
	 * line.
	 *
	 * When we "put back" a character from the parsing context, we
	 * do not want to have the character appear twice in the error
	 * output. So, we set a flag, the "ugflag", which the
	 * get_char() function uses to check for this condition.
	 */
Ted Lemon's avatar
Ted Lemon committed
314 315 316 317 318 319 320 321 322
	char line1 [81];
	char line2 [81];
	int lpos;
	int line;
	int tlpos;
	int tline;
	enum dhcp_token token;
	int ugflag;
	char *tval;
323
	int tlen;
Ted Lemon's avatar
Ted Lemon committed
324 325 326 327 328
	char tokbuf [1500];

	int warnings_occurred;
	int file;
	char *inbuf;
329 330
	size_t bufix, buflen;
	size_t bufsiz;
331 332

	struct parse *saved_state;
333 334 335 336 337 338 339 340 341 342

#if defined(LDAP_CONFIGURATION)
	/*
	 * LDAP configuration uses a call-back to iteratively read config
	 * off of the LDAP repository.
	 * XXX: The token stream can not be rewound reliably, so this must
	 * be addressed for DHCPv6 support.
	 */
	int (*read_function)(struct parse *);
#endif
Ted Lemon's avatar
Ted Lemon committed
343 344
};

345 346
/* Variable-length array of data. */

347 348 349 350 351
struct string_list {
	struct string_list *next;
	char string [1];
};

352 353 354 355 356 357 358 359 360 361 362 363 364 365
/* A name server, from /etc/resolv.conf. */
struct name_server {
	struct name_server *next;
	struct sockaddr_in addr;
	TIME rcdate;
};

/* A domain search list element. */
struct domain_search_list {
	struct domain_search_list *next;
	char *domain;
	TIME rcdate;
};

366 367 368 369 370 371 372 373 374 375 376
/* Option tag structures are used to build chains of option tags, for
   when we're sure we're not going to have enough of them to justify
   maintaining an array. */

struct option_tag {
	struct option_tag *next;
	u_int8_t data [1];
};

/* An agent option structure.   We need a special structure for the
   Relay Agent Information option because if more than one appears in
377
   a message, we have to keep them separate. */
378 379 380 381 382 383 384

struct agent_options {
	struct agent_options *next;
	int length;
	struct option_tag *first;
};

385 386 387 388 389 390
struct option_cache {
	int refcnt;
	struct option_cache *next;
	struct expression *expression;
	struct option *option;
	struct data_string data;
391 392 393

	#define OPTION_HAD_NULLS	0x00000001
	u_int32_t flags;
394
};
395

396
struct option_state {
397
	int refcnt;
398
	int universe_count;
399 400
	int site_universe;
	int site_code_min;
401
	void *universes [1];
402 403
};

Ted Lemon's avatar
Ted Lemon committed
404 405 406
/* A dhcp packet and the pointers to its option values. */
struct packet {
	struct dhcp_packet *raw;
407
	int refcnt;
408
	unsigned packet_length;
Ted Lemon's avatar
Ted Lemon committed
409
	int packet_type;
David Hankins's avatar
David Hankins committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423

	unsigned char dhcpv6_msg_type;		/* DHCPv6 message type */

	/* DHCPv6 transaction ID */
	unsigned char dhcpv6_transaction_id[3];

	/* DHCPv6 relay information */
	unsigned char dhcpv6_hop_count;
	struct in6_addr dhcpv6_link_address;
	struct in6_addr dhcpv6_peer_address;

	/* DHCPv6 packet containing this one, or NULL if none */
	struct packet *dhcpv6_container_packet;

424 425 426 427 428 429
	/* DHCPv4-over-DHCPv6 flags */
	unsigned char dhcp4o6_flags[3];

	/* DHCPv4-over-DHCPv6 response, or NULL */
	struct data_string *dhcp4o6_response;

Ted Lemon's avatar
Ted Lemon committed
430
	int options_valid;
431 432
	int client_port;
	struct iaddr client_addr;
433 434 435 436
	struct interface_info *interface;	/* Interface on which packet
						   was received. */
	struct hardware *haddr;		/* Physical link address
					   of local sender (maybe gateway). */
437 438 439 440 441 442 443 444

	/* Information for relay agent options (see
	   draft-ietf-dhc-agent-options-xx.txt). */
	u_int8_t *circuit_id;		/* Circuit ID of client connection. */
	int circuit_id_len;
	u_int8_t *remote_id;		/* Remote ID of client. */
	int remote_id_len;

Ted Lemon's avatar
Ted Lemon committed
445 446 447
	int got_requested_address;	/* True if client sent the
					   dhcp-requested-address option. */

448
	struct shared_network *shared_network;
449
	struct option_state *options;
450 451 452 453 454 455

#if !defined (PACKET_MAX_CLASSES)
# define PACKET_MAX_CLASSES 5
#endif
	int class_count;
	struct class *classes [PACKET_MAX_CLASSES];
456 457 458

	int known;
	int authenticated;
459 460 461 462 463 464

	/* If we stash agent options onto the packet option state, to pretend
	 * options we got in a previous exchange were still there, we need
	 * to signal this in a reliable way.
	 */
	isc_boolean_t agent_options_stashed;
David Hankins's avatar
David Hankins committed
465

466 467
	/*
	 * ISC_TRUE if packet received unicast (as opposed to multicast).
David Hankins's avatar
David Hankins committed
468 469 470
	 * Only used in DHCPv6.
	 */
	isc_boolean_t unicast;
471 472 473 474

	/* Propogates server value SV_ECHO_CLIENT_ID so it is available
         * in cons_options() */
	int sv_echo_client_id;
Ted Lemon's avatar
Ted Lemon committed
475 476
};

477 478 479 480 481 482 483
/*
 * A network interface's MAC address.
 * 20 bytes for the hardware address
 * and 1 byte for the type tag
 */

#define HARDWARE_ADDR_LEN 20
484

Ted Lemon's avatar
Ted Lemon committed
485 486
struct hardware {
	u_int8_t hlen;
487
	u_int8_t hbuf[HARDWARE_ADDR_LEN + 1];
Ted Lemon's avatar
Ted Lemon committed
488 489
};

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
#if defined(LDAP_CONFIGURATION)
# define LDAP_BUFFER_SIZE		8192
# define LDAP_METHOD_STATIC		0
# define LDAP_METHOD_DYNAMIC	1
#if defined (LDAP_USE_SSL)
# define LDAP_SSL_OFF			0
# define LDAP_SSL_ON			1
# define LDAP_SSL_TLS			2
# define LDAP_SSL_LDAPS			3
#endif

/* This is a tree of the current configuration we are building from LDAP */
struct ldap_config_stack {
	LDAPMessage * res;	/* Pointer returned from ldap_search */
	LDAPMessage * ldent;	/* Current item in LDAP that we're processing.
							in res */
	int close_brace;	/* Put a closing } after we're through with
						this item */
	int processed;	/* We set this flag if this base item has been
					processed. After this base item is processed,
					we can start processing the children */
	struct ldap_config_stack *children;
	struct ldap_config_stack *next;
};
#endif

516 517 518 519 520 521 522 523 524 525 526 527 528
typedef enum {
	server_startup = 0,
	server_running = 1,
	server_shutdown = 2,
	server_hibernate = 3,
	server_awaken = 4
} control_object_state_t;

typedef struct {
	OMAPI_OBJECT_PREAMBLE;
	control_object_state_t state;
} dhcp_control_object_t;

Ted Lemon's avatar
Ted Lemon committed
529
/* Lease states: */
530 531 532 533 534 535 536 537 538 539
#define FTS_FREE	1
#define FTS_ACTIVE	2
#define FTS_EXPIRED	3
#define FTS_RELEASED	4
#define FTS_ABANDONED	5
#define FTS_RESET	6
#define FTS_BACKUP	7
typedef u_int8_t binding_state_t;

/* FTS_LAST is the highest value that is valid for a lease binding state. */
540
#define FTS_LAST FTS_BACKUP
Ted Lemon's avatar
Ted Lemon committed
541

Shawn Routhier's avatar
Shawn Routhier committed
542 543 544 545 546 547 548 549 550 551
/*
 * A block for the on statements so we can share the structure
 * between v4 and v6
 */
struct on_star {
	struct executable_statement *on_expiry;
	struct executable_statement *on_commit;
	struct executable_statement *on_release;
};

Ted Lemon's avatar
Ted Lemon committed
552 553
/* A dhcp lease declaration structure. */
struct lease {
Ted Lemon's avatar
Ted Lemon committed
554
	OMAPI_OBJECT_PREAMBLE;
Ted Lemon's avatar
Ted Lemon committed
555
	struct lease *next;
556 557 558 559
#if defined (BINARY_LEASES)
	struct lease *prev;
	struct leasechain *lc;
#endif
560
	struct lease *n_uid, *n_hw;
561

562
	struct iaddr ip_addr;
Shane Kerr's avatar
Shane Kerr committed
563
	TIME starts, ends, sort_time;
564 565 566
#if defined (BINARY_LEASES)
	long int sort_tiebreaker;
#endif
567
	char *client_hostname;
568
	struct binding_scope *scope;
Ted Lemon's avatar
Ted Lemon committed
569
	struct host_decl *host;
570
	struct subnet *subnet;
571
	struct pool *pool;
572
	struct class *billing_class;
573
	struct option_chain_head *agent_options;
574

Shawn Routhier's avatar
Shawn Routhier committed
575 576
	/* insert the structure directly */
	struct on_star on_star;
577

578 579 580 581 582 583 584
	unsigned char *uid;
	unsigned short uid_len;
	unsigned short uid_max;
	unsigned char uid_buf [7];
	struct hardware hardware_addr;

	u_int8_t flags;
585
#       define STATIC_LEASE		1
586
#	define BOOTP_LEASE		2
587
#	define RESERVED_LEASE		4
588
#	define MS_NULL_TERMINATION	8
589 590
#	define ON_UPDATE_QUEUE		16
#	define ON_ACK_QUEUE		32
591
#	define ON_QUEUE			(ON_UPDATE_QUEUE | ON_ACK_QUEUE)
592
#	define UNICAST_BROADCAST_HACK	64
593
#	define ON_DEFERRED_QUEUE	128
594 595 596 597

/* Persistent flags are to be preserved on a given lease structure. */
#       define PERSISTENT_FLAGS		(ON_ACK_QUEUE | ON_UPDATE_QUEUE)
/* Ephemeral flags are to be preserved on a given lease (copied etc). */
598
#	define EPHEMERAL_FLAGS		(MS_NULL_TERMINATION | \
599 600 601
					 UNICAST_BROADCAST_HACK | \
					 RESERVED_LEASE | \
					 BOOTP_LEASE)
602

603 604 605 606 607 608 609 610 611 612 613 614 615
	/*
	 * The lease's binding state is its current state.  The next binding
	 * state is the next state this lease will move into by expiration,
	 * or timers in general.  The desired binding state is used on lease
	 * updates; the caller is attempting to move the lease to the desired
	 * binding state (and this may either succeed or fail, so the binding
	 * state must be preserved).
	 *
	 * The 'rewind' binding state is used in failover processing.  It
	 * is used for an optimization when out of communications; it allows
	 * the server to "rewind" a lease to the previous state acknowledged
	 * by the peer, and progress forward from that point.
	 */
616 617 618
	binding_state_t binding_state;
	binding_state_t next_binding_state;
	binding_state_t desired_binding_state;
619 620
	binding_state_t rewind_binding_state;

621
	struct lease_state *state;
622

623 624
	/*
	 * 'tsfp' is more of an 'effective' tsfp.  It may be calculated from
625 626 627 628 629
	 * stos+mclt for example if it's an expired lease and the server is
	 * in partner-down state.  'atsfp' is zeroed whenever a lease is
	 * updated - and only set when the peer acknowledges it.  This
	 * ensures every state change is transmitted.
	 */
630 631
	TIME tstp;	/* Time sent to partner. */
	TIME tsfp;	/* Time sent from partner. */
632
	TIME atsfp;	/* Actual time sent from partner. */
633
	TIME cltt;	/* Client last transaction time. */
634
	u_int32_t last_xid; /* XID we sent in this lease's BNDUPD */
635
	struct lease *next_pending;
636 637 638 639 640 641 642 643

	/*
	 * A pointer to the state of the ddns update for this lease.
	 * It should be set while the update is in progress and cleared
	 * when the update finishes.  It can be used to cancel the
	 * update if we want to do a different update.
	 */
	struct dhcp_ddns_cb *ddns_cb;
644 645 646

	/* Set when a lease has been disqualified for cache-threshold reuse */
	unsigned short cannot_reuse;
647 648 649 650 651 652 653
};

struct lease_state {
	struct lease_state *next;

	struct interface_info *ip;

654 655
	struct packet *packet;	/* The incoming packet. */

656 657
	TIME offered_expiry;

658
	struct option_state *options;
659
	struct data_string parameter_request_list;
660
	int max_message_size;
661
	unsigned char expiry[4], renewal[4], rebind[4];
662
	struct data_string filename, server_name;
Ted Lemon's avatar
Ted Lemon committed
663 664 665 666
	int got_requested_address;
	int got_server_identifier;
	struct shared_network *shared_network;	/* Shared network of interface
						   on which request arrived. */
667 668 669 670 671

	u_int32_t xid;
	u_int16_t secs;
	u_int16_t bootp_flags;
	struct in_addr ciaddr;
672
	struct in_addr siaddr;
673 674 675
	struct in_addr giaddr;
	u_int8_t hops;
	u_int8_t offer;
676
	struct iaddr from;
677 678
};

679
#define	ROOT_GROUP	0
Ted Lemon's avatar
Ted Lemon committed
680 681 682 683 684
#define HOST_DECL	1
#define SHARED_NET_DECL	2
#define SUBNET_DECL	3
#define CLASS_DECL	4
#define	GROUP_DECL	5
685
#define POOL_DECL	6
686

687 688 689 690 691
/* Possible modes in which discover_interfaces can run. */

#define DISCOVER_RUNNING	0
#define DISCOVER_SERVER		1
#define DISCOVER_UNCONFIGURED	2
Ted Lemon's avatar
Ted Lemon committed
692
#define DISCOVER_RELAY		3
693 694
#define DISCOVER_SERVER46	4
#define DISCOVER_REQUESTED	5
695

David Hankins's avatar
David Hankins committed
696 697 698 699
/* DDNS_UPDATE_STYLE enumerations. */
#define DDNS_UPDATE_STYLE_NONE		0
#define DDNS_UPDATE_STYLE_AD_HOC	1
#define DDNS_UPDATE_STYLE_INTERIM	2
Shawn Routhier's avatar
Shawn Routhier committed
700
#define DDNS_UPDATE_STYLE_STANDARD	3
David Hankins's avatar
David Hankins committed
701

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/* Server option names. */

#define SV_DEFAULT_LEASE_TIME		1
#define SV_MAX_LEASE_TIME		2
#define SV_MIN_LEASE_TIME		3
#define SV_BOOTP_LEASE_CUTOFF		4
#define SV_BOOTP_LEASE_LENGTH		5
#define SV_BOOT_UNKNOWN_CLIENTS		6
#define SV_DYNAMIC_BOOTP		7
#define	SV_ALLOW_BOOTP			8
#define	SV_ALLOW_BOOTING		9
#define	SV_ONE_LEASE_PER_CLIENT		10
#define	SV_GET_LEASE_HOSTNAMES		11
#define	SV_USE_HOST_DECL_NAMES		12
#define	SV_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE	13
#define	SV_MIN_SECS			14
#define	SV_FILENAME			15
#define SV_SERVER_NAME			16
#define	SV_NEXT_SERVER			17
Ted Lemon's avatar
Ted Lemon committed
721
#define SV_AUTHORITATIVE		18
722
#define SV_VENDOR_OPTION_SPACE		19
723
#define SV_ALWAYS_REPLY_RFC1048		20
724
#define SV_SITE_OPTION_SPACE		21
725
#define SV_ALWAYS_BROADCAST		22
726 727 728
#define SV_DDNS_DOMAIN_NAME		23
#define SV_DDNS_HOST_NAME		24
#define SV_DDNS_REV_DOMAIN_NAME		25
729 730
#define SV_LEASE_FILE_NAME		26
#define SV_PID_FILE_NAME		27
Ted Lemon's avatar
Ted Lemon committed
731 732
#define SV_DUPLICATES			28
#define SV_DECLINES			29
733
#define SV_DDNS_UPDATES			30
734 735 736 737 738
#define SV_OMAPI_PORT			31
#define SV_LOCAL_PORT			32
#define SV_LIMITED_BROADCAST_ADDRESS	33
#define SV_REMOTE_PORT			34
#define SV_LOCAL_ADDRESS		35
739
#define SV_OMAPI_KEY			36
Ted Lemon's avatar
Ted Lemon committed
740
#define SV_STASH_AGENT_OPTIONS		37
741
#define SV_DDNS_TTL			38
742
#define SV_DDNS_UPDATE_STYLE		39
Ted Lemon's avatar
Ted Lemon committed
743 744
#define SV_CLIENT_UPDATES		40
#define SV_UPDATE_OPTIMIZATION		41
745 746
#define SV_PING_CHECKS			42
#define SV_UPDATE_STATIC_LEASES		43
747
#define SV_LOG_FACILITY			44
748
#define SV_DO_FORWARD_UPDATES		45
749 750
#define SV_PING_TIMEOUT			46
#define SV_RESERVE_INFINITE		47
751
#define SV_DDNS_CONFLICT_DETECT		48
Shane Kerr's avatar
Shane Kerr committed
752
#define SV_LEASEQUERY			49
753
#define SV_ADAPTIVE_LEASE_TIME_THRESHOLD	50
754
#define SV_DO_REVERSE_UPDATES		51
755
#define SV_FQDN_REPLY			52
David Hankins's avatar
David Hankins committed
756
#define SV_PREFER_LIFETIME		53
757 758
#define SV_DHCPV6_LEASE_FILE_NAME       54
#define SV_DHCPV6_PID_FILE_NAME         55
759
#define SV_LIMIT_ADDRS_PER_IA		56
760 761
#define SV_LIMIT_PREFS_PER_IA		57
#define SV_DELAYED_ACK			58
762
#define SV_MAX_ACK_DELAY		59
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
#if defined(LDAP_CONFIGURATION)
# define SV_LDAP_SERVER                 60
# define SV_LDAP_PORT                   61
# define SV_LDAP_USERNAME               62
# define SV_LDAP_PASSWORD               63
# define SV_LDAP_BASE_DN                64
# define SV_LDAP_METHOD                 65
# define SV_LDAP_DEBUG_FILE             66
# define SV_LDAP_DHCP_SERVER_CN         67
# define SV_LDAP_REFERRALS              68
#if defined (LDAP_USE_SSL)
# define SV_LDAP_SSL                    69
# define SV_LDAP_TLS_REQCERT            70
# define SV_LDAP_TLS_CA_FILE            71
# define SV_LDAP_TLS_CA_DIR             72
# define SV_LDAP_TLS_CERT               73
# define SV_LDAP_TLS_KEY                74
# define SV_LDAP_TLS_CRLCHECK           75
# define SV_LDAP_TLS_CIPHERS            76
# define SV_LDAP_TLS_RANDFILE           77
#endif
784 785 786 787 788
# define SV_LDAP_INIT_RETRY            178
#if defined (LDAP_USE_GSSAPI)
# define SV_LDAP_GSSAPI_KEYTAB         179
# define SV_LDAP_GSSAPI_PRINCIPAL      180
#endif
789 790
#endif
#define SV_CACHE_THRESHOLD		78
791
#define SV_DONT_USE_FSYNC		79
792 793
#define SV_DDNS_LOCAL_ADDRESS4		80
#define SV_DDNS_LOCAL_ADDRESS6		81
Shawn Routhier's avatar
Shawn Routhier committed
794
#define SV_IGNORE_CLIENT_UIDS		82
795 796
#define SV_LOG_THRESHOLD_LOW		83
#define SV_LOG_THRESHOLD_HIGH		84
797
#define SV_ECHO_CLIENT_ID		85
798
#define SV_SERVER_ID_CHECK		86
799
#define SV_PREFIX_LEN_MODE		87
800
#define SV_DHCPV6_SET_TEE_TIMES		88
801
#define SV_ABANDON_LEASE_TIME		89
802 803 804
#ifdef EUI_64
#define SV_USE_EUI_64			90
#endif
805 806 807 808

#if !defined (DEFAULT_PING_TIMEOUT)
# define DEFAULT_PING_TIMEOUT 1
#endif
809

810 811 812 813
#if !defined (DEFAULT_DELAYED_ACK)
# define DEFAULT_DELAYED_ACK 28  /* default SO_SNDBUF size / 576 bytes */
#endif

814 815 816 817 818 819 820 821
#if !defined (DEFAULT_ACK_DELAY_SECS)
# define DEFAULT_ACK_DELAY_SECS 0
#endif

#if !defined (DEFAULT_ACK_DELAY_USECS)
# define DEFAULT_ACK_DELAY_USECS 250000 /* 1/4 of a second */
#endif

822 823 824 825
#if !defined (DEFAULT_MIN_ACK_DELAY_USECS)
# define DEFAULT_MIN_ACK_DELAY_USECS 10000 /* 1/100 second */
#endif

826 827
#if !defined (DEFAULT_CACHE_THRESHOLD)
# define DEFAULT_CACHE_THRESHOLD 25
828 829
#endif

830 831 832 833 834
#if !defined (DEFAULT_DEFAULT_LEASE_TIME)
# define DEFAULT_DEFAULT_LEASE_TIME 43200
#endif

#if !defined (DEFAULT_MIN_LEASE_TIME)
835
# define DEFAULT_MIN_LEASE_TIME 300
836 837 838 839 840 841
#endif

#if !defined (DEFAULT_MAX_LEASE_TIME)
# define DEFAULT_MAX_LEASE_TIME 86400
#endif

842 843 844
#if !defined (DEFAULT_DDNS_TTL)
# define DEFAULT_DDNS_TTL 3600
#endif
845 846 847
#if !defined (MAX_DEFAULT_DDNS_TTL)
# define MAX_DEFAULT_DDNS_TTL 3600
#endif
848

849 850 851 852
#if !defined (MIN_LEASE_WRITE)
# define MIN_LEASE_WRITE 15
#endif

853 854 855 856
#if !defined (DEFAULT_ABANDON_LEASE_TIME)
# define DEFAULT_ABANDON_LEASE_TIME 86400
#endif

857 858 859 860 861 862
#define PLM_IGNORE 0
#define PLM_PREFER 1
#define PLM_EXACT 2
#define PLM_MINIMUM 3
#define PLM_MAXIMUM 4

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
/* Client option names */

#define	CL_TIMEOUT		1
#define	CL_SELECT_INTERVAL	2
#define CL_REBOOT_TIMEOUT	3
#define CL_RETRY_INTERVAL	4
#define CL_BACKOFF_CUTOFF	5
#define CL_INITIAL_INTERVAL	6
#define CL_BOOTP_POLICY		7
#define	CL_SCRIPT_NAME		8
#define CL_REQUESTED_OPTIONS	9
#define CL_REQUESTED_LEASE_TIME	10
#define CL_SEND_OPTIONS		11
#define CL_MEDIA		12
#define	CL_REJECT_LIST		13

#ifndef CL_DEFAULT_TIMEOUT
# define CL_DEFAULT_TIMEOUT	60
#endif

#ifndef CL_DEFAULT_SELECT_INTERVAL
# define CL_DEFAULT_SELECT_INTERVAL 0
#endif

#ifndef CL_DEFAULT_REBOOT_TIMEOUT
# define CL_DEFAULT_REBOOT_TIMEOUT 10
#endif

#ifndef CL_DEFAULT_RETRY_INTERVAL
# define CL_DEFAULT_RETRY_INTERVAL 300
#endif

#ifndef CL_DEFAULT_BACKOFF_CUTOFF
# define CL_DEFAULT_BACKOFF_CUTOFF 120
#endif

#ifndef CL_DEFAULT_INITIAL_INTERVAL
# define CL_DEFAULT_INITIAL_INTERVAL 10
#endif

#ifndef CL_DEFAULT_BOOTP_POLICY
Ted Lemon's avatar
Ted Lemon committed
904
# define CL_DEFAULT_BOOTP_POLICY P_ACCEPT
905 906 907 908 909 910 911 912 913 914 915 916 917
#endif

#ifndef CL_DEFAULT_REQUESTED_OPTIONS
# define CL_DEFAULT_REQUESTED_OPTIONS \
	{ DHO_SUBNET_MASK, \
	  DHO_BROADCAST_ADDRESS, \
	  DHO_TIME_OFFSET, \
	  DHO_ROUTERS, \
	  DHO_DOMAIN_NAME, \
	  DHO_DOMAIN_NAME_SERVERS, \
	  DHO_HOST_NAME }
#endif

Ted Lemon's avatar
Ted Lemon committed
918 919 920 921 922 923 924 925 926 927 928 929
struct group_object {
	OMAPI_OBJECT_PREAMBLE;

	struct group_object *n_dynamic;
	struct group *group;
	char *name;
	int flags;
#define GROUP_OBJECT_DELETED	1
#define GROUP_OBJECT_DYNAMIC	2
#define GROUP_OBJECT_STATIC	4
};

Ted Lemon's avatar
Ted Lemon committed
930
/* Group of declarations that share common parameters. */
931 932 933
struct group {
	struct group *next;

934
	int refcnt;
Ted Lemon's avatar
Ted Lemon committed
935
	struct group_object *object;
936 937
	struct subnet *subnet;
	struct shared_network *shared_network;
Ted Lemon's avatar
Ted Lemon committed
938
	int authoritative;
939
	struct executable_statement *statements;
940 941 942 943
};

/* A dhcp host declaration structure. */
struct host_decl {
Ted Lemon's avatar
Ted Lemon committed
944
	OMAPI_OBJECT_PREAMBLE;
945
	struct host_decl *n_ipaddr;
Ted Lemon's avatar
Ted Lemon committed
946
	struct host_decl *n_dynamic;
947 948
	char *name;
	struct hardware interface;
949
	struct data_string client_identifier;
David Hankins's avatar
David Hankins committed
950 951 952 953 954
	struct option *host_id_option;
	struct data_string host_id;
	/* XXXSK: fixed_addr should be an array of iaddr values,
		  not an option_cache, but it's referenced in a lot of
		  places, so we'll leave it for now. */
955
	struct option_cache *fixed_addr;
956
	struct iaddrcidrnetlist *fixed_prefix;
957
	struct group *group;
Ted Lemon's avatar
Ted Lemon committed
958
	struct group_object *named_group;
959
	struct data_string auth_key_id;
Ted Lemon's avatar
Ted Lemon committed
960
	int flags;
961 962
#define HOST_DECL_DELETED	1
#define HOST_DECL_DYNAMIC	2
Ted Lemon's avatar
Ted Lemon committed
963
#define HOST_DECL_STATIC	4
964 965 966 967
	/* For v6 the host-identifer option can specify which relay
	   to use when trying to look up an option.  We store the
	   value here. */
	int relays;
968 969
};

970 971 972 973 974 975 976 977 978
struct permit {
	struct permit *next;
	enum {
		permit_unknown_clients,
		permit_known_clients,
		permit_authenticated_clients,
		permit_unauthenticated_clients,
		permit_all_clients,
		permit_dynamic_bootp_clients,
979 980
		permit_class,
		permit_after
981 982
	} type;
	struct class *class;
983
	TIME after;	/* date after which this clause applies */
984 985
};

986 987 988 989 990 991 992 993 994 995 996 997
#if defined (BINARY_LEASES)
struct leasechain {
	struct lease **list; /* lease list */
	size_t total;	     /* max number of elements in this list,
			      * including free pointers at the end if any */
	size_t nelem;	     /* the number of elements, also the next index to use */
	size_t growth;	     /* the growth factor to use when increase an array
			      * this is set after parsing the pools and before
			      * creatin an array.  */
};
#endif

998
struct pool {
Ted Lemon's avatar
Ted Lemon committed
999
	OMAPI_OBJECT_PREAMBLE;
1000
	struct pool *next;
1001
	struct group *group;
1002 1003 1004
	struct shared_network *shared_network;
	struct permit *permit_list;
	struct permit *prohibit_list;
1005 1006 1007 1008 1009 1010
	LEASE_STRUCT active;
	LEASE_STRUCT expired;
	LEASE_STRUCT free;
	LEASE_STRUCT backup;
	LEASE_STRUCT abandoned;
	LEASE_STRUCT reserved;
1011
	TIME next_event_time;
1012
	int lease_count;
1013 1014
	int free_leases;
	int backup_leases;
1015
	int index;
1016 1017 1018
	TIME valid_from;        /* deny pool use before this date */
	TIME valid_until;       /* deny pool use after this date */

Ted Lemon's avatar
Ted Lemon committed
1019
#if defined (FAILOVER_PROTOCOL)
1020 1021
	dhcp_failover_state_t *failover_peer;
#endif
1022 1023
	int logged;		/* already logged a message */
	int low_threshold;	/* low threshold to restart logging */
Ted Lemon's avatar
Ted Lemon committed
1024 1025
};

1026
struct shared_network {
Ted Lemon's avatar
Ted Lemon committed
1027
	OMAPI_OBJECT_PREAMBLE;
1028
	struct shared_network *next;
1029
	char *name;
1030 1031 1032 1033

#define SHARED_IMPLICIT	  1 /* This network was synthesized. */
	int flags;

1034 1035
	struct subnet *subnets;
	struct interface_info *interface;
1036
	struct pool *pools;
Shawn Routhier's avatar
Shawn Routhier committed
1037
	struct ipv6_pond *ipv6_pond;
1038
	struct group *group;
Ted Lemon's avatar
Ted Lemon committed
1039
#if defined (FAILOVER_PROTOCOL)
1040
	dhcp_failover_state_t *failover_peer;
Ted Lemon's avatar
Ted Lemon committed
1041
#endif
Ted Lemon's avatar
Ted Lemon committed
1042 1043 1044
};

struct subnet {
Ted Lemon's avatar
Ted Lemon committed
1045
	OMAPI_OBJECT_PREAMBLE;
1046 1047 1048 1049 1050
	struct subnet *next_subnet;
	struct subnet *next_sibling;
	struct shared_network *shared_network;
	struct interface_info *interface;
	struct iaddr interface_address;
1051 1052
	struct iaddr net;
	struct iaddr netmask;
David Hankins's avatar
David Hankins committed
1053
	int prefix_len;			/* XXX: currently for IPv6 only */
1054
	struct group *group;
Ted Lemon's avatar
Ted Lemon committed
1055 1056
};

<