1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2019 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 "config.h" 36 #ifdef HAVE_SYS_QUEUE_H 37 #include <sys/queue.h> 38 #endif 39 40 #include "defs.h" 41 #include "control.h" 42 #include "if-options.h" 43 44 #define HWADDR_LEN 20 45 #define IF_SSIDLEN 32 46 #define PROFILE_LEN 64 47 #define SECRET_LEN 64 48 49 #define IF_INACTIVE 0 50 #define IF_ACTIVE 1 51 #define IF_ACTIVE_USER 2 52 53 #define LINK_UP 1 54 #define LINK_UNKNOWN 0 55 #define LINK_DOWN -1 56 #define LINK_DOWN_IFFUP -2 57 58 #define IF_DATA_IPV4 0 59 #define IF_DATA_ARP 1 60 #define IF_DATA_IPV4LL 2 61 #define IF_DATA_DHCP 3 62 #define IF_DATA_IPV6 4 63 #define IF_DATA_IPV6ND 5 64 #define IF_DATA_DHCP6 6 65 #define IF_DATA_MAX 7 66 67 /* If the interface does not support carrier status (ie PPP), 68 * dhcpcd can poll it for the relevant flags periodically */ 69 #define IF_POLL_UP 100 /* milliseconds */ 70 71 #ifdef __QNX__ 72 /* QNX carries defines for, but does not actually support PF_LINK */ 73 #undef IFLR_ACTIVE 74 #endif 75 76 struct interface { 77 struct dhcpcd_ctx *ctx; 78 TAILQ_ENTRY(interface) next; 79 char name[IF_NAMESIZE]; 80 unsigned int index; 81 unsigned int active; 82 unsigned int flags; 83 sa_family_t family; 84 unsigned char hwaddr[HWADDR_LEN]; 85 uint8_t hwlen; 86 unsigned short vlanid; 87 unsigned int metric; 88 int carrier; 89 bool wireless; 90 uint8_t ssid[IF_SSIDLEN]; 91 unsigned int ssid_len; 92 93 char profile[PROFILE_LEN]; 94 struct if_options *options; 95 void *if_data[IF_DATA_MAX]; 96 }; 97 TAILQ_HEAD(if_head, interface); 98 99 100 #ifdef INET6 101 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */ 102 #if defined(__QNX) || \ 103 (defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000) 104 #undef CMSG_SPACE 105 #endif 106 107 #ifndef ALIGNBYTES 108 #define ALIGNBYTES (sizeof(int) - 1) 109 #endif 110 #ifndef ALIGN 111 #define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) 112 #endif 113 #ifndef CMSG_SPACE 114 #define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) 115 #endif 116 117 #define IP6BUFLEN (CMSG_SPACE(sizeof(struct in6_pktinfo)) + \ 118 CMSG_SPACE(sizeof(int))) 119 #endif 120 121 struct dhcpcd_ctx { 122 char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; 123 const char *cffile; 124 unsigned long long options; 125 char *logfile; 126 int argc; 127 char **argv; 128 int ifac; /* allowed interfaces */ 129 char **ifav; /* allowed interfaces */ 130 int ifdc; /* denied interfaces */ 131 char **ifdv; /* denied interfaces */ 132 int ifc; /* listed interfaces */ 133 char **ifv; /* listed interfaces */ 134 int ifcc; /* configured interfaces */ 135 char **ifcv; /* configured interfaces */ 136 unsigned char *duid; 137 size_t duid_len; 138 struct if_head *ifaces; 139 140 rb_tree_t routes; /* our routes */ 141 #ifdef RT_FREE_ROUTE_TABLE 142 rb_tree_t froutes; /* free routes for re-use */ 143 #endif 144 size_t rt_order; /* route order storage */ 145 146 int pf_inet_fd; 147 void *priv; 148 int link_fd; 149 #ifndef SMALL 150 int link_rcvbuf; 151 #endif 152 int seq; /* route message sequence no */ 153 int sseq; /* successful seq no sent */ 154 155 #ifdef USE_SIGNALS 156 sigset_t sigset; 157 #endif 158 struct eloop *eloop; 159 160 #ifdef HAVE_OPEN_MEMSTREAM 161 FILE *script_fp; 162 #endif 163 char *script_buf; 164 size_t script_buflen; 165 char **script_env; 166 size_t script_envlen; 167 168 int control_fd; 169 int control_unpriv_fd; 170 struct fd_list_head control_fds; 171 char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE]; 172 gid_t control_group; 173 174 /* DHCP Enterprise options, RFC3925 */ 175 struct dhcp_opt *vivso; 176 size_t vivso_len; 177 178 char *randomstate; /* original state */ 179 180 #ifdef INET 181 struct dhcp_opt *dhcp_opts; 182 size_t dhcp_opts_len; 183 184 int udp_fd; 185 186 /* Our aggregate option buffer. 187 * We ONLY use this when options are split, which for most purposes is 188 * practically never. See RFC3396 for details. */ 189 uint8_t *opt_buffer; 190 size_t opt_buffer_len; 191 #endif 192 #ifdef INET6 193 uint8_t *secret; 194 size_t secret_len; 195 196 #ifndef __sun 197 int nd_fd; 198 #endif 199 struct ra_head *ra_routers; 200 201 int dhcp6_fd; 202 203 struct dhcp_opt *nd_opts; 204 size_t nd_opts_len; 205 #ifdef DHCP6 206 struct dhcp_opt *dhcp6_opts; 207 size_t dhcp6_opts_len; 208 #endif 209 210 #ifndef __linux__ 211 int ra_global; 212 #endif 213 #endif /* INET6 */ 214 215 #ifdef PLUGIN_DEV 216 char *dev_load; 217 int dev_fd; 218 struct dev *dev; 219 void *dev_handle; 220 #endif 221 }; 222 223 #ifdef USE_SIGNALS 224 extern const int dhcpcd_signals[]; 225 extern const size_t dhcpcd_signals_len; 226 #endif 227 228 int dhcpcd_ifafwaiting(const struct interface *); 229 int dhcpcd_afwaiting(const struct dhcpcd_ctx *); 230 pid_t dhcpcd_daemonise(struct dhcpcd_ctx *); 231 232 void dhcpcd_linkoverflow(struct dhcpcd_ctx *); 233 int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **); 234 void dhcpcd_handlecarrier(struct dhcpcd_ctx *, int, unsigned int, const char *); 235 int dhcpcd_handleinterface(void *, int, const char *); 236 void dhcpcd_handlehwaddr(struct dhcpcd_ctx *, const char *, 237 const void *, uint8_t); 238 void dhcpcd_dropinterface(struct interface *, const char *); 239 int dhcpcd_selectprofile(struct interface *, const char *); 240 241 void dhcpcd_startinterface(void *); 242 void dhcpcd_activateinterface(struct interface *, unsigned long long); 243 244 #endif 245