1 /* $OpenBSD: dhcpd.h,v 1.33 2004/05/06 22:29:15 deraadt Exp $ */
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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''.
44 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
54 #include <net/if_dl.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
80 #define REMOTE_PORT 67
88 struct string_list *next;
94 unsigned char iabuf[16];
98 struct iaddrlist *next;
103 struct dhcp_packet *raw;
108 struct iaddr client_addr;
109 struct interface_info *interface;
110 struct hardware *haddr;
111 struct option_data options[256];
120 struct client_lease {
121 struct client_lease *next;
122 time_t expiry, renewal, rebind;
123 struct iaddr address;
124 struct iaddr nextserver;
127 struct string_list *medium;
128 unsigned int is_static : 1;
129 unsigned int is_bootp : 1;
130 struct option_data options[256];
133 /* Possible states in which the client can be. */
144 struct client_config {
145 struct option_data defaults[256];
151 } default_actions[256];
153 struct option_data send_options[256];
154 u_int8_t required_options[256];
155 u_int8_t requested_options[256];
156 int requested_option_count;
158 time_t initial_interval;
159 time_t retry_interval;
160 time_t select_interval;
161 time_t reboot_timeout;
162 time_t backoff_cutoff;
163 struct string_list *media;
165 enum { IGNORE, ACCEPT, PREFER }
167 struct string_list *medium;
168 struct iaddrlist *reject_list;
171 struct client_state {
172 struct client_lease *active;
173 struct client_lease *new;
174 struct client_lease *offered_leases;
175 struct client_lease *leases;
176 struct client_lease *alias;
177 enum dhcp_state state;
178 struct iaddr destination;
181 time_t first_sending;
183 struct string_list *medium;
184 struct dhcp_packet packet;
186 struct iaddr requested_address;
187 struct client_config *config;
190 struct string_list *env;
194 struct interface_info {
195 struct interface_info *next;
196 struct hardware hw_address;
197 struct in_addr primary_address;
207 struct client_state *client;
216 struct timeout *next;
218 void (*func)(void *);
223 struct protocol *next;
225 void (*handler)(struct protocol *);
229 #define DEFAULT_HASH_SIZE 97
232 struct hash_bucket *next;
233 const unsigned char *name;
235 unsigned char *value;
240 struct hash_bucket *buckets[DEFAULT_HASH_SIZE];
243 /* Default path to dhcpd config file. */
244 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
245 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
246 #define DHCPD_LOG_FACILITY LOG_DAEMON
248 #define MAX_TIME 0x7fffffff
251 /* External definitions... */
254 int cons_options(struct packet *, struct dhcp_packet *, int,
255 struct tree_cache **, int, int, int, u_int8_t *, int);
256 const char *pretty_print_option(unsigned int,
257 unsigned char *, int, int, int);
258 void do_packet(struct interface_info *, struct dhcp_packet *,
259 int, unsigned int, struct iaddr, struct hardware *);
262 extern int warnings_occurred;
263 void error(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
264 int warning(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
265 int note(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
266 int debug(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
267 int parse_warn(const char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
270 extern int lexline, lexchar;
271 extern char *token_line;
272 extern const char *tlname;
273 extern char comments[4096];
274 extern int comment_index;
275 extern int eol_token;
276 void new_parse(const char *);
277 int next_token(char **, FILE *);
278 int peek_token(char **, FILE *);
281 void skip_to_semi(FILE *);
282 int parse_semi(FILE *);
283 char *parse_string(FILE *);
284 int parse_ip_addr(FILE *, struct iaddr *);
285 void parse_hardware_param(FILE *, struct hardware *);
286 void parse_lease_time(FILE *, time_t *);
287 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, size_t *,
289 void convert_num(unsigned char *, char *, unsigned, int);
290 time_t parse_date(FILE *);
293 pair cons(caddr_t, pair);
296 struct string_list *new_string_list(size_t size);
297 struct hash_table *new_hash_table(int);
298 struct hash_bucket *new_hash_bucket(void);
301 int if_register_bpf(struct interface_info *, int);
302 void if_register_send(struct interface_info *);
303 void if_register_receive(struct interface_info *);
304 void send_packet_unpriv(int, struct dhcp_packet *, size_t, struct in_addr,
307 void send_packet_priv(struct interface_info *, struct imsg_hdr *, int);
308 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
309 struct sockaddr_in *, struct hardware *);
312 extern void (*bootp_packet_handler)(struct interface_info *,
313 struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *);
314 void discover_interfaces(struct interface_info *);
315 void reinitialize_interfaces(void);
317 void got_one(struct protocol *);
318 void add_timeout(time_t, void (*)(void *), void *);
319 void cancel_timeout(void (*)(void *), void *);
320 void add_protocol(const char *, int, void (*)(struct protocol *), void *);
321 void remove_protocol(struct protocol *);
322 int interface_link_status(char *);
323 void interface_set_mtu_unpriv(int, u_int16_t);
324 void interface_set_mtu_priv(char *, u_int16_t);
327 struct hash_table *new_hash(void);
328 void add_hash(struct hash_table *, const unsigned char *, int, unsigned char *);
329 void *hash_lookup(struct hash_table *, unsigned char *, int);
332 extern struct option dhcp_options[256];
333 extern unsigned char dhcp_option_default_priority_list[];
334 extern int sizeof_dhcp_option_default_priority_list;
335 extern struct hash_table universe_hash;
336 extern struct universe dhcp_universe;
337 void initialize_universes(void);
340 u_int32_t getULong(unsigned char *);
341 int32_t getLong(unsigned char *);
342 u_int16_t getUShort(unsigned char *);
343 int16_t getShort(unsigned char *);
344 void putULong(unsigned char *, u_int32_t);
345 void putLong(unsigned char *, int32_t);
346 void putUShort(unsigned char *, unsigned int);
347 void putShort(unsigned char *, int);
350 struct iaddr subnet_number(struct iaddr, struct iaddr);
351 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
352 int addr_eq(struct iaddr, struct iaddr);
353 char *piaddr(struct iaddr);
356 extern const char *path_dhclient_conf;
357 extern char *path_dhclient_db;
358 extern time_t cur_time;
359 extern int log_priority;
360 extern int log_perror;
362 extern struct client_config top_level_config;
364 extern struct pidfh *pidfile;
366 void dhcpoffer(struct packet *);
367 void dhcpack(struct packet *);
368 void dhcpnak(struct packet *);
370 void send_discover(void *);
371 void send_request(void *);
372 void send_decline(void *);
374 void state_reboot(void *);
375 void state_init(void *);
376 void state_selecting(void *);
377 void state_requesting(void *);
378 void state_bound(void *);
379 void state_panic(void *);
381 void bind_lease(struct interface_info *);
383 void make_discover(struct interface_info *, struct client_lease *);
384 void make_request(struct interface_info *, struct client_lease *);
385 void make_decline(struct interface_info *, struct client_lease *);
387 void free_client_lease(struct client_lease *);
388 void rewrite_client_leases(void);
389 void write_client_lease(struct interface_info *, struct client_lease *, int);
391 void priv_script_init(const char *, char *);
392 void priv_script_write_params(const char *, struct client_lease *);
393 int priv_script_go(void);
395 void script_init(const char *, struct string_list *);
396 void script_write_params(const char *, struct client_lease *);
398 void client_envadd(struct client_state *,
399 const char *, const char *, const char *, ...);
400 void script_set_env(struct client_state *, const char *, const char *,
402 void script_flush_env(struct client_state *);
403 int dhcp_option_ev_name(char *, size_t, struct option *);
405 struct client_lease *packet_to_lease(struct packet *);
406 void go_daemon(void);
407 void client_location_changed(void);
409 void bootp(struct packet *);
410 void dhcp(struct packet *);
413 void assemble_hw_header(struct interface_info *, unsigned char *, int *);
414 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
415 unsigned int, unsigned char *, int);
416 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
417 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
418 unsigned char *, int);
421 int read_client_conf(void);
422 void read_client_leases(void);
423 void parse_client_statement(FILE *, struct interface_info *,
424 struct client_config *);
425 unsigned parse_X(FILE *, u_int8_t *, unsigned);
426 int parse_option_list(FILE *, u_int8_t *);
427 void parse_interface_declaration(FILE *, struct client_config *);
428 struct interface_info *interface_or_dummy(char *);
429 void make_client_state(struct interface_info *);
430 void make_client_config(struct interface_info *, struct client_config *);
431 void parse_client_lease_statement(FILE *, int);
432 void parse_client_lease_declaration(FILE *, struct client_lease *,
433 struct interface_info **);
434 struct option *parse_option_decl(FILE *, struct option_data *);
435 void parse_string_list(FILE *, struct string_list **, int);
436 void parse_reject_statement(FILE *, struct client_config *);
439 struct buf *buf_open(size_t);
440 int buf_add(struct buf *, const void *, size_t);
441 int buf_close(int, struct buf *);
442 ssize_t buf_read(int, void *, size_t);
443 void dispatch_imsg(struct interface_info *, int);