]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
network: wait until the DSA master interface becomes up
[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
YW
1028 /* bonding master and its slaves have the same hardware address. */
1029 if (hashmap_get(link->manager->links_by_hw_addr, &link->hw_addr) == link)
1030 hashmap_remove(link->manager->links_by_hw_addr, &link->hw_addr);
1031
63130eb3 1032 /* The following must be called at last. */
6eab614d 1033 assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
cc2d7efc 1034 return link_unref(link);
0d4ad91d
AR
1035}
1036
5e5b137a 1037static int link_drop_foreign_config(Link *link) {
0b81225e
YW
1038 int k, r;
1039
1040 assert(link);
1041 assert(link->manager);
5e5b137a 1042
5e3bb5da
YW
1043 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
1044 * to remove loopback address or addresses used for root NFS. */
1045
1046 if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
1047 return 0;
1048 if (FLAGS_SET(link->flags, IFF_LOOPBACK))
1049 return 0;
1050 if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
1051 return 0;
1052
e36d601c 1053 r = link_drop_foreign_routes(link);
5e5b137a 1054
e36d601c 1055 k = link_drop_foreign_nexthops(link);
0b81225e
YW
1056 if (k < 0 && r >= 0)
1057 r = k;
1058
e36d601c 1059 k = link_drop_foreign_addresses(link);
0b81225e
YW
1060 if (k < 0 && r >= 0)
1061 r = k;
d1bdafd2 1062
e36d601c 1063 k = link_drop_foreign_neighbors(link);
25b82b6e
YW
1064 if (k < 0 && r >= 0)
1065 r = k;
1066
0b81225e
YW
1067 k = manager_drop_foreign_routing_policy_rules(link->manager);
1068 if (k < 0 && r >= 0)
1069 r = k;
1070
1071 return r;
5e5b137a
TG
1072}
1073
3104883d 1074static int link_drop_config(Link *link) {
0b81225e
YW
1075 int k, r;
1076
1077 assert(link);
1078 assert(link->manager);
3104883d 1079
e36d601c 1080 r = link_drop_routes(link);
3104883d 1081
e36d601c 1082 k = link_drop_nexthops(link);
0b81225e
YW
1083 if (k < 0 && r >= 0)
1084 r = k;
d1bdafd2 1085
e36d601c 1086 k = link_drop_addresses(link);
0b81225e
YW
1087 if (k < 0 && r >= 0)
1088 r = k;
1089
e36d601c 1090 k = link_drop_neighbors(link);
25b82b6e
YW
1091 if (k < 0 && r >= 0)
1092 r = k;
1093
756f1f5f 1094 k = link_drop_routing_policy_rules(link);
0b81225e
YW
1095 if (k < 0 && r >= 0)
1096 r = k;
3104883d 1097
c69305ff
LP
1098 ndisc_flush(link);
1099
0b81225e 1100 return r;
3104883d
SS
1101}
1102
b4564f4e
YW
1103static void link_foreignize_config(Link *link) {
1104 assert(link);
1105 assert(link->manager);
1106
1107 link_foreignize_routes(link);
1108 link_foreignize_nexthops(link);
1109 link_foreignize_addresses(link);
1110 link_foreignize_neighbors(link);
1111 link_foreignize_routing_policy_rules(link);
1112}
1113
c3b94251 1114static int link_configure(Link *link) {
4ecdcb07
YW
1115 int r;
1116
ef1ba606 1117 assert(link);
b22d8a00 1118 assert(link->network);
bd08ce56 1119 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 1120
1187fc33
YW
1121 link_set_state(link, LINK_STATE_CONFIGURING);
1122
34658df2 1123 r = link_configure_traffic_control(link);
4ecdcb07
YW
1124 if (r < 0)
1125 return r;
1126
7558f9e7
YW
1127 if (link->iftype == ARPHRD_CAN) {
1128 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1129 r = link_request_to_set_can(link);
1130 if (r < 0)
1131 return r;
1132
1133 return link_request_to_activate(link);
1134 }
1135
518cd6b5
SS
1136 r = link_configure_sr_iov(link);
1137 if (r < 0)
1138 return r;
1139
5e0534f1 1140 r = link_set_sysctl(link);
b69c3180
SS
1141 if (r < 0)
1142 return r;
8749cbcd 1143
d05c332c 1144 r = link_request_to_set_mac(link, /* allow_retry = */ true);
e16e4b3b
DS
1145 if (r < 0)
1146 return r;
1147
72e65e6f
YW
1148 r = link_request_to_set_ipoib(link);
1149 if (r < 0)
1150 return r;
1151
1187fc33 1152 r = link_request_to_set_flags(link);
e16e4b3b
DS
1153 if (r < 0)
1154 return r;
1155
1187fc33 1156 r = link_request_to_set_group(link);
99d2baa2
SS
1157 if (r < 0)
1158 return r;
1159
1187fc33
YW
1160 r = link_configure_mtu(link);
1161 if (r < 0)
1162 return r;
1163
1164 r = link_request_to_set_addrgen_mode(link);
1165 if (r < 0)
1166 return r;
1167
1168 r = link_request_to_set_master(link);
1169 if (r < 0)
1170 return r;
1171
1172 r = link_request_stacked_netdevs(link);
1173 if (r < 0)
1174 return r;
1175
1176 r = link_request_to_set_bond(link);
1177 if (r < 0)
1178 return r;
1179
1180 r = link_request_to_set_bridge(link);
1181 if (r < 0)
1182 return r;
1183
1184 r = link_request_to_set_bridge_vlan(link);
1185 if (r < 0)
1186 return r;
1187
1188 r = link_request_to_activate(link);
89fe6535
SS
1189 if (r < 0)
1190 return r;
1191
2ffd6d73
YW
1192 r = ipv4ll_configure(link);
1193 if (r < 0)
1194 return r;
64b21ece 1195
ccffa166 1196 r = link_request_dhcp4_client(link);
2ffd6d73
YW
1197 if (r < 0)
1198 return r;
eb34d4af 1199
ccffa166 1200 r = link_request_dhcp6_client(link);
2ffd6d73
YW
1201 if (r < 0)
1202 return r;
f5a8c43f 1203
2ffd6d73
YW
1204 r = ndisc_configure(link);
1205 if (r < 0)
1206 return r;
4138fb2c 1207
1d28a3cf
YW
1208 r = link_request_dhcp_server(link);
1209 if (r < 0)
1210 return r;
1211
a254fab2 1212 r = link_request_radv(link);
086b8853
YW
1213 if (r < 0)
1214 return r;
7465dd22 1215
2ffd6d73
YW
1216 r = link_lldp_rx_configure(link);
1217 if (r < 0)
1218 return r;
ce43e484 1219
c01b9b87
YW
1220 r = link_lldp_tx_configure(link);
1221 if (r < 0)
1222 return r;
1223
5e3bb5da
YW
1224 r = link_drop_foreign_config(link);
1225 if (r < 0)
1226 return r;
4c649652 1227
1187fc33
YW
1228 r = link_request_static_configs(link);
1229 if (r < 0)
1230 return r;
1231
1232 if (!link_has_carrier(link))
1233 return 0;
1234
1235 return link_acquire_dynamic_conf(link);
505f8da7
TG
1236}
1237
170e88c8
YW
1238static int link_get_network(Link *link, Network **ret) {
1239 Network *network;
1a3caa49 1240 int r;
170e88c8
YW
1241
1242 assert(link);
1243 assert(link->manager);
1244 assert(ret);
1245
1246 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1247 bool warn = false;
1248
1a3caa49 1249 r = net_match_config(
170e88c8
YW
1250 &network->match,
1251 link->sd_device,
52135071
YW
1252 &link->hw_addr,
1253 &link->permanent_hw_addr,
170e88c8
YW
1254 link->driver,
1255 link->iftype,
1256 link->ifname,
1257 link->alternative_names,
1258 link->wlan_iftype,
1259 link->ssid,
1a3caa49
YW
1260 &link->bssid);
1261 if (r < 0)
1262 return r;
1263 if (r == 0)
170e88c8
YW
1264 continue;
1265
1266 if (network->match.ifname && link->sd_device) {
1267 uint8_t name_assign_type = NET_NAME_UNKNOWN;
1268 const char *attr;
1269
1270 if (sd_device_get_sysattr_value(link->sd_device, "name_assign_type", &attr) >= 0)
1271 (void) safe_atou8(attr, &name_assign_type);
1272
1273 warn = name_assign_type == NET_NAME_ENUM;
1274 }
1275
1276 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1277 "found matching network '%s'%s.",
1278 network->filename,
1279 warn ? ", based on potentially unpredictable interface name" : "");
1280
1281 if (network->unmanaged)
1282 return -ENOENT;
1283
1284 *ret = network;
1285 return 0;
1286 }
1287
1288 return -ENOENT;
1289}
1290
7f80fa12 1291static int link_reconfigure_impl(Link *link, bool force) {
cb71e9c6 1292 Network *network = NULL;
ad932b15
YW
1293 int r;
1294
5a1860f7 1295 assert(link);
572b21d9 1296
170e88c8 1297 r = link_get_network(link, &network);
cb71e9c6 1298 if (r < 0 && r != -ENOENT)
ad932b15
YW
1299 return r;
1300
99b8517c 1301 if (link->network == network && !force)
ad932b15
YW
1302 return 0;
1303
cb71e9c6
YW
1304 if (network)
1305 log_link_info(link, "Reconfiguring with %s.", network->filename);
1306 else
1307 log_link_info(link, "Unmanaging interface.");
ad932b15
YW
1308
1309 /* Dropping old .network file */
2a99eed0 1310 r = link_stop_engines(link, false);
572b21d9 1311 if (r < 0)
ad932b15 1312 return r;
ad932b15 1313
19d9a5ad
YW
1314 link_drop_requests(link);
1315
b4564f4e
YW
1316 if (network && !force)
1317 /* When a new/updated .network file is assigned, first make all configs (addresses,
1318 * routes, and so on) foreign, and then drop unnecessary configs later by
1319 * link_drop_foreign_config() in link_configure(). */
1320 link_foreignize_config(link);
1321 else {
1322 r = link_drop_config(link);
1323 if (r < 0)
1324 return r;
1325 }
ad932b15 1326
ad932b15
YW
1327 link_free_carrier_maps(link);
1328 link_free_engines(link);
1329 link->network = network_unref(link->network);
1330
cb71e9c6
YW
1331 if (!network) {
1332 link_set_state(link, LINK_STATE_UNMANAGED);
1333 return 0;
1334 }
1335
ad932b15 1336 /* Then, apply new .network file */
9092113d 1337 link->network = network_ref(network);
b156a95d 1338 link_update_operstate(link, true);
9092113d 1339 link_dirty(link);
ad932b15
YW
1340
1341 r = link_new_carrier_maps(link);
1342 if (r < 0)
1343 return r;
1344
1345 link_set_state(link, LINK_STATE_INITIALIZED);
61135582 1346 link->activated = false;
ad932b15 1347
ad932b15
YW
1348 r = link_configure(link);
1349 if (r < 0)
1350 return r;
1351
0e397560 1352 return 1;
ad932b15
YW
1353}
1354
96f5f9ef 1355static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force) {
572b21d9
YW
1356 int r;
1357
d09a179e
YW
1358 assert(link);
1359
5a1860f7
YW
1360 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1361 if (r <= 0)
1362 return r;
1363
7f80fa12 1364 r = link_reconfigure_impl(link, force);
d09a179e 1365 if (r < 0) {
572b21d9 1366 link_enter_failed(link);
d09a179e
YW
1367 return 0;
1368 }
572b21d9 1369
d09a179e 1370 return r;
572b21d9
YW
1371}
1372
5a1860f7 1373static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1374 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
5a1860f7 1375}
572b21d9 1376
5a1860f7 1377static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1378 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ true);
572b21d9
YW
1379}
1380
d09a179e 1381static int link_reconfigure_after_sleep_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
572b21d9
YW
1382 int r;
1383
d09a179e
YW
1384 assert(link);
1385
96f5f9ef 1386 r = link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
d09a179e
YW
1387 if (r != 0)
1388 return r;
1389
1390 /* r == 0 means an error occurs, the link is unmanaged, or the matching network file is unchanged. */
1391 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1392 return 0;
1393
1394 /* re-request static configs, and restart engines. */
1395 r = link_stop_engines(link, false);
1396 if (r < 0) {
1397 link_enter_failed(link);
1398 return 0;
1399 }
1400
1401 r = link_acquire_dynamic_conf(link);
1402 if (r < 0) {
1403 link_enter_failed(link);
1404 return 0;
1405 }
1406
1407 r = link_request_static_configs(link);
1408 if (r < 0) {
1409 link_enter_failed(link);
1410 return 0;
1411 }
1412
1413 return 0;
1414}
1415
1416static int link_reconfigure_internal(Link *link, link_netlink_message_handler_t callback) {
1417 int r;
1418
1419 assert(link);
1420 assert(callback);
1421
8bceafa7 1422 /* When link in pending or initialized state, then link_configure() will be called. To prevent
f8dd4077
ZJS
1423 * the function from being called multiple times simultaneously, refuse to reconfigure the
1424 * interface in these cases. */
8bceafa7 1425 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
f8dd4077 1426 return 0; /* 0 means no-op. */
2c0d7ed3 1427
d09a179e 1428 r = link_call_getlink(link, callback);
572b21d9
YW
1429 if (r < 0)
1430 return r;
1431
8bceafa7 1432 return 1; /* 1 means the interface will be reconfigured. */
572b21d9
YW
1433}
1434
d09a179e
YW
1435int link_reconfigure(Link *link, bool force) {
1436 return link_reconfigure_internal(link, force ? link_force_reconfigure_handler : link_reconfigure_handler);
1437}
1438
1439int link_reconfigure_after_sleep(Link *link) {
1440 return link_reconfigure_internal(link, link_reconfigure_after_sleep_handler);
1441}
1442
e6bf7774 1443static int link_initialized_and_synced(Link *link) {
505f8da7 1444 Network *network;
505f8da7
TG
1445 int r;
1446
1447 assert(link);
1448 assert(link->ifname);
1449 assert(link->manager);
1450
faa2e64f
YW
1451 if (link->manager->test_mode) {
1452 log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1453 link_set_state(link, LINK_STATE_UNMANAGED);
1454 return 0;
1455 }
1456
bd08ce56 1457 /* We may get called either from the asynchronous netlink callback,
0d411b7f 1458 * or directly from link_check_initialized() if running in a container. */
bd08ce56 1459 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
4232cf04 1460 return 0;
505f8da7 1461
6a7a4e4d 1462 log_link_debug(link, "Link state is up-to-date");
bd08ce56 1463 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 1464
0d4ad91d
AR
1465 r = link_new_bound_by_list(link);
1466 if (r < 0)
1467 return r;
1468
1469 r = link_handle_bound_by_list(link);
1470 if (r < 0)
1471 return r;
1472
c4a03a56 1473 if (!link->network) {
170e88c8 1474 r = link_get_network(link, &network);
c4a03a56 1475 if (r == -ENOENT) {
29836c16 1476 link_set_state(link, LINK_STATE_UNMANAGED);
4232cf04 1477 return 0;
170e88c8
YW
1478 }
1479 if (r < 0)
c4a03a56 1480 return r;
505f8da7 1481
c4a03a56
TG
1482 if (link->flags & IFF_LOOPBACK) {
1483 if (network->link_local != ADDRESS_FAMILY_NO)
1484 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
78c958f8 1485
c4a03a56
TG
1486 if (network->dhcp != ADDRESS_FAMILY_NO)
1487 log_link_debug(link, "Ignoring DHCP clients for loopback link");
78c958f8 1488
c4a03a56
TG
1489 if (network->dhcp_server)
1490 log_link_debug(link, "Ignoring DHCP server for loopback link");
1491 }
bd2efe92 1492
9092113d 1493 link->network = network_ref(network);
bcdcc596 1494 link_update_operstate(link, false);
9092113d 1495 link_dirty(link);
c4a03a56 1496 }
505f8da7 1497
0d4ad91d
AR
1498 r = link_new_bound_to_list(link);
1499 if (r < 0)
1500 return r;
1501
852a3916 1502 return link_configure(link);
505f8da7
TG
1503}
1504
302a796f 1505static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4ff296b0
YW
1506 int r;
1507
5a1860f7
YW
1508 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1509 if (r <= 0)
0b54c870 1510 return r;
572b21d9 1511
4ff296b0
YW
1512 r = link_initialized_and_synced(link);
1513 if (r < 0)
1514 link_enter_failed(link);
5a1860f7
YW
1515
1516 return 0;
e6bf7774
YW
1517}
1518
3be9d62a 1519static int link_initialized(Link *link, sd_device *device) {
4f561e8e 1520 assert(link);
4f561e8e
TG
1521 assert(device);
1522
160203e9
YW
1523 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1524 * or sysattrs) may be outdated. */
1525 sd_device_ref(device);
1526 sd_device_unref(link->sd_device);
1527 link->sd_device = device;
4f561e8e 1528
160203e9
YW
1529 /* Do not ignore unamanaged state case here. If an interface is renamed after being once
1530 * configured, and the corresponding .network file has Name= in [Match] section, then the
1531 * interface may be already in unmanaged state. See #20657. */
1532 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED))
679b3605
TG
1533 return 0;
1534
79008bdd 1535 log_link_debug(link, "udev initialized link");
bd08ce56 1536 link_set_state(link, LINK_STATE_INITIALIZED);
4f561e8e 1537
3c9b8860
TG
1538 /* udev has initialized the link, but we don't know if we have yet
1539 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1540 * when it returns we know that the pending NEWLINKs have already been
1541 * processed and that we are up-to-date */
4f561e8e 1542
5a1860f7 1543 return link_call_getlink(link, link_initialized_handler);
cc2d7efc
YW
1544}
1545
0d411b7f 1546static int link_check_initialized(Link *link) {
51517f9e 1547 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
5a937ea2 1548 int r;
505f8da7 1549
0d411b7f 1550 assert(link);
505f8da7 1551
0d411b7f 1552 if (path_is_read_only_fs("/sys") > 0)
852a3916 1553 /* no udev */
0d411b7f 1554 return link_initialized_and_synced(link);
852a3916
YW
1555
1556 /* udev should be around */
0ac655a6 1557 r = sd_device_new_from_ifindex(&device, link->ifindex);
852a3916
YW
1558 if (r < 0) {
1559 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1560 return 0;
1561 }
1562
1563 r = sd_device_get_is_initialized(device);
0d411b7f
YW
1564 if (r < 0)
1565 return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
852a3916
YW
1566 if (r == 0) {
1567 /* not yet ready */
1568 log_link_debug(link, "link pending udev initialization...");
1569 return 0;
1570 }
1571
1572 r = device_is_renaming(device);
0d411b7f
YW
1573 if (r < 0)
1574 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
852a3916
YW
1575 if (r > 0) {
1576 log_link_debug(link, "Interface is being renamed, pending initialization.");
1577 return 0;
4f561e8e 1578 }
505f8da7 1579
0d411b7f 1580 return link_initialized(link, device);
a748b692
TG
1581}
1582
3be9d62a
YW
1583int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
1584 sd_device_action_t action;
1585 Manager *m = userdata;
1586 Link *link = NULL;
1587 int r, ifindex;
1588
1589 assert(m);
1590 assert(device);
1591
1592 r = sd_device_get_action(device, &action);
1593 if (r < 0) {
1594 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
1595 return 0;
1596 }
1597
1598 /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
1599 * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
1600 * still exists — and we are interested in that. */
1601 if (action == SD_DEVICE_REMOVE)
1602 return 0;
1603
1604 r = sd_device_get_ifindex(device, &ifindex);
1605 if (r < 0) {
1606 log_device_debug_errno(device, r, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m",
1607 device_action_to_string(action));
1608 return 0;
1609 }
1610
1611 r = device_is_renaming(device);
1612 if (r < 0) {
4b9a8c2b 1613 log_device_debug_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
3be9d62a
YW
1614 device_action_to_string(action));
1615 return 0;
1616 }
1617 if (r > 0) {
1618 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed.");
1619 return 0;
1620 }
1621
6eab614d 1622 r = link_get_by_index(m, ifindex, &link);
3be9d62a 1623 if (r < 0) {
4b9a8c2b 1624 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
3be9d62a
YW
1625 return 0;
1626 }
1627
4b9a8c2b
YW
1628 r = link_initialized(link, device);
1629 if (r < 0)
1630 link_enter_failed(link);
3be9d62a
YW
1631
1632 return 0;
1633}
1634
9c0a72f9 1635static int link_carrier_gained(Link *link) {
6706ce2f 1636 bool force_reconfigure;
9c0a72f9
TG
1637 int r;
1638
1639 assert(link);
1640
6706ce2f
YW
1641 r = event_source_disable(link->carrier_lost_timer);
1642 if (r < 0)
1643 log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
1644
d9910948
YW
1645 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1646 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1647 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1648 * restarted, and the correct network information will be gained.
1649 *
1650 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1651 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1652 * already assigned to the interface (in that case, the .network file does not have the SSID=
1653 * setting in the [Match] section), and the interface is already being configured. Of course,
1654 * there may exist another .network file with higher priority and a matching SSID= setting. But
1655 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1656 *
6706ce2f 1657 * For non-wireless interfaces, we have no way to detect the connected network change. So,
d9910948
YW
1658 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1659 force_reconfigure = link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid);
6706ce2f
YW
1660 link->previous_ssid = mfree(link->previous_ssid);
1661
a72d2a7b 1662 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
b7ac1284
YW
1663 /* At this stage, both wlan and link information should be up-to-date. Hence,
1664 * it is not necessary to call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or
1665 * NL80211_CMD_GET_STATION commands, and simply call link_reconfigure_impl().
1666 * Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
6706ce2f 1667 r = link_reconfigure_impl(link, force_reconfigure);
b7ac1284
YW
1668 if (r != 0)
1669 return r;
1670 }
1671
aa9117c0
YW
1672 r = link_handle_bound_by_list(link);
1673 if (r < 0)
1674 return r;
1675
600b7898
YW
1676 if (link->iftype == ARPHRD_CAN)
1677 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0 1678 return 0;
600b7898 1679
b9ea3d2e 1680 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
8566df79 1681 r = link_acquire_dynamic_conf(link);
f0269653 1682 if (r < 0)
9c0a72f9 1683 return r;
6fc25497 1684
97108953 1685 r = link_request_static_configs(link);
6fc25497
SS
1686 if (r < 0)
1687 return r;
9c0a72f9
TG
1688 }
1689
aa9117c0 1690 return 0;
9c0a72f9
TG
1691}
1692
6706ce2f
YW
1693static int link_carrier_lost_impl(Link *link) {
1694 int r, ret = 0;
1695
1696 assert(link);
1697
1698 link->previous_ssid = mfree(link->previous_ssid);
1699
1700 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1701 return 0;
1702
1703 if (!link->network)
1704 return 0;
1705
1706 r = link_stop_engines(link, false);
1707 if (r < 0)
1708 ret = r;
1709
1710 r = link_drop_config(link);
1711 if (r < 0 && ret >= 0)
1712 ret = r;
1713
1714 return ret;
1715}
1716
1717static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
1718 Link *link = userdata;
1719 int r;
1720
1721 assert(link);
1722
1723 r = link_carrier_lost_impl(link);
1724 if (r < 0) {
1725 log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
1726 link_enter_failed(link);
1727 }
1728
1729 return 0;
1730}
1731
9c0a72f9
TG
1732static int link_carrier_lost(Link *link) {
1733 int r;
1734
1735 assert(link);
1736
aa9117c0
YW
1737 r = link_handle_bound_by_list(link);
1738 if (r < 0)
1739 return r;
93b4dab5 1740
600b7898
YW
1741 if (link->iftype == ARPHRD_CAN)
1742 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0
YW
1743 return 0;
1744
f1c22cf4
YW
1745 if (!link->network)
1746 return 0;
1747
6706ce2f 1748 if (link->network->ignore_carrier_loss_usec == USEC_INFINITY)
aa9117c0 1749 return 0;
600b7898 1750
6706ce2f
YW
1751 if (link->network->ignore_carrier_loss_usec == 0)
1752 return link_carrier_lost_impl(link);
1753
1754 return event_reset_time_relative(link->manager->event,
1755 &link->carrier_lost_timer,
1756 clock_boottime_or_monotonic(),
1757 link->network->ignore_carrier_loss_usec,
1758 0,
1759 link_carrier_lost_handler,
1760 link,
1761 0,
1762 "link-carrier-loss",
1763 true);
0c9ee5d5
YW
1764}
1765
0c9ee5d5
YW
1766static int link_admin_state_up(Link *link) {
1767 int r;
1768
1769 assert(link);
1770
1771 /* This is called every time an interface admin state changes to up;
1772 * specifically, when IFF_UP flag changes from unset to set. */
1773
1774 if (!link->network)
1775 return 0;
1776
899b0e5e 1777 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
712fd5d2
YW
1778 log_link_info(link, "ActivationPolicy is \"always-off\", forcing link down.");
1779 return link_request_to_bring_up_or_down(link, /* up = */ false);
0c9ee5d5
YW
1780 }
1781
1782 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1783 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1784 r = link_set_ipv6_mtu(link);
1785 if (r < 0)
1786 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1787
1788 return 0;
1789}
1790
1791static int link_admin_state_down(Link *link) {
1792 assert(link);
1793
1794 if (!link->network)
1795 return 0;
1796
899b0e5e 1797 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
712fd5d2
YW
1798 log_link_info(link, "ActivationPolicy is \"always-on\", forcing link up.");
1799 return link_request_to_bring_up_or_down(link, /* up = */ true);
0c9ee5d5
YW
1800 }
1801
1802 return 0;
1803}
1804
1805bool link_has_carrier(Link *link) {
1806 /* see Documentation/networking/operstates.txt in the kernel sources */
1807
1808 if (link->kernel_operstate == IF_OPER_UP)
1809 return true;
1810
1811 if (link->kernel_operstate == IF_OPER_UNKNOWN)
1812 /* operstate may not be implemented, so fall back to flags */
1813 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1814 !FLAGS_SET(link->flags, IFF_DORMANT))
1815 return true;
1816
1817 return false;
1818}
1819
1820static bool link_is_enslaved(Link *link) {
1821 if (link->flags & IFF_SLAVE)
0c9ee5d5
YW
1822 return true;
1823
1f024462 1824 if (link->master_ifindex > 0)
0c9ee5d5
YW
1825 return true;
1826
0c9ee5d5
YW
1827 return false;
1828}
1829
1830static LinkAddressState address_state_from_scope(uint8_t scope) {
1831 if (scope < RT_SCOPE_SITE)
1832 /* universally accessible addresses found */
1833 return LINK_ADDRESS_STATE_ROUTABLE;
1834
1835 if (scope < RT_SCOPE_HOST)
1836 /* only link or site local addresses found */
1837 return LINK_ADDRESS_STATE_DEGRADED;
1838
1839 /* no useful addresses found */
1840 return LINK_ADDRESS_STATE_OFF;
1841}
1842
1843void link_update_operstate(Link *link, bool also_update_master) {
1844 LinkOperationalState operstate;
1845 LinkCarrierState carrier_state;
1846 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1847 LinkOnlineState online_state;
1848 _cleanup_strv_free_ char **p = NULL;
1849 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
1850 bool changed = false;
1851 Address *address;
1852
1853 assert(link);
1854
1855 if (link->kernel_operstate == IF_OPER_DORMANT)
1856 carrier_state = LINK_CARRIER_STATE_DORMANT;
1857 else if (link_has_carrier(link)) {
1858 if (link_is_enslaved(link))
1859 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1860 else
1861 carrier_state = LINK_CARRIER_STATE_CARRIER;
1862 } else if (link->flags & IFF_UP)
1863 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1864 else
1865 carrier_state = LINK_CARRIER_STATE_OFF;
1866
1867 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1868 Link *slave;
1869
1870 SET_FOREACH(slave, link->slaves) {
1871 link_update_operstate(slave, false);
1872
1873 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1874 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1875 }
1876 }
1877
1878 SET_FOREACH(address, link->addresses) {
1879 if (!address_is_ready(address))
1880 continue;
1881
1882 if (address->family == AF_INET)
1883 ipv4_scope = MIN(ipv4_scope, address->scope);
1884
1885 if (address->family == AF_INET6)
1886 ipv6_scope = MIN(ipv6_scope, address->scope);
1887 }
1888
0c9ee5d5
YW
1889 ipv4_address_state = address_state_from_scope(ipv4_scope);
1890 ipv6_address_state = address_state_from_scope(ipv6_scope);
1891 address_state = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
1892
1893 /* Mapping of address and carrier state vs operational state
1894 * carrier state
1895 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1896 * ------------------------------------------------------------------------------
1897 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1898 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1899 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1900 */
1901
1902 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
1903 operstate = (LinkOperationalState) carrier_state;
1904 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1905 operstate = LINK_OPERSTATE_ROUTABLE;
1906 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1907 operstate = LINK_OPERSTATE_DEGRADED;
1908 else
1909 operstate = LINK_OPERSTATE_ENSLAVED;
1910
1911 /* Only determine online state for managed links with RequiredForOnline=yes */
1912 if (!link->network || !link->network->required_for_online)
1913 online_state = _LINK_ONLINE_STATE_INVALID;
1914 else if (operstate < link->network->required_operstate_for_online.min ||
1915 operstate > link->network->required_operstate_for_online.max)
1916 online_state = LINK_ONLINE_STATE_OFFLINE;
1917 else {
1918 AddressFamily required_family = link->network->required_family_for_online;
1919 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1920 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1921
1922 /* The operational state is within the range required for online.
1923 * If a particular address family is also required, we might revert
1924 * to offline in the blocks below. */
1925 online_state = LINK_ONLINE_STATE_ONLINE;
1926
1927 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) {
1928 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1929 online_state = LINK_ONLINE_STATE_OFFLINE;
1930 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1931 online_state = LINK_ONLINE_STATE_OFFLINE;
1932 }
1933
1934 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) {
1935 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1936 online_state = LINK_ONLINE_STATE_OFFLINE;
1937 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1938 online_state = LINK_ONLINE_STATE_OFFLINE;
1939 }
1940 }
1941
1942 if (link->carrier_state != carrier_state) {
1943 link->carrier_state = carrier_state;
1944 changed = true;
1945 if (strv_extend(&p, "CarrierState") < 0)
1946 log_oom();
1947 }
1948
1949 if (link->address_state != address_state) {
1950 link->address_state = address_state;
1951 changed = true;
1952 if (strv_extend(&p, "AddressState") < 0)
1953 log_oom();
1954 }
0d4ad91d 1955
0c9ee5d5
YW
1956 if (link->ipv4_address_state != ipv4_address_state) {
1957 link->ipv4_address_state = ipv4_address_state;
1958 changed = true;
1959 if (strv_extend(&p, "IPv4AddressState") < 0)
1960 log_oom();
1961 }
9c0a72f9 1962
0c9ee5d5
YW
1963 if (link->ipv6_address_state != ipv6_address_state) {
1964 link->ipv6_address_state = ipv6_address_state;
1965 changed = true;
1966 if (strv_extend(&p, "IPv6AddressState") < 0)
1967 log_oom();
1968 }
9c0a72f9 1969
0c9ee5d5
YW
1970 if (link->operstate != operstate) {
1971 link->operstate = operstate;
1972 changed = true;
1973 if (strv_extend(&p, "OperationalState") < 0)
1974 log_oom();
1975 }
9c0a72f9 1976
0c9ee5d5
YW
1977 if (link->online_state != online_state) {
1978 link->online_state = online_state;
1979 changed = true;
1980 if (strv_extend(&p, "OnlineState") < 0)
1981 log_oom();
1982 }
9c0a72f9 1983
0c9ee5d5
YW
1984 if (p)
1985 link_send_changed_strv(link, p);
1986 if (changed)
1987 link_dirty(link);
9c0a72f9 1988
0c9ee5d5
YW
1989 if (also_update_master) {
1990 Link *master;
9c0a72f9 1991
0c9ee5d5
YW
1992 if (link_get_master(link, &master) >= 0)
1993 link_update_operstate(master, true);
1994 }
9c0a72f9
TG
1995}
1996
0c9ee5d5
YW
1997#define FLAG_STRING(string, flag, old, new) \
1998 (((old ^ new) & flag) \
1999 ? ((old & flag) ? (" -" string) : (" +" string)) \
2000 : "")
2001
0d411b7f 2002static int link_update_flags(Link *link, sd_netlink_message *message) {
96f5f9ef 2003 bool link_was_admin_up, had_carrier;
0c9ee5d5
YW
2004 uint8_t operstate;
2005 unsigned flags;
d236718c
DS
2006 int r;
2007
61135582 2008 assert(link);
0d411b7f 2009 assert(message);
61135582 2010
0d411b7f 2011 r = sd_rtnl_message_link_get_flags(message, &flags);
0c9ee5d5 2012 if (r < 0)
0d411b7f 2013 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
3be9d62a 2014
0d411b7f
YW
2015 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
2016 if (r == -ENODATA)
2017 /* If we got a message without operstate, assume the state was unchanged. */
0c9ee5d5 2018 operstate = link->kernel_operstate;
0d411b7f
YW
2019 else if (r < 0)
2020 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
0c9ee5d5 2021
0d411b7f 2022 if (link->flags == flags && link->kernel_operstate == operstate)
61135582
DS
2023 return 0;
2024
0c9ee5d5
YW
2025 if (link->flags != flags) {
2026 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
61135582 2027
0c9ee5d5
YW
2028 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",
2029 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2030 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2031 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2032 FLAG_STRING("UP", IFF_UP, link->flags, flags),
2033 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2034 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2035 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2036 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2037 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2038 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2039 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2040 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2041 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2042 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2043 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2044 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2045 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2046 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2047 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
d236718c 2048
0c9ee5d5
YW
2049 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
2050 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2051 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2052 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2053 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2054 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2055 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
2056 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
d236718c 2057
0c9ee5d5
YW
2058 if (unknown_flags_added)
2059 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
61135582 2060
0c9ee5d5
YW
2061 if (unknown_flags_removed)
2062 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
2063 }
61135582 2064
0d411b7f
YW
2065 link_was_admin_up = link->flags & IFF_UP;
2066 had_carrier = link_has_carrier(link);
2067
0c9ee5d5
YW
2068 link->flags = flags;
2069 link->kernel_operstate = operstate;
36161cba 2070
0c9ee5d5 2071 link_update_operstate(link, true);
61135582 2072
0d411b7f
YW
2073 if (!link_was_admin_up && (link->flags & IFF_UP)) {
2074 log_link_info(link, "Link UP");
22936833 2075
0d411b7f
YW
2076 r = link_admin_state_up(link);
2077 if (r < 0)
2078 return r;
2079 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
2080 log_link_info(link, "Link DOWN");
0d4ad91d 2081
0d411b7f 2082 r = link_admin_state_down(link);
0d4ad91d
AR
2083 if (r < 0)
2084 return r;
7619683b
TG
2085 }
2086
0d411b7f
YW
2087 if (!had_carrier && link_has_carrier(link)) {
2088 log_link_info(link, "Gained carrier");
0d4ad91d 2089
0d411b7f 2090 r = link_carrier_gained(link);
30de2b89
YW
2091 if (r < 0)
2092 return r;
0d411b7f
YW
2093 } else if (had_carrier && !link_has_carrier(link)) {
2094 log_link_info(link, "Lost carrier");
2095
2096 r = link_carrier_lost(link);
0b54c870
YW
2097 if (r < 0)
2098 return r;
b8941f74
TG
2099 }
2100
0d411b7f
YW
2101 return 0;
2102}
afe7fd56 2103
0d411b7f
YW
2104static int link_update_master(Link *link, sd_netlink_message *message) {
2105 int master_ifindex, r;
7465dd22 2106
0d411b7f
YW
2107 assert(link);
2108 assert(message);
69629de9 2109
0d411b7f
YW
2110 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
2111 if (r == -ENODATA)
2112 return 0;
2113 if (r < 0)
2114 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
807667f7 2115
571bf1aa
YW
2116 if (master_ifindex == link->ifindex)
2117 master_ifindex = 0;
2118
0d411b7f
YW
2119 if (master_ifindex == link->master_ifindex)
2120 return 0;
807667f7 2121
0d411b7f
YW
2122 if (link->master_ifindex == 0)
2123 log_link_debug(link, "Joined to master interface: %i", master_ifindex);
2124 else if (master_ifindex == 0)
2125 log_link_debug(link, "Leaved from master interface: %i", link->master_ifindex);
2126 else
2127 log_link_debug(link, "Master interface is changed: %i → %i", link->master_ifindex, master_ifindex);
807667f7 2128
0d411b7f 2129 link_drop_from_master(link);
807667f7 2130
0d411b7f 2131 link->master_ifindex = master_ifindex;
413708d1 2132
0d411b7f
YW
2133 r = link_append_to_master(link);
2134 if (r < 0)
2135 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
7465dd22 2136
0d411b7f
YW
2137 return 0;
2138}
807667f7 2139
0d411b7f 2140static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
e2bacccd 2141 struct hw_addr_data addr;
0d411b7f 2142 int r;
d93d655c 2143
0d411b7f
YW
2144 assert(link);
2145 assert(message);
dfc58b47 2146
0d411b7f
YW
2147 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2148 if (r < 0 && r != -ENODATA)
2149 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
4f882b2a 2150
e2bacccd 2151 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
0d411b7f
YW
2152 if (r == -ENODATA)
2153 return 0;
2154 if (r < 0)
fe321d45 2155 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
bb262ef0 2156
e2bacccd 2157 if (hw_addr_equal(&link->hw_addr, &addr))
0d411b7f 2158 return 0;
a61bb41c 2159
de0f1579 2160 if (link->hw_addr.length == 0)
e2bacccd 2161 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
fe321d45
YW
2162 else {
2163 log_link_debug(link, "Hardware address is changed: %s → %s",
e2bacccd 2164 HW_ADDR_TO_STR(&link->hw_addr), HW_ADDR_TO_STR(&addr));
0d411b7f 2165
e2bacccd
YW
2166 if (hashmap_get(link->manager->links_by_hw_addr, &link->hw_addr) == link)
2167 hashmap_remove(link->manager->links_by_hw_addr, &link->hw_addr);
fe321d45
YW
2168 }
2169
e2bacccd
YW
2170 link->hw_addr = addr;
2171
fe321d45
YW
2172 if (!hw_addr_is_null(&link->hw_addr)) {
2173 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2174 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2175 /* bonding master and its slaves have the same hardware address. */
2176 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2177 if (r < 0)
2178 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2179 }
0d411b7f 2180
76a86ffd
YW
2181 r = ipv4ll_update_mac(link);
2182 if (r < 0)
2183 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2184
0d411b7f 2185 r = ipv4ll_update_mac(link);
a61bb41c 2186 if (r < 0)
0d411b7f 2187 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
a61bb41c 2188
0d411b7f
YW
2189 r = dhcp4_update_mac(link);
2190 if (r < 0)
2191 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
d236718c 2192
0d411b7f
YW
2193 r = dhcp6_update_mac(link);
2194 if (r < 0)
2195 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2196
2197 r = radv_update_mac(link);
2198 if (r < 0)
2199 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2200
2201 if (link->ndisc) {
ca2b7cd8 2202 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
d236718c 2203 if (r < 0)
0d411b7f
YW
2204 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2205 }
d236718c 2206
35778343
YW
2207 if (link->lldp_rx) {
2208 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
61135582 2209 if (r < 0)
c01b9b87
YW
2210 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2211 }
2212
2213 if (link->lldp_tx) {
2214 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2215 if (r < 0)
2216 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
61135582
DS
2217 }
2218
0d411b7f
YW
2219 return 0;
2220}
a61bb41c 2221
0d411b7f 2222static int link_update_mtu(Link *link, sd_netlink_message *message) {
717ba5fc 2223 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
0d411b7f 2224 int r;
a61bb41c 2225
0d411b7f
YW
2226 assert(link);
2227 assert(message);
2228
2229 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2230 if (r == -ENODATA)
2231 return 0;
2232 if (r < 0)
2233 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
b5d0fd1e
YW
2234 if (mtu == 0)
2235 return 0;
0d411b7f 2236
717ba5fc
YW
2237 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2238 if (r < 0 && r != -ENODATA)
2239 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2240
2241 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2242 if (r < 0 && r != -ENODATA)
2243 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2244
717ba5fc
YW
2245 if (max_mtu == 0)
2246 max_mtu = UINT32_MAX;
2247
2248 link->min_mtu = min_mtu;
2249 link->max_mtu = max_mtu;
2250
0d411b7f
YW
2251 if (link->original_mtu == 0) {
2252 link->original_mtu = mtu;
717ba5fc
YW
2253 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2254 link->original_mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2255 }
2256
717ba5fc
YW
2257 if (link->mtu == mtu)
2258 return 0;
2259
b5d0fd1e
YW
2260 if (link->mtu != 0)
2261 log_link_debug(link, "MTU is changed: %"PRIu32" → %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2262 link->mtu, mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2263
2264 link->mtu = mtu;
2265
2266 if (link->dhcp_client) {
2267 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
9c0a72f9 2268 if (r < 0)
0d411b7f
YW
2269 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2270 }
a61bb41c 2271
0d411b7f
YW
2272 if (link->radv) {
2273 r = sd_radv_set_mtu(link->radv, link->mtu);
9c0a72f9 2274 if (r < 0)
0d411b7f 2275 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
a61bb41c
TG
2276 }
2277
2278 return 0;
dd3efc09 2279}
fe8db0c5 2280
0d411b7f
YW
2281static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2282 _cleanup_strv_free_ char **altnames = NULL;
2283 char **n;
2284 int r;
2285
2286 assert(link);
2287 assert(message);
2288
2289 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
50df02a7
YW
2290 if (r == -ENODATA)
2291 /* The message does not have IFLA_PROP_LIST container attribute. It does not means the
2292 * interface has no alternative name. */
2293 return 0;
2294 if (r < 0)
0d411b7f
YW
2295 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2296
1b345c1e
YW
2297 if (strv_equal(altnames, link->alternative_names))
2298 return 0;
2299
0d411b7f
YW
2300 STRV_FOREACH(n, link->alternative_names)
2301 hashmap_remove(link->manager->links_by_name, *n);
2302
2303 strv_free_and_replace(link->alternative_names, altnames);
2304
2305 STRV_FOREACH(n, link->alternative_names) {
2306 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2307 if (r < 0)
2308 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2309 }
2310
2311 return 0;
2312}
2313
2314static int link_update_name(Link *link, sd_netlink_message *message) {
01afd0f7 2315 char ifname_from_index[IF_NAMESIZE];
0d411b7f
YW
2316 const char *ifname;
2317 int r;
2318
2319 assert(link);
2320 assert(message);
2321
2322 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2323 if (r == -ENODATA)
7802194a 2324 /* Hmm?? But ok. */
0d411b7f
YW
2325 return 0;
2326 if (r < 0)
2327 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2328
2329 if (streq(ifname, link->ifname))
2330 return 0;
2331
01afd0f7
YW
2332 r = format_ifname(link->ifindex, ifname_from_index);
2333 if (r < 0)
2334 return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
176b8be1
YW
2335
2336 if (!streq(ifname, ifname_from_index)) {
2337 log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
2338 "with the name currently configured on the actual interface '%s'. Ignoring.",
2339 ifname, ifname_from_index);
2340 return 0;
2341 }
2342
0d411b7f
YW
2343 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2344
2345 hashmap_remove(link->manager->links_by_name, link->ifname);
2346
2347 r = free_and_strdup(&link->ifname, ifname);
2348 if (r < 0)
2349 return log_oom_debug();
2350
2351 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2352 if (r < 0)
2353 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2354
54d1fdb2
YW
2355 if (link->dhcp_client) {
2356 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2357 if (r < 0)
2358 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2359 }
2360
2361 if (link->dhcp6_client) {
2362 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2363 if (r < 0)
2364 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m");
2365 }
2366
2367 if (link->ndisc) {
2368 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2369 if (r < 0)
2370 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2371 }
2372
2373 if (link->dhcp_server) {
2374 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2375 if (r < 0)
2376 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2377 }
2378
2379 if (link->radv) {
2380 r = sd_radv_set_ifname(link->radv, link->ifname);
2381 if (r < 0)
2382 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2383 }
2384
35778343
YW
2385 if (link->lldp_rx) {
2386 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
54d1fdb2 2387 if (r < 0)
c01b9b87
YW
2388 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2389 }
2390
2391 if (link->lldp_tx) {
2392 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2393 if (r < 0)
2394 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
54d1fdb2
YW
2395 }
2396
2397 if (link->ipv4ll) {
2398 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2399 if (r < 0)
2400 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2401 }
2402
86173383
YW
2403 r = ipv4acd_set_ifname(link);
2404 if (r < 0)
2405 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
54d1fdb2 2406
0d411b7f
YW
2407 return 0;
2408}
2409
2410static int link_update(Link *link, sd_netlink_message *message) {
2411 int r;
2412
2413 assert(link);
2414 assert(message);
2415
2416 r = link_update_name(link, message);
2417 if (r < 0)
2418 return r;
2419
2420 r = link_update_alternative_names(link, message);
2421 if (r < 0)
2422 return r;
2423
2424 r = link_update_mtu(link, message);
2425 if (r < 0)
2426 return r;
2427
2428 r = link_update_hardware_address(link, message);
2429 if (r < 0)
2430 return r;
2431
2432 r = link_update_master(link, message);
2433 if (r < 0)
2434 return r;
2435
2436 return link_update_flags(link, message);
2437}
2438
0c9ee5d5
YW
2439static Link *link_drop_or_unref(Link *link) {
2440 if (!link)
2441 return NULL;
2442 if (!link->manager)
2443 return link_unref(link);
2444 return link_drop(link);
2445}
2446
2447DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2448
2449static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
0d411b7f 2450 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
0c9ee5d5 2451 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
0c9ee5d5
YW
2452 unsigned short iftype;
2453 int r, ifindex;
0c9ee5d5
YW
2454
2455 assert(manager);
2456 assert(message);
2457 assert(ret);
2458
0c9ee5d5
YW
2459 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2460 if (r < 0)
0d411b7f 2461 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
0c9ee5d5 2462 else if (ifindex <= 0)
0d411b7f 2463 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
0c9ee5d5
YW
2464
2465 r = sd_rtnl_message_link_get_type(message, &iftype);
2466 if (r < 0)
0d411b7f 2467 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
0c9ee5d5
YW
2468
2469 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2470 if (r < 0)
0d411b7f 2471 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
0c9ee5d5
YW
2472
2473 /* check for link kind */
2474 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2475 if (r >= 0) {
0d411b7f
YW
2476 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2477 if (r < 0 && r != -ENODATA)
2478 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
0c9ee5d5
YW
2479 r = sd_netlink_message_exit_container(message);
2480 if (r < 0)
0d411b7f 2481 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
0c9ee5d5
YW
2482 }
2483
4c78dc17
YW
2484 if (!manager->test_mode) {
2485 /* Do not update state files when running in test mode. */
2486 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2487 return log_oom_debug();
0d411b7f 2488
4c78dc17
YW
2489 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2490 return log_oom_debug();
0d411b7f 2491
4c78dc17
YW
2492 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2493 return log_oom_debug();
2494 }
0d411b7f 2495
0c9ee5d5
YW
2496 link = new(Link, 1);
2497 if (!link)
2498 return -ENOMEM;
2499
2500 *link = (Link) {
2501 .n_ref = 1,
2502 .state = LINK_STATE_PENDING,
2503 .online_state = _LINK_ONLINE_STATE_INVALID,
2504 .ifindex = ifindex,
2505 .iftype = iftype,
2506 .ifname = TAKE_PTR(ifname),
2507 .kind = TAKE_PTR(kind),
2508
0d411b7f
YW
2509 .state_file = TAKE_PTR(state_file),
2510 .lease_file = TAKE_PTR(lease_file),
2511 .lldp_file = TAKE_PTR(lldp_file),
2512
0c9ee5d5
YW
2513 .n_dns = UINT_MAX,
2514 .dns_default_route = -1,
2515 .llmnr = _RESOLVE_SUPPORT_INVALID,
2516 .mdns = _RESOLVE_SUPPORT_INVALID,
2517 .dnssec_mode = _DNSSEC_MODE_INVALID,
2518 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2519 };
2520
6eab614d 2521 r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
0c9ee5d5 2522 if (r < 0)
0d411b7f 2523 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
0c9ee5d5
YW
2524
2525 link->manager = manager;
2526
0d411b7f 2527 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
0c9ee5d5 2528 if (r < 0)
0d411b7f 2529 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
0c9ee5d5 2530
8a77e245
YW
2531 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2532 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2533
4fea97a6
YW
2534 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2535 if (r < 0) {
2536 if (r != -ENODATA)
2537 log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute, ignoring: %m");
2538
2539 if (netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL) >= 0) {
2540 /* Fallback to ethtool, if the link has a hardware address. */
2541 r = ethtool_get_permanent_hw_addr(&manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2542 if (r < 0)
2543 log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2544 }
2545 }
8a77e245
YW
2546 if (link->permanent_hw_addr.length > 0)
2547 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
0c9ee5d5
YW
2548
2549 r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
2550 if (r < 0)
2551 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2552
b1d9c504
YW
2553 if (streq_ptr(link->driver, "dsa")) {
2554 uint32_t dsa_master_ifindex;
2555
2556 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2557 if (r < 0) {
2558 dsa_master_ifindex = 0;
2559 if (r != -ENODATA)
2560 log_link_warning_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface, ignoring: %m");
2561 } else if (dsa_master_ifindex > INT_MAX) {
2562 dsa_master_ifindex = 0;
2563 log_link_warning(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2564 dsa_master_ifindex);
2565 }
2566
2567 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2568 }
2569
0c9ee5d5 2570 *ret = TAKE_PTR(link);
0c9ee5d5
YW
2571 return 0;
2572}
2573
0d411b7f 2574int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
3be9d62a
YW
2575 Link *link = NULL;
2576 NetDev *netdev = NULL;
2577 uint16_t type;
2578 const char *name;
2579 int r, ifindex;
2580
2581 assert(rtnl);
2582 assert(message);
0d411b7f 2583 assert(manager);
3be9d62a
YW
2584
2585 if (sd_netlink_message_is_error(message)) {
2586 r = sd_netlink_message_get_errno(message);
2587 if (r < 0)
2588 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2589
2590 return 0;
2591 }
2592
2593 r = sd_netlink_message_get_type(message, &type);
2594 if (r < 0) {
2595 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2596 return 0;
2597 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2598 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2599 return 0;
2600 }
2601
2602 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2603 if (r < 0) {
2604 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2605 return 0;
2606 } else if (ifindex <= 0) {
2607 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2608 return 0;
2609 }
2610
2611 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2612 if (r < 0) {
2613 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2614 return 0;
2615 }
2616
6eab614d 2617 (void) link_get_by_index(manager, ifindex, &link);
0d411b7f 2618 (void) netdev_get(manager, name, &netdev);
3be9d62a
YW
2619
2620 switch (type) {
2621 case RTM_NEWLINK:
0d411b7f
YW
2622 if (netdev) {
2623 /* netdev exists, so make sure the ifindex matches */
2624 r = netdev_set_ifindex(netdev, message);
2625 if (r < 0) {
2626 log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m");
2627 return 0;
2628 }
2629 }
2630
3be9d62a
YW
2631 if (!link) {
2632 /* link is new, so add it */
0d411b7f 2633 r = link_new(manager, message, &link);
3be9d62a 2634 if (r < 0) {
0d411b7f 2635 log_warning_errno(r, "Could not process new link message: %m");
3be9d62a
YW
2636 return 0;
2637 }
3be9d62a 2638
0d411b7f 2639 r = link_update(link, message);
3be9d62a 2640 if (r < 0) {
0d411b7f
YW
2641 log_warning_errno(r, "Could not process link message: %m");
2642 link_enter_failed(link);
3be9d62a
YW
2643 return 0;
2644 }
3be9d62a 2645
0d411b7f
YW
2646 r = link_check_initialized(link);
2647 if (r < 0) {
2648 log_warning_errno(r, "Failed to check link is initialized: %m");
2649 link_enter_failed(link);
2650 return 0;
2651 }
2652 } else {
2653 r = link_update(link, message);
2654 if (r < 0) {
2655 log_warning_errno(r, "Could not process link message: %m");
2656 link_enter_failed(link);
2657 return 0;
2658 }
3be9d62a
YW
2659 }
2660
2661 break;
2662
2663 case RTM_DELLINK:
2664 link_drop(link);
2665 netdev_drop(netdev);
2666
2667 break;
2668
2669 default:
04499a70 2670 assert_not_reached();
3be9d62a
YW
2671 }
2672
2673 return 1;
2674}
2675
79c6e114
YW
2676int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2677 uint16_t message_type;
2678 int r;
2679
2680 assert(m);
2681 assert(link);
2682 assert(error_msg);
2683
2684 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2685 return 0;
2686
2687 r = sd_netlink_message_get_errno(m);
2688 if (r < 0) {
2689 log_link_message_warning_errno(link, m, r, error_msg);
2690 link_enter_failed(link);
2691 return 0;
2692 }
2693
2694 r = sd_netlink_message_get_type(m, &message_type);
2695 if (r < 0) {
2696 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2697 return 0;
2698 }
2699 if (message_type != RTM_NEWLINK) {
2700 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2701 return 0;
2702 }
2703
2704 r = link_update(link, m);
2705 if (r < 0) {
2706 link_enter_failed(link);
2707 return 0;
2708 }
2709
2710 return 1;
2711}
2712
2713int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2714 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2715 int r;
2716
2717 assert(link);
2718 assert(link->manager);
2719 assert(link->manager->rtnl);
2720 assert(callback);
2721
2722 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2723 if (r < 0)
2724 return r;
2725
2726 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2727 link_netlink_destroy_callback, link);
2728 if (r < 0)
2729 return r;
2730
2731 link_ref(link);
2732 return 0;
2733}
2734
fe8db0c5 2735static const char* const link_state_table[_LINK_STATE_MAX] = {
0580badc 2736 [LINK_STATE_PENDING] = "pending",
bd08ce56 2737 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 2738 [LINK_STATE_CONFIGURING] = "configuring",
0580badc
ZJS
2739 [LINK_STATE_CONFIGURED] = "configured",
2740 [LINK_STATE_UNMANAGED] = "unmanaged",
2741 [LINK_STATE_FAILED] = "failed",
2742 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
2743};
2744
2745DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
7ae55b78
YW
2746
2747int link_flags_to_string_alloc(uint32_t flags, char **ret) {
2748 _cleanup_free_ char *str = NULL;
19aa7eeb
ZJS
2749 static const char* map[] = {
2750 [LOG2U(IFF_UP)] = "up", /* interface is up. */
2751 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid.*/
2752 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
2753 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
2754 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2755 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
2756 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
2757 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
2758 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
2759 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
2760 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
2761 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
2762 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast.*/
2763 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
2764 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
2765 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
2766 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
2767 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
2768 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
7ae55b78
YW
2769 };
2770
2771 assert(ret);
2772
2773 for (size_t i = 0; i < ELEMENTSOF(map); i++)
19aa7eeb
ZJS
2774 if (FLAGS_SET(flags, 1 << i) && map[i])
2775 if (!strextend_with_separator(&str, ",", map[i]))
2776 return -ENOMEM;
7ae55b78
YW
2777
2778 *ret = TAKE_PTR(str);
2779 return 0;
2780}
2781
2782static const char * const kernel_operstate_table[] = {
2783 [IF_OPER_UNKNOWN] = "unknown",
2784 [IF_OPER_NOTPRESENT] = "not-present",
2785 [IF_OPER_DOWN] = "down",
2786 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
2787 [IF_OPER_TESTING] = "testing",
2788 [IF_OPER_DORMANT] = "dormant",
2789 [IF_OPER_UP] = "up",
2790};
2791
2792DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);