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