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