| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_DHCP6 |
| 20 |
|
| 21 |
#include <netinet/icmp6.h> |
| 22 |
|
| 23 |
struct iface_param { |
| 24 |
struct dhcp_context *current; |
| 25 |
struct in6_addr fallback, ll_addr, ula_addr; |
| 26 |
int ind, addr_match; |
| 27 |
}; |
| 28 |
|
| 29 |
|
| 30 |
static int complete_context6(struct in6_addr *local, int prefix, |
| 31 |
int scope, int if_index, int flags, |
| 32 |
unsigned int preferred, unsigned int valid, void *vparam); |
| 33 |
static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm); |
| 34 |
|
| 35 |
void dhcp6_init(void) |
| 36 |
{ |
| 37 |
int fd; |
| 38 |
struct sockaddr_in6 saddr; |
| 39 |
#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
| 40 |
int class = IPTOS_CLASS_CS6; |
| 41 |
#endif |
| 42 |
int oneopt = 1; |
| 43 |
|
| 44 |
if ((fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1 || |
| 45 |
#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6) |
| 46 |
setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 || |
| 47 |
#endif |
| 48 |
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &oneopt, sizeof(oneopt)) == -1 || |
| 49 |
!fix_fd(fd) || |
| 50 |
!set_ipv6pktinfo(fd)) |
| 51 |
die (_("cannot create DHCPv6 socket: %s"), NULL, EC_BADNET); |
| 52 |
|
| 53 |
/* When bind-interfaces is set, there might be more than one dnsmasq |
| 54 |
instance binding port 547. That's OK if they serve different networks. |
| 55 |
Need to set REUSEADDR|REUSEPORT to make this possible. |
| 56 |
Handle the case that REUSEPORT is defined, but the kernel doesn't |
| 57 |
support it. This handles the introduction of REUSEPORT on Linux. */ |
| 58 |
if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND)) |
| 59 |
{ |
| 60 |
int rc = 0; |
| 61 |
|
| 62 |
#ifdef SO_REUSEPORT |
| 63 |
if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt))) == -1 && |
| 64 |
errno == ENOPROTOOPT) |
| 65 |
rc = 0; |
| 66 |
#endif |
| 67 |
|
| 68 |
if (rc != -1) |
| 69 |
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt)); |
| 70 |
|
| 71 |
if (rc == -1) |
| 72 |
die(_("failed to set SO_REUSE{ADDR|PORT} on DHCPv6 socket: %s"), NULL, EC_BADNET); |
| 73 |
} |
| 74 |
|
| 75 |
memset(&saddr, 0, sizeof(saddr)); |
| 76 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 77 |
saddr.sin6_len = sizeof(struct sockaddr_in6); |
| 78 |
#endif |
| 79 |
saddr.sin6_family = AF_INET6; |
| 80 |
saddr.sin6_addr = in6addr_any; |
| 81 |
saddr.sin6_port = htons(DHCPV6_SERVER_PORT); |
| 82 |
|
| 83 |
if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in6))) |
| 84 |
die(_("failed to bind DHCPv6 server socket: %s"), NULL, EC_BADNET); |
| 85 |
|
| 86 |
daemon->dhcp6fd = fd; |
| 87 |
} |
| 88 |
|
| 89 |
void dhcp6_packet(time_t now) |
| 90 |
{ |
| 91 |
struct dhcp_context *context; |
| 92 |
struct dhcp_relay *relay; |
| 93 |
struct iface_param parm; |
| 94 |
struct cmsghdr *cmptr; |
| 95 |
struct msghdr msg; |
| 96 |
uint32_t if_index = 0; |
| 97 |
union { |
| 98 |
struct cmsghdr align; /* this ensures alignment */ |
| 99 |
char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 100 |
} control_u; |
| 101 |
struct sockaddr_in6 from; |
| 102 |
ssize_t sz; |
| 103 |
struct ifreq ifr; |
| 104 |
struct iname *tmp; |
| 105 |
unsigned short port; |
| 106 |
struct in6_addr dst_addr; |
| 107 |
struct in6_addr all_servers; |
| 108 |
|
| 109 |
memset(&dst_addr, 0, sizeof(dst_addr)); |
| 110 |
|
| 111 |
msg.msg_control = control_u.control6; |
| 112 |
msg.msg_controllen = sizeof(control_u); |
| 113 |
msg.msg_flags = 0; |
| 114 |
msg.msg_name = &from; |
| 115 |
msg.msg_namelen = sizeof(from); |
| 116 |
msg.msg_iov = &daemon->dhcp_packet; |
| 117 |
msg.msg_iovlen = 1; |
| 118 |
|
| 119 |
if ((sz = recv_dhcp_packet(daemon->dhcp6fd, &msg)) == -1) |
| 120 |
return; |
| 121 |
|
| 122 |
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 123 |
if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 124 |
{ |
| 125 |
union { |
| 126 |
unsigned char *c; |
| 127 |
struct in6_pktinfo *p; |
| 128 |
} p; |
| 129 |
p.c = CMSG_DATA(cmptr); |
| 130 |
|
| 131 |
if_index = p.p->ipi6_ifindex; |
| 132 |
dst_addr = p.p->ipi6_addr; |
| 133 |
} |
| 134 |
|
| 135 |
if (!indextoname(daemon->dhcp6fd, if_index, ifr.ifr_name)) |
| 136 |
return; |
| 137 |
|
| 138 |
#ifdef HAVE_LINUX_NETWORK |
| 139 |
/* This works around a possible Linux kernel bug when using interfaces |
| 140 |
enslaved to a VRF. The scope_id in the source address gets set |
| 141 |
to the index of the VRF interface, not the slave. Fortunately, |
| 142 |
the interface index returned by packetinfo is correct so we use |
| 143 |
that instead. Log this once, so if it triggers in other circumstances |
| 144 |
we've not anticipated and breaks things, we get some clues. */ |
| 145 |
if (from.sin6_scope_id != if_index) |
| 146 |
{ |
| 147 |
static int logged = 0; |
| 148 |
|
| 149 |
if (!logged) |
| 150 |
{ |
| 151 |
my_syslog(MS_DHCP | LOG_WARNING, |
| 152 |
_("Working around kernel bug: faulty source address scope for VRF slave %s"), |
| 153 |
ifr.ifr_name); |
| 154 |
logged = 1; |
| 155 |
} |
| 156 |
|
| 157 |
from.sin6_scope_id = if_index; |
| 158 |
} |
| 159 |
#endif |
| 160 |
|
| 161 |
#ifdef HAVE_DUMPFILE |
| 162 |
dump_packet_udp(DUMP_DHCPV6, (void *)daemon->dhcp_packet.iov_base, sz, |
| 163 |
(union mysockaddr *)&from, NULL, daemon->dhcp6fd); |
| 164 |
#endif |
| 165 |
|
| 166 |
if (relay_reply6(&from, sz, ifr.ifr_name)) |
| 167 |
{ |
| 168 |
#ifdef HAVE_DUMPFILE |
| 169 |
dump_packet_udp(DUMP_DHCPV6, (void *)daemon->outpacket.iov_base, save_counter(-1), NULL, |
| 170 |
(union mysockaddr *)&from, daemon->dhcp6fd); |
| 171 |
#endif |
| 172 |
|
| 173 |
while (retry_send(sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, |
| 174 |
save_counter(-1), 0, (struct sockaddr *)&from, |
| 175 |
sizeof(from)))); |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
struct dhcp_bridge *bridge, *alias; |
| 180 |
int multicast_dest = 0; |
| 181 |
|
| 182 |
for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
| 183 |
if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 184 |
return; |
| 185 |
|
| 186 |
for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 187 |
if (tmp->name && (tmp->flags & INAME_6) && |
| 188 |
wildcard_match(tmp->name, ifr.ifr_name)) |
| 189 |
return; |
| 190 |
|
| 191 |
parm.current = NULL; |
| 192 |
parm.ind = if_index; |
| 193 |
parm.addr_match = 0; |
| 194 |
memset(&parm.fallback, 0, IN6ADDRSZ); |
| 195 |
memset(&parm.ll_addr, 0, IN6ADDRSZ); |
| 196 |
memset(&parm.ula_addr, 0, IN6ADDRSZ); |
| 197 |
|
| 198 |
/* If the interface on which the DHCPv6 request was received is |
| 199 |
an alias of some other interface (as specified by the |
| 200 |
--bridge-interface option), change parm.ind so that we look |
| 201 |
for DHCPv6 contexts associated with the aliased interface |
| 202 |
instead of with the aliasing one. */ |
| 203 |
for (bridge = daemon->bridges; bridge; bridge = bridge->next) |
| 204 |
{ |
| 205 |
for (alias = bridge->alias; alias; alias = alias->next) |
| 206 |
if (wildcard_matchn(alias->iface, ifr.ifr_name, IF_NAMESIZE)) |
| 207 |
{ |
| 208 |
parm.ind = if_nametoindex(bridge->iface); |
| 209 |
if (!parm.ind) |
| 210 |
{ |
| 211 |
my_syslog(MS_DHCP | LOG_WARNING, |
| 212 |
_("unknown interface %s in bridge-interface"), |
| 213 |
bridge->iface); |
| 214 |
return; |
| 215 |
} |
| 216 |
break; |
| 217 |
} |
| 218 |
if (alias) |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
for (context = daemon->dhcp6; context; context = context->next) |
| 223 |
if (IN6_IS_ADDR_UNSPECIFIED(&context->start6) && context->prefix == 0) |
| 224 |
{ |
| 225 |
/* wildcard context for DHCP-stateless only */ |
| 226 |
parm.current = context; |
| 227 |
context->current = NULL; |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
/* unlinked contexts are marked by context->current == context */ |
| 232 |
context->current = context; |
| 233 |
memset(&context->local6, 0, IN6ADDRSZ); |
| 234 |
} |
| 235 |
|
| 236 |
for (relay = daemon->relay6; relay; relay = relay->next) |
| 237 |
relay->matchcount = 0; |
| 238 |
|
| 239 |
inet_pton(AF_INET6, ALL_RELAY_AGENTS_AND_SERVERS, &all_servers); |
| 240 |
if (IN6_ARE_ADDR_EQUAL(&dst_addr, &all_servers)) |
| 241 |
multicast_dest = 1; |
| 242 |
|
| 243 |
inet_pton(AF_INET6, ALL_SERVERS, &all_servers); |
| 244 |
if (IN6_ARE_ADDR_EQUAL(&dst_addr, &all_servers)) |
| 245 |
multicast_dest = 1; |
| 246 |
else |
| 247 |
{ |
| 248 |
/* Ignore requests sent to the ALL_SERVERS multicast address for relay when |
| 249 |
we're listening there for DHCPv6 server reasons. */ |
| 250 |
if (relay_upstream6(if_index, (size_t)sz, &from.sin6_addr, from.sin6_scope_id, now)) |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
if (!iface_enumerate(AF_INET6, &parm, (callback_t){.af_inet6=complete_context6})) |
| 255 |
return; |
| 256 |
|
| 257 |
/* Check for a relay again after iface_enumerate/complete_context has had |
| 258 |
chance to fill in relay->iface_index fields. This handles first time through |
| 259 |
and any changes in interface config. */ |
| 260 |
if (!IN6_ARE_ADDR_EQUAL(&dst_addr, &all_servers) && |
| 261 |
relay_upstream6(if_index, (size_t)sz, &from.sin6_addr, from.sin6_scope_id, now)) |
| 262 |
return; |
| 263 |
|
| 264 |
if (daemon->if_names || daemon->if_addrs) |
| 265 |
{ |
| 266 |
|
| 267 |
for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
| 268 |
if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 269 |
break; |
| 270 |
|
| 271 |
if (!tmp && !parm.addr_match) |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
/* May have configured relay, but not DHCP server */ |
| 276 |
if (!daemon->doing_dhcp6) |
| 277 |
return; |
| 278 |
|
| 279 |
lease_prune(NULL, now); /* lose any expired leases */ |
| 280 |
|
| 281 |
port = dhcp6_reply(parm.current, multicast_dest, if_index, ifr.ifr_name, &parm.fallback, |
| 282 |
&parm.ll_addr, &parm.ula_addr, sz, &from.sin6_addr, now); |
| 283 |
|
| 284 |
/* The port in the source address of the original request should |
| 285 |
be correct, but at least once client sends from the server port, |
| 286 |
so we explicitly send to the client port to a client, and the |
| 287 |
server port to a relay. */ |
| 288 |
if (port != 0) |
| 289 |
{ |
| 290 |
from.sin6_port = htons(port); |
| 291 |
|
| 292 |
#ifdef HAVE_DUMPFILE |
| 293 |
dump_packet_udp(DUMP_DHCPV6, (void *)daemon->outpacket.iov_base, save_counter(-1), |
| 294 |
NULL, (union mysockaddr *)&from, daemon->dhcp6fd); |
| 295 |
#endif |
| 296 |
|
| 297 |
while (retry_send(sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, |
| 298 |
save_counter(-1), 0, (struct sockaddr *)&from, sizeof(from)))); |
| 299 |
} |
| 300 |
|
| 301 |
/* These need to be called _after_ we send DHCPv6 packet, since lease_update_file() |
| 302 |
may trigger sending an RA packet, which overwrites our buffer. */ |
| 303 |
lease_update_file(now); |
| 304 |
lease_update_dns(0); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void get_client_mac(struct in6_addr *client, int iface, unsigned char *mac, unsigned int *maclenp, unsigned int *mactypep, time_t now) |
| 309 |
{ |
| 310 |
/* Receiving a packet from a host does not populate the neighbour |
| 311 |
cache, so we send a neighbour discovery request if we can't |
| 312 |
find the sender. Repeat a few times in case of packet loss. */ |
| 313 |
|
| 314 |
struct neigh_packet neigh; |
| 315 |
union mysockaddr addr; |
| 316 |
int i, maclen; |
| 317 |
|
| 318 |
neigh.type = ND_NEIGHBOR_SOLICIT; |
| 319 |
neigh.code = 0; |
| 320 |
neigh.reserved = 0; |
| 321 |
neigh.target = *client; |
| 322 |
/* RFC4443 section-2.3: checksum has to be zero to be calculated */ |
| 323 |
neigh.checksum = 0; |
| 324 |
|
| 325 |
memset(&addr, 0, sizeof(addr)); |
| 326 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 327 |
addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 328 |
#endif |
| 329 |
addr.in6.sin6_family = AF_INET6; |
| 330 |
addr.in6.sin6_port = htons(IPPROTO_ICMPV6); |
| 331 |
addr.in6.sin6_addr = *client; |
| 332 |
addr.in6.sin6_scope_id = iface; |
| 333 |
|
| 334 |
for (i = 0; i < 5; i++) |
| 335 |
{ |
| 336 |
struct timespec ts; |
| 337 |
|
| 338 |
if ((maclen = find_mac(&addr, mac, 0, now)) != 0) |
| 339 |
break; |
| 340 |
|
| 341 |
while(retry_send(sendto(daemon->icmp6fd, &neigh, sizeof(neigh), 0, &addr.sa, sizeof(addr)))); |
| 342 |
|
| 343 |
ts.tv_sec = 0; |
| 344 |
ts.tv_nsec = 100000000; /* 100ms */ |
| 345 |
nanosleep(&ts, NULL); |
| 346 |
} |
| 347 |
|
| 348 |
*maclenp = maclen; |
| 349 |
*mactypep = ARPHRD_ETHER; |
| 350 |
} |
| 351 |
|
| 352 |
static int complete_context6(struct in6_addr *local, int prefix, |
| 353 |
int scope, int if_index, int flags, unsigned int preferred, |
| 354 |
unsigned int valid, void *vparam) |
| 355 |
{ |
| 356 |
struct dhcp_context *context; |
| 357 |
struct shared_network *share; |
| 358 |
struct dhcp_relay *relay; |
| 359 |
struct iface_param *param = vparam; |
| 360 |
struct iname *tmp; |
| 361 |
int match = !daemon->if_addrs; |
| 362 |
|
| 363 |
(void)scope; /* warning */ |
| 364 |
|
| 365 |
if (if_index != param->ind) |
| 366 |
return 1; |
| 367 |
|
| 368 |
if (IN6_IS_ADDR_LINKLOCAL(local)) |
| 369 |
param->ll_addr = *local; |
| 370 |
else if (IN6_IS_ADDR_ULA(local)) |
| 371 |
param->ula_addr = *local; |
| 372 |
|
| 373 |
if (IN6_IS_ADDR_LOOPBACK(local) || |
| 374 |
IN6_IS_ADDR_LINKLOCAL(local) || |
| 375 |
IN6_IS_ADDR_MULTICAST(local)) |
| 376 |
return 1; |
| 377 |
|
| 378 |
/* if we have --listen-address config, see if the |
| 379 |
arrival interface has a matching address. */ |
| 380 |
for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 381 |
if (tmp->addr.sa.sa_family == AF_INET6 && |
| 382 |
IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, local)) |
| 383 |
match = param->addr_match = 1; |
| 384 |
|
| 385 |
/* Determine a globally address on the arrival interface, even |
| 386 |
if we have no matching dhcp-context, because we're only |
| 387 |
allocating on remote subnets via relays. This |
| 388 |
is used as a default for the DNS server option. */ |
| 389 |
param->fallback = *local; |
| 390 |
|
| 391 |
for (context = daemon->dhcp6; context; context = context->next) |
| 392 |
if ((context->flags & CONTEXT_DHCP) && |
| 393 |
!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) && |
| 394 |
prefix <= context->prefix && |
| 395 |
context->current == context) |
| 396 |
{ |
| 397 |
if (is_same_net6(local, &context->start6, context->prefix) && |
| 398 |
is_same_net6(local, &context->end6, context->prefix)) |
| 399 |
{ |
| 400 |
struct dhcp_context *tmp, **up; |
| 401 |
|
| 402 |
/* use interface values only for constructed contexts */ |
| 403 |
if (!(context->flags & CONTEXT_CONSTRUCTED)) |
| 404 |
preferred = valid = 0xffffffff; |
| 405 |
else if (flags & IFACE_DEPRECATED) |
| 406 |
preferred = 0; |
| 407 |
|
| 408 |
if (context->flags & CONTEXT_DEPRECATE) |
| 409 |
preferred = 0; |
| 410 |
|
| 411 |
/* order chain, longest preferred time first */ |
| 412 |
for (up = ¶m->current, tmp = param->current; tmp; tmp = tmp->current) |
| 413 |
if (tmp->preferred <= preferred) |
| 414 |
break; |
| 415 |
else |
| 416 |
up = &tmp->current; |
| 417 |
|
| 418 |
context->current = *up; |
| 419 |
*up = context; |
| 420 |
context->local6 = *local; |
| 421 |
context->preferred = preferred; |
| 422 |
context->valid = valid; |
| 423 |
} |
| 424 |
else |
| 425 |
{ |
| 426 |
for (share = daemon->shared_networks; share; share = share->next) |
| 427 |
{ |
| 428 |
/* IPv4 shared_address - ignore */ |
| 429 |
if (share->shared_addr.s_addr != 0) |
| 430 |
continue; |
| 431 |
|
| 432 |
if (share->if_index != 0) |
| 433 |
{ |
| 434 |
if (share->if_index != if_index) |
| 435 |
continue; |
| 436 |
} |
| 437 |
else |
| 438 |
{ |
| 439 |
if (!IN6_ARE_ADDR_EQUAL(&share->match_addr6, local)) |
| 440 |
continue; |
| 441 |
} |
| 442 |
|
| 443 |
if (is_same_net6(&share->shared_addr6, &context->start6, context->prefix) && |
| 444 |
is_same_net6(&share->shared_addr6, &context->end6, context->prefix)) |
| 445 |
{ |
| 446 |
context->current = param->current; |
| 447 |
param->current = context; |
| 448 |
context->local6 = *local; |
| 449 |
context->preferred = context->flags & CONTEXT_DEPRECATE ? 0 :0xffffffff; |
| 450 |
context->valid = 0xffffffff; |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
if (match) |
| 457 |
for (relay = daemon->relay6; relay; relay = relay->next) |
| 458 |
if (IN6_ARE_ADDR_EQUAL(local, &relay->local.addr6)) |
| 459 |
{ |
| 460 |
relay->iface_index = if_index; |
| 461 |
|
| 462 |
/* More than one interface with the relay address breaks things. */ |
| 463 |
if (relay->matchcount++ == 1 && !relay->warned) |
| 464 |
{ |
| 465 |
relay->warned = 1; |
| 466 |
inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN); |
| 467 |
my_syslog(MS_DHCP | LOG_WARNING, _("DHCP relay address %s appears on more than one interface"), daemon->addrbuff); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
return 1; |
| 472 |
} |
| 473 |
|
| 474 |
struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct in6_addr *net, int prefix, struct in6_addr *addr) |
| 475 |
{ |
| 476 |
struct dhcp_config *config; |
| 477 |
|
| 478 |
for (config = configs; config; config = config->next) |
| 479 |
if (config->flags & CONFIG_ADDR6) |
| 480 |
{ |
| 481 |
struct addrlist *addr_list; |
| 482 |
|
| 483 |
for (addr_list = config->addr6; addr_list; addr_list = addr_list->next) |
| 484 |
if ((!net || is_same_net6(&addr_list->addr.addr6, net, prefix) || ((addr_list->flags & ADDRLIST_WILDCARD) && prefix == 64)) && |
| 485 |
is_same_net6(&addr_list->addr.addr6, addr, (addr_list->flags & ADDRLIST_PREFIX) ? addr_list->prefixlen : 128)) |
| 486 |
return config; |
| 487 |
} |
| 488 |
|
| 489 |
return NULL; |
| 490 |
} |
| 491 |
|
| 492 |
struct dhcp_context *address6_allocate(struct dhcp_context *context, unsigned char *clid, int clid_len, int temp_addr, |
| 493 |
unsigned int iaid, int serial, struct dhcp_netid *netids, int plain_range, struct in6_addr *ans) |
| 494 |
{ |
| 495 |
/* Find a free address: exclude anything in use and anything allocated to |
| 496 |
a particular hwaddr/clientid/hostname in our configuration. |
| 497 |
Try to return from contexts which match netids first. |
| 498 |
|
| 499 |
Note that we assume the address prefix lengths are 64 or greater, so we can |
| 500 |
get by with 64 bit arithmetic. |
| 501 |
*/ |
| 502 |
|
| 503 |
u64 start, addr; |
| 504 |
struct dhcp_context *c, *d; |
| 505 |
int i, pass; |
| 506 |
u64 j; |
| 507 |
|
| 508 |
/* hash hwaddr: use the SDBM hashing algorithm. This works |
| 509 |
for MAC addresses, let's see how it manages with client-ids! |
| 510 |
For temporary addresses, we generate a new random one each time. */ |
| 511 |
if (temp_addr) |
| 512 |
j = rand64(); |
| 513 |
else |
| 514 |
for (j = iaid, i = 0; i < clid_len; i++) |
| 515 |
j = clid[i] + (j << 6) + (j << 16) - j; |
| 516 |
|
| 517 |
for (pass = 0; pass <= plain_range ? 1 : 0; pass++) |
| 518 |
for (c = context; c; c = c->current) |
| 519 |
if (c->flags & (CONTEXT_DEPRECATE | CONTEXT_STATIC | CONTEXT_RA_STATELESS | CONTEXT_USED)) |
| 520 |
continue; |
| 521 |
else if (!match_netid(c->filter, netids, pass)) |
| 522 |
continue; |
| 523 |
else |
| 524 |
{ |
| 525 |
if (!temp_addr && option_bool(OPT_CONSEC_ADDR)) |
| 526 |
{ |
| 527 |
/* seed is largest extant lease addr in this context, |
| 528 |
skip addresses equal to the number of addresses rejected |
| 529 |
by clients. This should avoid the same client being offered the same |
| 530 |
address after it has rjected it. */ |
| 531 |
start = lease_find_max_addr6(c) + 1 + serial + c->addr_epoch; |
| 532 |
if (c->addr_epoch) |
| 533 |
c->addr_epoch--; |
| 534 |
} |
| 535 |
else |
| 536 |
{ |
| 537 |
u64 range = 1 + addr6part(&c->end6) - addr6part(&c->start6); |
| 538 |
u64 offset = j + c->addr_epoch; |
| 539 |
|
| 540 |
/* don't divide by zero if range is whole 2^64 */ |
| 541 |
if (range != 0) |
| 542 |
offset = offset % range; |
| 543 |
|
| 544 |
start = addr6part(&c->start6) + offset; |
| 545 |
} |
| 546 |
|
| 547 |
/* iterate until we find a free address. */ |
| 548 |
addr = start; |
| 549 |
|
| 550 |
do { |
| 551 |
/* eliminate addresses in use by the server. */ |
| 552 |
for (d = context; d; d = d->current) |
| 553 |
if (addr == addr6part(&d->local6)) |
| 554 |
break; |
| 555 |
|
| 556 |
*ans = c->start6; |
| 557 |
setaddr6part (ans, addr); |
| 558 |
|
| 559 |
if (!d && |
| 560 |
!lease6_find_by_addr(&c->start6, c->prefix, addr) && |
| 561 |
!config_find_by_address6(daemon->dhcp_conf, &c->start6, c->prefix, ans)) |
| 562 |
return c; |
| 563 |
|
| 564 |
addr++; |
| 565 |
|
| 566 |
if (addr == addr6part(&c->end6) + 1) |
| 567 |
addr = addr6part(&c->start6); |
| 568 |
|
| 569 |
} while (addr != start); |
| 570 |
} |
| 571 |
|
| 572 |
return NULL; |
| 573 |
} |
| 574 |
|
| 575 |
/* can dynamically allocate addr */ |
| 576 |
struct dhcp_context *address6_available(struct dhcp_context *context, |
| 577 |
struct in6_addr *taddr, |
| 578 |
struct dhcp_netid *netids, |
| 579 |
int plain_range) |
| 580 |
{ |
| 581 |
u64 start, end, addr = addr6part(taddr); |
| 582 |
struct dhcp_context *tmp; |
| 583 |
|
| 584 |
for (tmp = context; tmp; tmp = tmp->current) |
| 585 |
{ |
| 586 |
start = addr6part(&tmp->start6); |
| 587 |
end = addr6part(&tmp->end6); |
| 588 |
|
| 589 |
if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_RA_STATELESS)) && |
| 590 |
is_same_net6(&tmp->start6, taddr, tmp->prefix) && |
| 591 |
is_same_net6(&tmp->end6, taddr, tmp->prefix) && |
| 592 |
addr >= start && |
| 593 |
addr <= end && |
| 594 |
match_netid(tmp->filter, netids, plain_range)) |
| 595 |
return tmp; |
| 596 |
} |
| 597 |
|
| 598 |
return NULL; |
| 599 |
} |
| 600 |
|
| 601 |
/* address OK if configured */ |
| 602 |
struct dhcp_context *address6_valid(struct dhcp_context *context, |
| 603 |
struct in6_addr *taddr, |
| 604 |
struct dhcp_netid *netids, |
| 605 |
int plain_range) |
| 606 |
{ |
| 607 |
struct dhcp_context *tmp; |
| 608 |
|
| 609 |
for (tmp = context; tmp; tmp = tmp->current) |
| 610 |
if (is_same_net6(&tmp->start6, taddr, tmp->prefix) && |
| 611 |
match_netid(tmp->filter, netids, plain_range)) |
| 612 |
return tmp; |
| 613 |
|
| 614 |
return NULL; |
| 615 |
} |
| 616 |
|
| 617 |
void make_duid(time_t now) |
| 618 |
{ |
| 619 |
(void)now; |
| 620 |
|
| 621 |
if (daemon->duid_config) |
| 622 |
{ |
| 623 |
unsigned char *p; |
| 624 |
|
| 625 |
daemon->duid = p = safe_malloc(daemon->duid_config_len + 6); |
| 626 |
daemon->duid_len = daemon->duid_config_len + 6; |
| 627 |
PUTSHORT(2, p); /* DUID_EN */ |
| 628 |
PUTLONG(daemon->duid_enterprise, p); |
| 629 |
memcpy(p, daemon->duid_config, daemon->duid_config_len); |
| 630 |
} |
| 631 |
else |
| 632 |
{ |
| 633 |
time_t newnow = 0; |
| 634 |
|
| 635 |
/* If we have no persistent lease database, or a non-stable RTC, use DUID_LL (newnow == 0) */ |
| 636 |
#ifndef HAVE_BROKEN_RTC |
| 637 |
/* rebase epoch to 1/1/2000 */ |
| 638 |
if (!option_bool(OPT_LEASE_RO) || daemon->lease_change_command) |
| 639 |
newnow = now - 946684800; |
| 640 |
#endif |
| 641 |
|
| 642 |
iface_enumerate(AF_LOCAL, &newnow, (callback_t){.af_local=make_duid1}); |
| 643 |
|
| 644 |
if(!daemon->duid) |
| 645 |
die("Cannot create DHCPv6 server DUID: %s", NULL, EC_MISC); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm) |
| 650 |
{ |
| 651 |
/* create DUID as specified in RFC3315. We use the MAC of the |
| 652 |
first interface we find that isn't loopback or P-to-P and |
| 653 |
has address-type < 256. Address types above 256 are things like |
| 654 |
tunnels which don't have usable MAC addresses. */ |
| 655 |
|
| 656 |
unsigned char *p; |
| 657 |
(void)index; |
| 658 |
(void)parm; |
| 659 |
time_t newnow = *((time_t *)parm); |
| 660 |
|
| 661 |
if (type >= 256) |
| 662 |
return 1; |
| 663 |
|
| 664 |
if (newnow == 0) |
| 665 |
{ |
| 666 |
daemon->duid = p = safe_malloc(maclen + 4); |
| 667 |
daemon->duid_len = maclen + 4; |
| 668 |
PUTSHORT(3, p); /* DUID_LL */ |
| 669 |
PUTSHORT(type, p); /* address type */ |
| 670 |
} |
| 671 |
else |
| 672 |
{ |
| 673 |
daemon->duid = p = safe_malloc(maclen + 8); |
| 674 |
daemon->duid_len = maclen + 8; |
| 675 |
PUTSHORT(1, p); /* DUID_LLT */ |
| 676 |
PUTSHORT(type, p); /* address type */ |
| 677 |
PUTLONG(*((time_t *)parm), p); /* time */ |
| 678 |
} |
| 679 |
|
| 680 |
memcpy(p, mac, maclen); |
| 681 |
|
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
struct cparam { |
| 686 |
time_t now; |
| 687 |
int newone, newname; |
| 688 |
}; |
| 689 |
|
| 690 |
static int construct_worker(struct in6_addr *local, int prefix, |
| 691 |
int scope, int if_index, int flags, |
| 692 |
unsigned int preferred, unsigned int valid, void *vparam) |
| 693 |
{ |
| 694 |
char ifrn_name[IFNAMSIZ]; |
| 695 |
struct in6_addr start6, end6; |
| 696 |
struct dhcp_context *template, *context; |
| 697 |
struct iname *tmp; |
| 698 |
|
| 699 |
(void)scope; |
| 700 |
(void)flags; |
| 701 |
(void)valid; |
| 702 |
(void)preferred; |
| 703 |
|
| 704 |
struct cparam *param = vparam; |
| 705 |
|
| 706 |
if (IN6_IS_ADDR_LOOPBACK(local) || |
| 707 |
IN6_IS_ADDR_LINKLOCAL(local) || |
| 708 |
IN6_IS_ADDR_MULTICAST(local)) |
| 709 |
return 1; |
| 710 |
|
| 711 |
if (!(flags & IFACE_PERMANENT)) |
| 712 |
return 1; |
| 713 |
|
| 714 |
if (flags & IFACE_DEPRECATED) |
| 715 |
return 1; |
| 716 |
|
| 717 |
/* Ignore interfaces where we're not doing RA/DHCP6 */ |
| 718 |
if (!indextoname(daemon->icmp6fd, if_index, ifrn_name) || |
| 719 |
!iface_check(AF_LOCAL, NULL, ifrn_name, NULL)) |
| 720 |
return 1; |
| 721 |
|
| 722 |
for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
| 723 |
if (tmp->name && wildcard_match(tmp->name, ifrn_name)) |
| 724 |
return 1; |
| 725 |
|
| 726 |
for (template = daemon->dhcp6; template; template = template->next) |
| 727 |
if (!(template->flags & (CONTEXT_TEMPLATE | CONTEXT_CONSTRUCTED))) |
| 728 |
{ |
| 729 |
/* non-template entries, just fill in interface and local addresses */ |
| 730 |
if (prefix <= template->prefix && |
| 731 |
is_same_net6(local, &template->start6, template->prefix) && |
| 732 |
is_same_net6(local, &template->end6, template->prefix)) |
| 733 |
{ |
| 734 |
/* First time found, do fast RA. */ |
| 735 |
if (template->if_index == 0) |
| 736 |
{ |
| 737 |
ra_start_unsolicited(param->now, template); |
| 738 |
param->newone = 1; |
| 739 |
} |
| 740 |
|
| 741 |
template->if_index = if_index; |
| 742 |
template->local6 = *local; |
| 743 |
} |
| 744 |
|
| 745 |
} |
| 746 |
else if (wildcard_match(template->template_interface, ifrn_name) && |
| 747 |
template->prefix >= prefix) |
| 748 |
{ |
| 749 |
start6 = *local; |
| 750 |
setaddr6part(&start6, addr6part(&template->start6)); |
| 751 |
end6 = *local; |
| 752 |
setaddr6part(&end6, addr6part(&template->end6)); |
| 753 |
|
| 754 |
for (context = daemon->dhcp6; context; context = context->next) |
| 755 |
if (!(context->flags & CONTEXT_TEMPLATE) && |
| 756 |
IN6_ARE_ADDR_EQUAL(&start6, &context->start6) && |
| 757 |
IN6_ARE_ADDR_EQUAL(&end6, &context->end6)) |
| 758 |
{ |
| 759 |
/* If there's an absolute address context covering this address |
| 760 |
then don't construct one as well. */ |
| 761 |
if (!(context->flags & CONTEXT_CONSTRUCTED)) |
| 762 |
break; |
| 763 |
|
| 764 |
if (context->if_index == if_index) |
| 765 |
{ |
| 766 |
int cflags = context->flags; |
| 767 |
context->flags &= ~(CONTEXT_GC | CONTEXT_OLD); |
| 768 |
if (cflags & CONTEXT_OLD) |
| 769 |
{ |
| 770 |
/* address went, now it's back, and on the same interface */ |
| 771 |
log_context(AF_INET6, context); |
| 772 |
/* fast RAs for a while */ |
| 773 |
ra_start_unsolicited(param->now, context); |
| 774 |
param->newone = 1; |
| 775 |
/* Add address to name again */ |
| 776 |
if (context->flags & CONTEXT_RA_NAME) |
| 777 |
param->newname = 1; |
| 778 |
|
| 779 |
} |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
if (!context && (context = whine_malloc(sizeof (struct dhcp_context)))) |
| 785 |
{ |
| 786 |
*context = *template; |
| 787 |
context->start6 = start6; |
| 788 |
context->end6 = end6; |
| 789 |
context->flags &= ~CONTEXT_TEMPLATE; |
| 790 |
context->flags |= CONTEXT_CONSTRUCTED; |
| 791 |
context->if_index = if_index; |
| 792 |
context->local6 = *local; |
| 793 |
context->saved_valid = 0; |
| 794 |
|
| 795 |
context->next = daemon->dhcp6; |
| 796 |
daemon->dhcp6 = context; |
| 797 |
|
| 798 |
ra_start_unsolicited(param->now, context); |
| 799 |
/* we created a new one, need to call |
| 800 |
lease_update_file to get periodic functions called */ |
| 801 |
param->newone = 1; |
| 802 |
|
| 803 |
/* Will need to add new putative SLAAC addresses to existing leases */ |
| 804 |
if (context->flags & CONTEXT_RA_NAME) |
| 805 |
param->newname = 1; |
| 806 |
|
| 807 |
log_context(AF_INET6, context); |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
return 1; |
| 812 |
} |
| 813 |
|
| 814 |
void dhcp_construct_contexts(time_t now) |
| 815 |
{ |
| 816 |
struct dhcp_context *context, *tmp, **up; |
| 817 |
struct cparam param; |
| 818 |
param.newone = 0; |
| 819 |
param.newname = 0; |
| 820 |
param.now = now; |
| 821 |
|
| 822 |
for (context = daemon->dhcp6; context; context = context->next) |
| 823 |
if (context->flags & CONTEXT_CONSTRUCTED) |
| 824 |
context->flags |= CONTEXT_GC; |
| 825 |
|
| 826 |
iface_enumerate(AF_INET6, ¶m, (callback_t){.af_inet6=construct_worker}); |
| 827 |
|
| 828 |
for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp) |
| 829 |
{ |
| 830 |
|
| 831 |
tmp = context->next; |
| 832 |
|
| 833 |
if (context->flags & CONTEXT_GC && !(context->flags & CONTEXT_OLD)) |
| 834 |
{ |
| 835 |
if ((context->flags & CONTEXT_RA) || option_bool(OPT_RA)) |
| 836 |
{ |
| 837 |
/* previously constructed context has gone; advertise its demise */ |
| 838 |
context->flags |= CONTEXT_OLD; |
| 839 |
context->address_lost_time = now; |
| 840 |
/* Apply same ceiling of configured lease time as in radv.c */ |
| 841 |
if (context->saved_valid > context->lease_time) |
| 842 |
context->saved_valid = context->lease_time; |
| 843 |
/* maximum time is 2 hours, from RFC */ |
| 844 |
if (context->saved_valid > 7200) /* 2 hours */ |
| 845 |
context->saved_valid = 7200; |
| 846 |
ra_start_unsolicited(now, context); |
| 847 |
param.newone = 1; /* include deletion */ |
| 848 |
|
| 849 |
if (context->flags & CONTEXT_RA_NAME) |
| 850 |
param.newname = 1; |
| 851 |
|
| 852 |
log_context(AF_INET6, context); |
| 853 |
|
| 854 |
up = &context->next; |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
/* we were never doing RA for this, so free now */ |
| 859 |
*up = context->next; |
| 860 |
free(context); |
| 861 |
} |
| 862 |
} |
| 863 |
else |
| 864 |
up = &context->next; |
| 865 |
} |
| 866 |
|
| 867 |
if (param.newone) |
| 868 |
{ |
| 869 |
if (daemon->dhcp || daemon->doing_dhcp6) |
| 870 |
{ |
| 871 |
if (param.newname) |
| 872 |
lease_update_slaac(now); |
| 873 |
lease_update_file(now); |
| 874 |
} |
| 875 |
else |
| 876 |
/* Not doing DHCP, so no lease system, manage alarms for ra only */ |
| 877 |
send_alarm(periodic_ra(now), now); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
#endif /* HAVE_DHCP6 */ |