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