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