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