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