xref: /openbsd/sbin/dhclient/dhcpd.h (revision 5429682b)
1 /*	$OpenBSD: dhcpd.h,v 1.299 2021/03/28 16:23:05 krw 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 
42 #define	LOCAL_PORT	68
43 #define	REMOTE_PORT	67
44 #define	TERMINATE	1
45 #define	RESTART		2
46 #define DB_TIMEFMT	"%w %Y/%m/%d %T UTC"
47 #define	RT_BUF_SIZE	2048
48 
49 struct option_data {
50 	unsigned int	 len;
51 	uint8_t		*data;
52 };
53 
54 struct reject_elem {
55 	TAILQ_ENTRY(reject_elem) next;
56 	struct in_addr		 addr;
57 };
58 
59 struct client_lease {
60 	TAILQ_ENTRY(client_lease) next;
61 	time_t			 epoch;
62 	struct in_addr		 address;
63 	struct in_addr		 next_server;
64 	char			*server_name;
65 	char			*filename;
66 	char			 ssid[32];
67 	uint8_t			 ssid_len;
68 	struct option_data	 options[DHO_COUNT];
69 };
70 #define BOOTP_LEASE(l)	((l)->options[DHO_DHCP_MESSAGE_TYPE].len == 0)
71 
72 /* Possible states in which the client can be. */
73 enum dhcp_state {
74 	S_PREBOOT,
75 	S_REBOOTING,
76 	S_INIT,
77 	S_SELECTING,
78 	S_REQUESTING,
79 	S_BOUND,
80 	S_RENEWING
81 };
82 
83 enum actions {
84 	ACTION_USELEASE,
85 	ACTION_DEFAULT,
86 	ACTION_SUPERSEDE,
87 	ACTION_PREPEND,
88 	ACTION_APPEND,
89 	ACTION_IGNORE
90 };
91 
92 TAILQ_HEAD(client_lease_tq, client_lease);
93 
94 struct client_config {
95 	struct option_data	 defaults[DHO_COUNT];
96 	enum actions		 default_actions[DHO_COUNT];
97 	struct in_addr		 address;
98 	struct in_addr		 next_server;
99 	struct option_data	 send_options[DHO_COUNT];
100 	uint8_t			 required_options[DHO_COUNT];
101 	uint8_t			 requested_options[DHO_COUNT];
102 	int			 requested_option_count;
103 	int			 required_option_count;
104 	time_t			 offer_interval;
105 	time_t			 initial_interval;
106 	time_t			 link_interval;
107 	time_t			 retry_interval;
108 	time_t			 select_interval;
109 	time_t			 reboot_interval;
110 	time_t			 backoff_cutoff;
111 	TAILQ_HEAD(, reject_elem) reject_list;
112 	char			*filename;
113 	char			*server_name;
114 };
115 
116 
117 struct interface_info {
118 	struct ether_addr	 hw_address;
119 	char			 name[IFNAMSIZ];
120 	char			 ssid[32];
121 	uint8_t			 ssid_len;
122 	int			 bpffd; /* bpf - reading & broadcast writing*/
123 	int			 udpfd; /* udp - unicast writing */
124 	unsigned char		*rbuf;
125 	size_t			 rbuf_max;
126 	int			 errors;
127 	uint16_t		 index;
128 	int			 link_state;
129 	int			 rdomain;
130 	int			 flags;
131 #define IFI_IN_CHARGE		0x01
132 	uint32_t		 mtu;
133 	struct dhcp_packet	 recv_packet;
134 	struct dhcp_packet	 sent_packet;
135 	int			 sent_packet_length;
136 	uint32_t		 xid;
137 	struct timespec		 timeout;
138 	struct timespec		 reboot_timeout;
139 	struct timespec		 expiry;
140 	struct timespec		 rebind;
141 	struct timespec		 renew;
142 	void			(*timeout_func)(struct interface_info *);
143 	uint16_t		 secs;
144 	struct timespec		 first_sending;
145 	struct timespec		 link_timeout;
146 	struct timespec		 offer_timeout;
147 	struct timespec		 select_timeout;
148 	enum dhcp_state		 state;
149 	struct in_addr		 destination;
150 	time_t			 interval;
151 	struct in_addr		 requested_address;
152 	struct client_lease	*active;
153 	struct client_lease	*offer;
154 	char			*offer_src;
155 	struct proposal		*configured;
156 	struct unwind_info	*unwind_info;
157 	struct client_lease_tq	 lease_db;
158 };
159 
160 #define	_PATH_DHCLIENT_CONF	"/etc/dhclient.conf"
161 #define	_PATH_LEASE_DB		"/var/db/dhclient.leases"
162 
163 /* options.c */
164 int			 pack_options(unsigned char *, int,
165 	struct option_data *);
166 struct option_data	*unpack_options(struct dhcp_packet *);
167 char			*pretty_print_option(unsigned int, struct option_data *,
168     int);
169 char			*pretty_print_string(unsigned char *, size_t, int);
170 char			*code_to_name(int);
171 char			*code_to_format(int);
172 int			 code_to_action(int, int);
173 int			 name_to_code(char *);
174 void			 merge_option_data(char *, struct option_data *,
175     struct option_data *, struct option_data *);
176 
177 /* conflex.c */
178 extern int	 lexline, lexchar;
179 extern char	*token_line, *tlname;
180 
181 void		 new_parse(char *);
182 int		 next_token(char **, FILE *);
183 int		 peek_token(char **, FILE *);
184 
185 /* parse.c */
186 void		 skip_to_semi(FILE *);
187 int		 parse_semi(FILE *);
188 int		 parse_string(FILE *, char **);
189 int		 parse_ip_addr(FILE *, struct in_addr *);
190 int		 parse_cidr(FILE *, unsigned char *);
191 int		 parse_number(FILE *, long long *, long long, long long);
192 int		 parse_boolean(FILE *, unsigned char *);
193 void		 parse_warn(char *);
194 
195 /* bpf.c */
196 int		 get_bpf_sock(char *);
197 int		 get_udp_sock(int);
198 int		 configure_bpf_sock(int);
199 ssize_t		 send_packet(struct interface_info *, struct in_addr,
200     struct in_addr, const char *);
201 ssize_t		 receive_packet(unsigned char *, unsigned char *,
202     struct sockaddr_in *, struct ether_addr *, struct dhcp_packet *);
203 
204 /* dispatch.c */
205 void		 dispatch(struct interface_info *, int);
206 void		 set_timeout( struct interface_info *, time_t,
207     void (*)(struct interface_info *));
208 void		 cancel_timeout(struct interface_info *);
209 
210 /* dhclient.c */
211 extern char			*path_dhclient_conf;
212 extern char			*path_lease_db;
213 extern char			*log_procname;
214 extern struct client_config	*config;
215 extern struct imsgbuf		*unpriv_ibuf;
216 extern int			 quit;
217 extern int			 cmd_opts;
218 #define		OPT_NOACTION	0x01
219 #define		OPT_VERBOSE	0x02
220 #define		OPT_FOREGROUND	0x04
221 #define		OPT_RELEASE	0x08
222 
223 void		 dhcpoffer(struct interface_info *, struct option_data *,
224     const char *);
225 void		 dhcpack(struct interface_info *, struct option_data *,
226     const char *);
227 void		 dhcpnak(struct interface_info *, const char *);
228 void		 bootreply(struct interface_info *, struct option_data *,
229     const char *);
230 void		 free_client_lease(struct client_lease *);
231 void		 routefd_handler(struct interface_info *, int);
232 void		 state_preboot(struct interface_info *);
233 char		*rfc1035_as_string(unsigned char *, size_t);
234 
235 /* packet.c */
236 void		 assemble_eh_header(struct ether_addr, struct ether_header *);
237 ssize_t		 decode_udp_ip_header(unsigned char *, uint32_t,
238     struct sockaddr_in *);
239 uint32_t	 checksum(unsigned char *, uint32_t, uint32_t);
240 uint32_t	 wrapsum(uint32_t);
241 
242 /* clparse.c */
243 void		 init_config(void);
244 void		 read_conf(char *, uint8_t *, struct ether_addr *);
245 void		 read_lease_db(struct client_lease_tq *);
246 
247 /* kroute.c */
248 unsigned int	 extract_route(uint8_t *, unsigned int, in_addr_t *,
249     in_addr_t *, in_addr_t *);
250 void		 write_resolv_conf(void);
251 
252 void		 propose(struct proposal *);
253 void		 revoke_proposal(struct proposal *);
254 
255 void		 tell_unwind(struct unwind_info *, int);
256