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