29 static const char copyright[] =
30 "Copyright 2004-2015 Internet Systems Consortium.";
31 static const char arr [] =
"All rights reserved.";
32 static const char message [] =
"Internet Systems Consortium DHCP Server";
33 static const char url [] =
34 "For info, please visit https://www.isc.org/software/dhcp/";
42 #include <sys/types.h>
45 #if defined (PARANOIA)
46 # include <sys/types.h>
50 # define group real_group
61 #ifdef HAVE_LIBSYSTEMD
62 #include <systemd/sd-daemon.h>
66 static void usage(
void);
72 #if defined (NSUPDATE)
76 char std_nsupdate [] =
" \n\
77 option server.ddns-hostname = \n\
78 pick (option fqdn.hostname, option host-name, config-option host-name); \n\
79 option server.ddns-domainname = config-option domain-name; \n\
80 option server.ddns-rev-domainname = \"in-addr.arpa.\";";
104 return ISC_R_SUCCESS;
110 return ISC_R_SUCCESS;
113 static void omapi_listener_start (
void *foo)
121 if (result != ISC_R_SUCCESS)
122 log_fatal (
"Can't allocate new generic object: %s",
123 isc_result_totext (result));
126 if (result == ISC_R_SUCCESS && omapi_key)
128 (listener, verify_addr, verify_auth);
129 if (result != ISC_R_SUCCESS) {
130 log_error (
"Can't start OMAPI protocol: %s",
131 isc_result_totext (result));
132 tv.tv_sec =
cur_tv.tv_sec + 5;
133 tv.tv_usec =
cur_tv.tv_usec;
145 #if defined (PARANOIA)
147 static void setup_chroot (
char *chroot_dir) {
149 log_fatal (
"you must be root to use chroot");
151 if (chroot(chroot_dir)) {
152 log_fatal (
"chroot(\"%s\"): %m", chroot_dir);
175 char *server = (
char *)0;
179 #if defined (NSUPDATE)
183 int no_dhcpd_conf = 0;
185 int no_dhcpd_pid = 0;
187 int local_family_set = 0;
189 #if defined (TRACING)
190 char *traceinfile = (
char *)0;
191 char *traceoutfile = (
char *)0;
194 #if defined (PARANOIA)
197 char *set_chroot = 0;
203 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
205 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
207 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
216 if (status != ISC_R_SUCCESS)
217 log_fatal(
"Can't initialize context: %s",
218 isc_result_totext(status));
225 if (result != ISC_R_SUCCESS)
227 isc_result_totext (result));
238 for (i = 1; i < argc; i++) {
239 if (!strcmp (argv [i],
"-p")) {
243 log_debug (
"binding to user-specified port %d",
245 }
else if (!strcmp (argv [i],
"-f")) {
249 }
else if (!strcmp (argv [i],
"-d")) {
254 }
else if (!strcmp (argv [i],
"-s")) {
258 #if defined (PARANOIA)
259 }
else if (!strcmp (argv [i],
"-user")) {
263 }
else if (!strcmp (argv [i],
"-group")) {
266 set_group = argv [i];
267 }
else if (!strcmp (argv [i],
"-chroot")) {
270 set_chroot = argv [i];
272 }
else if (!strcmp (argv [i],
"-cf")) {
277 }
else if (!strcmp (argv [i],
"-lf")) {
282 }
else if (!strcmp (argv [i],
"-pf")) {
287 }
else if (!strcmp(argv[i],
"--no-pid")) {
289 }
else if (!strcmp (argv [i],
"-t")) {
296 }
else if (!strcmp (argv [i],
"-T")) {
304 }
else if (!strcmp (argv [i],
"-q")) {
308 }
else if (!strcmp(argv[i],
"-4")) {
310 log_fatal(
"Server cannot run in both IPv4 and "
311 "IPv6 mode at the same time.");
314 local_family_set = 1;
315 }
else if (!strcmp(argv[i],
"-6")) {
317 log_fatal(
"Server cannot run in both IPv4 and "
318 "IPv6 mode at the same time.");
321 local_family_set = 1;
323 }
else if (!strcmp (argv [i],
"--version")) {
324 const char vstring[] =
"isc-dhcpd-";
332 #if defined (TRACING)
333 }
else if (!strcmp (argv [i],
"-tf")) {
336 traceoutfile = argv [i];
337 }
else if (!strcmp (argv [i],
"-play")) {
340 traceinfile = argv [i];
343 }
else if (argv [i][0] ==
'-') {
348 if (strlen(argv[i]) >=
sizeof(tmp->
name))
351 argv[i], (
long)strlen(argv[i]));
352 result = interface_allocate (&tmp,
MDL);
353 if (result != ISC_R_SUCCESS)
354 log_fatal (
"Insufficient memory to %s %s: %s",
355 "record interface", argv [i],
356 isc_result_totext (result));
357 strcpy (tmp ->
name, argv [i]);
359 interface_reference (&tmp ->
next,
368 if (!no_dhcpd_conf && (s = getenv (
"PATH_DHCPD_CONF"))) {
376 if ((s = getenv (
"PATH_DHCPD6_DB")))
382 if ((s = getenv (
"PATH_DHCPD6_PID")))
390 if (!no_dhcpd_db && (s = getenv (
"PATH_DHCPD_DB"))) {
393 if (!no_dhcpd_pid && (s = getenv (
"PATH_DHCPD_PID"))) {
407 log_fatal(
"Failed to get realpath for %s: %s", path,
420 #if defined (TRACING)
424 if (result != ISC_R_SUCCESS)
426 isc_result_totext (result));
433 #if defined (NSUPDATE)
438 #if defined (PARANOIA)
441 struct passwd *tmp_pwd;
444 log_fatal (
"you must be root to set user");
446 if (!(tmp_pwd = getpwnam(set_user)))
447 log_fatal (
"no such user: %s", set_user);
458 #define group real_group
459 struct group *tmp_grp;
462 log_fatal (
"you must be root to set group");
464 if (!(tmp_grp = getgrnam(set_group)))
465 log_fatal (
"no such group: %s", set_group);
471 # if defined (EARLY_CHROOT)
472 if (set_chroot) setup_chroot (set_chroot);
479 if ((s = getenv (
"DHCPD_PORT"))) {
481 log_debug (
"binding to environment-specified port %d",
485 ent = getservbyname(
"dhcp",
"udp");
493 ent = getservbyname(
"dhcpv6-server",
"udp");
510 ent = getservbyname(
"dhcpv6-client",
"udp");
520 log_fatal(
"You can only specify address to send "
521 "replies to when running an IPv4 server.");
525 he = gethostbyname (server);
528 he -> h_addr_list [0],
538 gettimeofday(&
cur_tv, NULL);
547 #if defined (LDAP_CONFIGURATION)
549 #if defined (LDAP_USE_SSL)
557 log_fatal (
"Can't allocate root group!");
568 #if defined (NSUPDATE)
571 status =
new_parse(&parse, -1, std_nsupdate,
sizeof(std_nsupdate) - 1,
572 "standard name service update routine", 0);
573 if (status != ISC_R_SUCCESS)
574 log_fatal (
"can't begin parsing name service updater!");
581 log_fatal(
"can't parse standard name service updater!");
588 if (!cftest && !lftest)
591 #if defined (TRACING)
595 log_error (
"** You must specify a lease file with -lf.");
596 log_error (
" Dhcpd will not overwrite your default");
597 log_fatal (
" lease file when playing back a trace. **");
601 #if defined (DEBUG_MEMORY_LEAKAGE) && \
602 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
614 log_fatal(
"Out of memory creating hash for active IA_NA.");
617 log_fatal(
"Out of memory creating hash for active IA_TA.");
620 log_fatal(
"Out of memory creating hash for active IA_PD.");
626 log_fatal (
"Configuration file errors encountered -- exiting");
630 #if defined (PARANOIA) && !defined (EARLY_CHROOT)
631 if (set_chroot) setup_chroot (set_chroot);
640 if (cftest && !lftest)
653 status = read(i, pbuf, (
sizeof pbuf) - 1);
664 (pid != getpid() && kill(pid, 0) == 0))
666 "DHCP server running.");
708 &ip -> hw_address.hbuf [ip -> hw_address.hlen -
709 sizeof seed],
sizeof seed);
713 #if defined (TRACING)
732 log_fatal(
"Unable to set server identifier.");
740 if ((pid = fork ()) < 0)
752 i = open(
path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
754 sprintf(pbuf,
"%d\n", (
int) getpid());
758 log_error(
"Can't create PID file %s: %m.",
763 #if defined (PARANOIA)
767 if (setgroups (0, (
void *)0))
796 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
797 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
798 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
805 #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
806 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
807 dmalloc_cutoff_generation = dmalloc_generation;
808 dmalloc_longterm = dmalloc_outstanding;
809 dmalloc_outstanding = 0;
815 #if defined(ENABLE_GENTLE_SHUTDOWN)
823 log_info(
"Server starting service.");
827 #ifdef HAVE_LIBSYSTEMD
829 sd_notifyf(0,
"READY=1\n"
830 "STATUS=Dispatching packets...\n"
832 (
unsigned long) getpid());
853 #if defined (NSUPDATE)
854 struct in_addr local4, *local4_ptr = NULL;
855 struct in6_addr local6, *local6_ptr = NULL;
864 memset(&db, 0,
sizeof db);
871 log_fatal(
"no memory for lease db filename.");
884 log_fatal(
"no memory for pid filename.");
904 log_fatal(
"no memory for lease db filename.");
918 log_fatal(
"no memory for pid filename.");
935 log_fatal(
"invalid omapi port data length");
945 log_fatal(
"no memory for OMAPI key filename.");
951 if (result != ISC_R_SUCCESS)
953 s, isc_result_totext (result));
963 log_fatal(
"invalid local port data length");
974 log_fatal(
"invalid remote port data length");
986 log_fatal(
"invalid broadcast address data length");
997 log_fatal(
"invalid local address data length");
1014 #if defined (NSUPDATE)
1017 log_fatal(
"ddns-update-style ad_hoc no longer supported");
1025 memcpy(&local4, db.
data, 4);
1026 local4_ptr = &local4;
1037 memcpy(&local6, db.
data, 16);
1038 local6_ptr = &local6;
1046 log_fatal(
"Unable to complete ddns initialization");
1051 log_fatal(
"Support for ddns-update-style not compiled in");
1084 #if defined(DELAYED_ACK)
1092 log_fatal(
"invalid max delayed ACK count ");
1104 log_fatal(
"invalid max ack delay configuration");
1119 log_error(
"Not using fsync() to flush lease writes");
1126 log_info(
"Setting server-id-check true");
1143 #if defined (BINARY_LEASES)
1145 log_info(
"Source compiled to use binary-leases");
1157 omapi_listener_start (0);
1160 #if defined (FAILOVER_PROTOCOL)
1179 log_fatal(
"Usage: dhcpd [-p <UDP port #>] [-f] [-d] [-q] [-t|-T]\n"
1181 " [-4|-6] [-cf config-file] [-lf lease-file]\n"
1183 " [-cf config-file] [-lf lease-file]\n"
1187 " [-user user] [-group group] [-chroot dir]\n"
1190 " [-tf trace-output-file]\n"
1191 " [-play trace-input-file]\n"
1193 " [-pf pid-file] [--no-pid] [-s server]\n"
1212 lp = (
struct lease *)0;
1214 log_debug (
"unexpected ICMP Echo Reply from %s",
1220 #if defined (FAILOVER_PROTOCOL)
1222 !lp ->
pool -> failover_peer)
1224 log_debug (
"ICMP Echo Reply for %s late or spurious.",
1230 log_debug (
"ICMP Echo reply while lease %s valid.",
1244 lease_dereference (&lp,
MDL);
1250 struct lease *lp = vlp;
1252 #if defined (DEBUG_MEMORY_LEAKAGE)
1253 unsigned long previous_outstanding = dmalloc_outstanding;
1259 #if defined (DEBUG_MEMORY_LEAKAGE)
1260 log_info (
"generation %ld: %ld new, %ld outstanding, %ld long-term",
1262 dmalloc_outstanding - previous_outstanding,
1263 dmalloc_outstanding, dmalloc_longterm);
1265 #if defined (DEBUG_MEMORY_LEAKAGE)
1266 dmalloc_dump_outstanding ();
1274 isc_result_t status;
1279 const char *fnn =
"fallback-net";
1281 if (status != ISC_R_SUCCESS)
1282 log_fatal (
"No memory for shared subnet: %s",
1283 isc_result_totext (status));
1291 subnet = (
struct subnet *)0;
1299 log_error (
"Multiple interfaces match the %s: %s %s",
1301 subnet ->
interface -> name, ip -> name);
1306 log_fatal (
"Interface %s matches multiple shared %s",
1307 ip -> name,
"networks");
1310 shared_network_reference
1317 log_error (
"Multiple interfaces match the %s: %s %s",
1318 "same shared network",
1319 share ->
interface -> name, ip -> name);
1321 subnet_dereference (&subnet,
MDL);
1326 static TIME shutdown_time;
1327 static int omapi_connection_count;
1338 return ISC_R_SUCCESS;
1348 omapi_connection_count++;
1351 return ISC_R_SUCCESS;
1359 return ISC_R_SUCCESS;
1361 return ISC_R_SUCCESS;
1364 static isc_result_t dhcp_io_shutdown_countdown (
void *vlp)
1366 #if defined (FAILOVER_PROTOCOL)
1367 dhcp_failover_state_t *state;
1368 int failover_connection_count = 0;
1377 omapi_connection_count = 0;
1384 omapi_connection_count == 0) {
1407 #if defined (FAILOVER_PROTOCOL)
1411 if (state -> me.state ==
normal) {
1413 failover_connection_count++;
1417 failover_connection_count++;
1424 if (state -> link_to_peer)
1425 dhcp_failover_link_dereference (&state -> link_to_peer,
1430 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1431 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1441 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1442 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1453 !failover_connection_count &&
1460 tv.tv_sec =
cur_tv.tv_sec + 1;
1461 tv.tv_usec =
cur_tv.tv_usec;
1463 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1464 return ISC_R_SUCCESS;
1476 return ISC_R_SUCCESS;
1482 dhcp_io_shutdown_countdown (0);
1483 return ISC_R_SUCCESS;
1493 tv.tv_sec =
cur_tv.tv_sec;
1494 tv.tv_usec =
cur_tv.tv_usec + 1;
1496 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1497 return ISC_R_SUCCESS;
void do_packet6(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
const char * path_dhcpd_db
void initialize_server_option_spaces(void)
void(* dhcpv6_packet_handler)(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
int dhcpd_interface_setup_hook(struct interface_info *ip, struct iaddr *ia)
void mark_hosts_unavailable(void)
isc_result_t omapi_protocol_listen(omapi_object_t *, unsigned, int)
void add_enumeration(struct enumeration *enumeration)
void parse_trace_setup(void)
struct binding_scope * global_scope
omapi_object_type_t * omapi_type_connection
#define SV_DDNS_UPDATE_STYLE
const char * piaddr(const struct iaddr addr)
isc_boolean_t server_duid_isset(void)
isc_result_t end_parse(struct parse **cfile)
void lease_ping_timeout(void *vlp)
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, unsigned, unsigned int, struct iaddr, struct hardware *)
void * dmalloc(unsigned, const char *, int)
struct lease_state * state
struct universe server_universe
#define SV_DONT_USE_FSYNC
void cancel_timeout(void(*)(void *) where, void *what)
void icmp_startup(int routep, void *handler)
#define DHCP_R_INVALIDARG
#define DDNS_UPDATE_STYLE_AD_HOC
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define SV_DDNS_LOCAL_ADDRESS4
isc_result_t dhcp_interface_remove(omapi_object_t *lp, omapi_object_t *id)
isc_result_t omapi_auth_key_lookup_name(omapi_auth_key_t **, const char *)
#define DHCP_CONTEXT_PRE_DB
isc_boolean_t no_pid_file
#define SV_LEASE_FILE_NAME
void data_string_forget(struct data_string *data, const char *file, int line)
struct group * root_group
int log_error(const char *,...) __attribute__((__format__(__printf__
trace_type_t * trace_type_register(const char *, void *, void(*)(trace_type_t *, unsigned, char *), void(*)(trace_type_t *), const char *, int)
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
#define DHCP_R_INVALIDKEY
void omapi_print_dmalloc_usage_by_caller(void)
void mark_interfaces_unavailable(void)
void do_packet(struct interface_info *interface, struct dhcp_packet *packet, unsigned len, unsigned int from_port, struct iaddr from, struct hardware *hfrom)
void log_fatal(const char *,...) __attribute__((__format__(__printf__
#define SV_SERVER_ID_CHECK
isc_result_t omapi_io_state_foreach(isc_result_t(*func)(omapi_object_t *, void *), void *p)
calls a given function on every object
#define DHCP_CONTEXT_POST_DB
struct executable_statement * statements
void interface_trace_setup(void)
void free_lease_state(struct lease_state *, const char *, int)
omapi_object_type_t * omapi_type_listener
int server_identifier_matched
omapi_object_type_t * omapi_type_protocol
u_int16_t validate_port(char *port)
void dhcp_signal_handler(int signal)
void postconf_initialization(int quiet)
int find_subnet(struct subnet **sp, struct iaddr addr, const char *file, int line)
void execute_statements_in_scope(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct group *group, struct group *limiting_group, struct on_star *on_star)
isc_result_t readconf(void)
#define FAILOVER_PROTOCOL
int option_state_allocate(struct option_state **ptr, const char *file, int line)
isc_result_t dhcp_context_create(int flags, struct in_addr *local4, struct in6_addr *local6)
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
isc_result_t dhcp_failover_set_state(dhcp_failover_state_t *, enum failover_state)
void trace_seed_stop(trace_type_t *)
#define DHCPD_LOG_FACILITY
void mark_phosts_unavailable(void)
struct iaddr interface_address
void abandon_lease(struct lease *, const char *)
isc_result_t dhcp_set_control_state(control_object_state_t oldstate, control_object_state_t newstate)
int group_allocate(struct group **ptr, const char *file, int line)
isc_result_t omapi_listener_destroy(omapi_object_t *, const char *, int)
int write_server_duid(void)
int(* group_write_hook)(struct group_object *)
isc_result_t omapi_object_dereference(omapi_object_t **, const char *, int)
void trace_file_replay(const char *)
isc_result_t omapi_generic_new(omapi_object_t **, const char *, int)
omapi_object_type_t * omapi_type_protocol_listener
u_int32_t getUShort(const unsigned char *)
isc_result_t trace_begin(const char *, const char *, int)
void dfree(void *, const char *, int)
omapi_object_type_t * dhcp_type_interface
#define SV_DHCPV6_PID_FILE_NAME
dhcp_failover_state_t * failover_states
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
void dhcp_db_objects_setup(void)
struct in_addr limited_broadcast
int int log_info(const char *,...) __attribute__((__format__(__printf__
int main(int argc, char **argv)
void trace_seed_stash(trace_type_t *, unsigned)
struct interface_info * interfaces
u_int32_t getULong(const unsigned char *)
struct shared_network * shared_network
void trace_replay_init(void)
void db_startup(int testp)
void lease_pinged(struct iaddr from, u_int8_t *packet, int length)
dhcp_control_object_t * dhcp_control_object
int(* dhcp_interface_setup_hook)(struct interface_info *, struct iaddr *)
isc_result_t dhcp_io_shutdown(omapi_object_t *obj, void *foo)
#define DEFAULT_HASH_SIZE
void dhcp_failover_startup(void)
int quiet_interface_discovery
isc_result_t trace_init(void(*set_time)(time_t), const char *, int)
int option_state_dereference(struct option_state **ptr, const char *file, int line)
void initialize_common_option_spaces()
void trace_ddns_init(void)
struct interface_info * next
int evaluate_boolean_option_cache(int *ignorep, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
void schedule_all_ipv6_lease_timeouts()
void report_jumbo_ranges()
isc_result_t set_server_duid_from_option(void)
isc_result_t omapi_protocol_configure_security(omapi_object_t *, isc_result_t(*)(omapi_object_t *, omapi_addr_t *), isc_result_t(*)(omapi_object_t *, omapi_auth_key_t *))
#define DDNS_UPDATE_STYLE_NONE
#define SV_DHCPV6_LEASE_FILE_NAME
struct in_addr local_address
int dhcp_max_agent_option_packet_length
#define SV_DDNS_LOCAL_ADDRESS6
#define SV_PREFIX_LEN_MODE
void classification_setup(void)
isc_result_t omapi_set_int_value(omapi_object_t *, omapi_object_t *, const char *, int)
#define SV_LIMITED_BROADCAST_ADDRESS
int group_writer(struct group_object *)
const char * path_dhcpd_conf
void dhcp_reply(struct lease *)
struct enumeration syslog_enum
int parse_executable_statements(struct executable_statement **statements, struct parse *cfile, int *lose, enum expression_context case_context)
isc_result_t omapi_disconnect(omapi_object_t *, int)
const unsigned char * data
void dhcp_common_objects_setup(void)
isc_result_t generate_new_server_duid(void)
enum dhcp_shutdown_state shutdown_state
struct iaddr server_identifier
struct enumeration prefix_length_modes
const char * path_dhcpd_pid
void trace_seed_input(trace_type_t *, unsigned, char *)
int find_lease_by_ip_addr(struct lease **, struct iaddr, const char *, int)
struct enumeration ddns_styles
void discover_interfaces(int state)
struct interface_info * interface
isc_result_t new_parse(struct parse **cfile, int file, char *inbuf, unsigned buflen, const char *name, int eolp)
#define INTERFACE_REQUESTED
void postdb_startup(void)
isc_result_t omapi_init(void)