Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * dhcpcd - DHCP client daemon
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 #include "defs.h"
     39 #include "control.h"
     40 #include "if-options.h"
     41 #include "queue.h"
     42 
     43 #define HWADDR_LEN	20
     44 #define IF_SSIDLEN	32
     45 #define PROFILE_LEN	64
     46 #define SECRET_LEN	64
     47 
     48 #define IF_INACTIVE	0
     49 #define IF_ACTIVE	1
     50 #define IF_ACTIVE_USER	2
     51 
     52 #define	LINK_UP		1
     53 #define	LINK_UNKNOWN	0
     54 #define	LINK_DOWN	-1
     55 
     56 #define IF_DATA_IPV4	0
     57 #define IF_DATA_ARP	1
     58 #define IF_DATA_IPV4LL	2
     59 #define IF_DATA_DHCP	3
     60 #define IF_DATA_IPV6	4
     61 #define IF_DATA_IPV6ND	5
     62 #define IF_DATA_DHCP6	6
     63 #define IF_DATA_MAX	7
     64 
     65 #ifdef __QNX__
     66 /* QNX carries defines for, but does not actually support PF_LINK */
     67 #undef IFLR_ACTIVE
     68 #endif
     69 
     70 struct interface {
     71 	struct dhcpcd_ctx *ctx;
     72 	TAILQ_ENTRY(interface) next;
     73 	char name[IF_NAMESIZE];
     74 	unsigned int index;
     75 	unsigned int active;
     76 	unsigned int flags;
     77 	uint16_t hwtype; /* ARPHRD_ETHER for example */
     78 	unsigned char hwaddr[HWADDR_LEN];
     79 	uint8_t hwlen;
     80 	int mtu;
     81 	unsigned short vlanid;
     82 	unsigned int metric;
     83 	int carrier;
     84 	bool wireless;
     85 	uint8_t ssid[IF_SSIDLEN];
     86 	unsigned int ssid_len;
     87 
     88 	int argc;
     89 	char **argv;
     90 
     91 	char profile[PROFILE_LEN];
     92 	struct if_options *options;
     93 	void *if_data[IF_DATA_MAX];
     94 };
     95 TAILQ_HEAD(if_head, interface);
     96 
     97 #include "privsep.h"
     98 
     99 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */
    100 #if defined(__QNX) || \
    101 	(defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000)
    102 #undef CMSG_SPACE
    103 #endif
    104 
    105 #ifndef ALIGNBYTES
    106 #define ALIGNBYTES (sizeof(int) - 1)
    107 #endif
    108 #ifndef ALIGN
    109 #define	ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES)
    110 #endif
    111 #ifndef CMSG_SPACE
    112 #define	CMSG_SPACE(len)	(ALIGN(sizeof(struct cmsghdr)) + ALIGN(len))
    113 #endif
    114 
    115 struct passwd;
    116 
    117 struct dhcpcd_ctx {
    118 	char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1];
    119 	char vendor[256];
    120 	int fork_fd;	/* FD for the fork init signal pipe */
    121 	const char *cffile;
    122 	unsigned long long options;
    123 	char *logfile;
    124 	int argc;
    125 	char **argv;
    126 	int ifac;	/* allowed interfaces */
    127 	char **ifav;	/* allowed interfaces */
    128 	int ifdc;	/* denied interfaces */
    129 	char **ifdv;	/* denied interfaces */
    130 	int ifc;	/* listed interfaces */
    131 	char **ifv;	/* listed interfaces */
    132 	int ifcc;	/* configured interfaces */
    133 	char **ifcv;	/* configured interfaces */
    134 	uint8_t duid_type;
    135 	unsigned char *duid;
    136 	size_t duid_len;
    137 	struct if_head *ifaces;
    138 
    139 	char *ctl_buf;
    140 	size_t ctl_buflen;
    141 	size_t ctl_bufpos;
    142 	size_t ctl_extra;
    143 
    144 	rb_tree_t routes;	/* our routes */
    145 #ifdef RT_FREE_ROUTE_TABLE
    146 	rb_tree_t froutes;	/* free routes for re-use */
    147 #endif
    148 	size_t rt_order;	/* route order storage */
    149 
    150 	int pf_inet_fd;
    151 #ifdef PF_LINK
    152 	int pf_link_fd;
    153 #endif
    154 	void *priv;
    155 	int link_fd;
    156 #ifndef SMALL
    157 	int link_rcvbuf;
    158 #endif
    159 	int seq;	/* route message sequence no */
    160 	int sseq;	/* successful seq no sent */
    161 
    162 	struct eloop *eloop;
    163 
    164 	char *script;
    165 #ifdef HAVE_OPEN_MEMSTREAM
    166 	FILE *script_fp;
    167 #endif
    168 	char *script_buf;
    169 	size_t script_buflen;
    170 	char **script_env;
    171 	size_t script_envlen;
    172 
    173 	int control_fd;
    174 	int control_unpriv_fd;
    175 	struct fd_list_head control_fds;
    176 	char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE];
    177 	char control_sock_unpriv[sizeof(CONTROLSOCKET) + IF_NAMESIZE + 7];
    178 	gid_t control_group;
    179 
    180 	/* DHCP Enterprise options, RFC3925 */
    181 	struct dhcp_opt *vivso;
    182 	size_t vivso_len;
    183 
    184 	char *randomstate; /* original state */
    185 
    186 	/* For filtering RTM_MISS messages per router */
    187 #ifdef BSD
    188 	uint8_t *rt_missfilter;
    189 	size_t rt_missfilterlen;
    190 	size_t rt_missfiltersize;
    191 #endif
    192 
    193 #ifdef PRIVSEP
    194 	struct passwd *ps_user;	/* struct passwd for privsep user */
    195 	struct ps_process_head ps_processes;	/* List of spawned processes */
    196 	struct ps_process *ps_root;
    197 	struct ps_process *ps_inet;
    198 	struct ps_process *ps_ctl;
    199 	int ps_data_fd;		/* data returned from processes */
    200 	int ps_log_fd;		/* chroot logging */
    201 	int ps_log_root_fd;	/* outside chroot log reader */
    202 	struct fd_list *ps_control;		/* Queue for the above */
    203 	struct fd_list *ps_control_client;	/* Queue for the above */
    204 #endif
    205 
    206 #ifdef INET
    207 	struct dhcp_opt *dhcp_opts;
    208 	size_t dhcp_opts_len;
    209 
    210 	int udp_rfd;
    211 	int udp_wfd;
    212 
    213 	/* Our aggregate option buffer.
    214 	 * We ONLY use this when options are split, which for most purposes is
    215 	 * practically never. See RFC3396 for details. */
    216 	uint8_t *opt_buffer;
    217 	size_t opt_buffer_len;
    218 #endif
    219 #ifdef INET6
    220 	uint8_t *secret;
    221 	size_t secret_len;
    222 
    223 #ifndef __sun
    224 	int nd_fd;
    225 #endif
    226 	struct ra_head *ra_routers;
    227 
    228 	struct dhcp_opt *nd_opts;
    229 	size_t nd_opts_len;
    230 #ifdef DHCP6
    231 	int dhcp6_rfd;
    232 	int dhcp6_wfd;
    233 	struct dhcp_opt *dhcp6_opts;
    234 	size_t dhcp6_opts_len;
    235 #endif
    236 
    237 #ifndef __linux__
    238 	int ra_global;
    239 #endif
    240 #endif /* INET6 */
    241 
    242 #ifdef PLUGIN_DEV
    243 	char *dev_load;
    244 	int dev_fd;
    245 	struct dev *dev;
    246 	void *dev_handle;
    247 #endif
    248 };
    249 
    250 #ifdef USE_SIGNALS
    251 extern const int dhcpcd_signals[];
    252 extern const size_t dhcpcd_signals_len;
    253 extern const int dhcpcd_signals_ignore[];
    254 extern const size_t dhcpcd_signals_ignore_len;
    255 #endif
    256 
    257 extern const char *dhcpcd_default_script;
    258 
    259 int dhcpcd_ifafwaiting(const struct interface *);
    260 int dhcpcd_afwaiting(const struct dhcpcd_ctx *);
    261 void dhcpcd_daemonised(struct dhcpcd_ctx *);
    262 void dhcpcd_daemonise(struct dhcpcd_ctx *);
    263 
    264 void dhcpcd_linkoverflow(struct dhcpcd_ctx *);
    265 int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **);
    266 void dhcpcd_handlecarrier(struct interface *, int, unsigned int);
    267 int dhcpcd_handleinterface(void *, int, const char *);
    268 void dhcpcd_handlehwaddr(struct interface *, uint16_t, const void *, uint8_t);
    269 void dhcpcd_dropinterface(struct interface *, const char *);
    270 void dhcpcd_dropped(struct interface *);
    271 int dhcpcd_selectprofile(struct interface *, const char *);
    272 
    273 void dhcpcd_startinterface(void *);
    274 void dhcpcd_activateinterface(struct interface *, unsigned long long);
    275 
    276 #endif
    277