xref: /openbsd-src/usr.sbin/dhcrelay/dhcpd.h (revision ce7279d89b71439c96c854f612f4ac93a461fdc4)
1*ce7279d8Sjsg /*	$OpenBSD: dhcpd.h,v 1.24 2024/05/21 05:00:48 jsg Exp $	*/
248be18b4Shenning 
348be18b4Shenning /*
448be18b4Shenning  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
548be18b4Shenning  * Copyright (c) 1995, 1996, 1997, 1998, 1999
648be18b4Shenning  * The Internet Software Consortium.    All rights reserved.
748be18b4Shenning  *
848be18b4Shenning  * Redistribution and use in source and binary forms, with or without
948be18b4Shenning  * modification, are permitted provided that the following conditions
1048be18b4Shenning  * are met:
1148be18b4Shenning  *
1248be18b4Shenning  * 1. Redistributions of source code must retain the above copyright
1348be18b4Shenning  *    notice, this list of conditions and the following disclaimer.
1448be18b4Shenning  * 2. Redistributions in binary form must reproduce the above copyright
1548be18b4Shenning  *    notice, this list of conditions and the following disclaimer in the
1648be18b4Shenning  *    documentation and/or other materials provided with the distribution.
1748be18b4Shenning  * 3. Neither the name of The Internet Software Consortium nor the names
1848be18b4Shenning  *    of its contributors may be used to endorse or promote products derived
1948be18b4Shenning  *    from this software without specific prior written permission.
2048be18b4Shenning  *
2148be18b4Shenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
2248be18b4Shenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2348be18b4Shenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2448be18b4Shenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2548be18b4Shenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
2648be18b4Shenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2748be18b4Shenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2848be18b4Shenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2948be18b4Shenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3048be18b4Shenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3148be18b4Shenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3248be18b4Shenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3348be18b4Shenning  * SUCH DAMAGE.
3448be18b4Shenning  *
3548be18b4Shenning  * This software has been written for the Internet Software Consortium
3648be18b4Shenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
3748be18b4Shenning  * Enterprises.  To learn more about the Internet Software Consortium,
3848be18b4Shenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
3948be18b4Shenning  * Enterprises, see ``http://www.vix.com''.
4048be18b4Shenning  */
4148be18b4Shenning 
42962cae8eSrzalamena #include <sys/queue.h>
43962cae8eSrzalamena 
4465520a46Scanacar #define	SERVER_PORT	67
4565520a46Scanacar #define	CLIENT_PORT	68
4648be18b4Shenning 
47fa3d4f89Srzalamena /* Maximum size of client hardware address. */
48fa3d4f89Srzalamena #define CHADDR_SIZE	16
49fa3d4f89Srzalamena 
50fa3d4f89Srzalamena struct packet_ctx {
51fa3d4f89Srzalamena 	uint8_t				 pc_htype;
52fa3d4f89Srzalamena 	uint8_t				 pc_hlen;
53fa3d4f89Srzalamena 	uint8_t				 pc_smac[CHADDR_SIZE];
54fa3d4f89Srzalamena 	uint8_t				 pc_dmac[CHADDR_SIZE];
55fa3d4f89Srzalamena 
56fa3d4f89Srzalamena 	struct sockaddr_storage		 pc_src;
57fa3d4f89Srzalamena 	struct sockaddr_storage		 pc_dst;
58e4b49721Srzalamena 
59b2b4b45fSreyk 	const char 			*pc_circuit;
60e4b49721Srzalamena 	int				 pc_circuitlen;
61b2b4b45fSreyk 	const char			*pc_remote;
62e4b49721Srzalamena 	int				 pc_remotelen;
63fa3d4f89Srzalamena };
64fa3d4f89Srzalamena 
6548be18b4Shenning struct iaddr {
6648be18b4Shenning 	int len;
67fa3d4f89Srzalamena 	unsigned char iabuf[CHADDR_SIZE];
6848be18b4Shenning };
6948be18b4Shenning 
7048be18b4Shenning struct hardware {
7148be18b4Shenning 	u_int8_t htype;
7248be18b4Shenning 	u_int8_t hlen;
73fa3d4f89Srzalamena 	u_int8_t haddr[CHADDR_SIZE];
7448be18b4Shenning };
7548be18b4Shenning 
7648be18b4Shenning /* Possible states in which the client can be. */
7748be18b4Shenning enum dhcp_state {
7848be18b4Shenning 	S_REBOOTING,
7948be18b4Shenning 	S_INIT,
8048be18b4Shenning 	S_SELECTING,
8148be18b4Shenning 	S_REQUESTING,
8248be18b4Shenning 	S_BOUND,
8348be18b4Shenning 	S_RENEWING,
8448be18b4Shenning 	S_REBINDING
8548be18b4Shenning };
8648be18b4Shenning 
87e4b49721Srzalamena /* DHCP relaying modes. */
88e4b49721Srzalamena enum dhcp_relay_mode {
89e4b49721Srzalamena 	DRM_UNKNOWN,
90e4b49721Srzalamena 	DRM_LAYER2,
91e4b49721Srzalamena 	DRM_LAYER3,
92e4b49721Srzalamena };
9348be18b4Shenning 
9448be18b4Shenning struct interface_info {
9548be18b4Shenning 	struct hardware		 hw_address;
9648be18b4Shenning 	struct in_addr		 primary_address;
9748be18b4Shenning 	char			 name[IFNAMSIZ];
9848be18b4Shenning 	int			 rfdesc;
9948be18b4Shenning 	int			 wfdesc;
10048be18b4Shenning 	unsigned char		*rbuf;
10148be18b4Shenning 	size_t			 rbuf_max;
10248be18b4Shenning 	size_t			 rbuf_offset;
10348be18b4Shenning 	size_t			 rbuf_len;
104a29cd94bSrzalamena 	struct ifreq		 ifr;
10548be18b4Shenning 	int			 noifmedia;
10648be18b4Shenning 	int			 errors;
10748be18b4Shenning 	int			 dead;
10848be18b4Shenning 	u_int16_t		 index;
109962cae8eSrzalamena 
110962cae8eSrzalamena 	int			 ipv6; /* Has any IPv6 address. */
111962cae8eSrzalamena 	int			 gipv6; /* Has global IPv6 address. */
112962cae8eSrzalamena 	struct in6_addr		 linklocal; /* IPv6 link-local address. */
113962cae8eSrzalamena 
114962cae8eSrzalamena 	TAILQ_ENTRY(interface_info) entry;
11548be18b4Shenning };
116962cae8eSrzalamena TAILQ_HEAD(iflist, interface_info);
11748be18b4Shenning 
11848be18b4Shenning struct timeout {
11948be18b4Shenning 	struct timeout	*next;
12048be18b4Shenning 	time_t		 when;
12148be18b4Shenning 	void		 (*func)(void *);
12248be18b4Shenning 	void		*what;
12348be18b4Shenning };
12448be18b4Shenning 
12548be18b4Shenning struct protocol {
12648be18b4Shenning 	struct protocol	*next;
12748be18b4Shenning 	int fd;
12848be18b4Shenning 	void (*handler)(struct protocol *);
12948be18b4Shenning 	void *local;
13048be18b4Shenning };
13148be18b4Shenning 
132f51f0ea7Srzalamena struct server_list {
133f51f0ea7Srzalamena 	struct interface_info *intf;
134f51f0ea7Srzalamena 	struct server_list *next;
135f51f0ea7Srzalamena 	struct sockaddr_storage to;
136f51f0ea7Srzalamena 	int fd;
137f51f0ea7Srzalamena };
138f51f0ea7Srzalamena 
13948be18b4Shenning #define	DHCPD_LOG_FACILITY	LOG_DAEMON
14048be18b4Shenning 
14148be18b4Shenning /* External definitions... */
14248be18b4Shenning 
14348be18b4Shenning /* bpf.c */
14448be18b4Shenning int if_register_bpf(struct interface_info *);
14548be18b4Shenning void if_register_send(struct interface_info *);
146e4b49721Srzalamena void if_register_receive(struct interface_info *, int);
14748be18b4Shenning ssize_t send_packet(struct interface_info *,
148fa3d4f89Srzalamena     struct dhcp_packet *, size_t, struct packet_ctx *);
14948be18b4Shenning ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
150fa3d4f89Srzalamena     struct packet_ctx *);
15148be18b4Shenning 
15248be18b4Shenning /* dispatch.c */
15348be18b4Shenning extern void (*bootp_packet_handler)(struct interface_info *,
154fa3d4f89Srzalamena     struct dhcp_packet *, int, struct packet_ctx *);
155962cae8eSrzalamena struct interface_info *iflist_getbyname(const char *);
156962cae8eSrzalamena void setup_iflist(void);
157962cae8eSrzalamena struct interface_info *register_interface(const char *,
158e4b49721Srzalamena     void (*)(struct protocol *), int isserver);
15948be18b4Shenning void dispatch(void);
16048be18b4Shenning void got_one(struct protocol *);
16148be18b4Shenning void add_protocol(char *, int, void (*)(struct protocol *), void *);
16248be18b4Shenning void remove_protocol(struct protocol *);
16348be18b4Shenning 
16448be18b4Shenning /* packet.c */
16522c60a6bSreyk ssize_t assemble_hw_header(unsigned char *, size_t, size_t,
16622c60a6bSreyk     struct packet_ctx *, unsigned int);
16722c60a6bSreyk ssize_t assemble_udp_ip_header(unsigned char *, size_t, size_t,
16822c60a6bSreyk     struct packet_ctx *pc, unsigned char *, size_t);
16922c60a6bSreyk ssize_t decode_hw_header(unsigned char *, size_t, size_t, struct packet_ctx *,
17022c60a6bSreyk     unsigned int);
17122c60a6bSreyk ssize_t decode_udp_ip_header(unsigned char *, size_t, size_t,
17222c60a6bSreyk     struct packet_ctx *);
17348be18b4Shenning 
17465520a46Scanacar /* dhcrelay.c */
1754be048dcSreyk extern int server_fd;
176962cae8eSrzalamena extern struct iflist intflist;
17748be18b4Shenning 
17848be18b4Shenning /* crap */
17948be18b4Shenning extern time_t cur_time;
180fa3d4f89Srzalamena 
181fa3d4f89Srzalamena static inline struct sockaddr_in *
ss2sin(struct sockaddr_storage * ss)182fa3d4f89Srzalamena ss2sin(struct sockaddr_storage *ss)
183fa3d4f89Srzalamena {
184fa3d4f89Srzalamena 	return ((struct sockaddr_in *)ss);
185fa3d4f89Srzalamena }
186962cae8eSrzalamena 
187962cae8eSrzalamena static inline struct sockaddr_in6 *
ss2sin6(struct sockaddr_storage * ss)188962cae8eSrzalamena ss2sin6(struct sockaddr_storage *ss)
189962cae8eSrzalamena {
190962cae8eSrzalamena 	return ((struct sockaddr_in6 *)ss);
191962cae8eSrzalamena }
192