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