1 /* $OpenBSD: dhcpd.h,v 1.33 2004/05/06 22:29:15 deraadt 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''.
48 #define IFNAMSIZ MAX_INTERFACE_NAME_LEN
51 #define ETHER_ADDR_LEN ETH_ALEN
55 u_int8_t ether_dhost
[ETH_ALEN
]; /* destination eth addr */
56 u_int8_t ether_shost
[ETH_ALEN
]; /* source ether addr */
57 u_int16_t ether_type
; /* packet type ID field */
63 unsigned int ip_hl
:4; /* header length */
64 unsigned int ip_v
:4; /* version */
65 u_int8_t ip_tos
; /* type of service */
66 u_short ip_len
; /* total length */
67 u_short ip_id
; /* identification */
68 u_short ip_off
; /* fragment offset field */
69 #define IP_RF 0x8000 /* reserved fragment flag */
70 #define IP_DF 0x4000 /* dont fragment flag */
71 #define IP_MF 0x2000 /* more fragments flag */
72 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
73 u_int8_t ip_ttl
; /* time to live */
74 u_int8_t ip_p
; /* protocol */
75 u_short ip_sum
; /* checksum */
76 struct in_addr ip_src
, ip_dst
; /* source and dest address */
80 u_int16_t uh_sport
; /* source port */
81 u_int16_t uh_dport
; /* destination port */
82 u_int16_t uh_ulen
; /* udp length */
83 u_int16_t uh_sum
; /* udp checksum */
86 #define ETHERTYPE_IP 0x0800
87 #define IPTOS_LOWDELAY 0x10
88 #define ARPHRD_ETHER 1
90 // FIXME: I have no idea what this should be!
91 #define SIZE_T_MAX 1600
93 #define USE_SOCKET_RECEIVE
94 #define USE_SOCKET_SEND
96 #include <sys/types.h>
98 //#include <sys/time.h>
102 //#include <unistd.h>
107 //#include <unistd.h>
112 #define LOCAL_PORT 68
113 #define REMOTE_PORT 67
121 struct string_list
*next
;
127 unsigned char iabuf
[16];
131 struct iaddrlist
*next
;
136 struct dhcp_packet
*raw
;
141 struct iaddr client_addr
;
142 struct interface_info
*interface
;
143 struct hardware
*haddr
;
144 struct option_data options
[256];
153 struct client_lease
{
154 struct client_lease
*next
;
155 time_t expiry
, renewal
, rebind
;
156 struct iaddr address
;
160 struct iaddr serveraddress
;
163 struct string_list
*medium
;
164 unsigned int is_static
: 1;
165 unsigned int is_bootp
: 1;
166 struct option_data options
[256];
169 /* Possible states in which the client can be. */
181 struct client_config
{
182 struct option_data defaults
[256];
188 } default_actions
[256];
190 struct option_data send_options
[256];
191 u_int8_t required_options
[256];
192 u_int8_t requested_options
[256];
193 int requested_option_count
;
195 time_t initial_interval
;
196 time_t retry_interval
;
197 time_t select_interval
;
198 time_t reboot_timeout
;
199 time_t backoff_cutoff
;
200 struct string_list
*media
;
202 enum { IGNORE
, ACCEPT
, PREFER
}
204 struct string_list
*medium
;
205 struct iaddrlist
*reject_list
;
208 struct client_state
{
209 struct client_lease
*active
;
210 struct client_lease
*new;
211 struct client_lease
*offered_leases
;
212 struct client_lease
*leases
;
213 struct client_lease
*alias
;
214 enum dhcp_state state
;
215 struct iaddr destination
;
218 time_t first_sending
;
220 struct string_list
*medium
;
221 struct dhcp_packet packet
;
223 struct iaddr requested_address
;
224 struct client_config
*config
;
227 struct interface_info
{
228 struct interface_info
*next
;
229 struct hardware hw_address
;
230 struct in_addr primary_address
;
238 struct client_state
*client
;
246 struct timeout
*next
;
248 void (*func
)(void *);
253 struct protocol
*next
;
255 void (*handler
)(struct protocol
*);
259 #define DEFAULT_HASH_SIZE 97
262 struct hash_bucket
*next
;
265 unsigned char *value
;
270 struct hash_bucket
*buckets
[DEFAULT_HASH_SIZE
];
273 /* Default path to dhcpd config file. */
274 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
275 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
276 #define DHCPD_LOG_FACILITY LOG_DAEMON
278 #define MAX_TIME 0x7fffffff
281 /* External definitions... */
284 int cons_options(struct packet
*, struct dhcp_packet
*, int,
285 struct tree_cache
**, int, int, int, u_int8_t
*, int);
286 char *pretty_print_option(unsigned int,
287 unsigned char *, int, int, int);
288 void do_packet(struct interface_info
*, struct dhcp_packet
*,
289 int, unsigned int, struct iaddr
, struct hardware
*);
292 extern int warnings_occurred
;
294 void error(char *, ...);
295 int warning(char *, ...);
296 int note(char *, ...);
297 int debug(char *, ...);
298 int parse_warn(char *, ...);
300 void error(char *, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
301 int warning(char *, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
302 int note(char *, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
303 int debug(char *, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
304 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__
, 1, 2)));
308 extern int lexline
, lexchar
;
309 extern char *token_line
, *tlname
;
310 extern char comments
[4096];
311 extern int comment_index
;
312 extern int eol_token
;
313 void new_parse(char *);
314 int next_token(char **, FILE *);
315 int peek_token(char **, FILE *);
318 void skip_to_semi(FILE *);
319 int parse_semi(FILE *);
320 char *parse_string(FILE *);
321 int parse_ip_addr(FILE *, struct iaddr
*);
322 void parse_hardware_param(FILE *, struct hardware
*);
323 void parse_lease_time(FILE *, time_t *);
324 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, int *,
326 void convert_num(unsigned char *, char *, int, int);
327 time_t parse_date(FILE *);
330 pair
cons(caddr_t
, pair
);
333 struct string_list
*new_string_list(size_t size
);
334 struct hash_table
*new_hash_table(int);
335 struct hash_bucket
*new_hash_bucket(void);
336 void dfree(void *, char *);
337 void free_hash_bucket(struct hash_bucket
*, char *);
341 int if_register_bpf(struct interface_info
*);
342 void if_register_send(struct interface_info
*);
343 void if_register_receive(struct interface_info
*);
344 ssize_t
send_packet(struct interface_info
*, struct dhcp_packet
*, size_t,
345 struct in_addr
, struct sockaddr_in
*, struct hardware
*);
346 ssize_t
receive_packet(struct interface_info
*, unsigned char *, size_t,
347 struct sockaddr_in
*, struct hardware
*);
350 extern void (*bootp_packet_handler
)(struct interface_info
*,
351 struct dhcp_packet
*, int, unsigned int, struct iaddr
, struct hardware
*);
352 void discover_interfaces(struct interface_info
*);
353 void reinitialize_interfaces(void);
355 void got_one(struct protocol
*);
356 void add_timeout(time_t, void (*)(void *), void *);
357 void cancel_timeout(void (*)(void *), void *);
358 void add_protocol(char *, int, void (*)(struct protocol
*), void *);
359 void remove_protocol(struct protocol
*);
360 struct protocol
*find_protocol_by_adapter( struct interface_info
* );
361 int interface_link_status(char *);
364 struct hash_table
*new_hash(void);
365 void add_hash(struct hash_table
*, unsigned char *, int, unsigned char *);
366 unsigned char *hash_lookup(struct hash_table
*, unsigned char *, int);
369 extern struct dhcp_option dhcp_options
[256];
370 extern unsigned char dhcp_option_default_priority_list
[];
371 extern int sizeof_dhcp_option_default_priority_list
;
372 extern struct hash_table universe_hash
;
373 extern struct universe dhcp_universe
;
374 void initialize_universes(void);
377 u_int32_t
getULong(unsigned char *);
378 int32_t getLong(unsigned char *);
379 u_int16_t
getUShort(unsigned char *);
380 int16_t getShort(unsigned char *);
381 void putULong(unsigned char *, u_int32_t
);
382 void putLong(unsigned char *, int32_t);
383 void putUShort(unsigned char *, unsigned int);
384 void putShort(unsigned char *, int);
387 struct iaddr
subnet_number(struct iaddr
, struct iaddr
);
388 struct iaddr
broadcast_addr(struct iaddr
, struct iaddr
);
389 int addr_eq(struct iaddr
, struct iaddr
);
390 char *piaddr(struct iaddr
);
393 extern char *path_dhclient_conf
;
394 extern char *path_dhclient_db
;
395 extern time_t cur_time
;
396 extern int log_priority
;
397 extern int log_perror
;
399 extern struct client_config top_level_config
;
401 void dhcpoffer(struct packet
*);
402 void dhcpack(struct packet
*);
403 void dhcpnak(struct packet
*);
405 void send_discover(void *);
406 void send_request(void *);
407 void send_decline(void *);
409 void state_reboot(void *);
410 void state_init(void *);
411 void state_selecting(void *);
412 void state_requesting(void *);
413 void state_bound(void *);
414 void state_panic(void *);
416 void bind_lease(struct interface_info
*);
418 void make_discover(struct interface_info
*, struct client_lease
*);
419 void make_request(struct interface_info
*, struct client_lease
*);
420 void make_decline(struct interface_info
*, struct client_lease
*);
422 void free_client_lease(struct client_lease
*);
423 void rewrite_client_leases(struct interface_info
*);
424 void write_client_lease(struct interface_info
*, struct client_lease
*, int);
426 void priv_script_init(struct interface_info
*, char *, char *);
427 void priv_script_write_params(struct interface_info
*, char *, struct client_lease
*);
428 int priv_script_go(void);
430 void script_init(char *, struct string_list
*);
431 void script_write_params(char *, struct client_lease
*);
433 void client_envadd(struct client_state
*,
434 const char *, const char *, const char *, ...);
435 void script_set_env(struct client_state
*, const char *, const char *,
437 void script_flush_env(struct client_state
*);
438 int dhcp_option_ev_name(char *, size_t, struct dhcp_option
*);
440 struct client_lease
*packet_to_lease(struct packet
*);
441 void go_daemon(void);
442 void client_location_changed(void);
444 void bootp(struct packet
*);
445 void dhcp(struct packet
*);
448 void assemble_hw_header(struct interface_info
*, unsigned char *,
449 int *, struct hardware
*);
450 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t
, u_int32_t
,
451 unsigned int, unsigned char *, int);
452 ssize_t
decode_hw_header(unsigned char *, int, struct hardware
*);
453 ssize_t
decode_udp_ip_header(unsigned char *, int, struct sockaddr_in
*,
454 unsigned char *, int);
457 void assemble_ethernet_header(struct interface_info
*, unsigned char *,
458 int *, struct hardware
*);
459 ssize_t
decode_ethernet_header(struct interface_info
*, unsigned char *,
460 int, struct hardware
*);
463 int read_client_conf(struct interface_info
*);
464 void read_client_leases(void);
465 void parse_client_statement(FILE *, struct interface_info
*,
466 struct client_config
*);
467 int parse_X(FILE *, u_int8_t
*, int);
468 int parse_option_list(FILE *, u_int8_t
*);
469 void parse_interface_declaration(FILE *, struct client_config
*);
470 struct interface_info
*interface_or_dummy(char *);
471 void make_client_state(struct interface_info
*);
472 void make_client_config(struct interface_info
*, struct client_config
*);
473 void parse_client_lease_statement(FILE *, int);
474 void parse_client_lease_declaration(FILE *, struct client_lease
*,
475 struct interface_info
**);
476 struct dhcp_option
*parse_option_decl(FILE *, struct option_data
*);
477 void parse_string_list(FILE *, struct string_list
**, int);
478 void parse_reject_statement(FILE *, struct client_config
*);
481 struct buf
*buf_open(size_t);
482 int buf_add(struct buf
*, void *, size_t);
483 int buf_close(int, struct buf
*);
484 ssize_t
buf_read(int, void *, size_t);
485 void dispatch_imsg(int);