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