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