| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
|
| 5 |
#include "missing-network.h" |
| 6 |
#include "netlink-util.h" |
| 7 |
#include "networkd-address.h" |
| 8 |
#include "networkd-link.h" |
| 9 |
#include "networkd-manager.h" |
| 10 |
#include "networkd-netlabel.h" |
| 11 |
|
| 12 |
static int netlabel_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
| 13 |
int r; |
| 14 |
|
| 15 |
assert_se(rtnl); |
| 16 |
assert_se(m); |
| 17 |
assert_se(link); |
| 18 |
|
| 19 |
r = sd_netlink_message_get_errno(m); |
| 20 |
if (r < 0) { |
| 21 |
log_link_message_warning_errno(link, m, r, "NetLabel operation failed, ignoring"); |
| 22 |
return 1; |
| 23 |
} |
| 24 |
|
| 25 |
log_link_debug(link, "NetLabel operation successful"); |
| 26 |
|
| 27 |
return 1; |
| 28 |
} |
| 29 |
|
| 30 |
static int netlabel_command(uint16_t command, const char *label, const Address *address) { |
| 31 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 32 |
int r; |
| 33 |
|
| 34 |
assert(command != NLBL_UNLABEL_C_UNSPEC && command < __NLBL_UNLABEL_C_MAX); |
| 35 |
assert(address); |
| 36 |
assert(address->link); |
| 37 |
assert(address->link->ifname); |
| 38 |
assert(address->link->manager); |
| 39 |
assert(address->link->manager->genl); |
| 40 |
assert(IN_SET(address->family, AF_INET, AF_INET6)); |
| 41 |
|
| 42 |
if (address->link->manager->state == MANAGER_STOPPED) |
| 43 |
return 0; /* We cannot call link_ref() below. */ |
| 44 |
|
| 45 |
r = sd_genl_message_new(address->link->manager->genl, NETLBL_NLTYPE_UNLABELED_NAME, command, &m); |
| 46 |
if (r < 0) |
| 47 |
return r; |
| 48 |
|
| 49 |
r = sd_netlink_message_append_string(m, NLBL_UNLABEL_A_IFACE, address->link->ifname); |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
|
| 53 |
if (command == NLBL_UNLABEL_C_STATICADD) { |
| 54 |
assert(label); |
| 55 |
r = sd_netlink_message_append_string(m, NLBL_UNLABEL_A_SECCTX, label); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
} |
| 59 |
|
| 60 |
union in_addr_union netmask, masked_addr; |
| 61 |
r = in_addr_prefixlen_to_netmask(address->family, &netmask, address->prefixlen); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
/* |
| 66 |
* When adding rules, kernel adds the address to its hash table _applying also the netmask_, but on |
| 67 |
* removal, an exact match is required _without netmask applied_, so apply the mask on both |
| 68 |
* operations. |
| 69 |
*/ |
| 70 |
masked_addr = address->in_addr; |
| 71 |
r = in_addr_mask(address->family, &masked_addr, address->prefixlen); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
if (address->family == AF_INET) { |
| 76 |
r = sd_netlink_message_append_in_addr(m, NLBL_UNLABEL_A_IPV4ADDR, &masked_addr.in); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
|
| 80 |
r = sd_netlink_message_append_in_addr(m, NLBL_UNLABEL_A_IPV4MASK, &netmask.in); |
| 81 |
} else if (address->family == AF_INET6) { |
| 82 |
r = sd_netlink_message_append_in6_addr(m, NLBL_UNLABEL_A_IPV6ADDR, &masked_addr.in6); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
r = sd_netlink_message_append_in6_addr(m, NLBL_UNLABEL_A_IPV6MASK, &netmask.in6); |
| 87 |
} |
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
|
| 91 |
r = netlink_call_async(address->link->manager->genl, NULL, m, netlabel_handler, link_netlink_destroy_callback, |
| 92 |
address->link); |
| 93 |
if (r < 0) |
| 94 |
return r; |
| 95 |
|
| 96 |
link_ref(address->link); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
void address_add_netlabel(const Address *address) { |
| 101 |
int r; |
| 102 |
|
| 103 |
assert(address); |
| 104 |
|
| 105 |
if (!address->netlabel) |
| 106 |
return; |
| 107 |
|
| 108 |
r = netlabel_command(NLBL_UNLABEL_C_STATICADD, address->netlabel, address); |
| 109 |
if (r < 0) |
| 110 |
log_link_warning_errno(address->link, r, "Adding NetLabel %s for IP address %s failed, ignoring", address->netlabel, |
| 111 |
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); |
| 112 |
else |
| 113 |
log_link_debug(address->link, "Adding NetLabel %s for IP address %s", address->netlabel, |
| 114 |
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); |
| 115 |
} |
| 116 |
|
| 117 |
void address_del_netlabel(const Address *address) { |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(address); |
| 121 |
|
| 122 |
if (!address->netlabel) |
| 123 |
return; |
| 124 |
|
| 125 |
r = netlabel_command(NLBL_UNLABEL_C_STATICREMOVE, address->netlabel, address); |
| 126 |
if (r < 0) |
| 127 |
log_link_warning_errno(address->link, r, "Deleting NetLabel %s for IP address %s failed, ignoring", address->netlabel, |
| 128 |
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); |
| 129 |
else |
| 130 |
log_link_debug(address->link, "Deleting NetLabel %s for IP address %s", address->netlabel, |
| 131 |
IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); |
| 132 |
} |