]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
network: do not drop requests on carrier lost
[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
695 if (hashmap_isempty(link->bound_to_links))
696 return 0;
697
698 if (link->flags & IFF_UP)
699 link_is_up = true;
700
852a3916 701 HASHMAP_FOREACH(l, link->bound_to_links)
0d4ad91d
AR
702 if (link_has_carrier(l)) {
703 required_up = true;
704 break;
705 }
706
852a3916 707 if (!required_up && link_is_up)
112a0972 708 return link_down(link);
852a3916
YW
709 if (required_up && !link_is_up)
710 return link_up(link);
0d4ad91d
AR
711
712 return 0;
713}
714
715static int link_handle_bound_by_list(Link *link) {
0d4ad91d
AR
716 Link *l;
717 int r;
718
719 assert(link);
720
721 if (hashmap_isempty(link->bound_by_links))
722 return 0;
723
852a3916 724 HASHMAP_FOREACH(l, link->bound_by_links) {
0d4ad91d
AR
725 r = link_handle_bound_to_list(l);
726 if (r < 0)
727 return r;
728 }
729
730 return 0;
731}
732
733static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
734 int r;
735
736 assert(link);
737 assert(carrier);
738
739 if (link == carrier)
740 return 0;
741
742 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
743 return 0;
744
190b3b5c 745 r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
0d4ad91d
AR
746 if (r < 0)
747 return r;
748
9092113d
YW
749 link_dirty(link);
750
0d4ad91d
AR
751 return 0;
752}
753
754static int link_new_bound_by_list(Link *link) {
755 Manager *m;
756 Link *carrier;
0d4ad91d 757 int r;
0d4ad91d
AR
758
759 assert(link);
760 assert(link->manager);
761
762 m = link->manager;
763
90e74a66 764 HASHMAP_FOREACH(carrier, m->links) {
0d4ad91d
AR
765 if (!carrier->network)
766 continue;
767
768 if (strv_isempty(carrier->network->bind_carrier))
769 continue;
770
191a3f16 771 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
0d4ad91d
AR
772 r = link_put_carrier(link, carrier, &link->bound_by_links);
773 if (r < 0)
774 return r;
0d4ad91d
AR
775 }
776 }
777
90e74a66 778 HASHMAP_FOREACH(carrier, link->bound_by_links) {
0d4ad91d
AR
779 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
780 if (r < 0)
781 return r;
0d4ad91d
AR
782 }
783
784 return 0;
785}
786
787static int link_new_bound_to_list(Link *link) {
788 Manager *m;
789 Link *carrier;
0d4ad91d 790 int r;
0d4ad91d
AR
791
792 assert(link);
793 assert(link->manager);
794
795 if (!link->network)
796 return 0;
797
798 if (strv_isempty(link->network->bind_carrier))
799 return 0;
800
801 m = link->manager;
802
852a3916 803 HASHMAP_FOREACH(carrier, m->links) {
191a3f16 804 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
0d4ad91d
AR
805 r = link_put_carrier(link, carrier, &link->bound_to_links);
806 if (r < 0)
807 return r;
0d4ad91d
AR
808 }
809 }
810
852a3916 811 HASHMAP_FOREACH(carrier, link->bound_to_links) {
0d4ad91d
AR
812 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
813 if (r < 0)
814 return r;
0d4ad91d
AR
815 }
816
817 return 0;
818}
819
820static int link_new_carrier_maps(Link *link) {
821 int r;
822
823 r = link_new_bound_by_list(link);
824 if (r < 0)
825 return r;
826
827 r = link_handle_bound_by_list(link);
828 if (r < 0)
829 return r;
830
831 r = link_new_bound_to_list(link);
832 if (r < 0)
833 return r;
834
852a3916 835 return link_handle_bound_to_list(link);
0d4ad91d
AR
836}
837
838static void link_free_bound_to_list(Link *link) {
9092113d 839 bool updated = false;
0d4ad91d 840 Link *bound_to;
0d4ad91d 841
9092113d
YW
842 assert(link);
843
844 while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
845 updated = true;
0d4ad91d
AR
846
847 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
84de38c5 848 link_dirty(bound_to);
0d4ad91d
AR
849 }
850
9092113d
YW
851 if (updated)
852 link_dirty(link);
0d4ad91d
AR
853}
854
855static void link_free_bound_by_list(Link *link) {
9092113d 856 bool updated = false;
0d4ad91d 857 Link *bound_by;
0d4ad91d 858
9092113d
YW
859 assert(link);
860
861 while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
862 updated = true;
0d4ad91d
AR
863
864 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
84de38c5 865 link_dirty(bound_by);
0d4ad91d
AR
866 link_handle_bound_to_list(bound_by);
867 }
868 }
869
9092113d
YW
870 if (updated)
871 link_dirty(link);
0d4ad91d
AR
872}
873
874static void link_free_carrier_maps(Link *link) {
0d4ad91d
AR
875 assert(link);
876
9092113d
YW
877 link_free_bound_to_list(link);
878 link_free_bound_by_list(link);
0d4ad91d
AR
879}
880
81357285 881static int link_append_to_master(Link *link) {
5f707e12
YW
882 Link *master;
883 int r;
884
885 assert(link);
5f707e12 886
81357285
YW
887 /* - The link may have no master.
888 * - RTM_NEWLINK message about master interface may not be received yet. */
889 if (link_get_master(link, &master) < 0)
890 return 0;
5f707e12 891
de7fef4b 892 r = set_ensure_put(&master->slaves, NULL, link);
38288f0b 893 if (r <= 0)
5f707e12
YW
894 return r;
895
896 link_ref(link);
897 return 0;
898}
899
81357285 900static void link_drop_from_master(Link *link) {
5f707e12
YW
901 Link *master;
902
903 assert(link);
904
81357285 905 if (!link->manager)
5f707e12
YW
906 return;
907
81357285 908 if (link_get_master(link, &master) < 0)
5f707e12
YW
909 return;
910
911 link_unref(set_remove(master->slaves, link));
912}
913
56001f02
YW
914static void link_drop_requests(Link *link) {
915 Request *req;
916
917 assert(link);
918 assert(link->manager);
919
920 ORDERED_SET_FOREACH(req, link->manager->request_queue)
921 if (req->link == link)
922 request_drop(req);
923}
924
cc2d7efc 925static Link *link_drop(Link *link) {
0b54c870
YW
926 char **n;
927
63130eb3 928 if (!link)
cc2d7efc 929 return NULL;
0d4ad91d 930
63130eb3
YW
931 assert(link->manager);
932
0d4ad91d
AR
933 link_set_state(link, LINK_STATE_LINGER);
934
63130eb3
YW
935 /* Drop all references from other links and manager. Note that async netlink calls may have
936 * references to the link, and they will be dropped when we receive replies. */
937
56001f02
YW
938 link_drop_requests(link);
939
0d4ad91d
AR
940 link_free_carrier_maps(link);
941
81357285 942 link_drop_from_master(link);
5f707e12 943
63130eb3 944 link_unref(set_remove(link->manager->links_requesting_uuid, link));
0d4ad91d 945
db2f8a2e 946 (void) unlink(link->state_file);
63130eb3
YW
947 link_clean(link);
948
0b54c870
YW
949 STRV_FOREACH(n, link->alternative_names)
950 hashmap_remove(link->manager->links_by_name, *n);
951
952 hashmap_remove(link->manager->links_by_name, link->ifname);
953
63130eb3
YW
954 /* The following must be called at last. */
955 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
cc2d7efc 956 return link_unref(link);
0d4ad91d
AR
957}
958
5e5b137a 959static int link_drop_foreign_config(Link *link) {
0b81225e
YW
960 int k, r;
961
962 assert(link);
963 assert(link->manager);
5e5b137a 964
e36d601c 965 r = link_drop_foreign_routes(link);
5e5b137a 966
e36d601c 967 k = link_drop_foreign_nexthops(link);
0b81225e
YW
968 if (k < 0 && r >= 0)
969 r = k;
970
e36d601c 971 k = link_drop_foreign_addresses(link);
0b81225e
YW
972 if (k < 0 && r >= 0)
973 r = k;
d1bdafd2 974
e36d601c 975 k = link_drop_foreign_neighbors(link);
25b82b6e
YW
976 if (k < 0 && r >= 0)
977 r = k;
978
0b81225e
YW
979 k = manager_drop_foreign_routing_policy_rules(link->manager);
980 if (k < 0 && r >= 0)
981 r = k;
982
983 return r;
5e5b137a
TG
984}
985
3104883d 986static int link_drop_config(Link *link) {
0b81225e
YW
987 int k, r;
988
989 assert(link);
990 assert(link->manager);
3104883d 991
e36d601c 992 r = link_drop_routes(link);
3104883d 993
e36d601c 994 k = link_drop_nexthops(link);
0b81225e
YW
995 if (k < 0 && r >= 0)
996 r = k;
d1bdafd2 997
e36d601c 998 k = link_drop_addresses(link);
0b81225e
YW
999 if (k < 0 && r >= 0)
1000 r = k;
1001
e36d601c 1002 k = link_drop_neighbors(link);
25b82b6e
YW
1003 if (k < 0 && r >= 0)
1004 r = k;
1005
0b81225e
YW
1006 k = manager_drop_routing_policy_rules(link->manager, link);
1007 if (k < 0 && r >= 0)
1008 r = k;
3104883d 1009
c69305ff
LP
1010 ndisc_flush(link);
1011
0b81225e 1012 return r;
3104883d
SS
1013}
1014
c3b94251 1015static int link_configure(Link *link) {
4ecdcb07
YW
1016 int r;
1017
ef1ba606 1018 assert(link);
b22d8a00 1019 assert(link->network);
bd08ce56 1020 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 1021
1187fc33
YW
1022 link_set_state(link, LINK_STATE_CONFIGURING);
1023
34658df2 1024 r = link_configure_traffic_control(link);
4ecdcb07
YW
1025 if (r < 0)
1026 return r;
1027
7558f9e7
YW
1028 if (link->iftype == ARPHRD_CAN) {
1029 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1030 r = link_request_to_set_can(link);
1031 if (r < 0)
1032 return r;
1033
1034 return link_request_to_activate(link);
1035 }
1036
518cd6b5
SS
1037 r = link_configure_sr_iov(link);
1038 if (r < 0)
1039 return r;
1040
5e0534f1 1041 r = link_set_sysctl(link);
b69c3180
SS
1042 if (r < 0)
1043 return r;
8749cbcd 1044
a8e5e27c 1045 r = link_request_to_set_mac(link);
e16e4b3b
DS
1046 if (r < 0)
1047 return r;
1048
1187fc33 1049 r = link_request_to_set_flags(link);
e16e4b3b
DS
1050 if (r < 0)
1051 return r;
1052
1187fc33 1053 r = link_request_to_set_group(link);
99d2baa2
SS
1054 if (r < 0)
1055 return r;
1056
1187fc33
YW
1057 r = link_configure_mtu(link);
1058 if (r < 0)
1059 return r;
1060
1061 r = link_request_to_set_addrgen_mode(link);
1062 if (r < 0)
1063 return r;
1064
1065 r = link_request_to_set_master(link);
1066 if (r < 0)
1067 return r;
1068
1069 r = link_request_stacked_netdevs(link);
1070 if (r < 0)
1071 return r;
1072
1073 r = link_request_to_set_bond(link);
1074 if (r < 0)
1075 return r;
1076
1077 r = link_request_to_set_bridge(link);
1078 if (r < 0)
1079 return r;
1080
1081 r = link_request_to_set_bridge_vlan(link);
1082 if (r < 0)
1083 return r;
1084
1085 r = link_request_to_activate(link);
89fe6535
SS
1086 if (r < 0)
1087 return r;
1088
2ffd6d73
YW
1089 r = ipv4ll_configure(link);
1090 if (r < 0)
1091 return r;
64b21ece 1092
2ffd6d73
YW
1093 r = dhcp4_configure(link);
1094 if (r < 0)
1095 return r;
eb34d4af 1096
2ffd6d73
YW
1097 r = dhcp6_configure(link);
1098 if (r < 0)
1099 return r;
f5a8c43f 1100
2ffd6d73
YW
1101 r = ndisc_configure(link);
1102 if (r < 0)
1103 return r;
4138fb2c 1104
1d28a3cf
YW
1105 r = link_request_dhcp_server(link);
1106 if (r < 0)
1107 return r;
1108
086b8853
YW
1109 r = radv_configure(link);
1110 if (r < 0)
1111 return r;
7465dd22 1112
2ffd6d73
YW
1113 r = link_lldp_rx_configure(link);
1114 if (r < 0)
1115 return r;
ce43e484 1116
4c649652
DS
1117 /* Drop foreign config, but ignore loopback or critical devices.
1118 * We do not want to remove loopback address or addresses used for root NFS. */
1119 if (!(link->flags & IFF_LOOPBACK) &&
1120 link->network->keep_configuration != KEEP_CONFIGURATION_YES) {
1121 r = link_drop_foreign_config(link);
1122 if (r < 0)
1123 return r;
1124 }
1125
1187fc33
YW
1126 r = link_request_static_configs(link);
1127 if (r < 0)
1128 return r;
1129
1130 if (!link_has_carrier(link))
1131 return 0;
1132
1133 return link_acquire_dynamic_conf(link);
505f8da7
TG
1134}
1135
170e88c8
YW
1136static int link_get_network(Link *link, Network **ret) {
1137 Network *network;
1a3caa49 1138 int r;
170e88c8
YW
1139
1140 assert(link);
1141 assert(link->manager);
1142 assert(ret);
1143
1144 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1145 bool warn = false;
1146
1a3caa49 1147 r = net_match_config(
170e88c8
YW
1148 &network->match,
1149 link->sd_device,
1150 &link->hw_addr.addr.ether,
1151 &link->permanent_mac,
1152 link->driver,
1153 link->iftype,
1154 link->ifname,
1155 link->alternative_names,
1156 link->wlan_iftype,
1157 link->ssid,
1a3caa49
YW
1158 &link->bssid);
1159 if (r < 0)
1160 return r;
1161 if (r == 0)
170e88c8
YW
1162 continue;
1163
1164 if (network->match.ifname && link->sd_device) {
1165 uint8_t name_assign_type = NET_NAME_UNKNOWN;
1166 const char *attr;
1167
1168 if (sd_device_get_sysattr_value(link->sd_device, "name_assign_type", &attr) >= 0)
1169 (void) safe_atou8(attr, &name_assign_type);
1170
1171 warn = name_assign_type == NET_NAME_ENUM;
1172 }
1173
1174 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1175 "found matching network '%s'%s.",
1176 network->filename,
1177 warn ? ", based on potentially unpredictable interface name" : "");
1178
1179 if (network->unmanaged)
1180 return -ENOENT;
1181
1182 *ret = network;
1183 return 0;
1184 }
1185
1186 return -ENOENT;
1187}
1188
5a1860f7 1189static int link_reconfigure_internal(Link *link, bool force) {
ad932b15
YW
1190 Network *network;
1191 int r;
1192
5a1860f7 1193 assert(link);
572b21d9 1194
170e88c8 1195 r = link_get_network(link, &network);
ad932b15 1196 if (r == -ENOENT) {
29836c16 1197 link_set_state(link, LINK_STATE_UNMANAGED);
ad932b15 1198 return 0;
170e88c8
YW
1199 }
1200 if (r < 0)
ad932b15
YW
1201 return r;
1202
99b8517c 1203 if (link->network == network && !force)
ad932b15
YW
1204 return 0;
1205
1206 log_link_info(link, "Re-configuring with %s", network->filename);
1207
1208 /* Dropping old .network file */
2a99eed0 1209 r = link_stop_engines(link, false);
572b21d9 1210 if (r < 0)
ad932b15 1211 return r;
ad932b15 1212
19d9a5ad
YW
1213 link_drop_requests(link);
1214
ad932b15
YW
1215 r = link_drop_config(link);
1216 if (r < 0)
1217 return r;
1218
2c7b826d 1219 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
942cf4b8 1220 log_link_debug(link, "State is %s, dropping foreign config", link_state_to_string(link->state));
ad932b15
YW
1221 r = link_drop_foreign_config(link);
1222 if (r < 0)
1223 return r;
1224 }
1225
1226 link_free_carrier_maps(link);
1227 link_free_engines(link);
1228 link->network = network_unref(link->network);
59c31eba 1229 link_unref(set_remove(link->manager->links_requesting_uuid, link));
ad932b15
YW
1230
1231 /* Then, apply new .network file */
9092113d 1232 link->network = network_ref(network);
b156a95d 1233 link_update_operstate(link, true);
9092113d 1234 link_dirty(link);
ad932b15
YW
1235
1236 r = link_new_carrier_maps(link);
1237 if (r < 0)
1238 return r;
1239
1240 link_set_state(link, LINK_STATE_INITIALIZED);
61135582 1241 link->activated = false;
ad932b15 1242
ad932b15
YW
1243 r = link_configure(link);
1244 if (r < 0)
1245 return r;
1246
0e397560 1247 return 1;
ad932b15
YW
1248}
1249
5a1860f7 1250static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force) {
572b21d9
YW
1251 int r;
1252
5a1860f7
YW
1253 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1254 if (r <= 0)
1255 return r;
1256
1257 r = link_reconfigure_internal(link, force);
572b21d9
YW
1258 if (r < 0)
1259 link_enter_failed(link);
1260
5a1860f7 1261 return 0;
572b21d9
YW
1262}
1263
5a1860f7
YW
1264static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1265 return link_reconfigure_handler_internal(rtnl, m, link, false);
1266}
572b21d9 1267
5a1860f7
YW
1268static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1269 return link_reconfigure_handler_internal(rtnl, m, link, true);
572b21d9
YW
1270}
1271
1272int link_reconfigure(Link *link, bool force) {
572b21d9
YW
1273 int r;
1274
8bceafa7 1275 /* When link in pending or initialized state, then link_configure() will be called. To prevent
f8dd4077
ZJS
1276 * the function from being called multiple times simultaneously, refuse to reconfigure the
1277 * interface in these cases. */
8bceafa7 1278 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
f8dd4077 1279 return 0; /* 0 means no-op. */
2c0d7ed3 1280
5a1860f7 1281 r = link_call_getlink(link, force ? link_force_reconfigure_handler : link_reconfigure_handler);
572b21d9
YW
1282 if (r < 0)
1283 return r;
1284
8bceafa7 1285 return 1; /* 1 means the interface will be reconfigured. */
572b21d9
YW
1286}
1287
e6bf7774 1288static int link_initialized_and_synced(Link *link) {
505f8da7 1289 Network *network;
505f8da7
TG
1290 int r;
1291
1292 assert(link);
1293 assert(link->ifname);
1294 assert(link->manager);
1295
bd08ce56 1296 /* We may get called either from the asynchronous netlink callback,
0d411b7f 1297 * or directly from link_check_initialized() if running in a container. */
bd08ce56 1298 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
4232cf04 1299 return 0;
505f8da7 1300
6a7a4e4d 1301 log_link_debug(link, "Link state is up-to-date");
bd08ce56 1302 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 1303
0d4ad91d
AR
1304 r = link_new_bound_by_list(link);
1305 if (r < 0)
1306 return r;
1307
1308 r = link_handle_bound_by_list(link);
1309 if (r < 0)
1310 return r;
1311
c4a03a56 1312 if (!link->network) {
0894cfe4 1313 r = wifi_get_info(link);
8d968fdd
YW
1314 if (r < 0)
1315 return r;
1316
170e88c8 1317 r = link_get_network(link, &network);
c4a03a56 1318 if (r == -ENOENT) {
29836c16 1319 link_set_state(link, LINK_STATE_UNMANAGED);
4232cf04 1320 return 0;
170e88c8
YW
1321 }
1322 if (r < 0)
c4a03a56 1323 return r;
505f8da7 1324
c4a03a56
TG
1325 if (link->flags & IFF_LOOPBACK) {
1326 if (network->link_local != ADDRESS_FAMILY_NO)
1327 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
78c958f8 1328
c4a03a56
TG
1329 if (network->dhcp != ADDRESS_FAMILY_NO)
1330 log_link_debug(link, "Ignoring DHCP clients for loopback link");
78c958f8 1331
c4a03a56
TG
1332 if (network->dhcp_server)
1333 log_link_debug(link, "Ignoring DHCP server for loopback link");
1334 }
bd2efe92 1335
9092113d 1336 link->network = network_ref(network);
bcdcc596 1337 link_update_operstate(link, false);
9092113d 1338 link_dirty(link);
c4a03a56 1339 }
505f8da7 1340
0d4ad91d
AR
1341 r = link_new_bound_to_list(link);
1342 if (r < 0)
1343 return r;
1344
852a3916 1345 return link_configure(link);
505f8da7
TG
1346}
1347
302a796f 1348static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4ff296b0
YW
1349 int r;
1350
5a1860f7
YW
1351 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1352 if (r <= 0)
0b54c870 1353 return r;
572b21d9 1354
4ff296b0
YW
1355 r = link_initialized_and_synced(link);
1356 if (r < 0)
1357 link_enter_failed(link);
5a1860f7
YW
1358
1359 return 0;
e6bf7774
YW
1360}
1361
3be9d62a 1362static int link_initialized(Link *link, sd_device *device) {
4f561e8e 1363 assert(link);
4f561e8e
TG
1364 assert(device);
1365
8434fd5c 1366 if (link->state != LINK_STATE_PENDING)
4f561e8e
TG
1367 return 0;
1368
51517f9e 1369 if (link->sd_device)
679b3605
TG
1370 return 0;
1371
79008bdd 1372 log_link_debug(link, "udev initialized link");
bd08ce56 1373 link_set_state(link, LINK_STATE_INITIALIZED);
4f561e8e 1374
51517f9e 1375 link->sd_device = sd_device_ref(device);
4f561e8e 1376
3c9b8860
TG
1377 /* udev has initialized the link, but we don't know if we have yet
1378 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1379 * when it returns we know that the pending NEWLINKs have already been
1380 * processed and that we are up-to-date */
4f561e8e 1381
5a1860f7 1382 return link_call_getlink(link, link_initialized_handler);
cc2d7efc
YW
1383}
1384
0d411b7f 1385static int link_check_initialized(Link *link) {
51517f9e 1386 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
505f8da7 1387 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
5a937ea2 1388 int r;
505f8da7 1389
0d411b7f 1390 assert(link);
505f8da7 1391
0d411b7f 1392 if (path_is_read_only_fs("/sys") > 0)
852a3916 1393 /* no udev */
0d411b7f 1394 return link_initialized_and_synced(link);
852a3916
YW
1395
1396 /* udev should be around */
1397 xsprintf(ifindex_str, "n%d", link->ifindex);
1398 r = sd_device_new_from_device_id(&device, ifindex_str);
1399 if (r < 0) {
1400 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1401 return 0;
1402 }
1403
1404 r = sd_device_get_is_initialized(device);
0d411b7f
YW
1405 if (r < 0)
1406 return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
852a3916
YW
1407 if (r == 0) {
1408 /* not yet ready */
1409 log_link_debug(link, "link pending udev initialization...");
1410 return 0;
1411 }
1412
1413 r = device_is_renaming(device);
0d411b7f
YW
1414 if (r < 0)
1415 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
852a3916
YW
1416 if (r > 0) {
1417 log_link_debug(link, "Interface is being renamed, pending initialization.");
1418 return 0;
4f561e8e 1419 }
505f8da7 1420
0d411b7f 1421 return link_initialized(link, device);
a748b692
TG
1422}
1423
3be9d62a
YW
1424int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
1425 sd_device_action_t action;
1426 Manager *m = userdata;
1427 Link *link = NULL;
1428 int r, ifindex;
1429
1430 assert(m);
1431 assert(device);
1432
1433 r = sd_device_get_action(device, &action);
1434 if (r < 0) {
1435 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
1436 return 0;
1437 }
1438
1439 /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
1440 * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
1441 * still exists — and we are interested in that. */
1442 if (action == SD_DEVICE_REMOVE)
1443 return 0;
1444
1445 r = sd_device_get_ifindex(device, &ifindex);
1446 if (r < 0) {
1447 log_device_debug_errno(device, r, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m",
1448 device_action_to_string(action));
1449 return 0;
1450 }
1451
1452 r = device_is_renaming(device);
1453 if (r < 0) {
4b9a8c2b 1454 log_device_debug_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
3be9d62a
YW
1455 device_action_to_string(action));
1456 return 0;
1457 }
1458 if (r > 0) {
1459 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed.");
1460 return 0;
1461 }
1462
1463 r = link_get(m, ifindex, &link);
1464 if (r < 0) {
4b9a8c2b 1465 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
3be9d62a
YW
1466 return 0;
1467 }
1468
4b9a8c2b
YW
1469 r = link_initialized(link, device);
1470 if (r < 0)
1471 link_enter_failed(link);
3be9d62a
YW
1472
1473 return 0;
1474}
1475
9c0a72f9
TG
1476static int link_carrier_gained(Link *link) {
1477 int r;
1478
1479 assert(link);
1480
600b7898
YW
1481 if (link->iftype == ARPHRD_CAN)
1482 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1483 return link_handle_bound_by_list(link);
1484
0894cfe4 1485 r = wifi_get_info(link);
8d968fdd
YW
1486 if (r < 0)
1487 return r;
1488 if (r > 0) {
0e397560
YW
1489 r = link_reconfigure_internal(link, false);
1490 if (r != 0)
8d968fdd
YW
1491 return r;
1492 }
1493
b9ea3d2e 1494 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
8566df79 1495 r = link_acquire_dynamic_conf(link);
f0269653 1496 if (r < 0)
9c0a72f9 1497 return r;
6fc25497 1498
97108953 1499 r = link_request_static_configs(link);
6fc25497
SS
1500 if (r < 0)
1501 return r;
9c0a72f9
TG
1502 }
1503
9a038aac 1504 return link_handle_bound_by_list(link);
9c0a72f9
TG
1505}
1506
1507static int link_carrier_lost(Link *link) {
1508 int r;
1509
1510 assert(link);
1511
25e992ba 1512 if (link->network && link->network->ignore_carrier_loss)
93b4dab5
SS
1513 return 0;
1514
600b7898
YW
1515 if (link->iftype == ARPHRD_CAN)
1516 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1517 return link_handle_bound_by_list(link);
1518
2a99eed0 1519 r = link_stop_engines(link, false);
9c0a72f9
TG
1520 if (r < 0) {
1521 link_enter_failed(link);
1522 return r;
1523 }
1524
3104883d
SS
1525 r = link_drop_config(link);
1526 if (r < 0)
1527 return r;
1528
2c7b826d 1529 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
942cf4b8 1530 log_link_debug(link, "State is %s, dropping foreign config", link_state_to_string(link->state));
c436d553
MM
1531 r = link_drop_foreign_config(link);
1532 if (r < 0)
1533 return r;
1534 }
3104883d 1535
852a3916 1536 return link_handle_bound_by_list(link);
0c9ee5d5
YW
1537}
1538
1539int link_carrier_reset(Link *link) {
1540 int r;
1541
1542 assert(link);
1543
852a3916
YW
1544 if (!link_has_carrier(link))
1545 return 0;
0c9ee5d5 1546
852a3916
YW
1547 r = link_carrier_lost(link);
1548 if (r < 0)
1549 return r;
0c9ee5d5 1550
852a3916
YW
1551 r = link_carrier_gained(link);
1552 if (r < 0)
1553 return r;
0c9ee5d5 1554
852a3916 1555 log_link_info(link, "Reset carrier");
0c9ee5d5
YW
1556 return 0;
1557}
1558
1559static int link_admin_state_up(Link *link) {
1560 int r;
1561
1562 assert(link);
1563
1564 /* This is called every time an interface admin state changes to up;
1565 * specifically, when IFF_UP flag changes from unset to set. */
1566
1567 if (!link->network)
1568 return 0;
1569
1570 if (link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
1571 log_link_info(link, "ActivationPolicy is \"always-off\", forcing link down");
112a0972 1572 return link_down(link);
0c9ee5d5
YW
1573 }
1574
1575 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1576 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1577 r = link_set_ipv6_mtu(link);
1578 if (r < 0)
1579 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1580
1581 return 0;
1582}
1583
1584static int link_admin_state_down(Link *link) {
1585 assert(link);
1586
1587 if (!link->network)
1588 return 0;
1589
1590 if (link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
0c9ee5d5
YW
1591 log_link_info(link, "ActivationPolicy is \"always-on\", forcing link up");
1592 return link_up(link);
1593 }
1594
1595 return 0;
1596}
1597
1598bool link_has_carrier(Link *link) {
1599 /* see Documentation/networking/operstates.txt in the kernel sources */
1600
1601 if (link->kernel_operstate == IF_OPER_UP)
1602 return true;
1603
1604 if (link->kernel_operstate == IF_OPER_UNKNOWN)
1605 /* operstate may not be implemented, so fall back to flags */
1606 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1607 !FLAGS_SET(link->flags, IFF_DORMANT))
1608 return true;
1609
1610 return false;
1611}
1612
1613static bool link_is_enslaved(Link *link) {
1614 if (link->flags & IFF_SLAVE)
1615 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
1616 return true;
1617
1618 if (!link->network)
1619 return false;
1620
1621 if (link->master_ifindex > 0 && link->network->bridge)
1622 return true;
1623
1624 /* TODO: add conditions for other netdevs. */
1625
1626 return false;
1627}
1628
1629static LinkAddressState address_state_from_scope(uint8_t scope) {
1630 if (scope < RT_SCOPE_SITE)
1631 /* universally accessible addresses found */
1632 return LINK_ADDRESS_STATE_ROUTABLE;
1633
1634 if (scope < RT_SCOPE_HOST)
1635 /* only link or site local addresses found */
1636 return LINK_ADDRESS_STATE_DEGRADED;
1637
1638 /* no useful addresses found */
1639 return LINK_ADDRESS_STATE_OFF;
1640}
1641
1642void link_update_operstate(Link *link, bool also_update_master) {
1643 LinkOperationalState operstate;
1644 LinkCarrierState carrier_state;
1645 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1646 LinkOnlineState online_state;
1647 _cleanup_strv_free_ char **p = NULL;
1648 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
1649 bool changed = false;
1650 Address *address;
1651
1652 assert(link);
1653
1654 if (link->kernel_operstate == IF_OPER_DORMANT)
1655 carrier_state = LINK_CARRIER_STATE_DORMANT;
1656 else if (link_has_carrier(link)) {
1657 if (link_is_enslaved(link))
1658 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1659 else
1660 carrier_state = LINK_CARRIER_STATE_CARRIER;
1661 } else if (link->flags & IFF_UP)
1662 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1663 else
1664 carrier_state = LINK_CARRIER_STATE_OFF;
1665
1666 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1667 Link *slave;
1668
1669 SET_FOREACH(slave, link->slaves) {
1670 link_update_operstate(slave, false);
1671
1672 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1673 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1674 }
1675 }
1676
1677 SET_FOREACH(address, link->addresses) {
1678 if (!address_is_ready(address))
1679 continue;
1680
1681 if (address->family == AF_INET)
1682 ipv4_scope = MIN(ipv4_scope, address->scope);
1683
1684 if (address->family == AF_INET6)
1685 ipv6_scope = MIN(ipv6_scope, address->scope);
1686 }
1687
1688 /* for operstate we also take foreign addresses into account */
1689 SET_FOREACH(address, link->addresses_foreign) {
1690 if (!address_is_ready(address))
1691 continue;
1692
1693 if (address->family == AF_INET)
1694 ipv4_scope = MIN(ipv4_scope, address->scope);
1695
1696 if (address->family == AF_INET6)
1697 ipv6_scope = MIN(ipv6_scope, address->scope);
1698 }
1699
1700 ipv4_address_state = address_state_from_scope(ipv4_scope);
1701 ipv6_address_state = address_state_from_scope(ipv6_scope);
1702 address_state = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
1703
1704 /* Mapping of address and carrier state vs operational state
1705 * carrier state
1706 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1707 * ------------------------------------------------------------------------------
1708 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1709 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1710 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1711 */
1712
1713 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
1714 operstate = (LinkOperationalState) carrier_state;
1715 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1716 operstate = LINK_OPERSTATE_ROUTABLE;
1717 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1718 operstate = LINK_OPERSTATE_DEGRADED;
1719 else
1720 operstate = LINK_OPERSTATE_ENSLAVED;
1721
1722 /* Only determine online state for managed links with RequiredForOnline=yes */
1723 if (!link->network || !link->network->required_for_online)
1724 online_state = _LINK_ONLINE_STATE_INVALID;
1725 else if (operstate < link->network->required_operstate_for_online.min ||
1726 operstate > link->network->required_operstate_for_online.max)
1727 online_state = LINK_ONLINE_STATE_OFFLINE;
1728 else {
1729 AddressFamily required_family = link->network->required_family_for_online;
1730 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1731 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1732
1733 /* The operational state is within the range required for online.
1734 * If a particular address family is also required, we might revert
1735 * to offline in the blocks below. */
1736 online_state = LINK_ONLINE_STATE_ONLINE;
1737
1738 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) {
1739 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1740 online_state = LINK_ONLINE_STATE_OFFLINE;
1741 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1742 online_state = LINK_ONLINE_STATE_OFFLINE;
1743 }
1744
1745 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) {
1746 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1747 online_state = LINK_ONLINE_STATE_OFFLINE;
1748 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1749 online_state = LINK_ONLINE_STATE_OFFLINE;
1750 }
1751 }
1752
1753 if (link->carrier_state != carrier_state) {
1754 link->carrier_state = carrier_state;
1755 changed = true;
1756 if (strv_extend(&p, "CarrierState") < 0)
1757 log_oom();
1758 }
1759
1760 if (link->address_state != address_state) {
1761 link->address_state = address_state;
1762 changed = true;
1763 if (strv_extend(&p, "AddressState") < 0)
1764 log_oom();
1765 }
0d4ad91d 1766
0c9ee5d5
YW
1767 if (link->ipv4_address_state != ipv4_address_state) {
1768 link->ipv4_address_state = ipv4_address_state;
1769 changed = true;
1770 if (strv_extend(&p, "IPv4AddressState") < 0)
1771 log_oom();
1772 }
9c0a72f9 1773
0c9ee5d5
YW
1774 if (link->ipv6_address_state != ipv6_address_state) {
1775 link->ipv6_address_state = ipv6_address_state;
1776 changed = true;
1777 if (strv_extend(&p, "IPv6AddressState") < 0)
1778 log_oom();
1779 }
9c0a72f9 1780
0c9ee5d5
YW
1781 if (link->operstate != operstate) {
1782 link->operstate = operstate;
1783 changed = true;
1784 if (strv_extend(&p, "OperationalState") < 0)
1785 log_oom();
1786 }
9c0a72f9 1787
0c9ee5d5
YW
1788 if (link->online_state != online_state) {
1789 link->online_state = online_state;
1790 changed = true;
1791 if (strv_extend(&p, "OnlineState") < 0)
1792 log_oom();
1793 }
9c0a72f9 1794
0c9ee5d5
YW
1795 if (p)
1796 link_send_changed_strv(link, p);
1797 if (changed)
1798 link_dirty(link);
9c0a72f9 1799
0c9ee5d5
YW
1800 if (also_update_master) {
1801 Link *master;
9c0a72f9 1802
0c9ee5d5
YW
1803 if (link_get_master(link, &master) >= 0)
1804 link_update_operstate(master, true);
1805 }
9c0a72f9
TG
1806}
1807
0c9ee5d5
YW
1808#define FLAG_STRING(string, flag, old, new) \
1809 (((old ^ new) & flag) \
1810 ? ((old & flag) ? (" -" string) : (" +" string)) \
1811 : "")
1812
0d411b7f
YW
1813static int link_update_flags(Link *link, sd_netlink_message *message) {
1814 bool link_was_admin_up, had_carrier;
0c9ee5d5
YW
1815 uint8_t operstate;
1816 unsigned flags;
d236718c
DS
1817 int r;
1818
61135582 1819 assert(link);
0d411b7f 1820 assert(message);
61135582 1821
0d411b7f 1822 r = sd_rtnl_message_link_get_flags(message, &flags);
0c9ee5d5 1823 if (r < 0)
0d411b7f 1824 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
3be9d62a 1825
0d411b7f
YW
1826 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
1827 if (r == -ENODATA)
1828 /* If we got a message without operstate, assume the state was unchanged. */
0c9ee5d5 1829 operstate = link->kernel_operstate;
0d411b7f
YW
1830 else if (r < 0)
1831 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
0c9ee5d5 1832
0d411b7f 1833 if (link->flags == flags && link->kernel_operstate == operstate)
61135582
DS
1834 return 0;
1835
0c9ee5d5
YW
1836 if (link->flags != flags) {
1837 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
61135582 1838
0c9ee5d5
YW
1839 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",
1840 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1841 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1842 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1843 FLAG_STRING("UP", IFF_UP, link->flags, flags),
1844 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1845 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1846 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1847 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1848 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1849 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1850 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1851 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1852 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1853 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1854 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1855 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
1856 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
1857 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
1858 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
d236718c 1859
0c9ee5d5
YW
1860 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
1861 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
1862 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
1863 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
1864 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
1865 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
1866 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
1867 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
d236718c 1868
0c9ee5d5
YW
1869 if (unknown_flags_added)
1870 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
61135582 1871
0c9ee5d5
YW
1872 if (unknown_flags_removed)
1873 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
1874 }
61135582 1875
0d411b7f
YW
1876 link_was_admin_up = link->flags & IFF_UP;
1877 had_carrier = link_has_carrier(link);
1878
0c9ee5d5
YW
1879 link->flags = flags;
1880 link->kernel_operstate = operstate;
36161cba 1881
0c9ee5d5 1882 link_update_operstate(link, true);
61135582 1883
0d411b7f
YW
1884 if (!link_was_admin_up && (link->flags & IFF_UP)) {
1885 log_link_info(link, "Link UP");
22936833 1886
0d411b7f
YW
1887 r = link_admin_state_up(link);
1888 if (r < 0)
1889 return r;
1890 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1891 log_link_info(link, "Link DOWN");
0d4ad91d 1892
0d411b7f 1893 r = link_admin_state_down(link);
0d4ad91d
AR
1894 if (r < 0)
1895 return r;
7619683b
TG
1896 }
1897
0d411b7f
YW
1898 r = link_update_lldp(link);
1899 if (r < 0)
1900 return r;
b8941f74 1901
0d411b7f
YW
1902 if (!had_carrier && link_has_carrier(link)) {
1903 log_link_info(link, "Gained carrier");
0d4ad91d 1904
0d411b7f 1905 r = link_carrier_gained(link);
30de2b89
YW
1906 if (r < 0)
1907 return r;
0d411b7f
YW
1908 } else if (had_carrier && !link_has_carrier(link)) {
1909 log_link_info(link, "Lost carrier");
1910
1911 r = link_carrier_lost(link);
0b54c870
YW
1912 if (r < 0)
1913 return r;
b8941f74
TG
1914 }
1915
0d411b7f
YW
1916 return 0;
1917}
afe7fd56 1918
0d411b7f
YW
1919static int link_update_master(Link *link, sd_netlink_message *message) {
1920 int master_ifindex, r;
7465dd22 1921
0d411b7f
YW
1922 assert(link);
1923 assert(message);
69629de9 1924
0d411b7f
YW
1925 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
1926 if (r == -ENODATA)
1927 return 0;
1928 if (r < 0)
1929 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
807667f7 1930
0d411b7f
YW
1931 if (master_ifindex == link->master_ifindex)
1932 return 0;
807667f7 1933
0d411b7f
YW
1934 if (link->master_ifindex == 0)
1935 log_link_debug(link, "Joined to master interface: %i", master_ifindex);
1936 else if (master_ifindex == 0)
1937 log_link_debug(link, "Leaved from master interface: %i", link->master_ifindex);
1938 else
1939 log_link_debug(link, "Master interface is changed: %i → %i", link->master_ifindex, master_ifindex);
807667f7 1940
0d411b7f 1941 link_drop_from_master(link);
807667f7 1942
0d411b7f 1943 link->master_ifindex = master_ifindex;
413708d1 1944
0d411b7f
YW
1945 r = link_append_to_master(link);
1946 if (r < 0)
1947 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
7465dd22 1948
0d411b7f
YW
1949 return 0;
1950}
807667f7 1951
0d411b7f
YW
1952static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
1953 hw_addr_data hw_addr;
1954 int r;
d93d655c 1955
0d411b7f
YW
1956 assert(link);
1957 assert(message);
dfc58b47 1958
0d411b7f
YW
1959 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
1960 if (r < 0 && r != -ENODATA)
1961 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
4f882b2a 1962
0d411b7f
YW
1963 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &hw_addr);
1964 if (r == -ENODATA)
1965 return 0;
1966 if (r < 0)
1967 return log_link_warning_errno(link, r, "rtnl: failed to read hardware address: %m");
bb262ef0 1968
0d411b7f
YW
1969 if (link->hw_addr.length == hw_addr.length &&
1970 memcmp(link->hw_addr.addr.bytes, hw_addr.addr.bytes, hw_addr.length) == 0)
1971 return 0;
a61bb41c 1972
0d411b7f
YW
1973 link->hw_addr = hw_addr;
1974
1975 log_link_debug(link, "Gained new hardware address: %s", HW_ADDR_TO_STR(&hw_addr));
1976
1977 r = ipv4ll_update_mac(link);
a61bb41c 1978 if (r < 0)
0d411b7f 1979 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
a61bb41c 1980
0d411b7f
YW
1981 r = dhcp4_update_mac(link);
1982 if (r < 0)
1983 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
d236718c 1984
0d411b7f
YW
1985 r = dhcp6_update_mac(link);
1986 if (r < 0)
1987 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
1988
1989 r = radv_update_mac(link);
1990 if (r < 0)
1991 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
1992
1993 if (link->ndisc) {
1994 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.addr.ether);
d236718c 1995 if (r < 0)
0d411b7f
YW
1996 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
1997 }
d236718c 1998
0d411b7f
YW
1999 if (link->lldp) {
2000 r = sd_lldp_set_filter_address(link->lldp, &link->hw_addr.addr.ether);
61135582 2001 if (r < 0)
0d411b7f 2002 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP: %m");
61135582
DS
2003 }
2004
0d411b7f 2005 r = ipv4_dad_update_mac(link);
273eec24 2006 if (r < 0)
0d411b7f 2007 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
273eec24 2008
0d411b7f
YW
2009 return 0;
2010}
a61bb41c 2011
0d411b7f 2012static int link_update_mtu(Link *link, sd_netlink_message *message) {
717ba5fc 2013 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
0d411b7f 2014 int r;
a61bb41c 2015
0d411b7f
YW
2016 assert(link);
2017 assert(message);
2018
2019 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2020 if (r == -ENODATA)
2021 return 0;
2022 if (r < 0)
2023 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2024
717ba5fc
YW
2025 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2026 if (r < 0 && r != -ENODATA)
2027 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2028
2029 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2030 if (r < 0 && r != -ENODATA)
2031 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2032
2033 if (mtu == 0)
0d411b7f
YW
2034 return 0;
2035
717ba5fc
YW
2036 if (max_mtu == 0)
2037 max_mtu = UINT32_MAX;
2038
2039 link->min_mtu = min_mtu;
2040 link->max_mtu = max_mtu;
2041
0d411b7f
YW
2042 if (link->original_mtu == 0) {
2043 link->original_mtu = mtu;
717ba5fc
YW
2044 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2045 link->original_mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2046 }
2047
717ba5fc
YW
2048 if (link->mtu == mtu)
2049 return 0;
2050
2051 log_link_debug(link, "MTU is changed: %"PRIu32" → %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2052 link->mtu, mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2053
2054 link->mtu = mtu;
2055
2056 if (link->dhcp_client) {
2057 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
9c0a72f9 2058 if (r < 0)
0d411b7f
YW
2059 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2060 }
a61bb41c 2061
0d411b7f
YW
2062 if (link->radv) {
2063 r = sd_radv_set_mtu(link->radv, link->mtu);
9c0a72f9 2064 if (r < 0)
0d411b7f 2065 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
a61bb41c
TG
2066 }
2067
2068 return 0;
dd3efc09 2069}
fe8db0c5 2070
0d411b7f
YW
2071static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2072 _cleanup_strv_free_ char **altnames = NULL;
2073 char **n;
2074 int r;
2075
2076 assert(link);
2077 assert(message);
2078
2079 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
2080 if (r < 0 && r != -ENODATA)
2081 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2082
2083 STRV_FOREACH(n, link->alternative_names)
2084 hashmap_remove(link->manager->links_by_name, *n);
2085
2086 strv_free_and_replace(link->alternative_names, altnames);
2087
2088 STRV_FOREACH(n, link->alternative_names) {
2089 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2090 if (r < 0)
2091 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2092 }
2093
2094 return 0;
2095}
2096
2097static int link_update_name(Link *link, sd_netlink_message *message) {
2098 const char *ifname;
2099 int r;
2100
2101 assert(link);
2102 assert(message);
2103
2104 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2105 if (r == -ENODATA)
2106 /* Hmm?? But ok.*/
2107 return 0;
2108 if (r < 0)
2109 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2110
2111 if (streq(ifname, link->ifname))
2112 return 0;
2113
2114 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2115
2116 hashmap_remove(link->manager->links_by_name, link->ifname);
2117
2118 r = free_and_strdup(&link->ifname, ifname);
2119 if (r < 0)
2120 return log_oom_debug();
2121
2122 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2123 if (r < 0)
2124 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2125
2126 return 0;
2127}
2128
2129static int link_update(Link *link, sd_netlink_message *message) {
2130 int r;
2131
2132 assert(link);
2133 assert(message);
2134
2135 r = link_update_name(link, message);
2136 if (r < 0)
2137 return r;
2138
2139 r = link_update_alternative_names(link, message);
2140 if (r < 0)
2141 return r;
2142
2143 r = link_update_mtu(link, message);
2144 if (r < 0)
2145 return r;
2146
2147 r = link_update_hardware_address(link, message);
2148 if (r < 0)
2149 return r;
2150
2151 r = link_update_master(link, message);
2152 if (r < 0)
2153 return r;
2154
2155 return link_update_flags(link, message);
2156}
2157
0c9ee5d5
YW
2158static Link *link_drop_or_unref(Link *link) {
2159 if (!link)
2160 return NULL;
2161 if (!link->manager)
2162 return link_unref(link);
2163 return link_drop(link);
2164}
2165
2166DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2167
2168static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
0d411b7f 2169 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
0c9ee5d5 2170 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
0c9ee5d5
YW
2171 unsigned short iftype;
2172 int r, ifindex;
0c9ee5d5
YW
2173
2174 assert(manager);
2175 assert(message);
2176 assert(ret);
2177
0c9ee5d5
YW
2178 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2179 if (r < 0)
0d411b7f 2180 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
0c9ee5d5 2181 else if (ifindex <= 0)
0d411b7f 2182 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
0c9ee5d5
YW
2183
2184 r = sd_rtnl_message_link_get_type(message, &iftype);
2185 if (r < 0)
0d411b7f 2186 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
0c9ee5d5
YW
2187
2188 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2189 if (r < 0)
0d411b7f 2190 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
0c9ee5d5
YW
2191
2192 /* check for link kind */
2193 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2194 if (r >= 0) {
0d411b7f
YW
2195 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2196 if (r < 0 && r != -ENODATA)
2197 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
0c9ee5d5
YW
2198 r = sd_netlink_message_exit_container(message);
2199 if (r < 0)
0d411b7f 2200 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
0c9ee5d5
YW
2201 }
2202
0d411b7f
YW
2203 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2204 return log_oom_debug();
2205
2206 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2207 return log_oom_debug();
2208
2209 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2210 return log_oom_debug();
2211
0c9ee5d5
YW
2212 link = new(Link, 1);
2213 if (!link)
2214 return -ENOMEM;
2215
2216 *link = (Link) {
2217 .n_ref = 1,
2218 .state = LINK_STATE_PENDING,
2219 .online_state = _LINK_ONLINE_STATE_INVALID,
2220 .ifindex = ifindex,
2221 .iftype = iftype,
2222 .ifname = TAKE_PTR(ifname),
2223 .kind = TAKE_PTR(kind),
2224
0d411b7f
YW
2225 .state_file = TAKE_PTR(state_file),
2226 .lease_file = TAKE_PTR(lease_file),
2227 .lldp_file = TAKE_PTR(lldp_file),
2228
0c9ee5d5
YW
2229 .n_dns = UINT_MAX,
2230 .dns_default_route = -1,
2231 .llmnr = _RESOLVE_SUPPORT_INVALID,
2232 .mdns = _RESOLVE_SUPPORT_INVALID,
2233 .dnssec_mode = _DNSSEC_MODE_INVALID,
2234 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2235 };
2236
2237 r = hashmap_ensure_put(&manager->links, NULL, INT_TO_PTR(link->ifindex), link);
2238 if (r < 0)
0d411b7f 2239 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
0c9ee5d5
YW
2240
2241 link->manager = manager;
2242
0d411b7f 2243 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
0c9ee5d5 2244 if (r < 0)
0d411b7f 2245 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
0c9ee5d5
YW
2246
2247 r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac);
2248 if (r < 0)
2249 log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
2250
2251 r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
2252 if (r < 0)
2253 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2254
0d411b7f 2255 log_link_debug(link, "Link %d added", link->ifindex);
0c9ee5d5 2256 *ret = TAKE_PTR(link);
0c9ee5d5
YW
2257 return 0;
2258}
2259
0d411b7f 2260int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
3be9d62a
YW
2261 Link *link = NULL;
2262 NetDev *netdev = NULL;
2263 uint16_t type;
2264 const char *name;
2265 int r, ifindex;
2266
2267 assert(rtnl);
2268 assert(message);
0d411b7f 2269 assert(manager);
3be9d62a
YW
2270
2271 if (sd_netlink_message_is_error(message)) {
2272 r = sd_netlink_message_get_errno(message);
2273 if (r < 0)
2274 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2275
2276 return 0;
2277 }
2278
2279 r = sd_netlink_message_get_type(message, &type);
2280 if (r < 0) {
2281 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2282 return 0;
2283 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2284 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2285 return 0;
2286 }
2287
2288 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2289 if (r < 0) {
2290 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2291 return 0;
2292 } else if (ifindex <= 0) {
2293 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2294 return 0;
2295 }
2296
2297 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2298 if (r < 0) {
2299 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2300 return 0;
2301 }
2302
0d411b7f
YW
2303 (void) link_get(manager, ifindex, &link);
2304 (void) netdev_get(manager, name, &netdev);
3be9d62a
YW
2305
2306 switch (type) {
2307 case RTM_NEWLINK:
0d411b7f
YW
2308 if (netdev) {
2309 /* netdev exists, so make sure the ifindex matches */
2310 r = netdev_set_ifindex(netdev, message);
2311 if (r < 0) {
2312 log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m");
2313 return 0;
2314 }
2315 }
2316
3be9d62a
YW
2317 if (!link) {
2318 /* link is new, so add it */
0d411b7f 2319 r = link_new(manager, message, &link);
3be9d62a 2320 if (r < 0) {
0d411b7f 2321 log_warning_errno(r, "Could not process new link message: %m");
3be9d62a
YW
2322 return 0;
2323 }
3be9d62a 2324
0d411b7f 2325 r = link_update(link, message);
3be9d62a 2326 if (r < 0) {
0d411b7f
YW
2327 log_warning_errno(r, "Could not process link message: %m");
2328 link_enter_failed(link);
3be9d62a
YW
2329 return 0;
2330 }
3be9d62a 2331
0d411b7f
YW
2332 r = link_check_initialized(link);
2333 if (r < 0) {
2334 log_warning_errno(r, "Failed to check link is initialized: %m");
2335 link_enter_failed(link);
2336 return 0;
2337 }
2338 } else {
2339 r = link_update(link, message);
2340 if (r < 0) {
2341 log_warning_errno(r, "Could not process link message: %m");
2342 link_enter_failed(link);
2343 return 0;
2344 }
3be9d62a
YW
2345 }
2346
2347 break;
2348
2349 case RTM_DELLINK:
2350 link_drop(link);
2351 netdev_drop(netdev);
2352
2353 break;
2354
2355 default:
2356 assert_not_reached("Received link message with invalid RTNL message type.");
2357 }
2358
2359 return 1;
2360}
2361
79c6e114
YW
2362int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2363 uint16_t message_type;
2364 int r;
2365
2366 assert(m);
2367 assert(link);
2368 assert(error_msg);
2369
2370 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2371 return 0;
2372
2373 r = sd_netlink_message_get_errno(m);
2374 if (r < 0) {
2375 log_link_message_warning_errno(link, m, r, error_msg);
2376 link_enter_failed(link);
2377 return 0;
2378 }
2379
2380 r = sd_netlink_message_get_type(m, &message_type);
2381 if (r < 0) {
2382 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2383 return 0;
2384 }
2385 if (message_type != RTM_NEWLINK) {
2386 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2387 return 0;
2388 }
2389
2390 r = link_update(link, m);
2391 if (r < 0) {
2392 link_enter_failed(link);
2393 return 0;
2394 }
2395
2396 return 1;
2397}
2398
2399int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2400 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2401 int r;
2402
2403 assert(link);
2404 assert(link->manager);
2405 assert(link->manager->rtnl);
2406 assert(callback);
2407
2408 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2409 if (r < 0)
2410 return r;
2411
2412 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2413 link_netlink_destroy_callback, link);
2414 if (r < 0)
2415 return r;
2416
2417 link_ref(link);
2418 return 0;
2419}
2420
fe8db0c5 2421static const char* const link_state_table[_LINK_STATE_MAX] = {
8434fd5c 2422 [LINK_STATE_PENDING] = "pending",
bd08ce56 2423 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 2424 [LINK_STATE_CONFIGURING] = "configuring",
fe8db0c5 2425 [LINK_STATE_CONFIGURED] = "configured",
57bd6899 2426 [LINK_STATE_UNMANAGED] = "unmanaged",
fe8db0c5 2427 [LINK_STATE_FAILED] = "failed",
370e9930 2428 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
2429};
2430
2431DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);