| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "conf-parser.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "in-addr-util.h" |
| 10 |
#include "networkd-address.h" |
| 11 |
#include "networkd-ipv6ll.h" |
| 12 |
#include "networkd-link.h" |
| 13 |
#include "networkd-manager.h" |
| 14 |
#include "networkd-network.h" |
| 15 |
#include "siphash24.h" |
| 16 |
#include "socket-util.h" |
| 17 |
#include "string-table.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "sysctl-util.h" |
| 20 |
|
| 21 |
bool link_ipv6ll_enabled(Link *link) { |
| 22 |
assert(link); |
| 23 |
|
| 24 |
if (!socket_ipv6_is_supported()) |
| 25 |
return false; |
| 26 |
|
| 27 |
if (link->flags & IFF_LOOPBACK) |
| 28 |
return false; |
| 29 |
|
| 30 |
if (!link->network) |
| 31 |
return false; |
| 32 |
|
| 33 |
if (link->iftype == ARPHRD_CAN) |
| 34 |
return false; |
| 35 |
|
| 36 |
if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon")) |
| 37 |
return false; |
| 38 |
|
| 39 |
if (link->network->bond) |
| 40 |
return false; |
| 41 |
|
| 42 |
return link->network->link_local & ADDRESS_FAMILY_IPV6; |
| 43 |
} |
| 44 |
|
| 45 |
bool network_has_static_ipv6ll_address(const Network *network) { |
| 46 |
assert(network); |
| 47 |
|
| 48 |
Address *a; |
| 49 |
ORDERED_HASHMAP_FOREACH(a, network->addresses_by_section) { |
| 50 |
if (a->family != AF_INET6) |
| 51 |
continue; |
| 52 |
if (in6_addr_is_set(&a->in_addr_peer.in6)) |
| 53 |
continue; |
| 54 |
if (in6_addr_is_link_local(&a->in_addr.in6)) |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
bool link_ipv6ll_enabled_harder(Link *link) { |
| 62 |
assert(link); |
| 63 |
|
| 64 |
/* This is mostly equivalent to link_ipv6ll_enabled(), but also checks if an IPv6LL address is |
| 65 |
* manually configured. */ |
| 66 |
|
| 67 |
if (link_ipv6ll_enabled(link)) |
| 68 |
return true; |
| 69 |
|
| 70 |
if (!link->network) |
| 71 |
return false; |
| 72 |
|
| 73 |
return network_has_static_ipv6ll_address(link->network); |
| 74 |
} |
| 75 |
|
| 76 |
IPv6LinkLocalAddressGenMode link_get_ipv6ll_addrgen_mode(Link *link) { |
| 77 |
assert(link); |
| 78 |
|
| 79 |
if (!link_ipv6ll_enabled(link)) |
| 80 |
return IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE; |
| 81 |
|
| 82 |
if (link->network->ipv6ll_address_gen_mode >= 0) |
| 83 |
return link->network->ipv6ll_address_gen_mode; |
| 84 |
|
| 85 |
if (in6_addr_is_set(&link->network->ipv6ll_stable_secret)) |
| 86 |
return IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_STABLE_PRIVACY; |
| 87 |
|
| 88 |
return IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_EUI64; |
| 89 |
} |
| 90 |
|
| 91 |
int ipv6ll_addrgen_mode_fill_message(sd_netlink_message *message, IPv6LinkLocalAddressGenMode mode) { |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(message); |
| 95 |
assert(mode >= 0 && mode < _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_MAX); |
| 96 |
|
| 97 |
r = sd_netlink_message_open_container(message, IFLA_AF_SPEC); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
r = sd_netlink_message_open_container(message, AF_INET6); |
| 102 |
if (r < 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
r = sd_netlink_message_append_u8(message, IFLA_INET6_ADDR_GEN_MODE, mode); |
| 106 |
if (r < 0) |
| 107 |
return r; |
| 108 |
|
| 109 |
r = sd_netlink_message_close_container(message); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
r = sd_netlink_message_close_container(message); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
int link_update_ipv6ll_addrgen_mode(Link *link, sd_netlink_message *message) { |
| 121 |
uint8_t mode; |
| 122 |
int family, r; |
| 123 |
|
| 124 |
assert(link); |
| 125 |
assert(message); |
| 126 |
|
| 127 |
r = sd_rtnl_message_get_family(message, &family); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
|
| 131 |
if (family != AF_UNSPEC) |
| 132 |
return 0; |
| 133 |
|
| 134 |
r = sd_netlink_message_enter_container(message, IFLA_AF_SPEC); |
| 135 |
if (r == -ENODATA) |
| 136 |
return 0; |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
|
| 140 |
r = sd_netlink_message_enter_container(message, AF_INET6); |
| 141 |
if (r == -ENODATA) |
| 142 |
return sd_netlink_message_exit_container(message); |
| 143 |
if (r < 0) |
| 144 |
return r; |
| 145 |
|
| 146 |
mode = (uint8_t) link->ipv6ll_address_gen_mode; |
| 147 |
r = sd_netlink_message_read_u8(message, IFLA_INET6_ADDR_GEN_MODE, &mode); |
| 148 |
if (r < 0 && r != -ENODATA) |
| 149 |
return r; |
| 150 |
|
| 151 |
r = sd_netlink_message_exit_container(message); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
|
| 155 |
r = sd_netlink_message_exit_container(message); |
| 156 |
if (r < 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
if (mode == (uint8_t) link->ipv6ll_address_gen_mode) |
| 160 |
return 0; |
| 161 |
|
| 162 |
if (mode >= _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_MAX) { |
| 163 |
log_link_debug(link, "Received invalid IPv6 link-local address generation mode (%u), ignoring.", mode); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
if (link->ipv6ll_address_gen_mode < 0) |
| 168 |
log_link_debug(link, "Saved IPv6 link-local address generation mode: %s", |
| 169 |
ipv6_link_local_address_gen_mode_to_string(mode)); |
| 170 |
else |
| 171 |
log_link_debug(link, "IPv6 link-local address generation mode is changed: %s -> %s", |
| 172 |
ipv6_link_local_address_gen_mode_to_string(link->ipv6ll_address_gen_mode), |
| 173 |
ipv6_link_local_address_gen_mode_to_string(mode)); |
| 174 |
|
| 175 |
link->ipv6ll_address_gen_mode = mode; |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
#define STABLE_SECRET_APP_ID_1 SD_ID128_MAKE(aa,05,1d,94,43,68,45,07,b9,73,f1,e8,e4,b7,34,52) |
| 180 |
#define STABLE_SECRET_APP_ID_2 SD_ID128_MAKE(52,c4,40,a0,9f,2f,48,58,a9,3a,f6,29,25,ba,7a,7d) |
| 181 |
|
| 182 |
int link_set_ipv6ll_stable_secret(Link *link) { |
| 183 |
struct in6_addr a; |
| 184 |
int r; |
| 185 |
|
| 186 |
assert(link); |
| 187 |
assert(link->manager); |
| 188 |
assert(link->network); |
| 189 |
|
| 190 |
if (link->network->ipv6ll_address_gen_mode != IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_STABLE_PRIVACY) |
| 191 |
return 0; |
| 192 |
|
| 193 |
if (in6_addr_is_set(&link->network->ipv6ll_stable_secret)) |
| 194 |
a = link->network->ipv6ll_stable_secret; |
| 195 |
else { |
| 196 |
sd_id128_t key; |
| 197 |
le64_t v; |
| 198 |
|
| 199 |
/* Generate a stable secret address from machine-ID and the interface name. */ |
| 200 |
|
| 201 |
r = sd_id128_get_machine_app_specific(STABLE_SECRET_APP_ID_1, &key); |
| 202 |
if (r < 0) |
| 203 |
return log_link_debug_errno(link, r, "Failed to generate key: %m"); |
| 204 |
|
| 205 |
v = htole64(siphash24_string(link->ifname, key.bytes)); |
| 206 |
memcpy(a.s6_addr, &v, sizeof(v)); |
| 207 |
|
| 208 |
r = sd_id128_get_machine_app_specific(STABLE_SECRET_APP_ID_2, &key); |
| 209 |
if (r < 0) |
| 210 |
return log_link_debug_errno(link, r, "Failed to generate key: %m"); |
| 211 |
|
| 212 |
v = htole64(siphash24_string(link->ifname, key.bytes)); |
| 213 |
assert_cc(sizeof(v) * 2 == sizeof(a.s6_addr)); |
| 214 |
memcpy(a.s6_addr + sizeof(v), &v, sizeof(v)); |
| 215 |
} |
| 216 |
|
| 217 |
return sysctl_write_ip_property(AF_INET6, link->ifname, "stable_secret", |
| 218 |
IN6_ADDR_TO_STRING(&a), manager_get_sysctl_shadow(link->manager)); |
| 219 |
} |
| 220 |
|
| 221 |
int link_set_ipv6ll_addrgen_mode(Link *link, IPv6LinkLocalAddressGenMode mode) { |
| 222 |
assert(link); |
| 223 |
assert(link->manager); |
| 224 |
assert(mode >= 0 && mode < _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_MAX); |
| 225 |
|
| 226 |
if (mode == link->ipv6ll_address_gen_mode) |
| 227 |
return 0; |
| 228 |
|
| 229 |
return sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "addr_gen_mode", mode, manager_get_sysctl_shadow(link->manager)); |
| 230 |
} |
| 231 |
|
| 232 |
static const char* const ipv6_link_local_address_gen_mode_table[_IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_MAX] = { |
| 233 |
[IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_EUI64] = "eui64", |
| 234 |
[IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE] = "none", |
| 235 |
[IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_STABLE_PRIVACY] = "stable-privacy", |
| 236 |
[IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_RANDOM] = "random", |
| 237 |
}; |
| 238 |
|
| 239 |
DEFINE_STRING_TABLE_LOOKUP(ipv6_link_local_address_gen_mode, IPv6LinkLocalAddressGenMode); |
| 240 |
DEFINE_CONFIG_PARSE_ENUM( |
| 241 |
config_parse_ipv6_link_local_address_gen_mode, |
| 242 |
ipv6_link_local_address_gen_mode, |
| 243 |
IPv6LinkLocalAddressGenMode); |