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