]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
network/route: drop Route object even if we fail to remove the route
[thirdparty/systemd.git] / src / network / networkd-link.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f579559b 2
176b8be1 3#include <net/if.h>
9aa5d8ba 4#include <netinet/in.h>
f579559b 5#include <linux/if.h>
8f815e8b 6#include <linux/if_arp.h>
518cd6b5 7#include <linux/if_link.h>
2a0d07d6 8#include <linux/netdevice.h>
8430841b 9#include <sys/socket.h>
4cc7a82c 10#include <unistd.h>
f579559b 11
b5efdb8a 12#include "alloc-util.h"
8a77e245 13#include "arphrd-util.h"
c0267a59 14#include "batadv.h"
737f1405
YW
15#include "bond.h"
16#include "bridge.h"
1346b1f0 17#include "bus-util.h"
3be9d62a
YW
18#include "device-private.h"
19#include "device-util.h"
bd91b83e 20#include "dhcp-lease-internal.h"
686d13b9 21#include "env-file.h"
4bb7cc82 22#include "ethtool-util.h"
6706ce2f 23#include "event-util.h"
3ffd4af2 24#include "fd-util.h"
cf1d700d 25#include "fileio.h"
176b8be1 26#include "format-util.h"
af664001 27#include "fs-util.h"
e2341b6b 28#include "glyph-util.h"
3ae6b3bf 29#include "logarithm.h"
ef118d00 30#include "missing_network.h"
cf1d700d 31#include "netlink-util.h"
c6f7c917 32#include "network-internal.h"
fb486c90 33#include "networkd-address-label.h"
093e3533 34#include "networkd-address.h"
9671ae9d 35#include "networkd-bridge-fdb.h"
ff9e0783 36#include "networkd-bridge-mdb.h"
11cee6ef 37#include "networkd-bridge-vlan.h"
3ddcbeea 38#include "networkd-can.h"
d5ebcf65 39#include "networkd-dhcp-prefix-delegation.h"
8fcf1d61 40#include "networkd-dhcp-server.h"
ca5ad760
YW
41#include "networkd-dhcp4.h"
42#include "networkd-dhcp6.h"
76a86ffd 43#include "networkd-ipv4acd.h"
ca5ad760 44#include "networkd-ipv4ll.h"
76c5a0f2 45#include "networkd-ipv6-proxy-ndp.h"
6a1af3d4
YW
46#include "networkd-link-bus.h"
47#include "networkd-link.h"
8e1ad1ea 48#include "networkd-lldp-tx.h"
23f53b99 49#include "networkd-manager.h"
1e7a0e21 50#include "networkd-ndisc.h"
e4a71bf3 51#include "networkd-neighbor.h"
75156ccb 52#include "networkd-nexthop.h"
19d9a5ad 53#include "networkd-queue.h"
7465dd22 54#include "networkd-radv.h"
fc35a9f8 55#include "networkd-route-util.h"
3b6a3bde 56#include "networkd-route.h"
bce67bbe 57#include "networkd-routing-policy-rule.h"
0fa8ee6c 58#include "networkd-setlink.h"
19d9a5ad 59#include "networkd-sriov.h"
3b5a4fc6 60#include "networkd-state-file.h"
19d9a5ad 61#include "networkd-sysctl.h"
986b2514 62#include "networkd-wifi.h"
cf1d700d
TG
63#include "set.h"
64#include "socket-util.h"
15a5e950 65#include "stdio-util.h"
8b43440b 66#include "string-table.h"
51517f9e 67#include "strv.h"
34658df2 68#include "tc.h"
e4de7287 69#include "tmpfile-util.h"
af7a86b8 70#include "tuntap.h"
299ad32d 71#include "udev-util.h"
737f1405 72#include "vrf.h"
fc2f9534 73
2278d9f6
YW
74void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
75 assert(link);
76 assert(ret);
77
78 if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
79 *ret = link->network->required_operstate_for_online;
3255bda6
YW
80 else if (link->iftype == ARPHRD_CAN)
81 *ret = (const LinkOperationalStateRange) {
82 .min = LINK_OPERSTATE_CARRIER,
83 .max = LINK_OPERSTATE_CARRIER,
84 };
2278d9f6
YW
85 else
86 *ret = LINK_OPERSTATE_RANGE_DEFAULT;
87}
88
5e0534f1 89bool link_ipv6_enabled(Link *link) {
439689c6
SS
90 assert(link);
91
92 if (!socket_ipv6_is_supported())
93 return false;
94
5fb5fae9 95 if (link->iftype == ARPHRD_CAN)
2b00a4e0
TY
96 return false;
97
5fb5fae9
YW
98 if (!link->network)
99 return false;
100
101 if (link->network->bond)
af9ba57a
YW
102 return false;
103
bd7e0a3f 104 if (link_may_have_ipv6ll(link, /* check_multicast = */ false))
adfeee49
YW
105 return true;
106
107 if (network_has_static_ipv6_configurations(link->network))
108 return true;
109
110 return false;
439689c6
SS
111}
112
fc35a9f8
YW
113bool link_has_ipv6_connectivity(Link *link) {
114 LinkAddressState ipv6_address_state;
115
116 assert(link);
117
118 link_get_address_states(link, NULL, &ipv6_address_state, NULL);
119
120 switch (ipv6_address_state) {
121 case LINK_ADDRESS_STATE_ROUTABLE:
122 /* If the interface has a routable IPv6 address, then we assume yes. */
123 return true;
124
125 case LINK_ADDRESS_STATE_DEGRADED:
126 /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
127 * there is an IPv6 default gateway. */
128 return link_has_default_gateway(link, AF_INET6);
129
130 case LINK_ADDRESS_STATE_OFF:
131 /* No IPv6 address. */
132 return false;
133
134 default:
135 assert_not_reached();
136 }
137}
138
e33232d4 139static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
7191a57a
YW
140 assert(link);
141
d8d4db58
YW
142 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
143 return false;
144
2fd3908b
YW
145 if (!link->network)
146 return allow_unmanaged;
7191a57a 147
baa95d22
YW
148 if (!link->network->configure_without_carrier) {
149 if (link->set_flags_messages > 0)
150 return false;
151
152 if (!link_has_carrier(link))
153 return false;
154 }
7191a57a 155
0fa8ee6c
YW
156 if (link->set_link_messages > 0)
157 return false;
158
112a0972
YW
159 if (!link->activated)
160 return false;
112a0972 161
7191a57a
YW
162 return true;
163}
164
e33232d4
YW
165bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
166 return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
167}
168
15761549
YW
169void link_ntp_settings_clear(Link *link) {
170 link->ntp = strv_free(link->ntp);
171}
172
173void link_dns_settings_clear(Link *link) {
f5fbe71d 174 if (link->n_dns != UINT_MAX)
e77bd3fd
YW
175 for (unsigned i = 0; i < link->n_dns; i++)
176 in_addr_full_free(link->dns[i]);
15761549 177 link->dns = mfree(link->dns);
f5fbe71d 178 link->n_dns = UINT_MAX;
15761549 179
6e4571f0
YW
180 link->search_domains = ordered_set_free(link->search_domains);
181 link->route_domains = ordered_set_free(link->route_domains);
15761549
YW
182
183 link->dns_default_route = -1;
184 link->llmnr = _RESOLVE_SUPPORT_INVALID;
185 link->mdns = _RESOLVE_SUPPORT_INVALID;
186 link->dnssec_mode = _DNSSEC_MODE_INVALID;
187 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
188
189 link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
190}
191
1a6bb31f
YW
192static void link_free_engines(Link *link) {
193 if (!link)
194 return;
195
196 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
b3fac19f 197
1a6bb31f
YW
198 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
199 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
e49bad01 200 link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
1a6bb31f 201
35778343 202 link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
c01b9b87 203 link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
1a6bb31f 204
5385e5f9
YW
205 link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
206
1a6bb31f 207 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
b3fac19f 208
1a6bb31f 209 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1633c457 210 link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
b3fac19f 211
1a6bb31f 212 link->ndisc = sd_ndisc_unref(link->ndisc);
b3fac19f
YW
213 link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
214 ndisc_flush(link);
215
1a6bb31f
YW
216 link->radv = sd_radv_unref(link->radv);
217}
218
8301aa0b 219static Link *link_free(Link *link) {
8301aa0b 220 assert(link);
f579559b 221
15761549
YW
222 link_ntp_settings_clear(link);
223 link_dns_settings_clear(link);
224
8eec0b9d 225 link->neighbors = set_free(link->neighbors);
8eec0b9d 226 link->addresses = set_free(link->addresses);
3a67b8bb
YW
227 link->qdiscs = set_free(link->qdiscs);
228 link->tclasses = set_free(link->tclasses);
adda1ed9 229
a27588d4 230 link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
4b409e85 231
1a6bb31f 232 link_free_engines(link);
49699bac 233
b0ff6dea 234 set_free(link->sr_iov_virt_port_ifindices);
c166a070 235 free(link->ifname);
572b21d9 236 strv_free(link->alternative_names);
ceac4078 237 free(link->kind);
8d968fdd 238 free(link->ssid);
6706ce2f 239 free(link->previous_ssid);
c643bda5 240 free(link->driver);
6cad256d 241
a34e58d4
YW
242 unlink_and_free(link->lease_file);
243 unlink_and_free(link->lldp_file);
244 unlink_and_free(link->state_file);
c166a070 245
8de56fb3 246 sd_device_unref(link->dev);
9aa2585e 247 netdev_unref(link->netdev);
b5db00e5 248
0d4ad91d 249 hashmap_free(link->bound_to_links);
0d4ad91d
AR
250 hashmap_free(link->bound_by_links);
251
5f707e12 252 set_free_with_destructor(link->slaves, link_unref);
033295c1 253
c9c908a6
YW
254 network_unref(link->network);
255
6706ce2f
YW
256 sd_event_source_disable_unref(link->carrier_lost_timer);
257
8301aa0b 258 return mfree(link);
14b746f7
TG
259}
260
8301aa0b 261DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
14b746f7 262
6eab614d 263int link_get_by_index(Manager *m, int ifindex, Link **ret) {
11a7f229 264 Link *link;
11a7f229
TG
265
266 assert(m);
c2aed128
YW
267
268 if (ifindex <= 0)
269 return -EINVAL;
11a7f229 270
6eab614d 271 link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
11a7f229
TG
272 if (!link)
273 return -ENODEV;
274
0b54c870
YW
275 if (ret)
276 *ret = link;
277 return 0;
278}
279
280int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
281 Link *link;
282
283 assert(m);
284 assert(ifname);
11a7f229 285
0b54c870
YW
286 link = hashmap_get(m->links_by_name, ifname);
287 if (!link)
288 return -ENODEV;
289
290 if (ret)
291 *ret = link;
11a7f229
TG
292 return 0;
293}
294
fe321d45
YW
295int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
296 Link *link;
297
298 assert(m);
299 assert(hw_addr);
300
301 link = hashmap_get(m->links_by_hw_addr, hw_addr);
302 if (!link)
303 return -ENODEV;
304
305 if (ret)
306 *ret = link;
307 return 0;
308}
309
81357285
YW
310int link_get_master(Link *link, Link **ret) {
311 assert(link);
312 assert(link->manager);
313 assert(ret);
314
315 if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
316 return -ENODEV;
317
6eab614d 318 return link_get_by_index(link->manager, link->master_ifindex, ret);
81357285
YW
319}
320
af9ba57a 321void link_set_state(Link *link, LinkState state) {
e331e246
TG
322 assert(link);
323
324 if (link->state == state)
325 return;
326
0beb9542
YW
327 log_link_debug(link, "State changed: %s -> %s",
328 link_state_to_string(link->state),
329 link_state_to_string(state));
330
e331e246
TG
331 link->state = state;
332
333 link_send_changed(link, "AdministrativeState", NULL);
9092113d 334 link_dirty(link);
e331e246
TG
335}
336
2a99eed0 337int link_stop_engines(Link *link, bool may_keep_dhcp) {
372acaad 338 int r, ret = 0;
111bb8f9
TG
339
340 assert(link);
341 assert(link->manager);
342 assert(link->manager->event);
343
80060352
ZJS
344 bool keep_dhcp = may_keep_dhcp &&
345 link->network &&
76a86ffd 346 !link->network->dhcp_send_decline && /* IPv4 ACD for the DHCPv4 address is running. */
80060352
ZJS
347 (link->manager->restarting ||
348 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
349
84add3cd 350 if (!keep_dhcp) {
372acaad
YW
351 r = sd_dhcp_client_stop(link->dhcp_client);
352 if (r < 0)
353 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
111bb8f9
TG
354 }
355
372acaad
YW
356 r = sd_dhcp_server_stop(link->dhcp_server);
357 if (r < 0)
358 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
2a99eed0 359
372acaad
YW
360 r = sd_lldp_rx_stop(link->lldp_rx);
361 if (r < 0)
362 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
c01b9b87 363
372acaad
YW
364 r = sd_lldp_tx_stop(link->lldp_tx);
365 if (r < 0)
366 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
9cc65242 367
372acaad
YW
368 r = sd_ipv4ll_stop(link->ipv4ll);
369 if (r < 0)
370 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
dd43110f 371
372acaad
YW
372 r = ipv4acd_stop(link);
373 if (r < 0)
374 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
051e77ca 375
372acaad
YW
376 r = sd_dhcp6_client_stop(link->dhcp6_client);
377 if (r < 0)
378 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
4138fb2c 379
372acaad
YW
380 r = dhcp_pd_remove(link, /* only_marked = */ false);
381 if (r < 0)
382 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
1633c457 383
372acaad
YW
384 r = ndisc_stop(link);
385 if (r < 0)
386 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
4138fb2c 387
259c65f3
YW
388 ndisc_flush(link);
389
372acaad
YW
390 r = sd_radv_stop(link->radv);
391 if (r < 0)
392 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
7465dd22 393
372acaad 394 return ret;
111bb8f9
TG
395}
396
b22d8a00 397void link_enter_failed(Link *link) {
c2eb7753
YW
398 int r;
399
ef1ba606 400 assert(link);
f882c247 401
370e9930 402 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2139694e
TG
403 return;
404
6a7a4e4d 405 log_link_warning(link, "Failed");
449f7554 406
e331e246 407 link_set_state(link, LINK_STATE_FAILED);
fe8db0c5 408
c2eb7753
YW
409 if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
410 log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
411 goto stop;
412 }
413
414 log_link_info(link, "Trying to reconfigure the interface.");
415 r = link_reconfigure(link, /* force = */ true);
416 if (r < 0) {
417 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
418 goto stop;
419 }
420
421 return;
422
423stop:
424 (void) link_stop_engines(link, /* may_keep_dhcp = */ false);
f882c247
TG
425}
426
6accfd31
DA
427void link_check_ready(Link *link) {
428 Address *a;
6accfd31
DA
429
430 assert(link);
431
5f58af25
YW
432 if (link->state == LINK_STATE_CONFIGURED)
433 return;
434
d19b9939
YW
435 if (link->state != LINK_STATE_CONFIGURING)
436 return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
6accfd31
DA
437
438 if (!link->network)
a7f07cbe 439 return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
6accfd31 440
7558f9e7
YW
441 if (!link->tc_configured)
442 return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
600b7898 443
a7f07cbe
YW
444 if (link->set_link_messages > 0)
445 return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
446
447 if (!link->activated)
448 return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
449
24b9c341 450 if (link->iftype == ARPHRD_CAN)
7558f9e7 451 /* let's shortcut things for CAN which doesn't need most of checks below. */
24b9c341 452 goto ready;
7558f9e7 453
879377ca
YW
454 if (!link->stacked_netdevs_created)
455 return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
456
76c5a0f2 457 if (!link->static_addresses_configured)
d19b9939 458 return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
6accfd31 459
354bc760
YW
460 if (!link->static_address_labels_configured)
461 return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
462
e5b35bf6
YW
463 if (!link->static_bridge_fdb_configured)
464 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
465
ff9e0783
YW
466 if (!link->static_bridge_mdb_configured)
467 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
468
fdeba3f5
YW
469 if (!link->static_ipv6_proxy_ndp_configured)
470 return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
471
40ca350e
YW
472 if (!link->static_neighbors_configured)
473 return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
474
d19b9939
YW
475 if (!link->static_nexthops_configured)
476 return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
c16c7808 477
76c5a0f2
YW
478 if (!link->static_routes_configured)
479 return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
480
0e5ef6be 481 if (!link->static_routing_policy_rules_configured)
d19b9939 482 return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
6accfd31 483
d19b9939
YW
484 if (!link->sr_iov_configured)
485 return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
518cd6b5 486
4bcb8625
YW
487 /* IPv6LL is assigned after the link gains its carrier. */
488 if (!link->network->configure_without_carrier &&
489 link_ipv6ll_enabled(link) &&
490 !in6_addr_is_set(&link->ipv6ll_address))
491 return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
492
9e7d91ed
YW
493 /* All static addresses must be ready. */
494 bool has_static_address = false;
495 SET_FOREACH(a, link->addresses) {
496 if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
497 continue;
498 if (!address_is_ready(a))
499 return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
500 IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
501 has_static_address = true;
502 }
503
504 /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
505 if (has_static_address)
506 goto ready;
507
5f950e56
YW
508 /* If no dynamic addressing protocol enabled, assume the interface is ready.
509 * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
510 if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
511 !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
512 (link->network->configure_without_carrier || !link_ipv6_accept_ra_enabled(link)))
513 goto ready;
514
515 bool ipv4ll_ready =
516 link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
517 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
518 bool dhcp4_ready =
519 link_dhcp4_enabled(link) && link->dhcp4_configured &&
520 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
521 bool dhcp6_ready =
522 link_dhcp6_enabled(link) && link->dhcp6_configured &&
523 (!link->network->dhcp6_use_address ||
524 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
525 bool dhcp_pd_ready =
526 link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
527 (!link->network->dhcp_pd_assign ||
528 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
529 bool ndisc_ready =
530 link_ipv6_accept_ra_enabled(link) && link->ndisc_configured &&
531 (!link->network->ipv6_accept_ra_use_autonomous_prefix ||
532 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
533
195b83ed
YW
534 /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
535 if (dhcp_pd_is_uplink(link, link, /* accept_auto = */ false)) {
536 if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
0ad8d953 537 sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
195b83ed
YW
538 if (!dhcp4_ready)
539 return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
540 if (!dhcp_pd_ready)
541 return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
542 }
543
544 if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
fb70992d 545 sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
195b83ed
YW
546 if (!dhcp6_ready)
547 return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
548 if (!dhcp_pd_ready)
549 return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
550 }
551 }
552
5f950e56
YW
553 /* At least one dynamic addressing protocol is finished. */
554 if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
555 return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
556
557 log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
558 __func__,
559 yes_no(ipv4ll_ready),
560 yes_no(dhcp4_ready),
561 yes_no(dhcp6_ready),
562 yes_no(dhcp_pd_ready),
563 yes_no(ndisc_ready));
564
565ready:
29836c16 566 link_set_state(link, LINK_STATE_CONFIGURED);
6accfd31
DA
567}
568
97108953 569static int link_request_static_configs(Link *link) {
f882c247
TG
570 int r;
571
572 assert(link);
573 assert(link->network);
f5be5601 574 assert(link->state != _LINK_STATE_INVALID);
f882c247 575
354bc760 576 r = link_request_static_addresses(link);
0e5ef6be
YW
577 if (r < 0)
578 return r;
579
354bc760 580 r = link_request_static_address_labels(link);
f3ef324d
YW
581 if (r < 0)
582 return r;
e4a71bf3 583
e5b35bf6
YW
584 r = link_request_static_bridge_fdb(link);
585 if (r < 0)
586 return r;
587
9a038aac
YW
588 r = link_request_static_bridge_mdb(link);
589 if (r < 0)
590 return r;
591
fdeba3f5
YW
592 r = link_request_static_ipv6_proxy_ndp_addresses(link);
593 if (r < 0)
594 return r;
595
40ca350e 596 r = link_request_static_neighbors(link);
682c65b0
YW
597 if (r < 0)
598 return r;
bd6379ec 599
76c5a0f2 600 r = link_request_static_nexthops(link, false);
fe2bc17c
YW
601 if (r < 0)
602 return r;
f882c247 603
76c5a0f2
YW
604 r = link_request_static_routes(link, false);
605 if (r < 0)
606 return r;
607
608 r = link_request_static_routing_policy_rules(link);
5ae0fb7f
YW
609 if (r < 0)
610 return r;
d4cdbea5 611
f882c247
TG
612 return 0;
613}
614
1187fc33
YW
615static int link_request_stacked_netdevs(Link *link) {
616 NetDev *netdev;
e16e4b3b
DS
617 int r;
618
619 assert(link);
620
1187fc33 621 link->stacked_netdevs_created = false;
e16e4b3b 622
1187fc33 623 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
b14686ff 624 r = link_request_stacked_netdev(link, netdev);
1187fc33
YW
625 if (r < 0)
626 return r;
627 }
e16e4b3b 628
bb916f35 629 if (link->create_stacked_netdev_messages == 0) {
1187fc33 630 link->stacked_netdevs_created = true;
bb916f35
YW
631 link_check_ready(link);
632 }
e16e4b3b
DS
633
634 return 0;
635}
636
8566df79 637static int link_acquire_dynamic_ipv6_conf(Link *link) {
e7ab854c
TG
638 int r;
639
640 assert(link);
641
0f96a823
YW
642 r = radv_start(link);
643 if (r < 0)
644 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
7465dd22 645
294f129b
YW
646 r = ndisc_start(link);
647 if (r < 0)
648 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
cd305af1 649
294f129b
YW
650 r = dhcp6_start(link);
651 if (r < 0)
652 return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
cd305af1 653
e7ab854c
TG
654 return 0;
655}
656
8566df79 657static int link_acquire_dynamic_ipv4_conf(Link *link) {
ff254138
TG
658 int r;
659
660 assert(link);
ff254138
TG
661 assert(link->manager);
662 assert(link->manager->event);
663
0107b769 664 if (link->dhcp_client) {
294f129b 665 r = dhcp4_start(link);
0107b769 666 if (r < 0)
294f129b 667 return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
0107b769 668
ccffa166 669 log_link_debug(link, "Acquiring DHCPv4 lease.");
5c1d3fc9 670
ccffa166 671 } else if (link->ipv4ll) {
59c27231
AK
672 if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
673 r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
674 if (r < 0)
675 return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
676 }
677
5c1d3fc9 678 r = sd_ipv4ll_start(link->ipv4ll);
6a7a4e4d
LP
679 if (r < 0)
680 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
ccffa166
YW
681
682 log_link_debug(link, "Acquiring IPv4 link-local address.");
5c1d3fc9
UTL
683 }
684
ab486ef4
YW
685 if (link->dhcp_server) {
686 r = sd_dhcp_server_start(link->dhcp_server);
687 if (r < 0)
688 return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
689 }
690
76a86ffd
YW
691 r = ipv4acd_start(link);
692 if (r < 0)
693 return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
694
6fc25497
SS
695 return 0;
696}
697
8566df79 698static int link_acquire_dynamic_conf(Link *link) {
6fc25497
SS
699 int r;
700
701 assert(link);
0f96a823 702 assert(link->network);
6fc25497 703
8566df79 704 r = link_acquire_dynamic_ipv4_conf(link);
6fc25497
SS
705 if (r < 0)
706 return r;
707
94876904 708 if (in6_addr_is_set(&link->ipv6ll_address)) {
8566df79 709 r = link_acquire_dynamic_ipv6_conf(link);
6fc25497
SS
710 if (r < 0)
711 return r;
712 }
713
a27588d4 714 if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
0f96a823
YW
715 /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
716 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
717 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
718 * below will be called in radv_start(). */
a27588d4 719 r = dhcp_request_prefix_delegation(link);
0f96a823 720 if (r < 0)
a27588d4 721 return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
0f96a823
YW
722 }
723
c01b9b87
YW
724 if (link->lldp_tx) {
725 r = sd_lldp_tx_start(link->lldp_tx);
726 if (r < 0)
727 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
728 }
8e1ad1ea 729
35778343
YW
730 if (link->lldp_rx) {
731 r = sd_lldp_rx_start(link->lldp_rx);
f8549910
YW
732 if (r < 0)
733 return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
734 }
735
ff254138
TG
736 return 0;
737}
738
7657ec3e 739int link_ipv6ll_gained(Link *link) {
0c9ee5d5 740 int r;
deb2e523 741
0c9ee5d5 742 assert(link);
deb2e523 743
0c9ee5d5 744 log_link_info(link, "Gained IPv6LL");
deb2e523 745
7657ec3e
YW
746 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
747 return 0;
0c9ee5d5 748
7657ec3e
YW
749 r = link_acquire_dynamic_ipv6_conf(link);
750 if (r < 0)
751 return r;
0c9ee5d5 752
7657ec3e 753 link_check_ready(link);
0c9ee5d5 754 return 0;
deb2e523
TG
755}
756
112a0972 757int link_handle_bound_to_list(Link *link) {
0d4ad91d
AR
758 bool required_up = false;
759 bool link_is_up = false;
852a3916 760 Link *l;
0d4ad91d
AR
761
762 assert(link);
763
ad82f0c3
YW
764 /* If at least one interface in bound_to_links has carrier, then make this interface up.
765 * If all interfaces in bound_to_links do not, then make this interface down. */
766
0d4ad91d
AR
767 if (hashmap_isempty(link->bound_to_links))
768 return 0;
769
770 if (link->flags & IFF_UP)
771 link_is_up = true;
772
852a3916 773 HASHMAP_FOREACH(l, link->bound_to_links)
0d4ad91d
AR
774 if (link_has_carrier(l)) {
775 required_up = true;
776 break;
777 }
778
852a3916 779 if (!required_up && link_is_up)
68f52063 780 return link_request_to_bring_up_or_down(link, /* up = */ false);
852a3916 781 if (required_up && !link_is_up)
68f52063 782 return link_request_to_bring_up_or_down(link, /* up = */ true);
0d4ad91d
AR
783
784 return 0;
785}
786
787static int link_handle_bound_by_list(Link *link) {
0d4ad91d
AR
788 Link *l;
789 int r;
790
791 assert(link);
792
ad82f0c3
YW
793 /* Update up or down state of interfaces which depend on this interface's carrier state. */
794
0d4ad91d
AR
795 if (hashmap_isempty(link->bound_by_links))
796 return 0;
797
852a3916 798 HASHMAP_FOREACH(l, link->bound_by_links) {
0d4ad91d
AR
799 r = link_handle_bound_to_list(l);
800 if (r < 0)
801 return r;
802 }
803
804 return 0;
805}
806
807static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
808 int r;
809
810 assert(link);
811 assert(carrier);
812
813 if (link == carrier)
814 return 0;
815
816 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
817 return 0;
818
190b3b5c 819 r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
0d4ad91d
AR
820 if (r < 0)
821 return r;
822
9092113d
YW
823 link_dirty(link);
824
0d4ad91d
AR
825 return 0;
826}
827
828static int link_new_bound_by_list(Link *link) {
829 Manager *m;
830 Link *carrier;
0d4ad91d 831 int r;
0d4ad91d
AR
832
833 assert(link);
834 assert(link->manager);
835
836 m = link->manager;
837
6eab614d 838 HASHMAP_FOREACH(carrier, m->links_by_index) {
0d4ad91d
AR
839 if (!carrier->network)
840 continue;
841
842 if (strv_isempty(carrier->network->bind_carrier))
843 continue;
844
191a3f16 845 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
0d4ad91d
AR
846 r = link_put_carrier(link, carrier, &link->bound_by_links);
847 if (r < 0)
848 return r;
0d4ad91d
AR
849 }
850 }
851
90e74a66 852 HASHMAP_FOREACH(carrier, link->bound_by_links) {
0d4ad91d
AR
853 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
854 if (r < 0)
855 return r;
0d4ad91d
AR
856 }
857
858 return 0;
859}
860
861static int link_new_bound_to_list(Link *link) {
862 Manager *m;
863 Link *carrier;
0d4ad91d 864 int r;
0d4ad91d
AR
865
866 assert(link);
867 assert(link->manager);
868
869 if (!link->network)
870 return 0;
871
872 if (strv_isempty(link->network->bind_carrier))
873 return 0;
874
875 m = link->manager;
876
6eab614d 877 HASHMAP_FOREACH(carrier, m->links_by_index) {
191a3f16 878 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
0d4ad91d
AR
879 r = link_put_carrier(link, carrier, &link->bound_to_links);
880 if (r < 0)
881 return r;
0d4ad91d
AR
882 }
883 }
884
852a3916 885 HASHMAP_FOREACH(carrier, link->bound_to_links) {
0d4ad91d
AR
886 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
887 if (r < 0)
888 return r;
0d4ad91d
AR
889 }
890
891 return 0;
892}
893
0d4ad91d 894static void link_free_bound_to_list(Link *link) {
9092113d 895 bool updated = false;
0d4ad91d 896 Link *bound_to;
0d4ad91d 897
9092113d
YW
898 assert(link);
899
900 while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
901 updated = true;
0d4ad91d
AR
902
903 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
84de38c5 904 link_dirty(bound_to);
0d4ad91d
AR
905 }
906
9092113d
YW
907 if (updated)
908 link_dirty(link);
0d4ad91d
AR
909}
910
911static void link_free_bound_by_list(Link *link) {
9092113d 912 bool updated = false;
0d4ad91d 913 Link *bound_by;
0d4ad91d 914
9092113d
YW
915 assert(link);
916
917 while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
918 updated = true;
0d4ad91d
AR
919
920 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
84de38c5 921 link_dirty(bound_by);
0d4ad91d
AR
922 link_handle_bound_to_list(bound_by);
923 }
924 }
925
9092113d
YW
926 if (updated)
927 link_dirty(link);
0d4ad91d
AR
928}
929
81357285 930static int link_append_to_master(Link *link) {
5f707e12
YW
931 Link *master;
932 int r;
933
934 assert(link);
5f707e12 935
81357285
YW
936 /* - The link may have no master.
937 * - RTM_NEWLINK message about master interface may not be received yet. */
938 if (link_get_master(link, &master) < 0)
939 return 0;
5f707e12 940
de7fef4b 941 r = set_ensure_put(&master->slaves, NULL, link);
38288f0b 942 if (r <= 0)
5f707e12
YW
943 return r;
944
945 link_ref(link);
946 return 0;
947}
948
81357285 949static void link_drop_from_master(Link *link) {
5f707e12
YW
950 Link *master;
951
952 assert(link);
953
81357285 954 if (!link->manager)
5f707e12
YW
955 return;
956
81357285 957 if (link_get_master(link, &master) < 0)
5f707e12
YW
958 return;
959
960 link_unref(set_remove(master->slaves, link));
961}
962
4303e980 963static int link_drop_requests(Link *link) {
56001f02 964 Request *req;
4303e980 965 int ret = 0;
56001f02
YW
966
967 assert(link);
968 assert(link->manager);
969
4303e980
YW
970 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
971 if (req->link != link)
972 continue;
973
974 /* If the request is already called, but its reply is not received, then we need to
975 * drop the configuration (e.g. address) here. Note, if the configuration is known,
976 * it will be handled later by link_drop_foreign_addresses() or so. */
977 if (req->waiting_reply && link->state != LINK_STATE_LINGER)
978 switch (req->type) {
979 case REQUEST_TYPE_ADDRESS: {
980 Address *address = ASSERT_PTR(req->userdata);
981
982 if (address_get(link, address, NULL) < 0)
983 RET_GATHER(ret, address_remove(address, link));
984 break;
985 }
c902fa08
YW
986 case REQUEST_TYPE_NEIGHBOR: {
987 Neighbor *neighbor = ASSERT_PTR(req->userdata);
988
989 if (neighbor_get(link, neighbor, NULL) < 0)
990 RET_GATHER(ret, neighbor_remove(neighbor, link));
991 break;
992 }
9a988d16
YW
993 case REQUEST_TYPE_NEXTHOP: {
994 NextHop *nexthop = ASSERT_PTR(req->userdata);
995
996 if (nexthop_get_by_id(link->manager, nexthop->id, NULL) < 0)
997 RET_GATHER(ret, nexthop_remove(nexthop, link->manager));
998 break;
999 }
4303e980
YW
1000 default:
1001 ;
1002 }
1003
b9b5096e 1004 request_detach(req);
4303e980
YW
1005 }
1006
1007 return ret;
56001f02
YW
1008}
1009
cc2d7efc 1010static Link *link_drop(Link *link) {
63130eb3 1011 if (!link)
cc2d7efc 1012 return NULL;
0d4ad91d 1013
63130eb3
YW
1014 assert(link->manager);
1015
0d4ad91d
AR
1016 link_set_state(link, LINK_STATE_LINGER);
1017
63130eb3
YW
1018 /* Drop all references from other links and manager. Note that async netlink calls may have
1019 * references to the link, and they will be dropped when we receive replies. */
1020
4303e980 1021 (void) link_drop_requests(link);
56001f02 1022
bb193d2d
YW
1023 link_free_bound_to_list(link);
1024 link_free_bound_by_list(link);
0d4ad91d 1025
b0ff6dea
YW
1026 link_clear_sr_iov_ifindices(link);
1027
81357285 1028 link_drop_from_master(link);
5f707e12 1029
4c78dc17
YW
1030 if (link->state_file)
1031 (void) unlink(link->state_file);
1032
63130eb3
YW
1033 link_clean(link);
1034
0b54c870
YW
1035 STRV_FOREACH(n, link->alternative_names)
1036 hashmap_remove(link->manager->links_by_name, *n);
0b54c870
YW
1037 hashmap_remove(link->manager->links_by_name, link->ifname);
1038
fe321d45 1039 /* bonding master and its slaves have the same hardware address. */
e74f1d07 1040 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
fe321d45 1041
63130eb3 1042 /* The following must be called at last. */
6eab614d 1043 assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
cc2d7efc 1044 return link_unref(link);
0d4ad91d
AR
1045}
1046
5e5b137a 1047static int link_drop_foreign_config(Link *link) {
1339b950 1048 int r;
0b81225e
YW
1049
1050 assert(link);
1051 assert(link->manager);
5e5b137a 1052
5e3bb5da
YW
1053 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
1054 * to remove loopback address or addresses used for root NFS. */
1055
1056 if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
1057 return 0;
1058 if (FLAGS_SET(link->flags, IFF_LOOPBACK))
1059 return 0;
1060 if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
1061 return 0;
1062
e36d601c 1063 r = link_drop_foreign_routes(link);
5e5b137a 1064
1339b950
ZJS
1065 RET_GATHER(r, link_drop_foreign_nexthops(link));
1066 RET_GATHER(r, link_drop_foreign_addresses(link));
1067 RET_GATHER(r, link_drop_foreign_neighbors(link));
1068 RET_GATHER(r, manager_drop_foreign_routing_policy_rules(link->manager));
0b81225e
YW
1069
1070 return r;
5e5b137a
TG
1071}
1072
a0e99a37 1073static int link_drop_managed_config(Link *link) {
1339b950 1074 int r;
0b81225e
YW
1075
1076 assert(link);
1077 assert(link->manager);
3104883d 1078
a0e99a37 1079 r = link_drop_managed_routes(link);
3104883d 1080
1339b950
ZJS
1081 RET_GATHER(r, link_drop_managed_nexthops(link));
1082 RET_GATHER(r, link_drop_managed_addresses(link));
1083 RET_GATHER(r, link_drop_managed_neighbors(link));
1084 RET_GATHER(r, link_drop_managed_routing_policy_rules(link));
3104883d 1085
0b81225e 1086 return r;
3104883d
SS
1087}
1088
b4564f4e
YW
1089static void link_foreignize_config(Link *link) {
1090 assert(link);
1091 assert(link->manager);
1092
1093 link_foreignize_routes(link);
1094 link_foreignize_nexthops(link);
1095 link_foreignize_addresses(link);
1096 link_foreignize_neighbors(link);
1097 link_foreignize_routing_policy_rules(link);
1098}
1099
c3b94251 1100static int link_configure(Link *link) {
4ecdcb07
YW
1101 int r;
1102
ef1ba606 1103 assert(link);
b22d8a00 1104 assert(link->network);
bd08ce56 1105 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 1106
1187fc33
YW
1107 link_set_state(link, LINK_STATE_CONFIGURING);
1108
3cf58ef3
YW
1109 r = link_new_bound_to_list(link);
1110 if (r < 0)
1111 return r;
1112
1dec9d81 1113 r = link_request_traffic_control(link);
4ecdcb07
YW
1114 if (r < 0)
1115 return r;
1116
941f8e13
YW
1117 r = link_configure_mtu(link);
1118 if (r < 0)
1119 return r;
1120
7558f9e7
YW
1121 if (link->iftype == ARPHRD_CAN) {
1122 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1123 r = link_request_to_set_can(link);
1124 if (r < 0)
1125 return r;
1126
1127 return link_request_to_activate(link);
1128 }
1129
cb8453cc 1130 r = link_request_sr_iov_vfs(link);
518cd6b5
SS
1131 if (r < 0)
1132 return r;
1133
5e0534f1 1134 r = link_set_sysctl(link);
b69c3180
SS
1135 if (r < 0)
1136 return r;
8749cbcd 1137
d05c332c 1138 r = link_request_to_set_mac(link, /* allow_retry = */ true);
e16e4b3b
DS
1139 if (r < 0)
1140 return r;
1141
72e65e6f
YW
1142 r = link_request_to_set_ipoib(link);
1143 if (r < 0)
1144 return r;
1145
1187fc33 1146 r = link_request_to_set_flags(link);
e16e4b3b
DS
1147 if (r < 0)
1148 return r;
1149
1187fc33 1150 r = link_request_to_set_group(link);
99d2baa2
SS
1151 if (r < 0)
1152 return r;
1153
1187fc33
YW
1154 r = link_request_to_set_addrgen_mode(link);
1155 if (r < 0)
1156 return r;
1157
1158 r = link_request_to_set_master(link);
1159 if (r < 0)
1160 return r;
1161
1162 r = link_request_stacked_netdevs(link);
1163 if (r < 0)
1164 return r;
1165
1166 r = link_request_to_set_bond(link);
1167 if (r < 0)
1168 return r;
1169
1170 r = link_request_to_set_bridge(link);
1171 if (r < 0)
1172 return r;
1173
1174 r = link_request_to_set_bridge_vlan(link);
1175 if (r < 0)
1176 return r;
1177
1178 r = link_request_to_activate(link);
89fe6535
SS
1179 if (r < 0)
1180 return r;
1181
2ffd6d73
YW
1182 r = ipv4ll_configure(link);
1183 if (r < 0)
1184 return r;
64b21ece 1185
ccffa166 1186 r = link_request_dhcp4_client(link);
2ffd6d73
YW
1187 if (r < 0)
1188 return r;
eb34d4af 1189
ccffa166 1190 r = link_request_dhcp6_client(link);
2ffd6d73
YW
1191 if (r < 0)
1192 return r;
f5a8c43f 1193
ba4c7184 1194 r = link_request_ndisc(link);
2ffd6d73
YW
1195 if (r < 0)
1196 return r;
4138fb2c 1197
1d28a3cf
YW
1198 r = link_request_dhcp_server(link);
1199 if (r < 0)
1200 return r;
1201
a254fab2 1202 r = link_request_radv(link);
086b8853
YW
1203 if (r < 0)
1204 return r;
7465dd22 1205
2ffd6d73
YW
1206 r = link_lldp_rx_configure(link);
1207 if (r < 0)
1208 return r;
ce43e484 1209
c01b9b87
YW
1210 r = link_lldp_tx_configure(link);
1211 if (r < 0)
1212 return r;
1213
5e3bb5da
YW
1214 r = link_drop_foreign_config(link);
1215 if (r < 0)
1216 return r;
4c649652 1217
1187fc33
YW
1218 r = link_request_static_configs(link);
1219 if (r < 0)
1220 return r;
1221
1222 if (!link_has_carrier(link))
1223 return 0;
1224
1225 return link_acquire_dynamic_conf(link);
505f8da7
TG
1226}
1227
170e88c8
YW
1228static int link_get_network(Link *link, Network **ret) {
1229 Network *network;
1a3caa49 1230 int r;
170e88c8
YW
1231
1232 assert(link);
1233 assert(link->manager);
1234 assert(ret);
1235
1236 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1237 bool warn = false;
1238
1a3caa49 1239 r = net_match_config(
170e88c8 1240 &network->match,
8de56fb3 1241 link->dev,
52135071
YW
1242 &link->hw_addr,
1243 &link->permanent_hw_addr,
170e88c8
YW
1244 link->driver,
1245 link->iftype,
65022cd7 1246 link->kind,
170e88c8
YW
1247 link->ifname,
1248 link->alternative_names,
1249 link->wlan_iftype,
1250 link->ssid,
1a3caa49
YW
1251 &link->bssid);
1252 if (r < 0)
1253 return r;
1254 if (r == 0)
170e88c8
YW
1255 continue;
1256
8de56fb3 1257 if (network->match.ifname && link->dev) {
170e88c8
YW
1258 uint8_t name_assign_type = NET_NAME_UNKNOWN;
1259 const char *attr;
1260
8de56fb3 1261 if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
170e88c8
YW
1262 (void) safe_atou8(attr, &name_assign_type);
1263
1264 warn = name_assign_type == NET_NAME_ENUM;
1265 }
1266
1267 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1268 "found matching network '%s'%s.",
1269 network->filename,
1270 warn ? ", based on potentially unpredictable interface name" : "");
1271
1272 if (network->unmanaged)
1273 return -ENOENT;
1274
1275 *ret = network;
1276 return 0;
1277 }
1278
1279 return -ENOENT;
1280}
1281
8a4ad01a 1282int link_reconfigure_impl(Link *link, bool force) {
cb71e9c6 1283 Network *network = NULL;
9aa2585e 1284 NetDev *netdev = NULL;
ad932b15
YW
1285 int r;
1286
5a1860f7 1287 assert(link);
572b21d9 1288
4faca0a3 1289 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
fb961119
YW
1290 return 0;
1291
9aa2585e
YW
1292 r = netdev_get(link->manager, link->ifname, &netdev);
1293 if (r < 0 && r != -ENOENT)
1294 return r;
1295
170e88c8 1296 r = link_get_network(link, &network);
cb71e9c6 1297 if (r < 0 && r != -ENOENT)
ad932b15
YW
1298 return r;
1299
fb961119
YW
1300 if (link->state != LINK_STATE_UNMANAGED && !network)
1301 /* If link is in initialized state, then link->network is also NULL. */
1302 force = true;
1303
99b8517c 1304 if (link->network == network && !force)
ad932b15
YW
1305 return 0;
1306
fb961119 1307 if (network) {
b993e7e7
YW
1308 _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1309
fb961119 1310 if (link->state == LINK_STATE_INITIALIZED)
b993e7e7
YW
1311 log_link_info(link, "Configuring with %s%s%s%s.",
1312 network->filename,
1313 isempty(joined) ? "" : " (dropins: ",
1314 joined,
1315 isempty(joined) ? "" : ")");
fb961119 1316 else
b993e7e7
YW
1317 log_link_info(link, "Reconfiguring with %s%s%s%s.",
1318 network->filename,
1319 isempty(joined) ? "" : " (dropins: ",
1320 joined,
1321 isempty(joined) ? "" : ")");
fb961119
YW
1322 } else
1323 log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1324 "Unmanaging interface.");
ad932b15
YW
1325
1326 /* Dropping old .network file */
2a99eed0 1327 r = link_stop_engines(link, false);
572b21d9 1328 if (r < 0)
ad932b15 1329 return r;
ad932b15 1330
4303e980
YW
1331 r = link_drop_requests(link);
1332 if (r < 0)
1333 return r;
19d9a5ad 1334
81db4f3d 1335 if (network && !force && network->keep_configuration != KEEP_CONFIGURATION_YES)
b4564f4e
YW
1336 /* When a new/updated .network file is assigned, first make all configs (addresses,
1337 * routes, and so on) foreign, and then drop unnecessary configs later by
81db4f3d
YW
1338 * link_drop_foreign_config() in link_configure().
1339 * Note, when KeepConfiguration=yes, link_drop_foreign_config() does nothing. Hence,
1340 * here we need to drop the configs such as addresses, routes, and so on configured by
1341 * the previously assigned .network file. */
b4564f4e
YW
1342 link_foreignize_config(link);
1343 else {
a0e99a37
YW
1344 /* Remove all managed configs. Note, foreign configs are removed in later by
1345 * link_configure() -> link_drop_foreign_config() if the link is managed by us. */
1346 r = link_drop_managed_config(link);
b4564f4e
YW
1347 if (r < 0)
1348 return r;
1349 }
ad932b15 1350
3cf58ef3
YW
1351 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1352 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1353 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1354 * map here, as it depends on .network files assigned to other links. */
1355 link_free_bound_to_list(link);
1356
ad932b15
YW
1357 link_free_engines(link);
1358 link->network = network_unref(link->network);
1359
9aa2585e
YW
1360 netdev_unref(link->netdev);
1361 link->netdev = netdev_ref(netdev);
1362
cb71e9c6
YW
1363 if (!network) {
1364 link_set_state(link, LINK_STATE_UNMANAGED);
1365 return 0;
1366 }
1367
ad932b15 1368 /* Then, apply new .network file */
9092113d 1369 link->network = network_ref(network);
b156a95d 1370 link_update_operstate(link, true);
9092113d 1371 link_dirty(link);
ad932b15 1372
ad932b15 1373 link_set_state(link, LINK_STATE_INITIALIZED);
61135582 1374 link->activated = false;
ad932b15 1375
ad932b15
YW
1376 r = link_configure(link);
1377 if (r < 0)
1378 return r;
1379
0e397560 1380 return 1;
ad932b15
YW
1381}
1382
96f5f9ef 1383static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force) {
572b21d9
YW
1384 int r;
1385
d09a179e
YW
1386 assert(link);
1387
5a1860f7
YW
1388 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1389 if (r <= 0)
1390 return r;
1391
7f80fa12 1392 r = link_reconfigure_impl(link, force);
d09a179e 1393 if (r < 0) {
572b21d9 1394 link_enter_failed(link);
d09a179e
YW
1395 return 0;
1396 }
572b21d9 1397
d09a179e 1398 return r;
572b21d9
YW
1399}
1400
5a1860f7 1401static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1402 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
5a1860f7 1403}
572b21d9 1404
5a1860f7 1405static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1406 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ true);
572b21d9
YW
1407}
1408
a39a9ac8 1409int link_reconfigure(Link *link, bool force) {
d09a179e
YW
1410 int r;
1411
1412 assert(link);
d09a179e 1413
8bceafa7 1414 /* When link in pending or initialized state, then link_configure() will be called. To prevent
f8dd4077
ZJS
1415 * the function from being called multiple times simultaneously, refuse to reconfigure the
1416 * interface in these cases. */
8bceafa7 1417 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
f8dd4077 1418 return 0; /* 0 means no-op. */
2c0d7ed3 1419
a39a9ac8 1420 r = link_call_getlink(link, force ? link_force_reconfigure_handler : link_reconfigure_handler);
572b21d9
YW
1421 if (r < 0)
1422 return r;
1423
8bceafa7 1424 return 1; /* 1 means the interface will be reconfigured. */
572b21d9
YW
1425}
1426
e6bf7774 1427static int link_initialized_and_synced(Link *link) {
505f8da7
TG
1428 int r;
1429
1430 assert(link);
505f8da7
TG
1431 assert(link->manager);
1432
faa2e64f
YW
1433 if (link->manager->test_mode) {
1434 log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1435 link_set_state(link, LINK_STATE_UNMANAGED);
1436 return 0;
1437 }
1438
40695457
YW
1439 if (link->state == LINK_STATE_PENDING) {
1440 log_link_debug(link, "Link state is up-to-date");
1441 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 1442
40695457
YW
1443 r = link_new_bound_by_list(link);
1444 if (r < 0)
1445 return r;
0d4ad91d 1446
40695457
YW
1447 r = link_handle_bound_by_list(link);
1448 if (r < 0)
1449 return r;
1450 }
0d4ad91d 1451
fb961119 1452 return link_reconfigure_impl(link, /* force = */ false);
505f8da7
TG
1453}
1454
302a796f 1455static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4ff296b0
YW
1456 int r;
1457
5a1860f7
YW
1458 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1459 if (r <= 0)
0b54c870 1460 return r;
572b21d9 1461
4ff296b0
YW
1462 r = link_initialized_and_synced(link);
1463 if (r < 0)
1464 link_enter_failed(link);
5a1860f7
YW
1465
1466 return 0;
e6bf7774
YW
1467}
1468
3be9d62a 1469static int link_initialized(Link *link, sd_device *device) {
b0ff6dea
YW
1470 int r;
1471
4f561e8e 1472 assert(link);
4f561e8e
TG
1473 assert(device);
1474
160203e9
YW
1475 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1476 * or sysattrs) may be outdated. */
8de56fb3 1477 device_unref_and_replace(link->dev, device);
4f561e8e 1478
3b1dbdf0
YW
1479 if (link->dhcp_client) {
1480 r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
1481 if (r < 0)
1482 log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
1483 }
1484
1485 if (link->dhcp6_client) {
1486 r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
1487 if (r < 0)
1488 log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
1489 }
1490
b0ff6dea
YW
1491 r = link_set_sr_iov_ifindices(link);
1492 if (r < 0)
1493 log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
1494
40695457
YW
1495 if (link->state != LINK_STATE_PENDING)
1496 return link_reconfigure(link, /* force = */ false);
679b3605 1497
79008bdd 1498 log_link_debug(link, "udev initialized link");
4f561e8e 1499
3c9b8860
TG
1500 /* udev has initialized the link, but we don't know if we have yet
1501 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1502 * when it returns we know that the pending NEWLINKs have already been
1503 * processed and that we are up-to-date */
4f561e8e 1504
5a1860f7 1505 return link_call_getlink(link, link_initialized_handler);
cc2d7efc
YW
1506}
1507
0d411b7f 1508static int link_check_initialized(Link *link) {
51517f9e 1509 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
5a937ea2 1510 int r;
505f8da7 1511
0d411b7f 1512 assert(link);
505f8da7 1513
82f52245 1514 if (!udev_available())
0d411b7f 1515 return link_initialized_and_synced(link);
852a3916
YW
1516
1517 /* udev should be around */
0ac655a6 1518 r = sd_device_new_from_ifindex(&device, link->ifindex);
852a3916
YW
1519 if (r < 0) {
1520 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1521 return 0;
1522 }
1523
1524 r = sd_device_get_is_initialized(device);
0d411b7f
YW
1525 if (r < 0)
1526 return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
852a3916
YW
1527 if (r == 0) {
1528 /* not yet ready */
1529 log_link_debug(link, "link pending udev initialization...");
1530 return 0;
1531 }
1532
1533 r = device_is_renaming(device);
0d411b7f
YW
1534 if (r < 0)
1535 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
852a3916
YW
1536 if (r > 0) {
1537 log_link_debug(link, "Interface is being renamed, pending initialization.");
1538 return 0;
4f561e8e 1539 }
505f8da7 1540
0d411b7f 1541 return link_initialized(link, device);
a748b692
TG
1542}
1543
1fd3ac1e 1544int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
3be9d62a 1545 int r, ifindex;
ba87a61d 1546 const char *s;
1fd3ac1e 1547 Link *link;
3be9d62a
YW
1548
1549 assert(m);
1550 assert(device);
1551
3be9d62a 1552 r = sd_device_get_ifindex(device, &ifindex);
1fd3ac1e
YW
1553 if (r < 0)
1554 return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
3be9d62a 1555
75255a8d
YW
1556 r = link_get_by_index(m, ifindex, &link);
1557 if (r < 0) {
1558 /* This error is not critical, as the corresponding rtnl message may be received later. */
1559 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
1560 return 0;
1561 }
1562
1563 /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1564 * object here. It will be removed only when rtnetlink says so. */
1565 if (action == SD_DEVICE_REMOVE) {
1566 link->dev = sd_device_unref(link->dev);
1567 return 0;
1568 }
1569
3be9d62a 1570 r = device_is_renaming(device);
1fd3ac1e
YW
1571 if (r < 0)
1572 return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
3be9d62a 1573 if (r > 0) {
1fd3ac1e 1574 log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
71cdfe16
YW
1575 /* TODO:
1576 * What happens when a device is initialized, then soon renamed after that? When we detect
1577 * such, maybe we should cancel or postpone all queued requests for the interface. */
3be9d62a
YW
1578 return 0;
1579 }
1580
ba87a61d
YW
1581 r = sd_device_get_property_value(device, "ID_NET_MANAGED_BY", &s);
1582 if (r < 0 && r != -ENOENT)
1583 log_device_debug_errno(device, r, "Failed to get ID_NET_MANAGED_BY udev property, ignoring: %m");
1584 if (r >= 0 && !streq(s, "io.systemd.Network")) {
1585 log_device_debug(device, "Interface is requested to be managed by '%s', not managing the interface.", s);
1586 link_set_state(link, LINK_STATE_UNMANAGED);
1587 return 0;
1588 }
1589
4b9a8c2b
YW
1590 r = link_initialized(link, device);
1591 if (r < 0)
1592 link_enter_failed(link);
3be9d62a
YW
1593
1594 return 0;
1595}
1596
9c0a72f9 1597static int link_carrier_gained(Link *link) {
6706ce2f 1598 bool force_reconfigure;
9c0a72f9
TG
1599 int r;
1600
1601 assert(link);
1602
6706ce2f
YW
1603 r = event_source_disable(link->carrier_lost_timer);
1604 if (r < 0)
1605 log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
1606
d9910948
YW
1607 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1608 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1609 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1610 * restarted, and the correct network information will be gained.
1611 *
1612 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1613 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1614 * already assigned to the interface (in that case, the .network file does not have the SSID=
1615 * setting in the [Match] section), and the interface is already being configured. Of course,
1616 * there may exist another .network file with higher priority and a matching SSID= setting. But
1617 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1618 *
6706ce2f 1619 * For non-wireless interfaces, we have no way to detect the connected network change. So,
d9910948
YW
1620 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1621 force_reconfigure = link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid);
6706ce2f
YW
1622 link->previous_ssid = mfree(link->previous_ssid);
1623
986b2514
1624 /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1625 * profile wants to match on it with SSID= in its [Match] section.
1626 */
1627 if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1628 r = link_get_wlan_interface(link);
1629 if (r < 0)
1630 return r;
1631 }
1632
081466bd
YW
1633 /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1634 * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1635 * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1636 r = link_reconfigure_impl(link, force_reconfigure);
1637 if (r != 0)
1638 return r;
b7ac1284 1639
aa9117c0
YW
1640 r = link_handle_bound_by_list(link);
1641 if (r < 0)
1642 return r;
1643
600b7898
YW
1644 if (link->iftype == ARPHRD_CAN)
1645 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0 1646 return 0;
600b7898 1647
b9ea3d2e 1648 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
8566df79 1649 r = link_acquire_dynamic_conf(link);
f0269653 1650 if (r < 0)
9c0a72f9 1651 return r;
6fc25497 1652
97108953 1653 r = link_request_static_configs(link);
6fc25497
SS
1654 if (r < 0)
1655 return r;
9c0a72f9
TG
1656 }
1657
aa9117c0 1658 return 0;
9c0a72f9
TG
1659}
1660
6706ce2f
YW
1661static int link_carrier_lost_impl(Link *link) {
1662 int r, ret = 0;
1663
1664 assert(link);
1665
1666 link->previous_ssid = mfree(link->previous_ssid);
1667
1668 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1669 return 0;
1670
1671 if (!link->network)
1672 return 0;
1673
1674 r = link_stop_engines(link, false);
1675 if (r < 0)
1676 ret = r;
1677
a0e99a37 1678 r = link_drop_managed_config(link);
6706ce2f
YW
1679 if (r < 0 && ret >= 0)
1680 ret = r;
1681
1682 return ret;
1683}
1684
1685static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
99534007 1686 Link *link = ASSERT_PTR(userdata);
6706ce2f
YW
1687 int r;
1688
6706ce2f
YW
1689 r = link_carrier_lost_impl(link);
1690 if (r < 0) {
1691 log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
1692 link_enter_failed(link);
1693 }
1694
1695 return 0;
1696}
1697
9c0a72f9 1698static int link_carrier_lost(Link *link) {
b7326069
YW
1699 uint16_t dhcp_mtu;
1700 usec_t usec;
9c0a72f9
TG
1701 int r;
1702
1703 assert(link);
1704
aa9117c0
YW
1705 r = link_handle_bound_by_list(link);
1706 if (r < 0)
1707 return r;
93b4dab5 1708
600b7898
YW
1709 if (link->iftype == ARPHRD_CAN)
1710 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0
YW
1711 return 0;
1712
f1c22cf4
YW
1713 if (!link->network)
1714 return 0;
1715
b7326069
YW
1716 if (link->network->ignore_carrier_loss_set)
1717 /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1718 usec = link->network->ignore_carrier_loss_usec;
1719
1720 else if (link->network->bond && link->wlan_iftype > 0)
1721 /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1722 * carrier to be lost. See #19832. */
1723 usec = 3 * USEC_PER_SEC;
1724
1725 else if (link->network->dhcp_use_mtu &&
1726 link->dhcp_lease &&
1727 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
1728 dhcp_mtu != link->original_mtu)
1729 /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1730 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1731 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1732 usec = 5 * USEC_PER_SEC;
1733
1734 else
27438545 1735 /* Otherwise, use the implied default value. */
b7326069
YW
1736 usec = link->network->ignore_carrier_loss_usec;
1737
1738 if (usec == USEC_INFINITY)
aa9117c0 1739 return 0;
600b7898 1740
b7326069 1741 if (usec == 0)
6706ce2f
YW
1742 return link_carrier_lost_impl(link);
1743
1744 return event_reset_time_relative(link->manager->event,
1745 &link->carrier_lost_timer,
ba4e0427 1746 CLOCK_BOOTTIME,
b7326069 1747 usec,
6706ce2f
YW
1748 0,
1749 link_carrier_lost_handler,
1750 link,
1751 0,
1752 "link-carrier-loss",
1753 true);
0c9ee5d5
YW
1754}
1755
0c9ee5d5
YW
1756static int link_admin_state_up(Link *link) {
1757 int r;
1758
1759 assert(link);
1760
1761 /* This is called every time an interface admin state changes to up;
1762 * specifically, when IFF_UP flag changes from unset to set. */
1763
1764 if (!link->network)
1765 return 0;
1766
899b0e5e 1767 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
1a054a20 1768 log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
712fd5d2 1769 return link_request_to_bring_up_or_down(link, /* up = */ false);
0c9ee5d5
YW
1770 }
1771
1772 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1773 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1774 r = link_set_ipv6_mtu(link);
1775 if (r < 0)
1776 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1777
1778 return 0;
1779}
1780
1781static int link_admin_state_down(Link *link) {
1782 assert(link);
1783
1784 if (!link->network)
1785 return 0;
1786
899b0e5e 1787 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
1a054a20 1788 log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
712fd5d2 1789 return link_request_to_bring_up_or_down(link, /* up = */ true);
0c9ee5d5
YW
1790 }
1791
1792 return 0;
1793}
1794
0c9ee5d5
YW
1795static bool link_is_enslaved(Link *link) {
1796 if (link->flags & IFF_SLAVE)
0c9ee5d5
YW
1797 return true;
1798
1f024462 1799 if (link->master_ifindex > 0)
0c9ee5d5
YW
1800 return true;
1801
0c9ee5d5
YW
1802 return false;
1803}
1804
0c9ee5d5
YW
1805void link_update_operstate(Link *link, bool also_update_master) {
1806 LinkOperationalState operstate;
1807 LinkCarrierState carrier_state;
1808 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1809 LinkOnlineState online_state;
1810 _cleanup_strv_free_ char **p = NULL;
0c9ee5d5 1811 bool changed = false;
0c9ee5d5
YW
1812
1813 assert(link);
1814
1815 if (link->kernel_operstate == IF_OPER_DORMANT)
1816 carrier_state = LINK_CARRIER_STATE_DORMANT;
1817 else if (link_has_carrier(link)) {
1818 if (link_is_enslaved(link))
1819 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1820 else
1821 carrier_state = LINK_CARRIER_STATE_CARRIER;
1822 } else if (link->flags & IFF_UP)
1823 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1824 else
1825 carrier_state = LINK_CARRIER_STATE_OFF;
1826
1827 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1828 Link *slave;
1829
1830 SET_FOREACH(slave, link->slaves) {
1831 link_update_operstate(slave, false);
1832
1833 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1834 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1835 }
1836 }
1837
60415c13 1838 link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
0c9ee5d5
YW
1839
1840 /* Mapping of address and carrier state vs operational state
1841 * carrier state
1842 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1843 * ------------------------------------------------------------------------------
1844 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
cf4dbd84
HH
1845 * address_state degraded | off | no-carrier | dormant | degraded | degraded | enslaved
1846 * routable | off | no-carrier | dormant | routable | routable | routable
0c9ee5d5
YW
1847 */
1848
cf4dbd84
HH
1849 if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
1850 operstate = LINK_OPERSTATE_ROUTABLE;
1851 else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
1852 operstate = LINK_OPERSTATE_DEGRADED;
1853 else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
0c9ee5d5
YW
1854 operstate = (LinkOperationalState) carrier_state;
1855 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1856 operstate = LINK_OPERSTATE_ROUTABLE;
1857 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1858 operstate = LINK_OPERSTATE_DEGRADED;
1859 else
1860 operstate = LINK_OPERSTATE_ENSLAVED;
1861
2278d9f6
YW
1862 LinkOperationalStateRange req;
1863 link_required_operstate_for_online(link, &req);
1864
0c9ee5d5
YW
1865 /* Only determine online state for managed links with RequiredForOnline=yes */
1866 if (!link->network || !link->network->required_for_online)
1867 online_state = _LINK_ONLINE_STATE_INVALID;
2278d9f6
YW
1868
1869 else if (!operational_state_is_in_range(operstate, &req))
0c9ee5d5 1870 online_state = LINK_ONLINE_STATE_OFFLINE;
2278d9f6 1871
0c9ee5d5
YW
1872 else {
1873 AddressFamily required_family = link->network->required_family_for_online;
1874 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1875 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1876
1877 /* The operational state is within the range required for online.
1878 * If a particular address family is also required, we might revert
1879 * to offline in the blocks below. */
1880 online_state = LINK_ONLINE_STATE_ONLINE;
1881
2278d9f6 1882 if (req.min >= LINK_OPERSTATE_DEGRADED) {
0c9ee5d5
YW
1883 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1884 online_state = LINK_ONLINE_STATE_OFFLINE;
1885 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1886 online_state = LINK_ONLINE_STATE_OFFLINE;
1887 }
1888
2278d9f6 1889 if (req.min >= LINK_OPERSTATE_ROUTABLE) {
0c9ee5d5
YW
1890 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1891 online_state = LINK_ONLINE_STATE_OFFLINE;
1892 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1893 online_state = LINK_ONLINE_STATE_OFFLINE;
1894 }
1895 }
1896
1897 if (link->carrier_state != carrier_state) {
1898 link->carrier_state = carrier_state;
1899 changed = true;
1900 if (strv_extend(&p, "CarrierState") < 0)
1901 log_oom();
1902 }
1903
1904 if (link->address_state != address_state) {
1905 link->address_state = address_state;
1906 changed = true;
1907 if (strv_extend(&p, "AddressState") < 0)
1908 log_oom();
1909 }
0d4ad91d 1910
0c9ee5d5
YW
1911 if (link->ipv4_address_state != ipv4_address_state) {
1912 link->ipv4_address_state = ipv4_address_state;
1913 changed = true;
1914 if (strv_extend(&p, "IPv4AddressState") < 0)
1915 log_oom();
1916 }
9c0a72f9 1917
0c9ee5d5
YW
1918 if (link->ipv6_address_state != ipv6_address_state) {
1919 link->ipv6_address_state = ipv6_address_state;
1920 changed = true;
1921 if (strv_extend(&p, "IPv6AddressState") < 0)
1922 log_oom();
1923 }
9c0a72f9 1924
0c9ee5d5
YW
1925 if (link->operstate != operstate) {
1926 link->operstate = operstate;
1927 changed = true;
1928 if (strv_extend(&p, "OperationalState") < 0)
1929 log_oom();
1930 }
9c0a72f9 1931
0c9ee5d5
YW
1932 if (link->online_state != online_state) {
1933 link->online_state = online_state;
1934 changed = true;
1935 if (strv_extend(&p, "OnlineState") < 0)
1936 log_oom();
1937 }
9c0a72f9 1938
0c9ee5d5
YW
1939 if (p)
1940 link_send_changed_strv(link, p);
1941 if (changed)
1942 link_dirty(link);
9c0a72f9 1943
0c9ee5d5
YW
1944 if (also_update_master) {
1945 Link *master;
9c0a72f9 1946
0c9ee5d5
YW
1947 if (link_get_master(link, &master) >= 0)
1948 link_update_operstate(master, true);
1949 }
9c0a72f9
TG
1950}
1951
0c9ee5d5
YW
1952#define FLAG_STRING(string, flag, old, new) \
1953 (((old ^ new) & flag) \
1954 ? ((old & flag) ? (" -" string) : (" +" string)) \
1955 : "")
1956
0d411b7f 1957static int link_update_flags(Link *link, sd_netlink_message *message) {
96f5f9ef 1958 bool link_was_admin_up, had_carrier;
0c9ee5d5
YW
1959 uint8_t operstate;
1960 unsigned flags;
d236718c
DS
1961 int r;
1962
61135582 1963 assert(link);
0d411b7f 1964 assert(message);
61135582 1965
0d411b7f 1966 r = sd_rtnl_message_link_get_flags(message, &flags);
0c9ee5d5 1967 if (r < 0)
0d411b7f 1968 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
3be9d62a 1969
0d411b7f
YW
1970 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
1971 if (r == -ENODATA)
1972 /* If we got a message without operstate, assume the state was unchanged. */
0c9ee5d5 1973 operstate = link->kernel_operstate;
0d411b7f
YW
1974 else if (r < 0)
1975 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
0c9ee5d5 1976
0d411b7f 1977 if (link->flags == flags && link->kernel_operstate == operstate)
61135582
DS
1978 return 0;
1979
0c9ee5d5
YW
1980 if (link->flags != flags) {
1981 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
61135582 1982
0c9ee5d5
YW
1983 log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1984 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1985 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1986 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1987 FLAG_STRING("UP", IFF_UP, link->flags, flags),
1988 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1989 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1990 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1991 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1992 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1993 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1994 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1995 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1996 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1997 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1998 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1999 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2000 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2001 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2002 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
d236718c 2003
0c9ee5d5
YW
2004 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
2005 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2006 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2007 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2008 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2009 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2010 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
2011 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
d236718c 2012
0c9ee5d5
YW
2013 if (unknown_flags_added)
2014 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
61135582 2015
0c9ee5d5
YW
2016 if (unknown_flags_removed)
2017 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
2018 }
61135582 2019
0d411b7f
YW
2020 link_was_admin_up = link->flags & IFF_UP;
2021 had_carrier = link_has_carrier(link);
2022
0c9ee5d5
YW
2023 link->flags = flags;
2024 link->kernel_operstate = operstate;
36161cba 2025
0c9ee5d5 2026 link_update_operstate(link, true);
61135582 2027
0d411b7f
YW
2028 if (!link_was_admin_up && (link->flags & IFF_UP)) {
2029 log_link_info(link, "Link UP");
22936833 2030
0d411b7f
YW
2031 r = link_admin_state_up(link);
2032 if (r < 0)
2033 return r;
2034 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
2035 log_link_info(link, "Link DOWN");
0d4ad91d 2036
0d411b7f 2037 r = link_admin_state_down(link);
0d4ad91d
AR
2038 if (r < 0)
2039 return r;
7619683b
TG
2040 }
2041
0d411b7f
YW
2042 if (!had_carrier && link_has_carrier(link)) {
2043 log_link_info(link, "Gained carrier");
0d4ad91d 2044
0d411b7f 2045 r = link_carrier_gained(link);
30de2b89
YW
2046 if (r < 0)
2047 return r;
0d411b7f
YW
2048 } else if (had_carrier && !link_has_carrier(link)) {
2049 log_link_info(link, "Lost carrier");
2050
2051 r = link_carrier_lost(link);
0b54c870
YW
2052 if (r < 0)
2053 return r;
b8941f74
TG
2054 }
2055
0d411b7f
YW
2056 return 0;
2057}
afe7fd56 2058
0d411b7f
YW
2059static int link_update_master(Link *link, sd_netlink_message *message) {
2060 int master_ifindex, r;
7465dd22 2061
0d411b7f
YW
2062 assert(link);
2063 assert(message);
69629de9 2064
0d411b7f
YW
2065 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
2066 if (r == -ENODATA)
2067 return 0;
2068 if (r < 0)
2069 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
807667f7 2070
571bf1aa
YW
2071 if (master_ifindex == link->ifindex)
2072 master_ifindex = 0;
2073
74e95e3a
YW
2074 if (master_ifindex != link->master_ifindex) {
2075 if (link->master_ifindex == 0)
2076 log_link_debug(link, "Attached to master interface: %i", master_ifindex);
2077 else if (master_ifindex == 0)
2078 log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
2079 else
2080 log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
2081 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), master_ifindex);
807667f7 2082
74e95e3a
YW
2083 link_drop_from_master(link);
2084 link->master_ifindex = master_ifindex;
2085 }
413708d1 2086
0d411b7f
YW
2087 r = link_append_to_master(link);
2088 if (r < 0)
2089 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
7465dd22 2090
0d411b7f
YW
2091 return 0;
2092}
807667f7 2093
e1658632
YW
2094static int link_update_driver(Link *link, sd_netlink_message *message) {
2095 int r;
2096
2097 assert(link);
2098 assert(link->manager);
2099 assert(message);
2100
2101 /* Driver is already read. Assuming the driver is never changed. */
6625c39e 2102 if (link->ethtool_driver_read)
e1658632
YW
2103 return 0;
2104
2105 /* When udevd is running, read the driver after the interface is initialized by udevd.
2106 * Otherwise, ethtool may not work correctly. See issue #22538.
2107 * When udevd is not running, read the value when the interface is detected. */
6625c39e 2108 if (udev_available() && !link->dev)
e1658632
YW
2109 return 0;
2110
6625c39e
YW
2111 link->ethtool_driver_read = true;
2112
e1658632
YW
2113 r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2114 if (r < 0) {
2115 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2116 return 0;
2117 }
2118
2119 log_link_debug(link, "Found driver: %s", strna(link->driver));
2120
2121 if (streq_ptr(link->driver, "dsa")) {
2122 uint32_t dsa_master_ifindex = 0;
2123
2124 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2125 if (r < 0 && r != -ENODATA)
2126 return log_link_debug_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface: %m");
2127
2128 if (dsa_master_ifindex > INT_MAX) {
2129 log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2130 dsa_master_ifindex);
2131 dsa_master_ifindex = 0;
2132 }
2133
2134 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2135 }
2136
40695457 2137 return 1; /* needs reconfigure */
e1658632
YW
2138}
2139
6625c39e 2140static int link_update_permanent_hardware_address_from_ethtool(Link *link, sd_netlink_message *message) {
e1658632
YW
2141 int r;
2142
2143 assert(link);
2144 assert(link->manager);
2145 assert(message);
2146
6625c39e 2147 if (link->ethtool_permanent_hw_addr_read)
e1658632
YW
2148 return 0;
2149
2150 /* When udevd is running, read the permanent hardware address after the interface is
2151 * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2152 * When udevd is not running, read the value when the interface is detected. */
6625c39e
YW
2153 if (udev_available() && !link->dev)
2154 return 0;
2155
2156 /* If the interface does not have a hardware address, then it will not have a permanent address either. */
2157 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL);
2158 if (r == -ENODATA)
2159 return 0;
2160 if (r < 0)
2161 return log_link_debug_errno(link, r, "Failed to read IFLA_ADDRESS attribute: %m");
2162
2163 link->ethtool_permanent_hw_addr_read = true;
2164
2165 r = ethtool_get_permanent_hw_addr(&link->manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2166 if (r < 0)
2167 log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2168
2169 return 0;
2170}
2171
2172static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
2173 int r;
2174
2175 assert(link);
2176 assert(link->manager);
2177 assert(message);
2178
2179 if (link->permanent_hw_addr.length > 0)
e1658632
YW
2180 return 0;
2181
2182 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2183 if (r < 0) {
2184 if (r != -ENODATA)
2185 return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
2186
6625c39e
YW
2187 /* Fallback to ethtool for older kernels. */
2188 r = link_update_permanent_hardware_address_from_ethtool(link, message);
2189 if (r < 0)
2190 return r;
e1658632
YW
2191 }
2192
2193 if (link->permanent_hw_addr.length > 0)
2194 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2195
40695457 2196 return 1; /* needs reconfigure */
e1658632
YW
2197}
2198
0d411b7f 2199static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
e2bacccd 2200 struct hw_addr_data addr;
0d411b7f 2201 int r;
d93d655c 2202
0d411b7f
YW
2203 assert(link);
2204 assert(message);
dfc58b47 2205
0d411b7f
YW
2206 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2207 if (r < 0 && r != -ENODATA)
2208 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
4f882b2a 2209
e2bacccd 2210 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
0d411b7f
YW
2211 if (r == -ENODATA)
2212 return 0;
2213 if (r < 0)
fe321d45 2214 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
bb262ef0 2215
e2bacccd 2216 if (hw_addr_equal(&link->hw_addr, &addr))
0d411b7f 2217 return 0;
a61bb41c 2218
de0f1579 2219 if (link->hw_addr.length == 0)
e2bacccd 2220 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
fe321d45 2221 else {
e2341b6b
DT
2222 log_link_debug(link, "Hardware address is changed: %s %s %s",
2223 HW_ADDR_TO_STR(&link->hw_addr),
2224 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
2225 HW_ADDR_TO_STR(&addr));
0d411b7f 2226
e74f1d07 2227 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
fe321d45
YW
2228 }
2229
e2bacccd
YW
2230 link->hw_addr = addr;
2231
fe321d45
YW
2232 if (!hw_addr_is_null(&link->hw_addr)) {
2233 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2234 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2235 /* bonding master and its slaves have the same hardware address. */
2236 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2237 if (r < 0)
2238 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2239 }
0d411b7f 2240
0a14f83a 2241 r = ipv4acd_update_mac(link);
76a86ffd
YW
2242 if (r < 0)
2243 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2244
0d411b7f 2245 r = ipv4ll_update_mac(link);
a61bb41c 2246 if (r < 0)
0d411b7f 2247 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
a61bb41c 2248
0d411b7f
YW
2249 r = dhcp4_update_mac(link);
2250 if (r < 0)
2251 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
d236718c 2252
0d411b7f
YW
2253 r = dhcp6_update_mac(link);
2254 if (r < 0)
2255 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2256
2257 r = radv_update_mac(link);
2258 if (r < 0)
2259 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2260
3be64aa4 2261 if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
ca2b7cd8 2262 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
d236718c 2263 if (r < 0)
0d411b7f
YW
2264 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2265 }
d236718c 2266
35778343
YW
2267 if (link->lldp_rx) {
2268 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
61135582 2269 if (r < 0)
c01b9b87
YW
2270 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2271 }
2272
2273 if (link->lldp_tx) {
2274 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2275 if (r < 0)
2276 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
61135582
DS
2277 }
2278
40695457 2279 return 1; /* needs reconfigure */
0d411b7f 2280}
a61bb41c 2281
0d411b7f 2282static int link_update_mtu(Link *link, sd_netlink_message *message) {
717ba5fc 2283 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
0d411b7f 2284 int r;
a61bb41c 2285
0d411b7f
YW
2286 assert(link);
2287 assert(message);
2288
2289 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2290 if (r == -ENODATA)
2291 return 0;
2292 if (r < 0)
2293 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
b5d0fd1e
YW
2294 if (mtu == 0)
2295 return 0;
0d411b7f 2296
717ba5fc
YW
2297 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2298 if (r < 0 && r != -ENODATA)
2299 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2300
2301 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2302 if (r < 0 && r != -ENODATA)
2303 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2304
717ba5fc
YW
2305 if (max_mtu == 0)
2306 max_mtu = UINT32_MAX;
2307
2308 link->min_mtu = min_mtu;
2309 link->max_mtu = max_mtu;
2310
0d411b7f
YW
2311 if (link->original_mtu == 0) {
2312 link->original_mtu = mtu;
717ba5fc
YW
2313 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2314 link->original_mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2315 }
2316
717ba5fc
YW
2317 if (link->mtu == mtu)
2318 return 0;
2319
b5d0fd1e 2320 if (link->mtu != 0)
e2341b6b
DT
2321 log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2322 link->mtu, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), mtu,
2323 link->min_mtu, link->max_mtu);
0d411b7f
YW
2324
2325 link->mtu = mtu;
2326
2327 if (link->dhcp_client) {
2328 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
9c0a72f9 2329 if (r < 0)
0d411b7f
YW
2330 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2331 }
a61bb41c 2332
0d411b7f
YW
2333 if (link->radv) {
2334 r = sd_radv_set_mtu(link->radv, link->mtu);
9c0a72f9 2335 if (r < 0)
0d411b7f 2336 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
a61bb41c
TG
2337 }
2338
2339 return 0;
dd3efc09 2340}
fe8db0c5 2341
0d411b7f
YW
2342static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2343 _cleanup_strv_free_ char **altnames = NULL;
0d411b7f
YW
2344 int r;
2345
2346 assert(link);
2347 assert(message);
2348
2349 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
50df02a7 2350 if (r == -ENODATA)
2368ff81 2351 /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
50df02a7
YW
2352 * interface has no alternative name. */
2353 return 0;
2354 if (r < 0)
0d411b7f
YW
2355 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2356
1b345c1e
YW
2357 if (strv_equal(altnames, link->alternative_names))
2358 return 0;
2359
0d411b7f
YW
2360 STRV_FOREACH(n, link->alternative_names)
2361 hashmap_remove(link->manager->links_by_name, *n);
2362
2363 strv_free_and_replace(link->alternative_names, altnames);
2364
2365 STRV_FOREACH(n, link->alternative_names) {
2366 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2367 if (r < 0)
2368 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2369 }
2370
40695457 2371 return 1; /* needs reconfigure */
0d411b7f
YW
2372}
2373
2374static int link_update_name(Link *link, sd_netlink_message *message) {
01afd0f7 2375 char ifname_from_index[IF_NAMESIZE];
0d411b7f
YW
2376 const char *ifname;
2377 int r;
2378
2379 assert(link);
2380 assert(message);
2381
2382 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2383 if (r == -ENODATA)
7802194a 2384 /* Hmm?? But ok. */
0d411b7f
YW
2385 return 0;
2386 if (r < 0)
2387 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2388
2389 if (streq(ifname, link->ifname))
2390 return 0;
2391
01afd0f7
YW
2392 r = format_ifname(link->ifindex, ifname_from_index);
2393 if (r < 0)
2394 return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
176b8be1
YW
2395
2396 if (!streq(ifname, ifname_from_index)) {
2397 log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
2398 "with the name currently configured on the actual interface '%s'. Ignoring.",
2399 ifname, ifname_from_index);
2400 return 0;
2401 }
2402
0d411b7f
YW
2403 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2404
2405 hashmap_remove(link->manager->links_by_name, link->ifname);
2406
2407 r = free_and_strdup(&link->ifname, ifname);
2408 if (r < 0)
2409 return log_oom_debug();
2410
2411 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2412 if (r < 0)
2413 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2414
54d1fdb2
YW
2415 if (link->dhcp_client) {
2416 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2417 if (r < 0)
2418 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2419 }
2420
2421 if (link->dhcp6_client) {
2422 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2423 if (r < 0)
2424 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m");
2425 }
2426
2427 if (link->ndisc) {
2428 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2429 if (r < 0)
2430 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2431 }
2432
2433 if (link->dhcp_server) {
2434 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2435 if (r < 0)
2436 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2437 }
2438
2439 if (link->radv) {
2440 r = sd_radv_set_ifname(link->radv, link->ifname);
2441 if (r < 0)
2442 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2443 }
2444
35778343
YW
2445 if (link->lldp_rx) {
2446 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
54d1fdb2 2447 if (r < 0)
c01b9b87
YW
2448 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2449 }
2450
2451 if (link->lldp_tx) {
2452 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2453 if (r < 0)
2454 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
54d1fdb2
YW
2455 }
2456
2457 if (link->ipv4ll) {
2458 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2459 if (r < 0)
2460 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2461 }
2462
86173383
YW
2463 r = ipv4acd_set_ifname(link);
2464 if (r < 0)
2465 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
54d1fdb2 2466
40695457 2467 return 1; /* needs reconfigure */
0d411b7f
YW
2468}
2469
2470static int link_update(Link *link, sd_netlink_message *message) {
40695457 2471 bool needs_reconfigure = false;
0d411b7f
YW
2472 int r;
2473
2474 assert(link);
2475 assert(message);
2476
2477 r = link_update_name(link, message);
2478 if (r < 0)
2479 return r;
40695457 2480 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2481
2482 r = link_update_alternative_names(link, message);
2483 if (r < 0)
2484 return r;
40695457 2485 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2486
2487 r = link_update_mtu(link, message);
2488 if (r < 0)
2489 return r;
2490
e1658632
YW
2491 r = link_update_driver(link, message);
2492 if (r < 0)
2493 return r;
40695457 2494 needs_reconfigure = needs_reconfigure || r > 0;
e1658632
YW
2495
2496 r = link_update_permanent_hardware_address(link, message);
2497 if (r < 0)
2498 return r;
40695457 2499 needs_reconfigure = needs_reconfigure || r > 0;
e1658632 2500
0d411b7f
YW
2501 r = link_update_hardware_address(link, message);
2502 if (r < 0)
2503 return r;
40695457 2504 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2505
2506 r = link_update_master(link, message);
2507 if (r < 0)
2508 return r;
2509
7d0d832d
YW
2510 r = link_update_ipv6ll_addrgen_mode(link, message);
2511 if (r < 0)
2512 return r;
2513
40695457
YW
2514 r = link_update_flags(link, message);
2515 if (r < 0)
2516 return r;
2517
11cee6ef
YW
2518 r = link_update_bridge_vlan(link, message);
2519 if (r < 0)
2520 return r;
2521
40695457 2522 return needs_reconfigure;
0d411b7f
YW
2523}
2524
0c9ee5d5
YW
2525static Link *link_drop_or_unref(Link *link) {
2526 if (!link)
2527 return NULL;
2528 if (!link->manager)
2529 return link_unref(link);
2530 return link_drop(link);
2531}
2532
2533DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2534
2535static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
0d411b7f 2536 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
0c9ee5d5 2537 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
0c9ee5d5
YW
2538 unsigned short iftype;
2539 int r, ifindex;
0c9ee5d5
YW
2540
2541 assert(manager);
2542 assert(message);
2543 assert(ret);
2544
0c9ee5d5
YW
2545 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2546 if (r < 0)
0d411b7f 2547 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
0c9ee5d5 2548 else if (ifindex <= 0)
0d411b7f 2549 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
0c9ee5d5
YW
2550
2551 r = sd_rtnl_message_link_get_type(message, &iftype);
2552 if (r < 0)
0d411b7f 2553 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
0c9ee5d5
YW
2554
2555 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2556 if (r < 0)
0d411b7f 2557 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
0c9ee5d5
YW
2558
2559 /* check for link kind */
2560 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2561 if (r >= 0) {
0d411b7f
YW
2562 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2563 if (r < 0 && r != -ENODATA)
2564 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
0c9ee5d5
YW
2565 r = sd_netlink_message_exit_container(message);
2566 if (r < 0)
0d411b7f 2567 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
0c9ee5d5
YW
2568 }
2569
4c78dc17
YW
2570 if (!manager->test_mode) {
2571 /* Do not update state files when running in test mode. */
2572 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2573 return log_oom_debug();
0d411b7f 2574
4c78dc17
YW
2575 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2576 return log_oom_debug();
0d411b7f 2577
4c78dc17
YW
2578 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2579 return log_oom_debug();
2580 }
0d411b7f 2581
0c9ee5d5
YW
2582 link = new(Link, 1);
2583 if (!link)
2584 return -ENOMEM;
2585
2586 *link = (Link) {
2587 .n_ref = 1,
2588 .state = LINK_STATE_PENDING,
2589 .online_state = _LINK_ONLINE_STATE_INVALID,
c2eb7753 2590 .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
0c9ee5d5
YW
2591 .ifindex = ifindex,
2592 .iftype = iftype,
2593 .ifname = TAKE_PTR(ifname),
2594 .kind = TAKE_PTR(kind),
2595
11cee6ef
YW
2596 .bridge_vlan_pvid = UINT16_MAX,
2597
7d0d832d
YW
2598 .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2599
0d411b7f
YW
2600 .state_file = TAKE_PTR(state_file),
2601 .lease_file = TAKE_PTR(lease_file),
2602 .lldp_file = TAKE_PTR(lldp_file),
2603
0c9ee5d5
YW
2604 .n_dns = UINT_MAX,
2605 .dns_default_route = -1,
2606 .llmnr = _RESOLVE_SUPPORT_INVALID,
2607 .mdns = _RESOLVE_SUPPORT_INVALID,
2608 .dnssec_mode = _DNSSEC_MODE_INVALID,
2609 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2610 };
2611
6eab614d 2612 r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
0c9ee5d5 2613 if (r < 0)
0d411b7f 2614 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
0c9ee5d5
YW
2615
2616 link->manager = manager;
2617
0d411b7f 2618 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
0c9ee5d5 2619 if (r < 0)
0d411b7f 2620 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
0c9ee5d5 2621
8a77e245
YW
2622 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2623 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2624
9c5b8d46
2625 /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2626 * message arrived too early. Request the wireless link information again.
2627 */
2628 if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2629 r = link_get_wlan_interface(link);
2630 if (r < 0)
2631 log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
2632 }
2633
0c9ee5d5 2634 *ret = TAKE_PTR(link);
0c9ee5d5
YW
2635 return 0;
2636}
2637
0d411b7f 2638int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
3be9d62a
YW
2639 Link *link = NULL;
2640 NetDev *netdev = NULL;
2641 uint16_t type;
2642 const char *name;
2643 int r, ifindex;
2644
2645 assert(rtnl);
2646 assert(message);
0d411b7f 2647 assert(manager);
3be9d62a
YW
2648
2649 if (sd_netlink_message_is_error(message)) {
2650 r = sd_netlink_message_get_errno(message);
2651 if (r < 0)
2652 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2653
2654 return 0;
2655 }
2656
2657 r = sd_netlink_message_get_type(message, &type);
2658 if (r < 0) {
2659 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2660 return 0;
2661 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2662 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2663 return 0;
2664 }
2665
2666 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2667 if (r < 0) {
2668 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2669 return 0;
2670 } else if (ifindex <= 0) {
2671 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2672 return 0;
2673 }
2674
2675 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2676 if (r < 0) {
2677 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2678 return 0;
2679 }
2680
6eab614d 2681 (void) link_get_by_index(manager, ifindex, &link);
0d411b7f 2682 (void) netdev_get(manager, name, &netdev);
3be9d62a
YW
2683
2684 switch (type) {
2685 case RTM_NEWLINK:
0d411b7f
YW
2686 if (netdev) {
2687 /* netdev exists, so make sure the ifindex matches */
2688 r = netdev_set_ifindex(netdev, message);
2689 if (r < 0) {
66141164 2690 log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
0d411b7f
YW
2691 return 0;
2692 }
2693 }
2694
3be9d62a
YW
2695 if (!link) {
2696 /* link is new, so add it */
0d411b7f 2697 r = link_new(manager, message, &link);
3be9d62a 2698 if (r < 0) {
0d411b7f 2699 log_warning_errno(r, "Could not process new link message: %m");
3be9d62a
YW
2700 return 0;
2701 }
3be9d62a 2702
0d411b7f 2703 r = link_update(link, message);
3be9d62a 2704 if (r < 0) {
66141164 2705 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f 2706 link_enter_failed(link);
3be9d62a
YW
2707 return 0;
2708 }
3be9d62a 2709
0d411b7f
YW
2710 r = link_check_initialized(link);
2711 if (r < 0) {
66141164 2712 log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
0d411b7f
YW
2713 link_enter_failed(link);
2714 return 0;
2715 }
2716 } else {
2717 r = link_update(link, message);
2718 if (r < 0) {
66141164 2719 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f
YW
2720 link_enter_failed(link);
2721 return 0;
2722 }
40695457
YW
2723 if (r > 0) {
2724 r = link_reconfigure_impl(link, /* force = */ false);
2725 if (r < 0) {
2726 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
2727 link_enter_failed(link);
2728 return 0;
2729 }
2730 }
3be9d62a 2731 }
3be9d62a
YW
2732 break;
2733
2734 case RTM_DELLINK:
2735 link_drop(link);
2736 netdev_drop(netdev);
3be9d62a
YW
2737 break;
2738
2739 default:
04499a70 2740 assert_not_reached();
3be9d62a
YW
2741 }
2742
2743 return 1;
2744}
2745
79c6e114
YW
2746int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2747 uint16_t message_type;
2748 int r;
2749
2750 assert(m);
2751 assert(link);
2752 assert(error_msg);
2753
2754 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2755 return 0;
2756
2757 r = sd_netlink_message_get_errno(m);
2758 if (r < 0) {
2759 log_link_message_warning_errno(link, m, r, error_msg);
2760 link_enter_failed(link);
2761 return 0;
2762 }
2763
2764 r = sd_netlink_message_get_type(m, &message_type);
2765 if (r < 0) {
2766 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2767 return 0;
2768 }
2769 if (message_type != RTM_NEWLINK) {
2770 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2771 return 0;
2772 }
2773
2774 r = link_update(link, m);
2775 if (r < 0) {
2776 link_enter_failed(link);
2777 return 0;
2778 }
2779
2780 return 1;
2781}
2782
2783int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2784 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2785 int r;
2786
2787 assert(link);
2788 assert(link->manager);
2789 assert(link->manager->rtnl);
2790 assert(callback);
2791
2792 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2793 if (r < 0)
2794 return r;
2795
2796 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2797 link_netlink_destroy_callback, link);
2798 if (r < 0)
2799 return r;
2800
2801 link_ref(link);
2802 return 0;
2803}
2804
fe8db0c5 2805static const char* const link_state_table[_LINK_STATE_MAX] = {
0580badc 2806 [LINK_STATE_PENDING] = "pending",
bd08ce56 2807 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 2808 [LINK_STATE_CONFIGURING] = "configuring",
0580badc
ZJS
2809 [LINK_STATE_CONFIGURED] = "configured",
2810 [LINK_STATE_UNMANAGED] = "unmanaged",
2811 [LINK_STATE_FAILED] = "failed",
2812 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
2813};
2814
2815DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
7ae55b78
YW
2816
2817int link_flags_to_string_alloc(uint32_t flags, char **ret) {
2818 _cleanup_free_ char *str = NULL;
19aa7eeb
ZJS
2819 static const char* map[] = {
2820 [LOG2U(IFF_UP)] = "up", /* interface is up. */
0b75493d 2821 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid. */
19aa7eeb
ZJS
2822 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
2823 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
2824 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2825 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
2826 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
2827 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
2828 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
2829 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
2830 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
2831 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
0b75493d 2832 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast. */
19aa7eeb
ZJS
2833 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
2834 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
2835 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
2836 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
2837 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
2838 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
7ae55b78
YW
2839 };
2840
2841 assert(ret);
2842
2843 for (size_t i = 0; i < ELEMENTSOF(map); i++)
19aa7eeb
ZJS
2844 if (FLAGS_SET(flags, 1 << i) && map[i])
2845 if (!strextend_with_separator(&str, ",", map[i]))
2846 return -ENOMEM;
7ae55b78
YW
2847
2848 *ret = TAKE_PTR(str);
2849 return 0;
2850}
2851
2852static const char * const kernel_operstate_table[] = {
2853 [IF_OPER_UNKNOWN] = "unknown",
2854 [IF_OPER_NOTPRESENT] = "not-present",
2855 [IF_OPER_DOWN] = "down",
2856 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
2857 [IF_OPER_TESTING] = "testing",
2858 [IF_OPER_DORMANT] = "dormant",
2859 [IF_OPER_UP] = "up",
2860};
2861
2862DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);