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