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