1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2020 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 #define LINK_DOWN_IFFUP -2 59 60 #define IF_DATA_IPV4 0 61 #define IF_DATA_ARP 1 62 #define IF_DATA_IPV4LL 2 63 #define IF_DATA_DHCP 3 64 #define IF_DATA_IPV6 4 65 #define IF_DATA_IPV6ND 5 66 #define IF_DATA_DHCP6 6 67 #define IF_DATA_MAX 7 68 69 #ifdef __QNX__ 70 /* QNX carries defines for, but does not actually support PF_LINK */ 71 #undef IFLR_ACTIVE 72 #endif 73 74 struct interface { 75 struct dhcpcd_ctx *ctx; 76 TAILQ_ENTRY(interface) next; 77 char name[IF_NAMESIZE]; 78 unsigned int index; 79 unsigned int active; 80 unsigned int flags; 81 uint16_t hwtype; /* ARPHRD_ETHER for example */ 82 unsigned char hwaddr[HWADDR_LEN]; 83 uint8_t hwlen; 84 unsigned short vlanid; 85 unsigned int metric; 86 int carrier; 87 bool wireless; 88 uint8_t ssid[IF_SSIDLEN]; 89 unsigned int ssid_len; 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 #ifdef INET6 100 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */ 101 #if defined(__QNX) || \ 102 (defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000) 103 #undef CMSG_SPACE 104 #endif 105 106 #ifndef ALIGNBYTES 107 #define ALIGNBYTES (sizeof(int) - 1) 108 #endif 109 #ifndef ALIGN 110 #define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) 111 #endif 112 #ifndef CMSG_SPACE 113 #define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) 114 #endif 115 116 #define IP6BUFLEN (CMSG_SPACE(sizeof(struct in6_pktinfo)) + \ 117 CMSG_SPACE(sizeof(int))) 118 #endif 119 120 struct passwd; 121 122 struct dhcpcd_ctx { 123 char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; 124 char vendor[256]; 125 int fork_fd; /* FD for the fork init signal pipe */ 126 const char *cffile; 127 unsigned long long options; 128 char *logfile; 129 int argc; 130 char **argv; 131 int ifac; /* allowed interfaces */ 132 char **ifav; /* allowed interfaces */ 133 int ifdc; /* denied interfaces */ 134 char **ifdv; /* denied interfaces */ 135 int ifc; /* listed interfaces */ 136 char **ifv; /* listed interfaces */ 137 int ifcc; /* configured interfaces */ 138 char **ifcv; /* configured interfaces */ 139 unsigned char *duid; 140 size_t duid_len; 141 struct if_head *ifaces; 142 143 char *ctl_buf; 144 size_t ctl_buflen; 145 size_t ctl_bufpos; 146 size_t ctl_extra; 147 148 rb_tree_t routes; /* our routes */ 149 #ifdef RT_FREE_ROUTE_TABLE 150 rb_tree_t froutes; /* free routes for re-use */ 151 #endif 152 size_t rt_order; /* route order storage */ 153 154 int pf_inet_fd; 155 #ifdef PF_LINK 156 int pf_link_fd; 157 #endif 158 void *priv; 159 int link_fd; 160 #ifndef SMALL 161 int link_rcvbuf; 162 #endif 163 int seq; /* route message sequence no */ 164 int sseq; /* successful seq no sent */ 165 166 #ifdef USE_SIGNALS 167 sigset_t sigset; 168 #endif 169 struct eloop *eloop; 170 171 char *script; 172 #ifdef HAVE_OPEN_MEMSTREAM 173 FILE *script_fp; 174 #endif 175 char *script_buf; 176 size_t script_buflen; 177 char **script_env; 178 size_t script_envlen; 179 180 int control_fd; 181 int control_unpriv_fd; 182 struct fd_list_head control_fds; 183 char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE]; 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 pid_t ps_root_pid; 202 int ps_root_fd; /* Privileged Actioneer commands */ 203 int ps_data_fd; /* Data from root spawned processes */ 204 struct eloop *ps_eloop; /* eloop for polling root data */ 205 struct ps_process_head ps_processes; /* List of spawned processes */ 206 pid_t ps_inet_pid; 207 int ps_inet_fd; /* Network Proxy commands and data */ 208 pid_t ps_control_pid; 209 int ps_control_fd; /* Control Proxy - generic listener */ 210 int ps_control_data_fd; /* Control Proxy - data query */ 211 struct fd_list *ps_control; /* Queue for the above */ 212 struct fd_list *ps_control_client; /* Queue for the above */ 213 #endif 214 215 #ifdef INET 216 struct dhcp_opt *dhcp_opts; 217 size_t dhcp_opts_len; 218 219 int udp_rfd; 220 int udp_wfd; 221 222 /* Our aggregate option buffer. 223 * We ONLY use this when options are split, which for most purposes is 224 * practically never. See RFC3396 for details. */ 225 uint8_t *opt_buffer; 226 size_t opt_buffer_len; 227 #endif 228 #ifdef INET6 229 uint8_t *secret; 230 size_t secret_len; 231 232 #ifndef __sun 233 int nd_fd; 234 #endif 235 struct ra_head *ra_routers; 236 237 struct dhcp_opt *nd_opts; 238 size_t nd_opts_len; 239 #ifdef DHCP6 240 int dhcp6_rfd; 241 int dhcp6_wfd; 242 struct dhcp_opt *dhcp6_opts; 243 size_t dhcp6_opts_len; 244 #endif 245 246 #ifndef __linux__ 247 int ra_global; 248 #endif 249 #endif /* INET6 */ 250 251 #ifdef PLUGIN_DEV 252 char *dev_load; 253 int dev_fd; 254 struct dev *dev; 255 void *dev_handle; 256 #endif 257 }; 258 259 #ifdef USE_SIGNALS 260 extern const int dhcpcd_signals[]; 261 extern const size_t dhcpcd_signals_len; 262 extern const int dhcpcd_signals_ignore[]; 263 extern const size_t dhcpcd_signals_ignore_len; 264 #endif 265 266 extern const char *dhcpcd_default_script; 267 268 int dhcpcd_ifafwaiting(const struct interface *); 269 int dhcpcd_afwaiting(const struct dhcpcd_ctx *); 270 void dhcpcd_daemonise(struct dhcpcd_ctx *); 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 dhcpcd_ctx *, int, unsigned int, const char *); 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