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
57 #ifdef HAVE_LIBSYSTEMD
58 #include <systemd/sd-daemon.h>
62 static void usage(
void);
68 #if defined (NSUPDATE)
72 char std_nsupdate [] =
" \n\
73 option server.ddns-hostname = \n\
74 pick (option fqdn.hostname, option host-name, config-option host-name); \n\
75 option server.ddns-domainname = config-option domain-name; \n\
76 option server.ddns-rev-domainname = \"in-addr.arpa.\";";
100 return ISC_R_SUCCESS;
106 return ISC_R_SUCCESS;
109 static void omapi_listener_start (
void *foo)
117 if (result != ISC_R_SUCCESS)
118 log_fatal (
"Can't allocate new generic object: %s",
119 isc_result_totext (result));
122 if (result == ISC_R_SUCCESS && omapi_key)
124 (listener, verify_addr, verify_auth);
125 if (result != ISC_R_SUCCESS) {
126 log_error (
"Can't start OMAPI protocol: %s",
127 isc_result_totext (result));
128 tv.tv_sec =
cur_tv.tv_sec + 5;
129 tv.tv_usec =
cur_tv.tv_usec;
135 #if defined (PARANOIA)
137 static void setup_chroot (
char *chroot_dir) {
139 log_fatal (
"you must be root to use chroot");
141 if (chroot(chroot_dir)) {
142 log_fatal (
"chroot(\"%s\"): %m", chroot_dir);
166 char *server = (
char *)0;
170 #if defined (NSUPDATE)
174 int no_dhcpd_conf = 0;
176 int no_dhcpd_pid = 0;
178 int local_family_set = 0;
180 #if defined (TRACING)
181 char *traceinfile = (
char *)0;
182 char *traceoutfile = (
char *)0;
185 #if defined (PARANOIA)
188 char *set_chroot = 0;
197 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
199 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
201 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
210 if (status != ISC_R_SUCCESS)
211 log_fatal(
"Can't initialize context: %s",
212 isc_result_totext(status));
219 if (result != ISC_R_SUCCESS)
221 isc_result_totext (result));
232 for (i = 1; i < argc; i++) {
233 if (!strcmp (argv [i],
"-p")) {
237 log_debug (
"binding to user-specified port %d",
239 }
else if (!strcmp (argv [i],
"-f")) {
243 }
else if (!strcmp (argv [i],
"-d")) {
248 }
else if (!strcmp (argv [i],
"-s")) {
252 #if defined (PARANOIA)
253 }
else if (!strcmp (argv [i],
"-user")) {
257 }
else if (!strcmp (argv [i],
"-group")) {
260 set_group = argv [i];
261 }
else if (!strcmp (argv [i],
"-chroot")) {
264 set_chroot = argv [i];
266 }
else if (!strcmp (argv [i],
"-cf")) {
271 }
else if (!strcmp (argv [i],
"-lf")) {
276 }
else if (!strcmp (argv [i],
"-pf")) {
281 }
else if (!strcmp(argv[i],
"--no-pid")) {
283 }
else if (!strcmp (argv [i],
"-t")) {
290 }
else if (!strcmp (argv [i],
"-T")) {
298 }
else if (!strcmp (argv [i],
"-q")) {
302 }
else if (!strcmp(argv[i],
"-4")) {
304 log_fatal(
"Server cannot run in both IPv4 and "
305 "IPv6 mode at the same time.");
308 local_family_set = 1;
309 }
else if (!strcmp(argv[i],
"-6")) {
311 log_fatal(
"Server cannot run in both IPv4 and "
312 "IPv6 mode at the same time.");
315 local_family_set = 1;
317 }
else if (!strcmp (argv [i],
"--version")) {
318 const char vstring[] =
"isc-dhcpd-";
326 #if defined (TRACING)
327 }
else if (!strcmp (argv [i],
"-tf")) {
330 traceoutfile = argv [i];
331 }
else if (!strcmp (argv [i],
"-play")) {
334 traceinfile = argv [i];
337 }
else if (argv [i][0] ==
'-') {
342 if (strlen(argv[i]) >=
sizeof(tmp->
name))
345 argv[i], (
long)strlen(argv[i]));
346 result = interface_allocate (&tmp,
MDL);
347 if (result != ISC_R_SUCCESS)
348 log_fatal (
"Insufficient memory to %s %s: %s",
349 "record interface", argv [i],
350 isc_result_totext (result));
351 strcpy (tmp ->
name, argv [i]);
353 interface_reference (&tmp ->
next,
362 if (!no_dhcpd_conf && (s = getenv (
"PATH_DHCPD_CONF"))) {
370 if ((s = getenv (
"PATH_DHCPD6_DB")))
376 if ((s = getenv (
"PATH_DHCPD6_PID")))
384 if (!no_dhcpd_db && (s = getenv (
"PATH_DHCPD_DB"))) {
387 if (!no_dhcpd_pid && (s = getenv (
"PATH_DHCPD_PID"))) {
401 log_fatal(
"Failed to get realpath for %s: %s", path,
414 #if defined (TRACING)
418 if (result != ISC_R_SUCCESS)
420 isc_result_totext (result));
427 #if defined (NSUPDATE)
432 #if defined (PARANOIA)
435 struct passwd *tmp_pwd;
438 log_fatal (
"you must be root to set user");
440 if (!(tmp_pwd = getpwnam(set_user)))
441 log_fatal (
"no such user: %s", set_user);
443 set_uid = tmp_pwd->pw_uid;
447 set_gid = tmp_pwd->pw_gid;
452 #define group real_group
453 struct group *tmp_grp;
456 log_fatal (
"you must be root to set group");
458 if (!(tmp_grp = getgrnam(set_group)))
459 log_fatal (
"no such group: %s", set_group);
461 set_gid = tmp_grp->gr_gid;
465 # if defined (EARLY_CHROOT)
466 if (set_chroot) setup_chroot (set_chroot);
473 if ((s = getenv (
"DHCPD_PORT"))) {
475 log_debug (
"binding to environment-specified port %d",
479 ent = getservbyname(
"dhcp",
"udp");
487 ent = getservbyname(
"dhcpv6-server",
"udp");
504 ent = getservbyname(
"dhcpv6-client",
"udp");
514 log_fatal(
"You can only specify address to send "
515 "replies to when running an IPv4 server.");
519 he = gethostbyname (server);
522 he -> h_addr_list [0],
532 gettimeofday(&
cur_tv, NULL);
541 #if defined (LDAP_CONFIGURATION)
543 #if defined (LDAP_USE_SSL)
551 log_fatal (
"Can't allocate root group!");
562 #if defined (NSUPDATE)
565 status =
new_parse(&parse, -1, std_nsupdate,
sizeof(std_nsupdate) - 1,
566 "standard name service update routine", 0);
567 if (status != ISC_R_SUCCESS)
568 log_fatal (
"can't begin parsing name service updater!");
575 log_fatal(
"can't parse standard name service updater!");
582 if (!cftest && !lftest)
585 #if defined (TRACING)
589 log_error (
"** You must specify a lease file with -lf.");
590 log_error (
" Dhcpd will not overwrite your default");
591 log_fatal (
" lease file when playing back a trace. **");
595 #if defined (DEBUG_MEMORY_LEAKAGE) && \
596 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
608 log_fatal(
"Out of memory creating hash for active IA_NA.");
611 log_fatal(
"Out of memory creating hash for active IA_TA.");
614 log_fatal(
"Out of memory creating hash for active IA_PD.");
620 log_fatal (
"Configuration file errors encountered -- exiting");
624 #if defined (PARANOIA) && !defined (EARLY_CHROOT)
625 if (set_chroot) setup_chroot (set_chroot);
634 if (cftest && !lftest)
647 status = read(i, pbuf, (
sizeof pbuf) - 1);
658 (pid != getpid() && kill(pid, 0) == 0))
660 "DHCP server running.");
668 #if defined (PARANOIA)
706 &ip -> hw_address.hbuf [ip -> hw_address.hlen -
707 sizeof seed],
sizeof seed);
711 #if defined (TRACING)
726 log_fatal(
"Unable to set server identifier.");
736 if ((pid = fork ()) < 0)
748 i = open(
path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
750 sprintf(pbuf,
"%d\n", (
int) getpid());
754 log_error(
"Can't create PID file %s: %m.",
759 #if defined (PARANOIA)
763 if (setgroups (0, (
void *)0))
765 if (setgid (set_gid))
766 log_fatal (
"setgid(%d): %m", (
int) set_gid);
770 if (setuid (set_uid))
771 log_fatal (
"setuid(%d): %m", (
int) set_uid);
792 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
793 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
794 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
801 #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
802 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
803 dmalloc_cutoff_generation = dmalloc_generation;
804 dmalloc_longterm = dmalloc_outstanding;
805 dmalloc_outstanding = 0;
811 #if defined(ENABLE_GENTLE_SHUTDOWN)
819 log_info(
"Server starting service.");
823 #ifdef HAVE_LIBSYSTEMD
825 sd_notifyf(0,
"READY=1\n"
826 "STATUS=Dispatching packets...\n"
828 (
unsigned long) getpid());
849 #if defined (NSUPDATE)
850 struct in_addr local4, *local4_ptr = NULL;
851 struct in6_addr local6, *local6_ptr = NULL;
860 memset(&db, 0,
sizeof db);
867 log_fatal(
"no memory for lease db filename.");
880 log_fatal(
"no memory for pid filename.");
900 log_fatal(
"no memory for lease db filename.");
914 log_fatal(
"no memory for pid filename.");
931 log_fatal(
"invalid omapi port data length");
941 log_fatal(
"no memory for OMAPI key filename.");
947 if (result != ISC_R_SUCCESS)
949 s, isc_result_totext (result));
959 log_fatal(
"invalid local port data length");
970 log_fatal(
"invalid remote port data length");
982 log_fatal(
"invalid broadcast address data length");
993 log_fatal(
"invalid local address data length");
1010 #if defined (NSUPDATE)
1013 log_fatal(
"ddns-update-style ad_hoc no longer supported");
1021 memcpy(&local4, db.
data, 4);
1022 local4_ptr = &local4;
1033 memcpy(&local6, db.
data, 16);
1034 local6_ptr = &local6;
1042 log_fatal(
"Unable to complete ddns initialization");
1047 log_fatal(
"Support for ddns-update-style not compiled in");
1087 log_fatal(
"invalid max delayed ACK count ");
1099 log_fatal(
"invalid max ack delay configuration");
1113 log_error(
"Not using fsync() to flush lease writes");
1120 log_info(
"Setting server-id-check true");
1145 omapi_listener_start (0);
1148 #if defined (FAILOVER_PROTOCOL)
1167 log_fatal(
"Usage: dhcpd [-p <UDP port #>] [-f] [-d] [-q] [-t|-T]\n"
1169 " [-4|-6] [-cf config-file] [-lf lease-file]\n"
1171 " [-cf config-file] [-lf lease-file]\n"
1175 " [-user user] [-group group] [-chroot dir]\n"
1178 " [-tf trace-output-file]\n"
1179 " [-play trace-input-file]\n"
1181 " [-pf pid-file] [--no-pid] [-s server]\n"
1200 lp = (
struct lease *)0;
1202 log_debug (
"unexpected ICMP Echo Reply from %s",
1208 #if defined (FAILOVER_PROTOCOL)
1210 !lp ->
pool -> failover_peer)
1212 log_debug (
"ICMP Echo Reply for %s late or spurious.",
1218 log_debug (
"ICMP Echo reply while lease %s valid.",
1232 lease_dereference (&lp,
MDL);
1238 struct lease *lp = vlp;
1240 #if defined (DEBUG_MEMORY_LEAKAGE)
1241 unsigned long previous_outstanding = dmalloc_outstanding;
1247 #if defined (DEBUG_MEMORY_LEAKAGE)
1248 log_info (
"generation %ld: %ld new, %ld outstanding, %ld long-term",
1250 dmalloc_outstanding - previous_outstanding,
1251 dmalloc_outstanding, dmalloc_longterm);
1253 #if defined (DEBUG_MEMORY_LEAKAGE)
1254 dmalloc_dump_outstanding ();
1262 isc_result_t status;
1267 const char *fnn =
"fallback-net";
1269 if (status != ISC_R_SUCCESS)
1270 log_fatal (
"No memory for shared subnet: %s",
1271 isc_result_totext (status));
1279 subnet = (
struct subnet *)0;
1287 log_error (
"Multiple interfaces match the %s: %s %s",
1289 subnet ->
interface -> name, ip -> name);
1294 log_fatal (
"Interface %s matches multiple shared %s",
1295 ip -> name,
"networks");
1298 shared_network_reference
1305 log_error (
"Multiple interfaces match the %s: %s %s",
1306 "same shared network",
1307 share ->
interface -> name, ip -> name);
1309 subnet_dereference (&subnet,
MDL);
1314 static TIME shutdown_time;
1315 static int omapi_connection_count;
1326 return ISC_R_SUCCESS;
1336 omapi_connection_count++;
1339 return ISC_R_SUCCESS;
1347 return ISC_R_SUCCESS;
1349 return ISC_R_SUCCESS;
1352 static isc_result_t dhcp_io_shutdown_countdown (
void *vlp)
1354 #if defined (FAILOVER_PROTOCOL)
1355 dhcp_failover_state_t *state;
1356 int failover_connection_count = 0;
1365 omapi_connection_count = 0;
1372 omapi_connection_count == 0) {
1395 #if defined (FAILOVER_PROTOCOL)
1399 if (state -> me.state ==
normal) {
1401 failover_connection_count++;
1405 failover_connection_count++;
1412 if (state -> link_to_peer)
1413 dhcp_failover_link_dereference (&state -> link_to_peer,
1418 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1419 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1429 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1430 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1441 !failover_connection_count &&
1448 tv.tv_sec =
cur_tv.tv_sec + 1;
1449 tv.tv_usec =
cur_tv.tv_usec;
1451 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1452 return ISC_R_SUCCESS;
1464 return ISC_R_SUCCESS;
1470 dhcp_io_shutdown_countdown (0);
1471 return ISC_R_SUCCESS;
1481 tv.tv_sec =
cur_tv.tv_sec;
1482 tv.tv_usec =
cur_tv.tv_usec + 1;
1484 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1485 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_result_t omapi_io_state_foreach(isc_result_t(*func)(omapi_object_t *, void *), void *p)
calls a given function on every object
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
void db_startup(int, uid_t, gid_t)
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__
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
#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)
trace_type_t * trace_type_register(const char *, void *, void(*)(trace_type_t *, unsigned, char *), void(*)(trace_type_t *), const char *, int)
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 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)