xref: /dflybsd-src/contrib/dhcpcd/src/dhcpcd.h (revision 49ecb1a08dc8bbbd8cc8cec29fd6e3a0a12651c2)
18d36e1dfSRoy Marples /* SPDX-License-Identifier: BSD-2-Clause */
27827cba2SAaron LI /*
37827cba2SAaron LI  * dhcpcd - DHCP client daemon
480aa9461SRoy Marples  * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
57827cba2SAaron LI  * All rights reserved
67827cba2SAaron LI 
77827cba2SAaron LI  * Redistribution and use in source and binary forms, with or without
87827cba2SAaron LI  * modification, are permitted provided that the following conditions
97827cba2SAaron LI  * are met:
107827cba2SAaron LI  * 1. Redistributions of source code must retain the above copyright
117827cba2SAaron LI  *    notice, this list of conditions and the following disclaimer.
127827cba2SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
137827cba2SAaron LI  *    notice, this list of conditions and the following disclaimer in the
147827cba2SAaron LI  *    documentation and/or other materials provided with the distribution.
157827cba2SAaron LI  *
167827cba2SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177827cba2SAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187827cba2SAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197827cba2SAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207827cba2SAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217827cba2SAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227827cba2SAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237827cba2SAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247827cba2SAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257827cba2SAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267827cba2SAaron LI  * SUCH DAMAGE.
277827cba2SAaron LI  */
287827cba2SAaron LI 
297827cba2SAaron LI #ifndef DHCPCD_H
307827cba2SAaron LI #define DHCPCD_H
317827cba2SAaron LI 
327827cba2SAaron LI #include <sys/socket.h>
337827cba2SAaron LI #include <net/if.h>
347827cba2SAaron LI 
356e63cc1fSRoy Marples #include <stdio.h>
366e63cc1fSRoy Marples 
377827cba2SAaron LI #include "config.h"
387827cba2SAaron LI #ifdef HAVE_SYS_QUEUE_H
397827cba2SAaron LI #include <sys/queue.h>
407827cba2SAaron LI #endif
417827cba2SAaron LI 
427827cba2SAaron LI #include "defs.h"
437827cba2SAaron LI #include "control.h"
447827cba2SAaron LI #include "if-options.h"
457827cba2SAaron LI 
467827cba2SAaron LI #define HWADDR_LEN	20
477827cba2SAaron LI #define IF_SSIDLEN	32
487827cba2SAaron LI #define PROFILE_LEN	64
497827cba2SAaron LI #define SECRET_LEN	64
507827cba2SAaron LI 
517827cba2SAaron LI #define IF_INACTIVE	0
527827cba2SAaron LI #define IF_ACTIVE	1
537827cba2SAaron LI #define IF_ACTIVE_USER	2
547827cba2SAaron LI 
557827cba2SAaron LI #define	LINK_UP		1
567827cba2SAaron LI #define	LINK_UNKNOWN	0
577827cba2SAaron LI #define	LINK_DOWN	-1
587827cba2SAaron LI 
597827cba2SAaron LI #define IF_DATA_IPV4	0
607827cba2SAaron LI #define IF_DATA_ARP	1
617827cba2SAaron LI #define IF_DATA_IPV4LL	2
627827cba2SAaron LI #define IF_DATA_DHCP	3
637827cba2SAaron LI #define IF_DATA_IPV6	4
647827cba2SAaron LI #define IF_DATA_IPV6ND	5
657827cba2SAaron LI #define IF_DATA_DHCP6	6
667827cba2SAaron LI #define IF_DATA_MAX	7
677827cba2SAaron LI 
687827cba2SAaron LI #ifdef __QNX__
697827cba2SAaron LI /* QNX carries defines for, but does not actually support PF_LINK */
707827cba2SAaron LI #undef IFLR_ACTIVE
717827cba2SAaron LI #endif
727827cba2SAaron LI 
737827cba2SAaron LI struct interface {
747827cba2SAaron LI 	struct dhcpcd_ctx *ctx;
757827cba2SAaron LI 	TAILQ_ENTRY(interface) next;
767827cba2SAaron LI 	char name[IF_NAMESIZE];
777827cba2SAaron LI 	unsigned int index;
787827cba2SAaron LI 	unsigned int active;
797827cba2SAaron LI 	unsigned int flags;
80d4fb1e02SRoy Marples 	uint16_t hwtype; /* ARPHRD_ETHER for example */
817827cba2SAaron LI 	unsigned char hwaddr[HWADDR_LEN];
827827cba2SAaron LI 	uint8_t hwlen;
837827cba2SAaron LI 	unsigned short vlanid;
847827cba2SAaron LI 	unsigned int metric;
857827cba2SAaron LI 	int carrier;
868d36e1dfSRoy Marples 	bool wireless;
878d36e1dfSRoy Marples 	uint8_t ssid[IF_SSIDLEN];
887827cba2SAaron LI 	unsigned int ssid_len;
897827cba2SAaron LI 
907827cba2SAaron LI 	char profile[PROFILE_LEN];
917827cba2SAaron LI 	struct if_options *options;
927827cba2SAaron LI 	void *if_data[IF_DATA_MAX];
937827cba2SAaron LI };
947827cba2SAaron LI TAILQ_HEAD(if_head, interface);
957827cba2SAaron LI 
966e63cc1fSRoy Marples #include "privsep.h"
977827cba2SAaron LI 
987827cba2SAaron LI /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */
997827cba2SAaron LI #if defined(__QNX) || \
1007827cba2SAaron LI 	(defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000)
1017827cba2SAaron LI #undef CMSG_SPACE
1027827cba2SAaron LI #endif
1037827cba2SAaron LI 
1047827cba2SAaron LI #ifndef ALIGNBYTES
1057827cba2SAaron LI #define ALIGNBYTES (sizeof(int) - 1)
1067827cba2SAaron LI #endif
1077827cba2SAaron LI #ifndef ALIGN
1087827cba2SAaron LI #define	ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES)
1097827cba2SAaron LI #endif
1107827cba2SAaron LI #ifndef CMSG_SPACE
1117827cba2SAaron LI #define	CMSG_SPACE(len)	(ALIGN(sizeof(struct cmsghdr)) + ALIGN(len))
1127827cba2SAaron LI #endif
1137827cba2SAaron LI 
1146e63cc1fSRoy Marples struct passwd;
1156e63cc1fSRoy Marples 
1167827cba2SAaron LI struct dhcpcd_ctx {
1177827cba2SAaron LI 	char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
118d4fb1e02SRoy Marples 	char vendor[256];
119a0d9933aSRoy Marples 	int fork_fd;	/* FD for the fork init signal pipe */
1207827cba2SAaron LI 	const char *cffile;
1217827cba2SAaron LI 	unsigned long long options;
1227827cba2SAaron LI 	char *logfile;
1237827cba2SAaron LI 	int argc;
1247827cba2SAaron LI 	char **argv;
1257827cba2SAaron LI 	int ifac;	/* allowed interfaces */
1267827cba2SAaron LI 	char **ifav;	/* allowed interfaces */
1277827cba2SAaron LI 	int ifdc;	/* denied interfaces */
1287827cba2SAaron LI 	char **ifdv;	/* denied interfaces */
1297827cba2SAaron LI 	int ifc;	/* listed interfaces */
1307827cba2SAaron LI 	char **ifv;	/* listed interfaces */
1317827cba2SAaron LI 	int ifcc;	/* configured interfaces */
1327827cba2SAaron LI 	char **ifcv;	/* configured interfaces */
133a0d9933aSRoy Marples 	uint8_t duid_type;
1347827cba2SAaron LI 	unsigned char *duid;
1357827cba2SAaron LI 	size_t duid_len;
1367827cba2SAaron LI 	struct if_head *ifaces;
1377827cba2SAaron LI 
1387f8103cdSRoy Marples 	char *ctl_buf;
1397f8103cdSRoy Marples 	size_t ctl_buflen;
1407f8103cdSRoy Marples 	size_t ctl_bufpos;
1417f8103cdSRoy Marples 	size_t ctl_extra;
1427f8103cdSRoy Marples 
1438d36e1dfSRoy Marples 	rb_tree_t routes;	/* our routes */
1448d36e1dfSRoy Marples #ifdef RT_FREE_ROUTE_TABLE
1458d36e1dfSRoy Marples 	rb_tree_t froutes;	/* free routes for re-use */
1468d36e1dfSRoy Marples #endif
1478d36e1dfSRoy Marples 	size_t rt_order;	/* route order storage */
1487827cba2SAaron LI 
1497827cba2SAaron LI 	int pf_inet_fd;
1507f8103cdSRoy Marples #ifdef PF_LINK
1517f8103cdSRoy Marples 	int pf_link_fd;
1527f8103cdSRoy Marples #endif
1537827cba2SAaron LI 	void *priv;
1547827cba2SAaron LI 	int link_fd;
1558d36e1dfSRoy Marples #ifndef SMALL
1568d36e1dfSRoy Marples 	int link_rcvbuf;
1578d36e1dfSRoy Marples #endif
1587827cba2SAaron LI 	int seq;	/* route message sequence no */
1597827cba2SAaron LI 	int sseq;	/* successful seq no sent */
1607827cba2SAaron LI 
1617827cba2SAaron LI #ifdef USE_SIGNALS
1627827cba2SAaron LI 	sigset_t sigset;
1637827cba2SAaron LI #endif
1647827cba2SAaron LI 	struct eloop *eloop;
1657827cba2SAaron LI 
166d4fb1e02SRoy Marples 	char *script;
1678d36e1dfSRoy Marples #ifdef HAVE_OPEN_MEMSTREAM
1688d36e1dfSRoy Marples 	FILE *script_fp;
1698d36e1dfSRoy Marples #endif
1708d36e1dfSRoy Marples 	char *script_buf;
1718d36e1dfSRoy Marples 	size_t script_buflen;
1728d36e1dfSRoy Marples 	char **script_env;
1738d36e1dfSRoy Marples 	size_t script_envlen;
1748d36e1dfSRoy Marples 
1757827cba2SAaron LI 	int control_fd;
1767827cba2SAaron LI 	int control_unpriv_fd;
1777827cba2SAaron LI 	struct fd_list_head control_fds;
1787827cba2SAaron LI 	char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE];
179b2927f2bSRoy Marples 	char control_sock_unpriv[sizeof(CONTROLSOCKET) + IF_NAMESIZE + 7];
1807827cba2SAaron LI 	gid_t control_group;
1817827cba2SAaron LI 
1827827cba2SAaron LI 	/* DHCP Enterprise options, RFC3925 */
1837827cba2SAaron LI 	struct dhcp_opt *vivso;
1847827cba2SAaron LI 	size_t vivso_len;
1857827cba2SAaron LI 
1867827cba2SAaron LI 	char *randomstate; /* original state */
1877827cba2SAaron LI 
1886e63cc1fSRoy Marples 	/* For filtering RTM_MISS messages per router */
1896e63cc1fSRoy Marples #ifdef BSD
1906e63cc1fSRoy Marples 	uint8_t *rt_missfilter;
1916e63cc1fSRoy Marples 	size_t rt_missfilterlen;
1926e63cc1fSRoy Marples 	size_t rt_missfiltersize;
1936e63cc1fSRoy Marples #endif
1946e63cc1fSRoy Marples 
1956e63cc1fSRoy Marples #ifdef PRIVSEP
1966e63cc1fSRoy Marples 	struct passwd *ps_user;	/* struct passwd for privsep user */
1976e63cc1fSRoy Marples 	struct ps_process_head ps_processes;	/* List of spawned processes */
19880aa9461SRoy Marples 	struct ps_process *ps_root;
19980aa9461SRoy Marples 	struct ps_process *ps_inet;
20080aa9461SRoy Marples 	struct ps_process *ps_ctl;
20180aa9461SRoy Marples 	int ps_data_fd;		/* data returned from processes */
20280aa9461SRoy Marples 	int ps_log_fd;		/* chroot logging */
20380aa9461SRoy Marples 	int ps_log_root_fd;	/* outside chroot log reader */
20480aa9461SRoy Marples 	struct eloop *ps_eloop;	/* eloop for polling root data */
2057f8103cdSRoy Marples 	struct fd_list *ps_control;		/* Queue for the above */
2067f8103cdSRoy Marples 	struct fd_list *ps_control_client;	/* Queue for the above */
2076e63cc1fSRoy Marples #endif
2086e63cc1fSRoy Marples 
2097827cba2SAaron LI #ifdef INET
2107827cba2SAaron LI 	struct dhcp_opt *dhcp_opts;
2117827cba2SAaron LI 	size_t dhcp_opts_len;
2127827cba2SAaron LI 
213d4fb1e02SRoy Marples 	int udp_rfd;
214d4fb1e02SRoy Marples 	int udp_wfd;
2157827cba2SAaron LI 
2167827cba2SAaron LI 	/* Our aggregate option buffer.
2177827cba2SAaron LI 	 * We ONLY use this when options are split, which for most purposes is
2187827cba2SAaron LI 	 * practically never. See RFC3396 for details. */
2197827cba2SAaron LI 	uint8_t *opt_buffer;
2207827cba2SAaron LI 	size_t opt_buffer_len;
2217827cba2SAaron LI #endif
2227827cba2SAaron LI #ifdef INET6
2237827cba2SAaron LI 	uint8_t *secret;
2247827cba2SAaron LI 	size_t secret_len;
2257827cba2SAaron LI 
2268d36e1dfSRoy Marples #ifndef __sun
2277827cba2SAaron LI 	int nd_fd;
2288d36e1dfSRoy Marples #endif
2297827cba2SAaron LI 	struct ra_head *ra_routers;
2307827cba2SAaron LI 
2317827cba2SAaron LI 	struct dhcp_opt *nd_opts;
2327827cba2SAaron LI 	size_t nd_opts_len;
2338d36e1dfSRoy Marples #ifdef DHCP6
234d4fb1e02SRoy Marples 	int dhcp6_rfd;
235d4fb1e02SRoy Marples 	int dhcp6_wfd;
2367827cba2SAaron LI 	struct dhcp_opt *dhcp6_opts;
2377827cba2SAaron LI 	size_t dhcp6_opts_len;
2388d36e1dfSRoy Marples #endif
2397827cba2SAaron LI 
2407827cba2SAaron LI #ifndef __linux__
2417827cba2SAaron LI 	int ra_global;
2427827cba2SAaron LI #endif
2437827cba2SAaron LI #endif /* INET6 */
2447827cba2SAaron LI 
2457827cba2SAaron LI #ifdef PLUGIN_DEV
2467827cba2SAaron LI 	char *dev_load;
2477827cba2SAaron LI 	int dev_fd;
2487827cba2SAaron LI 	struct dev *dev;
2497827cba2SAaron LI 	void *dev_handle;
2507827cba2SAaron LI #endif
2517827cba2SAaron LI };
2527827cba2SAaron LI 
2537827cba2SAaron LI #ifdef USE_SIGNALS
2547827cba2SAaron LI extern const int dhcpcd_signals[];
2557827cba2SAaron LI extern const size_t dhcpcd_signals_len;
256d4fb1e02SRoy Marples extern const int dhcpcd_signals_ignore[];
257d4fb1e02SRoy Marples extern const size_t dhcpcd_signals_ignore_len;
2587827cba2SAaron LI #endif
2597827cba2SAaron LI 
260d4fb1e02SRoy Marples extern const char *dhcpcd_default_script;
261d4fb1e02SRoy Marples 
2627827cba2SAaron LI int dhcpcd_ifafwaiting(const struct interface *);
2637827cba2SAaron LI int dhcpcd_afwaiting(const struct dhcpcd_ctx *);
264*a85d0907SRoy Marples void dhcpcd_daemonised(struct dhcpcd_ctx *);
2656e63cc1fSRoy Marples void dhcpcd_daemonise(struct dhcpcd_ctx *);
2667827cba2SAaron LI 
26780aa9461SRoy Marples void dhcpcd_signal_cb(int, void *);
26880aa9461SRoy Marples 
2697827cba2SAaron LI void dhcpcd_linkoverflow(struct dhcpcd_ctx *);
2707827cba2SAaron LI int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **);
271a0d9933aSRoy Marples void dhcpcd_handlecarrier(struct interface *, int, unsigned int);
2727827cba2SAaron LI int dhcpcd_handleinterface(void *, int, const char *);
273d4fb1e02SRoy Marples void dhcpcd_handlehwaddr(struct interface *, uint16_t, const void *, uint8_t);
2747827cba2SAaron LI void dhcpcd_dropinterface(struct interface *, const char *);
2757827cba2SAaron LI int dhcpcd_selectprofile(struct interface *, const char *);
2767827cba2SAaron LI 
2777827cba2SAaron LI void dhcpcd_startinterface(void *);
2787827cba2SAaron LI void dhcpcd_activateinterface(struct interface *, unsigned long long);
2797827cba2SAaron LI 
2807827cba2SAaron LI #endif
281