| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/rtnetlink.h> |
| 4 |
#include <netinet/in.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-ipv4ll.h" |
| 8 |
|
| 9 |
#include "errno-util.h" |
| 10 |
#include "netif-util.h" |
| 11 |
#include "networkd-address.h" |
| 12 |
#include "networkd-ipv4acd.h" |
| 13 |
#include "networkd-ipv4ll.h" |
| 14 |
#include "networkd-link.h" |
| 15 |
#include "networkd-manager.h" |
| 16 |
#include "parse-util.h" |
| 17 |
#include "set.h" |
| 18 |
#include "string-util.h" |
| 19 |
|
| 20 |
bool link_ipv4ll_enabled(Link *link) { |
| 21 |
assert(link); |
| 22 |
|
| 23 |
if (!link_ipv4acd_supported(link)) |
| 24 |
return false; |
| 25 |
|
| 26 |
if (!link->network) |
| 27 |
return false; |
| 28 |
|
| 29 |
if (link->network->bond) |
| 30 |
return false; |
| 31 |
|
| 32 |
return link->network->link_local & ADDRESS_FAMILY_IPV4; |
| 33 |
} |
| 34 |
|
| 35 |
static int address_new_from_ipv4ll(Link *link, Address **ret) { |
| 36 |
_cleanup_(address_unrefp) Address *address = NULL; |
| 37 |
struct in_addr addr; |
| 38 |
int r; |
| 39 |
|
| 40 |
assert(link); |
| 41 |
assert(link->ipv4ll); |
| 42 |
assert(ret); |
| 43 |
|
| 44 |
r = sd_ipv4ll_get_address(link->ipv4ll, &addr); |
| 45 |
if (r < 0) |
| 46 |
return r; |
| 47 |
|
| 48 |
r = address_new(&address); |
| 49 |
if (r < 0) |
| 50 |
return -ENOMEM; |
| 51 |
|
| 52 |
address->source = NETWORK_CONFIG_SOURCE_IPV4LL; |
| 53 |
address->family = AF_INET; |
| 54 |
address->in_addr.in = addr; |
| 55 |
address->prefixlen = 16; |
| 56 |
address->scope = RT_SCOPE_LINK; |
| 57 |
address->route_metric = IPV4LL_ROUTE_METRIC; |
| 58 |
|
| 59 |
*ret = TAKE_PTR(address); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static int ipv4ll_address_lost(Link *link) { |
| 64 |
_cleanup_(address_unrefp) Address *address = NULL; |
| 65 |
int r; |
| 66 |
|
| 67 |
assert(link); |
| 68 |
|
| 69 |
link->ipv4ll_address_configured = false; |
| 70 |
|
| 71 |
r = address_new_from_ipv4ll(link, &address); |
| 72 |
if (r == -ENOENT) |
| 73 |
return 0; |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
log_link_debug(link, "IPv4 link-local release "IPV4_ADDRESS_FMT_STR, |
| 78 |
IPV4_ADDRESS_FMT_VAL(address->in_addr.in)); |
| 79 |
|
| 80 |
return address_remove_and_cancel(address, link); |
| 81 |
} |
| 82 |
|
| 83 |
static int ipv4ll_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { |
| 84 |
int r; |
| 85 |
|
| 86 |
assert(link); |
| 87 |
assert(!link->ipv4ll_address_configured); |
| 88 |
assert(address); |
| 89 |
|
| 90 |
r = address_configure_handler_internal(m, link, address); |
| 91 |
if (r <= 0) |
| 92 |
return r; |
| 93 |
|
| 94 |
link->ipv4ll_address_configured = true; |
| 95 |
link_check_ready(link); |
| 96 |
|
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) { |
| 101 |
_cleanup_(address_unrefp) Address *address = NULL; |
| 102 |
int r; |
| 103 |
|
| 104 |
assert(ll); |
| 105 |
assert(link); |
| 106 |
|
| 107 |
link->ipv4ll_address_configured = false; |
| 108 |
|
| 109 |
r = address_new_from_ipv4ll(link, &address); |
| 110 |
if (r == -ENOENT) |
| 111 |
return 0; |
| 112 |
if (r < 0) |
| 113 |
return r; |
| 114 |
|
| 115 |
log_link_debug(link, "IPv4 link-local claim "IPV4_ADDRESS_FMT_STR, |
| 116 |
IPV4_ADDRESS_FMT_VAL(address->in_addr.in)); |
| 117 |
|
| 118 |
r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV4LL); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
|
| 122 |
return link_request_address(link, address, NULL, ipv4ll_address_handler, NULL); |
| 123 |
} |
| 124 |
|
| 125 |
static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata) { |
| 126 |
Link *link = ASSERT_PTR(userdata); |
| 127 |
int r; |
| 128 |
|
| 129 |
assert(link->network); |
| 130 |
|
| 131 |
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
| 132 |
return; |
| 133 |
|
| 134 |
switch (event) { |
| 135 |
case SD_IPV4LL_EVENT_STOP: |
| 136 |
r = ipv4ll_address_lost(link); |
| 137 |
if (r < 0) { |
| 138 |
link_enter_failed(link); |
| 139 |
return; |
| 140 |
} |
| 141 |
break; |
| 142 |
case SD_IPV4LL_EVENT_CONFLICT: |
| 143 |
r = ipv4ll_address_lost(link); |
| 144 |
if (r < 0) { |
| 145 |
link_enter_failed(link); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
r = sd_ipv4ll_restart(ll); |
| 150 |
if (r < 0) { |
| 151 |
log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); |
| 152 |
link_enter_failed(link); |
| 153 |
} |
| 154 |
break; |
| 155 |
case SD_IPV4LL_EVENT_BIND: |
| 156 |
r = ipv4ll_address_claimed(ll, link); |
| 157 |
if (r < 0) { |
| 158 |
log_link_error_errno(link, r, "Failed to configure ipv4ll address: %m"); |
| 159 |
link_enter_failed(link); |
| 160 |
return; |
| 161 |
} |
| 162 |
break; |
| 163 |
default: |
| 164 |
log_link_warning(link, "IPv4 link-local unknown event: %d", event); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static int ipv4ll_check_mac(sd_ipv4ll *ll, const struct ether_addr *mac, void *userdata) { |
| 169 |
Manager *m = ASSERT_PTR(userdata); |
| 170 |
struct hw_addr_data hw_addr; |
| 171 |
|
| 172 |
assert(mac); |
| 173 |
|
| 174 |
hw_addr = (struct hw_addr_data) { |
| 175 |
.length = ETH_ALEN, |
| 176 |
.ether = *mac, |
| 177 |
}; |
| 178 |
|
| 179 |
return link_get_by_hw_addr(m, &hw_addr, NULL) >= 0; |
| 180 |
} |
| 181 |
|
| 182 |
static int ipv4ll_set_address(Link *link) { |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(link); |
| 186 |
assert(link->network); |
| 187 |
assert(link->ipv4ll); |
| 188 |
|
| 189 |
/* 1. Use already assigned address. */ |
| 190 |
Address *a; |
| 191 |
SET_FOREACH(a, link->addresses) { |
| 192 |
if (a->source != NETWORK_CONFIG_SOURCE_IPV4LL) |
| 193 |
continue; |
| 194 |
|
| 195 |
assert(a->family == AF_INET); |
| 196 |
return sd_ipv4ll_set_address(link->ipv4ll, &a->in_addr.in); |
| 197 |
} |
| 198 |
|
| 199 |
/* 2. If no address is assigned yet, use explicitly configured address. */ |
| 200 |
if (in4_addr_is_set(&link->network->ipv4ll_start_address)) |
| 201 |
return sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address); |
| 202 |
|
| 203 |
/* 3. If KeepConfiguration=dynamic, use a foreign IPv4LL address. */ |
| 204 |
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) |
| 205 |
return 0; |
| 206 |
|
| 207 |
SET_FOREACH(a, link->addresses) { |
| 208 |
if (a->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
| 209 |
continue; |
| 210 |
if (a->family != AF_INET) |
| 211 |
continue; |
| 212 |
if (!in4_addr_is_link_local_dynamic(&a->in_addr.in)) |
| 213 |
continue; |
| 214 |
|
| 215 |
r = sd_ipv4ll_set_address(link->ipv4ll, &a->in_addr.in); |
| 216 |
if (r < 0) |
| 217 |
return r; |
| 218 |
|
| 219 |
/* Make sure the address is not removed by link_drop_unmanaged_addresses(). */ |
| 220 |
a->source = NETWORK_CONFIG_SOURCE_IPV4LL; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
int ipv4ll_configure(Link *link) { |
| 228 |
uint64_t seed; |
| 229 |
int r; |
| 230 |
|
| 231 |
assert(link); |
| 232 |
assert(link->network); |
| 233 |
|
| 234 |
if (!link_ipv4ll_enabled(link)) |
| 235 |
return 0; |
| 236 |
|
| 237 |
if (link->ipv4ll) |
| 238 |
return 0; |
| 239 |
|
| 240 |
r = sd_ipv4ll_new(&link->ipv4ll); |
| 241 |
if (r < 0) |
| 242 |
return r; |
| 243 |
|
| 244 |
r = sd_ipv4ll_attach_event(link->ipv4ll, link->manager->event, 0); |
| 245 |
if (r < 0) |
| 246 |
return r; |
| 247 |
|
| 248 |
if (link->dev && |
| 249 |
net_get_unique_predictable_data(link->dev, true, &seed) >= 0) { |
| 250 |
r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed); |
| 251 |
if (r < 0) |
| 252 |
return r; |
| 253 |
} |
| 254 |
|
| 255 |
r = ipv4ll_set_address(link); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
r = sd_ipv4ll_set_mac(link->ipv4ll, &link->hw_addr.ether); |
| 260 |
if (r < 0) |
| 261 |
return r; |
| 262 |
|
| 263 |
r = sd_ipv4ll_set_timeout(link->ipv4ll, link->network->ipv4_dad_timeout_usec); |
| 264 |
if (r < 0) |
| 265 |
return r; |
| 266 |
|
| 267 |
r = sd_ipv4ll_set_ifindex(link->ipv4ll, link->ifindex); |
| 268 |
if (r < 0) |
| 269 |
return r; |
| 270 |
|
| 271 |
r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link); |
| 272 |
if (r < 0) |
| 273 |
return r; |
| 274 |
|
| 275 |
return sd_ipv4ll_set_check_mac_callback(link->ipv4ll, ipv4ll_check_mac, link->manager); |
| 276 |
} |
| 277 |
|
| 278 |
int ipv4ll_start(Link *link) { |
| 279 |
int r; |
| 280 |
|
| 281 |
assert(link); |
| 282 |
|
| 283 |
if (!link->ipv4ll) |
| 284 |
return 0; |
| 285 |
|
| 286 |
if (sd_ipv4ll_is_running(link->ipv4ll)) |
| 287 |
return 0; |
| 288 |
|
| 289 |
if (!link_has_carrier(link)) |
| 290 |
return 0; |
| 291 |
|
| 292 |
/* On exit, we cannot and should not start sd-ipv4ll. */ |
| 293 |
r = sd_event_get_state(link->manager->event); |
| 294 |
if (r < 0) |
| 295 |
return r; |
| 296 |
if (r == SD_EVENT_FINISHED) |
| 297 |
return 0; |
| 298 |
|
| 299 |
r = sd_ipv4ll_start(link->ipv4ll); |
| 300 |
if (r < 0) |
| 301 |
return r; |
| 302 |
|
| 303 |
return 1; /* started */ |
| 304 |
} |
| 305 |
|
| 306 |
int link_drop_ipv4ll_config(Link *link, Network *network) { |
| 307 |
int ret = 0; |
| 308 |
|
| 309 |
assert(link); |
| 310 |
assert(link->network); |
| 311 |
|
| 312 |
if (link->network == network) |
| 313 |
return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */ |
| 314 |
|
| 315 |
if (!link_ipv4ll_enabled(link)) { |
| 316 |
/* The client is disabled. Stop if it is running, and drop the address. */ |
| 317 |
ret = sd_ipv4ll_stop(link->ipv4ll); |
| 318 |
|
| 319 |
/* Also, explicitly drop the address for the case that this is called on start up. |
| 320 |
* See also comments in link_drop_dhcp4_config(). */ |
| 321 |
Address *a; |
| 322 |
SET_FOREACH(a, link->addresses) { |
| 323 |
if (a->source != NETWORK_CONFIG_SOURCE_IPV4LL) |
| 324 |
continue; |
| 325 |
|
| 326 |
assert(a->family == AF_INET); |
| 327 |
RET_GATHER(ret, address_remove_and_cancel(a, link)); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll); |
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
int ipv4ll_update_mac(Link *link) { |
| 336 |
assert(link); |
| 337 |
|
| 338 |
if (link->hw_addr.length != ETH_ALEN) |
| 339 |
return 0; |
| 340 |
if (ether_addr_is_null(&link->hw_addr.ether)) |
| 341 |
return 0; |
| 342 |
if (!link->ipv4ll) |
| 343 |
return 0; |
| 344 |
|
| 345 |
return sd_ipv4ll_set_mac(link->ipv4ll, &link->hw_addr.ether); |
| 346 |
} |
| 347 |
|
| 348 |
int config_parse_ipv4ll( |
| 349 |
const char *unit, |
| 350 |
const char *filename, |
| 351 |
unsigned line, |
| 352 |
const char *section, |
| 353 |
unsigned section_line, |
| 354 |
const char *lvalue, |
| 355 |
int ltype, |
| 356 |
const char *rvalue, |
| 357 |
void *data, |
| 358 |
void *userdata) { |
| 359 |
|
| 360 |
AddressFamily *link_local = ASSERT_PTR(data); |
| 361 |
int r; |
| 362 |
|
| 363 |
assert(filename); |
| 364 |
assert(lvalue); |
| 365 |
assert(rvalue); |
| 366 |
|
| 367 |
/* Note that this is mostly like |
| 368 |
* config_parse_address_family(), except that it |
| 369 |
* applies only to IPv4 */ |
| 370 |
|
| 371 |
r = parse_boolean(rvalue); |
| 372 |
if (r < 0) { |
| 373 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 374 |
"Failed to parse %s=%s, ignoring assignment. " |
| 375 |
"Note that the setting %s= is deprecated, please use LinkLocalAddressing= instead.", |
| 376 |
lvalue, rvalue, lvalue); |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
SET_FLAG(*link_local, ADDRESS_FAMILY_IPV4, r); |
| 381 |
|
| 382 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 383 |
"%s=%s is deprecated, please use LinkLocalAddressing=%s instead.", |
| 384 |
lvalue, rvalue, address_family_to_string(*link_local)); |
| 385 |
|
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
int config_parse_ipv4ll_address( |
| 390 |
const char *unit, |
| 391 |
const char *filename, |
| 392 |
unsigned line, |
| 393 |
const char *section, |
| 394 |
unsigned section_line, |
| 395 |
const char *lvalue, |
| 396 |
int ltype, |
| 397 |
const char *rvalue, |
| 398 |
void *data, |
| 399 |
void *userdata) { |
| 400 |
|
| 401 |
union in_addr_union a; |
| 402 |
struct in_addr *ipv4ll_address = ASSERT_PTR(data); |
| 403 |
int r; |
| 404 |
|
| 405 |
assert(filename); |
| 406 |
assert(lvalue); |
| 407 |
assert(rvalue); |
| 408 |
|
| 409 |
if (isempty(rvalue)) { |
| 410 |
*ipv4ll_address = (struct in_addr) {}; |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
r = in_addr_from_string(AF_INET, rvalue, &a); |
| 415 |
if (r < 0) { |
| 416 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 417 |
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); |
| 418 |
return 0; |
| 419 |
} |
| 420 |
if (!in4_addr_is_link_local_dynamic(&a.in)) { |
| 421 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 422 |
"Specified address cannot be used as an IPv4 link local address, ignoring assignment: %s", |
| 423 |
rvalue); |
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
*ipv4ll_address = a.in; |
| 428 |
return 0; |
| 429 |
} |