2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/if_addrlabel.h>
|
|
|
|
|
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "netlink-util.h"
|
2020-09-29 18:40:03 +02:00
|
|
|
#include "networkd-address-label.h"
|
|
|
|
#include "networkd-link.h"
|
2017-04-25 12:36:50 +02:00
|
|
|
#include "networkd-manager.h"
|
2020-09-29 18:40:03 +02:00
|
|
|
#include "networkd-network.h"
|
2017-04-25 12:36:50 +02:00
|
|
|
#include "parse-util.h"
|
|
|
|
|
2020-09-29 18:25:12 +02:00
|
|
|
AddressLabel *address_label_free(AddressLabel *label) {
|
2017-04-25 12:36:50 +02:00
|
|
|
if (!label)
|
2020-09-29 18:25:12 +02:00
|
|
|
return NULL;
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
if (label->network) {
|
2020-09-29 18:20:43 +02:00
|
|
|
assert(label->section);
|
|
|
|
hashmap_remove(label->network->address_labels_by_section, label->section);
|
2017-04-25 12:36:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 18:20:43 +02:00
|
|
|
network_config_section_free(label->section);
|
2020-09-29 18:25:12 +02:00
|
|
|
return mfree(label);
|
2017-04-25 12:36:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 18:40:03 +02:00
|
|
|
DEFINE_NETWORK_SECTION_FUNCTIONS(AddressLabel, address_label_free);
|
|
|
|
|
2017-04-25 12:36:50 +02:00
|
|
|
static int address_label_new_static(Network *network, const char *filename, unsigned section_line, AddressLabel **ret) {
|
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_(network_config_section_freep) NetworkConfigSection *n = NULL;
|
|
|
|
_cleanup_(address_label_freep) AddressLabel *label = NULL;
|
2017-04-25 12:36:50 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(network);
|
|
|
|
assert(ret);
|
2020-09-29 18:20:43 +02:00
|
|
|
assert(filename);
|
|
|
|
assert(section_line > 0);
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2020-09-29 18:20:43 +02:00
|
|
|
r = network_config_section_new(filename, section_line, &n);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2020-09-29 18:20:43 +02:00
|
|
|
label = hashmap_get(network->address_labels_by_section, n);
|
|
|
|
if (label) {
|
|
|
|
*ret = TAKE_PTR(label);
|
|
|
|
return 0;
|
2017-04-25 12:36:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 06:57:04 +01:00
|
|
|
label = new(AddressLabel, 1);
|
|
|
|
if (!label)
|
|
|
|
return -ENOMEM;
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2018-11-12 06:57:04 +01:00
|
|
|
*label = (AddressLabel) {
|
|
|
|
.network = network,
|
2020-09-29 18:20:43 +02:00
|
|
|
.section = TAKE_PTR(n),
|
2018-11-12 06:57:04 +01:00
|
|
|
};
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2020-09-29 18:20:43 +02:00
|
|
|
r = hashmap_ensure_allocated(&network->address_labels_by_section, &network_config_hash_ops);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2018-11-12 06:57:04 +01:00
|
|
|
|
2020-09-29 18:20:43 +02:00
|
|
|
r = hashmap_put(network->address_labels_by_section, label->section, label);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2018-11-12 06:57:04 +01:00
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
*ret = TAKE_PTR(label);
|
2017-04-25 12:36:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
static int address_label_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
|
2018-11-28 21:14:53 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(m);
|
|
|
|
assert(link);
|
|
|
|
assert(link->ifname);
|
|
|
|
assert(link->address_label_messages > 0);
|
|
|
|
|
|
|
|
link->address_label_messages--;
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = sd_netlink_message_get_errno(m);
|
2019-07-14 17:35:49 +02: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 address label");
|
2019-07-14 17:35:49 +02:00
|
|
|
link_enter_failed(link);
|
|
|
|
return 1;
|
|
|
|
} else if (r >= 0)
|
|
|
|
(void) manager_rtnl_process_address(rtnl, m, link->manager);
|
2018-11-28 21:14:53 +01:00
|
|
|
|
|
|
|
if (link->address_label_messages == 0)
|
|
|
|
log_link_debug(link, "Addresses label set");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-29 18:33:25 +02:00
|
|
|
static int address_label_configure(AddressLabel *label, Link *link) {
|
2017-04-25 12:36:50 +02:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(label);
|
|
|
|
assert(link);
|
|
|
|
assert(link->ifindex > 0);
|
|
|
|
assert(link->manager);
|
|
|
|
assert(link->manager->rtnl);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_addrlabel(link->manager->rtnl, &req, RTM_NEWADDRLABEL,
|
2017-05-06 02:04:07 +02:00
|
|
|
link->ifindex, AF_INET6);
|
2017-04-25 12:36:50 +02:00
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not allocate RTM_NEWADDR message: %m");
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
r = sd_rtnl_message_addrlabel_set_prefixlen(req, label->prefixlen);
|
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not set prefixlen: %m");
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
r = sd_netlink_message_append_u32(req, IFAL_LABEL, label->label);
|
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not append IFAL_LABEL attribute: %m");
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &label->in_addr.in6);
|
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m");
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2018-11-28 21:06:52 +01:00
|
|
|
r = netlink_call_async(link->manager->rtnl, NULL, req,
|
2020-09-29 18:33:25 +02:00
|
|
|
address_label_handler,
|
2018-11-28 21:06:52 +01:00
|
|
|
link_netlink_destroy_callback, link);
|
2017-04-25 12:36:50 +02:00
|
|
|
if (r < 0)
|
2020-01-07 04:53:19 +01:00
|
|
|
return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
link_ref(link);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-29 18:33:25 +02:00
|
|
|
int link_set_address_labels(Link *link) {
|
|
|
|
AddressLabel *label;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(label, link->network->address_labels_by_section) {
|
|
|
|
r = address_label_configure(label, link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_link_warning_errno(link, r, "Could not set address label: %m");
|
|
|
|
|
|
|
|
link->address_label_messages++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:21:59 +02:00
|
|
|
void network_drop_invalid_address_labels(Network *network) {
|
2020-09-29 18:28:35 +02:00
|
|
|
AddressLabel *label;
|
|
|
|
|
|
|
|
assert(network);
|
|
|
|
|
|
|
|
HASHMAP_FOREACH(label, network->address_labels_by_section)
|
|
|
|
if (section_is_invalid(label->section))
|
|
|
|
address_label_free(label);
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:36:50 +02:00
|
|
|
int config_parse_address_label_prefix(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) {
|
|
|
|
|
2019-03-01 05:27:47 +01:00
|
|
|
_cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
|
2017-04-25 12:36:50 +02:00
|
|
|
Network *network = userdata;
|
2017-05-06 02:04:07 +02:00
|
|
|
int r;
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
r = address_label_new_static(network, filename, section_line, &n);
|
|
|
|
if (r < 0)
|
2020-07-16 07:48:08 +02:00
|
|
|
return log_oom();
|
2017-04-25 12:36:50 +02:00
|
|
|
|
2017-05-06 02:04:07 +02:00
|
|
|
r = in_addr_prefix_from_string(rvalue, AF_INET6, &n->in_addr, &n->prefixlen);
|
2017-04-25 12:36:50 +02:00
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r, "Address label is invalid, ignoring assignment: %s", rvalue);
|
2017-04-25 12:36:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int config_parse_address_label(
|
|
|
|
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) {
|
|
|
|
|
2019-03-01 05:27:47 +01:00
|
|
|
_cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
|
2017-04-25 12:36:50 +02:00
|
|
|
Network *network = userdata;
|
|
|
|
uint32_t k;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
r = address_label_new_static(network, filename, section_line, &n);
|
|
|
|
if (r < 0)
|
2020-07-16 07:48:08 +02:00
|
|
|
return log_oom();
|
2017-04-25 12:36:50 +02:00
|
|
|
|
|
|
|
r = safe_atou32(rvalue, &k);
|
|
|
|
if (r < 0) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address label, ignoring: %s", rvalue);
|
2017-04-25 12:36:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k == 0xffffffffUL) {
|
2020-07-16 07:48:08 +02:00
|
|
|
log_syntax(unit, LOG_WARNING, filename, line, 0, "Address label is invalid, ignoring: %s", rvalue);
|
2017-04-25 12:36:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->label = k;
|
|
|
|
n = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|