1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2015 Roy Marples <roy (at) 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 "dhcpcd.h" 36 37 #define ALLROUTERS "ff02::2" 38 39 #define ROUNDUP8(a) (1 + (((a) - 1) | 7)) 40 #define ROUNDUP16(a) (1 + (((a) - 1) | 16)) 41 42 #define EUI64_GBIT 0x01 43 #define EUI64_UBIT 0x02 44 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) 45 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) 46 47 #ifndef ND6_INFINITE_LIFETIME 48 # define ND6_INFINITE_LIFETIME ((uint32_t)~0) 49 #endif 50 51 /* RFC4941 constants */ 52 #define TEMP_VALID_LIFETIME 604800 /* 1 week */ 53 #define TEMP_PREFERRED_LIFETIME 86400 /* 1 day */ 54 #define REGEN_ADVANCE 5 /* seconds */ 55 #define MAX_DESYNC_FACTOR 600 /* 10 minutes */ 56 57 #define TEMP_IDGEN_RETRIES 3 58 #define GEN_TEMPID_RETRY_MAX 5 59 60 /* RFC7217 constants */ 61 #define IDGEN_RETRIES 3 62 #define IDGEN_DELAY 1 /* second */ 63 64 /* 65 * BSD kernels don't inform userland of DAD results. 66 * See the discussion here: 67 * http://mail-index.netbsd.org/tech-net/2013/03/15/msg004019.html 68 */ 69 #ifndef __linux__ 70 /* We guard here to avoid breaking a compile on linux ppc-64 headers */ 71 # include <sys/param.h> 72 #endif 73 #ifdef BSD 74 # define IPV6_POLLADDRFLAG 75 #endif 76 77 /* This was fixed in NetBSD */ 78 #if defined(__NetBSD_Version__) && __NetBSD_Version__ >= 699002000 79 # undef IPV6_POLLADDRFLAG 80 #endif 81 82 /* Linux-3.18 can manage temporary addresses even with RA 83 * processing disabled. */ 84 //#undef IFA_F_MANAGETEMPADDR 85 #if defined(__linux__) && defined(IFA_F_MANAGETEMPADDR) 86 #define IPV6_MANAGETEMPADDR 87 #endif 88 89 /* Some BSDs do not allow userland to set temporary addresses. */ 90 #if defined(BSD) && defined(IN6_IFF_TEMPORARY) 91 #define IPV6_MANAGETEMPADDR 92 #endif 93 94 struct ipv6_addr { 95 TAILQ_ENTRY(ipv6_addr) next; 96 struct interface *iface; 97 struct in6_addr prefix; 98 uint8_t prefix_len; 99 uint32_t prefix_vltime; 100 uint32_t prefix_pltime; 101 struct timespec created; 102 struct timespec acquired; 103 struct in6_addr addr; 104 int addr_flags; 105 short flags; 106 char saddr[INET6_ADDRSTRLEN]; 107 uint8_t iaid[4]; 108 uint16_t ia_type; 109 struct interface *delegating_iface; 110 uint8_t prefix_exclude_len; 111 struct in6_addr prefix_exclude; 112 113 void (*dadcallback)(void *); 114 int dadcounter; 115 uint8_t *ns; 116 size_t nslen; 117 int nsprobes; 118 }; 119 TAILQ_HEAD(ipv6_addrhead, ipv6_addr); 120 121 #define IPV6_AF_ONLINK 0x0001 122 #define IPV6_AF_NEW 0x0002 123 #define IPV6_AF_STALE 0x0004 124 #define IPV6_AF_ADDED 0x0008 125 #define IPV6_AF_AUTOCONF 0x0010 126 #define IPV6_AF_DUPLICATED 0x0020 127 #define IPV6_AF_DADCOMPLETED 0x0040 128 #define IPV6_AF_DELEGATED 0x0080 129 #define IPV6_AF_DELEGATEDPFX 0x0100 130 #define IPV6_AF_DELEGATEDZERO 0x0200 131 #define IPV6_AF_REQUEST 0x0400 132 #ifdef IPV6_MANAGETEMPADDR 133 #define IPV6_AF_TEMPORARY 0X0800 134 #endif 135 136 struct rt6 { 137 TAILQ_ENTRY(rt6) next; 138 struct in6_addr dest; 139 struct in6_addr net; 140 struct in6_addr gate; 141 const struct interface *iface; 142 unsigned int flags; 143 #ifdef HAVE_ROUTE_METRIC 144 unsigned int metric; 145 #endif 146 unsigned int mtu; 147 }; 148 TAILQ_HEAD(rt6_head, rt6); 149 150 struct ll_callback { 151 TAILQ_ENTRY(ll_callback) next; 152 void (*callback)(void *); 153 void *arg; 154 }; 155 TAILQ_HEAD(ll_callback_head, ll_callback); 156 157 struct ipv6_state { 158 struct ipv6_addrhead addrs; 159 struct ll_callback_head ll_callbacks; 160 161 #ifdef IPV6_MANAGETEMPADDR 162 time_t desync_factor; 163 uint8_t randomseed0[8]; /* upper 64 bits of MD5 digest */ 164 uint8_t randomseed1[8]; /* lower 64 bits */ 165 uint8_t randomid[8]; 166 #endif 167 }; 168 169 #define IPV6_STATE(ifp) \ 170 ((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) 171 #define IPV6_CSTATE(ifp) \ 172 ((const struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) 173 174 /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */ 175 #ifdef __QNX__ 176 #undef CMSG_SPACE 177 #endif 178 179 #ifndef ALIGNBYTES 180 #define ALIGNBYTES (sizeof(int) - 1) 181 #endif 182 #ifndef ALIGN 183 #define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) 184 #endif 185 #ifndef CMSG_SPACE 186 #define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) 187 #endif 188 189 #define IP6BUFLEN (CMSG_SPACE(sizeof(struct in6_pktinfo)) + \ 190 CMSG_SPACE(sizeof(int))) 191 192 193 #ifdef INET6 194 struct ipv6_ctx { 195 struct sockaddr_in6 from; 196 struct msghdr sndhdr; 197 struct iovec sndiov[2]; 198 unsigned char sndbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; 199 struct msghdr rcvhdr; 200 struct iovec rcviov[2]; 201 unsigned char rcvbuf[IP6BUFLEN]; 202 unsigned char ansbuf[1500]; 203 char ntopbuf[INET6_ADDRSTRLEN]; 204 const char *sfrom; 205 206 int nd_fd; 207 struct ra_head *ra_routers; 208 struct rt6_head *routes; 209 210 struct rt6_head kroutes; 211 212 int dhcp_fd; 213 }; 214 215 struct ipv6_ctx *ipv6_init(struct dhcpcd_ctx *); 216 ssize_t ipv6_printaddr(char *, size_t, const uint8_t *, const char *); 217 int ipv6_makestableprivate(struct in6_addr *addr, 218 const struct in6_addr *prefix, int prefix_len, 219 const struct interface *ifp, int *dad_counter); 220 int ipv6_makeaddr(struct in6_addr *, const struct interface *, 221 const struct in6_addr *, int); 222 int ipv6_makeprefix(struct in6_addr *, const struct in6_addr *, int); 223 int ipv6_mask(struct in6_addr *, int); 224 uint8_t ipv6_prefixlen(const struct in6_addr *); 225 int ipv6_userprefix( const struct in6_addr *, short prefix_len, 226 uint64_t user_number, struct in6_addr *result, short result_len); 227 void ipv6_checkaddrflags(void *); 228 int ipv6_addaddr(struct ipv6_addr *, const struct timespec *); 229 ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs); 230 void ipv6_freedrop_addrs(struct ipv6_addrhead *, int, 231 const struct interface *); 232 void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *, 233 const char *, const struct in6_addr *, uint8_t, int); 234 int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, 235 const struct in6_addr *, int); 236 int ipv6_publicaddr(const struct ipv6_addr *); 237 const struct ipv6_addr *ipv6_iffindaddr(const struct interface *, 238 const struct in6_addr *); 239 struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *, 240 const struct in6_addr *, short); 241 #define ipv6_linklocal(ifp) ipv6_iffindaddr((ifp), NULL) 242 int ipv6_addlinklocalcallback(struct interface *, void (*)(void *), void *); 243 void ipv6_freeaddr(struct ipv6_addr *); 244 void ipv6_freedrop(struct interface *, int); 245 #define ipv6_free(ifp) ipv6_freedrop((ifp), 0) 246 #define ipv6_drop(ifp) ipv6_freedrop((ifp), 2) 247 248 #ifdef IPV6_MANAGETEMPADDR 249 void ipv6_gentempifid(struct interface *); 250 void ipv6_settempstale(struct interface *); 251 struct ipv6_addr *ipv6_createtempaddr(struct ipv6_addr *, 252 const struct timespec *); 253 struct ipv6_addr *ipv6_settemptime(struct ipv6_addr *, int); 254 void ipv6_addtempaddrs(struct interface *, const struct timespec *); 255 #else 256 #define ipv6_gentempifid(a) {} 257 #define ipv6_settempstale(a) {} 258 #endif 259 260 int ipv6_start(struct interface *); 261 void ipv6_ctxfree(struct dhcpcd_ctx *); 262 int ipv6_handlert(struct dhcpcd_ctx *, int cmd, struct rt6 *); 263 void ipv6_freerts(struct rt6_head *); 264 void ipv6_buildroutes(struct dhcpcd_ctx *); 265 266 #else 267 #define ipv6_init(a) (NULL) 268 #define ipv6_start(a) (-1) 269 #define ipv6_free_ll_callbacks(a) {} 270 #define ipv6_free(a) {} 271 #define ipv6_drop(a) {} 272 #define ipv6_ctxfree(a) {} 273 #define ipv6_gentempifid(a) {} 274 #endif 275 276 #endif 277