Update to dhcpcd-9.0.1 with the following changes:
[dragonfly.git] / contrib / dhcpcd / src / ipv6.h
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 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_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)
231 #endif
232
233 struct ll_callback {
234         TAILQ_ENTRY(ll_callback) next;
235         void (*callback)(void *);
236         void *arg;
237 };
238 TAILQ_HEAD(ll_callback_head, ll_callback);
239
240 struct ipv6_state {
241         struct ipv6_addrhead addrs;
242         struct ll_callback_head ll_callbacks;
243
244 #ifdef IPV6_MANAGETEMPADDR
245         uint32_t desync_factor;
246 #endif
247 };
248
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))
254
255
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 *,
277     pid_t);
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 *,
283     unsigned int);
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,
291     unsigned int);
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)
296
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 *);
303 #endif
304
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 *);
311 #endif /* INET6 */
312
313 #endif /* INET6_H */