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 /* If the interface does not support carrier status (ie PPP), 70 * dhcpcd can poll it for the relevant flags periodically */ 71 #define IF_POLL_UP 100 /* milliseconds */ 72 73 #ifdef __QNX__ 74 /* QNX carries defines for, but does not actually support PF_LINK */ 75 #undef IFLR_ACTIVE 76 #endif 77 78 struct interface { 79 struct dhcpcd_ctx *ctx; 80 TAILQ_ENTRY(interface) next; 81 char name[IF_NAMESIZE]; 82 unsigned int index; 83 unsigned int active; 84 unsigned int flags; 85 sa_family_t family; 86 unsigned char hwaddr[HWADDR_LEN]; 87 uint8_t hwlen; 88 unsigned short vlanid; 89 unsigned int metric; 90 int carrier; 91 bool wireless; 92 uint8_t ssid[IF_SSIDLEN]; 93 unsigned int ssid_len; 94 95 char profile[PROFILE_LEN]; 96 struct if_options *options; 97 void *if_data[IF_DATA_MAX]; 98 }; 99 TAILQ_HEAD(if_head, interface); 100 101 #include "privsep.h" 102 103 #ifdef INET6 104 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */ 105 #if defined(__QNX) || \ 106 (defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000) 107 #undef CMSG_SPACE 108 #endif 109 110 #ifndef ALIGNBYTES 111 #define ALIGNBYTES (sizeof(int) - 1) 112 #endif 113 #ifndef ALIGN 114 #define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) 115 #endif 116 #ifndef CMSG_SPACE 117 #define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) 118 #endif 119 120 #define IP6BUFLEN (CMSG_SPACE(sizeof(struct in6_pktinfo)) + \ 121 CMSG_SPACE(sizeof(int))) 122 #endif 123 124 struct passwd; 125 126 struct dhcpcd_ctx { 127 char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; 128 int fork_fd; /* FD for the fork init signal pipe */ 129 const char *cffile; 130 unsigned long long options; 131 char *logfile; 132 int argc; 133 char **argv; 134 int ifac; /* allowed interfaces */ 135 char **ifav; /* allowed interfaces */ 136 int ifdc; /* denied interfaces */ 137 char **ifdv; /* denied interfaces */ 138 int ifc; /* listed interfaces */ 139 char **ifv; /* listed interfaces */ 140 int ifcc; /* configured interfaces */ 141 char **ifcv; /* configured interfaces */ 142 unsigned char *duid; 143 size_t duid_len; 144 struct if_head *ifaces; 145 146 rb_tree_t routes; /* our routes */ 147 #ifdef RT_FREE_ROUTE_TABLE 148 rb_tree_t froutes; /* free routes for re-use */ 149 #endif 150 size_t rt_order; /* route order storage */ 151 152 int pf_inet_fd; 153 void *priv; 154 int link_fd; 155 #ifndef SMALL 156 int link_rcvbuf; 157 #endif 158 int seq; /* route message sequence no */ 159 int sseq; /* successful seq no sent */ 160 161 #ifdef USE_SIGNALS 162 sigset_t sigset; 163 #endif 164 struct eloop *eloop; 165 166 #ifdef HAVE_OPEN_MEMSTREAM 167 FILE *script_fp; 168 #endif 169 char *script_buf; 170 size_t script_buflen; 171 char **script_env; 172 size_t script_envlen; 173 174 int control_fd; 175 int control_unpriv_fd; 176 struct fd_list_head control_fds; 177 char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE]; 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 pid_t ps_root_pid; 196 int ps_root_fd; /* Privileged Actioneer commands */ 197 int ps_data_fd; /* Data from root spawned processes */ 198 struct eloop *ps_eloop; /* eloop for polling root data */ 199 struct ps_process_head ps_processes; /* List of spawned processes */ 200 pid_t ps_inet_pid; 201 int ps_inet_fd; /* Network Proxy commands and data */ 202 #endif 203 204 #ifdef INET 205 struct dhcp_opt *dhcp_opts; 206 size_t dhcp_opts_len; 207 208 int udp_fd; 209 210 /* Our aggregate option buffer. 211 * We ONLY use this when options are split, which for most purposes is 212 * practically never. See RFC3396 for details. */ 213 uint8_t *opt_buffer; 214 size_t opt_buffer_len; 215 #endif 216 #ifdef INET6 217 uint8_t *secret; 218 size_t secret_len; 219 220 #ifndef __sun 221 int nd_fd; 222 #endif 223 struct ra_head *ra_routers; 224 225 int dhcp6_fd; 226 227 struct dhcp_opt *nd_opts; 228 size_t nd_opts_len; 229 #ifdef DHCP6 230 struct dhcp_opt *dhcp6_opts; 231 size_t dhcp6_opts_len; 232 #endif 233 234 #ifndef __linux__ 235 int ra_global; 236 #endif 237 #endif /* INET6 */ 238 239 #ifdef PLUGIN_DEV 240 char *dev_load; 241 int dev_fd; 242 struct dev *dev; 243 void *dev_handle; 244 #endif 245 }; 246 247 #ifdef USE_SIGNALS 248 extern const int dhcpcd_signals[]; 249 extern const size_t dhcpcd_signals_len; 250 #endif 251 252 int dhcpcd_ifafwaiting(const struct interface *); 253 int dhcpcd_afwaiting(const struct dhcpcd_ctx *); 254 void dhcpcd_daemonise(struct dhcpcd_ctx *); 255 256 void dhcpcd_linkoverflow(struct dhcpcd_ctx *); 257 int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **); 258 void dhcpcd_handlecarrier(struct dhcpcd_ctx *, int, unsigned int, const char *); 259 int dhcpcd_handleinterface(void *, int, const char *); 260 void dhcpcd_handlehwaddr(struct dhcpcd_ctx *, const char *, 261 const void *, uint8_t); 262 void dhcpcd_dropinterface(struct interface *, const char *); 263 int dhcpcd_selectprofile(struct interface *, const char *); 264 265 void dhcpcd_startinterface(void *); 266 void dhcpcd_activateinterface(struct interface *, unsigned long long); 267 268 #endif 269