xref: /dragonfly/contrib/dhcpcd/src/ipv6.h (revision 56380a7f)
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2018 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef IPV6_H
29 #define IPV6_H
30 
31 #include <sys/uio.h>
32 #include <netinet/in.h>
33 
34 #include "config.h"
35 #include "if.h"
36 
37 #ifndef __linux__
38 #  if !defined(__QNX__) && !defined(__sun)
39 #    include <sys/endian.h>
40 #  endif
41 #  include <net/if.h>
42 #  ifndef __sun
43 #    include <netinet6/in6_var.h>
44 #  endif
45 #endif
46 
47 #define ALLROUTERS "ff02::2"
48 
49 #define EUI64_GBIT		0x01
50 #define EUI64_UBIT		0x02
51 #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
52 #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
53 
54 #ifndef ND6_INFINITE_LIFETIME
55 #  define ND6_INFINITE_LIFETIME		((uint32_t)~0)
56 #endif
57 
58 /* RFC4941 constants */
59 #define TEMP_VALID_LIFETIME	604800	/* 1 week */
60 #define TEMP_PREFERRED_LIFETIME	86400	/* 1 day */
61 #define REGEN_ADVANCE		5	/* seconds */
62 #define MAX_DESYNC_FACTOR	600	/* 10 minutes */
63 
64 #define TEMP_IDGEN_RETRIES	3
65 #define GEN_TEMPID_RETRY_MAX	5
66 
67 /* RFC7217 constants */
68 #define IDGEN_RETRIES	3
69 #define IDGEN_DELAY	1 /* second */
70 
71 #ifndef IN6_ARE_MASKED_ADDR_EQUAL
72 #define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m)	(	\
73 	(((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \
74 	(((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \
75 	(((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \
76 	(((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 )
77 #endif
78 
79 /*
80  * BSD kernels don't inform userland of DAD results.
81  * See the discussion here:
82  *    http://mail-index.netbsd.org/tech-net/2013/03/15/msg004019.html
83  */
84 #ifndef __linux__
85 /* We guard here to avoid breaking a compile on linux ppc-64 headers */
86 #  include <sys/param.h>
87 #endif
88 #ifdef BSD
89 #  define IPV6_POLLADDRFLAG
90 #endif
91 
92 /* This was fixed in NetBSD */
93 #if defined(__NetBSD_Version__) && __NetBSD_Version__ >= 699002000
94 #  undef IPV6_POLLADDRFLAG
95 #endif
96 
97 #ifdef __sun
98    /* Solaris lacks these defines.
99     * While it supports DaD, to seems to only expose IFF_DUPLICATE
100     * so we have no way of knowing if it's tentative or not.
101     * I don't even know if Solaris has any special treatment for tentative. */
102 #  define IN6_IFF_TENTATIVE	0
103 #  define IN6_IFF_DUPLICATED	0x04
104 #  define IN6_IFF_DETACHED	0
105 #endif
106 
107 #define IN6_IFF_NOTUSEABLE \
108 	(IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED | IN6_IFF_DETACHED)
109 
110 /*
111  * If dhcpcd handles RA processing instead of the kernel, the kernel needs
112  * to either allow userland to set temporary addresses or mark an address
113  * for the kernel to manage temporary addresses from.
114  * If the kernel allows the former, a global #define is needed, otherwise
115  * the address marking will be handled in the platform specific address handler.
116  *
117  * Some BSDs do not allow userland to set temporary addresses.
118  * Linux-3.18 allows the marking of addresses from which to manage temp addrs.
119  */
120 #if defined(IN6_IFF_TEMPORARY) && !defined(__linux__)
121 #define	IPV6_MANAGETEMPADDR
122 #endif
123 
124 #ifdef __linux__
125    /* Match Linux defines to BSD */
126 #  ifdef IFA_F_TEMPORARY
127 #    define IN6_IFF_TEMPORARY	IFA_F_TEMPORARY
128 #  endif
129 #  ifdef IFA_F_OPTIMISTIC
130 #    define IN6_IFF_TENTATIVE	(IFA_F_TENTATIVE | IFA_F_OPTIMISTIC)
131 #  else
132 #    define IN6_IFF_TENTATIVE   (IFA_F_TENTATIVE | 0x04)
133 #  endif
134 #  ifdef IF_F_DADFAILED
135 #    define IN6_IFF_DUPLICATED	IFA_F_DADFAILED
136 #  else
137 #    define IN6_IFF_DUPLICATED	0x08
138 #  endif
139 #  define IN6_IFF_DETACHED	0
140 #endif
141 
142 TAILQ_HEAD(ipv6_addrhead, ipv6_addr);
143 struct ipv6_addr {
144 	TAILQ_ENTRY(ipv6_addr) next;
145 	struct interface *iface;
146 	struct in6_addr prefix;
147 	uint8_t prefix_len;
148 	uint32_t prefix_vltime;
149 	uint32_t prefix_pltime;
150 	struct timespec created;
151 	struct timespec acquired;
152 	struct in6_addr addr;
153 	int addr_flags;
154 	unsigned int flags;
155 	char saddr[INET6_ADDRSTRLEN];
156 	uint8_t iaid[4];
157 	uint16_t ia_type;
158 	int dhcp6_fd;
159 
160 #ifndef SMALL
161 	struct ipv6_addr *delegating_prefix;
162 	struct ipv6_addrhead pd_pfxs;
163 	TAILQ_ENTRY(ipv6_addr) pd_next;
164 
165 	uint8_t prefix_exclude_len;
166 	struct in6_addr prefix_exclude;
167 #endif
168 
169 	void (*dadcallback)(void *);
170 	int dadcounter;
171 
172 #ifdef ALIAS_ADDR
173 	char alias[IF_NAMESIZE];
174 #endif
175 };
176 
177 #define	IPV6_AF_ONLINK		(1U << 0)
178 #define	IPV6_AF_NEW		(1U << 1)
179 #define	IPV6_AF_STALE		(1U << 2)
180 #define	IPV6_AF_ADDED		(1U << 3)
181 #define	IPV6_AF_AUTOCONF	(1U << 4)
182 #define	IPV6_AF_DUPLICATED	(1U << 5)
183 #define	IPV6_AF_DADCOMPLETED	(1U << 6)
184 #define	IPV6_AF_DELEGATED	(1U << 7)
185 #define	IPV6_AF_DELEGATEDPFX	(1U << 8)
186 #define	IPV6_AF_NOREJECT	(1U << 9)
187 #define	IPV6_AF_REQUEST		(1U << 10)
188 #define	IPV6_AF_STATIC		(1U << 11)
189 #define	IPV6_AF_DELEGATEDLOG	(1U << 12)
190 #define	IPV6_AF_RAPFX		(1U << 13)
191 #define	IPV6_AF_EXTENDED	(1U << 14)
192 #ifdef IPV6_MANAGETEMPADDR
193 #define	IPV6_AF_TEMPORARY	(1U << 15)
194 #endif
195 
196 struct ll_callback {
197 	TAILQ_ENTRY(ll_callback) next;
198 	void (*callback)(void *);
199 	void *arg;
200 };
201 TAILQ_HEAD(ll_callback_head, ll_callback);
202 
203 struct ipv6_state {
204 	struct ipv6_addrhead addrs;
205 	struct ll_callback_head ll_callbacks;
206 
207 #ifdef IPV6_MANAGETEMPADDR
208 	time_t desync_factor;
209 	uint8_t randomseed0[8]; /* upper 64 bits of MD5 digest */
210 	uint8_t randomseed1[8]; /* lower 64 bits */
211 	uint8_t randomid[8];
212 #endif
213 };
214 
215 #define IPV6_STATE(ifp)							       \
216 	((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6])
217 #define IPV6_CSTATE(ifp)						       \
218 	((const struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6])
219 #define IPV6_STATE_RUNNING(ifp) ipv6_staticdadcompleted((ifp))
220 
221 #ifdef INET6
222 
223 int ipv6_init(struct dhcpcd_ctx *);
224 int ipv6_makestableprivate(struct in6_addr *addr,
225     const struct in6_addr *prefix, int prefix_len,
226     const struct interface *ifp, int *dad_counter);
227 int ipv6_makeaddr(struct in6_addr *, struct interface *,
228     const struct in6_addr *, int);
229 int ipv6_mask(struct in6_addr *, int);
230 uint8_t ipv6_prefixlen(const struct in6_addr *);
231 int ipv6_userprefix( const struct in6_addr *, short prefix_len,
232     uint64_t user_number, struct in6_addr *result, short result_len);
233 void ipv6_checkaddrflags(void *);
234 void ipv6_markaddrsstale(struct interface *, unsigned int);
235 void ipv6_deletestaleaddrs(struct interface *);
236 int ipv6_addaddr(struct ipv6_addr *, const struct timespec *);
237 ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs);
238 void ipv6_deleteaddr(struct ipv6_addr *);
239 void ipv6_freedrop_addrs(struct ipv6_addrhead *, int,
240     const struct interface *);
241 void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *,
242     const char *, const struct in6_addr *, uint8_t, int, pid_t);
243 int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, const struct ipv6_addr *,
244     pid_t);
245 struct ipv6_addr *ipv6_iffindaddr(struct interface *,
246     const struct in6_addr *, int);
247 int ipv6_hasaddr(const struct interface *);
248 int ipv6_findaddrmatch(const struct ipv6_addr *, const struct in6_addr *,
249     unsigned int);
250 struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *,
251     const struct in6_addr *, unsigned int);
252 struct ipv6_addr *ipv6_findmaskaddr(struct dhcpcd_ctx *,
253     const struct in6_addr *);
254 #define ipv6_linklocal(ifp) ipv6_iffindaddr((ifp), NULL, IN6_IFF_NOTUSEABLE)
255 int ipv6_addlinklocalcallback(struct interface *, void (*)(void *), void *);
256 struct ipv6_addr *ipv6_newaddr(struct interface *, const struct in6_addr *, uint8_t,
257     unsigned int);
258 void ipv6_freeaddr(struct ipv6_addr *);
259 void ipv6_freedrop(struct interface *, int);
260 #define ipv6_free(ifp) ipv6_freedrop((ifp), 0)
261 #define ipv6_drop(ifp) ipv6_freedrop((ifp), 2)
262 
263 #ifdef IPV6_MANAGETEMPADDR
264 void ipv6_gentempifid(struct interface *);
265 struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *,
266     const struct timespec *);
267 struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int);
268 void ipv6_addtempaddrs(struct interface *, const struct timespec *);
269 #else
270 #define ipv6_gentempifid(a) {}
271 #define ipv6_settempstale(a) {}
272 #endif
273 
274 int ipv6_start(struct interface *);
275 int ipv6_staticdadcompleted(const struct interface *);
276 int ipv6_startstatic(struct interface *);
277 ssize_t ipv6_env(char **, const char *, const struct interface *);
278 void ipv6_ctxfree(struct dhcpcd_ctx *);
279 bool inet6_getroutes(struct dhcpcd_ctx *, struct rt_head *);
280 
281 #else
282 #define ipv6_start(a) (-1)
283 #define ipv6_startstatic(a)
284 #define ipv6_staticdadcompleted(a) (0)
285 #define ipv6_hasaddr(a) (0)
286 #define ipv6_free_ll_callbacks(a) {}
287 #define ipv6_free(a) {}
288 #define ipv6_ctxfree(a) {}
289 #define ipv6_gentempifid(a) {}
290 #endif
291 
292 #endif
293