1 /* $OpenBSD: dhcpd.h,v 1.69 2024/05/21 05:00:48 jsg 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 void do_packet(struct interface_info *, struct dhcp_packet *, int, 315 unsigned int, struct iaddr, struct hardware *); 316 317 /* dhcpd.c */ 318 extern time_t cur_time; 319 extern struct group root_group; 320 321 extern u_int16_t server_port; 322 extern u_int16_t client_port; 323 324 extern char *path_dhcpd_conf; 325 extern char *path_dhcpd_db; 326 327 int main(int, char *[]); 328 void lease_pinged(struct iaddr, u_int8_t *, int); 329 void lease_ping_timeout(void *); 330 void periodic_scan(void *); 331 332 /* conflex.c */ 333 extern int lexline, lexchar; 334 extern char *token_line, *tlname; 335 extern int eol_token; 336 337 void new_parse(char *); 338 int next_token(char **, FILE *); 339 int peek_token(char **, FILE *); 340 341 /* confpars.c */ 342 int readconf(void); 343 void read_leases(void); 344 int parse_statement(FILE *, struct group *, int, struct host_decl *, int); 345 void parse_allow_deny(FILE *, struct group *, int); 346 void skip_to_semi(FILE *); 347 int parse_boolean(FILE *); 348 int parse_semi(FILE *); 349 int parse_lbrace(FILE *); 350 void parse_host_declaration(FILE *, struct group *); 351 char *parse_host_name(FILE *); 352 void parse_class_declaration(FILE *, struct group *, int); 353 void parse_lease_time(FILE *, time_t *); 354 void parse_shared_net_declaration(FILE *, struct group *); 355 void parse_subnet_declaration(FILE *, struct shared_network *); 356 void parse_group_declaration(FILE *, struct group *); 357 void parse_hardware_param(FILE *, struct hardware *); 358 char *parse_string(FILE *); 359 360 struct tree *parse_ip_addr_or_hostname(FILE *, int); 361 struct tree_cache *parse_fixed_addr_param(FILE *); 362 void parse_option_param(FILE *, struct group *); 363 struct lease *parse_lease_declaration(FILE *); 364 void parse_address_range(FILE *, struct subnet *); 365 time_t parse_date(FILE *); 366 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, 367 int *, int, int, int); 368 void convert_num(unsigned char *, char *, int, int); 369 struct tree *parse_domain_and_comp(FILE *); 370 371 /* tree.c */ 372 pair cons(caddr_t, pair); 373 struct tree_cache *tree_cache(struct tree *); 374 struct tree *tree_const(unsigned char *, int); 375 struct tree *tree_concat(struct tree *, struct tree *); 376 struct tree *tree_limit(struct tree *, int); 377 int tree_evaluate(struct tree_cache *); 378 379 /* dhcp.c */ 380 extern int outstanding_pings; 381 382 void dhcp(struct packet *, int); 383 void dhcpdiscover(struct packet *); 384 void dhcprequest(struct packet *); 385 void dhcprelease(struct packet *); 386 void dhcpdecline(struct packet *); 387 void dhcpinform(struct packet *); 388 void nak_lease(struct packet *, struct iaddr *cip); 389 void ack_lease(struct packet *, struct lease *, unsigned int, time_t); 390 void dhcp_reply(struct lease *); 391 struct lease *find_lease(struct packet *, struct shared_network *, int *); 392 struct lease *mockup_lease(struct packet *, struct shared_network *, 393 struct host_decl *); 394 395 /* bootp.c */ 396 void bootp(struct packet *); 397 398 /* memory.c */ 399 void enter_host(struct host_decl *); 400 struct host_decl *find_hosts_by_haddr(int, unsigned char *, int); 401 struct host_decl *find_hosts_by_uid(unsigned char *, int); 402 struct subnet *find_host_for_network(struct host_decl **, struct iaddr *, 403 struct shared_network *); 404 void new_address_range(struct iaddr, struct iaddr, struct subnet *, int); 405 extern struct subnet *find_grouped_subnet(struct shared_network *, 406 struct iaddr); 407 extern struct subnet *find_subnet(struct iaddr); 408 void enter_shared_network(struct shared_network *); 409 int subnet_inner_than(struct subnet *, struct subnet *, int); 410 void enter_subnet(struct subnet *); 411 void enter_lease(struct lease *); 412 int supersede_lease(struct lease *, struct lease *, int); 413 void release_lease(struct lease *); 414 void abandon_lease(struct lease *, char *); 415 struct lease *find_lease_by_uid(unsigned char *, int); 416 struct lease *find_lease_by_hw_addr(unsigned char *, int); 417 struct lease *find_lease_by_ip_addr(struct iaddr); 418 void uid_hash_add(struct lease *); 419 void uid_hash_delete(struct lease *); 420 void hw_hash_add(struct lease *); 421 void hw_hash_delete(struct lease *); 422 struct class *add_class(int, char *); 423 struct class *find_class(int, unsigned char *, int); 424 struct group *clone_group(struct group *, char *); 425 void write_leases(void); 426 427 /* alloc.c */ 428 struct tree_cache *new_tree_cache(char *); 429 struct lease_state *new_lease_state(char *); 430 void free_lease_state(struct lease_state *, char *); 431 void free_tree_cache(struct tree_cache *); 432 433 /* print.c */ 434 char *print_hw_addr(int, int, unsigned char *); 435 436 /* bpf.c */ 437 int if_register_bpf(struct interface_info *); 438 void if_register_send(struct interface_info *); 439 void if_register_receive(struct interface_info *); 440 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, 441 struct sockaddr_in *, struct hardware *); 442 443 /* dispatch.c */ 444 extern struct interface_info *interfaces; 445 extern struct protocol *protocols; 446 extern struct dhcpd_timeout *timeouts; 447 void discover_interfaces(int *); 448 void dispatch(void); 449 int locate_network(struct packet *); 450 void got_one(struct protocol *); 451 void add_timeout(time_t, void (*)(void *), void *); 452 void cancel_timeout(void (*)(void *), void *); 453 void add_protocol (char *, int, void (*)(struct protocol *), void *); 454 void remove_protocol(struct protocol *); 455 456 /* hash.c */ 457 struct hash_table *new_hash(void); 458 void add_hash(struct hash_table *, unsigned char *, int, unsigned char *); 459 void delete_hash_entry(struct hash_table *, unsigned char *, int); 460 unsigned char *hash_lookup(struct hash_table *, unsigned char *, int); 461 462 /* tables.c */ 463 extern struct option dhcp_options[256]; 464 extern unsigned char dhcp_option_default_priority_list[256]; 465 extern char *hardware_types[256]; 466 extern struct hash_table universe_hash; 467 extern struct universe dhcp_universe; 468 void initialize_universes(void); 469 470 /* convert.c */ 471 u_int32_t getULong(unsigned char *); 472 u_int16_t getUShort(unsigned char *); 473 void putULong(unsigned char *, u_int32_t); 474 void putLong(unsigned char *, int32_t); 475 void putUShort(unsigned char *, unsigned int); 476 void putShort(unsigned char *, int); 477 478 /* inet.c */ 479 struct iaddr subnet_number(struct iaddr, struct iaddr); 480 struct iaddr ip_addr(struct iaddr, struct iaddr, u_int32_t); 481 u_int32_t host_addr(struct iaddr, struct iaddr); 482 int addr_eq(struct iaddr, struct iaddr); 483 char *piaddr(struct iaddr); 484 485 /* db.c */ 486 int write_lease(struct lease *); 487 int commit_leases(void); 488 void db_startup(void); 489 void new_lease_file(void); 490 491 /* packet.c */ 492 void assemble_hw_header(struct interface_info *, unsigned char *, 493 int *, struct hardware *); 494 void assemble_udp_ip_header(struct interface_info *, unsigned char *, 495 int *, u_int32_t, u_int32_t, unsigned int, unsigned char *, int); 496 ssize_t decode_hw_header(unsigned char *, u_int32_t, struct hardware *); 497 ssize_t decode_udp_ip_header(unsigned char *, u_int32_t, struct sockaddr_in *); 498 u_int32_t checksum(unsigned char *, u_int32_t, u_int32_t); 499 u_int32_t wrapsum(u_int32_t); 500 501 /* icmp.c */ 502 void icmp_startup(int, void (*)(struct iaddr, u_int8_t *, int)); 503 int icmp_echorequest(struct iaddr *); 504 void icmp_echoreply(struct protocol *); 505 506 /* pfutils.c */ 507 __dead void pftable_handler(void); 508 void pf_change_table(int, int, struct in_addr, char *); 509 void pf_kill_state(int, struct in_addr); 510 size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); 511 #define vwrite (ssize_t (*)(int, void *, size_t))write 512 void pfmsg(char, struct lease *); 513 514 /* udpsock.c */ 515 void udpsock_startup(struct in_addr); 516