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