1 /* $OpenBSD: dhcpd.h,v 1.33 2004/05/06 22:29:15 deraadt Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> 7 * Copyright (c) 1995, 1996, 1997, 1998, 1999 8 * The Internet Software Consortium. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of The Internet Software Consortium nor the names 20 * of its contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 31 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * This software has been written for the Internet Software Consortium 38 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 39 * Enterprises. To learn more about the Internet Software Consortium, 40 * see ``http://www.vix.com/isc''. To learn more about Vixie 41 * Enterprises, see ``http://www.vix.com''. 42 */ 43 44 #include <sys/param.h> 45 46 #include <sys/socket.h> 47 #include <sys/sockio.h> 48 #include <sys/stat.h> 49 #include <sys/time.h> 50 #include <sys/un.h> 51 #include <sys/wait.h> 52 53 #include <net/if.h> 54 #include <net/if_dl.h> 55 #include <net/route.h> 56 57 #ifndef WITHOUT_NETLINK 58 #include <netlink/netlink_snl.h> 59 #endif 60 61 #include <netinet/in.h> 62 #include <arpa/inet.h> 63 64 #include <ctype.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #include <libutil.h> 68 #include <limits.h> 69 #include <netdb.h> 70 #include <paths.h> 71 #include <unistd.h> 72 #include <stdarg.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <syslog.h> 77 #include <time.h> 78 #include <unistd.h> 79 80 #include <libcasper.h> 81 #include <casper/cap_syslog.h> 82 83 #include "dhcp.h" 84 #include "tree.h" 85 86 #define LOCAL_PORT 68 87 #define REMOTE_PORT 67 88 89 struct option_data { 90 size_t len; 91 u_int8_t *data; 92 }; 93 94 struct string_list { 95 struct string_list *next; 96 char *string; 97 }; 98 99 struct iaddr { 100 size_t len; 101 unsigned char iabuf[16]; 102 }; 103 104 struct iaddrlist { 105 struct iaddrlist *next; 106 struct iaddr addr; 107 }; 108 109 struct packet { 110 struct dhcp_packet *raw; 111 int packet_length; 112 int packet_type; 113 int options_valid; 114 int client_port; 115 struct iaddr client_addr; 116 struct interface_info *interface; 117 struct hardware *haddr; 118 struct option_data options[256]; 119 }; 120 121 struct hardware { 122 u_int8_t htype; 123 u_int8_t hlen; 124 u_int8_t haddr[16]; 125 }; 126 127 struct client_lease { 128 struct client_lease *next; 129 time_t expiry, renewal, rebind; 130 struct iaddr address; 131 struct iaddr nextserver; 132 char *server_name; 133 char *filename; 134 struct string_list *medium; 135 unsigned int is_static : 1; 136 unsigned int is_bootp : 1; 137 struct option_data options[256]; 138 }; 139 140 /* Possible states in which the client can be. */ 141 enum dhcp_state { 142 S_REBOOTING, 143 S_INIT, 144 S_SELECTING, 145 S_REQUESTING, 146 S_BOUND, 147 S_RENEWING, 148 S_REBINDING 149 }; 150 151 struct client_config { 152 struct option_data defaults[256]; 153 enum { 154 ACTION_DEFAULT, 155 ACTION_SUPERSEDE, 156 ACTION_PREPEND, 157 ACTION_APPEND 158 } default_actions[256]; 159 160 struct option_data send_options[256]; 161 u_int8_t required_options[256]; 162 u_int8_t requested_options[256]; 163 int requested_option_count; 164 u_int8_t ignored_options[256]; 165 u_int vlan_pcp; 166 time_t timeout; 167 time_t initial_interval; 168 time_t retry_interval; 169 time_t select_interval; 170 time_t reboot_timeout; 171 time_t backoff_cutoff; 172 struct string_list *media; 173 char *script_name; 174 enum { IGNORE, ACCEPT, PREFER } 175 bootp_policy; 176 struct string_list *medium; 177 struct iaddrlist *reject_list; 178 }; 179 180 struct client_state { 181 struct client_lease *active; 182 struct client_lease *new; 183 struct client_lease *offered_leases; 184 struct client_lease *leases; 185 struct client_lease *alias; 186 enum dhcp_state state; 187 struct iaddr destination; 188 u_int32_t xid; 189 u_int16_t secs; 190 time_t first_sending; 191 time_t interval; 192 struct string_list *medium; 193 struct dhcp_packet packet; 194 int packet_length; 195 struct iaddr requested_address; 196 struct client_config *config; 197 char **scriptEnv; 198 int scriptEnvsize; 199 struct string_list *env; 200 int envc; 201 }; 202 203 struct interface_info { 204 struct interface_info *next; 205 struct hardware hw_address; 206 char name[IFNAMSIZ]; 207 int rfdesc; 208 int wfdesc; 209 int ufdesc; 210 unsigned char *rbuf; 211 size_t rbuf_max; 212 size_t rbuf_offset; 213 size_t rbuf_len; 214 struct ifreq *ifp; 215 struct client_state *client; 216 int noifmedia; 217 int errors; 218 int dead; 219 u_int16_t index; 220 int linkstat; 221 }; 222 223 struct timeout { 224 struct timeout *next; 225 struct timespec when; 226 void (*func)(void *); 227 void *what; 228 }; 229 230 struct protocol { 231 struct protocol *next; 232 int fd; 233 void (*handler)(struct protocol *); 234 void *local; 235 }; 236 237 #define DEFAULT_HASH_SIZE 97 238 239 struct hash_bucket { 240 struct hash_bucket *next; 241 const unsigned char *name; 242 int len; 243 unsigned char *value; 244 }; 245 246 struct hash_table { 247 int hash_count; 248 struct hash_bucket *buckets[DEFAULT_HASH_SIZE]; 249 }; 250 251 /* Default path to dhcpd config file. */ 252 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf" 253 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases" 254 #define DHCPD_LOG_FACILITY LOG_DAEMON 255 256 #define MAX_TIME 0x7fffffff 257 #define MIN_TIME 0 258 259 /* External definitions... */ 260 261 /* options.c */ 262 int cons_options(struct packet *, struct dhcp_packet *, int, 263 struct tree_cache **, int, int, int, u_int8_t *, int); 264 const char *pretty_print_option(unsigned int, 265 unsigned char *, int, int, int); 266 void do_packet(struct interface_info *, struct dhcp_packet *, 267 int, unsigned int, struct iaddr, struct hardware *); 268 269 /* errwarn.c */ 270 extern int warnings_occurred; 271 void error(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))) __dead2; 272 int warning(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 273 int note(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 274 int debug(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 275 int parse_warn(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 276 277 /* conflex.c */ 278 extern int lexline, lexchar; 279 extern char *token_line; 280 extern const char *tlname; 281 extern char comments[4096]; 282 extern int comment_index; 283 extern int eol_token; 284 void new_parse(const char *); 285 int next_token(char **, FILE *); 286 int peek_token(char **, FILE *); 287 288 /* parse.c */ 289 void skip_to_semi(FILE *); 290 int parse_semi(FILE *); 291 char *parse_string(FILE *); 292 int parse_ip_addr(FILE *, struct iaddr *); 293 void parse_hardware_param(FILE *, struct hardware *); 294 void parse_lease_time(FILE *, time_t *); 295 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, size_t *, 296 int, unsigned, int); 297 void convert_num(unsigned char *, char *, unsigned, int); 298 time_t parse_date(FILE *); 299 300 /* tree.c */ 301 pair cons(caddr_t, pair); 302 303 /* alloc.c */ 304 struct string_list *new_string_list(size_t size); 305 struct hash_table *new_hash_table(int); 306 struct hash_bucket *new_hash_bucket(void); 307 308 /* bpf.c */ 309 int if_register_bpf(struct interface_info *, int); 310 void if_register_send(struct interface_info *); 311 void if_register_receive(struct interface_info *); 312 void send_packet_unpriv(int, struct dhcp_packet *, size_t, struct in_addr, 313 struct in_addr); 314 struct imsg_hdr; 315 void send_packet_priv(struct interface_info *, struct imsg_hdr *, int); 316 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, 317 struct sockaddr_in *, struct hardware *); 318 319 /* dispatch.c */ 320 extern void (*bootp_packet_handler)(struct interface_info *, 321 struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *); 322 void discover_interfaces(struct interface_info *); 323 void reinitialize_interfaces(void); 324 void dispatch(void); 325 void got_one(struct protocol *); 326 void add_timeout(time_t, void (*)(void *), void *); 327 void add_timeout_timespec(struct timespec, void (*)(void *), void *); 328 void cancel_timeout(void (*)(void *), void *); 329 void add_protocol(const char *, int, void (*)(struct protocol *), void *); 330 void remove_protocol(struct protocol *); 331 int interface_link_status(char *); 332 void interface_set_mtu_unpriv(int, u_int16_t); 333 void interface_set_mtu_priv(char *, u_int16_t); 334 335 /* hash.c */ 336 struct hash_table *new_hash(void); 337 void add_hash(struct hash_table *, const unsigned char *, int, unsigned char *); 338 void *hash_lookup(struct hash_table *, unsigned char *, int); 339 340 /* tables.c */ 341 extern struct option dhcp_options[256]; 342 extern unsigned char dhcp_option_default_priority_list[]; 343 extern int sizeof_dhcp_option_default_priority_list; 344 extern struct hash_table universe_hash; 345 extern struct universe dhcp_universe; 346 void initialize_universes(void); 347 348 /* convert.c */ 349 u_int32_t getULong(unsigned char *); 350 int32_t getLong(unsigned char *); 351 u_int16_t getUShort(unsigned char *); 352 int16_t getShort(unsigned char *); 353 void putULong(unsigned char *, u_int32_t); 354 void putLong(unsigned char *, int32_t); 355 void putUShort(unsigned char *, unsigned int); 356 void putShort(unsigned char *, int); 357 358 /* inet.c */ 359 struct iaddr subnet_number(struct iaddr, struct iaddr); 360 struct iaddr broadcast_addr(struct iaddr, struct iaddr); 361 int addr_eq(struct iaddr, struct iaddr); 362 char *piaddr(struct iaddr); 363 364 /* inet6.c */ 365 bool check_ipv6_connectivity(uint16_t ifindex); 366 367 /* dhclient.c */ 368 extern cap_channel_t *capsyslog; 369 extern const char *path_dhclient_conf; 370 extern char *path_dhclient_db; 371 extern struct timespec time_now; /* CLOCK_MONOTONIC */ 372 extern time_t cur_time; /* Seconds since epoch */ 373 extern int log_priority; 374 extern int log_perror; 375 376 extern struct client_config top_level_config; 377 378 extern struct pidfh *pidfile; 379 380 extern struct interface_info *ifi; 381 382 #ifndef WITHOUT_NETLINK 383 extern struct snl_state nl_ss; 384 #endif 385 386 void dhcpoffer(struct packet *); 387 void dhcpack(struct packet *); 388 void dhcpnak(struct packet *); 389 390 void send_discover(void *); 391 void send_request(void *); 392 void send_decline(void *); 393 394 void state_reboot(void *); 395 void state_init(void *); 396 void state_selecting(void *); 397 void state_requesting(void *); 398 void state_bound(void *); 399 void state_panic(void *); 400 401 void bind_lease(struct interface_info *); 402 403 void make_discover(struct interface_info *, struct client_lease *); 404 void make_request(struct interface_info *, struct client_lease *); 405 void make_decline(struct interface_info *, struct client_lease *); 406 407 void free_client_lease(struct client_lease *); 408 void rewrite_client_leases(void); 409 void write_client_lease(struct interface_info *, struct client_lease *, int); 410 411 void priv_script_init(const char *, char *); 412 void priv_script_write_params(const char *, struct client_lease *); 413 int priv_script_go(void); 414 415 void script_init(const char *, struct string_list *); 416 void script_write_params(const char *, struct client_lease *); 417 int script_go(void); 418 void client_envadd(struct client_state *, 419 const char *, const char *, const char *, ...); 420 void script_set_env(struct client_state *, const char *, const char *, 421 const char *); 422 void script_flush_env(struct client_state *); 423 int dhcp_option_ev_name(char *, size_t, struct option *); 424 425 struct client_lease *packet_to_lease(struct packet *); 426 void go_daemon(void); 427 void client_location_changed(void); 428 429 void bootp(struct packet *); 430 void dhcp(struct packet *); 431 432 /* packet.c */ 433 void assemble_hw_header(struct interface_info *, unsigned char *, int *); 434 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t, 435 unsigned int, unsigned char *, int); 436 ssize_t decode_hw_header(unsigned char *, int, struct hardware *); 437 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *, 438 unsigned char *, int); 439 440 /* clparse.c */ 441 int read_client_conf(void); 442 void read_client_leases(void); 443 void parse_client_statement(FILE *, struct interface_info *, 444 struct client_config *); 445 unsigned parse_X(FILE *, u_int8_t *, unsigned); 446 int parse_option_list(FILE *, u_int8_t *); 447 void parse_interface_declaration(FILE *, struct client_config *); 448 struct interface_info *interface_or_dummy(char *); 449 void make_client_state(struct interface_info *); 450 void make_client_config(struct interface_info *, struct client_config *); 451 void parse_client_lease_statement(FILE *, int); 452 void parse_client_lease_declaration(FILE *, struct client_lease *, 453 struct interface_info **); 454 struct option *parse_option_decl(FILE *, struct option_data *); 455 void parse_string_list(FILE *, struct string_list **, int); 456 void parse_reject_statement(FILE *, struct client_config *); 457 458 /* privsep.c */ 459 struct buf *buf_open(size_t); 460 int buf_add(struct buf *, const void *, size_t); 461 int buf_close(int, struct buf *); 462 ssize_t buf_read(int, void *, size_t); 463 void dispatch_imsg(struct interface_info *, int); 464