]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
man: fix typo
[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 1638static int link_carrier_lost(Link *link) {
b7326069
YW
1639 uint16_t dhcp_mtu;
1640 usec_t usec;
9c0a72f9
TG
1641 int r;
1642
1643 assert(link);
1644
aa9117c0
YW
1645 r = link_handle_bound_by_list(link);
1646 if (r < 0)
1647 return r;
93b4dab5 1648
600b7898
YW
1649 if (link->iftype == ARPHRD_CAN)
1650 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0
YW
1651 return 0;
1652
f1c22cf4
YW
1653 if (!link->network)
1654 return 0;
1655
b7326069
YW
1656 if (link->network->ignore_carrier_loss_set)
1657 /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1658 usec = link->network->ignore_carrier_loss_usec;
1659
1660 else if (link->network->bond && link->wlan_iftype > 0)
1661 /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1662 * carrier to be lost. See #19832. */
1663 usec = 3 * USEC_PER_SEC;
1664
1665 else if (link->network->dhcp_use_mtu &&
1666 link->dhcp_lease &&
1667 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
1668 dhcp_mtu != link->original_mtu)
1669 /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1670 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1671 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1672 usec = 5 * USEC_PER_SEC;
1673
1674 else
1675 /* Otherwise, use the currently set value. */
1676 usec = link->network->ignore_carrier_loss_usec;
1677
1678 if (usec == USEC_INFINITY)
aa9117c0 1679 return 0;
600b7898 1680
b7326069 1681 if (usec == 0)
6706ce2f
YW
1682 return link_carrier_lost_impl(link);
1683
1684 return event_reset_time_relative(link->manager->event,
1685 &link->carrier_lost_timer,
ba4e0427 1686 CLOCK_BOOTTIME,
b7326069 1687 usec,
6706ce2f
YW
1688 0,
1689 link_carrier_lost_handler,
1690 link,
1691 0,
1692 "link-carrier-loss",
1693 true);
0c9ee5d5
YW
1694}
1695
0c9ee5d5
YW
1696static int link_admin_state_up(Link *link) {
1697 int r;
1698
1699 assert(link);
1700
1701 /* This is called every time an interface admin state changes to up;
1702 * specifically, when IFF_UP flag changes from unset to set. */
1703
1704 if (!link->network)
1705 return 0;
1706
899b0e5e 1707 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
1a054a20 1708 log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
712fd5d2 1709 return link_request_to_bring_up_or_down(link, /* up = */ false);
0c9ee5d5
YW
1710 }
1711
1712 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1713 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1714 r = link_set_ipv6_mtu(link);
1715 if (r < 0)
1716 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1717
1718 return 0;
1719}
1720
1721static int link_admin_state_down(Link *link) {
1722 assert(link);
1723
1724 if (!link->network)
1725 return 0;
1726
899b0e5e 1727 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
1a054a20 1728 log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
712fd5d2 1729 return link_request_to_bring_up_or_down(link, /* up = */ true);
0c9ee5d5
YW
1730 }
1731
1732 return 0;
1733}
1734
0c9ee5d5
YW
1735static bool link_is_enslaved(Link *link) {
1736 if (link->flags & IFF_SLAVE)
0c9ee5d5
YW
1737 return true;
1738
1f024462 1739 if (link->master_ifindex > 0)
0c9ee5d5
YW
1740 return true;
1741
0c9ee5d5
YW
1742 return false;
1743}
1744
1745static LinkAddressState address_state_from_scope(uint8_t scope) {
1746 if (scope < RT_SCOPE_SITE)
1747 /* universally accessible addresses found */
1748 return LINK_ADDRESS_STATE_ROUTABLE;
1749
1750 if (scope < RT_SCOPE_HOST)
1751 /* only link or site local addresses found */
1752 return LINK_ADDRESS_STATE_DEGRADED;
1753
1754 /* no useful addresses found */
1755 return LINK_ADDRESS_STATE_OFF;
1756}
1757
1758void link_update_operstate(Link *link, bool also_update_master) {
1759 LinkOperationalState operstate;
1760 LinkCarrierState carrier_state;
1761 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1762 LinkOnlineState online_state;
1763 _cleanup_strv_free_ char **p = NULL;
1764 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
1765 bool changed = false;
1766 Address *address;
1767
1768 assert(link);
1769
1770 if (link->kernel_operstate == IF_OPER_DORMANT)
1771 carrier_state = LINK_CARRIER_STATE_DORMANT;
1772 else if (link_has_carrier(link)) {
1773 if (link_is_enslaved(link))
1774 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1775 else
1776 carrier_state = LINK_CARRIER_STATE_CARRIER;
1777 } else if (link->flags & IFF_UP)
1778 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1779 else
1780 carrier_state = LINK_CARRIER_STATE_OFF;
1781
1782 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1783 Link *slave;
1784
1785 SET_FOREACH(slave, link->slaves) {
1786 link_update_operstate(slave, false);
1787
1788 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1789 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1790 }
1791 }
1792
1793 SET_FOREACH(address, link->addresses) {
1794 if (!address_is_ready(address))
1795 continue;
1796
1797 if (address->family == AF_INET)
1798 ipv4_scope = MIN(ipv4_scope, address->scope);
1799
1800 if (address->family == AF_INET6)
1801 ipv6_scope = MIN(ipv6_scope, address->scope);
1802 }
1803
0c9ee5d5
YW
1804 ipv4_address_state = address_state_from_scope(ipv4_scope);
1805 ipv6_address_state = address_state_from_scope(ipv6_scope);
1806 address_state = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
1807
1808 /* Mapping of address and carrier state vs operational state
1809 * carrier state
1810 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1811 * ------------------------------------------------------------------------------
1812 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1813 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1814 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1815 */
1816
1817 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
1818 operstate = (LinkOperationalState) carrier_state;
1819 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1820 operstate = LINK_OPERSTATE_ROUTABLE;
1821 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1822 operstate = LINK_OPERSTATE_DEGRADED;
1823 else
1824 operstate = LINK_OPERSTATE_ENSLAVED;
1825
1826 /* Only determine online state for managed links with RequiredForOnline=yes */
1827 if (!link->network || !link->network->required_for_online)
1828 online_state = _LINK_ONLINE_STATE_INVALID;
1829 else if (operstate < link->network->required_operstate_for_online.min ||
1830 operstate > link->network->required_operstate_for_online.max)
1831 online_state = LINK_ONLINE_STATE_OFFLINE;
1832 else {
1833 AddressFamily required_family = link->network->required_family_for_online;
1834 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1835 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1836
1837 /* The operational state is within the range required for online.
1838 * If a particular address family is also required, we might revert
1839 * to offline in the blocks below. */
1840 online_state = LINK_ONLINE_STATE_ONLINE;
1841
1842 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) {
1843 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1844 online_state = LINK_ONLINE_STATE_OFFLINE;
1845 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1846 online_state = LINK_ONLINE_STATE_OFFLINE;
1847 }
1848
1849 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) {
1850 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1851 online_state = LINK_ONLINE_STATE_OFFLINE;
1852 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1853 online_state = LINK_ONLINE_STATE_OFFLINE;
1854 }
1855 }
1856
1857 if (link->carrier_state != carrier_state) {
1858 link->carrier_state = carrier_state;
1859 changed = true;
1860 if (strv_extend(&p, "CarrierState") < 0)
1861 log_oom();
1862 }
1863
1864 if (link->address_state != address_state) {
1865 link->address_state = address_state;
1866 changed = true;
1867 if (strv_extend(&p, "AddressState") < 0)
1868 log_oom();
1869 }
0d4ad91d 1870
0c9ee5d5
YW
1871 if (link->ipv4_address_state != ipv4_address_state) {
1872 link->ipv4_address_state = ipv4_address_state;
1873 changed = true;
1874 if (strv_extend(&p, "IPv4AddressState") < 0)
1875 log_oom();
1876 }
9c0a72f9 1877
0c9ee5d5
YW
1878 if (link->ipv6_address_state != ipv6_address_state) {
1879 link->ipv6_address_state = ipv6_address_state;
1880 changed = true;
1881 if (strv_extend(&p, "IPv6AddressState") < 0)
1882 log_oom();
1883 }
9c0a72f9 1884
0c9ee5d5
YW
1885 if (link->operstate != operstate) {
1886 link->operstate = operstate;
1887 changed = true;
1888 if (strv_extend(&p, "OperationalState") < 0)
1889 log_oom();
1890 }
9c0a72f9 1891
0c9ee5d5
YW
1892 if (link->online_state != online_state) {
1893 link->online_state = online_state;
1894 changed = true;
1895 if (strv_extend(&p, "OnlineState") < 0)
1896 log_oom();
1897 }
9c0a72f9 1898
0c9ee5d5
YW
1899 if (p)
1900 link_send_changed_strv(link, p);
1901 if (changed)
1902 link_dirty(link);
9c0a72f9 1903
0c9ee5d5
YW
1904 if (also_update_master) {
1905 Link *master;
9c0a72f9 1906
0c9ee5d5
YW
1907 if (link_get_master(link, &master) >= 0)
1908 link_update_operstate(master, true);
1909 }
9c0a72f9
TG
1910}
1911
0c9ee5d5
YW
1912#define FLAG_STRING(string, flag, old, new) \
1913 (((old ^ new) & flag) \
1914 ? ((old & flag) ? (" -" string) : (" +" string)) \
1915 : "")
1916
0d411b7f 1917static int link_update_flags(Link *link, sd_netlink_message *message) {
96f5f9ef 1918 bool link_was_admin_up, had_carrier;
0c9ee5d5
YW
1919 uint8_t operstate;
1920 unsigned flags;
d236718c
DS
1921 int r;
1922
61135582 1923 assert(link);
0d411b7f 1924 assert(message);
61135582 1925
0d411b7f 1926 r = sd_rtnl_message_link_get_flags(message, &flags);
0c9ee5d5 1927 if (r < 0)
0d411b7f 1928 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
3be9d62a 1929
0d411b7f
YW
1930 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
1931 if (r == -ENODATA)
1932 /* If we got a message without operstate, assume the state was unchanged. */
0c9ee5d5 1933 operstate = link->kernel_operstate;
0d411b7f
YW
1934 else if (r < 0)
1935 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
0c9ee5d5 1936
0d411b7f 1937 if (link->flags == flags && link->kernel_operstate == operstate)
61135582
DS
1938 return 0;
1939
0c9ee5d5
YW
1940 if (link->flags != flags) {
1941 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
61135582 1942
0c9ee5d5
YW
1943 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",
1944 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1945 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1946 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1947 FLAG_STRING("UP", IFF_UP, link->flags, flags),
1948 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1949 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1950 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1951 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1952 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1953 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1954 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1955 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1956 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1957 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1958 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1959 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
1960 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
1961 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
1962 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
d236718c 1963
0c9ee5d5
YW
1964 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
1965 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
1966 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
1967 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
1968 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
1969 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
1970 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
1971 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
d236718c 1972
0c9ee5d5
YW
1973 if (unknown_flags_added)
1974 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
61135582 1975
0c9ee5d5
YW
1976 if (unknown_flags_removed)
1977 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
1978 }
61135582 1979
0d411b7f
YW
1980 link_was_admin_up = link->flags & IFF_UP;
1981 had_carrier = link_has_carrier(link);
1982
0c9ee5d5
YW
1983 link->flags = flags;
1984 link->kernel_operstate = operstate;
36161cba 1985
0c9ee5d5 1986 link_update_operstate(link, true);
61135582 1987
0d411b7f
YW
1988 if (!link_was_admin_up && (link->flags & IFF_UP)) {
1989 log_link_info(link, "Link UP");
22936833 1990
0d411b7f
YW
1991 r = link_admin_state_up(link);
1992 if (r < 0)
1993 return r;
1994 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1995 log_link_info(link, "Link DOWN");
0d4ad91d 1996
0d411b7f 1997 r = link_admin_state_down(link);
0d4ad91d
AR
1998 if (r < 0)
1999 return r;
7619683b
TG
2000 }
2001
0d411b7f
YW
2002 if (!had_carrier && link_has_carrier(link)) {
2003 log_link_info(link, "Gained carrier");
0d4ad91d 2004
0d411b7f 2005 r = link_carrier_gained(link);
30de2b89
YW
2006 if (r < 0)
2007 return r;
0d411b7f
YW
2008 } else if (had_carrier && !link_has_carrier(link)) {
2009 log_link_info(link, "Lost carrier");
2010
2011 r = link_carrier_lost(link);
0b54c870
YW
2012 if (r < 0)
2013 return r;
b8941f74
TG
2014 }
2015
0d411b7f
YW
2016 return 0;
2017}
afe7fd56 2018
0d411b7f
YW
2019static int link_update_master(Link *link, sd_netlink_message *message) {
2020 int master_ifindex, r;
7465dd22 2021
0d411b7f
YW
2022 assert(link);
2023 assert(message);
69629de9 2024
0d411b7f
YW
2025 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
2026 if (r == -ENODATA)
2027 return 0;
2028 if (r < 0)
2029 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
807667f7 2030
571bf1aa
YW
2031 if (master_ifindex == link->ifindex)
2032 master_ifindex = 0;
2033
0d411b7f
YW
2034 if (master_ifindex == link->master_ifindex)
2035 return 0;
807667f7 2036
0d411b7f
YW
2037 if (link->master_ifindex == 0)
2038 log_link_debug(link, "Joined to master interface: %i", master_ifindex);
2039 else if (master_ifindex == 0)
3881fd40 2040 log_link_debug(link, "Left from master interface: %i", link->master_ifindex);
0d411b7f
YW
2041 else
2042 log_link_debug(link, "Master interface is changed: %i → %i", link->master_ifindex, master_ifindex);
807667f7 2043
0d411b7f 2044 link_drop_from_master(link);
807667f7 2045
0d411b7f 2046 link->master_ifindex = master_ifindex;
413708d1 2047
0d411b7f
YW
2048 r = link_append_to_master(link);
2049 if (r < 0)
2050 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
7465dd22 2051
0d411b7f
YW
2052 return 0;
2053}
807667f7 2054
e1658632
YW
2055static int link_update_driver(Link *link, sd_netlink_message *message) {
2056 int r;
2057
2058 assert(link);
2059 assert(link->manager);
2060 assert(message);
2061
2062 /* Driver is already read. Assuming the driver is never changed. */
2063 if (link->driver)
2064 return 0;
2065
2066 /* When udevd is running, read the driver after the interface is initialized by udevd.
2067 * Otherwise, ethtool may not work correctly. See issue #22538.
2068 * When udevd is not running, read the value when the interface is detected. */
2069 if (link->state != (udev_available() ? LINK_STATE_INITIALIZED : LINK_STATE_PENDING))
2070 return 0;
2071
2072 r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2073 if (r < 0) {
2074 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2075 return 0;
2076 }
2077
2078 log_link_debug(link, "Found driver: %s", strna(link->driver));
2079
2080 if (streq_ptr(link->driver, "dsa")) {
2081 uint32_t dsa_master_ifindex = 0;
2082
2083 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2084 if (r < 0 && r != -ENODATA)
2085 return log_link_debug_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface: %m");
2086
2087 if (dsa_master_ifindex > INT_MAX) {
2088 log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2089 dsa_master_ifindex);
2090 dsa_master_ifindex = 0;
2091 }
2092
2093 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2094 }
2095
2096 return 0;
2097}
2098
2099static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
2100 int r;
2101
2102 assert(link);
2103 assert(link->manager);
2104 assert(message);
2105
2106 if (link->permanent_hw_addr.length > 0)
2107 return 0;
2108
2109 /* When udevd is running, read the permanent hardware address after the interface is
2110 * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2111 * When udevd is not running, read the value when the interface is detected. */
2112 if (link->state != (udev_available() ? LINK_STATE_INITIALIZED : LINK_STATE_PENDING))
2113 return 0;
2114
2115 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2116 if (r < 0) {
2117 if (r != -ENODATA)
2118 return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
2119
2120 if (netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL) >= 0) {
2121 /* Fallback to ethtool, if the link has a hardware address. */
2122 r = ethtool_get_permanent_hw_addr(&link->manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2123 if (r < 0)
2124 log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2125 }
2126 }
2127
2128 if (link->permanent_hw_addr.length > 0)
2129 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2130
2131 return 0;
2132}
2133
0d411b7f 2134static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
e2bacccd 2135 struct hw_addr_data addr;
0d411b7f 2136 int r;
d93d655c 2137
0d411b7f
YW
2138 assert(link);
2139 assert(message);
dfc58b47 2140
0d411b7f
YW
2141 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2142 if (r < 0 && r != -ENODATA)
2143 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
4f882b2a 2144
e2bacccd 2145 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
0d411b7f
YW
2146 if (r == -ENODATA)
2147 return 0;
2148 if (r < 0)
fe321d45 2149 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
bb262ef0 2150
e2bacccd 2151 if (hw_addr_equal(&link->hw_addr, &addr))
0d411b7f 2152 return 0;
a61bb41c 2153
de0f1579 2154 if (link->hw_addr.length == 0)
e2bacccd 2155 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
fe321d45
YW
2156 else {
2157 log_link_debug(link, "Hardware address is changed: %s → %s",
e2bacccd 2158 HW_ADDR_TO_STR(&link->hw_addr), HW_ADDR_TO_STR(&addr));
0d411b7f 2159
e74f1d07 2160 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
fe321d45
YW
2161 }
2162
e2bacccd
YW
2163 link->hw_addr = addr;
2164
fe321d45
YW
2165 if (!hw_addr_is_null(&link->hw_addr)) {
2166 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2167 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2168 /* bonding master and its slaves have the same hardware address. */
2169 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2170 if (r < 0)
2171 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2172 }
0d411b7f 2173
76a86ffd
YW
2174 r = ipv4ll_update_mac(link);
2175 if (r < 0)
2176 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2177
0d411b7f 2178 r = ipv4ll_update_mac(link);
a61bb41c 2179 if (r < 0)
0d411b7f 2180 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
a61bb41c 2181
0d411b7f
YW
2182 r = dhcp4_update_mac(link);
2183 if (r < 0)
2184 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
d236718c 2185
0d411b7f
YW
2186 r = dhcp6_update_mac(link);
2187 if (r < 0)
2188 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2189
2190 r = radv_update_mac(link);
2191 if (r < 0)
2192 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2193
2194 if (link->ndisc) {
ca2b7cd8 2195 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
d236718c 2196 if (r < 0)
0d411b7f
YW
2197 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2198 }
d236718c 2199
35778343
YW
2200 if (link->lldp_rx) {
2201 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
61135582 2202 if (r < 0)
c01b9b87
YW
2203 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2204 }
2205
2206 if (link->lldp_tx) {
2207 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2208 if (r < 0)
2209 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
61135582
DS
2210 }
2211
0d411b7f
YW
2212 return 0;
2213}
a61bb41c 2214
0d411b7f 2215static int link_update_mtu(Link *link, sd_netlink_message *message) {
717ba5fc 2216 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
0d411b7f 2217 int r;
a61bb41c 2218
0d411b7f
YW
2219 assert(link);
2220 assert(message);
2221
2222 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2223 if (r == -ENODATA)
2224 return 0;
2225 if (r < 0)
2226 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
b5d0fd1e
YW
2227 if (mtu == 0)
2228 return 0;
0d411b7f 2229
717ba5fc
YW
2230 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2231 if (r < 0 && r != -ENODATA)
2232 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2233
2234 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2235 if (r < 0 && r != -ENODATA)
2236 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2237
717ba5fc
YW
2238 if (max_mtu == 0)
2239 max_mtu = UINT32_MAX;
2240
2241 link->min_mtu = min_mtu;
2242 link->max_mtu = max_mtu;
2243
0d411b7f
YW
2244 if (link->original_mtu == 0) {
2245 link->original_mtu = mtu;
717ba5fc
YW
2246 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2247 link->original_mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2248 }
2249
717ba5fc
YW
2250 if (link->mtu == mtu)
2251 return 0;
2252
b5d0fd1e
YW
2253 if (link->mtu != 0)
2254 log_link_debug(link, "MTU is changed: %"PRIu32" → %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2255 link->mtu, mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2256
2257 link->mtu = mtu;
2258
2259 if (link->dhcp_client) {
2260 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
9c0a72f9 2261 if (r < 0)
0d411b7f
YW
2262 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2263 }
a61bb41c 2264
0d411b7f
YW
2265 if (link->radv) {
2266 r = sd_radv_set_mtu(link->radv, link->mtu);
9c0a72f9 2267 if (r < 0)
0d411b7f 2268 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
a61bb41c
TG
2269 }
2270
2271 return 0;
dd3efc09 2272}
fe8db0c5 2273
0d411b7f
YW
2274static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2275 _cleanup_strv_free_ char **altnames = NULL;
0d411b7f
YW
2276 int r;
2277
2278 assert(link);
2279 assert(message);
2280
2281 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
50df02a7
YW
2282 if (r == -ENODATA)
2283 /* The message does not have IFLA_PROP_LIST container attribute. It does not means the
2284 * interface has no alternative name. */
2285 return 0;
2286 if (r < 0)
0d411b7f
YW
2287 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2288
1b345c1e
YW
2289 if (strv_equal(altnames, link->alternative_names))
2290 return 0;
2291
0d411b7f
YW
2292 STRV_FOREACH(n, link->alternative_names)
2293 hashmap_remove(link->manager->links_by_name, *n);
2294
2295 strv_free_and_replace(link->alternative_names, altnames);
2296
2297 STRV_FOREACH(n, link->alternative_names) {
2298 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2299 if (r < 0)
2300 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2301 }
2302
2303 return 0;
2304}
2305
2306static int link_update_name(Link *link, sd_netlink_message *message) {
01afd0f7 2307 char ifname_from_index[IF_NAMESIZE];
0d411b7f
YW
2308 const char *ifname;
2309 int r;
2310
2311 assert(link);
2312 assert(message);
2313
2314 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2315 if (r == -ENODATA)
7802194a 2316 /* Hmm?? But ok. */
0d411b7f
YW
2317 return 0;
2318 if (r < 0)
2319 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2320
2321 if (streq(ifname, link->ifname))
2322 return 0;
2323
01afd0f7
YW
2324 r = format_ifname(link->ifindex, ifname_from_index);
2325 if (r < 0)
2326 return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
176b8be1
YW
2327
2328 if (!streq(ifname, ifname_from_index)) {
2329 log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
2330 "with the name currently configured on the actual interface '%s'. Ignoring.",
2331 ifname, ifname_from_index);
2332 return 0;
2333 }
2334
0d411b7f
YW
2335 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2336
2337 hashmap_remove(link->manager->links_by_name, link->ifname);
2338
2339 r = free_and_strdup(&link->ifname, ifname);
2340 if (r < 0)
2341 return log_oom_debug();
2342
2343 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2344 if (r < 0)
2345 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2346
54d1fdb2
YW
2347 if (link->dhcp_client) {
2348 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2349 if (r < 0)
2350 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2351 }
2352
2353 if (link->dhcp6_client) {
2354 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2355 if (r < 0)
2356 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m");
2357 }
2358
2359 if (link->ndisc) {
2360 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2361 if (r < 0)
2362 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2363 }
2364
2365 if (link->dhcp_server) {
2366 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2367 if (r < 0)
2368 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2369 }
2370
2371 if (link->radv) {
2372 r = sd_radv_set_ifname(link->radv, link->ifname);
2373 if (r < 0)
2374 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2375 }
2376
35778343
YW
2377 if (link->lldp_rx) {
2378 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
54d1fdb2 2379 if (r < 0)
c01b9b87
YW
2380 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2381 }
2382
2383 if (link->lldp_tx) {
2384 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2385 if (r < 0)
2386 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
54d1fdb2
YW
2387 }
2388
2389 if (link->ipv4ll) {
2390 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2391 if (r < 0)
2392 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2393 }
2394
86173383
YW
2395 r = ipv4acd_set_ifname(link);
2396 if (r < 0)
2397 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
54d1fdb2 2398
0d411b7f
YW
2399 return 0;
2400}
2401
2402static int link_update(Link *link, sd_netlink_message *message) {
2403 int r;
2404
2405 assert(link);
2406 assert(message);
2407
2408 r = link_update_name(link, message);
2409 if (r < 0)
2410 return r;
2411
2412 r = link_update_alternative_names(link, message);
2413 if (r < 0)
2414 return r;
2415
2416 r = link_update_mtu(link, message);
2417 if (r < 0)
2418 return r;
2419
e1658632
YW
2420 r = link_update_driver(link, message);
2421 if (r < 0)
2422 return r;
2423
2424 r = link_update_permanent_hardware_address(link, message);
2425 if (r < 0)
2426 return r;
2427
0d411b7f
YW
2428 r = link_update_hardware_address(link, message);
2429 if (r < 0)
2430 return r;
2431
2432 r = link_update_master(link, message);
2433 if (r < 0)
2434 return r;
2435
7d0d832d
YW
2436 r = link_update_ipv6ll_addrgen_mode(link, message);
2437 if (r < 0)
2438 return r;
2439
0d411b7f
YW
2440 return link_update_flags(link, message);
2441}
2442
0c9ee5d5
YW
2443static Link *link_drop_or_unref(Link *link) {
2444 if (!link)
2445 return NULL;
2446 if (!link->manager)
2447 return link_unref(link);
2448 return link_drop(link);
2449}
2450
2451DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2452
2453static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
0d411b7f 2454 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
0c9ee5d5 2455 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
0c9ee5d5
YW
2456 unsigned short iftype;
2457 int r, ifindex;
0c9ee5d5
YW
2458
2459 assert(manager);
2460 assert(message);
2461 assert(ret);
2462
0c9ee5d5
YW
2463 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2464 if (r < 0)
0d411b7f 2465 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
0c9ee5d5 2466 else if (ifindex <= 0)
0d411b7f 2467 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
0c9ee5d5
YW
2468
2469 r = sd_rtnl_message_link_get_type(message, &iftype);
2470 if (r < 0)
0d411b7f 2471 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
0c9ee5d5
YW
2472
2473 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2474 if (r < 0)
0d411b7f 2475 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
0c9ee5d5
YW
2476
2477 /* check for link kind */
2478 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2479 if (r >= 0) {
0d411b7f
YW
2480 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2481 if (r < 0 && r != -ENODATA)
2482 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
0c9ee5d5
YW
2483 r = sd_netlink_message_exit_container(message);
2484 if (r < 0)
0d411b7f 2485 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
0c9ee5d5
YW
2486 }
2487
4c78dc17
YW
2488 if (!manager->test_mode) {
2489 /* Do not update state files when running in test mode. */
2490 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2491 return log_oom_debug();
0d411b7f 2492
4c78dc17
YW
2493 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2494 return log_oom_debug();
0d411b7f 2495
4c78dc17
YW
2496 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2497 return log_oom_debug();
2498 }
0d411b7f 2499
0c9ee5d5
YW
2500 link = new(Link, 1);
2501 if (!link)
2502 return -ENOMEM;
2503
2504 *link = (Link) {
2505 .n_ref = 1,
2506 .state = LINK_STATE_PENDING,
2507 .online_state = _LINK_ONLINE_STATE_INVALID,
2508 .ifindex = ifindex,
2509 .iftype = iftype,
2510 .ifname = TAKE_PTR(ifname),
2511 .kind = TAKE_PTR(kind),
2512
7d0d832d
YW
2513 .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2514
0d411b7f
YW
2515 .state_file = TAKE_PTR(state_file),
2516 .lease_file = TAKE_PTR(lease_file),
2517 .lldp_file = TAKE_PTR(lldp_file),
2518
0c9ee5d5
YW
2519 .n_dns = UINT_MAX,
2520 .dns_default_route = -1,
2521 .llmnr = _RESOLVE_SUPPORT_INVALID,
2522 .mdns = _RESOLVE_SUPPORT_INVALID,
2523 .dnssec_mode = _DNSSEC_MODE_INVALID,
2524 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2525 };
2526
6eab614d 2527 r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
0c9ee5d5 2528 if (r < 0)
0d411b7f 2529 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
0c9ee5d5
YW
2530
2531 link->manager = manager;
2532
0d411b7f 2533 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
0c9ee5d5 2534 if (r < 0)
0d411b7f 2535 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
0c9ee5d5 2536
8a77e245
YW
2537 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2538 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2539
0c9ee5d5 2540 *ret = TAKE_PTR(link);
0c9ee5d5
YW
2541 return 0;
2542}
2543
0d411b7f 2544int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
3be9d62a
YW
2545 Link *link = NULL;
2546 NetDev *netdev = NULL;
2547 uint16_t type;
2548 const char *name;
2549 int r, ifindex;
2550
2551 assert(rtnl);
2552 assert(message);
0d411b7f 2553 assert(manager);
3be9d62a
YW
2554
2555 if (sd_netlink_message_is_error(message)) {
2556 r = sd_netlink_message_get_errno(message);
2557 if (r < 0)
2558 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2559
2560 return 0;
2561 }
2562
2563 r = sd_netlink_message_get_type(message, &type);
2564 if (r < 0) {
2565 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2566 return 0;
2567 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2568 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2569 return 0;
2570 }
2571
2572 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2573 if (r < 0) {
2574 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2575 return 0;
2576 } else if (ifindex <= 0) {
2577 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2578 return 0;
2579 }
2580
2581 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2582 if (r < 0) {
2583 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2584 return 0;
2585 }
2586
6eab614d 2587 (void) link_get_by_index(manager, ifindex, &link);
0d411b7f 2588 (void) netdev_get(manager, name, &netdev);
3be9d62a
YW
2589
2590 switch (type) {
2591 case RTM_NEWLINK:
0d411b7f
YW
2592 if (netdev) {
2593 /* netdev exists, so make sure the ifindex matches */
2594 r = netdev_set_ifindex(netdev, message);
2595 if (r < 0) {
66141164 2596 log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
0d411b7f
YW
2597 return 0;
2598 }
2599 }
2600
3be9d62a
YW
2601 if (!link) {
2602 /* link is new, so add it */
0d411b7f 2603 r = link_new(manager, message, &link);
3be9d62a 2604 if (r < 0) {
0d411b7f 2605 log_warning_errno(r, "Could not process new link message: %m");
3be9d62a
YW
2606 return 0;
2607 }
3be9d62a 2608
0d411b7f 2609 r = link_update(link, message);
3be9d62a 2610 if (r < 0) {
66141164 2611 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f 2612 link_enter_failed(link);
3be9d62a
YW
2613 return 0;
2614 }
3be9d62a 2615
0d411b7f
YW
2616 r = link_check_initialized(link);
2617 if (r < 0) {
66141164 2618 log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
0d411b7f
YW
2619 link_enter_failed(link);
2620 return 0;
2621 }
2622 } else {
2623 r = link_update(link, message);
2624 if (r < 0) {
66141164 2625 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f
YW
2626 link_enter_failed(link);
2627 return 0;
2628 }
3be9d62a 2629 }
3be9d62a
YW
2630 break;
2631
2632 case RTM_DELLINK:
2633 link_drop(link);
2634 netdev_drop(netdev);
3be9d62a
YW
2635 break;
2636
2637 default:
04499a70 2638 assert_not_reached();
3be9d62a
YW
2639 }
2640
2641 return 1;
2642}
2643
79c6e114
YW
2644int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2645 uint16_t message_type;
2646 int r;
2647
2648 assert(m);
2649 assert(link);
2650 assert(error_msg);
2651
2652 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2653 return 0;
2654
2655 r = sd_netlink_message_get_errno(m);
2656 if (r < 0) {
2657 log_link_message_warning_errno(link, m, r, error_msg);
2658 link_enter_failed(link);
2659 return 0;
2660 }
2661
2662 r = sd_netlink_message_get_type(m, &message_type);
2663 if (r < 0) {
2664 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2665 return 0;
2666 }
2667 if (message_type != RTM_NEWLINK) {
2668 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2669 return 0;
2670 }
2671
2672 r = link_update(link, m);
2673 if (r < 0) {
2674 link_enter_failed(link);
2675 return 0;
2676 }
2677
2678 return 1;
2679}
2680
2681int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2682 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2683 int r;
2684
2685 assert(link);
2686 assert(link->manager);
2687 assert(link->manager->rtnl);
2688 assert(callback);
2689
2690 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2691 if (r < 0)
2692 return r;
2693
2694 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2695 link_netlink_destroy_callback, link);
2696 if (r < 0)
2697 return r;
2698
2699 link_ref(link);
2700 return 0;
2701}
2702
fe8db0c5 2703static const char* const link_state_table[_LINK_STATE_MAX] = {
0580badc 2704 [LINK_STATE_PENDING] = "pending",
bd08ce56 2705 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 2706 [LINK_STATE_CONFIGURING] = "configuring",
0580badc
ZJS
2707 [LINK_STATE_CONFIGURED] = "configured",
2708 [LINK_STATE_UNMANAGED] = "unmanaged",
2709 [LINK_STATE_FAILED] = "failed",
2710 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
2711};
2712
2713DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
7ae55b78
YW
2714
2715int link_flags_to_string_alloc(uint32_t flags, char **ret) {
2716 _cleanup_free_ char *str = NULL;
19aa7eeb
ZJS
2717 static const char* map[] = {
2718 [LOG2U(IFF_UP)] = "up", /* interface is up. */
2719 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid.*/
2720 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
2721 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
2722 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2723 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
2724 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
2725 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
2726 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
2727 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
2728 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
2729 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
2730 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast.*/
2731 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
2732 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
2733 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
2734 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
2735 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
2736 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
7ae55b78
YW
2737 };
2738
2739 assert(ret);
2740
2741 for (size_t i = 0; i < ELEMENTSOF(map); i++)
19aa7eeb
ZJS
2742 if (FLAGS_SET(flags, 1 << i) && map[i])
2743 if (!strextend_with_separator(&str, ",", map[i]))
2744 return -ENOMEM;
7ae55b78
YW
2745
2746 *ret = TAKE_PTR(str);
2747 return 0;
2748}
2749
2750static const char * const kernel_operstate_table[] = {
2751 [IF_OPER_UNKNOWN] = "unknown",
2752 [IF_OPER_NOTPRESENT] = "not-present",
2753 [IF_OPER_DOWN] = "down",
2754 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
2755 [IF_OPER_TESTING] = "testing",
2756 [IF_OPER_DORMANT] = "dormant",
2757 [IF_OPER_UP] = "up",
2758};
2759
2760DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);