dhclient - Fix OpenBSD CVS ids to match current version.
[dragonfly.git] / sbin / dhclient / dhcpd.h
1 /*      $OpenBSD: src/sbin/dhclient/dhcpd.h,v 1.67 2008/05/26 03:11:48 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/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48
49 #include <arpa/inet.h>
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54
55 #include <ctype.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <limits.h>
59 #include <netdb.h>
60 #include <paths.h>
61 #include <stdarg.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <syslog.h>
66 #include <time.h>
67 #include <unistd.h>
68
69 #include "dhcp.h"
70
71 #define LOCAL_PORT      68
72 #define REMOTE_PORT     67
73
74 struct option {
75         char *name;
76         char *format;
77 };
78
79 struct option_data {
80         unsigned int     len;
81         u_int8_t        *data;
82 };
83
84 struct string_list {
85         struct string_list      *next;
86         char                    string[1];      /* Actually bigger. */
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 hardware {
100         u_int8_t htype;
101         u_int8_t hlen;
102         u_int8_t haddr[16];
103 };
104
105 struct client_lease {
106         struct client_lease     *next;
107         time_t                   expiry, renewal, rebind;
108         struct iaddr             address;
109         char                    *server_name;
110         char                    *filename;
111         struct string_list      *medium;
112         unsigned int             is_static : 1;
113         unsigned int             is_bootp : 1;
114         struct option_data       options[256];
115 };
116
117 /* Possible states in which the client can be. */
118 enum dhcp_state {
119         S_REBOOTING,
120         S_INIT,
121         S_SELECTING,
122         S_REQUESTING,
123         S_BOUND,
124         S_RENEWING,
125         S_REBINDING
126 };
127
128 struct client_config {
129         struct option_data      defaults[256];
130         enum {
131                 ACTION_DEFAULT,
132                 ACTION_SUPERSEDE,
133                 ACTION_PREPEND,
134                 ACTION_APPEND
135         } default_actions[256];
136
137         struct option_data       send_options[256];
138         u_int8_t                 required_options[256];
139         u_int8_t                 requested_options[256];
140         int                      requested_option_count;
141         time_t                   timeout;
142         time_t                   initial_interval;
143         time_t                   link_timeout;
144         time_t                   retry_interval;
145         time_t                   select_interval;
146         time_t                   reboot_timeout;
147         time_t                   backoff_cutoff;
148         struct string_list      *media;
149         char                    *script_name;
150         enum { IGNORE, ACCEPT, PREFER }
151                                  bootp_policy;
152         struct string_list      *medium;
153         struct iaddrlist        *reject_list;
154 };
155
156 struct client_state {
157         struct client_lease      *active;
158         struct client_lease      *new;
159         struct client_lease      *offered_leases;
160         struct client_lease      *leases;
161         struct client_lease      *alias;
162         enum dhcp_state           state;
163         struct iaddr              destination;
164         u_int32_t                 xid;
165         u_int16_t                 secs;
166         time_t                    first_sending;
167         time_t                    interval;
168         struct string_list       *medium;
169         struct dhcp_packet        packet;
170         int                       packet_length;
171         struct iaddr              requested_address;
172         char                    **scriptEnv;
173         int                       scriptEnvsize;
174 };
175
176 struct interface_info {
177         struct hardware          hw_address;
178         struct in_addr           primary_address;
179         char                     name[IFNAMSIZ];
180         int                      rfdesc;
181         int                      wfdesc;
182         int                      ufdesc; /* unicast */
183         unsigned char           *rbuf;
184         size_t                   rbuf_max;
185         size_t                   rbuf_offset;
186         size_t                   rbuf_len;
187         struct ifreq            *ifp;
188         int                      noifmedia;
189         int                      errors;
190         u_int16_t                index;
191         int                      linkstat;
192 };
193
194 struct timeout {
195         struct timeout  *next;
196         time_t           when;
197         void             (*func)(void);
198 };
199
200 #define _PATH_DHCLIENT_CONF     "/etc/dhclient.conf"
201 #define _PATH_DHCLIENT_DB       "/var/db/dhclient.leases"
202 #define _PATH_DHCLIENT_SCRIPT   "/sbin/dhclient-script"
203 #define DHCPD_LOG_FACILITY      LOG_DAEMON
204
205 /* External definitions... */
206
207 extern struct interface_info *ifi;
208 extern struct client_state *client;
209 extern struct client_config *config;
210
211 /* options.c */
212 int cons_options(unsigned char *, const int, struct option_data *);
213 char *pretty_print_option(unsigned int, unsigned char *, int, int, int);
214 void do_packet(int, unsigned int, struct iaddr, struct hardware *);
215
216 /* errwarn.c */
217 extern int warnings_occurred;
218 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
219 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
220 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
221 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
222 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
223
224 /* conflex.c */
225 extern int lexline, lexchar;
226 extern char *token_line, *tlname;
227 void new_parse(char *);
228 int next_token(char **, FILE *);
229 int peek_token(char **, FILE *);
230
231 /* parse.c */
232 void skip_to_semi(FILE *);
233 int parse_semi(FILE *);
234 char *parse_string(FILE *);
235 int parse_ip_addr(FILE *, struct iaddr *);
236 void parse_hardware_param(FILE *, struct hardware *);
237 void parse_lease_time(FILE *, time_t *);
238 int parse_numeric_aggregate(FILE *, unsigned char *, int, int, int);
239 void convert_num(unsigned char *, char *, int, int);
240 time_t parse_date(FILE *);
241
242 /* bpf.c */
243 int if_register_bpf(void);
244 void if_register_send(void);
245 void if_register_receive(void);
246 ssize_t send_packet(struct in_addr, struct sockaddr_in *, struct hardware *);
247 ssize_t receive_packet(struct sockaddr_in *, struct hardware *);
248
249 /* dispatch.c */
250 void discover_interface(void);
251 void reinitialize_interface(void);
252 void dispatch(void);
253 void got_one(void);
254 void add_timeout(time_t, void (*)(void));
255 void cancel_timeout(void (*)(void));
256 int interface_status(char *);
257 int interface_link_status(char *);
258 int interface_link_forceup(char *);
259 void interface_link_forcedown(char *);
260
261 /* tables.c */
262 extern const struct option dhcp_options[256];
263
264 /* convert.c */
265 u_int32_t getULong(unsigned char *);
266 int32_t getLong(unsigned char *);
267 u_int16_t getUShort(unsigned char *);
268 int16_t getShort(unsigned char *);
269 void putULong(unsigned char *, u_int32_t);
270 void putLong(unsigned char *, int32_t);
271 void putUShort(unsigned char *, unsigned int);
272 void putShort(unsigned char *, int);
273
274 /* inet.c */
275 struct iaddr subnet_number(struct iaddr, struct iaddr);
276 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
277 int addr_eq(struct iaddr, struct iaddr);
278 char *piaddr(struct iaddr);
279
280 /* dhclient.c */
281 extern char *path_dhclient_conf;
282 extern char *path_dhclient_db;
283 extern time_t cur_time;
284 extern int log_perror;
285 extern int routefd;
286
287 void dhcpoffer(struct iaddr, struct option_data *);
288 void dhcpack(struct iaddr, struct option_data *);
289 void dhcpnak(struct iaddr, struct option_data *);
290
291 void send_discover(void);
292 void send_request(void);
293 void send_decline(void);
294
295 void state_reboot(void);
296 void state_init(void);
297 void state_selecting(void);
298 void state_bound(void);
299 void state_panic(void);
300
301 void bind_lease(void);
302
303 void make_discover(struct client_lease *);
304 void make_request(struct client_lease *);
305 void make_decline(struct client_lease *);
306
307 void free_client_lease(struct client_lease *);
308 void rewrite_client_leases(void);
309 void write_client_lease(struct client_lease *, int);
310
311 void     priv_script_init(char *, char *);
312 void     priv_script_write_params(char *, struct client_lease *);
313 int      priv_script_go(void);
314
315 void script_init(char *, struct string_list *);
316 void script_write_params(char *, struct client_lease *);
317 int script_go(void);
318 void script_set_env(const char *, const char *, const char *);
319 void script_flush_env(void);
320 int dhcp_option_ev_name(char *, size_t, const struct option *);
321
322 struct client_lease *packet_to_lease(struct option_data *);
323 void go_daemon(void);
324
325 void routehandler(void);
326
327 /* packet.c */
328 void assemble_hw_header(unsigned char *, int *, struct hardware *);
329 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
330     unsigned int, unsigned char *, int);
331 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
332 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
333     unsigned char *, int);
334
335 /* clparse.c */
336 int read_client_conf(void);
337 void read_client_leases(void);
338 void parse_client_statement(FILE *);
339 int parse_X(FILE *, u_int8_t *, int);
340 int parse_option_list(FILE *, u_int8_t *);
341 void parse_interface_declaration(FILE *);
342 void parse_client_lease_statement(FILE *, int);
343 void parse_client_lease_declaration(FILE *, struct client_lease *);
344 int parse_option_decl(FILE *, struct option_data *);
345 void parse_string_list(FILE *, struct string_list **, int);
346 void parse_reject_statement(FILE *);