2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2017-02-11 00:47:55 +01:00
|
|
|
|
2019-05-09 02:33:45 +02:00
|
|
|
#include <netinet/in.h>
|
2017-02-11 00:47:55 +01:00
|
|
|
#include <linux/if.h>
|
|
|
|
|
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "networkd-ipv6-proxy-ndp.h"
|
|
|
|
#include "networkd-link.h"
|
|
|
|
#include "networkd-manager.h"
|
|
|
|
#include "networkd-network.h"
|
2017-12-13 05:47:10 +01:00
|
|
|
#include "socket-util.h"
|
2019-02-18 06:30:32 +01:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "sysctl-util.h"
|
2017-02-11 00:47:55 +01:00
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int set_ipv6_proxy_ndp_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2017-02-11 00:47:55 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0 && r != -EEXIST)
|
2019-11-30 07:54:07 +01:00
|
|
|
log_link_message_warning_errno(link, m, r, "Could not add IPv6 proxy ndp address entry, ignoring");
|
2017-02-11 00:47:55 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a request to the kernel to add a IPv6 Proxy entry to the neighbour table */
|
2020-09-29 22:06:17 +02:00
|
|
|
static int ipv6_proxy_ndp_address_configure(Link *link, const struct in6_addr *address) {
|
2017-02-11 00:47:55 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->manager);
|
2020-09-29 22:06:17 +02:00
|
|
|
assert(address);
|
2017-02-11 00:47:55 +01:00
|
|
|
|
|
|
|
/* create new netlink message */
|
2020-09-29 22:06:17 +02:00
|
|
|
r = sd_rtnl_message_new_neigh(link->manager->rtnl, &req, RTM_NEWNEIGH, link->ifindex, AF_INET6);
|
2017-02-11 00:47:55 +01:00
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not create RTM_NEWNEIGH message: %m");
|
2017-02-11 00:47:55 +01:00
|
|
|
|
|
|
|
r = sd_rtnl_message_neigh_set_flags(req, NLM_F_REQUEST | NTF_PROXY);
|
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not set neighbor flags: %m");
|
2017-02-11 00:47:55 +01:00
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
r = sd_netlink_message_append_in6_addr(req, NDA_DST, address);
|
2017-02-11 00:47:55 +01:00
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
|
2017-02-11 00:47:55 +01:00
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
r = netlink_call_async(link->manager->rtnl, NULL, req, set_ipv6_proxy_ndp_address_handler,
|
2018-11-28 21:06:52 +01:00
|
|
|
link_netlink_destroy_callback, link);
|
2017-02-11 00:47:55 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
|
|
|
|
|
2018-10-06 06:55:19 +02:00
|
|
|
link_ref(link);
|
|
|
|
|
2017-02-11 00:47:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:37:18 +02:00
|
|
|
static bool ipv6_proxy_ndp_is_needed(Link *link) {
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (link->flags & IFF_LOOPBACK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!link->network)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (link->network->ipv6_proxy_ndp >= 0)
|
|
|
|
return link->network->ipv6_proxy_ndp;
|
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
return !set_isempty(link->network->ipv6_proxy_ndp_addresses);
|
2020-09-29 21:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ipv6_proxy_ndp_set(Link *link) {
|
|
|
|
bool v;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
if (!socket_ipv6_is_supported())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
v = ipv6_proxy_ndp_is_needed(link);
|
|
|
|
|
|
|
|
r = sysctl_write_ip_property_boolean(AF_INET6, link->ifname, "proxy_ndp", v);
|
|
|
|
if (r < 0)
|
2020-10-23 10:32:19 +02:00
|
|
|
return log_link_warning_errno(link, r, "Cannot configure proxy NDP for the interface, ignoring: %m");
|
2020-09-29 21:37:18 +02:00
|
|
|
|
2020-09-29 22:24:03 +02:00
|
|
|
return v;
|
2020-09-29 21:37:18 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 00:47:55 +01:00
|
|
|
/* configure all ipv6 proxy ndp addresses */
|
2020-09-29 22:15:32 +02:00
|
|
|
int link_set_ipv6_proxy_ndp_addresses(Link *link) {
|
2020-09-29 22:06:17 +02:00
|
|
|
struct in6_addr *address;
|
2017-02-11 00:47:55 +01:00
|
|
|
int r;
|
|
|
|
|
2017-12-13 05:47:10 +01:00
|
|
|
assert(link);
|
2020-09-29 22:06:17 +02:00
|
|
|
assert(link->network);
|
2017-12-13 05:47:10 +01:00
|
|
|
|
2017-02-11 00:47:55 +01:00
|
|
|
/* enable or disable proxy_ndp itself depending on whether ipv6_proxy_ndp_addresses are set or not */
|
|
|
|
r = ipv6_proxy_ndp_set(link);
|
2020-09-29 22:24:03 +02:00
|
|
|
if (r <= 0)
|
2020-10-23 10:32:19 +02:00
|
|
|
return 0;
|
2017-02-11 00:47:55 +01:00
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
SET_FOREACH(address, link->network->ipv6_proxy_ndp_addresses) {
|
|
|
|
r = ipv6_proxy_ndp_address_configure(link, address);
|
|
|
|
if (r < 0)
|
2017-02-11 00:47:55 +01:00
|
|
|
return r;
|
|
|
|
}
|
2020-09-29 22:06:17 +02:00
|
|
|
|
2017-02-11 00:47:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-09-29 21:37:18 +02:00
|
|
|
|
|
|
|
int config_parse_ipv6_proxy_ndp_address(
|
|
|
|
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-09-29 22:06:17 +02:00
|
|
|
_cleanup_free_ struct in6_addr *address = NULL;
|
2020-09-29 21:37:18 +02:00
|
|
|
Network *network = userdata;
|
|
|
|
union in_addr_union buffer;
|
2020-09-29 22:06:17 +02:00
|
|
|
int r;
|
2020-09-29 21:37:18 +02:00
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(rvalue);
|
2020-09-29 22:06:17 +02:00
|
|
|
assert(network);
|
2020-09-29 21:37:18 +02:00
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
if (isempty(rvalue)) {
|
|
|
|
network->ipv6_proxy_ndp_addresses = set_free_free(network->ipv6_proxy_ndp_addresses);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-29 21:37:18 +02:00
|
|
|
|
|
|
|
r = in_addr_from_string(AF_INET6, rvalue, &buffer);
|
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r,
|
2020-09-29 22:06:17 +02:00
|
|
|
"Failed to parse IPv6 proxy NDP address, ignoring: %s", rvalue);
|
2020-09-29 21:37:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_addr_is_null(AF_INET6, &buffer)) {
|
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
|
|
|
"IPv6 proxy NDP address cannot be the ANY address, ignoring: %s", rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:06:17 +02:00
|
|
|
address = newdup(struct in6_addr, &buffer.in6, 1);
|
|
|
|
if (!address)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = set_ensure_put(&network->ipv6_proxy_ndp_addresses, &in6_addr_hash_ops, address);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
if (r > 0)
|
|
|
|
TAKE_PTR(address);
|
2020-09-29 21:37:18 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|