1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 #include <netinet/in.h>
39 # if !defined(__QNX__) && !defined(__sun)
40 # include <sys/endian.h>
44 # include <netinet6/in6_var.h>
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)
53 #ifndef ND6_INFINITE_LIFETIME
54 # define ND6_INFINITE_LIFETIME ((uint32_t)~0)
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
64 /* RFC7217 constants */
65 #define IDGEN_RETRIES 3
66 #define IDGEN_DELAY 1 /* second */
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)
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 )
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 }}}
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 }}}
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
98 /* We guard here to avoid breaking a compile on linux ppc-64 headers */
99 # include <sys/param.h>
102 # define IPV6_POLLADDRFLAG
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
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
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
126 #define IN6_IFF_NOTUSEABLE \
127 (IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED)
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.
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.
139 #if defined(IN6_IFF_TEMPORARY) && !defined(__linux__)
140 #define IPV6_MANAGETEMPADDR
144 /* Match Linux defines to BSD */
145 # ifdef IFA_F_TEMPORARY
146 # define IN6_IFF_TEMPORARY IFA_F_TEMPORARY
148 # ifdef IFA_F_OPTIMISTIC
149 # define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC)
151 # define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | 0x04)
153 # ifdef IF_F_DADFAILED
154 # define IN6_IFF_DUPLICATED IFA_F_DADFAILED
156 # define IN6_IFF_DUPLICATED 0x08
158 # define IN6_IFF_DETACHED 0
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.
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
175 TAILQ_HEAD(ipv6_addrhead, ipv6_addr);
177 TAILQ_ENTRY(ipv6_addr) next;
178 struct interface *iface;
179 struct in6_addr prefix;
181 uint32_t prefix_vltime;
182 uint32_t prefix_pltime;
183 struct timespec created;
184 struct timespec acquired;
185 struct in6_addr addr;
188 char saddr[INET6_ADDRSTRLEN];
194 struct ipv6_addr *delegating_prefix;
195 struct ipv6_addrhead pd_pfxs;
196 TAILQ_ENTRY(ipv6_addr) pd_next;
198 uint8_t prefix_exclude_len;
199 struct in6_addr prefix_exclude;
202 void (*dadcallback)(void *);
205 struct nd_neighbor_advert *na;
210 char alias[IF_NAMESIZE];
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_DUPLICATED (1U << 5)
220 #define IPV6_AF_DADCOMPLETED (1U << 6)
221 #define IPV6_AF_DELEGATED (1U << 7)
222 #define IPV6_AF_DELEGATEDPFX (1U << 8)
223 #define IPV6_AF_NOREJECT (1U << 9)
224 #define IPV6_AF_REQUEST (1U << 10)
225 #define IPV6_AF_STATIC (1U << 11)
226 #define IPV6_AF_DELEGATEDLOG (1U << 12)
227 #define IPV6_AF_RAPFX (1U << 13)
228 #define IPV6_AF_EXTENDED (1U << 14)
229 #ifdef IPV6_MANAGETEMPADDR
230 #define IPV6_AF_TEMPORARY (1U << 15)
234 TAILQ_ENTRY(ll_callback) next;
235 void (*callback)(void *);
238 TAILQ_HEAD(ll_callback_head, ll_callback);
241 struct ipv6_addrhead addrs;
242 struct ll_callback_head ll_callbacks;
244 #ifdef IPV6_MANAGETEMPADDR
245 uint32_t desync_factor;
249 #define IPV6_STATE(ifp) \
250 ((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))
256 int ipv6_init(struct dhcpcd_ctx *);
257 int ipv6_makestableprivate(struct in6_addr *,
258 const struct in6_addr *, int, const struct interface *, int *);
259 int ipv6_makeaddr(struct in6_addr *, struct interface *,
260 const struct in6_addr *, int, unsigned int);
261 int ipv6_mask(struct in6_addr *, int);
262 uint8_t ipv6_prefixlen(const struct in6_addr *);
263 int ipv6_userprefix( const struct in6_addr *, short prefix_len,
264 uint64_t user_number, struct in6_addr *result, short result_len);
265 void ipv6_checkaddrflags(void *);
266 void ipv6_markaddrsstale(struct interface *, unsigned int);
267 void ipv6_deletestaleaddrs(struct interface *);
268 int ipv6_addaddr(struct ipv6_addr *, const struct timespec *);
269 int ipv6_doaddr(struct ipv6_addr *, struct timespec *);
270 ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs);
271 void ipv6_deleteaddr(struct ipv6_addr *);
272 void ipv6_freedrop_addrs(struct ipv6_addrhead *, int,
273 const struct interface *);
274 void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *,
275 const char *, const struct in6_addr *, uint8_t, int, pid_t);
276 int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, const struct ipv6_addr *,
278 struct ipv6_addr *ipv6_iffindaddr(struct interface *,
279 const struct in6_addr *, int);
280 int ipv6_hasaddr(const struct interface *);
281 struct ipv6_addr *ipv6_anyglobal(struct interface *);
282 int ipv6_findaddrmatch(const struct ipv6_addr *, const struct in6_addr *,
284 struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *,
285 const struct in6_addr *, unsigned int);
286 struct ipv6_addr *ipv6_findmaskaddr(struct dhcpcd_ctx *,
287 const struct in6_addr *);
288 #define ipv6_linklocal(ifp) ipv6_iffindaddr((ifp), NULL, IN6_IFF_NOTUSEABLE)
289 int ipv6_addlinklocalcallback(struct interface *, void (*)(void *), void *);
290 struct ipv6_addr *ipv6_newaddr(struct interface *, const struct in6_addr *, uint8_t,
292 void ipv6_freeaddr(struct ipv6_addr *);
293 void ipv6_freedrop(struct interface *, int);
294 #define ipv6_free(ifp) ipv6_freedrop((ifp), 0)
295 #define ipv6_drop(ifp) ipv6_freedrop((ifp), 2)
297 #ifdef IPV6_MANAGETEMPADDR
298 struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *,
299 const struct timespec *);
300 struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int);
301 void ipv6_addtempaddrs(struct interface *, const struct timespec *);
302 void ipv6_regentempaddrs(void *);
305 int ipv6_start(struct interface *);
306 int ipv6_staticdadcompleted(const struct interface *);
307 int ipv6_startstatic(struct interface *);
308 ssize_t ipv6_env(FILE *, const char *, const struct interface *);
309 void ipv6_ctxfree(struct dhcpcd_ctx *);
310 bool inet6_getroutes(struct dhcpcd_ctx *, rb_tree_t *);