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