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