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