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