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	struct iaddr		 nextserver;
125	char			*server_name;
126	char			*filename;
127	struct string_list	*medium;
128	unsigned int		 is_static : 1;
129	unsigned int		 is_bootp : 1;
130	struct option_data	 options[256];
131};
132
133/* Possible states in which the client can be. */
134enum dhcp_state {
135	S_REBOOTING,
136	S_INIT,
137	S_SELECTING,
138	S_REQUESTING,
139	S_BOUND,
140	S_RENEWING,
141	S_REBINDING
142};
143
144struct client_config {
145	struct option_data	defaults[256];
146	enum {
147		ACTION_DEFAULT,
148		ACTION_SUPERSEDE,
149		ACTION_PREPEND,
150		ACTION_APPEND
151	} default_actions[256];
152
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;
157	time_t			 timeout;
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;
164	char			*script_name;
165	enum { IGNORE, ACCEPT, PREFER }
166				 bootp_policy;
167	struct string_list	*medium;
168	struct iaddrlist	*reject_list;
169};
170
171struct 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;
179	u_int32_t		  xid;
180	u_int16_t		  secs;
181	time_t			  first_sending;
182	time_t			  interval;
183	struct string_list	 *medium;
184	struct dhcp_packet	  packet;
185	int			  packet_length;
186	struct iaddr		  requested_address;
187	struct client_config	 *config;
188	char			**scriptEnv;
189	int			  scriptEnvsize;
190	struct string_list	 *env;
191	int			  envc;
192};
193
194struct interface_info {
195	struct interface_info	*next;
196	struct hardware		 hw_address;
197	struct in_addr		 primary_address;
198	char			 name[IFNAMSIZ];
199	int			 rfdesc;
200	int			 wfdesc;
201	int			 ufdesc;
202	unsigned char		*rbuf;
203	size_t			 rbuf_max;
204	size_t			 rbuf_offset;
205	size_t			 rbuf_len;
206	struct ifreq		*ifp;
207	struct client_state	*client;
208	int			 noifmedia;
209	int			 errors;
210	int			 dead;
211	u_int16_t		 index;
212	int			 linkstat;
213};
214
215struct timeout {
216	struct timeout	*next;
217	time_t		 when;
218	void		 (*func)(void *);
219	void		*what;
220};
221
222struct protocol {
223	struct protocol	*next;
224	int fd;
225	void (*handler)(struct protocol *);
226	void *local;
227};
228
229#define DEFAULT_HASH_SIZE 97
230
231struct hash_bucket {
232	struct hash_bucket *next;
233	unsigned char *name;
234	int len;
235	unsigned char *value;
236};
237
238struct hash_table {
239	int hash_count;
240	struct hash_bucket *buckets[DEFAULT_HASH_SIZE];
241};
242
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
247
248#define	MAX_TIME 0x7fffffff
249#define	MIN_TIME 0
250
251/* External definitions... */
252
253/* options.c */
254int cons_options(struct packet *, struct dhcp_packet *, int,
255    struct tree_cache **, int, int, int, u_int8_t *, int);
256char *pretty_print_option(unsigned int,
257    unsigned char *, int, int, int);
258void do_packet(struct interface_info *, struct dhcp_packet *,
259    int, unsigned int, struct iaddr, struct hardware *);
260
261/* errwarn.c */
262extern int warnings_occurred;
263void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
264int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
265int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
266int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
267int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
268
269/* conflex.c */
270extern int lexline, lexchar;
271extern char *token_line, *tlname;
272extern char comments[4096];
273extern int comment_index;
274extern int eol_token;
275void new_parse(char *);
276int next_token(char **, FILE *);
277int peek_token(char **, FILE *);
278
279/* parse.c */
280void skip_to_semi(FILE *);
281int parse_semi(FILE *);
282char *parse_string(FILE *);
283int parse_ip_addr(FILE *, struct iaddr *);
284void parse_hardware_param(FILE *, struct hardware *);
285void parse_lease_time(FILE *, time_t *);
286unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, int *,
287    int, int, int);
288void convert_num(unsigned char *, char *, int, int);
289time_t parse_date(FILE *);
290
291/* tree.c */
292pair cons(caddr_t, pair);
293
294/* alloc.c */
295struct string_list	*new_string_list(size_t size);
296struct hash_table	*new_hash_table(int);
297struct hash_bucket	*new_hash_bucket(void);
298
299/* bpf.c */
300int if_register_bpf(struct interface_info *, int);
301void if_register_send(struct interface_info *);
302void if_register_receive(struct interface_info *);
303void send_packet_unpriv(int, struct dhcp_packet *, size_t, struct in_addr,
304    struct in_addr);
305struct imsg_hdr;
306void send_packet_priv(struct interface_info *, struct imsg_hdr *, int);
307ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
308    struct sockaddr_in *, struct hardware *);
309
310/* dispatch.c */
311extern void (*bootp_packet_handler)(struct interface_info *,
312    struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *);
313void discover_interfaces(struct interface_info *);
314void reinitialize_interfaces(void);
315void dispatch(void);
316void got_one(struct protocol *);
317void add_timeout(time_t, void (*)(void *), void *);
318void cancel_timeout(void (*)(void *), void *);
319void add_protocol(char *, int, void (*)(struct protocol *), void *);
320void remove_protocol(struct protocol *);
321int interface_link_status(char *);
322
323/* hash.c */
324struct hash_table *new_hash(void);
325void add_hash(struct hash_table *, unsigned char *, int, unsigned char *);
326unsigned char *hash_lookup(struct hash_table *, unsigned char *, int);
327
328/* tables.c */
329extern struct option dhcp_options[256];
330extern unsigned char dhcp_option_default_priority_list[];
331extern int sizeof_dhcp_option_default_priority_list;
332extern struct hash_table universe_hash;
333extern struct universe dhcp_universe;
334void initialize_universes(void);
335
336/* convert.c */
337u_int32_t getULong(unsigned char *);
338int32_t getLong(unsigned char *);
339u_int16_t getUShort(unsigned char *);
340int16_t getShort(unsigned char *);
341void putULong(unsigned char *, u_int32_t);
342void putLong(unsigned char *, int32_t);
343void putUShort(unsigned char *, unsigned int);
344void putShort(unsigned char *, int);
345
346/* inet.c */
347struct iaddr subnet_number(struct iaddr, struct iaddr);
348struct iaddr broadcast_addr(struct iaddr, struct iaddr);
349int addr_eq(struct iaddr, struct iaddr);
350char *piaddr(struct iaddr);
351
352/* dhclient.c */
353extern char *path_dhclient_conf;
354extern char *path_dhclient_db;
355extern time_t cur_time;
356extern int log_priority;
357extern int log_perror;
358
359extern struct client_config top_level_config;
360
361extern struct pidfh *pidfile;
362
363void dhcpoffer(struct packet *);
364void dhcpack(struct packet *);
365void dhcpnak(struct packet *);
366
367void send_discover(void *);
368void send_request(void *);
369void send_decline(void *);
370
371void state_reboot(void *);
372void state_init(void *);
373void state_selecting(void *);
374void state_requesting(void *);
375void state_bound(void *);
376void state_panic(void *);
377
378void bind_lease(struct interface_info *);
379
380void make_discover(struct interface_info *, struct client_lease *);
381void make_request(struct interface_info *, struct client_lease *);
382void make_decline(struct interface_info *, struct client_lease *);
383
384void free_client_lease(struct client_lease *);
385void rewrite_client_leases(void);
386void write_client_lease(struct interface_info *, struct client_lease *, int);
387
388void	 priv_script_init(char *, char *);
389void	 priv_script_write_params(char *, struct client_lease *);
390int	 priv_script_go(void);
391
392void script_init(char *, struct string_list *);
393void script_write_params(char *, struct client_lease *);
394int script_go(void);
395void client_envadd(struct client_state *,
396    const char *, const char *, const char *, ...);
397void script_set_env(struct client_state *, const char *, const char *,
398    const char *);
399void script_flush_env(struct client_state *);
400int dhcp_option_ev_name(char *, size_t, struct option *);
401
402struct client_lease *packet_to_lease(struct packet *);
403void go_daemon(void);
404void client_location_changed(void);
405
406void bootp(struct packet *);
407void dhcp(struct packet *);
408
409/* packet.c */
410void assemble_hw_header(struct interface_info *, unsigned char *, int *);
411void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
412    unsigned int, unsigned char *, int);
413ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
414ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
415    unsigned char *, int);
416
417/* clparse.c */
418int read_client_conf(void);
419void read_client_leases(void);
420void parse_client_statement(FILE *, struct interface_info *,
421    struct client_config *);
422int parse_X(FILE *, u_int8_t *, int);
423int parse_option_list(FILE *, u_int8_t *);
424void parse_interface_declaration(FILE *, struct client_config *);
425struct interface_info *interface_or_dummy(char *);
426void make_client_state(struct interface_info *);
427void make_client_config(struct interface_info *, struct client_config *);
428void parse_client_lease_statement(FILE *, int);
429void parse_client_lease_declaration(FILE *, struct client_lease *,
430    struct interface_info **);
431struct option *parse_option_decl(FILE *, struct option_data *);
432void parse_string_list(FILE *, struct string_list **, int);
433void parse_reject_statement(FILE *, struct client_config *);
434
435/* privsep.c */
436struct buf	*buf_open(size_t);
437int		 buf_add(struct buf *, void *, size_t);
438int		 buf_close(int, struct buf *);
439ssize_t		 buf_read(int, void *, size_t);
440void		 dispatch_imsg(struct interface_info *, int);
441