2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-12-10 15:17:34 +01:00
|
|
|
/***
|
2018-06-12 17:15:23 +02:00
|
|
|
Copyright © 2014 Intel Corporation. All rights reserved.
|
2014-12-10 15:17:34 +01:00
|
|
|
***/
|
|
|
|
|
2019-05-09 02:33:45 +02:00
|
|
|
#include <netinet/in.h>
|
2014-12-10 15:17:34 +01:00
|
|
|
#include <linux/if.h>
|
2019-07-24 11:22:43 +02:00
|
|
|
#include <linux/if_arp.h>
|
2014-12-10 15:17:34 +01:00
|
|
|
|
|
|
|
#include "sd-dhcp6-client.h"
|
|
|
|
|
2020-06-01 06:18:22 +02:00
|
|
|
#include "escape.h"
|
2018-01-04 14:11:59 +01:00
|
|
|
#include "hashmap.h"
|
2017-11-16 10:07:07 +01:00
|
|
|
#include "hostname-util.h"
|
2019-02-03 18:38:10 +01:00
|
|
|
#include "missing_network.h"
|
2020-10-02 07:01:59 +02:00
|
|
|
#include "networkd-address.h"
|
2019-06-29 20:57:47 +02:00
|
|
|
#include "networkd-dhcp6.h"
|
2016-11-13 04:59:06 +01:00
|
|
|
#include "networkd-link.h"
|
|
|
|
#include "networkd-manager.h"
|
2020-07-07 04:19:49 +02:00
|
|
|
#include "networkd-radv.h"
|
2018-01-04 14:11:59 +01:00
|
|
|
#include "siphash24.h"
|
2020-07-02 01:03:17 +02:00
|
|
|
#include "string-table.h"
|
2018-01-04 14:11:59 +01:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "radv-internal.h"
|
2020-06-01 06:18:22 +02:00
|
|
|
#include "web-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2020-10-04 00:37:22 +02:00
|
|
|
bool link_dhcp6_pd_is_enabled(Link *link) {
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->network)
|
|
|
|
return false;
|
|
|
|
|
2020-10-16 08:50:44 +02:00
|
|
|
return link->network->dhcp6_pd;
|
2020-10-04 00:37:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static bool dhcp6_lease_has_pd_prefix(sd_dhcp6_lease *lease) {
|
|
|
|
uint32_t lifetime_preferred, lifetime_valid;
|
|
|
|
union in_addr_union pd_prefix;
|
|
|
|
uint8_t pd_prefix_len;
|
|
|
|
|
|
|
|
if (!lease)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sd_dhcp6_lease_reset_pd_prefix_iter(lease);
|
|
|
|
|
|
|
|
return sd_dhcp6_lease_get_pd(lease, &pd_prefix.in6, &pd_prefix_len, &lifetime_preferred, &lifetime_valid) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DHCP6DelegatedPrefix *dhcp6_pd_free(DHCP6DelegatedPrefix *p) {
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (p->link && p->link->manager) {
|
|
|
|
hashmap_remove(p->link->manager->dhcp6_prefixes, &p->prefix);
|
|
|
|
set_remove(p->link->manager->dhcp6_pd_prefixes, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
link_unref(p->link);
|
|
|
|
return mfree(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dhcp6_pd_hash_func(const DHCP6DelegatedPrefix *p, struct siphash *state) {
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
siphash24_compress(&p->pd_prefix, sizeof(p->pd_prefix), state);
|
|
|
|
siphash24_compress(&p->link, sizeof(p->link), state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_compare_func(const DHCP6DelegatedPrefix *a, const DHCP6DelegatedPrefix *b) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = memcmp(&a->pd_prefix, &b->pd_prefix, sizeof(a->pd_prefix));
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return CMP(a->link, b->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_HASH_OPS(dhcp6_pd_hash_ops, DHCP6DelegatedPrefix, dhcp6_pd_hash_func, dhcp6_pd_compare_func);
|
|
|
|
|
|
|
|
static Link *dhcp6_pd_get_link_by_prefix(Link *link, const union in_addr_union *prefix) {
|
|
|
|
DHCP6DelegatedPrefix *pd;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
assert(prefix);
|
|
|
|
|
|
|
|
pd = hashmap_get(link->manager->dhcp6_prefixes, &prefix->in6);
|
|
|
|
if (!pd)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pd->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_get_assigned_prefix(Link *link, const union in_addr_union *pd_prefix, union in_addr_union *ret_prefix) {
|
|
|
|
DHCP6DelegatedPrefix *pd, in;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
assert(pd_prefix);
|
|
|
|
assert(ret_prefix);
|
|
|
|
|
|
|
|
in = (DHCP6DelegatedPrefix) {
|
|
|
|
.pd_prefix = pd_prefix->in6,
|
|
|
|
.link = link,
|
|
|
|
};
|
|
|
|
|
|
|
|
pd = set_get(link->manager->dhcp6_pd_prefixes, &in);
|
|
|
|
if (!pd)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
ret_prefix->in6 = pd->prefix;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_remove_old(Link *link, bool force);
|
|
|
|
|
|
|
|
static int dhcp6_pd_address_callback(Address *address) {
|
|
|
|
Address *a;
|
|
|
|
|
|
|
|
assert(address);
|
|
|
|
assert(address->link);
|
|
|
|
|
|
|
|
/* Make this called only once */
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(a, address->link->dhcp6_pd_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
a->callback = NULL;
|
|
|
|
|
|
|
|
return dhcp6_pd_remove_old(address->link, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_remove_old(Link *link, bool force) {
|
|
|
|
Address *address;
|
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
|
|
|
|
if (!force && (link->dhcp6_pd_address_messages != 0 || link->dhcp6_pd_route_configured != 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (set_isempty(link->dhcp6_pd_addresses_old) && set_isempty(link->dhcp6_pd_routes_old))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
bool set_callback = !set_isempty(link->dhcp6_pd_addresses);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_pd_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
if (address_is_ready(address)) {
|
|
|
|
set_callback = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set_callback) {
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_pd_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
address->callback = dhcp6_pd_address_callback;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_link_debug(link, "Removing old DHCPv6 Prefix Delegation addresses and routes.");
|
|
|
|
|
|
|
|
link_dirty(link);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp6_pd_routes_old) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, NULL);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
|
2020-09-11 11:39:16 +02:00
|
|
|
if (link->radv)
|
|
|
|
(void) sd_radv_remove_prefix(link->radv, &route->dst.in6, 64);
|
2020-07-22 20:13:42 +02:00
|
|
|
dhcp6_pd_free(hashmap_get(link->manager->dhcp6_prefixes, &route->dst.in6));
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_pd_addresses_old) {
|
2020-07-22 20:13:42 +02:00
|
|
|
k = address_remove(address, link, NULL);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dhcp6_pd_remove(Link *link) {
|
|
|
|
Address *address;
|
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
|
2020-10-04 00:37:22 +02:00
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
|
|
|
return 0;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
link->dhcp6_pd_address_configured = false;
|
|
|
|
link->dhcp6_pd_route_configured = false;
|
|
|
|
|
|
|
|
k = dhcp6_pd_remove_old(link, true);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
|
|
|
|
if (set_isempty(link->dhcp6_pd_addresses) && set_isempty(link->dhcp6_pd_routes))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
log_link_debug(link, "Removing DHCPv6 Prefix Delegation addresses and routes.");
|
|
|
|
|
|
|
|
link_dirty(link);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp6_pd_routes) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, NULL);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
|
2020-09-11 11:39:16 +02:00
|
|
|
if (link->radv)
|
|
|
|
(void) sd_radv_remove_prefix(link->radv, &route->dst.in6, 64);
|
2020-07-22 20:13:42 +02:00
|
|
|
dhcp6_pd_free(hashmap_get(link->manager->dhcp6_prefixes, &route->dst.in6));
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_pd_addresses) {
|
2020-07-22 20:13:42 +02:00
|
|
|
k = address_remove(address, link, NULL);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp6_pd_route_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp6_pd_route_messages--;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0 && r != -EEXIST) {
|
|
|
|
log_link_message_warning_errno(link, m, r, "Failed to add DHCPv6 Prefix Delegation route");
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp6_pd_route_messages == 0) {
|
|
|
|
log_link_debug(link, "DHCPv6 prefix delegation routes set");
|
|
|
|
if (link->dhcp6_pd_prefixes_assigned)
|
|
|
|
link->dhcp6_pd_route_configured = true;
|
|
|
|
|
|
|
|
r = dhcp6_pd_remove_old(link, false);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
link_check_ready(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_set_pd_route(Link *link, const union in_addr_union *prefix, const union in_addr_union *pd_prefix) {
|
|
|
|
_cleanup_(dhcp6_pd_freep) DHCP6DelegatedPrefix *pd = NULL;
|
|
|
|
_cleanup_(route_freep) Route *route = NULL;
|
|
|
|
Link *assigned_link;
|
|
|
|
Route *ret;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
assert(prefix);
|
|
|
|
assert(pd_prefix);
|
|
|
|
|
|
|
|
r = route_new(&route);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
route->family = AF_INET6;
|
|
|
|
route->dst = *prefix;
|
|
|
|
route->dst_prefixlen = 64;
|
2020-12-03 11:00:56 +01:00
|
|
|
route->protocol = RTPROT_DHCP;
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
r = route_configure(route, link, dhcp6_pd_route_handler, &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to set DHCPv6 prefix route: %m");
|
|
|
|
|
|
|
|
link->dhcp6_pd_route_messages++;
|
|
|
|
|
|
|
|
r = set_ensure_put(&link->dhcp6_pd_routes, &route_hash_ops, ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route: %m");
|
|
|
|
|
|
|
|
(void) set_remove(link->dhcp6_pd_routes_old, ret);
|
|
|
|
|
|
|
|
assigned_link = dhcp6_pd_get_link_by_prefix(link, prefix);
|
|
|
|
if (assigned_link) {
|
|
|
|
assert(assigned_link == link);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pd = new(DHCP6DelegatedPrefix, 1);
|
|
|
|
if (!pd)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
*pd = (DHCP6DelegatedPrefix) {
|
|
|
|
.prefix = prefix->in6,
|
|
|
|
.pd_prefix = pd_prefix->in6,
|
|
|
|
.link = link_ref(link),
|
|
|
|
};
|
|
|
|
|
|
|
|
r = hashmap_ensure_allocated(&link->manager->dhcp6_prefixes, &in6_addr_hash_ops);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = hashmap_put(link->manager->dhcp6_prefixes, &pd->prefix, pd);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route at manager: %m");
|
|
|
|
|
|
|
|
r = set_ensure_put(&link->manager->dhcp6_pd_prefixes, &dhcp6_pd_hash_ops, pd);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route at manager: %m");
|
|
|
|
|
|
|
|
TAKE_PTR(pd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp6_pd_address_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp6_pd_address_messages--;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0 && r != -EEXIST) {
|
|
|
|
log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 delegated prefix address");
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
} else if (r >= 0)
|
|
|
|
(void) manager_rtnl_process_address(rtnl, m, link->manager);
|
|
|
|
|
|
|
|
if (link->dhcp6_pd_address_messages == 0) {
|
|
|
|
log_link_debug(link, "DHCPv6 delegated prefix addresses set");
|
|
|
|
if (link->dhcp6_pd_prefixes_assigned)
|
|
|
|
link->dhcp6_pd_address_configured = true;
|
|
|
|
|
|
|
|
r = dhcp6_pd_remove_old(link, false);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:27:28 +02:00
|
|
|
r = link_set_routes(link);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_set_pd_address(Link *link,
|
|
|
|
const union in_addr_union *prefix,
|
|
|
|
uint8_t prefix_len,
|
|
|
|
uint32_t lifetime_preferred,
|
|
|
|
uint32_t lifetime_valid) {
|
|
|
|
|
|
|
|
_cleanup_(address_freep) Address *address = NULL;
|
|
|
|
Address *ret;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
assert(prefix);
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
if (!link->network->dhcp6_pd_assign)
|
2020-07-22 20:13:42 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = address_new(&address);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to allocate address for DHCPv6 delegated prefix: %m");
|
|
|
|
|
|
|
|
address->in_addr = *prefix;
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
if (!in_addr_is_null(AF_INET6, &link->network->dhcp6_pd_token))
|
|
|
|
memcpy(address->in_addr.in6.s6_addr + 8, link->network->dhcp6_pd_token.in6.s6_addr + 8, 8);
|
2020-07-22 20:13:42 +02:00
|
|
|
else {
|
|
|
|
r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "Failed to generate EUI64 address for acquired DHCPv6 delegated prefix: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
address->prefixlen = prefix_len;
|
|
|
|
address->family = AF_INET6;
|
|
|
|
address->cinfo.ifa_prefered = lifetime_preferred;
|
|
|
|
address->cinfo.ifa_valid = lifetime_valid;
|
|
|
|
|
|
|
|
r = address_configure(address, link, dhcp6_pd_address_handler, true, &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to set DHCPv6 delegated prefix address: %m");
|
|
|
|
|
|
|
|
link->dhcp6_pd_address_messages++;
|
|
|
|
|
|
|
|
r = set_ensure_put(&link->dhcp6_pd_addresses, &address_hash_ops, ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv6 delegated prefix address: %m");
|
|
|
|
|
|
|
|
(void) set_remove(link->dhcp6_pd_addresses_old, ret);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_pd_assign_prefix(Link *link, const union in_addr_union *prefix, const union in_addr_union *pd_prefix,
|
|
|
|
uint8_t prefix_len, uint32_t lifetime_preferred, uint32_t lifetime_valid) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
2020-10-16 09:00:49 +02:00
|
|
|
assert(link->network);
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(prefix);
|
|
|
|
|
2020-10-16 09:00:49 +02:00
|
|
|
if (link->network->dhcp6_pd_announce) {
|
|
|
|
r = radv_add_prefix(link, &prefix->in6, prefix_len, lifetime_preferred, lifetime_valid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
r = dhcp6_set_pd_route(link, prefix, pd_prefix);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = dhcp6_set_pd_address(link, prefix, prefix_len, lifetime_preferred, lifetime_valid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool link_has_preferred_subnet_id(Link *link) {
|
2020-05-14 00:54:37 +02:00
|
|
|
if (!link->network)
|
|
|
|
return false;
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
return link->network->dhcp6_pd_subnet_id >= 0;
|
2020-05-14 00:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_get_preferred_delegated_prefix(
|
|
|
|
Link *link,
|
2020-07-22 20:13:42 +02:00
|
|
|
const union in_addr_union *masked_pd_prefix,
|
2020-05-14 00:54:37 +02:00
|
|
|
uint8_t pd_prefix_len,
|
2020-07-22 20:13:42 +02:00
|
|
|
union in_addr_union *ret) {
|
|
|
|
|
|
|
|
/* We start off with the original PD prefix we have been assigned and iterate from there */
|
|
|
|
union in_addr_union prefix;
|
|
|
|
uint64_t n_prefixes;
|
|
|
|
Link *assigned_link;
|
2020-06-10 04:57:35 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(link->manager);
|
|
|
|
assert(masked_pd_prefix);
|
|
|
|
assert(pd_prefix_len <= 64);
|
|
|
|
|
|
|
|
n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);
|
|
|
|
prefix = *masked_pd_prefix;
|
|
|
|
|
|
|
|
if (link_has_preferred_subnet_id(link)) {
|
2020-07-28 22:50:04 +02:00
|
|
|
uint64_t subnet_id = link->network->dhcp6_pd_subnet_id;
|
2020-05-14 00:54:37 +02:00
|
|
|
|
|
|
|
/* If the link has a preference for a particular subnet id try to allocate that */
|
2020-07-22 20:13:42 +02:00
|
|
|
if (subnet_id >= n_prefixes)
|
|
|
|
return log_link_warning_errno(link, SYNTHETIC_ERRNO(ERANGE),
|
|
|
|
"subnet id %" PRIu64 " is out of range. Only have %" PRIu64 " subnets.",
|
|
|
|
subnet_id, n_prefixes);
|
2020-05-14 00:54:37 +02:00
|
|
|
|
|
|
|
r = in_addr_prefix_nth(AF_INET6, &prefix, 64, subnet_id);
|
|
|
|
if (r < 0)
|
2020-07-22 20:13:42 +02:00
|
|
|
return log_link_warning_errno(link, r,
|
|
|
|
"subnet id %" PRIu64 " is out of range. Only have %" PRIu64 " subnets.",
|
|
|
|
subnet_id, n_prefixes);
|
2020-05-14 00:54:37 +02:00
|
|
|
|
|
|
|
/* Verify that the prefix we did calculate fits in the pd prefix.
|
|
|
|
* This should not fail as we checked the prefix size beforehand */
|
2020-07-22 20:13:42 +02:00
|
|
|
assert_se(in_addr_prefix_covers(AF_INET6, masked_pd_prefix, pd_prefix_len, &prefix) > 0);
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assigned_link = dhcp6_pd_get_link_by_prefix(link, &prefix);
|
|
|
|
if (assigned_link && assigned_link != link) {
|
|
|
|
_cleanup_free_ char *assigned_buf = NULL;
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
(void) in_addr_to_string(AF_INET6, &prefix, &assigned_buf);
|
|
|
|
return log_link_warning_errno(link, SYNTHETIC_ERRNO(EAGAIN),
|
|
|
|
"The requested prefix %s is already assigned to another link.",
|
|
|
|
strna(assigned_buf));
|
|
|
|
}
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
*ret = prefix;
|
2020-05-14 00:54:37 +02:00
|
|
|
return 0;
|
2020-07-07 02:14:21 +02:00
|
|
|
}
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-07 02:14:21 +02:00
|
|
|
for (uint64_t n = 0; n < n_prefixes; n++) {
|
2020-07-22 20:13:42 +02:00
|
|
|
/* If we do not have an allocation preference just iterate
|
2020-07-07 02:14:21 +02:00
|
|
|
* through the address space and return the first free prefix. */
|
2020-07-22 20:13:42 +02:00
|
|
|
assigned_link = dhcp6_pd_get_link_by_prefix(link, &prefix);
|
2020-07-07 02:14:21 +02:00
|
|
|
if (!assigned_link || assigned_link == link) {
|
2020-07-22 20:13:42 +02:00
|
|
|
*ret = prefix;
|
2020-07-07 02:14:21 +02:00
|
|
|
return 0;
|
2020-05-14 00:54:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:14:21 +02:00
|
|
|
r = in_addr_prefix_next(AF_INET6, &prefix, 64);
|
|
|
|
if (r < 0)
|
2020-07-22 20:13:42 +02:00
|
|
|
return log_link_warning_errno(link, r, "Can't allocate another prefix. Out of address space?: %m");
|
2020-05-14 00:54:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 02:14:21 +02:00
|
|
|
return log_link_warning_errno(link, SYNTHETIC_ERRNO(ERANGE), "Couldn't find a suitable prefix. Ran out of address space.");
|
2020-05-14 00:54:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static void dhcp6_pd_prefix_distribute(Link *dhcp6_link,
|
|
|
|
const union in_addr_union *masked_pd_prefix,
|
|
|
|
uint8_t pd_prefix_len,
|
|
|
|
uint32_t lifetime_preferred,
|
|
|
|
uint32_t lifetime_valid,
|
|
|
|
bool assign_preferred_subnet_id) {
|
|
|
|
|
|
|
|
Link *link;
|
|
|
|
int r;
|
2018-01-04 14:11:51 +01:00
|
|
|
|
|
|
|
assert(dhcp6_link);
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(dhcp6_link->manager);
|
|
|
|
assert(masked_pd_prefix);
|
|
|
|
assert(pd_prefix_len <= 64);
|
2018-01-04 14:11:51 +01:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(link, dhcp6_link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
_cleanup_free_ char *assigned_buf = NULL;
|
|
|
|
union in_addr_union assigned_prefix;
|
2018-01-04 14:11:51 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link == dhcp6_link)
|
2018-01-04 14:11:51 +01:00
|
|
|
continue;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
2018-01-04 14:11:51 +01:00
|
|
|
continue;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
continue;
|
2018-01-04 14:11:51 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (assign_preferred_subnet_id != link_has_preferred_subnet_id(link))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = dhcp6_pd_get_assigned_prefix(link, masked_pd_prefix, &assigned_prefix);
|
|
|
|
if (r < 0) {
|
|
|
|
r = dhcp6_get_preferred_delegated_prefix(link, masked_pd_prefix, pd_prefix_len, &assigned_prefix);
|
|
|
|
if (r < 0) {
|
|
|
|
link->dhcp6_pd_prefixes_assigned = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) in_addr_to_string(AF_INET6, &assigned_prefix, &assigned_buf);
|
|
|
|
r = dhcp6_pd_assign_prefix(link, &assigned_prefix, masked_pd_prefix, 64,
|
|
|
|
lifetime_preferred, lifetime_valid);
|
|
|
|
if (r < 0) {
|
|
|
|
log_link_error_errno(link, r, "Unable to assign/update prefix %s/64: %m",
|
|
|
|
strna(assigned_buf));
|
|
|
|
link_enter_failed(link);
|
|
|
|
} else
|
|
|
|
log_link_debug(link, "Assigned prefix %s/64", strna(assigned_buf));
|
|
|
|
}
|
2018-01-04 14:11:51 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_pd_prepare(Link *link) {
|
|
|
|
Address *address;
|
|
|
|
Route *route;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
link_dirty(link);
|
|
|
|
|
|
|
|
link->dhcp6_pd_address_configured = false;
|
|
|
|
link->dhcp6_pd_route_configured = false;
|
|
|
|
link->dhcp6_pd_prefixes_assigned = true;
|
|
|
|
|
|
|
|
while ((address = set_steal_first(link->dhcp6_pd_addresses))) {
|
|
|
|
r = set_ensure_put(&link->dhcp6_pd_addresses_old, &address_hash_ops, address);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store old DHCPv6 Prefix Delegation address: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((route = set_steal_first(link->dhcp6_pd_routes))) {
|
|
|
|
r = set_ensure_put(&link->dhcp6_pd_routes_old, &route_hash_ops, route);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store old DHCPv6 Prefix Delegation route: %m");
|
|
|
|
}
|
|
|
|
|
2015-01-20 18:36:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_pd_finalize(Link *link) {
|
2018-01-04 14:11:59 +01:00
|
|
|
int r;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 0;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
|
|
|
return 0;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link->dhcp6_pd_address_messages == 0) {
|
|
|
|
if (link->dhcp6_pd_prefixes_assigned)
|
|
|
|
link->dhcp6_pd_address_configured = true;
|
|
|
|
} else {
|
|
|
|
log_link_debug(link, "Setting DHCPv6 PD addresses");
|
2020-10-02 02:27:28 +02:00
|
|
|
/* address_handler calls link_set_routes() and link_set_nexthop(). Before they are
|
|
|
|
* called, the related flags must be cleared. Otherwise, the link becomes configured
|
|
|
|
* state before routes are configured. */
|
2020-07-22 20:13:42 +02:00
|
|
|
link->static_routes_configured = false;
|
|
|
|
link->static_nexthops_configured = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp6_pd_route_messages == 0) {
|
|
|
|
if (link->dhcp6_pd_prefixes_assigned)
|
|
|
|
link->dhcp6_pd_route_configured = true;
|
|
|
|
} else
|
|
|
|
log_link_debug(link, "Setting DHCPv6 PD routes");
|
|
|
|
|
|
|
|
r = dhcp6_pd_remove_old(link, false);
|
2020-07-08 04:19:13 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2020-04-04 20:07:18 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured)
|
|
|
|
link_check_ready(link);
|
|
|
|
else
|
|
|
|
link_set_state(link, LINK_STATE_CONFIGURING);
|
|
|
|
|
2020-07-07 04:19:49 +02:00
|
|
|
return 0;
|
2018-01-04 14:11:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static void dhcp6_pd_prefix_lost(Link *dhcp6_link) {
|
|
|
|
Link *link;
|
2018-09-19 02:32:19 +02:00
|
|
|
int r;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(dhcp6_link);
|
|
|
|
assert(dhcp6_link->manager);
|
2018-10-06 06:55:19 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(link, dhcp6_link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link == dhcp6_link)
|
|
|
|
continue;
|
2019-07-14 17:35:49 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_pd_remove(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
}
|
2018-09-19 02:32:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_remove_old(Link *link, bool force);
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_address_callback(Address *address) {
|
|
|
|
Address *a;
|
|
|
|
|
|
|
|
assert(address);
|
|
|
|
assert(address->link);
|
|
|
|
|
|
|
|
/* Make this called only once */
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(a, address->link->dhcp6_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
a->callback = NULL;
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
return dhcp6_remove_old(address->link, true);
|
|
|
|
}
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_remove_old(Link *link, bool force) {
|
|
|
|
Address *address;
|
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(link);
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (!force && (!link->dhcp6_address_configured || !link->dhcp6_route_configured))
|
|
|
|
return 0;
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (set_isempty(link->dhcp6_addresses_old) && set_isempty(link->dhcp6_routes_old))
|
|
|
|
return 0;
|
2018-09-26 02:09:17 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (!force) {
|
|
|
|
bool set_callback = !set_isempty(link->dhcp6_addresses);
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
if (address_is_ready(address)) {
|
|
|
|
set_callback = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-04 15:59:36 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (set_callback) {
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_addresses)
|
2020-07-22 20:13:42 +02:00
|
|
|
address->callback = dhcp6_address_callback;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-19 02:32:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
log_link_debug(link, "Removing old DHCPv6 addresses and routes.");
|
2020-06-10 04:57:35 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
link_dirty(link);
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp6_routes_old) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, NULL);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_addresses_old) {
|
2020-07-22 20:13:42 +02:00
|
|
|
k = address_remove(address, link, NULL);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
return r;
|
|
|
|
}
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_remove(Link *link) {
|
|
|
|
Address *address;
|
|
|
|
Route *route;
|
|
|
|
int k, r = 0;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(link);
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
link->dhcp6_address_configured = false;
|
|
|
|
link->dhcp6_route_configured = false;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
k = dhcp6_remove_old(link, true);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (set_isempty(link->dhcp6_addresses) && set_isempty(link->dhcp6_routes))
|
|
|
|
return r;
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
log_link_debug(link, "Removing DHCPv6 addresses and routes.");
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
link_dirty(link);
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(route, link->dhcp6_routes) {
|
2020-10-07 02:41:52 +02:00
|
|
|
k = route_remove(route, NULL, link, NULL);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2018-01-04 14:11:59 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(address, link->dhcp6_addresses) {
|
2020-07-22 20:13:42 +02:00
|
|
|
k = address_remove(address, link, NULL);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
return r;
|
2018-09-19 02:32:19 +02:00
|
|
|
}
|
2018-05-11 08:43:04 +02:00
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int dhcp6_route_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
|
2018-09-19 02:32:19 +02:00
|
|
|
int r;
|
2018-01-04 14:12:01 +01:00
|
|
|
|
2018-10-06 06:55:19 +02:00
|
|
|
assert(link);
|
2020-07-08 04:19:13 +02:00
|
|
|
assert(link->dhcp6_route_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp6_route_messages--;
|
2018-10-06 06:55:19 +02:00
|
|
|
|
2019-07-14 17:35:49 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
2018-09-19 02:32:19 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2020-07-08 04:19:13 +02:00
|
|
|
if (r < 0 && r != -EEXIST) {
|
|
|
|
log_link_message_warning_errno(link, m, r, "Failed to add unreachable route for DHCPv6 delegated subnet");
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link->dhcp6_route_messages == 0) {
|
|
|
|
log_link_debug(link, "Unreachable routes for DHCPv6 delegated subnets set");
|
|
|
|
link->dhcp6_route_configured = true;
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
r = dhcp6_remove_old(link, false);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:19:13 +02:00
|
|
|
link_check_ready(link);
|
|
|
|
}
|
2018-01-04 14:12:01 +01:00
|
|
|
|
2018-10-10 07:39:35 +02:00
|
|
|
return 1;
|
2018-01-04 14:11:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_set_unreachable_route(Link *link, const union in_addr_union *addr, uint8_t prefixlen) {
|
|
|
|
_cleanup_(route_freep) Route *route = NULL;
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
Route *ret;
|
2020-06-10 04:57:35 +02:00
|
|
|
int r;
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(link);
|
|
|
|
assert(addr);
|
2019-02-12 05:03:57 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
(void) in_addr_to_string(AF_INET6, addr, &buf);
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (prefixlen > 64) {
|
|
|
|
log_link_debug(link, "PD Prefix length > 64, ignoring prefix %s/%u",
|
|
|
|
strna(buf), prefixlen);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (prefixlen == 64) {
|
|
|
|
log_link_debug(link, "Not adding a blocking route for DHCPv6 delegated subnet %s/64 since distributed prefix is 64",
|
|
|
|
strna(buf));
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-19 02:32:19 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (prefixlen < 48)
|
|
|
|
log_link_warning(link, "PD Prefix length < 48, looks unusual %s/%u",
|
|
|
|
strna(buf), prefixlen);
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = route_new(&route);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
route->family = AF_INET6;
|
|
|
|
route->dst = *addr;
|
|
|
|
route->dst_prefixlen = prefixlen;
|
|
|
|
route->table = link_get_dhcp_route_table(link);
|
|
|
|
route->type = RTN_UNREACHABLE;
|
2020-12-03 11:00:56 +01:00
|
|
|
route->protocol = RTPROT_DHCP;
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = route_configure(route, link, dhcp6_route_handler, &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to set unreachable route for DHCPv6 delegated subnet %s/%u: %m",
|
|
|
|
strna(buf), prefixlen);
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
link->dhcp6_route_messages++;
|
2020-05-14 00:54:37 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = set_ensure_put(&link->dhcp6_routes, &route_hash_ops, ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store unreachable route for DHCPv6 delegated subnet %s/%u: %m",
|
|
|
|
strna(buf), prefixlen);
|
2018-01-04 14:11:59 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
(void) set_remove(link->dhcp6_routes_old, ret);
|
2020-07-08 04:19:13 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
return 1;
|
2018-01-04 14:11:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_pd_prefix_acquired(Link *dhcp6_link) {
|
|
|
|
Link *link;
|
|
|
|
int r;
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(dhcp6_link);
|
|
|
|
assert(dhcp6_link->dhcp6_lease);
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(link, dhcp6_link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link == dhcp6_link)
|
|
|
|
continue;
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_pd_prepare(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
|
|
|
}
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
for (sd_dhcp6_lease_reset_pd_prefix_iter(dhcp6_link->dhcp6_lease);;) {
|
|
|
|
uint32_t lifetime_preferred, lifetime_valid;
|
|
|
|
union in_addr_union pd_prefix, prefix;
|
|
|
|
uint8_t pd_prefix_len;
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = sd_dhcp6_lease_get_pd(dhcp6_link->dhcp6_lease, &pd_prefix.in6, &pd_prefix_len,
|
|
|
|
&lifetime_preferred, &lifetime_valid);
|
|
|
|
if (r < 0)
|
|
|
|
break;
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_set_unreachable_route(dhcp6_link, &pd_prefix, pd_prefix_len);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
2018-10-02 20:29:09 +02:00
|
|
|
continue;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
/* We are doing prefix allocation in two steps:
|
|
|
|
* 1. all those links that have a preferred subnet id will be assigned their subnet
|
|
|
|
* 2. all those links that remain will receive prefixes in sequential order. Prefixes
|
|
|
|
* that were previously already allocated to another link will be skipped.
|
|
|
|
* The assignment has to be split in two phases since subnet id
|
|
|
|
* preferences should be honored. Meaning that any subnet id should be
|
|
|
|
* handed out to the requesting link and not to some link that didn't
|
|
|
|
* specify any preference. */
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(pd_prefix_len <= 64);
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
prefix = pd_prefix;
|
|
|
|
r = in_addr_mask(AF_INET6, &prefix, pd_prefix_len);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(dhcp6_link, r, "Failed to mask DHCPv6 PD prefix: %m");
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (DEBUG_LOGGING) {
|
|
|
|
uint64_t n_prefixes = UINT64_C(1) << (64 - pd_prefix_len);
|
|
|
|
_cleanup_free_ char *buf = NULL;
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
(void) in_addr_to_string(AF_INET6, &prefix, &buf);
|
|
|
|
log_link_debug(dhcp6_link, "Assigning up to %" PRIu64 " prefixes from %s/%u",
|
|
|
|
n_prefixes, strna(buf), pd_prefix_len);
|
2018-10-02 20:29:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
dhcp6_pd_prefix_distribute(dhcp6_link,
|
|
|
|
&prefix,
|
|
|
|
pd_prefix_len,
|
|
|
|
lifetime_preferred,
|
|
|
|
lifetime_valid,
|
|
|
|
true);
|
|
|
|
|
|
|
|
dhcp6_pd_prefix_distribute(dhcp6_link,
|
|
|
|
&prefix,
|
|
|
|
pd_prefix_len,
|
|
|
|
lifetime_preferred,
|
|
|
|
lifetime_valid,
|
|
|
|
false);
|
|
|
|
}
|
2018-10-02 20:29:09 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(link, dhcp6_link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link == dhcp6_link)
|
2018-10-02 20:29:09 +02:00
|
|
|
continue;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_pd_finalize(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
2018-10-02 20:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2015-01-20 18:36:04 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
2020-07-08 04:19:13 +02:00
|
|
|
assert(link->dhcp6_address_messages > 0);
|
|
|
|
|
|
|
|
link->dhcp6_address_messages--;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2019-07-14 17:35:49 +02:00
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0 && r != -EEXIST) {
|
2019-11-30 07:54:07 +01:00
|
|
|
log_link_message_warning_errno(link, m, r, "Could not set DHCPv6 address");
|
2015-01-20 18:36:04 +01:00
|
|
|
link_enter_failed(link);
|
2019-07-06 02:03:02 +02:00
|
|
|
return 1;
|
2019-07-17 02:40:06 +02:00
|
|
|
} else if (r >= 0)
|
2019-07-14 17:35:49 +02:00
|
|
|
(void) manager_rtnl_process_address(rtnl, m, link->manager);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-08 04:19:13 +02:00
|
|
|
if (link->dhcp6_address_messages == 0) {
|
|
|
|
log_link_debug(link, "DHCPv6 addresses set");
|
|
|
|
link->dhcp6_address_configured = true;
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
r = dhcp6_remove_old(link, false);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:27:28 +02:00
|
|
|
r = link_set_routes(link);
|
2020-07-08 04:19:13 +02:00
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-07-14 17:35:49 +02:00
|
|
|
}
|
2019-07-06 02:04:10 +02:00
|
|
|
|
2015-01-20 18:36:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:35:48 +01:00
|
|
|
static void log_dhcp6_address(Link *link, const Address *address, char **ret) {
|
|
|
|
char valid_buf[FORMAT_TIMESPAN_MAX], preferred_buf[FORMAT_TIMESPAN_MAX];
|
|
|
|
const char *valid_str = NULL, *preferred_str = NULL;
|
|
|
|
_cleanup_free_ char *buffer = NULL;
|
|
|
|
bool by_ndisc = false;
|
|
|
|
Address *existing;
|
|
|
|
NDiscAddress *na;
|
|
|
|
int log_level, r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(address);
|
|
|
|
|
|
|
|
(void) in_addr_to_string(address->family, &address->in_addr, &buffer);
|
|
|
|
if (address->cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
|
|
|
|
valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
|
|
|
|
address->cinfo.ifa_valid * USEC_PER_SEC,
|
|
|
|
USEC_PER_SEC);
|
|
|
|
if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
|
|
|
|
preferred_str = format_timespan(preferred_buf, FORMAT_TIMESPAN_MAX,
|
|
|
|
address->cinfo.ifa_prefered * USEC_PER_SEC,
|
|
|
|
USEC_PER_SEC);
|
|
|
|
|
|
|
|
r = address_get(link, address, &existing);
|
|
|
|
if (r < 0) {
|
|
|
|
/* New address. */
|
|
|
|
log_level = LOG_INFO;
|
|
|
|
goto simple_log;
|
|
|
|
} else
|
|
|
|
log_level = LOG_DEBUG;
|
|
|
|
|
|
|
|
if (set_contains(link->dhcp6_addresses, address))
|
|
|
|
/* Already warned. */
|
|
|
|
goto simple_log;
|
|
|
|
|
|
|
|
if (address->prefixlen == existing->prefixlen)
|
|
|
|
/* Currently, only conflict in prefix length is reported. */
|
|
|
|
goto simple_log;
|
|
|
|
|
|
|
|
SET_FOREACH(na, link->ndisc_addresses)
|
|
|
|
if (address_compare_func(na->address, existing)) {
|
|
|
|
by_ndisc = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_link_warning(link, "DHCPv6 address %s/%u (valid %s%s, preferred %s%s) conflicts the existing address %s/%u%s.",
|
|
|
|
strnull(buffer), address->prefixlen,
|
|
|
|
valid_str ? "for " : "forever", strempty(valid_str),
|
|
|
|
preferred_str ? "for " : "forever", strempty(preferred_str),
|
|
|
|
strnull(buffer), existing->prefixlen,
|
|
|
|
by_ndisc ? "assigned by NDISC. Please try to use or update IPv6Token= setting "
|
|
|
|
"to change the address generated by NDISC, or disable UseAutonomousPrefix=" : "");
|
|
|
|
goto finalize;
|
|
|
|
|
|
|
|
simple_log:
|
|
|
|
log_link_full(link, log_level, "DHCPv6 address %s/%u (valid %s%s, preferred %s%s)",
|
|
|
|
strnull(buffer), address->prefixlen,
|
|
|
|
valid_str ? "for " : "forever", strempty(valid_str),
|
|
|
|
preferred_str ? "for " : "forever", strempty(preferred_str));
|
|
|
|
|
|
|
|
finalize:
|
|
|
|
if (ret)
|
|
|
|
*ret = TAKE_PTR(buffer);
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_update_address(
|
network: beef up ipv6 RA support considerably
This reworks sd-ndisc and networkd substantially to support IPv6 RA much more
comprehensively. Since the API is extended quite a bit networkd has been ported
over too, and the patch is not as straight-forward as one could wish. The
rework includes:
- Support for DNSSL, RDNSS and RA routing options in sd-ndisc and networkd. Two
new configuration options have been added to networkd to make this
configurable.
- sd-ndisc now exposes an sd_ndisc_router object that encapsulates a full RA
message, and has direct, friendly acessor functions for the singleton RA
properties, as well as an iterative interface to iterate through known and
unsupported options. The router object may either be retrieved from the wire,
or generated from raw data. In many ways the sd-ndisc API now matches the
sd-lldp API, except that no implicit database of seen data is kept. (Note
that sd-ndisc actually had a half-written, but unused implementaiton of such
a store, which is removed now.)
- sd-ndisc will now collect the reception timestamps of RA, which is useful to
make sd_ndisc_router fully descriptive of what it covers.
Fixes: #1079
2016-06-02 20:38:12 +02:00
|
|
|
Link *link,
|
2020-07-22 20:13:42 +02:00
|
|
|
const struct in6_addr *ip6_addr,
|
network: beef up ipv6 RA support considerably
This reworks sd-ndisc and networkd substantially to support IPv6 RA much more
comprehensively. Since the API is extended quite a bit networkd has been ported
over too, and the patch is not as straight-forward as one could wish. The
rework includes:
- Support for DNSSL, RDNSS and RA routing options in sd-ndisc and networkd. Two
new configuration options have been added to networkd to make this
configurable.
- sd-ndisc now exposes an sd_ndisc_router object that encapsulates a full RA
message, and has direct, friendly acessor functions for the singleton RA
properties, as well as an iterative interface to iterate through known and
unsupported options. The router object may either be retrieved from the wire,
or generated from raw data. In many ways the sd-ndisc API now matches the
sd-lldp API, except that no implicit database of seen data is kept. (Note
that sd-ndisc actually had a half-written, but unused implementaiton of such
a store, which is removed now.)
- sd-ndisc will now collect the reception timestamps of RA, which is useful to
make sd_ndisc_router fully descriptive of what it covers.
Fixes: #1079
2016-06-02 20:38:12 +02:00
|
|
|
uint32_t lifetime_preferred,
|
|
|
|
uint32_t lifetime_valid) {
|
|
|
|
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(address_freep) Address *addr = NULL;
|
2019-05-09 00:03:54 +02:00
|
|
|
_cleanup_free_ char *buffer = NULL;
|
2020-07-22 20:13:42 +02:00
|
|
|
Address *ret;
|
network: beef up ipv6 RA support considerably
This reworks sd-ndisc and networkd substantially to support IPv6 RA much more
comprehensively. Since the API is extended quite a bit networkd has been ported
over too, and the patch is not as straight-forward as one could wish. The
rework includes:
- Support for DNSSL, RDNSS and RA routing options in sd-ndisc and networkd. Two
new configuration options have been added to networkd to make this
configurable.
- sd-ndisc now exposes an sd_ndisc_router object that encapsulates a full RA
message, and has direct, friendly acessor functions for the singleton RA
properties, as well as an iterative interface to iterate through known and
unsupported options. The router object may either be retrieved from the wire,
or generated from raw data. In many ways the sd-ndisc API now matches the
sd-lldp API, except that no implicit database of seen data is kept. (Note
that sd-ndisc actually had a half-written, but unused implementaiton of such
a store, which is removed now.)
- sd-ndisc will now collect the reception timestamps of RA, which is useful to
make sd_ndisc_router fully descriptive of what it covers.
Fixes: #1079
2016-06-02 20:38:12 +02:00
|
|
|
int r;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2015-09-21 15:53:40 +02:00
|
|
|
r = address_new(&addr);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
2020-07-22 20:13:42 +02:00
|
|
|
return log_oom();
|
2015-01-20 18:36:04 +01:00
|
|
|
|
|
|
|
addr->family = AF_INET6;
|
2019-05-09 00:03:54 +02:00
|
|
|
addr->in_addr.in6 = *ip6_addr;
|
2015-02-02 12:13:17 +01:00
|
|
|
addr->flags = IFA_F_NOPREFIXROUTE;
|
2015-10-18 16:59:21 +02:00
|
|
|
addr->prefixlen = 128;
|
2015-01-20 18:36:04 +01:00
|
|
|
addr->cinfo.ifa_prefered = lifetime_preferred;
|
|
|
|
addr->cinfo.ifa_valid = lifetime_valid;
|
|
|
|
|
2020-12-08 05:35:48 +01:00
|
|
|
log_dhcp6_address(link, addr, &buffer);
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
r = address_configure(addr, link, dhcp6_address_handler, true, &ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to set DHCPv6 address %s/%u: %m",
|
2020-12-08 05:35:48 +01:00
|
|
|
strnull(buffer), addr->prefixlen);
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
link->dhcp6_address_messages++;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = set_ensure_put(&link->dhcp6_addresses, &address_hash_ops, ret);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
2020-07-22 20:13:42 +02:00
|
|
|
return log_link_error_errno(link, r, "Failed to store DHCPv6 address %s/%u: %m",
|
2020-12-08 05:35:48 +01:00
|
|
|
strnull(buffer), addr->prefixlen);
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
(void) set_remove(link->dhcp6_addresses_old, ret);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2019-07-14 17:35:49 +02:00
|
|
|
return 0;
|
2015-01-20 18:36:04 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_address_acquired(Link *link) {
|
2015-01-20 18:36:04 +01:00
|
|
|
int r;
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp6_lease);
|
|
|
|
|
|
|
|
for (sd_dhcp6_lease_reset_address_iter(link->dhcp6_lease);;) {
|
|
|
|
uint32_t lifetime_preferred, lifetime_valid;
|
|
|
|
struct in6_addr ip6_addr;
|
|
|
|
|
|
|
|
r = sd_dhcp6_lease_get_address(link->dhcp6_lease, &ip6_addr, &lifetime_preferred, &lifetime_valid);
|
|
|
|
if (r < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r = dhcp6_update_address(link, &ip6_addr, lifetime_preferred, lifetime_valid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_lease_ip_acquired(sd_dhcp6_client *client, Link *link) {
|
|
|
|
_cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease_old = NULL;
|
2015-01-20 18:36:04 +01:00
|
|
|
sd_dhcp6_lease *lease;
|
2020-07-22 20:13:42 +02:00
|
|
|
Address *a;
|
|
|
|
Route *rt;
|
|
|
|
int r;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-08 04:19:13 +02:00
|
|
|
link->dhcp6_address_configured = false;
|
2020-07-22 20:13:42 +02:00
|
|
|
link->dhcp6_route_configured = false;
|
|
|
|
|
|
|
|
link_dirty(link);
|
|
|
|
|
|
|
|
while ((a = set_steal_first(link->dhcp6_addresses))) {
|
|
|
|
r = set_ensure_put(&link->dhcp6_addresses_old, &address_hash_ops, a);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store old DHCPv6 address: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((rt = set_steal_first(link->dhcp6_routes))) {
|
|
|
|
r = set_ensure_put(&link->dhcp6_routes_old, &route_hash_ops, rt);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Failed to store old DHCPv6 route: %m");
|
|
|
|
}
|
2020-07-08 04:19:13 +02:00
|
|
|
|
2015-01-20 18:36:04 +01:00
|
|
|
r = sd_dhcp6_client_get_lease(client, &lease);
|
|
|
|
if (r < 0)
|
2020-07-22 20:13:42 +02:00
|
|
|
return log_link_error_errno(link, r, "Failed to get DHCPv6 lease: %m");
|
|
|
|
|
|
|
|
lease_old = TAKE_PTR(link->dhcp6_lease);
|
|
|
|
link->dhcp6_lease = sd_dhcp6_lease_ref(lease);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_address_acquired(link);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (dhcp6_lease_has_pd_prefix(lease)) {
|
|
|
|
r = dhcp6_pd_prefix_acquired(link);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2020-07-22 20:13:42 +02:00
|
|
|
} else if (dhcp6_lease_has_pd_prefix(lease_old))
|
|
|
|
/* When we had PD prefixes but not now, we need to remove them. */
|
|
|
|
dhcp6_pd_prefix_lost(link);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link->dhcp6_address_messages == 0)
|
2020-07-08 04:19:13 +02:00
|
|
|
link->dhcp6_address_configured = true;
|
2020-07-22 20:13:42 +02:00
|
|
|
else {
|
2020-07-08 04:19:13 +02:00
|
|
|
log_link_debug(link, "Setting DHCPv6 addresses");
|
2020-10-02 02:27:28 +02:00
|
|
|
/* address_handler calls link_set_routes() and link_set_nexthop(). Before they are
|
|
|
|
* called, the related flags must be cleared. Otherwise, the link becomes configured
|
|
|
|
* state before routes are configured. */
|
2020-07-12 20:12:11 +02:00
|
|
|
link->static_routes_configured = false;
|
|
|
|
link->static_nexthops_configured = false;
|
2020-07-08 04:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link->dhcp6_route_messages == 0)
|
|
|
|
link->dhcp6_route_configured = true;
|
|
|
|
else
|
|
|
|
log_link_debug(link, "Setting unreachable routes for DHCPv6 delegated subnets");
|
|
|
|
|
|
|
|
r = dhcp6_remove_old(link, false);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (link->dhcp6_address_configured && link->dhcp6_route_configured)
|
|
|
|
link_check_ready(link);
|
|
|
|
else
|
|
|
|
link_set_state(link, LINK_STATE_CONFIGURING);
|
|
|
|
|
2015-01-20 18:36:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static int dhcp6_lease_information_acquired(sd_dhcp6_client *client, Link *link) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_lease_lost(Link *link) {
|
2015-01-20 18:36:04 +01:00
|
|
|
int r;
|
2020-07-22 20:13:42 +02:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
|
|
|
|
log_link_info(link, "DHCPv6 lease lost");
|
|
|
|
|
|
|
|
if (dhcp6_lease_has_pd_prefix(link->dhcp6_lease))
|
|
|
|
dhcp6_pd_prefix_lost(link);
|
|
|
|
|
|
|
|
link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
|
|
|
|
|
|
|
|
r = dhcp6_remove(link);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
|
2014-12-10 15:17:34 +01:00
|
|
|
Link *link = userdata;
|
2020-07-22 20:13:42 +02:00
|
|
|
int r;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return;
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
switch (event) {
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_STOP:
|
|
|
|
case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
|
|
|
|
case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_lease_lost(link);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(link);
|
2015-01-20 18:36:04 +01:00
|
|
|
break;
|
|
|
|
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
|
2020-07-22 20:13:42 +02:00
|
|
|
r = dhcp6_lease_ip_acquired(client, link);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:06:10 +01:00
|
|
|
_fallthrough_;
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
|
2015-01-20 18:36:04 +01:00
|
|
|
r = dhcp6_lease_information_acquired(client, link);
|
2020-07-22 20:13:42 +02:00
|
|
|
if (r < 0)
|
2015-01-20 18:36:04 +01:00
|
|
|
link_enter_failed(link);
|
2014-12-10 15:17:34 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (event < 0)
|
2015-09-30 22:16:17 +02:00
|
|
|
log_link_warning_errno(link, event, "DHCPv6 error: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
else
|
2015-09-30 22:16:17 +02:00
|
|
|
log_link_warning(link, "DHCPv6 unknown event: %d", event);
|
2014-12-10 15:17:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:54:46 +02:00
|
|
|
int dhcp6_request_address(Link *link, int ir) {
|
2018-09-07 22:15:55 +02:00
|
|
|
int r, inf_req, pd;
|
2015-11-10 15:43:52 +01:00
|
|
|
bool running;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp6_client);
|
2018-09-07 22:15:55 +02:00
|
|
|
assert(link->network);
|
2020-07-22 20:13:42 +02:00
|
|
|
assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) > 0);
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_is_running(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2019-08-26 08:40:30 +02:00
|
|
|
running = r;
|
2015-09-23 13:10:26 +02:00
|
|
|
|
2018-09-07 22:15:55 +02:00
|
|
|
r = sd_dhcp6_client_get_prefix_delegation(link->dhcp6_client, &pd);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (pd && ir && link->network->dhcp6_force_pd_other_information) {
|
|
|
|
log_link_debug(link, "Enabling managed mode to request DHCPv6 PD with 'Other Information' set");
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
r = sd_dhcp6_client_set_address_request(link->dhcp6_client, false);
|
2020-07-08 04:19:13 +02:00
|
|
|
if (r < 0)
|
2018-09-07 22:15:55 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
ir = false;
|
|
|
|
}
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
if (running) {
|
2016-06-01 08:54:46 +02:00
|
|
|
r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &inf_req);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (inf_req == ir)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_stop(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2016-06-01 08:54:46 +02:00
|
|
|
} else {
|
|
|
|
r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-11-10 15:43:52 +01:00
|
|
|
}
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2016-06-01 08:54:46 +02:00
|
|
|
r = sd_dhcp6_client_set_information_request(link->dhcp6_client, ir);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_start(link->dhcp6_client);
|
2015-11-10 15:43:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-09-23 13:52:03 +02:00
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
int dhcp6_request_prefix_delegation(Link *link) {
|
|
|
|
Link *l;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
|
|
|
|
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
log_link_debug(link, "Requesting DHCPv6 prefixes to be delegated for new link");
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(l, link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
int r, enabled;
|
|
|
|
|
|
|
|
if (l == link)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!l->dhcp6_client)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_get_prefix_delegation(l->dhcp6_client, &enabled);
|
|
|
|
if (r < 0) {
|
|
|
|
log_link_warning_errno(l, r, "Cannot get prefix delegation when adding new link: %m");
|
|
|
|
link_enter_failed(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled == 0) {
|
|
|
|
r = sd_dhcp6_client_set_prefix_delegation(l->dhcp6_client, 1);
|
|
|
|
if (r < 0) {
|
|
|
|
log_link_warning_errno(l, r, "Cannot enable prefix delegation when adding new link: %m");
|
|
|
|
link_enter_failed(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_is_running(l->dhcp6_client);
|
|
|
|
if (r <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (enabled != 0) {
|
|
|
|
if (dhcp6_lease_has_pd_prefix(l->dhcp6_lease)) {
|
|
|
|
log_link_debug(l, "Requesting re-assignment of delegated prefixes after adding new link");
|
|
|
|
r = dhcp6_pd_prefix_acquired(l);
|
|
|
|
if (r < 0)
|
|
|
|
link_enter_failed(l);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_stop(l->dhcp6_client);
|
|
|
|
if (r < 0) {
|
|
|
|
log_link_warning_errno(l, r, "Cannot stop DHCPv6 prefix delegation client after adding new link: %m");
|
|
|
|
link_enter_failed(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_start(l->dhcp6_client);
|
|
|
|
if (r < 0) {
|
|
|
|
log_link_warning_errno(l, r, "Cannot restart DHCPv6 prefix delegation client after adding new link: %m");
|
|
|
|
link_enter_failed(l);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_link_debug(l, "Restarted DHCPv6 client to acquire prefix delegations after adding new link");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dhcp6_pd_prefix_acquired() may make the link in failed state. */
|
|
|
|
if (link->state == LINK_STATE_FAILED)
|
|
|
|
return -ENOANO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-16 10:07:07 +01:00
|
|
|
static int dhcp6_set_hostname(sd_dhcp6_client *client, Link *link) {
|
|
|
|
_cleanup_free_ char *hostname = NULL;
|
|
|
|
const char *hn;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->network->dhcp_send_hostname)
|
|
|
|
hn = NULL;
|
|
|
|
else if (link->network->dhcp_hostname)
|
|
|
|
hn = link->network->dhcp_hostname;
|
|
|
|
else {
|
|
|
|
r = gethostname_strict(&hostname);
|
|
|
|
if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
|
|
|
|
return r;
|
|
|
|
|
|
|
|
hn = hostname;
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:31:10 +02:00
|
|
|
r = sd_dhcp6_client_set_fqdn(client, hn);
|
|
|
|
if (r == -EINVAL && hostname)
|
|
|
|
/* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
|
|
|
|
log_link_warning_errno(link, r, "DHCP6 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
|
|
|
|
else if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set hostname: %m");
|
|
|
|
|
|
|
|
return 0;
|
2017-11-16 10:07:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:13:42 +02:00
|
|
|
static bool dhcp6_enable_prefix_delegation(Link *dhcp6_link) {
|
|
|
|
Link *link;
|
|
|
|
|
|
|
|
assert(dhcp6_link);
|
|
|
|
assert(dhcp6_link->manager);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
HASHMAP_FOREACH(link, dhcp6_link->manager->links) {
|
2020-07-22 20:13:42 +02:00
|
|
|
if (link == dhcp6_link)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!link_dhcp6_pd_is_enabled(link))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:41:41 +02:00
|
|
|
static int dhcp6_set_identifier(Link *link, sd_dhcp6_client *client) {
|
|
|
|
const DUID *duid;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
assert(client);
|
|
|
|
|
2020-10-26 14:09:13 +01:00
|
|
|
r = sd_dhcp6_client_set_mac(client, link->hw_addr.addr.bytes, link->hw_addr.length, link->iftype);
|
2020-10-02 13:41:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (link->network->iaid_set) {
|
|
|
|
r = sd_dhcp6_client_set_iaid(client, link->network->iaid);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
duid = link_get_duid(link);
|
|
|
|
if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
|
|
|
|
r = sd_dhcp6_client_set_duid_llt(client, duid->llt_time);
|
|
|
|
else
|
|
|
|
r = sd_dhcp6_client_set_duid(client,
|
|
|
|
duid->type,
|
|
|
|
duid->raw_data_len > 0 ? duid->raw_data : NULL,
|
|
|
|
duid->raw_data_len);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
int dhcp6_configure(Link *link) {
|
2018-08-01 03:42:49 +02:00
|
|
|
_cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
|
2020-05-26 10:46:54 +02:00
|
|
|
sd_dhcp6_option *vendor_option;
|
2020-05-01 16:30:31 +02:00
|
|
|
sd_dhcp6_option *send_option;
|
2020-04-23 14:31:47 +02:00
|
|
|
void *request_options;
|
2018-01-22 09:09:18 +01:00
|
|
|
int r;
|
2015-11-10 15:43:52 +01:00
|
|
|
|
|
|
|
assert(link);
|
2018-01-22 09:09:18 +01:00
|
|
|
assert(link->network);
|
2015-11-10 15:43:52 +01:00
|
|
|
|
2020-10-04 00:37:22 +02:00
|
|
|
if (!link_dhcp6_enabled(link) && !link_ipv6_accept_ra_enabled(link))
|
|
|
|
return 0;
|
|
|
|
|
2015-11-16 16:47:18 +01:00
|
|
|
if (link->dhcp6_client)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_new(&client);
|
2018-08-01 03:42:49 +02:00
|
|
|
if (r == -ENOMEM)
|
|
|
|
return log_oom();
|
2015-11-10 15:43:52 +01:00
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to create DHCP6 client: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2020-10-04 00:43:41 +02:00
|
|
|
r = sd_dhcp6_client_attach_event(client, link->manager->event, 0);
|
2014-12-10 15:17:34 +01:00
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to attach event: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2020-10-02 13:41:41 +02:00
|
|
|
r = dhcp6_set_identifier(link, client);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
2020-10-02 13:41:41 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set identifier: %m");
|
2016-03-31 01:33:55 +02:00
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp6_client_send_options) {
|
2020-05-01 16:30:31 +02:00
|
|
|
r = sd_dhcp6_client_add_option(client, send_option);
|
|
|
|
if (r == -EEXIST)
|
|
|
|
continue;
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set option: %m");
|
|
|
|
}
|
|
|
|
|
2017-11-16 10:07:07 +01:00
|
|
|
r = dhcp6_set_hostname(client, link);
|
|
|
|
if (r < 0)
|
2018-08-02 09:31:10 +02:00
|
|
|
return r;
|
2017-11-16 10:07:07 +01:00
|
|
|
|
2016-05-23 16:13:18 +02:00
|
|
|
r = sd_dhcp6_client_set_ifindex(client, link->ifindex);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set ifindex: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2020-10-16 07:56:38 +02:00
|
|
|
if (link->network->dhcp6_rapid_commit) {
|
2018-01-22 09:09:18 +01:00
|
|
|
r = sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_RAPID_COMMIT);
|
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for rapid commit: %m");
|
2018-01-22 09:09:18 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 16:31:10 +02:00
|
|
|
if (link->network->dhcp6_mudurl) {
|
|
|
|
r = sd_dhcp6_client_set_request_mud_url(client, link->network->dhcp6_mudurl);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set MUD URL: %m");
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(request_options, link->network->dhcp6_request_options) {
|
2020-04-23 14:31:47 +02:00
|
|
|
uint32_t option = PTR_TO_UINT32(request_options);
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_set_request_option(client, option);
|
|
|
|
if (r == -EEXIST) {
|
|
|
|
log_link_debug(link, "DHCP6 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set request flag for '%u': %m", option);
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:49:47 +02:00
|
|
|
if (link->network->dhcp6_user_class) {
|
|
|
|
r = sd_dhcp6_client_set_request_user_class(client, link->network->dhcp6_user_class);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set user class: %m");
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:14:47 +02:00
|
|
|
if (link->network->dhcp6_vendor_class) {
|
|
|
|
r = sd_dhcp6_client_set_request_vendor_class(client, link->network->dhcp6_vendor_class);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor class: %m");
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(vendor_option, link->network->dhcp6_client_send_vendor_options) {
|
2020-05-26 10:46:54 +02:00
|
|
|
r = sd_dhcp6_client_add_vendor_option(client, vendor_option);
|
|
|
|
if (r == -EEXIST)
|
|
|
|
continue;
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set vendor option: %m");
|
|
|
|
}
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set callback: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2018-01-04 14:11:51 +01:00
|
|
|
if (dhcp6_enable_prefix_delegation(link)) {
|
|
|
|
r = sd_dhcp6_client_set_prefix_delegation(client, true);
|
|
|
|
if (r < 0)
|
2018-08-01 03:42:49 +02:00
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix delegation: %m");
|
2018-01-04 14:11:51 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 05:14:12 +02:00
|
|
|
if (link->network->dhcp6_pd_length > 0) {
|
|
|
|
r = sd_dhcp6_client_set_prefix_delegation_hint(client, link->network->dhcp6_pd_length, &link->network->dhcp6_pd_address);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "DHCP6 CLIENT: Failed to set prefix hint: %m");
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:42:49 +02:00
|
|
|
link->dhcp6_client = TAKE_PTR(client);
|
2015-09-23 13:10:26 +02:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
return 0;
|
2014-12-10 15:17:34 +01:00
|
|
|
}
|
2019-06-29 21:09:51 +02:00
|
|
|
|
2020-10-02 13:41:41 +02:00
|
|
|
int dhcp6_update_mac(Link *link) {
|
|
|
|
bool restart;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->dhcp6_client)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
restart = sd_dhcp6_client_is_running(link->dhcp6_client) > 0;
|
|
|
|
|
|
|
|
if (restart) {
|
|
|
|
r = sd_dhcp6_client_stop(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = dhcp6_set_identifier(link, link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (restart) {
|
|
|
|
r = sd_dhcp6_client_start(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "Could not restart DHCPv6 client: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:15:44 +02:00
|
|
|
int link_serialize_dhcp6_client(Link *link, FILE *f) {
|
|
|
|
_cleanup_free_ char *duid = NULL;
|
|
|
|
uint32_t iaid;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!link->dhcp6_client)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_get_iaid(link->dhcp6_client, &iaid);
|
|
|
|
if (r >= 0)
|
|
|
|
fprintf(f, "DHCP6_CLIENT_IAID=0x%x\n", iaid);
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_duid_as_string(link->dhcp6_client, &duid);
|
|
|
|
if (r >= 0)
|
|
|
|
fprintf(f, "DHCP6_CLIENT_DUID=%s\n", duid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-01 06:18:22 +02:00
|
|
|
int config_parse_dhcp6_pd_hint(
|
|
|
|
const char* unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
|
|
|
Network *network = data;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
r = in_addr_prefix_from_string(rvalue, AF_INET6, (union in_addr_union *) &network->dhcp6_pd_address, &network->dhcp6_pd_length);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse PrefixDelegationHint=%s, ignoring assignment", rvalue);
|
2020-06-01 06:18:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (network->dhcp6_pd_length < 1 || network->dhcp6_pd_length > 128) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid prefix length='%d', ignoring assignment", network->dhcp6_pd_length);
|
2020-06-01 06:18:22 +02:00
|
|
|
network->dhcp6_pd_length = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int config_parse_dhcp6_mud_url(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
2020-06-10 04:57:35 +02:00
|
|
|
|
2020-06-01 06:18:22 +02:00
|
|
|
_cleanup_free_ char *unescaped = NULL;
|
|
|
|
Network *network = data;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
|
|
|
|
if (isempty(rvalue)) {
|
|
|
|
network->dhcp6_mudurl = mfree(network->dhcp6_mudurl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = cunescape(rvalue, 0, &unescaped);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-06-01 06:18:22 +02:00
|
|
|
"Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!http_url_is_valid(unescaped) || strlen(unescaped) > UINT8_MAX) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
2020-06-01 06:18:22 +02:00
|
|
|
"Failed to parse MUD URL '%s', ignoring: %m", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return free_and_replace(network->dhcp6_mudurl, unescaped);
|
|
|
|
}
|
2020-06-10 04:57:35 +02:00
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,
|
|
|
|
"Failed to parse WithoutRA= setting");
|
|
|
|
|
|
|
|
static const char* const dhcp6_client_start_mode_table[_DHCP6_CLIENT_START_MODE_MAX] = {
|
|
|
|
[DHCP6_CLIENT_START_MODE_NO] = "no",
|
|
|
|
[DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST] = "information-request",
|
|
|
|
[DHCP6_CLIENT_START_MODE_SOLICIT] = "solicit",
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(dhcp6_client_start_mode, DHCP6ClientStartMode);
|
|
|
|
|
|
|
|
int config_parse_dhcp6_pd_subnet_id(
|
2020-06-10 04:57:35 +02:00
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
int64_t *p = data;
|
|
|
|
uint64_t t;
|
2020-06-10 04:57:35 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
if (isempty(rvalue) || streq(rvalue, "auto")) {
|
|
|
|
*p = -1;
|
2020-06-10 04:57:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
r = safe_atoux64(rvalue, &t);
|
2020-06-10 04:57:35 +02:00
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-07-28 22:50:04 +02:00
|
|
|
"Failed to parse %s=, ignoring assignment: %s",
|
|
|
|
lvalue, rvalue);
|
2020-06-10 04:57:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-28 22:50:04 +02:00
|
|
|
if (t > INT64_MAX) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
2020-07-28 22:50:04 +02:00
|
|
|
"Invalid subnet id '%s', ignoring assignment.",
|
|
|
|
rvalue);
|
2020-06-10 04:57:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
*p = (int64_t) t;
|
|
|
|
|
2020-06-10 04:57:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-02 01:03:17 +02:00
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
int config_parse_dhcp6_pd_token(
|
|
|
|
const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
|
|
|
unsigned section_line,
|
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
2020-07-02 01:03:17 +02:00
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
union in_addr_union *addr = data, tmp;
|
|
|
|
int r;
|
2020-07-02 01:03:17 +02:00
|
|
|
|
2020-07-28 22:50:04 +02:00
|
|
|
assert(filename);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
if (isempty(rvalue)) {
|
|
|
|
*addr = IN_ADDR_NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = in_addr_from_string(AF_INET6, rvalue, &tmp);
|
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
|
|
|
"Failed to parse DHCPv6 Prefix Delegation token, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_addr_is_null(AF_INET6, &tmp)) {
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
|
|
|
"DHCPv6 Prefix Delegation token cannot be the ANY address, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = tmp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|