xref: /openbsd/sbin/dhcpleased/dhcpleased.h (revision 45c5e5ad)
1 /*	$OpenBSD: dhcpleased.h,v 1.17 2024/08/25 09:53:53 florian Exp $	*/
2 
3 /*
4  * Copyright (c) 2017, 2021 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #define	_PATH_LOCKFILE		"/dev/dhcpleased.lock"
22 #define	_PATH_CONF_FILE		"/etc/dhcpleased.conf"
23 #define	_PATH_DHCPLEASED_SOCKET	"/dev/dhcpleased.sock"
24 #define	DHCPLEASED_USER		"_dhcp"
25 #define	DHCPLEASED_RTA_LABEL	"dhcpleased"
26 #define	SERVER_PORT		67
27 #define	CLIENT_PORT		68
28 #define	_PATH_LEASE		"/var/db/dhcpleased/"
29 #define	LEASE_VERSION		"version: 2"
30 #define	LEASE_IP_PREFIX		"ip: "
31 #define	LEASE_NEXTSERVER_PREFIX	"next-server: "
32 #define	LEASE_BOOTFILE_PREFIX	"filename: "
33 #define	LEASE_HOSTNAME_PREFIX	"host-name: "
34 #define	LEASE_DOMAIN_PREFIX	"domain-name: "
35 #define	LEASE_SIZE		4096
36 /* MAXDNAME from arpa/namesr.h */
37 #define	DHCPLEASED_MAX_DNSSL	1025
38 #define	MAX_RDNS_COUNT		8 /* max nameserver in a RTM_PROPOSAL */
39 
40 /* A 1500 bytes packet can hold less than 300 classless static routes */
41 #define	MAX_DHCP_ROUTES		256
42 
43 #define	DHCP_COOKIE		{99, 130, 83, 99}
44 
45 /* Possible values for hardware type (htype) field. */
46 #define	HTYPE_NONE		0
47 #define	HTYPE_ETHER		1
48 #define	HTYPE_IPSEC_TUNNEL	31
49 
50 /* DHCP op code */
51 #define	DHCP_BOOTREQUEST		1
52 #define	DHCP_BOOTREPLY			2
53 
54 /* DHCP Option codes: */
55 #define	DHO_PAD				0
56 #define	DHO_SUBNET_MASK			1
57 #define	DHO_TIME_OFFSET			2
58 #define	DHO_ROUTERS			3
59 #define	DHO_TIME_SERVERS		4
60 #define	DHO_NAME_SERVERS		5
61 #define	DHO_DOMAIN_NAME_SERVERS		6
62 #define	DHO_LOG_SERVERS			7
63 #define	DHO_COOKIE_SERVERS		8
64 #define	DHO_LPR_SERVERS			9
65 #define	DHO_IMPRESS_SERVERS		10
66 #define	DHO_RESOURCE_LOCATION_SERVERS	11
67 #define	DHO_HOST_NAME			12
68 #define	DHO_BOOT_SIZE			13
69 #define	DHO_MERIT_DUMP			14
70 #define	DHO_DOMAIN_NAME			15
71 #define	DHO_SWAP_SERVER			16
72 #define	DHO_ROOT_PATH			17
73 #define	DHO_EXTENSIONS_PATH		18
74 #define	DHO_IP_FORWARDING		19
75 #define	DHO_NON_LOCAL_SOURCE_ROUTING	20
76 #define	DHO_POLICY_FILTER		21
77 #define	DHO_MAX_DGRAM_REASSEMBLY	22
78 #define	DHO_DEFAULT_IP_TTL		23
79 #define	DHO_PATH_MTU_AGING_TIMEOUT	24
80 #define	DHO_PATH_MTU_PLATEAU_TABLE	25
81 #define	DHO_INTERFACE_MTU		26
82 #define	DHO_ALL_SUBNETS_LOCAL		27
83 #define	DHO_BROADCAST_ADDRESS		28
84 #define	DHO_PERFORM_MASK_DISCOVERY	29
85 #define	DHO_MASK_SUPPLIER		30
86 #define	DHO_ROUTER_DISCOVERY		31
87 #define	DHO_ROUTER_SOLICITATION_ADDRESS	32
88 #define	DHO_STATIC_ROUTES		33
89 #define	DHO_TRAILER_ENCAPSULATION	34
90 #define	DHO_ARP_CACHE_TIMEOUT		35
91 #define	DHO_IEEE802_3_ENCAPSULATION	36
92 #define	DHO_DEFAULT_TCP_TTL		37
93 #define	DHO_TCP_KEEPALIVE_INTERVAL	38
94 #define	DHO_TCP_KEEPALIVE_GARBAGE	39
95 #define	DHO_NIS_DOMAIN			40
96 #define	DHO_NIS_SERVERS			41
97 #define	DHO_NTP_SERVERS			42
98 #define	DHO_VENDOR_ENCAPSULATED_OPTIONS	43
99 #define	DHO_NETBIOS_NAME_SERVERS	44
100 #define	DHO_NETBIOS_DD_SERVER		45
101 #define	DHO_NETBIOS_NODE_TYPE		46
102 #define	DHO_NETBIOS_SCOPE		47
103 #define	DHO_FONT_SERVERS		48
104 #define	DHO_X_DISPLAY_MANAGER		49
105 #define	DHO_DHCP_REQUESTED_ADDRESS	50
106 #define	DHO_DHCP_LEASE_TIME		51
107 #define	DHO_DHCP_OPTION_OVERLOAD	52
108 #define	DHO_DHCP_MESSAGE_TYPE		53
109 #define	DHO_DHCP_SERVER_IDENTIFIER	54
110 #define	DHO_DHCP_PARAMETER_REQUEST_LIST	55
111 #define	DHO_DHCP_MESSAGE		56
112 #define	DHO_DHCP_MAX_MESSAGE_SIZE	57
113 #define	DHO_DHCP_RENEWAL_TIME		58
114 #define	DHO_DHCP_REBINDING_TIME		59
115 #define	DHO_DHCP_CLASS_IDENTIFIER	60
116 #define	DHO_DHCP_CLIENT_IDENTIFIER	61
117 #define	DHO_NISPLUS_DOMAIN		64
118 #define	DHO_NISPLUS_SERVERS		65
119 #define	DHO_TFTP_SERVER			66
120 #define	DHO_BOOTFILE_NAME		67
121 #define	DHO_MOBILE_IP_HOME_AGENT	68
122 #define	DHO_SMTP_SERVER			69
123 #define	DHO_POP_SERVER			70
124 #define	DHO_NNTP_SERVER			71
125 #define	DHO_WWW_SERVER			72
126 #define	DHO_FINGER_SERVER		73
127 #define	DHO_IRC_SERVER			74
128 #define	DHO_STREETTALK_SERVER		75
129 #define	DHO_STREETTALK_DIRECTORY_ASSISTANCE_SERVER	76
130 #define	DHO_DHCP_USER_CLASS_ID		77
131 #define	DHO_RELAY_AGENT_INFORMATION	82
132 #define	DHO_NDS_SERVERS			85
133 #define	DHO_NDS_TREE_NAME		86
134 #define	DHO_NDS_CONTEXT			87
135 #define	DHO_IPV6_ONLY_PREFERRED		108
136 #define	DHO_DOMAIN_SEARCH		119
137 #define	DHO_CLASSLESS_STATIC_ROUTES	121
138 #define	DHO_TFTP_CONFIG_FILE		144
139 #define	DHO_VOIP_CONFIGURATION_SERVER	150
140 #define	DHO_CLASSLESS_MS_STATIC_ROUTES	249
141 #define	DHO_AUTOPROXY_SCRIPT		252
142 #define	DHO_END				255
143 #define	DHO_COUNT			256	/* # of DHCP options */
144 
145 /* DHCP message types. */
146 #define	DHCPDISCOVER	1
147 #define	DHCPOFFER	2
148 #define	DHCPREQUEST	3
149 #define	DHCPDECLINE	4
150 #define	DHCPACK		5
151 #define	DHCPNAK		6
152 #define	DHCPRELEASE	7
153 #define	DHCPINFORM	8
154 
155 /* Ignore parts of DHCP lease */
156 #define	IGN_ROUTES	1
157 #define	IGN_DNS		2
158 
159 #define	MAX_SERVERS	16	/* max servers that can be ignored per if */
160 
161 #define	DHCP_SNAME_LEN		64
162 #define	DHCP_FILE_LEN		128
163 
164 struct dhcp_hdr {
165 	uint8_t		op;	/* Message opcode/type */
166 	uint8_t		htype;	/* Hardware addr type (see net/if_types.h) */
167 	uint8_t		hlen;	/* Hardware addr length */
168 	uint8_t		hops;	/* Number of relay agent hops from client */
169 	uint32_t	xid;	/* Transaction ID */
170 	uint16_t	secs;	/* Seconds since client started looking */
171 	uint16_t	flags;	/* Flag bits */
172 	struct in_addr	ciaddr;	/* Client IP address (if already in use) */
173 	struct in_addr	yiaddr;	/* Client IP address */
174 	struct in_addr	siaddr;	/* IP address of next server to talk to */
175 	struct in_addr	giaddr;	/* DHCP relay agent IP address */
176 	uint8_t		chaddr[16];		/* Client hardware address */
177 	char		sname[DHCP_SNAME_LEN];	/* Server name */
178 	char		file[DHCP_FILE_LEN];	/* Boot filename */
179 };
180 
181 struct imsgev {
182 	struct imsgbuf	 ibuf;
183 	void		(*handler)(int, short, void *);
184 	struct event	 ev;
185 	short		 events;
186 };
187 
188 struct dhcp_route {
189 	struct in_addr		 dst;
190 	struct in_addr		 mask;
191 	struct in_addr		 gw;
192 };
193 
194 enum imsg_type {
195 	IMSG_NONE,
196 #ifndef	SMALL
197 	IMSG_CTL_LOG_VERBOSE,
198 	IMSG_CTL_SHOW_INTERFACE_INFO,
199 	IMSG_CTL_SEND_REQUEST,
200 	IMSG_CTL_RELOAD,
201 	IMSG_CTL_END,
202 	IMSG_RECONF_CONF,
203 	IMSG_RECONF_IFACE,
204 	IMSG_RECONF_VC_ID,
205 	IMSG_RECONF_C_ID,
206 	IMSG_RECONF_H_NAME,
207 	IMSG_RECONF_END,
208 #endif	/* SMALL */
209 	IMSG_SEND_DISCOVER,
210 	IMSG_SEND_REQUEST,
211 	IMSG_SOCKET_IPC,
212 	IMSG_OPEN_BPFSOCK,
213 	IMSG_BPFSOCK,
214 	IMSG_UDPSOCK,
215 	IMSG_CLOSE_UDPSOCK,
216 	IMSG_ROUTESOCK,
217 	IMSG_CONTROLFD,
218 	IMSG_STARTUP,
219 	IMSG_UPDATE_IF,
220 	IMSG_REMOVE_IF,
221 	IMSG_DHCP,
222 	IMSG_CONFIGURE_INTERFACE,
223 	IMSG_DECONFIGURE_INTERFACE,
224 	IMSG_PROPOSE_RDNS,
225 	IMSG_WITHDRAW_RDNS,
226 	IMSG_WITHDRAW_ROUTES,
227 	IMSG_REPROPOSE_RDNS,
228 	IMSG_REQUEST_REBOOT,
229 };
230 
231 #ifndef	SMALL
232 struct ctl_engine_info {
233 	uint32_t		if_index;
234 	int			running;
235 	int			link_state;
236 	char			state[sizeof("IF_INIT_REBOOT")];
237 	struct timespec		request_time;
238 	struct in_addr		server_identifier;
239 	struct in_addr		dhcp_server; /* for unicast */
240 	struct in_addr		requested_ip;
241 	struct in_addr		mask;
242 	struct dhcp_route	routes[MAX_DHCP_ROUTES];
243 	int			routes_len;
244 	struct in_addr		nameservers[MAX_RDNS_COUNT];
245 	uint32_t		lease_time;
246 	uint32_t		renewal_time;
247 	uint32_t		rebinding_time;
248 };
249 
250 struct iface_conf {
251 	SIMPLEQ_ENTRY(iface_conf)	 entry;
252 	char				 name[IF_NAMESIZE];
253 	uint8_t				*vc_id;
254 	size_t				 vc_id_len;
255 	uint8_t				*c_id;
256 	size_t				 c_id_len;
257 	char				*h_name;
258 	int				 ignore;
259 	struct in_addr			 ignore_servers[MAX_SERVERS];
260 	int				 ignore_servers_len;
261 	int				 prefer_ipv6;
262 };
263 
264 struct dhcpleased_conf {
265 	SIMPLEQ_HEAD(iface_conf_head, iface_conf)	iface_list;
266 };
267 
268 #endif	/* SMALL */
269 
270 struct imsg_ifinfo {
271 	uint32_t		if_index;
272 	int			rdomain;
273 	int			running;
274 	int			link_state;
275 	struct ether_addr	hw_address;
276 	char			lease[LEASE_SIZE];
277 };
278 
279 struct imsg_propose_rdns {
280 	uint32_t		if_index;
281 	int			rdomain;
282 	int			rdns_count;
283 	struct in_addr		rdns[MAX_RDNS_COUNT];
284 };
285 
286 struct imsg_dhcp {
287 	uint32_t		if_index;
288 	ssize_t			len;
289 	uint16_t		csumflags;
290 	uint8_t			packet[1500];
291 };
292 
293 struct imsg_req_dhcp {
294 	uint32_t		if_index;
295 	uint32_t		xid;
296 	struct in_addr		ciaddr;
297 	struct in_addr		requested_ip;
298 	struct in_addr		server_identifier;
299 	struct in_addr		dhcp_server;
300 };
301 
302 /* dhcpleased.c */
303 void			 imsg_event_add(struct imsgev *);
304 int			 imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
305 			     pid_t, int, void *, uint16_t);
306 int			 imsg_forward_event(struct imsgev *, struct imsg *);
307 #ifndef	SMALL
308 void			 config_clear(struct dhcpleased_conf *);
309 struct dhcpleased_conf	*config_new_empty(void);
310 void			 merge_config(struct dhcpleased_conf *, struct
311 			     dhcpleased_conf *);
312 const char	*sin_to_str(struct sockaddr_in *);
313 const char	*i2s(uint32_t);
314 
315 /* frontend.c */
316 struct iface_conf	*find_iface_conf(struct iface_conf_head *, char *);
317 int			*changed_ifaces(struct dhcpleased_conf *, struct
318 			     dhcpleased_conf *);
319 
320 /* printconf.c */
321 void	print_config(struct dhcpleased_conf *);
322 
323 /* parse.y */
324 struct dhcpleased_conf	*parse_config(const char *);
325 int			 cmdline_symset(char *);
326 #else
327 #define	sin_to_str(x)	""
328 #define	i2s(x)		""
329 #endif	/* SMALL */
330 
331