1 /* $OpenBSD: dhcpd.h,v 1.67 2019/05/08 22:00:55 krw Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996, 1997, 1998, 1999 5 * The Internet Software Consortium. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The Internet Software Consortium nor the names 17 * of its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 21 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * This software has been written for the Internet Software Consortium 35 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 36 * Enterprises. To learn more about the Internet Software Consortium, 37 * see ``http://www.vix.com/isc''. To learn more about Vixie 38 * Enterprises, see ``http://www.vix.com''. 39 */ 40 41 #define ifr_netmask ifr_addr 42 43 #define HAVE_SA_LEN 44 #define HAVE_MKSTEMP 45 46 #define DB_TIMEFMT "%w %Y/%m/%d %T UTC" 47 #define OLD_DB_TIMEFMT "%w %Y/%m/%d %T" 48 49 #define SERVER_PORT 67 50 #define CLIENT_PORT 68 51 52 struct iaddr { 53 int len; 54 unsigned char iabuf[16]; 55 }; 56 57 #define DEFAULT_HASH_SIZE 97 58 59 struct hash_bucket { 60 struct hash_bucket *next; 61 unsigned char *name; 62 int len; 63 unsigned char *value; 64 }; 65 66 struct hash_table { 67 int hash_count; 68 struct hash_bucket *buckets[DEFAULT_HASH_SIZE]; 69 }; 70 71 struct option_data { 72 int len; 73 u_int8_t *data; 74 }; 75 76 /* A dhcp packet and the pointers to its option values. */ 77 struct packet { 78 struct dhcp_packet *raw; 79 int packet_length; 80 int packet_type; 81 int options_valid; 82 int client_port; 83 struct iaddr client_addr; 84 struct interface_info *interface; /* Interface on which packet 85 was received. */ 86 struct hardware *haddr; /* Physical link address 87 of local sender (maybe gateway). */ 88 struct shared_network *shared_network; 89 struct option_data options[256]; 90 int got_requested_address; /* True if client sent the 91 dhcp-requested-address option. */ 92 }; 93 94 struct hardware { 95 u_int8_t htype; 96 u_int8_t hlen; 97 u_int8_t haddr[16]; 98 }; 99 100 /* A dhcp lease declaration structure. */ 101 struct lease { 102 struct lease *next; 103 struct lease *prev; 104 struct lease *n_uid, *n_hw; 105 struct lease *waitq_next; 106 107 struct iaddr ip_addr; 108 time_t starts, ends, timestamp; 109 unsigned char *uid; 110 int uid_len; 111 int uid_max; 112 unsigned char uid_buf[32]; 113 char *hostname; 114 char *client_hostname; 115 uint8_t *client_identifier; 116 struct host_decl *host; 117 struct subnet *subnet; 118 struct shared_network *shared_network; 119 struct hardware hardware_addr; 120 121 int client_identifier_len; 122 int flags; 123 #define STATIC_LEASE 1 124 #define BOOTP_LEASE 2 125 #define DYNAMIC_BOOTP_OK 4 126 #define PERSISTENT_FLAGS (DYNAMIC_BOOTP_OK) 127 #define EPHEMERAL_FLAGS (BOOTP_LEASE) 128 #define MS_NULL_TERMINATION 8 129 #define ABANDONED_LEASE 16 130 #define INFORM_NOLEASE 32 131 132 struct lease_state *state; 133 u_int8_t releasing; 134 }; 135 136 struct lease_state { 137 struct lease_state *next; 138 139 struct interface_info *ip; 140 141 time_t offered_expiry; 142 143 struct tree_cache *options[256]; 144 u_int32_t expiry, renewal, rebind; 145 char filename[DHCP_FILE_LEN]; 146 char *server_name; 147 148 struct iaddr from; 149 150 int max_message_size; 151 u_int8_t *prl; 152 int prl_len; 153 int got_requested_address; /* True if client sent the 154 dhcp-requested-address option. */ 155 int got_server_identifier; /* True if client sent the 156 dhcp-server-identifier option. */ 157 struct shared_network *shared_network; /* Shared network of interface 158 on which request arrived. */ 159 160 u_int32_t xid; 161 u_int16_t secs; 162 u_int16_t bootp_flags; 163 struct in_addr ciaddr; 164 struct in_addr giaddr; 165 u_int8_t hops; 166 u_int8_t offer; 167 struct hardware haddr; 168 }; 169 170 #define ROOT_GROUP 0 171 #define HOST_DECL 1 172 #define SHARED_NET_DECL 2 173 #define SUBNET_DECL 3 174 #define CLASS_DECL 4 175 #define GROUP_DECL 5 176 177 /* Group of declarations that share common parameters. */ 178 struct group { 179 struct group *next; 180 181 struct subnet *subnet; 182 struct shared_network *shared_network; 183 184 time_t default_lease_time; 185 time_t max_lease_time; 186 time_t bootp_lease_cutoff; 187 time_t bootp_lease_length; 188 189 char *filename; 190 char *server_name; 191 struct iaddr next_server; 192 193 int boot_unknown_clients; 194 int dynamic_bootp; 195 int allow_bootp; 196 int allow_booting; 197 int get_lease_hostnames; 198 int use_host_decl_names; 199 int use_lease_addr_for_default_route; 200 int authoritative; 201 int always_reply_rfc1048; 202 int echo_client_id; 203 204 struct tree_cache *options[256]; 205 }; 206 207 /* A dhcp host declaration structure. */ 208 struct host_decl { 209 struct host_decl *n_ipaddr; 210 char *name; 211 struct hardware interface; 212 struct tree_cache *fixed_addr; 213 struct group *group; 214 }; 215 216 struct shared_network { 217 struct shared_network *next; 218 char *name; 219 struct subnet *subnets; 220 struct interface_info *interface; 221 struct lease *leases; 222 struct lease *insertion_point; 223 struct lease *last_lease; 224 225 struct group *group; 226 }; 227 228 struct subnet { 229 struct subnet *next_subnet; 230 struct subnet *next_sibling; 231 struct shared_network *shared_network; 232 struct interface_info *interface; 233 struct iaddr interface_address; 234 struct iaddr net; 235 struct iaddr netmask; 236 237 struct group *group; 238 }; 239 240 struct class { 241 char *name; 242 243 struct group *group; 244 }; 245 246 /* privsep message. fixed length for easy parsing */ 247 struct pf_cmd { 248 struct in_addr ip; 249 u_int32_t type; 250 }; 251 252 /* Information about each network interface. */ 253 254 struct interface_info { 255 struct interface_info *next; /* Next interface in list... */ 256 struct shared_network *shared_network; 257 /* Networks connected to this interface. */ 258 struct hardware hw_address; /* Its physical address. */ 259 struct in_addr primary_address; /* Primary interface address. */ 260 char name[IFNAMSIZ]; /* Its name... */ 261 int rfdesc; /* Its read file descriptor. */ 262 int wfdesc; /* Its write file descriptor, if 263 different. */ 264 unsigned char *rbuf; /* Read buffer, if required. */ 265 size_t rbuf_max; /* Size of read buffer. */ 266 size_t rbuf_offset; /* Current offset into buffer. */ 267 size_t rbuf_len; /* Length of data in buffer. */ 268 269 struct ifreq *ifp; /* Pointer to ifreq struct. */ 270 271 int noifmedia; 272 int errors; 273 int dead; 274 u_int16_t index; 275 int is_udpsock; 276 ssize_t (*send_packet)(struct interface_info *, struct dhcp_packet *, 277 size_t, struct in_addr, struct sockaddr_in *, struct hardware *); 278 }; 279 280 struct dhcpd_timeout { 281 struct dhcpd_timeout *next; 282 time_t when; 283 void (*func)(void *); 284 void *what; 285 }; 286 287 struct protocol { 288 struct protocol *next; 289 int fd; 290 void (*handler)(struct protocol *); 291 void *local; 292 }; 293 294 #define _PATH_DHCPD_CONF "/etc/dhcpd.conf" 295 #define _PATH_DHCPD_DB "/var/db/dhcpd.leases" 296 #define _PATH_DEV_PF "/dev/pf" 297 #define DHCPD_LOG_FACILITY LOG_DAEMON 298 299 #define MAX_TIME 0x7fffffff 300 #define MIN_TIME 0 301 302 /* External definitions... */ 303 304 /* parse.c */ 305 extern int warnings_occurred; 306 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 307 2))); 308 309 /* options.c */ 310 void parse_options(struct packet *); 311 void parse_option_buffer(struct packet *, unsigned char *, int); 312 int cons_options(struct packet *, struct dhcp_packet *, int, 313 struct tree_cache **, int, int, int, u_int8_t *, int); 314 char *pretty_print_option(unsigned int, unsigned char *, int, int, int); 315 void do_packet(struct interface_info *, struct dhcp_packet *, int, 316 unsigned int, struct iaddr, struct hardware *); 317 318 /* dhcpd.c */ 319 extern time_t cur_time; 320 extern struct group root_group; 321 322 extern u_int16_t server_port; 323 extern u_int16_t client_port; 324 325 extern char *path_dhcpd_conf; 326 extern char *path_dhcpd_db; 327 328 int main(int, char *[]); 329 void cleanup(void); 330 void lease_pinged(struct iaddr, u_int8_t *, int); 331 void lease_ping_timeout(void *); 332 void periodic_scan(void *); 333 334 /* conflex.c */ 335 extern int lexline, lexchar; 336 extern char *token_line, *tlname; 337 extern char comments[4096]; 338 extern int comment_index; 339 extern int eol_token; 340 341 void new_parse(char *); 342 int next_token(char **, FILE *); 343 int peek_token(char **, FILE *); 344 345 /* confpars.c */ 346 int readconf(void); 347 void read_leases(void); 348 int parse_statement(FILE *, struct group *, int, struct host_decl *, int); 349 void parse_allow_deny(FILE *, struct group *, int); 350 void skip_to_semi(FILE *); 351 int parse_boolean(FILE *); 352 int parse_semi(FILE *); 353 int parse_lbrace(FILE *); 354 void parse_host_declaration(FILE *, struct group *); 355 char *parse_host_name(FILE *); 356 void parse_class_declaration(FILE *, struct group *, int); 357 void parse_lease_time(FILE *, time_t *); 358 void parse_shared_net_declaration(FILE *, struct group *); 359 void parse_subnet_declaration(FILE *, struct shared_network *); 360 void parse_group_declaration(FILE *, struct group *); 361 void parse_hardware_param(FILE *, struct hardware *); 362 char *parse_string(FILE *); 363 364 struct tree *parse_ip_addr_or_hostname(FILE *, int); 365 struct tree_cache *parse_fixed_addr_param(FILE *); 366 void parse_option_param(FILE *, struct group *); 367 time_t parse_timestamp(FILE *); 368 struct lease *parse_lease_declaration(FILE *); 369 void parse_address_range(FILE *, struct subnet *); 370 time_t parse_date(FILE *); 371 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, 372 int *, int, int, int); 373 void convert_num(unsigned char *, char *, int, int); 374 struct tree *parse_domain_and_comp(FILE *); 375 376 /* tree.c */ 377 pair cons(caddr_t, pair); 378 struct tree_cache *tree_cache(struct tree *); 379 struct tree *tree_host_lookup(char *); 380 struct dns_host_entry *enter_dns_host(char *); 381 struct tree *tree_const(unsigned char *, int); 382 struct tree *tree_concat(struct tree *, struct tree *); 383 struct tree *tree_limit(struct tree *, int); 384 int tree_evaluate(struct tree_cache *); 385 386 /* dhcp.c */ 387 extern int outstanding_pings; 388 389 void dhcp(struct packet *, int); 390 void dhcpdiscover(struct packet *); 391 void dhcprequest(struct packet *); 392 void dhcprelease(struct packet *); 393 void dhcpdecline(struct packet *); 394 void dhcpinform(struct packet *); 395 void nak_lease(struct packet *, struct iaddr *cip); 396 void ack_lease(struct packet *, struct lease *, unsigned int, time_t); 397 void dhcp_reply(struct lease *); 398 struct lease *find_lease(struct packet *, struct shared_network *, int *); 399 struct lease *mockup_lease(struct packet *, struct shared_network *, 400 struct host_decl *); 401 402 /* bootp.c */ 403 void bootp(struct packet *); 404 405 /* memory.c */ 406 void enter_host(struct host_decl *); 407 struct host_decl *find_hosts_by_haddr(int, unsigned char *, int); 408 struct host_decl *find_hosts_by_uid(unsigned char *, int); 409 struct subnet *find_host_for_network(struct host_decl **, struct iaddr *, 410 struct shared_network *); 411 void new_address_range(struct iaddr, struct iaddr, struct subnet *, int); 412 extern struct subnet *find_grouped_subnet(struct shared_network *, 413 struct iaddr); 414 extern struct subnet *find_subnet(struct iaddr); 415 void enter_shared_network(struct shared_network *); 416 int subnet_inner_than(struct subnet *, struct subnet *, int); 417 void enter_subnet(struct subnet *); 418 void enter_lease(struct lease *); 419 int supersede_lease(struct lease *, struct lease *, int); 420 void release_lease(struct lease *); 421 void abandon_lease(struct lease *, char *); 422 struct lease *find_lease_by_uid(unsigned char *, int); 423 struct lease *find_lease_by_hw_addr(unsigned char *, int); 424 struct lease *find_lease_by_ip_addr(struct iaddr); 425 void uid_hash_add(struct lease *); 426 void uid_hash_delete(struct lease *); 427 void hw_hash_add(struct lease *); 428 void hw_hash_delete(struct lease *); 429 struct class *add_class(int, char *); 430 struct class *find_class(int, unsigned char *, int); 431 struct group *clone_group(struct group *, char *); 432 void write_leases(void); 433 434 /* alloc.c */ 435 struct tree_cache *new_tree_cache(char *); 436 struct lease_state *new_lease_state(char *); 437 void free_lease_state(struct lease_state *, char *); 438 void free_tree_cache(struct tree_cache *); 439 440 /* print.c */ 441 char *print_hw_addr(int, int, unsigned char *); 442 443 /* bpf.c */ 444 int if_register_bpf(struct interface_info *); 445 void if_register_send(struct interface_info *); 446 void if_register_receive(struct interface_info *); 447 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, 448 struct sockaddr_in *, struct hardware *); 449 450 /* dispatch.c */ 451 extern struct interface_info *interfaces; 452 extern struct protocol *protocols; 453 extern struct dhcpd_timeout *timeouts; 454 void discover_interfaces(int *); 455 void dispatch(void); 456 int locate_network(struct packet *); 457 void got_one(struct protocol *); 458 void add_timeout(time_t, void (*)(void *), void *); 459 void cancel_timeout(void (*)(void *), void *); 460 void add_protocol (char *, int, void (*)(struct protocol *), void *); 461 void remove_protocol(struct protocol *); 462 463 /* hash.c */ 464 struct hash_table *new_hash(void); 465 void add_hash(struct hash_table *, unsigned char *, int, unsigned char *); 466 void delete_hash_entry(struct hash_table *, unsigned char *, int); 467 unsigned char *hash_lookup(struct hash_table *, unsigned char *, int); 468 469 /* tables.c */ 470 extern struct option dhcp_options[256]; 471 extern unsigned char dhcp_option_default_priority_list[256]; 472 extern char *hardware_types[256]; 473 extern struct hash_table universe_hash; 474 extern struct universe dhcp_universe; 475 void initialize_universes(void); 476 477 /* convert.c */ 478 u_int32_t getULong(unsigned char *); 479 int32_t getLong(unsigned char *); 480 u_int16_t getUShort(unsigned char *); 481 int16_t getShort(unsigned char *); 482 void putULong(unsigned char *, u_int32_t); 483 void putLong(unsigned char *, int32_t); 484 void putUShort(unsigned char *, unsigned int); 485 void putShort(unsigned char *, int); 486 487 /* inet.c */ 488 struct iaddr subnet_number(struct iaddr, struct iaddr); 489 struct iaddr ip_addr(struct iaddr, struct iaddr, u_int32_t); 490 u_int32_t host_addr(struct iaddr, struct iaddr); 491 int addr_eq(struct iaddr, struct iaddr); 492 char *piaddr(struct iaddr); 493 494 /* db.c */ 495 int write_lease(struct lease *); 496 int commit_leases(void); 497 void db_startup(void); 498 void new_lease_file(void); 499 500 /* packet.c */ 501 void assemble_hw_header(struct interface_info *, unsigned char *, 502 int *, struct hardware *); 503 void assemble_udp_ip_header(struct interface_info *, unsigned char *, 504 int *, u_int32_t, u_int32_t, unsigned int, unsigned char *, int); 505 ssize_t decode_hw_header(unsigned char *, u_int32_t, struct hardware *); 506 ssize_t decode_udp_ip_header(unsigned char *, u_int32_t, struct sockaddr_in *); 507 u_int32_t checksum(unsigned char *, u_int32_t, u_int32_t); 508 u_int32_t wrapsum(u_int32_t); 509 510 /* icmp.c */ 511 void icmp_startup(int, void (*)(struct iaddr, u_int8_t *, int)); 512 int icmp_echorequest(struct iaddr *); 513 void icmp_echoreply(struct protocol *); 514 515 /* pfutils.c */ 516 __dead void pftable_handler(void); 517 void pf_change_table(int, int, struct in_addr, char *); 518 void pf_kill_state(int, struct in_addr); 519 size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); 520 #define vwrite (ssize_t (*)(int, void *, size_t))write 521 void pfmsg(char, struct lease *); 522 523 /* udpsock.c */ 524 void udpsock_startup(struct in_addr); 525