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