xref: /dragonfly/contrib/dhcpcd/src/dhcpcd.h (revision f984587a)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef DHCPCD_H
30 #define DHCPCD_H
31 
32 #include <sys/socket.h>
33 #include <net/if.h>
34 
35 #include <stdio.h>
36 
37 #include "config.h"
38 #ifdef HAVE_SYS_QUEUE_H
39 #include <sys/queue.h>
40 #endif
41 
42 #include "defs.h"
43 #include "control.h"
44 #include "if-options.h"
45 
46 #define HWADDR_LEN	20
47 #define IF_SSIDLEN	32
48 #define PROFILE_LEN	64
49 #define SECRET_LEN	64
50 
51 #define IF_INACTIVE	0
52 #define IF_ACTIVE	1
53 #define IF_ACTIVE_USER	2
54 
55 #define	LINK_UP		1
56 #define	LINK_UNKNOWN	0
57 #define	LINK_DOWN	-1
58 
59 #define IF_DATA_IPV4	0
60 #define IF_DATA_ARP	1
61 #define IF_DATA_IPV4LL	2
62 #define IF_DATA_DHCP	3
63 #define IF_DATA_IPV6	4
64 #define IF_DATA_IPV6ND	5
65 #define IF_DATA_DHCP6	6
66 #define IF_DATA_MAX	7
67 
68 #ifdef __QNX__
69 /* QNX carries defines for, but does not actually support PF_LINK */
70 #undef IFLR_ACTIVE
71 #endif
72 
73 struct interface {
74 	struct dhcpcd_ctx *ctx;
75 	TAILQ_ENTRY(interface) next;
76 	char name[IF_NAMESIZE];
77 	unsigned int index;
78 	unsigned int active;
79 	unsigned int flags;
80 	uint16_t hwtype; /* ARPHRD_ETHER for example */
81 	unsigned char hwaddr[HWADDR_LEN];
82 	uint8_t hwlen;
83 	unsigned short vlanid;
84 	unsigned int metric;
85 	int carrier;
86 	bool wireless;
87 	uint8_t ssid[IF_SSIDLEN];
88 	unsigned int ssid_len;
89 
90 	char profile[PROFILE_LEN];
91 	struct if_options *options;
92 	void *if_data[IF_DATA_MAX];
93 };
94 TAILQ_HEAD(if_head, interface);
95 
96 #include "privsep.h"
97 
98 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */
99 #if defined(__QNX) || \
100 	(defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000)
101 #undef CMSG_SPACE
102 #endif
103 
104 #ifndef ALIGNBYTES
105 #define ALIGNBYTES (sizeof(int) - 1)
106 #endif
107 #ifndef ALIGN
108 #define	ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES)
109 #endif
110 #ifndef CMSG_SPACE
111 #define	CMSG_SPACE(len)	(ALIGN(sizeof(struct cmsghdr)) + ALIGN(len))
112 #endif
113 
114 struct passwd;
115 
116 struct dhcpcd_ctx {
117 	char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
118 	char vendor[256];
119 	bool stdin_valid;	/* It's possible stdin, stdout and stderr */
120 	bool stdout_valid;	/* could be closed when dhcpcd starts. */
121 	bool stderr_valid;
122 	int stderr_fd;	/* FD for logging to stderr */
123 	int fork_fd;	/* FD for the fork init signal pipe */
124 	const char *cffile;
125 	unsigned long long options;
126 	char *logfile;
127 	int argc;
128 	char **argv;
129 	int ifac;	/* allowed interfaces */
130 	char **ifav;	/* allowed interfaces */
131 	int ifdc;	/* denied interfaces */
132 	char **ifdv;	/* denied interfaces */
133 	int ifc;	/* listed interfaces */
134 	char **ifv;	/* listed interfaces */
135 	int ifcc;	/* configured interfaces */
136 	char **ifcv;	/* configured interfaces */
137 	uint8_t duid_type;
138 	unsigned char *duid;
139 	size_t duid_len;
140 	struct if_head *ifaces;
141 
142 	char *ctl_buf;
143 	size_t ctl_buflen;
144 	size_t ctl_bufpos;
145 	size_t ctl_extra;
146 
147 	rb_tree_t routes;	/* our routes */
148 #ifdef RT_FREE_ROUTE_TABLE
149 	rb_tree_t froutes;	/* free routes for re-use */
150 #endif
151 	size_t rt_order;	/* route order storage */
152 
153 	int pf_inet_fd;
154 #ifdef PF_LINK
155 	int pf_link_fd;
156 #endif
157 	void *priv;
158 	int link_fd;
159 #ifndef SMALL
160 	int link_rcvbuf;
161 #endif
162 	int seq;	/* route message sequence no */
163 	int sseq;	/* successful seq no sent */
164 
165 #ifdef USE_SIGNALS
166 	sigset_t sigset;
167 #endif
168 	struct eloop *eloop;
169 
170 	char *script;
171 #ifdef HAVE_OPEN_MEMSTREAM
172 	FILE *script_fp;
173 #endif
174 	char *script_buf;
175 	size_t script_buflen;
176 	char **script_env;
177 	size_t script_envlen;
178 
179 	int control_fd;
180 	int control_unpriv_fd;
181 	struct fd_list_head control_fds;
182 	char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE];
183 	char control_sock_unpriv[sizeof(CONTROLSOCKET) + IF_NAMESIZE + 7];
184 	gid_t control_group;
185 
186 	/* DHCP Enterprise options, RFC3925 */
187 	struct dhcp_opt *vivso;
188 	size_t vivso_len;
189 
190 	char *randomstate; /* original state */
191 
192 	/* For filtering RTM_MISS messages per router */
193 #ifdef BSD
194 	uint8_t *rt_missfilter;
195 	size_t rt_missfilterlen;
196 	size_t rt_missfiltersize;
197 #endif
198 
199 #ifdef PRIVSEP
200 	struct passwd *ps_user;	/* struct passwd for privsep user */
201 	struct ps_process_head ps_processes;	/* List of spawned processes */
202 	struct ps_process *ps_root;
203 	struct ps_process *ps_inet;
204 	struct ps_process *ps_ctl;
205 	int ps_data_fd;		/* data returned from processes */
206 	int ps_log_fd;		/* chroot logging */
207 	int ps_log_root_fd;	/* outside chroot log reader */
208 	struct eloop *ps_eloop;	/* eloop for polling root data */
209 	struct fd_list *ps_control;		/* Queue for the above */
210 	struct fd_list *ps_control_client;	/* Queue for the above */
211 #endif
212 
213 #ifdef INET
214 	struct dhcp_opt *dhcp_opts;
215 	size_t dhcp_opts_len;
216 
217 	int udp_rfd;
218 	int udp_wfd;
219 
220 	/* Our aggregate option buffer.
221 	 * We ONLY use this when options are split, which for most purposes is
222 	 * practically never. See RFC3396 for details. */
223 	uint8_t *opt_buffer;
224 	size_t opt_buffer_len;
225 #endif
226 #ifdef INET6
227 	uint8_t *secret;
228 	size_t secret_len;
229 
230 #ifndef __sun
231 	int nd_fd;
232 #endif
233 	struct ra_head *ra_routers;
234 
235 	struct dhcp_opt *nd_opts;
236 	size_t nd_opts_len;
237 #ifdef DHCP6
238 	int dhcp6_rfd;
239 	int dhcp6_wfd;
240 	struct dhcp_opt *dhcp6_opts;
241 	size_t dhcp6_opts_len;
242 #endif
243 
244 #ifndef __linux__
245 	int ra_global;
246 #endif
247 #endif /* INET6 */
248 
249 #ifdef PLUGIN_DEV
250 	char *dev_load;
251 	int dev_fd;
252 	struct dev *dev;
253 	void *dev_handle;
254 #endif
255 };
256 
257 #ifdef USE_SIGNALS
258 extern const int dhcpcd_signals[];
259 extern const size_t dhcpcd_signals_len;
260 extern const int dhcpcd_signals_ignore[];
261 extern const size_t dhcpcd_signals_ignore_len;
262 #endif
263 
264 extern const char *dhcpcd_default_script;
265 
266 int dhcpcd_ifafwaiting(const struct interface *);
267 int dhcpcd_afwaiting(const struct dhcpcd_ctx *);
268 void dhcpcd_daemonise(struct dhcpcd_ctx *);
269 
270 void dhcpcd_signal_cb(int, void *);
271 
272 void dhcpcd_linkoverflow(struct dhcpcd_ctx *);
273 int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **);
274 void dhcpcd_handlecarrier(struct interface *, int, unsigned int);
275 int dhcpcd_handleinterface(void *, int, const char *);
276 void dhcpcd_handlehwaddr(struct interface *, uint16_t, const void *, uint8_t);
277 void dhcpcd_dropinterface(struct interface *, const char *);
278 int dhcpcd_selectprofile(struct interface *, const char *);
279 
280 void dhcpcd_startinterface(void *);
281 void dhcpcd_activateinterface(struct interface *, unsigned long long);
282 
283 #endif
284