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