xref: /openbsd/usr.sbin/dhcrelay/dhcpd.h (revision 150ad76c)
1 /*	$OpenBSD: dhcpd.h,v 1.3 2004/04/20 03:52:36 deraadt 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 #include <sys/types.h>
43 
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/un.h>
49 #include <sys/wait.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/route.h>
54 
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 
58 #include <ctype.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <limits.h>
62 #include <netdb.h>
63 #include <paths.h>
64 #include <pwd.h>
65 #include <unistd.h>
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <syslog.h>
71 #include <time.h>
72 #include <unistd.h>
73 
74 #include "dhcp.h"
75 
76 #define	LOCAL_PORT	68
77 #define	REMOTE_PORT	67
78 
79 struct option_data {
80 	int		 len;
81 	u_int8_t	*data;
82 };
83 
84 struct string_list {
85 	struct string_list	*next;
86 	char			*string;
87 };
88 
89 struct iaddr {
90 	int len;
91 	unsigned char iabuf[16];
92 };
93 
94 struct iaddrlist {
95 	struct iaddrlist *next;
96 	struct iaddr addr;
97 };
98 
99 struct packet {
100 	struct dhcp_packet	*raw;
101 	int			 packet_length;
102 	int			 packet_type;
103 	int			 options_valid;
104 	int			 client_port;
105 	struct iaddr		 client_addr;
106 	struct interface_info	*interface;
107 	struct hardware		*haddr;
108 	struct option_data	 options[256];
109 	int			 got_requested_address;
110 };
111 
112 struct hardware {
113 	u_int8_t htype;
114 	u_int8_t hlen;
115 	u_int8_t haddr[16];
116 };
117 
118 struct client_lease {
119 	struct client_lease	*next;
120 	time_t			 expiry, renewal, rebind;
121 	struct iaddr		 address;
122 	char			*server_name;
123 	char			*filename;
124 	struct string_list	*medium;
125 	unsigned int		 is_static : 1;
126 	unsigned int		 is_bootp : 1;
127 	struct option_data	 options[256];
128 };
129 
130 /* Possible states in which the client can be. */
131 enum dhcp_state {
132 	S_REBOOTING,
133 	S_INIT,
134 	S_SELECTING,
135 	S_REQUESTING,
136 	S_BOUND,
137 	S_RENEWING,
138 	S_REBINDING
139 };
140 
141 struct client_config {
142 	struct option_data	defaults[256];
143 	enum {
144 		ACTION_DEFAULT,
145 		ACTION_SUPERSEDE,
146 		ACTION_PREPEND,
147 		ACTION_APPEND
148 	} default_actions[256];
149 
150 	struct option_data	 send_options[256];
151 	u_int8_t		 required_options[256];
152 	u_int8_t		 requested_options[256];
153 	int			 requested_option_count;
154 	time_t			 timeout;
155 	time_t			 initial_interval;
156 	time_t			 retry_interval;
157 	time_t			 select_interval;
158 	time_t			 reboot_timeout;
159 	time_t			 backoff_cutoff;
160 	struct string_list	*media;
161 	char			*script_name;
162 	enum { IGNORE, ACCEPT, PREFER }
163 				 bootp_policy;
164 	struct string_list	*medium;
165 	struct iaddrlist	*reject_list;
166 };
167 
168 struct client_state {
169 	struct client_lease	 *active;
170 	struct client_lease	 *new;
171 	struct client_lease	 *offered_leases;
172 	struct client_lease	 *leases;
173 	struct client_lease	 *alias;
174 	enum dhcp_state		  state;
175 	struct iaddr		  destination;
176 	u_int32_t		  xid;
177 	u_int16_t		  secs;
178 	time_t			  first_sending;
179 	time_t			  interval;
180 	struct string_list	 *medium;
181 	struct dhcp_packet	  packet;
182 	int			  packet_length;
183 	struct iaddr		  requested_address;
184 	struct client_config	 *config;
185 	char			**scriptEnv;
186 	int			  scriptEnvsize;
187 	struct string_list	 *env;
188 	int			  envc;
189 };
190 
191 struct interface_info {
192 	struct interface_info	*next;
193 	struct hardware		 hw_address;
194 	struct in_addr		 primary_address;
195 	char			 name[IFNAMSIZ];
196 	int			 rfdesc;
197 	int			 wfdesc;
198 	unsigned char		*rbuf;
199 	size_t			 rbuf_max;
200 	size_t			 rbuf_offset;
201 	size_t			 rbuf_len;
202 	struct ifreq		*ifp;
203 	struct client_state	*client;
204 	int			 noifmedia;
205 	int			 errors;
206 	int			 dead;
207 	u_int16_t		 index;
208 };
209 
210 struct timeout {
211 	struct timeout	*next;
212 	time_t		 when;
213 	void		 (*func)(void *);
214 	void		*what;
215 };
216 
217 struct protocol {
218 	struct protocol	*next;
219 	int fd;
220 	void (*handler)(struct protocol *);
221 	void *local;
222 };
223 
224 #define DEFAULT_HASH_SIZE 97
225 
226 struct hash_bucket {
227 	struct hash_bucket *next;
228 	unsigned char *name;
229 	int len;
230 	unsigned char *value;
231 };
232 
233 struct hash_table {
234 	int hash_count;
235 	struct hash_bucket *buckets[DEFAULT_HASH_SIZE];
236 };
237 
238 /* Default path to dhcpd config file. */
239 #define	DHCPD_LOG_FACILITY	LOG_DAEMON
240 
241 #define	MAX_TIME 0x7fffffff
242 #define	MIN_TIME 0
243 
244 /* External definitions... */
245 
246 /* errwarn.c */
247 extern int warnings_occurred;
248 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
249 int warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
250 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
251 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
252 
253 /* bpf.c */
254 int if_register_bpf(struct interface_info *);
255 void if_register_send(struct interface_info *);
256 void if_register_receive(struct interface_info *);
257 ssize_t send_packet(struct interface_info *,
258     struct packet *, struct dhcp_packet *, size_t, struct in_addr,
259     struct sockaddr_in *, struct hardware *);
260 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
261     struct sockaddr_in *, struct hardware *);
262 
263 /* dispatch.c */
264 extern void (*bootp_packet_handler)(struct interface_info *,
265     struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *);
266 void discover_interfaces(struct interface_info *);
267 void reinitialize_interfaces(void);
268 void dispatch(void);
269 void got_one(struct protocol *);
270 void add_timeout(time_t, void (*)(void *), void *);
271 void cancel_timeout(void (*)(void *), void *);
272 void add_protocol(char *, int, void (*)(struct protocol *), void *);
273 void remove_protocol(struct protocol *);
274 int interface_link_status(char *);
275 
276 /* packet.c */
277 void assemble_hw_header(struct interface_info *, unsigned char *,
278     int *, struct hardware *);
279 void assemble_udp_ip_header(struct interface_info *, unsigned char *,
280     int *, u_int32_t, u_int32_t, unsigned int, unsigned char *, int);
281 ssize_t decode_hw_header(struct interface_info *, unsigned char *,
282     int, struct hardware *);
283 ssize_t decode_udp_ip_header(struct interface_info *, unsigned char *,
284     int, struct sockaddr_in *, unsigned char *, int);
285 
286 
287 /* crap */
288 extern time_t cur_time;
289 extern int log_priority;
290 extern int log_perror;
291