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