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