1 /* $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.79 2012/08/26 23:33:30 krw Exp $ */
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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''.
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
46 #include <sys/types.h>
49 #include <arpa/inet.h>
51 #include <net/if_dl.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
73 #define REMOTE_PORT 67
86 struct string_list *next;
87 char string[1]; /* Actually bigger. */
92 unsigned char iabuf[16];
96 struct iaddrlist *next;
106 struct client_lease {
107 struct client_lease *next;
108 time_t expiry, renewal, rebind;
109 struct iaddr address;
112 unsigned int is_static : 1;
113 unsigned int is_bootp : 1;
114 struct option_data options[256];
117 /* Possible states in which the client can be. */
128 struct client_config {
129 struct option_data defaults[256];
136 } default_actions[256];
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;
143 time_t initial_interval;
145 time_t retry_interval;
146 time_t select_interval;
147 time_t reboot_timeout;
148 time_t backoff_cutoff;
150 enum { IGNORE, ACCEPT, PREFER }
152 struct iaddrlist *reject_list;
155 struct client_state {
156 struct client_lease *active;
157 struct client_lease *new;
158 struct client_lease *offered_leases;
159 struct client_lease *leases;
160 enum dhcp_state state;
161 struct iaddr destination;
164 time_t first_sending;
166 struct dhcp_packet packet;
168 struct iaddr requested_address;
173 struct interface_info {
174 struct hardware hw_address;
175 struct in_addr primary_address;
179 int ufdesc; /* unicast */
196 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
197 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
198 #define _PATH_DHCLIENT_SCRIPT "/sbin/dhclient-script"
199 #define DHCPD_LOG_FACILITY LOG_DAEMON
201 /* External definitions... */
203 extern struct interface_info *ifi;
204 extern struct client_state *client;
205 extern struct client_config *config;
208 int cons_options(struct option_data *);
209 char *pretty_print_option(unsigned int, unsigned char *, int, int, int);
210 void do_packet(int, unsigned int, struct iaddr, struct hardware *);
213 extern int warnings_occurred;
214 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
215 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
216 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
218 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
220 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
223 extern int lexline, lexchar;
224 extern char *token_line, *tlname;
225 void new_parse(char *);
226 int next_token(char **, FILE *);
227 int peek_token(char **, FILE *);
230 void skip_to_semi(FILE *);
231 int parse_semi(FILE *);
232 char *parse_string(FILE *);
233 int parse_ip_addr(FILE *, struct iaddr *);
234 void parse_hardware_param(FILE *, struct hardware *);
235 void parse_lease_time(FILE *, time_t *);
236 int parse_numeric_aggregate(FILE *, unsigned char *, int, int, int);
237 void convert_num(unsigned char *, char *, int, int);
238 time_t parse_date(FILE *);
241 int if_register_bpf(void);
242 void if_register_send(void);
243 void if_register_receive(void);
244 ssize_t send_packet(struct in_addr, struct sockaddr_in *, struct hardware *);
245 ssize_t receive_packet(struct sockaddr_in *, struct hardware *);
248 void discover_interface(void);
249 void reinitialize_interface(void);
252 void set_timeout(time_t, void (*)(void));
253 void set_timeout_interval(time_t, void (*)(void));
254 void cancel_timeout(void);
255 int interface_status(char *);
256 int interface_link_forceup(char *);
259 extern const struct option dhcp_options[256];
262 u_int32_t getULong(unsigned char *);
263 int32_t getLong(unsigned char *);
264 u_int16_t getUShort(unsigned char *);
265 int16_t getShort(unsigned char *);
266 void putULong(unsigned char *, u_int32_t);
267 void putLong(unsigned char *, int32_t);
268 void putUShort(unsigned char *, unsigned int);
269 void putShort(unsigned char *, int);
272 struct iaddr subnet_number(struct iaddr, struct iaddr);
273 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
274 int addr_eq(struct iaddr, struct iaddr);
275 char *piaddr(struct iaddr);
278 extern char *path_dhclient_conf;
279 extern char *path_dhclient_db;
280 extern int log_perror;
283 void dhcpoffer(struct iaddr, struct option_data *);
284 void dhcpack(struct iaddr, struct option_data *);
285 void dhcpnak(struct iaddr, struct option_data *);
287 void send_discover(void);
288 void send_request(void);
289 void send_decline(void);
291 void state_reboot(void);
292 void state_init(void);
293 void state_selecting(void);
294 void state_bound(void);
295 void state_panic(void);
297 void bind_lease(void);
299 void make_discover(struct client_lease *);
300 void make_request(struct client_lease *);
301 void make_decline(struct client_lease *);
303 void free_client_lease(struct client_lease *);
304 void rewrite_client_leases(void);
305 void write_client_lease(struct client_lease *);
307 void priv_script_init(char *);
308 void priv_script_write_params(char *, struct client_lease *);
309 int priv_script_go(void);
311 void script_init(char *);
312 void script_write_params(char *, struct client_lease *);
314 void script_set_env(const char *, const char *, const char *);
315 void script_flush_env(void);
316 int dhcp_option_ev_name(char *, size_t, const struct option *);
318 struct client_lease *packet_to_lease(struct option_data *);
319 void go_daemon(void);
321 void routehandler(void);
324 void assemble_hw_header(unsigned char *, int *, struct hardware *);
325 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
326 unsigned int, unsigned char *, int);
327 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
328 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
329 unsigned char *, int);
332 int read_client_conf(void);
333 void read_client_leases(void);
334 void parse_client_statement(FILE *);
335 int parse_X(FILE *, u_int8_t *, int);
336 int parse_option_list(FILE *, u_int8_t *);
337 void parse_interface_declaration(FILE *);
338 void parse_client_lease_statement(FILE *, int);
339 void parse_client_lease_declaration(FILE *, struct client_lease *);
340 int parse_option_decl(FILE *, struct option_data *);
341 void parse_string_list(FILE *, struct string_list **, int);
342 void parse_reject_statement(FILE *);