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