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