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