| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
#include "sd-network.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "dns-configuration.h" |
| 8 |
#include "format-ifname.h" |
| 9 |
#include "hashmap.h" |
| 10 |
#include "log-link.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "strv.h" |
| 13 |
#include "wait-online-link.h" |
| 14 |
#include "wait-online-manager.h" |
| 15 |
|
| 16 |
static Link* link_free(Link *l) { |
| 17 |
|
| 18 |
if (!l) |
| 19 |
return NULL; |
| 20 |
|
| 21 |
if (l->manager) { |
| 22 |
hashmap_remove(l->manager->links_by_index, INT_TO_PTR(l->ifindex)); |
| 23 |
hashmap_remove(l->manager->links_by_name, l->ifname); |
| 24 |
|
| 25 |
STRV_FOREACH(n, l->altnames) |
| 26 |
hashmap_remove(l->manager->links_by_name, *n); |
| 27 |
} |
| 28 |
|
| 29 |
dns_configuration_free(l->dns_configuration); |
| 30 |
|
| 31 |
free(l->state); |
| 32 |
free(l->ifname); |
| 33 |
strv_free(l->altnames); |
| 34 |
return mfree(l); |
| 35 |
} |
| 36 |
|
| 37 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free); |
| 38 |
|
| 39 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 40 |
link_hash_ops_by_index, |
| 41 |
void, trivial_hash_func, trivial_compare_func, |
| 42 |
Link, link_free); |
| 43 |
|
| 44 |
static int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) { |
| 45 |
_cleanup_(link_freep) Link *l = NULL; |
| 46 |
_cleanup_free_ char *n = NULL; |
| 47 |
int r; |
| 48 |
|
| 49 |
assert(m); |
| 50 |
assert(ifindex > 0); |
| 51 |
assert(ifname); |
| 52 |
|
| 53 |
n = strdup(ifname); |
| 54 |
if (!n) |
| 55 |
return -ENOMEM; |
| 56 |
|
| 57 |
l = new(Link, 1); |
| 58 |
if (!l) |
| 59 |
return -ENOMEM; |
| 60 |
|
| 61 |
*l = (Link) { |
| 62 |
.manager = m, |
| 63 |
.ifname = TAKE_PTR(n), |
| 64 |
.ifindex = ifindex, |
| 65 |
.required_operstate = LINK_OPERSTATE_RANGE_DEFAULT, |
| 66 |
.dns_configuration = hashmap_remove(m->dns_configuration_by_link_index, INT_TO_PTR(ifindex)), |
| 67 |
}; |
| 68 |
|
| 69 |
r = hashmap_ensure_put(&m->links_by_index, &link_hash_ops_by_index, INT_TO_PTR(ifindex), l); |
| 70 |
if (r < 0) |
| 71 |
return r; |
| 72 |
|
| 73 |
r = hashmap_ensure_put(&m->links_by_name, &string_hash_ops, l->ifname, l); |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
if (ret) |
| 78 |
*ret = l; |
| 79 |
|
| 80 |
TAKE_PTR(l); |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
static int link_update_name(Link *l, sd_netlink_message *m) { |
| 85 |
char ifname_from_index[IF_NAMESIZE]; |
| 86 |
const char *ifname; |
| 87 |
int r; |
| 88 |
|
| 89 |
assert(l); |
| 90 |
assert(l->manager); |
| 91 |
assert(m); |
| 92 |
|
| 93 |
r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
| 94 |
if (r == -ENODATA) |
| 95 |
/* Hmm? But ok. */ |
| 96 |
return 0; |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
|
| 100 |
if (streq(ifname, l->ifname)) |
| 101 |
return 0; |
| 102 |
|
| 103 |
/* The kernel sometimes sends wrong ifname change. Let's confirm the received name. */ |
| 104 |
r = format_ifname(l->ifindex, ifname_from_index); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
|
| 108 |
if (!streq(ifname, ifname_from_index)) { |
| 109 |
log_link_debug(l, "New interface name '%s' received from the kernel does not correspond " |
| 110 |
"with the name currently configured on the actual interface '%s'. Ignoring.", |
| 111 |
ifname, ifname_from_index); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
hashmap_remove(l->manager->links_by_name, l->ifname); |
| 116 |
|
| 117 |
r = free_and_strdup(&l->ifname, ifname); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
r = hashmap_ensure_put(&l->manager->links_by_name, &string_hash_ops, l->ifname, l); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static int link_update_altnames(Link *l, sd_netlink_message *m) { |
| 129 |
_cleanup_strv_free_ char **altnames = NULL; |
| 130 |
int r; |
| 131 |
|
| 132 |
assert(l); |
| 133 |
assert(l->manager); |
| 134 |
assert(m); |
| 135 |
|
| 136 |
r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames); |
| 137 |
if (r == -ENODATA) |
| 138 |
/* The message does not have IFLA_PROP_LIST container attribute. It does not mean the |
| 139 |
* interface has no alternative name. */ |
| 140 |
return 0; |
| 141 |
if (r < 0) |
| 142 |
return r; |
| 143 |
|
| 144 |
if (strv_equal(altnames, l->altnames)) |
| 145 |
return 0; |
| 146 |
|
| 147 |
STRV_FOREACH(n, l->altnames) |
| 148 |
hashmap_remove(l->manager->links_by_name, *n); |
| 149 |
|
| 150 |
strv_free_and_replace(l->altnames, altnames); |
| 151 |
|
| 152 |
STRV_FOREACH(n, l->altnames) { |
| 153 |
r = hashmap_ensure_put(&l->manager->links_by_name, &string_hash_ops, *n, l); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
} |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int link_update_rtnl(Link *l, sd_netlink_message *m) { |
| 162 |
int r; |
| 163 |
|
| 164 |
assert(l); |
| 165 |
assert(l->manager); |
| 166 |
assert(m); |
| 167 |
|
| 168 |
r = sd_rtnl_message_link_get_flags(m, &l->flags); |
| 169 |
if (r < 0) |
| 170 |
return r; |
| 171 |
|
| 172 |
r = link_update_name(l, m); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
r = link_update_altnames(l, m); |
| 177 |
if (r < 0) |
| 178 |
return r; |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
int rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
| 184 |
Manager *m = ASSERT_PTR(userdata); |
| 185 |
uint16_t type; |
| 186 |
Link *l; |
| 187 |
const char *ifname; |
| 188 |
int ifindex, r; |
| 189 |
|
| 190 |
assert(rtnl); |
| 191 |
assert(mm); |
| 192 |
|
| 193 |
r = sd_netlink_message_get_type(mm, &type); |
| 194 |
if (r < 0) { |
| 195 |
log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
r = sd_rtnl_message_link_get_ifindex(mm, &ifindex); |
| 200 |
if (r < 0) { |
| 201 |
log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m"); |
| 202 |
return 0; |
| 203 |
} else if (ifindex <= 0) { |
| 204 |
log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex); |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname); |
| 209 |
if (r < 0) { |
| 210 |
log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
l = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex)); |
| 215 |
|
| 216 |
switch (type) { |
| 217 |
|
| 218 |
case RTM_NEWLINK: |
| 219 |
if (!l) { |
| 220 |
log_debug("Found link %s(%i)", ifname, ifindex); |
| 221 |
|
| 222 |
r = link_new(m, &l, ifindex, ifname); |
| 223 |
if (r < 0) { |
| 224 |
log_warning_errno(r, "Failed to create link object for %s(%i), ignoring: %m", ifname, ifindex); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
r = link_update_rtnl(l, mm); |
| 230 |
if (r < 0) |
| 231 |
log_link_warning_errno(l, r, "Failed to process RTNL link message, ignoring: %m"); |
| 232 |
|
| 233 |
r = link_update_monitor(l); |
| 234 |
if (r < 0) |
| 235 |
log_link_full_errno(l, IN_SET(r, -ENODATA, -ENOENT) ? LOG_DEBUG : LOG_WARNING, r, |
| 236 |
"Failed to update link state, ignoring: %m"); |
| 237 |
|
| 238 |
break; |
| 239 |
|
| 240 |
case RTM_DELLINK: |
| 241 |
if (l) { |
| 242 |
log_link_debug(l, "Removing link"); |
| 243 |
link_free(l); |
| 244 |
} |
| 245 |
|
| 246 |
break; |
| 247 |
} |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
int link_update_monitor(Link *l) { |
| 253 |
_cleanup_free_ char *required_operstate = NULL, *required_family = NULL, |
| 254 |
*ipv4_address_state = NULL, *ipv6_address_state = NULL, *state = NULL; |
| 255 |
int r, ret = 0; |
| 256 |
|
| 257 |
assert(l); |
| 258 |
assert(l->ifname); |
| 259 |
|
| 260 |
r = sd_network_link_get_required_for_online(l->ifindex); |
| 261 |
if (r < 0 && r != -ENODATA) |
| 262 |
ret = log_link_debug_errno(l, r, "Failed to determine whether the link is required for online or not, " |
| 263 |
"assuming required: %m"); |
| 264 |
l->required_for_online = r != 0; |
| 265 |
|
| 266 |
r = sd_network_link_get_required_operstate_for_online(l->ifindex, &required_operstate); |
| 267 |
if (r < 0 && r != -ENODATA) |
| 268 |
ret = log_link_debug_errno(l, r, "Failed to get required operational state, ignoring: %m"); |
| 269 |
|
| 270 |
if (isempty(required_operstate)) |
| 271 |
l->required_operstate = LINK_OPERSTATE_RANGE_DEFAULT; |
| 272 |
else { |
| 273 |
r = parse_operational_state_range(required_operstate, &l->required_operstate); |
| 274 |
if (r < 0) |
| 275 |
ret = log_link_debug_errno(l, r, "Failed to parse required operational state, ignoring: %m"); |
| 276 |
} |
| 277 |
|
| 278 |
r = network_link_get_operational_state(l->ifindex, &l->operational_state); |
| 279 |
if (r < 0) |
| 280 |
ret = log_link_debug_errno(l, r, "Failed to get operational state, ignoring: %m"); |
| 281 |
|
| 282 |
r = sd_network_link_get_required_family_for_online(l->ifindex, &required_family); |
| 283 |
if (r < 0 && r != -ENODATA) |
| 284 |
ret = log_link_debug_errno(l, r, "Failed to get required address family, ignoring: %m"); |
| 285 |
|
| 286 |
if (isempty(required_family)) |
| 287 |
l->required_family = ADDRESS_FAMILY_NO; |
| 288 |
else { |
| 289 |
AddressFamily f; |
| 290 |
|
| 291 |
f = link_required_address_family_from_string(required_family); |
| 292 |
if (f < 0) |
| 293 |
ret = log_link_debug_errno(l, f, "Failed to parse required address family, ignoring: %m"); |
| 294 |
else |
| 295 |
l->required_family = f; |
| 296 |
} |
| 297 |
|
| 298 |
r = sd_network_link_get_ipv4_address_state(l->ifindex, &ipv4_address_state); |
| 299 |
if (r < 0) |
| 300 |
ret = log_link_debug_errno(l, r, "Failed to get IPv4 address state, ignoring: %m"); |
| 301 |
else { |
| 302 |
LinkAddressState s; |
| 303 |
|
| 304 |
s = link_address_state_from_string(ipv4_address_state); |
| 305 |
if (s < 0) |
| 306 |
ret = log_link_debug_errno(l, s, "Failed to parse IPv4 address state, ignoring: %m"); |
| 307 |
else |
| 308 |
l->ipv4_address_state = s; |
| 309 |
} |
| 310 |
|
| 311 |
r = sd_network_link_get_ipv6_address_state(l->ifindex, &ipv6_address_state); |
| 312 |
if (r < 0) |
| 313 |
ret = log_link_debug_errno(l, r, "Failed to get IPv6 address state, ignoring: %m"); |
| 314 |
else { |
| 315 |
LinkAddressState s; |
| 316 |
|
| 317 |
s = link_address_state_from_string(ipv6_address_state); |
| 318 |
if (s < 0) |
| 319 |
ret = log_link_debug_errno(l, s, "Failed to parse IPv6 address state, ignoring: %m"); |
| 320 |
else |
| 321 |
l->ipv6_address_state = s; |
| 322 |
} |
| 323 |
|
| 324 |
r = sd_network_link_get_setup_state(l->ifindex, &state); |
| 325 |
if (r < 0) |
| 326 |
ret = log_link_debug_errno(l, r, "Failed to get setup state, ignoring: %m"); |
| 327 |
else |
| 328 |
free_and_replace(l->state, state); |
| 329 |
|
| 330 |
return ret; |
| 331 |
} |