1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 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 */
63 #define TEMP_IDGEN_RETRIES 3
64 #define GEN_TEMPID_RETRY_MAX 5
66 /* RFC7217 constants */
67 #define IDGEN_RETRIES 3
68 #define IDGEN_DELAY 1 /* second */
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 )
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 }}}
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 }}}
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
95 /* We guard here to avoid breaking a compile on linux ppc-64 headers */
96 # include <sys/param.h>
99 # define IPV6_POLLADDRFLAG
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
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
118 #define IN6_IFF_NOTUSEABLE \
119 (IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED)
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.
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.
131 #if defined(IN6_IFF_TEMPORARY) && !defined(__linux__)
132 #define IPV6_MANAGETEMPADDR
136 /* Match Linux defines to BSD */
137 # ifdef IFA_F_TEMPORARY
138 # define IN6_IFF_TEMPORARY IFA_F_TEMPORARY
140 # ifdef IFA_F_OPTIMISTIC
141 # define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC)
143 # define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | 0x04)
145 # ifdef IF_F_DADFAILED
146 # define IN6_IFF_DUPLICATED IFA_F_DADFAILED
148 # define IN6_IFF_DUPLICATED 0x08
150 # define IN6_IFF_DETACHED 0
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.
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
167 TAILQ_HEAD(ipv6_addrhead
, ipv6_addr
);
169 TAILQ_ENTRY(ipv6_addr
) next
;
170 struct interface
*iface
;
171 struct in6_addr prefix
;
173 uint32_t prefix_vltime
;
174 uint32_t prefix_pltime
;
175 struct timespec created
;
176 struct timespec acquired
;
177 struct in6_addr addr
;
180 char saddr
[INET6_ADDRSTRLEN
];
186 struct ipv6_addr
*delegating_prefix
;
187 struct ipv6_addrhead pd_pfxs
;
188 TAILQ_ENTRY(ipv6_addr
) pd_next
;
190 uint8_t prefix_exclude_len
;
191 struct in6_addr prefix_exclude
;
194 void (*dadcallback
)(void *);
197 struct nd_neighbor_advert
*na
;
202 char alias
[IF_NAMESIZE
];
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)
226 TAILQ_ENTRY(ll_callback
) next
;
227 void (*callback
)(void *);
230 TAILQ_HEAD(ll_callback_head
, ll_callback
);
233 struct ipv6_addrhead addrs
;
234 struct ll_callback_head ll_callbacks
;
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 */
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))
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
*,
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_ifanyglobal(struct interface
*);
277 int ipv6_findaddrmatch(const struct ipv6_addr
*, const struct in6_addr
*,
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,
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)
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
*);
299 #define ipv6_gentempifid(a) {}
300 #define ipv6_settempstale(a) {}
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
*);