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#include <netinet/in.h> 58#include <arpa/inet.h> 59 60#include <ctype.h> 61#include <errno.h> 62#include <fcntl.h> 63#include <libutil.h> 64#include <limits.h> 65#include <netdb.h> 66#include <paths.h> 67#include <unistd.h> 68#include <stdarg.h> 69#include <stdio.h> 70#include <stdlib.h> 71#include <string.h> 72#include <syslog.h> 73#include <time.h> 74#include <unistd.h> 75 76#include <libcasper.h> 77#include <casper/cap_syslog.h> 78 79#include "dhcp.h" 80#include "tree.h" 81 82#define LOCAL_PORT 68 83#define REMOTE_PORT 67 84 85struct option_data { 86 size_t len; 87 u_int8_t *data; 88}; 89 90struct string_list { 91 struct string_list *next; 92 char *string; 93}; 94 95struct iaddr { 96 size_t len; 97 unsigned char iabuf[16]; 98}; 99 100struct iaddrlist { 101 struct iaddrlist *next; 102 struct iaddr addr; 103}; 104 105struct packet { 106 struct dhcp_packet *raw; 107 int packet_length; 108 int packet_type; 109 int options_valid; 110 int client_port; 111 struct iaddr client_addr; 112 struct interface_info *interface; 113 struct hardware *haddr; 114 struct option_data options[256]; 115}; 116 117struct hardware { 118 u_int8_t htype; 119 u_int8_t hlen; 120 u_int8_t haddr[16]; 121}; 122 123struct client_lease { 124 struct client_lease *next; 125 time_t expiry, renewal, rebind; 126 struct iaddr address; 127 struct iaddr nextserver; 128 char *server_name; 129 char *filename; 130 struct string_list *medium; 131 unsigned int is_static : 1; 132 unsigned int is_bootp : 1; 133 struct option_data options[256]; 134}; 135 136/* Possible states in which the client can be. */ 137enum dhcp_state { 138 S_REBOOTING, 139 S_INIT, 140 S_SELECTING, 141 S_REQUESTING, 142 S_BOUND, 143 S_RENEWING, 144 S_REBINDING 145}; 146 147struct client_config { 148 struct option_data defaults[256]; 149 enum { 150 ACTION_DEFAULT, 151 ACTION_SUPERSEDE, 152 ACTION_PREPEND, 153 ACTION_APPEND 154 } default_actions[256]; 155 156 struct option_data send_options[256]; 157 u_int8_t required_options[256]; 158 u_int8_t requested_options[256]; 159 int requested_option_count; 160 u_int8_t ignored_options[256]; 161 u_int vlan_pcp; 162 time_t timeout; 163 time_t initial_interval; 164 time_t retry_interval; 165 time_t select_interval; 166 time_t reboot_timeout; 167 time_t backoff_cutoff; 168 struct string_list *media; 169 char *script_name; 170 enum { IGNORE, ACCEPT, PREFER } 171 bootp_policy; 172 struct string_list *medium; 173 struct iaddrlist *reject_list; 174}; 175 176struct client_state { 177 struct client_lease *active; 178 struct client_lease *new; 179 struct client_lease *offered_leases; 180 struct client_lease *leases; 181 struct client_lease *alias; 182 enum dhcp_state state; 183 struct iaddr destination; 184 u_int32_t xid; 185 u_int16_t secs; 186 time_t first_sending; 187 time_t interval; 188 struct string_list *medium; 189 struct dhcp_packet packet; 190 int packet_length; 191 struct iaddr requested_address; 192 struct client_config *config; 193 char **scriptEnv; 194 int scriptEnvsize; 195 struct string_list *env; 196 int envc; 197}; 198 199struct interface_info { 200 struct interface_info *next; 201 struct hardware hw_address; 202 char name[IFNAMSIZ]; 203 int rfdesc; 204 int wfdesc; 205 int ufdesc; 206 unsigned char *rbuf; 207 size_t rbuf_max; 208 size_t rbuf_offset; 209 size_t rbuf_len; 210 struct ifreq *ifp; 211 struct client_state *client; 212 int noifmedia; 213 int errors; 214 int dead; 215 u_int16_t index; 216 int linkstat; 217}; 218 219struct timeout { 220 struct timeout *next; 221 time_t when; 222 void (*func)(void *); 223 void *what; 224}; 225 226struct protocol { 227 struct protocol *next; 228 int fd; 229 void (*handler)(struct protocol *); 230 void *local; 231}; 232 233#define DEFAULT_HASH_SIZE 97 234 235struct hash_bucket { 236 struct hash_bucket *next; 237 const unsigned char *name; 238 int len; 239 unsigned char *value; 240}; 241 242struct hash_table { 243 int hash_count; 244 struct hash_bucket *buckets[DEFAULT_HASH_SIZE]; 245}; 246 247/* Default path to dhcpd config file. */ 248#define _PATH_DHCLIENT_CONF "/etc/dhclient.conf" 249#define _PATH_DHCLIENT_DB "/var/db/dhclient.leases" 250#define DHCPD_LOG_FACILITY LOG_DAEMON 251 252#define MAX_TIME 0x7fffffff 253#define MIN_TIME 0 254 255/* External definitions... */ 256 257/* options.c */ 258int cons_options(struct packet *, struct dhcp_packet *, int, 259 struct tree_cache **, int, int, int, u_int8_t *, int); 260const char *pretty_print_option(unsigned int, 261 unsigned char *, int, int, int); 262void do_packet(struct interface_info *, struct dhcp_packet *, 263 int, unsigned int, struct iaddr, struct hardware *); 264 265/* errwarn.c */ 266extern int warnings_occurred; 267void error(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))) __dead2; 268int warning(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 269int note(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 270int debug(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 271int parse_warn(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 272 273/* conflex.c */ 274extern int lexline, lexchar; 275extern char *token_line; 276extern const char *tlname; 277extern char comments[4096]; 278extern int comment_index; 279extern int eol_token; 280void new_parse(const char *); 281int next_token(char **, FILE *); 282int peek_token(char **, FILE *); 283 284/* parse.c */ 285void skip_to_semi(FILE *); 286int parse_semi(FILE *); 287char *parse_string(FILE *); 288int parse_ip_addr(FILE *, struct iaddr *); 289void parse_hardware_param(FILE *, struct hardware *); 290void parse_lease_time(FILE *, time_t *); 291unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, size_t *, 292 int, unsigned, int); 293void convert_num(unsigned char *, char *, unsigned, int); 294time_t parse_date(FILE *); 295 296/* tree.c */ 297pair cons(caddr_t, pair); 298 299/* alloc.c */ 300struct string_list *new_string_list(size_t size); 301struct hash_table *new_hash_table(int); 302struct hash_bucket *new_hash_bucket(void); 303 304/* bpf.c */ 305int if_register_bpf(struct interface_info *, int); 306void if_register_send(struct interface_info *); 307void if_register_receive(struct interface_info *); 308void send_packet_unpriv(int, struct dhcp_packet *, size_t, struct in_addr, 309 struct in_addr); 310struct imsg_hdr; 311void send_packet_priv(struct interface_info *, struct imsg_hdr *, int); 312ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, 313 struct sockaddr_in *, struct hardware *); 314 315/* dispatch.c */ 316extern void (*bootp_packet_handler)(struct interface_info *, 317 struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *); 318void discover_interfaces(struct interface_info *); 319void reinitialize_interfaces(void); 320void dispatch(void); 321void got_one(struct protocol *); 322void add_timeout(time_t, void (*)(void *), void *); 323void cancel_timeout(void (*)(void *), void *); 324void add_protocol(const char *, int, void (*)(struct protocol *), void *); 325void remove_protocol(struct protocol *); 326int interface_link_status(char *); 327void interface_set_mtu_unpriv(int, u_int16_t); 328void interface_set_mtu_priv(char *, u_int16_t); 329 330/* hash.c */ 331struct hash_table *new_hash(void); 332void add_hash(struct hash_table *, const unsigned char *, int, unsigned char *); 333void *hash_lookup(struct hash_table *, unsigned char *, int); 334 335/* tables.c */ 336extern struct option dhcp_options[256]; 337extern unsigned char dhcp_option_default_priority_list[]; 338extern int sizeof_dhcp_option_default_priority_list; 339extern struct hash_table universe_hash; 340extern struct universe dhcp_universe; 341void initialize_universes(void); 342 343/* convert.c */ 344u_int32_t getULong(unsigned char *); 345int32_t getLong(unsigned char *); 346u_int16_t getUShort(unsigned char *); 347int16_t getShort(unsigned char *); 348void putULong(unsigned char *, u_int32_t); 349void putLong(unsigned char *, int32_t); 350void putUShort(unsigned char *, unsigned int); 351void putShort(unsigned char *, int); 352 353/* inet.c */ 354struct iaddr subnet_number(struct iaddr, struct iaddr); 355struct iaddr broadcast_addr(struct iaddr, struct iaddr); 356int addr_eq(struct iaddr, struct iaddr); 357char *piaddr(struct iaddr); 358 359/* dhclient.c */ 360extern cap_channel_t *capsyslog; 361extern const char *path_dhclient_conf; 362extern char *path_dhclient_db; 363extern time_t cur_time; 364extern int log_priority; 365extern int log_perror; 366 367extern struct client_config top_level_config; 368 369extern struct pidfh *pidfile; 370 371extern struct interface_info *ifi; 372 373void dhcpoffer(struct packet *); 374void dhcpack(struct packet *); 375void dhcpnak(struct packet *); 376 377void send_discover(void *); 378void send_request(void *); 379void send_decline(void *); 380 381void state_reboot(void *); 382void state_init(void *); 383void state_selecting(void *); 384void state_requesting(void *); 385void state_bound(void *); 386void state_panic(void *); 387 388void bind_lease(struct interface_info *); 389 390void make_discover(struct interface_info *, struct client_lease *); 391void make_request(struct interface_info *, struct client_lease *); 392void make_decline(struct interface_info *, struct client_lease *); 393 394void free_client_lease(struct client_lease *); 395void rewrite_client_leases(void); 396void write_client_lease(struct interface_info *, struct client_lease *, int); 397 398void priv_script_init(const char *, char *); 399void priv_script_write_params(const char *, struct client_lease *); 400int priv_script_go(void); 401 402void script_init(const char *, struct string_list *); 403void script_write_params(const char *, struct client_lease *); 404int script_go(void); 405void client_envadd(struct client_state *, 406 const char *, const char *, const char *, ...); 407void script_set_env(struct client_state *, const char *, const char *, 408 const char *); 409void script_flush_env(struct client_state *); 410int dhcp_option_ev_name(char *, size_t, struct option *); 411 412struct client_lease *packet_to_lease(struct packet *); 413void go_daemon(void); 414void client_location_changed(void); 415 416void bootp(struct packet *); 417void dhcp(struct packet *); 418 419/* packet.c */ 420void assemble_hw_header(struct interface_info *, unsigned char *, int *); 421void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t, 422 unsigned int, unsigned char *, int); 423ssize_t decode_hw_header(unsigned char *, int, struct hardware *); 424ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *, 425 unsigned char *, int); 426 427/* clparse.c */ 428int read_client_conf(void); 429void read_client_leases(void); 430void parse_client_statement(FILE *, struct interface_info *, 431 struct client_config *); 432unsigned parse_X(FILE *, u_int8_t *, unsigned); 433int parse_option_list(FILE *, u_int8_t *); 434void parse_interface_declaration(FILE *, struct client_config *); 435struct interface_info *interface_or_dummy(char *); 436void make_client_state(struct interface_info *); 437void make_client_config(struct interface_info *, struct client_config *); 438void parse_client_lease_statement(FILE *, int); 439void parse_client_lease_declaration(FILE *, struct client_lease *, 440 struct interface_info **); 441struct option *parse_option_decl(FILE *, struct option_data *); 442void parse_string_list(FILE *, struct string_list **, int); 443void parse_reject_statement(FILE *, struct client_config *); 444 445/* privsep.c */ 446struct buf *buf_open(size_t); 447int buf_add(struct buf *, const void *, size_t); 448int buf_close(int, struct buf *); 449ssize_t buf_read(int, void *, size_t); 450void dispatch_imsg(struct interface_info *, int); 451