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