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