Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * dhcpcd - DHCP client daemon
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 #ifdef INET6
    162 TAILQ_HEAD(ipv6_addrhead, ipv6_addr);
    163 struct ipv6_addr {
    164 	TAILQ_ENTRY(ipv6_addr) next;
    165 	struct interface *iface;
    166 	struct in6_addr prefix;
    167 	uint8_t prefix_len;
    168 	uint32_t prefix_vltime;
    169 	uint32_t prefix_pltime;
    170 	struct timespec created;
    171 	struct timespec acquired;
    172 	struct in6_addr addr;
    173 	struct in6_addr dstaddr;
    174 	int addr_flags;
    175 	unsigned int flags;
    176 	char saddr[INET6_ADDRSTRLEN];
    177 	uint8_t iaid[4];
    178 	uint16_t ia_type;
    179 	int dhcp6_fd;
    180 
    181 #ifndef SMALL
    182 	struct ipv6_addr *delegating_prefix;
    183 	struct ipv6_addrhead pd_pfxs;
    184 	TAILQ_ENTRY(ipv6_addr) pd_next;
    185 
    186 	uint8_t prefix_exclude_len;
    187 	struct in6_addr prefix_exclude;
    188 #endif
    189 
    190 	void (*dadcallback)(void *);
    191 	int dadcounter;
    192 
    193 	struct nd_neighbor_advert *na;
    194 	size_t na_len;
    195 	int na_count;
    196 
    197 #ifdef ALIAS_ADDR
    198 	char alias[IF_NAMESIZE];
    199 #endif
    200 };
    201 
    202 #define	IPV6_AF_ONLINK		(1U << 0)
    203 #define	IPV6_AF_NEW		(1U << 1)
    204 #define	IPV6_AF_STALE		(1U << 2)
    205 #define	IPV6_AF_ADDED		(1U << 3)
    206 #define	IPV6_AF_AUTOCONF	(1U << 4)
    207 #define	IPV6_AF_DADCOMPLETED	(1U << 5)
    208 #define	IPV6_AF_PFXDELEGATION	(1U << 6)
    209 #define	IPV6_AF_DELEGATED	(1U << 7)
    210 #define	IPV6_AF_NOREJECT	(1U << 8)
    211 #define	IPV6_AF_REQUEST		(1U << 9)
    212 #define	IPV6_AF_STATIC		(1U << 10)
    213 #define	IPV6_AF_DELEGATEDLOG	(1U << 11)
    214 #define	IPV6_AF_RAPFX		(1U << 12)
    215 #define	IPV6_AF_EXTENDED	(1U << 13)
    216 #define	IPV6_AF_REGEN		(1U << 14)
    217 #define	IPV6_AF_ROUTER		(1U << 15)
    218 #define	IPV6_AF_ADVERTISED	(1U << 16)
    219 #ifdef IPV6_MANAGETEMPADDR
    220 #define	IPV6_AF_TEMPORARY	(1U << 17)
    221 #endif
    222 
    223 struct ll_callback {
    224 	TAILQ_ENTRY(ll_callback) next;
    225 	void (*callback)(void *);
    226 	void *arg;
    227 };
    228 TAILQ_HEAD(ll_callback_head, ll_callback);
    229 
    230 struct ipv6_state {
    231 	struct ipv6_addrhead addrs;
    232 	struct ll_callback_head ll_callbacks;
    233 
    234 #ifdef IPV6_MANAGETEMPADDR
    235 	uint32_t desync_factor;
    236 #endif
    237 };
    238 
    239 #define IPV6_STATE(ifp)							       \
    240 	((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6])
    241 #define IPV6_CSTATE(ifp)						       \
    242 	((const struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6])
    243 #define IPV6_STATE_RUNNING(ifp) ipv6_staticdadcompleted((ifp))
    244 
    245 
    246 int ipv6_init(struct dhcpcd_ctx *);
    247 int ipv6_makestableprivate(struct in6_addr *,
    248     const struct in6_addr *, int, const struct interface *, int *);
    249 int ipv6_makeaddr(struct in6_addr *, struct interface *,
    250     const struct in6_addr *, int, unsigned int);
    251 int ipv6_mask(struct in6_addr *, int);
    252 uint8_t ipv6_prefixlen(const struct in6_addr *);
    253 int ipv6_userprefix( const struct in6_addr *, short prefix_len,
    254     uint64_t user_number, struct in6_addr *result, short result_len);
    255 void ipv6_checkaddrflags(void *);
    256 void ipv6_markaddrsstale(struct interface *, unsigned int);
    257 void ipv6_deletestaleaddrs(struct interface *);
    258 int ipv6_addaddr(struct ipv6_addr *, struct timespec *);
    259 int ipv6_doaddr(struct ipv6_addr *, struct timespec *);
    260 ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs);
    261 void ipv6_deleteaddr(struct ipv6_addr *);
    262 void ipv6_freedrop_addrs(struct ipv6_addrhead *, int, unsigned int,
    263     const struct interface *);
    264 void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *,
    265     const char *, const struct in6_addr *, uint8_t,
    266     const struct in6_addr *, int, pid_t);
    267 int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, const struct ipv6_addr *,
    268     pid_t);
    269 struct ipv6_addr *ipv6_iffindaddr(struct interface *,
    270     const struct in6_addr *, int);
    271 int ipv6_hasaddr(const struct interface *);
    272 struct ipv6_addr *ipv6_anyglobal(struct interface *);
    273 int ipv6_findaddrmatch(const struct ipv6_addr *, const struct in6_addr *,
    274     unsigned int);
    275 struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *,
    276     const struct in6_addr *, unsigned int);
    277 struct ipv6_addr *ipv6_findmaskaddr(struct dhcpcd_ctx *,
    278     const struct in6_addr *);
    279 struct ipv6_addr *ipv6_finddstaddr(struct dhcpcd_ctx *,
    280     const struct in6_addr *);
    281 #define ipv6_linklocal(ifp) ipv6_iffindaddr((ifp), NULL, IN6_IFF_NOTUSEABLE)
    282 int ipv6_addlinklocalcallback(struct interface *, void (*)(void *), void *);
    283 void ipv6_setscope(struct sockaddr_in6 *, unsigned int);
    284 unsigned int ipv6_getscope(const struct sockaddr_in6 *);
    285 struct ipv6_addr *ipv6_newaddr(struct interface *, const struct in6_addr *,
    286     uint8_t, 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 struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *,
    294     const struct timespec *);
    295 struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int);
    296 void ipv6_addtempaddrs(struct interface *, struct timespec *);
    297 void ipv6_regentempaddrs(void *);
    298 #endif
    299 
    300 int ipv6_start(struct interface *);
    301 int ipv6_staticdadcompleted(const struct interface *);
    302 int ipv6_startstatic(struct interface *);
    303 ssize_t ipv6_env(FILE *, const char *, const struct interface *);
    304 void ipv6_ctxfree(struct dhcpcd_ctx *);
    305 bool inet6_getroutes(struct dhcpcd_ctx *, rb_tree_t *);
    306 #endif /* INET6 */
    307 
    308 #endif /* INET6_H */
    309