1 /* 2 * dhcpcd - DHCP client daemon 3 * SPDX-License-Identifier: BSD-2-Clause 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 IPV6_H 30 #define IPV6_H 31 32 #include <sys/uio.h> 33 34 #include <netinet/in.h> 35 36 #include "config.h" 37 #include "if.h" 38 39 #ifndef __linux__ 40 #if !defined(__QNX__) && !defined(__sun) 41 #include <sys/endian.h> 42 #endif 43 #include <net/if.h> 44 #ifndef __sun 45 #include <netinet6/in6_var.h> 46 #endif 47 #endif 48 49 #define EUI64_GBIT 0x01 50 #define EUI64_UBIT 0x02 51 #define EUI64_TO_IFID(in6) \ 52 do { \ 53 (in6)->s6_addr[8] ^= EUI64_UBIT; \ 54 } while (0) 55 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 56 57 #ifndef ND6_INFINITE_LIFETIME 58 #define ND6_INFINITE_LIFETIME ((uint32_t)~0) 59 #endif 60 61 /* RFC4941 constants */ 62 #define TEMP_VALID_LIFETIME 604800 /* 1 week */ 63 #define TEMP_PREFERRED_LIFETIME 86400 /* 1 day */ 64 #define REGEN_ADVANCE 5 /* seconds */ 65 #define MAX_DESYNC_FACTOR 600 /* 10 minutes */ 66 #define TEMP_IDGEN_RETRIES 3 67 68 /* RFC7217 constants */ 69 #define IDGEN_RETRIES 3 70 #define IDGEN_DELAY 1 /* second */ 71 72 /* Interface identifier length. Prefix + this == 128 for autoconf */ 73 #define ipv6_ifidlen(ifp) 64 74 #define IA6_CANAUTOCONF(ia) \ 75 ((ia)->prefix_len + ipv6_ifidlen((ia)->iface) == 128) 76 77 #ifndef IN6_ARE_MASKED_ADDR_EQUAL 78 #define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m) \ 79 ((((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \ 80 (((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == \ 81 0 && \ 82 (((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == \ 83 0 && \ 84 (((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == \ 85 0) 86 #endif 87 88 #ifndef IN6ADDR_LINKLOCAL_ALLNODES_INIT 89 #define IN6ADDR_LINKLOCAL_ALLNODES_INIT \ 90 { \ 91 {{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }} \ 93 } 94 #endif 95 #ifndef IN6ADDR_LINKLOCAL_ALLROUTERS_INIT 96 #define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \ 97 { \ 98 {{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }} \ 100 } 101 #endif 102 103 /* 104 * BSD kernels don't inform userland of DAD results. 105 * See the discussion here: 106 * http://mail-index.netbsd.org/tech-net/2013/03/15/msg004019.html 107 */ 108 #ifndef __linux__ 109 /* We guard here to avoid breaking a compile on linux ppc-64 headers */ 110 #include <sys/param.h> 111 #endif 112 #ifdef BSD 113 #define IPV6_POLLADDRFLAG 114 #endif 115 116 /* This was fixed in NetBSD */ 117 #if (defined(__DragonFly_version) && __DragonFly_version >= 500704) || \ 118 (defined(__NetBSD_Version__) && __NetBSD_Version__ >= 699002000) 119 #undef IPV6_POLLADDRFLAG 120 #endif 121 122 /* Of course OpenBSD has their own special name. */ 123 #if !defined(IN6_IFF_TEMPORARY) && defined(IN6_IFF_PRIVACY) 124 #define IN6_IFF_TEMPORARY IN6_IFF_PRIVACY 125 #endif 126 127 #ifdef __sun 128 /* Solaris lacks these defines. 129 * While it supports DaD, to seems to only expose IFF_DUPLICATE 130 * so we have no way of knowing if it's tentative or not. 131 * I don't even know if Solaris has any special treatment for tentative. */ 132 #define IN6_IFF_TENTATIVE 0x02 133 #define IN6_IFF_DUPLICATED 0x04 134 #define IN6_IFF_DETACHED 0x00 135 #endif 136 137 #define IN6_IFF_NOTUSEABLE \ 138 (IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED) 139 140 /* 141 * If dhcpcd handles RA processing instead of the kernel, the kernel needs 142 * to either allow userland to set temporary addresses or mark an address 143 * for the kernel to manage temporary addresses from. 144 * If the kernel allows the former, a global #define is needed, otherwise 145 * the address marking will be handled in the platform specific address handler. 146 * 147 * Some BSDs do not allow userland to set temporary addresses. 148 * Linux-3.18 allows the marking of addresses from which to manage temp addrs. 149 */ 150 #if defined(IN6_IFF_TEMPORARY) && !defined(__linux__) 151 #define IPV6_MANAGETEMPADDR 152 #endif 153 154 #ifdef __linux__ 155 /* Match Linux defines to BSD */ 156 #ifdef IFA_F_TEMPORARY 157 #define IN6_IFF_TEMPORARY IFA_F_TEMPORARY 158 #endif 159 #ifdef IFA_F_OPTIMISTIC 160 #define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC) 161 #else 162 #define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | 0x04) 163 #endif 164 #ifdef IF_F_DADFAILED 165 #define IN6_IFF_DUPLICATED IFA_F_DADFAILED 166 #else 167 #define IN6_IFF_DUPLICATED 0x08 168 #endif 169 #define IN6_IFF_DETACHED 0 170 #endif 171 172 #ifdef INET6 173 TAILQ_HEAD(ipv6_addrhead, ipv6_addr); 174 struct ipv6_addr { 175 TAILQ_ENTRY(ipv6_addr) next; 176 struct interface *iface; 177 struct in6_addr prefix; 178 uint8_t prefix_len; 179 uint32_t prefix_vltime; 180 uint32_t prefix_pltime; 181 struct timespec created; 182 struct timespec acquired; 183 struct in6_addr addr; 184 struct in6_addr dstaddr; 185 int addr_flags; 186 unsigned int flags; 187 char saddr[INET6_ADDRSTRLEN]; 188 uint8_t iaid[4]; 189 uint16_t ia_type; 190 int dhcp6_fd; 191 192 #ifndef SMALL 193 struct ipv6_addr *delegating_prefix; 194 struct ipv6_addrhead pd_pfxs; 195 TAILQ_ENTRY(ipv6_addr) pd_next; 196 197 uint8_t prefix_exclude_len; 198 struct in6_addr prefix_exclude; 199 #endif 200 201 void (*dadcallback)(void *); 202 int dadcounter; 203 204 struct nd_neighbor_advert *na; 205 size_t na_len; 206 int na_count; 207 208 #ifdef ALIAS_ADDR 209 char alias[IF_NAMESIZE]; 210 #endif 211 }; 212 213 #define IPV6_AF_ONLINK (1U << 0) 214 #define IPV6_AF_NEW (1U << 1) 215 #define IPV6_AF_STALE (1U << 2) 216 #define IPV6_AF_ADDED (1U << 3) 217 #define IPV6_AF_AUTOCONF (1U << 4) 218 #define IPV6_AF_DADCOMPLETED (1U << 5) 219 #define IPV6_AF_PFXDELEGATION (1U << 6) 220 #define IPV6_AF_DELEGATED (1U << 7) 221 #define IPV6_AF_NOREJECT (1U << 8) 222 #define IPV6_AF_REQUEST (1U << 9) 223 #define IPV6_AF_STATIC (1U << 10) 224 #define IPV6_AF_DELEGATEDLOG (1U << 11) 225 #define IPV6_AF_RAPFX (1U << 12) 226 #define IPV6_AF_EXTENDED (1U << 13) 227 #define IPV6_AF_REGEN (1U << 14) 228 #define IPV6_AF_ROUTER (1U << 15) 229 #define IPV6_AF_ADVERTISED (1U << 16) 230 #ifdef IPV6_MANAGETEMPADDR 231 #define IPV6_AF_TEMPORARY (1U << 17) 232 #endif 233 234 struct ll_callback { 235 TAILQ_ENTRY(ll_callback) next; 236 void (*callback)(void *); 237 void *arg; 238 }; 239 TAILQ_HEAD(ll_callback_head, ll_callback); 240 241 struct ipv6_state { 242 struct ipv6_addrhead addrs; 243 struct ll_callback_head ll_callbacks; 244 245 #ifdef IPV6_MANAGETEMPADDR 246 uint32_t desync_factor; 247 #endif 248 }; 249 250 #define IPV6_STATE(ifp) ((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) 251 #define IPV6_CSTATE(ifp) \ 252 ((const struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) 253 #define IPV6_STATE_RUNNING(ifp) ipv6_staticdadcompleted((ifp)) 254 255 int ipv6_init(struct dhcpcd_ctx *); 256 int ipv6_makestableprivate(struct in6_addr *, const struct in6_addr *, int, 257 const struct interface *, int *); 258 int ipv6_makeaddr(struct in6_addr *, struct interface *, 259 const struct in6_addr *, int, unsigned int); 260 int ipv6_mask(struct in6_addr *, int); 261 uint8_t ipv6_prefixlen(const struct in6_addr *); 262 int ipv6_userprefix(const struct in6_addr *, short prefix_len, 263 uint64_t user_number, struct in6_addr *result, short result_len); 264 void ipv6_checkaddrflags(void *); 265 void ipv6_markaddrsstale(struct interface *, unsigned int); 266 void ipv6_deletestaleaddrs(struct interface *); 267 int ipv6_addaddr(struct ipv6_addr *, struct timespec *); 268 int ipv6_doaddr(struct ipv6_addr *, struct timespec *); 269 ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs); 270 void ipv6_deleteaddr(struct ipv6_addr *); 271 void ipv6_freedrop_addrs(struct ipv6_addrhead *, int, unsigned int, 272 const struct interface *); 273 void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *, const char *, 274 const struct in6_addr *, uint8_t, const struct in6_addr *, int, pid_t); 275 int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, const struct ipv6_addr *, 276 pid_t); 277 struct ipv6_addr *ipv6_iffindaddr(struct interface *, const struct in6_addr *, 278 int); 279 int ipv6_hasaddr(const struct interface *); 280 struct ipv6_addr *ipv6_anyglobal(struct interface *); 281 int ipv6_findaddrmatch(const struct ipv6_addr *, const struct in6_addr *, 282 unsigned int); 283 struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *, const struct in6_addr *, 284 unsigned int); 285 struct ipv6_addr *ipv6_findmaskaddr(struct dhcpcd_ctx *, 286 const struct in6_addr *); 287 struct ipv6_addr *ipv6_finddstaddr(struct dhcpcd_ctx *, 288 const struct in6_addr *); 289 #define ipv6_linklocal(ifp) ipv6_iffindaddr((ifp), NULL, IN6_IFF_NOTUSEABLE) 290 int ipv6_addlinklocalcallback(struct interface *, void (*)(void *), void *); 291 void ipv6_setscope(struct sockaddr_in6 *, unsigned int); 292 unsigned int ipv6_getscope(const struct sockaddr_in6 *); 293 struct ipv6_addr *ipv6_newaddr(struct interface *, const struct in6_addr *, 294 uint8_t, unsigned int); 295 void ipv6_freeaddr(struct ipv6_addr *); 296 void ipv6_freedrop(struct interface *, int); 297 #define ipv6_free(ifp) ipv6_freedrop((ifp), 0) 298 #define ipv6_drop(ifp) ipv6_freedrop((ifp), 2) 299 300 #ifdef IPV6_MANAGETEMPADDR 301 struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *, 302 const struct timespec *); 303 struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int); 304 void ipv6_addtempaddrs(struct interface *, struct timespec *); 305 void ipv6_regentempaddrs(void *); 306 #endif 307 308 int ipv6_start(struct interface *); 309 int ipv6_staticdadcompleted(const struct interface *); 310 int ipv6_startstatic(struct interface *); 311 ssize_t ipv6_env(FILE *, const char *, const struct interface *); 312 void ipv6_ctxfree(struct dhcpcd_ctx *); 313 bool inet6_getroutes(struct dhcpcd_ctx *, rb_tree_t *); 314 #endif /* INET6 */ 315 316 #endif /* INET6_H */ 317