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