xref: /dragonfly/sbin/dhclient/dhcpd.h (revision 9ddb8543)
1 /*	$OpenBSD: dhcpd.h,v 1.66 2008/05/09 05:19:14 reyk Exp $	*/
2 /*	$DragonFly: src/sbin/dhclient/dhcpd.h,v 1.1 2008/08/30 16:07:58 hasso Exp $	*/
3 
4 /*
5  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
6  * Copyright (c) 1995, 1996, 1997, 1998, 1999
7  * The Internet Software Consortium.    All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38  * Enterprises.  To learn more about the Internet Software Consortium,
39  * see ``http://www.vix.com/isc''.  To learn more about Vixie
40  * Enterprises, see ``http://www.vix.com''.
41  */
42 
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <sys/wait.h>
49 
50 #include <arpa/inet.h>
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 
56 #include <ctype.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <limits.h>
60 #include <netdb.h>
61 #include <paths.h>
62 #include <stdarg.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <syslog.h>
67 #include <time.h>
68 #include <unistd.h>
69 
70 #include "dhcp.h"
71 
72 #define	LOCAL_PORT	68
73 #define	REMOTE_PORT	67
74 
75 struct option {
76 	char *name;
77 	char *format;
78 };
79 
80 struct option_data {
81 	unsigned int	 len;
82 	u_int8_t	*data;
83 };
84 
85 struct string_list {
86 	struct string_list	*next;
87 	char			string[1];	/* Actually bigger. */
88 };
89 
90 struct iaddr {
91 	int len;
92 	unsigned char iabuf[16];
93 };
94 
95 struct iaddrlist {
96 	struct iaddrlist *next;
97 	struct iaddr addr;
98 };
99 
100 struct hardware {
101 	u_int8_t htype;
102 	u_int8_t hlen;
103 	u_int8_t haddr[16];
104 };
105 
106 struct client_lease {
107 	struct client_lease	*next;
108 	time_t			 expiry, renewal, rebind;
109 	struct iaddr		 address;
110 	char			*server_name;
111 	char			*filename;
112 	struct string_list	*medium;
113 	unsigned int		 is_static : 1;
114 	unsigned int		 is_bootp : 1;
115 	struct option_data	 options[256];
116 };
117 
118 /* Possible states in which the client can be. */
119 enum dhcp_state {
120 	S_REBOOTING,
121 	S_INIT,
122 	S_SELECTING,
123 	S_REQUESTING,
124 	S_BOUND,
125 	S_RENEWING,
126 	S_REBINDING
127 };
128 
129 struct client_config {
130 	struct option_data	defaults[256];
131 	enum {
132 		ACTION_DEFAULT,
133 		ACTION_SUPERSEDE,
134 		ACTION_PREPEND,
135 		ACTION_APPEND
136 	} default_actions[256];
137 
138 	struct option_data	 send_options[256];
139 	u_int8_t		 required_options[256];
140 	u_int8_t		 requested_options[256];
141 	int			 requested_option_count;
142 	time_t			 timeout;
143 	time_t			 initial_interval;
144 	time_t			 link_timeout;
145 	time_t			 retry_interval;
146 	time_t			 select_interval;
147 	time_t			 reboot_timeout;
148 	time_t			 backoff_cutoff;
149 	struct string_list	*media;
150 	char			*script_name;
151 	enum { IGNORE, ACCEPT, PREFER }
152 				 bootp_policy;
153 	struct string_list	*medium;
154 	struct iaddrlist	*reject_list;
155 };
156 
157 struct client_state {
158 	struct client_lease	 *active;
159 	struct client_lease	 *new;
160 	struct client_lease	 *offered_leases;
161 	struct client_lease	 *leases;
162 	struct client_lease	 *alias;
163 	enum dhcp_state		  state;
164 	struct iaddr		  destination;
165 	u_int32_t		  xid;
166 	u_int16_t		  secs;
167 	time_t			  first_sending;
168 	time_t			  interval;
169 	struct string_list	 *medium;
170 	struct dhcp_packet	  packet;
171 	int			  packet_length;
172 	struct iaddr		  requested_address;
173 	char			**scriptEnv;
174 	int			  scriptEnvsize;
175 };
176 
177 struct interface_info {
178 	struct hardware		 hw_address;
179 	struct in_addr		 primary_address;
180 	char			 name[IFNAMSIZ];
181 	int			 rfdesc;
182 	int			 wfdesc;
183 	int			 ufdesc; /* unicast */
184 	unsigned char		*rbuf;
185 	size_t			 rbuf_max;
186 	size_t			 rbuf_offset;
187 	size_t			 rbuf_len;
188 	struct ifreq		*ifp;
189 	int			 noifmedia;
190 	int			 errors;
191 	u_int16_t		 index;
192 	int			 linkstat;
193 };
194 
195 struct timeout {
196 	struct timeout	*next;
197 	time_t		 when;
198 	void		 (*func)(void);
199 };
200 
201 #define	_PATH_DHCLIENT_CONF	"/etc/dhclient.conf"
202 #define	_PATH_DHCLIENT_DB	"/var/db/dhclient.leases"
203 #define	_PATH_DHCLIENT_SCRIPT	"/sbin/dhclient-script"
204 #define	DHCPD_LOG_FACILITY	LOG_DAEMON
205 
206 /* External definitions... */
207 
208 extern struct interface_info *ifi;
209 extern struct client_state *client;
210 extern struct client_config *config;
211 
212 /* options.c */
213 int cons_options(unsigned char *, const int, struct option_data *);
214 char *pretty_print_option(unsigned int, unsigned char *, int, int, int);
215 void do_packet(int, unsigned int, struct iaddr, struct hardware *);
216 
217 /* errwarn.c */
218 extern int warnings_occurred;
219 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
220 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
221 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
222 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
223 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
224 
225 /* conflex.c */
226 extern int lexline, lexchar;
227 extern char *token_line, *tlname;
228 void new_parse(char *);
229 int next_token(char **, FILE *);
230 int peek_token(char **, FILE *);
231 
232 /* parse.c */
233 void skip_to_semi(FILE *);
234 int parse_semi(FILE *);
235 char *parse_string(FILE *);
236 int parse_ip_addr(FILE *, struct iaddr *);
237 void parse_hardware_param(FILE *, struct hardware *);
238 void parse_lease_time(FILE *, time_t *);
239 int parse_numeric_aggregate(FILE *, unsigned char *, int, int, int);
240 void convert_num(unsigned char *, char *, int, int);
241 time_t parse_date(FILE *);
242 
243 /* bpf.c */
244 int if_register_bpf(void);
245 void if_register_send(void);
246 void if_register_receive(void);
247 ssize_t send_packet(struct in_addr, struct sockaddr_in *, struct hardware *);
248 ssize_t receive_packet(struct sockaddr_in *, struct hardware *);
249 
250 /* dispatch.c */
251 void discover_interface(void);
252 void reinitialize_interface(void);
253 void dispatch(void);
254 void got_one(void);
255 void add_timeout(time_t, void (*)(void));
256 void cancel_timeout(void (*)(void));
257 int interface_status(char *);
258 int interface_link_status(char *);
259 int interface_link_forceup(char *);
260 void interface_link_forcedown(char *);
261 
262 /* tables.c */
263 extern const struct option dhcp_options[256];
264 
265 /* convert.c */
266 u_int32_t getULong(unsigned char *);
267 int32_t getLong(unsigned char *);
268 u_int16_t getUShort(unsigned char *);
269 int16_t getShort(unsigned char *);
270 void putULong(unsigned char *, u_int32_t);
271 void putLong(unsigned char *, int32_t);
272 void putUShort(unsigned char *, unsigned int);
273 void putShort(unsigned char *, int);
274 
275 /* inet.c */
276 struct iaddr subnet_number(struct iaddr, struct iaddr);
277 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
278 int addr_eq(struct iaddr, struct iaddr);
279 char *piaddr(struct iaddr);
280 
281 /* dhclient.c */
282 extern char *path_dhclient_conf;
283 extern char *path_dhclient_db;
284 extern time_t cur_time;
285 extern int log_perror;
286 extern int routefd;
287 
288 void dhcpoffer(struct iaddr, struct option_data *);
289 void dhcpack(struct iaddr, struct option_data *);
290 void dhcpnak(struct iaddr, struct option_data *);
291 
292 void send_discover(void);
293 void send_request(void);
294 void send_decline(void);
295 
296 void state_reboot(void);
297 void state_init(void);
298 void state_selecting(void);
299 void state_bound(void);
300 void state_panic(void);
301 
302 void bind_lease(void);
303 
304 void make_discover(struct client_lease *);
305 void make_request(struct client_lease *);
306 void make_decline(struct client_lease *);
307 
308 void free_client_lease(struct client_lease *);
309 void rewrite_client_leases(void);
310 void write_client_lease(struct client_lease *, int);
311 
312 void	 priv_script_init(char *, char *);
313 void	 priv_script_write_params(char *, struct client_lease *);
314 int	 priv_script_go(void);
315 
316 void script_init(char *, struct string_list *);
317 void script_write_params(char *, struct client_lease *);
318 int script_go(void);
319 void script_set_env(const char *, const char *, const char *);
320 void script_flush_env(void);
321 int dhcp_option_ev_name(char *, size_t, const struct option *);
322 
323 struct client_lease *packet_to_lease(struct option_data *);
324 void go_daemon(void);
325 
326 void routehandler(void);
327 
328 /* packet.c */
329 void assemble_hw_header(unsigned char *, int *, struct hardware *);
330 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
331     unsigned int, unsigned char *, int);
332 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
333 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
334     unsigned char *, int);
335 
336 /* clparse.c */
337 int read_client_conf(void);
338 void read_client_leases(void);
339 void parse_client_statement(FILE *);
340 int parse_X(FILE *, u_int8_t *, int);
341 int parse_option_list(FILE *, u_int8_t *);
342 void parse_interface_declaration(FILE *);
343 void parse_client_lease_statement(FILE *, int);
344 void parse_client_lease_declaration(FILE *, struct client_lease *);
345 int parse_option_decl(FILE *, struct option_data *);
346 void parse_string_list(FILE *, struct string_list **, int);
347 void parse_reject_statement(FILE *);
348