| 1 | /* |
| 2 | * dhcpcd - DHCP client daemon |
| 3 | * Copyright (c) 2006-2019 Roy Marples <roy@marples.name> |
| 4 | * All rights reserved |
| 5 | |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 | * SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef DHCPCD_H |
| 29 | #define DHCPCD_H |
| 30 | |
| 31 | #include <sys/socket.h> |
| 32 | #include <net/if.h> |
| 33 | |
| 34 | #include "config.h" |
| 35 | #ifdef HAVE_SYS_QUEUE_H |
| 36 | #include <sys/queue.h> |
| 37 | #endif |
| 38 | |
| 39 | #include "defs.h" |
| 40 | #include "control.h" |
| 41 | #include "if-options.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 | #define LINK_DOWN_IFFUP -2 |
| 56 | |
| 57 | #define IF_DATA_IPV4 0 |
| 58 | #define IF_DATA_ARP 1 |
| 59 | #define IF_DATA_IPV4LL 2 |
| 60 | #define IF_DATA_DHCP 3 |
| 61 | #define IF_DATA_IPV6 4 |
| 62 | #define IF_DATA_IPV6ND 5 |
| 63 | #define IF_DATA_DHCP6 6 |
| 64 | #define IF_DATA_MAX 7 |
| 65 | |
| 66 | /* If the interface does not support carrier status (ie PPP), |
| 67 | * dhcpcd can poll it for the relevant flags periodically */ |
| 68 | #define IF_POLL_UP 100 /* milliseconds */ |
| 69 | |
| 70 | #ifdef __QNX__ |
| 71 | /* QNX carries defines for, but does not actually support PF_LINK */ |
| 72 | #undef IFLR_ACTIVE |
| 73 | #endif |
| 74 | |
| 75 | struct interface { |
| 76 | struct dhcpcd_ctx *ctx; |
| 77 | TAILQ_ENTRY(interface) next; |
| 78 | char name[IF_NAMESIZE]; |
| 79 | unsigned int index; |
| 80 | unsigned int active; |
| 81 | unsigned int flags; |
| 82 | sa_family_t family; |
| 83 | unsigned char hwaddr[HWADDR_LEN]; |
| 84 | uint8_t hwlen; |
| 85 | unsigned short vlanid; |
| 86 | unsigned int metric; |
| 87 | int carrier; |
| 88 | bool wireless; |
| 89 | uint8_t ssid[IF_SSIDLEN]; |
| 90 | unsigned int ssid_len; |
| 91 | |
| 92 | char profile[PROFILE_LEN]; |
| 93 | struct if_options *options; |
| 94 | void *if_data[IF_DATA_MAX]; |
| 95 | }; |
| 96 | TAILQ_HEAD(if_head, interface); |
| 97 | |
| 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 dhcpcd_ctx { |
| 121 | char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; |
| 122 | const char *cffile; |
| 123 | unsigned long long options; |
| 124 | char *logfile; |
| 125 | int argc; |
| 126 | char **argv; |
| 127 | int ifac; /* allowed interfaces */ |
| 128 | char **ifav; /* allowed interfaces */ |
| 129 | int ifdc; /* denied interfaces */ |
| 130 | char **ifdv; /* denied interfaces */ |
| 131 | int ifc; /* listed interfaces */ |
| 132 | char **ifv; /* listed interfaces */ |
| 133 | int ifcc; /* configured interfaces */ |
| 134 | char **ifcv; /* configured interfaces */ |
| 135 | unsigned char *duid; |
| 136 | size_t duid_len; |
| 137 | struct if_head *ifaces; |
| 138 | |
| 139 | struct rt_head routes; /* our routes */ |
| 140 | struct rt_head kroutes; /* all kernel routes */ |
| 141 | struct rt_head froutes; /* free routes for re-use */ |
| 142 | |
| 143 | int pf_inet_fd; |
| 144 | void *priv; |
| 145 | int link_fd; |
| 146 | int seq; /* route message sequence no */ |
| 147 | int sseq; /* successful seq no sent */ |
| 148 | |
| 149 | #ifdef USE_SIGNALS |
| 150 | sigset_t sigset; |
| 151 | #endif |
| 152 | struct eloop *eloop; |
| 153 | |
| 154 | int control_fd; |
| 155 | int control_unpriv_fd; |
| 156 | struct fd_list_head control_fds; |
| 157 | char control_sock[sizeof(CONTROLSOCKET) + IF_NAMESIZE]; |
| 158 | gid_t control_group; |
| 159 | |
| 160 | /* DHCP Enterprise options, RFC3925 */ |
| 161 | struct dhcp_opt *vivso; |
| 162 | size_t vivso_len; |
| 163 | |
| 164 | char *randomstate; /* original state */ |
| 165 | |
| 166 | #ifdef INET |
| 167 | struct dhcp_opt *dhcp_opts; |
| 168 | size_t dhcp_opts_len; |
| 169 | |
| 170 | int udp_fd; |
| 171 | |
| 172 | /* Our aggregate option buffer. |
| 173 | * We ONLY use this when options are split, which for most purposes is |
| 174 | * practically never. See RFC3396 for details. */ |
| 175 | uint8_t *opt_buffer; |
| 176 | size_t opt_buffer_len; |
| 177 | #endif |
| 178 | #ifdef INET6 |
| 179 | uint8_t *secret; |
| 180 | size_t secret_len; |
| 181 | |
| 182 | #ifndef __sun |
| 183 | int nd_fd; |
| 184 | #endif |
| 185 | struct ra_head *ra_routers; |
| 186 | |
| 187 | int dhcp6_fd; |
| 188 | |
| 189 | struct dhcp_opt *nd_opts; |
| 190 | size_t nd_opts_len; |
| 191 | #ifdef DHCP6 |
| 192 | struct dhcp_opt *dhcp6_opts; |
| 193 | size_t dhcp6_opts_len; |
| 194 | #endif |
| 195 | |
| 196 | #ifndef __linux__ |
| 197 | int ra_global; |
| 198 | #endif |
| 199 | #endif /* INET6 */ |
| 200 | |
| 201 | #ifdef PLUGIN_DEV |
| 202 | char *dev_load; |
| 203 | int dev_fd; |
| 204 | struct dev *dev; |
| 205 | void *dev_handle; |
| 206 | #endif |
| 207 | }; |
| 208 | |
| 209 | #ifdef USE_SIGNALS |
| 210 | extern const int dhcpcd_signals[]; |
| 211 | extern const size_t dhcpcd_signals_len; |
| 212 | #endif |
| 213 | |
| 214 | int dhcpcd_ifafwaiting(const struct interface *); |
| 215 | int dhcpcd_afwaiting(const struct dhcpcd_ctx *); |
| 216 | pid_t dhcpcd_daemonise(struct dhcpcd_ctx *); |
| 217 | |
| 218 | void dhcpcd_linkoverflow(struct dhcpcd_ctx *); |
| 219 | int dhcpcd_handleargs(struct dhcpcd_ctx *, struct fd_list *, int, char **); |
| 220 | void dhcpcd_handlecarrier(struct dhcpcd_ctx *, int, unsigned int, const char *); |
| 221 | int dhcpcd_handleinterface(void *, int, const char *); |
| 222 | void dhcpcd_handlehwaddr(struct dhcpcd_ctx *, const char *, |
| 223 | const void *, uint8_t); |
| 224 | void dhcpcd_dropinterface(struct interface *, const char *); |
| 225 | int dhcpcd_selectprofile(struct interface *, const char *); |
| 226 | |
| 227 | void dhcpcd_startinterface(void *); |
| 228 | void dhcpcd_activateinterface(struct interface *, unsigned long long); |
| 229 | |
| 230 | #endif |
| 231 | |