]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
network/neighbor: also remove neighbor on cancelling request
[thirdparty/systemd.git] / src / network / networkd-link.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f579559b 2
176b8be1 3#include <net/if.h>
9aa5d8ba 4#include <netinet/in.h>
f579559b 5#include <linux/if.h>
8f815e8b 6#include <linux/if_arp.h>
518cd6b5 7#include <linux/if_link.h>
2a0d07d6 8#include <linux/netdevice.h>
8430841b 9#include <sys/socket.h>
4cc7a82c 10#include <unistd.h>
f579559b 11
b5efdb8a 12#include "alloc-util.h"
8a77e245 13#include "arphrd-util.h"
c0267a59 14#include "batadv.h"
737f1405
YW
15#include "bond.h"
16#include "bridge.h"
1346b1f0 17#include "bus-util.h"
3be9d62a
YW
18#include "device-private.h"
19#include "device-util.h"
bd91b83e 20#include "dhcp-lease-internal.h"
686d13b9 21#include "env-file.h"
4bb7cc82 22#include "ethtool-util.h"
6706ce2f 23#include "event-util.h"
3ffd4af2 24#include "fd-util.h"
cf1d700d 25#include "fileio.h"
176b8be1 26#include "format-util.h"
af664001 27#include "fs-util.h"
e2341b6b 28#include "glyph-util.h"
3ae6b3bf 29#include "logarithm.h"
ef118d00 30#include "missing_network.h"
cf1d700d 31#include "netlink-util.h"
c6f7c917 32#include "network-internal.h"
fb486c90 33#include "networkd-address-label.h"
093e3533 34#include "networkd-address.h"
9671ae9d 35#include "networkd-bridge-fdb.h"
ff9e0783 36#include "networkd-bridge-mdb.h"
11cee6ef 37#include "networkd-bridge-vlan.h"
3ddcbeea 38#include "networkd-can.h"
d5ebcf65 39#include "networkd-dhcp-prefix-delegation.h"
8fcf1d61 40#include "networkd-dhcp-server.h"
ca5ad760
YW
41#include "networkd-dhcp4.h"
42#include "networkd-dhcp6.h"
76a86ffd 43#include "networkd-ipv4acd.h"
ca5ad760 44#include "networkd-ipv4ll.h"
76c5a0f2 45#include "networkd-ipv6-proxy-ndp.h"
6a1af3d4
YW
46#include "networkd-link-bus.h"
47#include "networkd-link.h"
8e1ad1ea 48#include "networkd-lldp-tx.h"
23f53b99 49#include "networkd-manager.h"
1e7a0e21 50#include "networkd-ndisc.h"
e4a71bf3 51#include "networkd-neighbor.h"
75156ccb 52#include "networkd-nexthop.h"
19d9a5ad 53#include "networkd-queue.h"
7465dd22 54#include "networkd-radv.h"
fc35a9f8 55#include "networkd-route-util.h"
3b6a3bde 56#include "networkd-route.h"
bce67bbe 57#include "networkd-routing-policy-rule.h"
0fa8ee6c 58#include "networkd-setlink.h"
19d9a5ad 59#include "networkd-sriov.h"
3b5a4fc6 60#include "networkd-state-file.h"
19d9a5ad 61#include "networkd-sysctl.h"
986b2514 62#include "networkd-wifi.h"
cf1d700d
TG
63#include "set.h"
64#include "socket-util.h"
15a5e950 65#include "stdio-util.h"
8b43440b 66#include "string-table.h"
51517f9e 67#include "strv.h"
34658df2 68#include "tc.h"
e4de7287 69#include "tmpfile-util.h"
af7a86b8 70#include "tuntap.h"
299ad32d 71#include "udev-util.h"
737f1405 72#include "vrf.h"
fc2f9534 73
5e0534f1 74bool link_ipv6_enabled(Link *link) {
439689c6
SS
75 assert(link);
76
77 if (!socket_ipv6_is_supported())
78 return false;
79
5fb5fae9 80 if (link->iftype == ARPHRD_CAN)
2b00a4e0
TY
81 return false;
82
5fb5fae9
YW
83 if (!link->network)
84 return false;
85
86 if (link->network->bond)
af9ba57a
YW
87 return false;
88
bd7e0a3f 89 if (link_may_have_ipv6ll(link, /* check_multicast = */ false))
adfeee49
YW
90 return true;
91
92 if (network_has_static_ipv6_configurations(link->network))
93 return true;
94
95 return false;
439689c6
SS
96}
97
fc35a9f8
YW
98bool link_has_ipv6_connectivity(Link *link) {
99 LinkAddressState ipv6_address_state;
100
101 assert(link);
102
103 link_get_address_states(link, NULL, &ipv6_address_state, NULL);
104
105 switch (ipv6_address_state) {
106 case LINK_ADDRESS_STATE_ROUTABLE:
107 /* If the interface has a routable IPv6 address, then we assume yes. */
108 return true;
109
110 case LINK_ADDRESS_STATE_DEGRADED:
111 /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
112 * there is an IPv6 default gateway. */
113 return link_has_default_gateway(link, AF_INET6);
114
115 case LINK_ADDRESS_STATE_OFF:
116 /* No IPv6 address. */
117 return false;
118
119 default:
120 assert_not_reached();
121 }
122}
123
e33232d4 124static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
7191a57a
YW
125 assert(link);
126
d8d4db58
YW
127 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
128 return false;
129
2fd3908b
YW
130 if (!link->network)
131 return allow_unmanaged;
7191a57a 132
baa95d22
YW
133 if (!link->network->configure_without_carrier) {
134 if (link->set_flags_messages > 0)
135 return false;
136
137 if (!link_has_carrier(link))
138 return false;
139 }
7191a57a 140
0fa8ee6c
YW
141 if (link->set_link_messages > 0)
142 return false;
143
112a0972
YW
144 if (!link->activated)
145 return false;
112a0972 146
7191a57a
YW
147 return true;
148}
149
e33232d4
YW
150bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
151 return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
152}
153
15761549
YW
154void link_ntp_settings_clear(Link *link) {
155 link->ntp = strv_free(link->ntp);
156}
157
158void link_dns_settings_clear(Link *link) {
f5fbe71d 159 if (link->n_dns != UINT_MAX)
e77bd3fd
YW
160 for (unsigned i = 0; i < link->n_dns; i++)
161 in_addr_full_free(link->dns[i]);
15761549 162 link->dns = mfree(link->dns);
f5fbe71d 163 link->n_dns = UINT_MAX;
15761549 164
6e4571f0
YW
165 link->search_domains = ordered_set_free(link->search_domains);
166 link->route_domains = ordered_set_free(link->route_domains);
15761549
YW
167
168 link->dns_default_route = -1;
169 link->llmnr = _RESOLVE_SUPPORT_INVALID;
170 link->mdns = _RESOLVE_SUPPORT_INVALID;
171 link->dnssec_mode = _DNSSEC_MODE_INVALID;
172 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
173
174 link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
175}
176
1a6bb31f
YW
177static void link_free_engines(Link *link) {
178 if (!link)
179 return;
180
181 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
b3fac19f 182
1a6bb31f
YW
183 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
184 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
e49bad01 185 link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
1a6bb31f 186
35778343 187 link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
c01b9b87 188 link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
1a6bb31f 189
5385e5f9
YW
190 link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
191
1a6bb31f 192 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
b3fac19f 193
1a6bb31f 194 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1633c457 195 link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
b3fac19f 196
1a6bb31f 197 link->ndisc = sd_ndisc_unref(link->ndisc);
b3fac19f
YW
198 link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
199 ndisc_flush(link);
200
1a6bb31f
YW
201 link->radv = sd_radv_unref(link->radv);
202}
203
8301aa0b 204static Link *link_free(Link *link) {
8301aa0b 205 assert(link);
f579559b 206
15761549
YW
207 link_ntp_settings_clear(link);
208 link_dns_settings_clear(link);
209
8eec0b9d 210 link->routes = set_free(link->routes);
8eec0b9d 211 link->neighbors = set_free(link->neighbors);
8eec0b9d 212 link->addresses = set_free(link->addresses);
3a67b8bb
YW
213 link->qdiscs = set_free(link->qdiscs);
214 link->tclasses = set_free(link->tclasses);
adda1ed9 215
a27588d4 216 link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
4b409e85 217
1a6bb31f 218 link_free_engines(link);
49699bac 219
b0ff6dea 220 set_free(link->sr_iov_virt_port_ifindices);
c166a070 221 free(link->ifname);
572b21d9 222 strv_free(link->alternative_names);
ceac4078 223 free(link->kind);
8d968fdd 224 free(link->ssid);
6706ce2f 225 free(link->previous_ssid);
c643bda5 226 free(link->driver);
6cad256d 227
a34e58d4
YW
228 unlink_and_free(link->lease_file);
229 unlink_and_free(link->lldp_file);
230 unlink_and_free(link->state_file);
c166a070 231
8de56fb3 232 sd_device_unref(link->dev);
9aa2585e 233 netdev_unref(link->netdev);
b5db00e5 234
0d4ad91d 235 hashmap_free(link->bound_to_links);
0d4ad91d
AR
236 hashmap_free(link->bound_by_links);
237
5f707e12 238 set_free_with_destructor(link->slaves, link_unref);
033295c1 239
c9c908a6
YW
240 network_unref(link->network);
241
6706ce2f
YW
242 sd_event_source_disable_unref(link->carrier_lost_timer);
243
8301aa0b 244 return mfree(link);
14b746f7
TG
245}
246
8301aa0b 247DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
14b746f7 248
6eab614d 249int link_get_by_index(Manager *m, int ifindex, Link **ret) {
11a7f229 250 Link *link;
11a7f229
TG
251
252 assert(m);
c2aed128
YW
253
254 if (ifindex <= 0)
255 return -EINVAL;
11a7f229 256
6eab614d 257 link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
11a7f229
TG
258 if (!link)
259 return -ENODEV;
260
0b54c870
YW
261 if (ret)
262 *ret = link;
263 return 0;
264}
265
266int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
267 Link *link;
268
269 assert(m);
270 assert(ifname);
11a7f229 271
0b54c870
YW
272 link = hashmap_get(m->links_by_name, ifname);
273 if (!link)
274 return -ENODEV;
275
276 if (ret)
277 *ret = link;
11a7f229
TG
278 return 0;
279}
280
fe321d45
YW
281int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
282 Link *link;
283
284 assert(m);
285 assert(hw_addr);
286
287 link = hashmap_get(m->links_by_hw_addr, hw_addr);
288 if (!link)
289 return -ENODEV;
290
291 if (ret)
292 *ret = link;
293 return 0;
294}
295
81357285
YW
296int link_get_master(Link *link, Link **ret) {
297 assert(link);
298 assert(link->manager);
299 assert(ret);
300
301 if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
302 return -ENODEV;
303
6eab614d 304 return link_get_by_index(link->manager, link->master_ifindex, ret);
81357285
YW
305}
306
af9ba57a 307void link_set_state(Link *link, LinkState state) {
e331e246
TG
308 assert(link);
309
310 if (link->state == state)
311 return;
312
0beb9542
YW
313 log_link_debug(link, "State changed: %s -> %s",
314 link_state_to_string(link->state),
315 link_state_to_string(state));
316
e331e246
TG
317 link->state = state;
318
319 link_send_changed(link, "AdministrativeState", NULL);
9092113d 320 link_dirty(link);
e331e246
TG
321}
322
2a99eed0 323int link_stop_engines(Link *link, bool may_keep_dhcp) {
372acaad 324 int r, ret = 0;
111bb8f9
TG
325
326 assert(link);
327 assert(link->manager);
328 assert(link->manager->event);
329
80060352
ZJS
330 bool keep_dhcp = may_keep_dhcp &&
331 link->network &&
76a86ffd 332 !link->network->dhcp_send_decline && /* IPv4 ACD for the DHCPv4 address is running. */
80060352
ZJS
333 (link->manager->restarting ||
334 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
335
84add3cd 336 if (!keep_dhcp) {
372acaad
YW
337 r = sd_dhcp_client_stop(link->dhcp_client);
338 if (r < 0)
339 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
111bb8f9
TG
340 }
341
372acaad
YW
342 r = sd_dhcp_server_stop(link->dhcp_server);
343 if (r < 0)
344 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
2a99eed0 345
372acaad
YW
346 r = sd_lldp_rx_stop(link->lldp_rx);
347 if (r < 0)
348 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
c01b9b87 349
372acaad
YW
350 r = sd_lldp_tx_stop(link->lldp_tx);
351 if (r < 0)
352 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
9cc65242 353
372acaad
YW
354 r = sd_ipv4ll_stop(link->ipv4ll);
355 if (r < 0)
356 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
dd43110f 357
372acaad
YW
358 r = ipv4acd_stop(link);
359 if (r < 0)
360 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
051e77ca 361
372acaad
YW
362 r = sd_dhcp6_client_stop(link->dhcp6_client);
363 if (r < 0)
364 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
4138fb2c 365
372acaad
YW
366 r = dhcp_pd_remove(link, /* only_marked = */ false);
367 if (r < 0)
368 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
1633c457 369
372acaad
YW
370 r = ndisc_stop(link);
371 if (r < 0)
372 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
4138fb2c 373
259c65f3
YW
374 ndisc_flush(link);
375
372acaad
YW
376 r = sd_radv_stop(link->radv);
377 if (r < 0)
378 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
7465dd22 379
372acaad 380 return ret;
111bb8f9
TG
381}
382
b22d8a00 383void link_enter_failed(Link *link) {
ef1ba606 384 assert(link);
f882c247 385
370e9930 386 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2139694e
TG
387 return;
388
6a7a4e4d 389 log_link_warning(link, "Failed");
449f7554 390
e331e246 391 link_set_state(link, LINK_STATE_FAILED);
fe8db0c5 392
2a99eed0 393 (void) link_stop_engines(link, false);
f882c247
TG
394}
395
6accfd31
DA
396void link_check_ready(Link *link) {
397 Address *a;
6accfd31
DA
398
399 assert(link);
400
5f58af25
YW
401 if (link->state == LINK_STATE_CONFIGURED)
402 return;
403
d19b9939
YW
404 if (link->state != LINK_STATE_CONFIGURING)
405 return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
6accfd31
DA
406
407 if (!link->network)
a7f07cbe 408 return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
6accfd31 409
7558f9e7
YW
410 if (!link->tc_configured)
411 return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
600b7898 412
a7f07cbe
YW
413 if (link->set_link_messages > 0)
414 return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
415
416 if (!link->activated)
417 return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
418
7558f9e7
YW
419 if (link->iftype == ARPHRD_CAN) {
420 /* let's shortcut things for CAN which doesn't need most of checks below. */
421 link_set_state(link, LINK_STATE_CONFIGURED);
422 return;
423 }
424
879377ca
YW
425 if (!link->stacked_netdevs_created)
426 return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
427
76c5a0f2 428 if (!link->static_addresses_configured)
d19b9939 429 return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
6accfd31 430
354bc760
YW
431 if (!link->static_address_labels_configured)
432 return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
433
e5b35bf6
YW
434 if (!link->static_bridge_fdb_configured)
435 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
436
ff9e0783
YW
437 if (!link->static_bridge_mdb_configured)
438 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
439
fdeba3f5
YW
440 if (!link->static_ipv6_proxy_ndp_configured)
441 return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
442
40ca350e
YW
443 if (!link->static_neighbors_configured)
444 return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
445
d19b9939
YW
446 if (!link->static_nexthops_configured)
447 return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
c16c7808 448
76c5a0f2
YW
449 if (!link->static_routes_configured)
450 return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
451
0e5ef6be 452 if (!link->static_routing_policy_rules_configured)
d19b9939 453 return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
6accfd31 454
d19b9939
YW
455 if (!link->sr_iov_configured)
456 return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
518cd6b5 457
4bcb8625
YW
458 /* IPv6LL is assigned after the link gains its carrier. */
459 if (!link->network->configure_without_carrier &&
460 link_ipv6ll_enabled(link) &&
461 !in6_addr_is_set(&link->ipv6ll_address))
462 return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
463
9e7d91ed
YW
464 /* All static addresses must be ready. */
465 bool has_static_address = false;
466 SET_FOREACH(a, link->addresses) {
467 if (a->source != NETWORK_CONFIG_SOURCE_STATIC)
468 continue;
469 if (!address_is_ready(a))
470 return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
471 IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
472 has_static_address = true;
473 }
474
475 /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
476 if (has_static_address)
477 goto ready;
478
5f950e56
YW
479 /* If no dynamic addressing protocol enabled, assume the interface is ready.
480 * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
481 if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
482 !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
483 (link->network->configure_without_carrier || !link_ipv6_accept_ra_enabled(link)))
484 goto ready;
485
486 bool ipv4ll_ready =
487 link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
488 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
489 bool dhcp4_ready =
490 link_dhcp4_enabled(link) && link->dhcp4_configured &&
491 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
492 bool dhcp6_ready =
493 link_dhcp6_enabled(link) && link->dhcp6_configured &&
494 (!link->network->dhcp6_use_address ||
495 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
496 bool dhcp_pd_ready =
497 link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
498 (!link->network->dhcp_pd_assign ||
499 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
500 bool ndisc_ready =
501 link_ipv6_accept_ra_enabled(link) && link->ndisc_configured &&
502 (!link->network->ipv6_accept_ra_use_autonomous_prefix ||
503 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
504
195b83ed
YW
505 /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
506 if (dhcp_pd_is_uplink(link, link, /* accept_auto = */ false)) {
507 if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
0ad8d953 508 sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
195b83ed
YW
509 if (!dhcp4_ready)
510 return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
511 if (!dhcp_pd_ready)
512 return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
513 }
514
515 if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
fb70992d 516 sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
195b83ed
YW
517 if (!dhcp6_ready)
518 return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
519 if (!dhcp_pd_ready)
520 return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
521 }
522 }
523
5f950e56
YW
524 /* At least one dynamic addressing protocol is finished. */
525 if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
526 return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
527
528 log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
529 __func__,
530 yes_no(ipv4ll_ready),
531 yes_no(dhcp4_ready),
532 yes_no(dhcp6_ready),
533 yes_no(dhcp_pd_ready),
534 yes_no(ndisc_ready));
535
536ready:
29836c16 537 link_set_state(link, LINK_STATE_CONFIGURED);
6accfd31
DA
538}
539
97108953 540static int link_request_static_configs(Link *link) {
f882c247
TG
541 int r;
542
543 assert(link);
544 assert(link->network);
f5be5601 545 assert(link->state != _LINK_STATE_INVALID);
f882c247 546
354bc760 547 r = link_request_static_addresses(link);
0e5ef6be
YW
548 if (r < 0)
549 return r;
550
354bc760 551 r = link_request_static_address_labels(link);
f3ef324d
YW
552 if (r < 0)
553 return r;
e4a71bf3 554
e5b35bf6
YW
555 r = link_request_static_bridge_fdb(link);
556 if (r < 0)
557 return r;
558
9a038aac
YW
559 r = link_request_static_bridge_mdb(link);
560 if (r < 0)
561 return r;
562
fdeba3f5
YW
563 r = link_request_static_ipv6_proxy_ndp_addresses(link);
564 if (r < 0)
565 return r;
566
40ca350e 567 r = link_request_static_neighbors(link);
682c65b0
YW
568 if (r < 0)
569 return r;
bd6379ec 570
76c5a0f2 571 r = link_request_static_nexthops(link, false);
fe2bc17c
YW
572 if (r < 0)
573 return r;
f882c247 574
76c5a0f2
YW
575 r = link_request_static_routes(link, false);
576 if (r < 0)
577 return r;
578
579 r = link_request_static_routing_policy_rules(link);
5ae0fb7f
YW
580 if (r < 0)
581 return r;
d4cdbea5 582
f882c247
TG
583 return 0;
584}
585
1187fc33
YW
586static int link_request_stacked_netdevs(Link *link) {
587 NetDev *netdev;
e16e4b3b
DS
588 int r;
589
590 assert(link);
591
1187fc33 592 link->stacked_netdevs_created = false;
e16e4b3b 593
1187fc33 594 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
b14686ff 595 r = link_request_stacked_netdev(link, netdev);
1187fc33
YW
596 if (r < 0)
597 return r;
598 }
e16e4b3b 599
bb916f35 600 if (link->create_stacked_netdev_messages == 0) {
1187fc33 601 link->stacked_netdevs_created = true;
bb916f35
YW
602 link_check_ready(link);
603 }
e16e4b3b
DS
604
605 return 0;
606}
607
8566df79 608static int link_acquire_dynamic_ipv6_conf(Link *link) {
e7ab854c
TG
609 int r;
610
611 assert(link);
612
0f96a823
YW
613 r = radv_start(link);
614 if (r < 0)
615 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
7465dd22 616
294f129b
YW
617 r = ndisc_start(link);
618 if (r < 0)
619 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
cd305af1 620
294f129b
YW
621 r = dhcp6_start(link);
622 if (r < 0)
623 return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
cd305af1 624
e7ab854c
TG
625 return 0;
626}
627
8566df79 628static int link_acquire_dynamic_ipv4_conf(Link *link) {
ff254138
TG
629 int r;
630
631 assert(link);
ff254138
TG
632 assert(link->manager);
633 assert(link->manager->event);
634
0107b769 635 if (link->dhcp_client) {
294f129b 636 r = dhcp4_start(link);
0107b769 637 if (r < 0)
294f129b 638 return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
0107b769 639
ccffa166 640 log_link_debug(link, "Acquiring DHCPv4 lease.");
5c1d3fc9 641
ccffa166 642 } else if (link->ipv4ll) {
59c27231
AK
643 if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
644 r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
645 if (r < 0)
646 return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
647 }
648
5c1d3fc9 649 r = sd_ipv4ll_start(link->ipv4ll);
6a7a4e4d
LP
650 if (r < 0)
651 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
ccffa166
YW
652
653 log_link_debug(link, "Acquiring IPv4 link-local address.");
5c1d3fc9
UTL
654 }
655
ab486ef4
YW
656 if (link->dhcp_server) {
657 r = sd_dhcp_server_start(link->dhcp_server);
658 if (r < 0)
659 return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
660 }
661
76a86ffd
YW
662 r = ipv4acd_start(link);
663 if (r < 0)
664 return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
665
6fc25497
SS
666 return 0;
667}
668
8566df79 669static int link_acquire_dynamic_conf(Link *link) {
6fc25497
SS
670 int r;
671
672 assert(link);
0f96a823 673 assert(link->network);
6fc25497 674
8566df79 675 r = link_acquire_dynamic_ipv4_conf(link);
6fc25497
SS
676 if (r < 0)
677 return r;
678
94876904 679 if (in6_addr_is_set(&link->ipv6ll_address)) {
8566df79 680 r = link_acquire_dynamic_ipv6_conf(link);
6fc25497
SS
681 if (r < 0)
682 return r;
683 }
684
a27588d4 685 if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
0f96a823
YW
686 /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
687 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
688 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
689 * below will be called in radv_start(). */
a27588d4 690 r = dhcp_request_prefix_delegation(link);
0f96a823 691 if (r < 0)
a27588d4 692 return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
0f96a823
YW
693 }
694
c01b9b87
YW
695 if (link->lldp_tx) {
696 r = sd_lldp_tx_start(link->lldp_tx);
697 if (r < 0)
698 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
699 }
8e1ad1ea 700
35778343
YW
701 if (link->lldp_rx) {
702 r = sd_lldp_rx_start(link->lldp_rx);
f8549910
YW
703 if (r < 0)
704 return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
705 }
706
ff254138
TG
707 return 0;
708}
709
7657ec3e 710int link_ipv6ll_gained(Link *link) {
0c9ee5d5 711 int r;
deb2e523 712
0c9ee5d5 713 assert(link);
deb2e523 714
0c9ee5d5 715 log_link_info(link, "Gained IPv6LL");
deb2e523 716
7657ec3e
YW
717 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
718 return 0;
0c9ee5d5 719
7657ec3e
YW
720 r = link_acquire_dynamic_ipv6_conf(link);
721 if (r < 0)
722 return r;
0c9ee5d5 723
7657ec3e 724 link_check_ready(link);
0c9ee5d5 725 return 0;
deb2e523
TG
726}
727
112a0972 728int link_handle_bound_to_list(Link *link) {
0d4ad91d
AR
729 bool required_up = false;
730 bool link_is_up = false;
852a3916 731 Link *l;
0d4ad91d
AR
732
733 assert(link);
734
ad82f0c3
YW
735 /* If at least one interface in bound_to_links has carrier, then make this interface up.
736 * If all interfaces in bound_to_links do not, then make this interface down. */
737
0d4ad91d
AR
738 if (hashmap_isempty(link->bound_to_links))
739 return 0;
740
741 if (link->flags & IFF_UP)
742 link_is_up = true;
743
852a3916 744 HASHMAP_FOREACH(l, link->bound_to_links)
0d4ad91d
AR
745 if (link_has_carrier(l)) {
746 required_up = true;
747 break;
748 }
749
852a3916 750 if (!required_up && link_is_up)
68f52063 751 return link_request_to_bring_up_or_down(link, /* up = */ false);
852a3916 752 if (required_up && !link_is_up)
68f52063 753 return link_request_to_bring_up_or_down(link, /* up = */ true);
0d4ad91d
AR
754
755 return 0;
756}
757
758static int link_handle_bound_by_list(Link *link) {
0d4ad91d
AR
759 Link *l;
760 int r;
761
762 assert(link);
763
ad82f0c3
YW
764 /* Update up or down state of interfaces which depend on this interface's carrier state. */
765
0d4ad91d
AR
766 if (hashmap_isempty(link->bound_by_links))
767 return 0;
768
852a3916 769 HASHMAP_FOREACH(l, link->bound_by_links) {
0d4ad91d
AR
770 r = link_handle_bound_to_list(l);
771 if (r < 0)
772 return r;
773 }
774
775 return 0;
776}
777
778static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
779 int r;
780
781 assert(link);
782 assert(carrier);
783
784 if (link == carrier)
785 return 0;
786
787 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
788 return 0;
789
190b3b5c 790 r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
0d4ad91d
AR
791 if (r < 0)
792 return r;
793
9092113d
YW
794 link_dirty(link);
795
0d4ad91d
AR
796 return 0;
797}
798
799static int link_new_bound_by_list(Link *link) {
800 Manager *m;
801 Link *carrier;
0d4ad91d 802 int r;
0d4ad91d
AR
803
804 assert(link);
805 assert(link->manager);
806
807 m = link->manager;
808
6eab614d 809 HASHMAP_FOREACH(carrier, m->links_by_index) {
0d4ad91d
AR
810 if (!carrier->network)
811 continue;
812
813 if (strv_isempty(carrier->network->bind_carrier))
814 continue;
815
191a3f16 816 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
0d4ad91d
AR
817 r = link_put_carrier(link, carrier, &link->bound_by_links);
818 if (r < 0)
819 return r;
0d4ad91d
AR
820 }
821 }
822
90e74a66 823 HASHMAP_FOREACH(carrier, link->bound_by_links) {
0d4ad91d
AR
824 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
825 if (r < 0)
826 return r;
0d4ad91d
AR
827 }
828
829 return 0;
830}
831
832static int link_new_bound_to_list(Link *link) {
833 Manager *m;
834 Link *carrier;
0d4ad91d 835 int r;
0d4ad91d
AR
836
837 assert(link);
838 assert(link->manager);
839
840 if (!link->network)
841 return 0;
842
843 if (strv_isempty(link->network->bind_carrier))
844 return 0;
845
846 m = link->manager;
847
6eab614d 848 HASHMAP_FOREACH(carrier, m->links_by_index) {
191a3f16 849 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
0d4ad91d
AR
850 r = link_put_carrier(link, carrier, &link->bound_to_links);
851 if (r < 0)
852 return r;
0d4ad91d
AR
853 }
854 }
855
852a3916 856 HASHMAP_FOREACH(carrier, link->bound_to_links) {
0d4ad91d
AR
857 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
858 if (r < 0)
859 return r;
0d4ad91d
AR
860 }
861
862 return 0;
863}
864
0d4ad91d 865static void link_free_bound_to_list(Link *link) {
9092113d 866 bool updated = false;
0d4ad91d 867 Link *bound_to;
0d4ad91d 868
9092113d
YW
869 assert(link);
870
871 while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
872 updated = true;
0d4ad91d
AR
873
874 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
84de38c5 875 link_dirty(bound_to);
0d4ad91d
AR
876 }
877
9092113d
YW
878 if (updated)
879 link_dirty(link);
0d4ad91d
AR
880}
881
882static void link_free_bound_by_list(Link *link) {
9092113d 883 bool updated = false;
0d4ad91d 884 Link *bound_by;
0d4ad91d 885
9092113d
YW
886 assert(link);
887
888 while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
889 updated = true;
0d4ad91d
AR
890
891 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
84de38c5 892 link_dirty(bound_by);
0d4ad91d
AR
893 link_handle_bound_to_list(bound_by);
894 }
895 }
896
9092113d
YW
897 if (updated)
898 link_dirty(link);
0d4ad91d
AR
899}
900
81357285 901static int link_append_to_master(Link *link) {
5f707e12
YW
902 Link *master;
903 int r;
904
905 assert(link);
5f707e12 906
81357285
YW
907 /* - The link may have no master.
908 * - RTM_NEWLINK message about master interface may not be received yet. */
909 if (link_get_master(link, &master) < 0)
910 return 0;
5f707e12 911
de7fef4b 912 r = set_ensure_put(&master->slaves, NULL, link);
38288f0b 913 if (r <= 0)
5f707e12
YW
914 return r;
915
916 link_ref(link);
917 return 0;
918}
919
81357285 920static void link_drop_from_master(Link *link) {
5f707e12
YW
921 Link *master;
922
923 assert(link);
924
81357285 925 if (!link->manager)
5f707e12
YW
926 return;
927
81357285 928 if (link_get_master(link, &master) < 0)
5f707e12
YW
929 return;
930
931 link_unref(set_remove(master->slaves, link));
932}
933
4303e980 934static int link_drop_requests(Link *link) {
56001f02 935 Request *req;
4303e980 936 int ret = 0;
56001f02
YW
937
938 assert(link);
939 assert(link->manager);
940
4303e980
YW
941 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
942 if (req->link != link)
943 continue;
944
945 /* If the request is already called, but its reply is not received, then we need to
946 * drop the configuration (e.g. address) here. Note, if the configuration is known,
947 * it will be handled later by link_drop_foreign_addresses() or so. */
948 if (req->waiting_reply && link->state != LINK_STATE_LINGER)
949 switch (req->type) {
950 case REQUEST_TYPE_ADDRESS: {
951 Address *address = ASSERT_PTR(req->userdata);
952
953 if (address_get(link, address, NULL) < 0)
954 RET_GATHER(ret, address_remove(address, link));
955 break;
956 }
c902fa08
YW
957 case REQUEST_TYPE_NEIGHBOR: {
958 Neighbor *neighbor = ASSERT_PTR(req->userdata);
959
960 if (neighbor_get(link, neighbor, NULL) < 0)
961 RET_GATHER(ret, neighbor_remove(neighbor, link));
962 break;
963 }
4303e980
YW
964 default:
965 ;
966 }
967
968 request_detach(link->manager, req);
969 }
970
971 return ret;
56001f02
YW
972}
973
cc2d7efc 974static Link *link_drop(Link *link) {
63130eb3 975 if (!link)
cc2d7efc 976 return NULL;
0d4ad91d 977
63130eb3
YW
978 assert(link->manager);
979
0d4ad91d
AR
980 link_set_state(link, LINK_STATE_LINGER);
981
63130eb3
YW
982 /* Drop all references from other links and manager. Note that async netlink calls may have
983 * references to the link, and they will be dropped when we receive replies. */
984
4303e980 985 (void) link_drop_requests(link);
56001f02 986
bb193d2d
YW
987 link_free_bound_to_list(link);
988 link_free_bound_by_list(link);
0d4ad91d 989
b0ff6dea
YW
990 link_clear_sr_iov_ifindices(link);
991
81357285 992 link_drop_from_master(link);
5f707e12 993
4c78dc17
YW
994 if (link->state_file)
995 (void) unlink(link->state_file);
996
63130eb3
YW
997 link_clean(link);
998
0b54c870
YW
999 STRV_FOREACH(n, link->alternative_names)
1000 hashmap_remove(link->manager->links_by_name, *n);
0b54c870
YW
1001 hashmap_remove(link->manager->links_by_name, link->ifname);
1002
fe321d45 1003 /* bonding master and its slaves have the same hardware address. */
e74f1d07 1004 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
fe321d45 1005
63130eb3 1006 /* The following must be called at last. */
6eab614d 1007 assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
cc2d7efc 1008 return link_unref(link);
0d4ad91d
AR
1009}
1010
5e5b137a 1011static int link_drop_foreign_config(Link *link) {
1339b950 1012 int r;
0b81225e
YW
1013
1014 assert(link);
1015 assert(link->manager);
5e5b137a 1016
5e3bb5da
YW
1017 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
1018 * to remove loopback address or addresses used for root NFS. */
1019
1020 if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
1021 return 0;
1022 if (FLAGS_SET(link->flags, IFF_LOOPBACK))
1023 return 0;
1024 if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
1025 return 0;
1026
e36d601c 1027 r = link_drop_foreign_routes(link);
5e5b137a 1028
1339b950
ZJS
1029 RET_GATHER(r, link_drop_foreign_nexthops(link));
1030 RET_GATHER(r, link_drop_foreign_addresses(link));
1031 RET_GATHER(r, link_drop_foreign_neighbors(link));
1032 RET_GATHER(r, manager_drop_foreign_routing_policy_rules(link->manager));
0b81225e
YW
1033
1034 return r;
5e5b137a
TG
1035}
1036
a0e99a37 1037static int link_drop_managed_config(Link *link) {
1339b950 1038 int r;
0b81225e
YW
1039
1040 assert(link);
1041 assert(link->manager);
3104883d 1042
a0e99a37 1043 r = link_drop_managed_routes(link);
3104883d 1044
1339b950
ZJS
1045 RET_GATHER(r, link_drop_managed_nexthops(link));
1046 RET_GATHER(r, link_drop_managed_addresses(link));
1047 RET_GATHER(r, link_drop_managed_neighbors(link));
1048 RET_GATHER(r, link_drop_managed_routing_policy_rules(link));
3104883d 1049
0b81225e 1050 return r;
3104883d
SS
1051}
1052
b4564f4e
YW
1053static void link_foreignize_config(Link *link) {
1054 assert(link);
1055 assert(link->manager);
1056
1057 link_foreignize_routes(link);
1058 link_foreignize_nexthops(link);
1059 link_foreignize_addresses(link);
1060 link_foreignize_neighbors(link);
1061 link_foreignize_routing_policy_rules(link);
1062}
1063
c3b94251 1064static int link_configure(Link *link) {
4ecdcb07
YW
1065 int r;
1066
ef1ba606 1067 assert(link);
b22d8a00 1068 assert(link->network);
bd08ce56 1069 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 1070
1187fc33
YW
1071 link_set_state(link, LINK_STATE_CONFIGURING);
1072
3cf58ef3
YW
1073 r = link_new_bound_to_list(link);
1074 if (r < 0)
1075 return r;
1076
1dec9d81 1077 r = link_request_traffic_control(link);
4ecdcb07
YW
1078 if (r < 0)
1079 return r;
1080
941f8e13
YW
1081 r = link_configure_mtu(link);
1082 if (r < 0)
1083 return r;
1084
7558f9e7
YW
1085 if (link->iftype == ARPHRD_CAN) {
1086 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1087 r = link_request_to_set_can(link);
1088 if (r < 0)
1089 return r;
1090
1091 return link_request_to_activate(link);
1092 }
1093
cb8453cc 1094 r = link_request_sr_iov_vfs(link);
518cd6b5
SS
1095 if (r < 0)
1096 return r;
1097
5e0534f1 1098 r = link_set_sysctl(link);
b69c3180
SS
1099 if (r < 0)
1100 return r;
8749cbcd 1101
d05c332c 1102 r = link_request_to_set_mac(link, /* allow_retry = */ true);
e16e4b3b
DS
1103 if (r < 0)
1104 return r;
1105
72e65e6f
YW
1106 r = link_request_to_set_ipoib(link);
1107 if (r < 0)
1108 return r;
1109
1187fc33 1110 r = link_request_to_set_flags(link);
e16e4b3b
DS
1111 if (r < 0)
1112 return r;
1113
1187fc33 1114 r = link_request_to_set_group(link);
99d2baa2
SS
1115 if (r < 0)
1116 return r;
1117
1187fc33
YW
1118 r = link_request_to_set_addrgen_mode(link);
1119 if (r < 0)
1120 return r;
1121
1122 r = link_request_to_set_master(link);
1123 if (r < 0)
1124 return r;
1125
1126 r = link_request_stacked_netdevs(link);
1127 if (r < 0)
1128 return r;
1129
1130 r = link_request_to_set_bond(link);
1131 if (r < 0)
1132 return r;
1133
1134 r = link_request_to_set_bridge(link);
1135 if (r < 0)
1136 return r;
1137
1138 r = link_request_to_set_bridge_vlan(link);
1139 if (r < 0)
1140 return r;
1141
1142 r = link_request_to_activate(link);
89fe6535
SS
1143 if (r < 0)
1144 return r;
1145
2ffd6d73
YW
1146 r = ipv4ll_configure(link);
1147 if (r < 0)
1148 return r;
64b21ece 1149
ccffa166 1150 r = link_request_dhcp4_client(link);
2ffd6d73
YW
1151 if (r < 0)
1152 return r;
eb34d4af 1153
ccffa166 1154 r = link_request_dhcp6_client(link);
2ffd6d73
YW
1155 if (r < 0)
1156 return r;
f5a8c43f 1157
ba4c7184 1158 r = link_request_ndisc(link);
2ffd6d73
YW
1159 if (r < 0)
1160 return r;
4138fb2c 1161
1d28a3cf
YW
1162 r = link_request_dhcp_server(link);
1163 if (r < 0)
1164 return r;
1165
a254fab2 1166 r = link_request_radv(link);
086b8853
YW
1167 if (r < 0)
1168 return r;
7465dd22 1169
2ffd6d73
YW
1170 r = link_lldp_rx_configure(link);
1171 if (r < 0)
1172 return r;
ce43e484 1173
c01b9b87
YW
1174 r = link_lldp_tx_configure(link);
1175 if (r < 0)
1176 return r;
1177
5e3bb5da
YW
1178 r = link_drop_foreign_config(link);
1179 if (r < 0)
1180 return r;
4c649652 1181
1187fc33
YW
1182 r = link_request_static_configs(link);
1183 if (r < 0)
1184 return r;
1185
1186 if (!link_has_carrier(link))
1187 return 0;
1188
1189 return link_acquire_dynamic_conf(link);
505f8da7
TG
1190}
1191
170e88c8
YW
1192static int link_get_network(Link *link, Network **ret) {
1193 Network *network;
1a3caa49 1194 int r;
170e88c8
YW
1195
1196 assert(link);
1197 assert(link->manager);
1198 assert(ret);
1199
1200 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1201 bool warn = false;
1202
1a3caa49 1203 r = net_match_config(
170e88c8 1204 &network->match,
8de56fb3 1205 link->dev,
52135071
YW
1206 &link->hw_addr,
1207 &link->permanent_hw_addr,
170e88c8
YW
1208 link->driver,
1209 link->iftype,
65022cd7 1210 link->kind,
170e88c8
YW
1211 link->ifname,
1212 link->alternative_names,
1213 link->wlan_iftype,
1214 link->ssid,
1a3caa49
YW
1215 &link->bssid);
1216 if (r < 0)
1217 return r;
1218 if (r == 0)
170e88c8
YW
1219 continue;
1220
8de56fb3 1221 if (network->match.ifname && link->dev) {
170e88c8
YW
1222 uint8_t name_assign_type = NET_NAME_UNKNOWN;
1223 const char *attr;
1224
8de56fb3 1225 if (sd_device_get_sysattr_value(link->dev, "name_assign_type", &attr) >= 0)
170e88c8
YW
1226 (void) safe_atou8(attr, &name_assign_type);
1227
1228 warn = name_assign_type == NET_NAME_ENUM;
1229 }
1230
1231 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1232 "found matching network '%s'%s.",
1233 network->filename,
1234 warn ? ", based on potentially unpredictable interface name" : "");
1235
1236 if (network->unmanaged)
1237 return -ENOENT;
1238
1239 *ret = network;
1240 return 0;
1241 }
1242
1243 return -ENOENT;
1244}
1245
8a4ad01a 1246int link_reconfigure_impl(Link *link, bool force) {
cb71e9c6 1247 Network *network = NULL;
9aa2585e 1248 NetDev *netdev = NULL;
ad932b15
YW
1249 int r;
1250
5a1860f7 1251 assert(link);
572b21d9 1252
4faca0a3 1253 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
fb961119
YW
1254 return 0;
1255
9aa2585e
YW
1256 r = netdev_get(link->manager, link->ifname, &netdev);
1257 if (r < 0 && r != -ENOENT)
1258 return r;
1259
170e88c8 1260 r = link_get_network(link, &network);
cb71e9c6 1261 if (r < 0 && r != -ENOENT)
ad932b15
YW
1262 return r;
1263
fb961119
YW
1264 if (link->state != LINK_STATE_UNMANAGED && !network)
1265 /* If link is in initialized state, then link->network is also NULL. */
1266 force = true;
1267
99b8517c 1268 if (link->network == network && !force)
ad932b15
YW
1269 return 0;
1270
fb961119 1271 if (network) {
b993e7e7
YW
1272 _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1273
fb961119 1274 if (link->state == LINK_STATE_INITIALIZED)
b993e7e7
YW
1275 log_link_info(link, "Configuring with %s%s%s%s.",
1276 network->filename,
1277 isempty(joined) ? "" : " (dropins: ",
1278 joined,
1279 isempty(joined) ? "" : ")");
fb961119 1280 else
b993e7e7
YW
1281 log_link_info(link, "Reconfiguring with %s%s%s%s.",
1282 network->filename,
1283 isempty(joined) ? "" : " (dropins: ",
1284 joined,
1285 isempty(joined) ? "" : ")");
fb961119
YW
1286 } else
1287 log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1288 "Unmanaging interface.");
ad932b15
YW
1289
1290 /* Dropping old .network file */
2a99eed0 1291 r = link_stop_engines(link, false);
572b21d9 1292 if (r < 0)
ad932b15 1293 return r;
ad932b15 1294
4303e980
YW
1295 r = link_drop_requests(link);
1296 if (r < 0)
1297 return r;
19d9a5ad 1298
81db4f3d 1299 if (network && !force && network->keep_configuration != KEEP_CONFIGURATION_YES)
b4564f4e
YW
1300 /* When a new/updated .network file is assigned, first make all configs (addresses,
1301 * routes, and so on) foreign, and then drop unnecessary configs later by
81db4f3d
YW
1302 * link_drop_foreign_config() in link_configure().
1303 * Note, when KeepConfiguration=yes, link_drop_foreign_config() does nothing. Hence,
1304 * here we need to drop the configs such as addresses, routes, and so on configured by
1305 * the previously assigned .network file. */
b4564f4e
YW
1306 link_foreignize_config(link);
1307 else {
a0e99a37
YW
1308 /* Remove all managed configs. Note, foreign configs are removed in later by
1309 * link_configure() -> link_drop_foreign_config() if the link is managed by us. */
1310 r = link_drop_managed_config(link);
b4564f4e
YW
1311 if (r < 0)
1312 return r;
1313 }
ad932b15 1314
3cf58ef3
YW
1315 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1316 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1317 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1318 * map here, as it depends on .network files assigned to other links. */
1319 link_free_bound_to_list(link);
1320
ad932b15
YW
1321 link_free_engines(link);
1322 link->network = network_unref(link->network);
1323
9aa2585e
YW
1324 netdev_unref(link->netdev);
1325 link->netdev = netdev_ref(netdev);
1326
cb71e9c6
YW
1327 if (!network) {
1328 link_set_state(link, LINK_STATE_UNMANAGED);
1329 return 0;
1330 }
1331
ad932b15 1332 /* Then, apply new .network file */
9092113d 1333 link->network = network_ref(network);
b156a95d 1334 link_update_operstate(link, true);
9092113d 1335 link_dirty(link);
ad932b15 1336
ad932b15 1337 link_set_state(link, LINK_STATE_INITIALIZED);
61135582 1338 link->activated = false;
ad932b15 1339
ad932b15
YW
1340 r = link_configure(link);
1341 if (r < 0)
1342 return r;
1343
0e397560 1344 return 1;
ad932b15
YW
1345}
1346
96f5f9ef 1347static int link_reconfigure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, bool force) {
572b21d9
YW
1348 int r;
1349
d09a179e
YW
1350 assert(link);
1351
5a1860f7
YW
1352 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1353 if (r <= 0)
1354 return r;
1355
7f80fa12 1356 r = link_reconfigure_impl(link, force);
d09a179e 1357 if (r < 0) {
572b21d9 1358 link_enter_failed(link);
d09a179e
YW
1359 return 0;
1360 }
572b21d9 1361
d09a179e 1362 return r;
572b21d9
YW
1363}
1364
5a1860f7 1365static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1366 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ false);
5a1860f7 1367}
572b21d9 1368
5a1860f7 1369static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
96f5f9ef 1370 return link_reconfigure_handler_internal(rtnl, m, link, /* force = */ true);
572b21d9
YW
1371}
1372
a39a9ac8 1373int link_reconfigure(Link *link, bool force) {
d09a179e
YW
1374 int r;
1375
1376 assert(link);
d09a179e 1377
8bceafa7 1378 /* When link in pending or initialized state, then link_configure() will be called. To prevent
f8dd4077
ZJS
1379 * the function from being called multiple times simultaneously, refuse to reconfigure the
1380 * interface in these cases. */
8bceafa7 1381 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
f8dd4077 1382 return 0; /* 0 means no-op. */
2c0d7ed3 1383
a39a9ac8 1384 r = link_call_getlink(link, force ? link_force_reconfigure_handler : link_reconfigure_handler);
572b21d9
YW
1385 if (r < 0)
1386 return r;
1387
8bceafa7 1388 return 1; /* 1 means the interface will be reconfigured. */
572b21d9
YW
1389}
1390
e6bf7774 1391static int link_initialized_and_synced(Link *link) {
505f8da7
TG
1392 int r;
1393
1394 assert(link);
505f8da7
TG
1395 assert(link->manager);
1396
faa2e64f
YW
1397 if (link->manager->test_mode) {
1398 log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
1399 link_set_state(link, LINK_STATE_UNMANAGED);
1400 return 0;
1401 }
1402
40695457
YW
1403 if (link->state == LINK_STATE_PENDING) {
1404 log_link_debug(link, "Link state is up-to-date");
1405 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 1406
40695457
YW
1407 r = link_new_bound_by_list(link);
1408 if (r < 0)
1409 return r;
0d4ad91d 1410
40695457
YW
1411 r = link_handle_bound_by_list(link);
1412 if (r < 0)
1413 return r;
1414 }
0d4ad91d 1415
fb961119 1416 return link_reconfigure_impl(link, /* force = */ false);
505f8da7
TG
1417}
1418
302a796f 1419static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4ff296b0
YW
1420 int r;
1421
5a1860f7
YW
1422 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1423 if (r <= 0)
0b54c870 1424 return r;
572b21d9 1425
4ff296b0
YW
1426 r = link_initialized_and_synced(link);
1427 if (r < 0)
1428 link_enter_failed(link);
5a1860f7
YW
1429
1430 return 0;
e6bf7774
YW
1431}
1432
3be9d62a 1433static int link_initialized(Link *link, sd_device *device) {
b0ff6dea
YW
1434 int r;
1435
4f561e8e 1436 assert(link);
4f561e8e
TG
1437 assert(device);
1438
160203e9
YW
1439 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1440 * or sysattrs) may be outdated. */
8de56fb3 1441 device_unref_and_replace(link->dev, device);
4f561e8e 1442
3b1dbdf0
YW
1443 if (link->dhcp_client) {
1444 r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
1445 if (r < 0)
1446 log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
1447 }
1448
1449 if (link->dhcp6_client) {
1450 r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
1451 if (r < 0)
1452 log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
1453 }
1454
b0ff6dea
YW
1455 r = link_set_sr_iov_ifindices(link);
1456 if (r < 0)
1457 log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
1458
40695457
YW
1459 if (link->state != LINK_STATE_PENDING)
1460 return link_reconfigure(link, /* force = */ false);
679b3605 1461
79008bdd 1462 log_link_debug(link, "udev initialized link");
4f561e8e 1463
3c9b8860
TG
1464 /* udev has initialized the link, but we don't know if we have yet
1465 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1466 * when it returns we know that the pending NEWLINKs have already been
1467 * processed and that we are up-to-date */
4f561e8e 1468
5a1860f7 1469 return link_call_getlink(link, link_initialized_handler);
cc2d7efc
YW
1470}
1471
0d411b7f 1472static int link_check_initialized(Link *link) {
51517f9e 1473 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
5a937ea2 1474 int r;
505f8da7 1475
0d411b7f 1476 assert(link);
505f8da7 1477
82f52245 1478 if (!udev_available())
0d411b7f 1479 return link_initialized_and_synced(link);
852a3916
YW
1480
1481 /* udev should be around */
0ac655a6 1482 r = sd_device_new_from_ifindex(&device, link->ifindex);
852a3916
YW
1483 if (r < 0) {
1484 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1485 return 0;
1486 }
1487
1488 r = sd_device_get_is_initialized(device);
0d411b7f
YW
1489 if (r < 0)
1490 return log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
852a3916
YW
1491 if (r == 0) {
1492 /* not yet ready */
1493 log_link_debug(link, "link pending udev initialization...");
1494 return 0;
1495 }
1496
1497 r = device_is_renaming(device);
0d411b7f
YW
1498 if (r < 0)
1499 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
852a3916
YW
1500 if (r > 0) {
1501 log_link_debug(link, "Interface is being renamed, pending initialization.");
1502 return 0;
4f561e8e 1503 }
505f8da7 1504
0d411b7f 1505 return link_initialized(link, device);
a748b692
TG
1506}
1507
1fd3ac1e 1508int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
3be9d62a 1509 int r, ifindex;
ba87a61d 1510 const char *s;
1fd3ac1e 1511 Link *link;
3be9d62a
YW
1512
1513 assert(m);
1514 assert(device);
1515
3be9d62a 1516 r = sd_device_get_ifindex(device, &ifindex);
1fd3ac1e
YW
1517 if (r < 0)
1518 return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
3be9d62a 1519
75255a8d
YW
1520 r = link_get_by_index(m, ifindex, &link);
1521 if (r < 0) {
1522 /* This error is not critical, as the corresponding rtnl message may be received later. */
1523 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
1524 return 0;
1525 }
1526
1527 /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1528 * object here. It will be removed only when rtnetlink says so. */
1529 if (action == SD_DEVICE_REMOVE) {
1530 link->dev = sd_device_unref(link->dev);
1531 return 0;
1532 }
1533
3be9d62a 1534 r = device_is_renaming(device);
1fd3ac1e
YW
1535 if (r < 0)
1536 return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
3be9d62a 1537 if (r > 0) {
1fd3ac1e 1538 log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
71cdfe16
YW
1539 /* TODO:
1540 * What happens when a device is initialized, then soon renamed after that? When we detect
1541 * such, maybe we should cancel or postpone all queued requests for the interface. */
3be9d62a
YW
1542 return 0;
1543 }
1544
ba87a61d
YW
1545 r = sd_device_get_property_value(device, "ID_NET_MANAGED_BY", &s);
1546 if (r < 0 && r != -ENOENT)
1547 log_device_debug_errno(device, r, "Failed to get ID_NET_MANAGED_BY udev property, ignoring: %m");
1548 if (r >= 0 && !streq(s, "io.systemd.Network")) {
1549 log_device_debug(device, "Interface is requested to be managed by '%s', not managing the interface.", s);
1550 link_set_state(link, LINK_STATE_UNMANAGED);
1551 return 0;
1552 }
1553
4b9a8c2b
YW
1554 r = link_initialized(link, device);
1555 if (r < 0)
1556 link_enter_failed(link);
3be9d62a
YW
1557
1558 return 0;
1559}
1560
9c0a72f9 1561static int link_carrier_gained(Link *link) {
6706ce2f 1562 bool force_reconfigure;
9c0a72f9
TG
1563 int r;
1564
1565 assert(link);
1566
6706ce2f
YW
1567 r = event_source_disable(link->carrier_lost_timer);
1568 if (r < 0)
1569 log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
1570
d9910948
YW
1571 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1572 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1573 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1574 * restarted, and the correct network information will be gained.
1575 *
1576 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1577 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1578 * already assigned to the interface (in that case, the .network file does not have the SSID=
1579 * setting in the [Match] section), and the interface is already being configured. Of course,
1580 * there may exist another .network file with higher priority and a matching SSID= setting. But
1581 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1582 *
6706ce2f 1583 * For non-wireless interfaces, we have no way to detect the connected network change. So,
d9910948
YW
1584 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1585 force_reconfigure = link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid);
6706ce2f
YW
1586 link->previous_ssid = mfree(link->previous_ssid);
1587
986b2514
1588 /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1589 * profile wants to match on it with SSID= in its [Match] section.
1590 */
1591 if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1592 r = link_get_wlan_interface(link);
1593 if (r < 0)
1594 return r;
1595 }
1596
081466bd
YW
1597 /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1598 * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1599 * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1600 r = link_reconfigure_impl(link, force_reconfigure);
1601 if (r != 0)
1602 return r;
b7ac1284 1603
aa9117c0
YW
1604 r = link_handle_bound_by_list(link);
1605 if (r < 0)
1606 return r;
1607
600b7898
YW
1608 if (link->iftype == ARPHRD_CAN)
1609 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0 1610 return 0;
600b7898 1611
b9ea3d2e 1612 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
8566df79 1613 r = link_acquire_dynamic_conf(link);
f0269653 1614 if (r < 0)
9c0a72f9 1615 return r;
6fc25497 1616
97108953 1617 r = link_request_static_configs(link);
6fc25497
SS
1618 if (r < 0)
1619 return r;
9c0a72f9
TG
1620 }
1621
aa9117c0 1622 return 0;
9c0a72f9
TG
1623}
1624
6706ce2f
YW
1625static int link_carrier_lost_impl(Link *link) {
1626 int r, ret = 0;
1627
1628 assert(link);
1629
1630 link->previous_ssid = mfree(link->previous_ssid);
1631
1632 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1633 return 0;
1634
1635 if (!link->network)
1636 return 0;
1637
1638 r = link_stop_engines(link, false);
1639 if (r < 0)
1640 ret = r;
1641
a0e99a37 1642 r = link_drop_managed_config(link);
6706ce2f
YW
1643 if (r < 0 && ret >= 0)
1644 ret = r;
1645
1646 return ret;
1647}
1648
1649static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
99534007 1650 Link *link = ASSERT_PTR(userdata);
6706ce2f
YW
1651 int r;
1652
6706ce2f
YW
1653 r = link_carrier_lost_impl(link);
1654 if (r < 0) {
1655 log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
1656 link_enter_failed(link);
1657 }
1658
1659 return 0;
1660}
1661
9c0a72f9 1662static int link_carrier_lost(Link *link) {
b7326069
YW
1663 uint16_t dhcp_mtu;
1664 usec_t usec;
9c0a72f9
TG
1665 int r;
1666
1667 assert(link);
1668
aa9117c0
YW
1669 r = link_handle_bound_by_list(link);
1670 if (r < 0)
1671 return r;
93b4dab5 1672
600b7898
YW
1673 if (link->iftype == ARPHRD_CAN)
1674 /* let's shortcut things for CAN which doesn't need most of what's done below. */
aa9117c0
YW
1675 return 0;
1676
f1c22cf4
YW
1677 if (!link->network)
1678 return 0;
1679
b7326069
YW
1680 if (link->network->ignore_carrier_loss_set)
1681 /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1682 usec = link->network->ignore_carrier_loss_usec;
1683
1684 else if (link->network->bond && link->wlan_iftype > 0)
1685 /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1686 * carrier to be lost. See #19832. */
1687 usec = 3 * USEC_PER_SEC;
1688
1689 else if (link->network->dhcp_use_mtu &&
1690 link->dhcp_lease &&
1691 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
1692 dhcp_mtu != link->original_mtu)
1693 /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1694 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1695 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1696 usec = 5 * USEC_PER_SEC;
1697
1698 else
27438545 1699 /* Otherwise, use the implied default value. */
b7326069
YW
1700 usec = link->network->ignore_carrier_loss_usec;
1701
1702 if (usec == USEC_INFINITY)
aa9117c0 1703 return 0;
600b7898 1704
b7326069 1705 if (usec == 0)
6706ce2f
YW
1706 return link_carrier_lost_impl(link);
1707
1708 return event_reset_time_relative(link->manager->event,
1709 &link->carrier_lost_timer,
ba4e0427 1710 CLOCK_BOOTTIME,
b7326069 1711 usec,
6706ce2f
YW
1712 0,
1713 link_carrier_lost_handler,
1714 link,
1715 0,
1716 "link-carrier-loss",
1717 true);
0c9ee5d5
YW
1718}
1719
0c9ee5d5
YW
1720static int link_admin_state_up(Link *link) {
1721 int r;
1722
1723 assert(link);
1724
1725 /* This is called every time an interface admin state changes to up;
1726 * specifically, when IFF_UP flag changes from unset to set. */
1727
1728 if (!link->network)
1729 return 0;
1730
899b0e5e 1731 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
1a054a20 1732 log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
712fd5d2 1733 return link_request_to_bring_up_or_down(link, /* up = */ false);
0c9ee5d5
YW
1734 }
1735
1736 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1737 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1738 r = link_set_ipv6_mtu(link);
1739 if (r < 0)
1740 log_link_warning_errno(link, r, "Cannot set IPv6 MTU, ignoring: %m");
1741
1742 return 0;
1743}
1744
1745static int link_admin_state_down(Link *link) {
1746 assert(link);
1747
1748 if (!link->network)
1749 return 0;
1750
899b0e5e 1751 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
1a054a20 1752 log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
712fd5d2 1753 return link_request_to_bring_up_or_down(link, /* up = */ true);
0c9ee5d5
YW
1754 }
1755
1756 return 0;
1757}
1758
0c9ee5d5
YW
1759static bool link_is_enslaved(Link *link) {
1760 if (link->flags & IFF_SLAVE)
0c9ee5d5
YW
1761 return true;
1762
1f024462 1763 if (link->master_ifindex > 0)
0c9ee5d5
YW
1764 return true;
1765
0c9ee5d5
YW
1766 return false;
1767}
1768
0c9ee5d5
YW
1769void link_update_operstate(Link *link, bool also_update_master) {
1770 LinkOperationalState operstate;
1771 LinkCarrierState carrier_state;
1772 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
1773 LinkOnlineState online_state;
1774 _cleanup_strv_free_ char **p = NULL;
0c9ee5d5 1775 bool changed = false;
0c9ee5d5
YW
1776
1777 assert(link);
1778
1779 if (link->kernel_operstate == IF_OPER_DORMANT)
1780 carrier_state = LINK_CARRIER_STATE_DORMANT;
1781 else if (link_has_carrier(link)) {
1782 if (link_is_enslaved(link))
1783 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
1784 else
1785 carrier_state = LINK_CARRIER_STATE_CARRIER;
1786 } else if (link->flags & IFF_UP)
1787 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
1788 else
1789 carrier_state = LINK_CARRIER_STATE_OFF;
1790
1791 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
1792 Link *slave;
1793
1794 SET_FOREACH(slave, link->slaves) {
1795 link_update_operstate(slave, false);
1796
1797 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
1798 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
1799 }
1800 }
1801
60415c13 1802 link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
0c9ee5d5
YW
1803
1804 /* Mapping of address and carrier state vs operational state
1805 * carrier state
1806 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1807 * ------------------------------------------------------------------------------
1808 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
cf4dbd84
HH
1809 * address_state degraded | off | no-carrier | dormant | degraded | degraded | enslaved
1810 * routable | off | no-carrier | dormant | routable | routable | routable
0c9ee5d5
YW
1811 */
1812
cf4dbd84
HH
1813 if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
1814 operstate = LINK_OPERSTATE_ROUTABLE;
1815 else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
1816 operstate = LINK_OPERSTATE_DEGRADED;
1817 else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
0c9ee5d5
YW
1818 operstate = (LinkOperationalState) carrier_state;
1819 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
1820 operstate = LINK_OPERSTATE_ROUTABLE;
1821 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
1822 operstate = LINK_OPERSTATE_DEGRADED;
1823 else
1824 operstate = LINK_OPERSTATE_ENSLAVED;
1825
1826 /* Only determine online state for managed links with RequiredForOnline=yes */
1827 if (!link->network || !link->network->required_for_online)
1828 online_state = _LINK_ONLINE_STATE_INVALID;
1829 else if (operstate < link->network->required_operstate_for_online.min ||
1830 operstate > link->network->required_operstate_for_online.max)
1831 online_state = LINK_ONLINE_STATE_OFFLINE;
1832 else {
1833 AddressFamily required_family = link->network->required_family_for_online;
1834 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
1835 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
1836
1837 /* The operational state is within the range required for online.
1838 * If a particular address family is also required, we might revert
1839 * to offline in the blocks below. */
1840 online_state = LINK_ONLINE_STATE_ONLINE;
1841
1842 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_DEGRADED) {
1843 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
1844 online_state = LINK_ONLINE_STATE_OFFLINE;
1845 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
1846 online_state = LINK_ONLINE_STATE_OFFLINE;
1847 }
1848
1849 if (link->network->required_operstate_for_online.min >= LINK_OPERSTATE_ROUTABLE) {
1850 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1851 online_state = LINK_ONLINE_STATE_OFFLINE;
1852 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
1853 online_state = LINK_ONLINE_STATE_OFFLINE;
1854 }
1855 }
1856
1857 if (link->carrier_state != carrier_state) {
1858 link->carrier_state = carrier_state;
1859 changed = true;
1860 if (strv_extend(&p, "CarrierState") < 0)
1861 log_oom();
1862 }
1863
1864 if (link->address_state != address_state) {
1865 link->address_state = address_state;
1866 changed = true;
1867 if (strv_extend(&p, "AddressState") < 0)
1868 log_oom();
1869 }
0d4ad91d 1870
0c9ee5d5
YW
1871 if (link->ipv4_address_state != ipv4_address_state) {
1872 link->ipv4_address_state = ipv4_address_state;
1873 changed = true;
1874 if (strv_extend(&p, "IPv4AddressState") < 0)
1875 log_oom();
1876 }
9c0a72f9 1877
0c9ee5d5
YW
1878 if (link->ipv6_address_state != ipv6_address_state) {
1879 link->ipv6_address_state = ipv6_address_state;
1880 changed = true;
1881 if (strv_extend(&p, "IPv6AddressState") < 0)
1882 log_oom();
1883 }
9c0a72f9 1884
0c9ee5d5
YW
1885 if (link->operstate != operstate) {
1886 link->operstate = operstate;
1887 changed = true;
1888 if (strv_extend(&p, "OperationalState") < 0)
1889 log_oom();
1890 }
9c0a72f9 1891
0c9ee5d5
YW
1892 if (link->online_state != online_state) {
1893 link->online_state = online_state;
1894 changed = true;
1895 if (strv_extend(&p, "OnlineState") < 0)
1896 log_oom();
1897 }
9c0a72f9 1898
0c9ee5d5
YW
1899 if (p)
1900 link_send_changed_strv(link, p);
1901 if (changed)
1902 link_dirty(link);
9c0a72f9 1903
0c9ee5d5
YW
1904 if (also_update_master) {
1905 Link *master;
9c0a72f9 1906
0c9ee5d5
YW
1907 if (link_get_master(link, &master) >= 0)
1908 link_update_operstate(master, true);
1909 }
9c0a72f9
TG
1910}
1911
0c9ee5d5
YW
1912#define FLAG_STRING(string, flag, old, new) \
1913 (((old ^ new) & flag) \
1914 ? ((old & flag) ? (" -" string) : (" +" string)) \
1915 : "")
1916
0d411b7f 1917static int link_update_flags(Link *link, sd_netlink_message *message) {
96f5f9ef 1918 bool link_was_admin_up, had_carrier;
0c9ee5d5
YW
1919 uint8_t operstate;
1920 unsigned flags;
d236718c
DS
1921 int r;
1922
61135582 1923 assert(link);
0d411b7f 1924 assert(message);
61135582 1925
0d411b7f 1926 r = sd_rtnl_message_link_get_flags(message, &flags);
0c9ee5d5 1927 if (r < 0)
0d411b7f 1928 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
3be9d62a 1929
0d411b7f
YW
1930 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
1931 if (r == -ENODATA)
1932 /* If we got a message without operstate, assume the state was unchanged. */
0c9ee5d5 1933 operstate = link->kernel_operstate;
0d411b7f
YW
1934 else if (r < 0)
1935 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
0c9ee5d5 1936
0d411b7f 1937 if (link->flags == flags && link->kernel_operstate == operstate)
61135582
DS
1938 return 0;
1939
0c9ee5d5
YW
1940 if (link->flags != flags) {
1941 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
61135582 1942
0c9ee5d5
YW
1943 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",
1944 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1945 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1946 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1947 FLAG_STRING("UP", IFF_UP, link->flags, flags),
1948 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1949 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1950 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1951 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1952 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1953 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1954 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1955 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1956 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1957 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1958 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1959 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
1960 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
1961 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
1962 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
d236718c 1963
0c9ee5d5
YW
1964 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
1965 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
1966 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
1967 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
1968 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
1969 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
1970 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
1971 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
d236718c 1972
0c9ee5d5
YW
1973 if (unknown_flags_added)
1974 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
61135582 1975
0c9ee5d5
YW
1976 if (unknown_flags_removed)
1977 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
1978 }
61135582 1979
0d411b7f
YW
1980 link_was_admin_up = link->flags & IFF_UP;
1981 had_carrier = link_has_carrier(link);
1982
0c9ee5d5
YW
1983 link->flags = flags;
1984 link->kernel_operstate = operstate;
36161cba 1985
0c9ee5d5 1986 link_update_operstate(link, true);
61135582 1987
0d411b7f
YW
1988 if (!link_was_admin_up && (link->flags & IFF_UP)) {
1989 log_link_info(link, "Link UP");
22936833 1990
0d411b7f
YW
1991 r = link_admin_state_up(link);
1992 if (r < 0)
1993 return r;
1994 } else if (link_was_admin_up && !(link->flags & IFF_UP)) {
1995 log_link_info(link, "Link DOWN");
0d4ad91d 1996
0d411b7f 1997 r = link_admin_state_down(link);
0d4ad91d
AR
1998 if (r < 0)
1999 return r;
7619683b
TG
2000 }
2001
0d411b7f
YW
2002 if (!had_carrier && link_has_carrier(link)) {
2003 log_link_info(link, "Gained carrier");
0d4ad91d 2004
0d411b7f 2005 r = link_carrier_gained(link);
30de2b89
YW
2006 if (r < 0)
2007 return r;
0d411b7f
YW
2008 } else if (had_carrier && !link_has_carrier(link)) {
2009 log_link_info(link, "Lost carrier");
2010
2011 r = link_carrier_lost(link);
0b54c870
YW
2012 if (r < 0)
2013 return r;
b8941f74
TG
2014 }
2015
0d411b7f
YW
2016 return 0;
2017}
afe7fd56 2018
0d411b7f
YW
2019static int link_update_master(Link *link, sd_netlink_message *message) {
2020 int master_ifindex, r;
7465dd22 2021
0d411b7f
YW
2022 assert(link);
2023 assert(message);
69629de9 2024
0d411b7f
YW
2025 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
2026 if (r == -ENODATA)
2027 return 0;
2028 if (r < 0)
2029 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
807667f7 2030
571bf1aa
YW
2031 if (master_ifindex == link->ifindex)
2032 master_ifindex = 0;
2033
0d411b7f
YW
2034 if (master_ifindex == link->master_ifindex)
2035 return 0;
807667f7 2036
0d411b7f 2037 if (link->master_ifindex == 0)
8458ca26 2038 log_link_debug(link, "Attached to master interface: %i", master_ifindex);
0d411b7f 2039 else if (master_ifindex == 0)
8458ca26 2040 log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
0d411b7f 2041 else
e2341b6b
DT
2042 log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
2043 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), master_ifindex);
807667f7 2044
0d411b7f 2045 link_drop_from_master(link);
807667f7 2046
0d411b7f 2047 link->master_ifindex = master_ifindex;
413708d1 2048
0d411b7f
YW
2049 r = link_append_to_master(link);
2050 if (r < 0)
2051 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
7465dd22 2052
0d411b7f
YW
2053 return 0;
2054}
807667f7 2055
e1658632
YW
2056static int link_update_driver(Link *link, sd_netlink_message *message) {
2057 int r;
2058
2059 assert(link);
2060 assert(link->manager);
2061 assert(message);
2062
2063 /* Driver is already read. Assuming the driver is never changed. */
6625c39e 2064 if (link->ethtool_driver_read)
e1658632
YW
2065 return 0;
2066
2067 /* When udevd is running, read the driver after the interface is initialized by udevd.
2068 * Otherwise, ethtool may not work correctly. See issue #22538.
2069 * When udevd is not running, read the value when the interface is detected. */
6625c39e 2070 if (udev_available() && !link->dev)
e1658632
YW
2071 return 0;
2072
6625c39e
YW
2073 link->ethtool_driver_read = true;
2074
e1658632
YW
2075 r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2076 if (r < 0) {
2077 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2078 return 0;
2079 }
2080
2081 log_link_debug(link, "Found driver: %s", strna(link->driver));
2082
2083 if (streq_ptr(link->driver, "dsa")) {
2084 uint32_t dsa_master_ifindex = 0;
2085
2086 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2087 if (r < 0 && r != -ENODATA)
2088 return log_link_debug_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface: %m");
2089
2090 if (dsa_master_ifindex > INT_MAX) {
2091 log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2092 dsa_master_ifindex);
2093 dsa_master_ifindex = 0;
2094 }
2095
2096 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2097 }
2098
40695457 2099 return 1; /* needs reconfigure */
e1658632
YW
2100}
2101
6625c39e 2102static int link_update_permanent_hardware_address_from_ethtool(Link *link, sd_netlink_message *message) {
e1658632
YW
2103 int r;
2104
2105 assert(link);
2106 assert(link->manager);
2107 assert(message);
2108
6625c39e 2109 if (link->ethtool_permanent_hw_addr_read)
e1658632
YW
2110 return 0;
2111
2112 /* When udevd is running, read the permanent hardware address after the interface is
2113 * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2114 * When udevd is not running, read the value when the interface is detected. */
6625c39e
YW
2115 if (udev_available() && !link->dev)
2116 return 0;
2117
2118 /* If the interface does not have a hardware address, then it will not have a permanent address either. */
2119 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, NULL);
2120 if (r == -ENODATA)
2121 return 0;
2122 if (r < 0)
2123 return log_link_debug_errno(link, r, "Failed to read IFLA_ADDRESS attribute: %m");
2124
2125 link->ethtool_permanent_hw_addr_read = true;
2126
2127 r = ethtool_get_permanent_hw_addr(&link->manager->ethtool_fd, link->ifname, &link->permanent_hw_addr);
2128 if (r < 0)
2129 log_link_debug_errno(link, r, "Permanent hardware address not found, continuing without: %m");
2130
2131 return 0;
2132}
2133
2134static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
2135 int r;
2136
2137 assert(link);
2138 assert(link->manager);
2139 assert(message);
2140
2141 if (link->permanent_hw_addr.length > 0)
e1658632
YW
2142 return 0;
2143
2144 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2145 if (r < 0) {
2146 if (r != -ENODATA)
2147 return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
2148
6625c39e
YW
2149 /* Fallback to ethtool for older kernels. */
2150 r = link_update_permanent_hardware_address_from_ethtool(link, message);
2151 if (r < 0)
2152 return r;
e1658632
YW
2153 }
2154
2155 if (link->permanent_hw_addr.length > 0)
2156 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2157
40695457 2158 return 1; /* needs reconfigure */
e1658632
YW
2159}
2160
0d411b7f 2161static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
e2bacccd 2162 struct hw_addr_data addr;
0d411b7f 2163 int r;
d93d655c 2164
0d411b7f
YW
2165 assert(link);
2166 assert(message);
dfc58b47 2167
0d411b7f
YW
2168 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2169 if (r < 0 && r != -ENODATA)
2170 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
4f882b2a 2171
e2bacccd 2172 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
0d411b7f
YW
2173 if (r == -ENODATA)
2174 return 0;
2175 if (r < 0)
fe321d45 2176 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
bb262ef0 2177
e2bacccd 2178 if (hw_addr_equal(&link->hw_addr, &addr))
0d411b7f 2179 return 0;
a61bb41c 2180
de0f1579 2181 if (link->hw_addr.length == 0)
e2bacccd 2182 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
fe321d45 2183 else {
e2341b6b
DT
2184 log_link_debug(link, "Hardware address is changed: %s %s %s",
2185 HW_ADDR_TO_STR(&link->hw_addr),
2186 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
2187 HW_ADDR_TO_STR(&addr));
0d411b7f 2188
e74f1d07 2189 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
fe321d45
YW
2190 }
2191
e2bacccd
YW
2192 link->hw_addr = addr;
2193
fe321d45
YW
2194 if (!hw_addr_is_null(&link->hw_addr)) {
2195 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2196 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2197 /* bonding master and its slaves have the same hardware address. */
2198 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2199 if (r < 0)
2200 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2201 }
0d411b7f 2202
0a14f83a 2203 r = ipv4acd_update_mac(link);
76a86ffd
YW
2204 if (r < 0)
2205 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2206
0d411b7f 2207 r = ipv4ll_update_mac(link);
a61bb41c 2208 if (r < 0)
0d411b7f 2209 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
a61bb41c 2210
0d411b7f
YW
2211 r = dhcp4_update_mac(link);
2212 if (r < 0)
2213 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
d236718c 2214
0d411b7f
YW
2215 r = dhcp6_update_mac(link);
2216 if (r < 0)
2217 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2218
2219 r = radv_update_mac(link);
2220 if (r < 0)
2221 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2222
3be64aa4 2223 if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
ca2b7cd8 2224 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
d236718c 2225 if (r < 0)
0d411b7f
YW
2226 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2227 }
d236718c 2228
35778343
YW
2229 if (link->lldp_rx) {
2230 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
61135582 2231 if (r < 0)
c01b9b87
YW
2232 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2233 }
2234
2235 if (link->lldp_tx) {
2236 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2237 if (r < 0)
2238 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
61135582
DS
2239 }
2240
40695457 2241 return 1; /* needs reconfigure */
0d411b7f 2242}
a61bb41c 2243
0d411b7f 2244static int link_update_mtu(Link *link, sd_netlink_message *message) {
717ba5fc 2245 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
0d411b7f 2246 int r;
a61bb41c 2247
0d411b7f
YW
2248 assert(link);
2249 assert(message);
2250
2251 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2252 if (r == -ENODATA)
2253 return 0;
2254 if (r < 0)
2255 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
b5d0fd1e
YW
2256 if (mtu == 0)
2257 return 0;
0d411b7f 2258
717ba5fc
YW
2259 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2260 if (r < 0 && r != -ENODATA)
2261 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2262
2263 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2264 if (r < 0 && r != -ENODATA)
2265 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2266
717ba5fc
YW
2267 if (max_mtu == 0)
2268 max_mtu = UINT32_MAX;
2269
2270 link->min_mtu = min_mtu;
2271 link->max_mtu = max_mtu;
2272
0d411b7f
YW
2273 if (link->original_mtu == 0) {
2274 link->original_mtu = mtu;
717ba5fc
YW
2275 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2276 link->original_mtu, link->min_mtu, link->max_mtu);
0d411b7f
YW
2277 }
2278
717ba5fc
YW
2279 if (link->mtu == mtu)
2280 return 0;
2281
b5d0fd1e 2282 if (link->mtu != 0)
e2341b6b
DT
2283 log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2284 link->mtu, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), mtu,
2285 link->min_mtu, link->max_mtu);
0d411b7f
YW
2286
2287 link->mtu = mtu;
2288
2289 if (link->dhcp_client) {
2290 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
9c0a72f9 2291 if (r < 0)
0d411b7f
YW
2292 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2293 }
a61bb41c 2294
0d411b7f
YW
2295 if (link->radv) {
2296 r = sd_radv_set_mtu(link->radv, link->mtu);
9c0a72f9 2297 if (r < 0)
0d411b7f 2298 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
a61bb41c
TG
2299 }
2300
2301 return 0;
dd3efc09 2302}
fe8db0c5 2303
0d411b7f
YW
2304static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2305 _cleanup_strv_free_ char **altnames = NULL;
0d411b7f
YW
2306 int r;
2307
2308 assert(link);
2309 assert(message);
2310
2311 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
50df02a7 2312 if (r == -ENODATA)
2368ff81 2313 /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
50df02a7
YW
2314 * interface has no alternative name. */
2315 return 0;
2316 if (r < 0)
0d411b7f
YW
2317 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2318
1b345c1e
YW
2319 if (strv_equal(altnames, link->alternative_names))
2320 return 0;
2321
0d411b7f
YW
2322 STRV_FOREACH(n, link->alternative_names)
2323 hashmap_remove(link->manager->links_by_name, *n);
2324
2325 strv_free_and_replace(link->alternative_names, altnames);
2326
2327 STRV_FOREACH(n, link->alternative_names) {
2328 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2329 if (r < 0)
2330 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2331 }
2332
40695457 2333 return 1; /* needs reconfigure */
0d411b7f
YW
2334}
2335
2336static int link_update_name(Link *link, sd_netlink_message *message) {
01afd0f7 2337 char ifname_from_index[IF_NAMESIZE];
0d411b7f
YW
2338 const char *ifname;
2339 int r;
2340
2341 assert(link);
2342 assert(message);
2343
2344 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2345 if (r == -ENODATA)
7802194a 2346 /* Hmm?? But ok. */
0d411b7f
YW
2347 return 0;
2348 if (r < 0)
2349 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2350
2351 if (streq(ifname, link->ifname))
2352 return 0;
2353
01afd0f7
YW
2354 r = format_ifname(link->ifindex, ifname_from_index);
2355 if (r < 0)
2356 return log_link_debug_errno(link, r, "Could not get interface name for index %i.", link->ifindex);
176b8be1
YW
2357
2358 if (!streq(ifname, ifname_from_index)) {
2359 log_link_debug(link, "New interface name '%s' received from the kernel does not correspond "
2360 "with the name currently configured on the actual interface '%s'. Ignoring.",
2361 ifname, ifname_from_index);
2362 return 0;
2363 }
2364
0d411b7f
YW
2365 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2366
2367 hashmap_remove(link->manager->links_by_name, link->ifname);
2368
2369 r = free_and_strdup(&link->ifname, ifname);
2370 if (r < 0)
2371 return log_oom_debug();
2372
2373 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2374 if (r < 0)
2375 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2376
54d1fdb2
YW
2377 if (link->dhcp_client) {
2378 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2379 if (r < 0)
2380 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2381 }
2382
2383 if (link->dhcp6_client) {
2384 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2385 if (r < 0)
2386 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP6 client: %m");
2387 }
2388
2389 if (link->ndisc) {
2390 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2391 if (r < 0)
2392 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2393 }
2394
2395 if (link->dhcp_server) {
2396 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2397 if (r < 0)
2398 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2399 }
2400
2401 if (link->radv) {
2402 r = sd_radv_set_ifname(link->radv, link->ifname);
2403 if (r < 0)
2404 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2405 }
2406
35778343
YW
2407 if (link->lldp_rx) {
2408 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
54d1fdb2 2409 if (r < 0)
c01b9b87
YW
2410 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2411 }
2412
2413 if (link->lldp_tx) {
2414 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2415 if (r < 0)
2416 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
54d1fdb2
YW
2417 }
2418
2419 if (link->ipv4ll) {
2420 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2421 if (r < 0)
2422 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2423 }
2424
86173383
YW
2425 r = ipv4acd_set_ifname(link);
2426 if (r < 0)
2427 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
54d1fdb2 2428
40695457 2429 return 1; /* needs reconfigure */
0d411b7f
YW
2430}
2431
2432static int link_update(Link *link, sd_netlink_message *message) {
40695457 2433 bool needs_reconfigure = false;
0d411b7f
YW
2434 int r;
2435
2436 assert(link);
2437 assert(message);
2438
2439 r = link_update_name(link, message);
2440 if (r < 0)
2441 return r;
40695457 2442 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2443
2444 r = link_update_alternative_names(link, message);
2445 if (r < 0)
2446 return r;
40695457 2447 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2448
2449 r = link_update_mtu(link, message);
2450 if (r < 0)
2451 return r;
2452
e1658632
YW
2453 r = link_update_driver(link, message);
2454 if (r < 0)
2455 return r;
40695457 2456 needs_reconfigure = needs_reconfigure || r > 0;
e1658632
YW
2457
2458 r = link_update_permanent_hardware_address(link, message);
2459 if (r < 0)
2460 return r;
40695457 2461 needs_reconfigure = needs_reconfigure || r > 0;
e1658632 2462
0d411b7f
YW
2463 r = link_update_hardware_address(link, message);
2464 if (r < 0)
2465 return r;
40695457 2466 needs_reconfigure = needs_reconfigure || r > 0;
0d411b7f
YW
2467
2468 r = link_update_master(link, message);
2469 if (r < 0)
2470 return r;
2471
7d0d832d
YW
2472 r = link_update_ipv6ll_addrgen_mode(link, message);
2473 if (r < 0)
2474 return r;
2475
40695457
YW
2476 r = link_update_flags(link, message);
2477 if (r < 0)
2478 return r;
2479
11cee6ef
YW
2480 r = link_update_bridge_vlan(link, message);
2481 if (r < 0)
2482 return r;
2483
40695457 2484 return needs_reconfigure;
0d411b7f
YW
2485}
2486
0c9ee5d5
YW
2487static Link *link_drop_or_unref(Link *link) {
2488 if (!link)
2489 return NULL;
2490 if (!link->manager)
2491 return link_unref(link);
2492 return link_drop(link);
2493}
2494
2495DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2496
2497static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
0d411b7f 2498 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL, *lease_file = NULL, *lldp_file = NULL;
0c9ee5d5 2499 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
0c9ee5d5
YW
2500 unsigned short iftype;
2501 int r, ifindex;
0c9ee5d5
YW
2502
2503 assert(manager);
2504 assert(message);
2505 assert(ret);
2506
0c9ee5d5
YW
2507 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2508 if (r < 0)
0d411b7f 2509 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
0c9ee5d5 2510 else if (ifindex <= 0)
0d411b7f 2511 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
0c9ee5d5
YW
2512
2513 r = sd_rtnl_message_link_get_type(message, &iftype);
2514 if (r < 0)
0d411b7f 2515 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
0c9ee5d5
YW
2516
2517 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2518 if (r < 0)
0d411b7f 2519 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
0c9ee5d5
YW
2520
2521 /* check for link kind */
2522 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2523 if (r >= 0) {
0d411b7f
YW
2524 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2525 if (r < 0 && r != -ENODATA)
2526 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
0c9ee5d5
YW
2527 r = sd_netlink_message_exit_container(message);
2528 if (r < 0)
0d411b7f 2529 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
0c9ee5d5
YW
2530 }
2531
4c78dc17
YW
2532 if (!manager->test_mode) {
2533 /* Do not update state files when running in test mode. */
2534 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2535 return log_oom_debug();
0d411b7f 2536
4c78dc17
YW
2537 if (asprintf(&lease_file, "/run/systemd/netif/leases/%d", ifindex) < 0)
2538 return log_oom_debug();
0d411b7f 2539
4c78dc17
YW
2540 if (asprintf(&lldp_file, "/run/systemd/netif/lldp/%d", ifindex) < 0)
2541 return log_oom_debug();
2542 }
0d411b7f 2543
0c9ee5d5
YW
2544 link = new(Link, 1);
2545 if (!link)
2546 return -ENOMEM;
2547
2548 *link = (Link) {
2549 .n_ref = 1,
2550 .state = LINK_STATE_PENDING,
2551 .online_state = _LINK_ONLINE_STATE_INVALID,
2552 .ifindex = ifindex,
2553 .iftype = iftype,
2554 .ifname = TAKE_PTR(ifname),
2555 .kind = TAKE_PTR(kind),
2556
11cee6ef
YW
2557 .bridge_vlan_pvid = UINT16_MAX,
2558
7d0d832d
YW
2559 .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2560
0d411b7f
YW
2561 .state_file = TAKE_PTR(state_file),
2562 .lease_file = TAKE_PTR(lease_file),
2563 .lldp_file = TAKE_PTR(lldp_file),
2564
0c9ee5d5
YW
2565 .n_dns = UINT_MAX,
2566 .dns_default_route = -1,
2567 .llmnr = _RESOLVE_SUPPORT_INVALID,
2568 .mdns = _RESOLVE_SUPPORT_INVALID,
2569 .dnssec_mode = _DNSSEC_MODE_INVALID,
2570 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2571 };
2572
6eab614d 2573 r = hashmap_ensure_put(&manager->links_by_index, NULL, INT_TO_PTR(link->ifindex), link);
0c9ee5d5 2574 if (r < 0)
0d411b7f 2575 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
0c9ee5d5
YW
2576
2577 link->manager = manager;
2578
0d411b7f 2579 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
0c9ee5d5 2580 if (r < 0)
0d411b7f 2581 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
0c9ee5d5 2582
8a77e245
YW
2583 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2584 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2585
9c5b8d46
2586 /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2587 * message arrived too early. Request the wireless link information again.
2588 */
2589 if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2590 r = link_get_wlan_interface(link);
2591 if (r < 0)
2592 log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
2593 }
2594
0c9ee5d5 2595 *ret = TAKE_PTR(link);
0c9ee5d5
YW
2596 return 0;
2597}
2598
0d411b7f 2599int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
3be9d62a
YW
2600 Link *link = NULL;
2601 NetDev *netdev = NULL;
2602 uint16_t type;
2603 const char *name;
2604 int r, ifindex;
2605
2606 assert(rtnl);
2607 assert(message);
0d411b7f 2608 assert(manager);
3be9d62a
YW
2609
2610 if (sd_netlink_message_is_error(message)) {
2611 r = sd_netlink_message_get_errno(message);
2612 if (r < 0)
2613 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2614
2615 return 0;
2616 }
2617
2618 r = sd_netlink_message_get_type(message, &type);
2619 if (r < 0) {
2620 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2621 return 0;
2622 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2623 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2624 return 0;
2625 }
2626
2627 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2628 if (r < 0) {
2629 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2630 return 0;
2631 } else if (ifindex <= 0) {
2632 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2633 return 0;
2634 }
2635
2636 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2637 if (r < 0) {
2638 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2639 return 0;
2640 }
2641
6eab614d 2642 (void) link_get_by_index(manager, ifindex, &link);
0d411b7f 2643 (void) netdev_get(manager, name, &netdev);
3be9d62a
YW
2644
2645 switch (type) {
2646 case RTM_NEWLINK:
0d411b7f
YW
2647 if (netdev) {
2648 /* netdev exists, so make sure the ifindex matches */
2649 r = netdev_set_ifindex(netdev, message);
2650 if (r < 0) {
66141164 2651 log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
0d411b7f
YW
2652 return 0;
2653 }
2654 }
2655
3be9d62a
YW
2656 if (!link) {
2657 /* link is new, so add it */
0d411b7f 2658 r = link_new(manager, message, &link);
3be9d62a 2659 if (r < 0) {
0d411b7f 2660 log_warning_errno(r, "Could not process new link message: %m");
3be9d62a
YW
2661 return 0;
2662 }
3be9d62a 2663
0d411b7f 2664 r = link_update(link, message);
3be9d62a 2665 if (r < 0) {
66141164 2666 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f 2667 link_enter_failed(link);
3be9d62a
YW
2668 return 0;
2669 }
3be9d62a 2670
0d411b7f
YW
2671 r = link_check_initialized(link);
2672 if (r < 0) {
66141164 2673 log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
0d411b7f
YW
2674 link_enter_failed(link);
2675 return 0;
2676 }
2677 } else {
2678 r = link_update(link, message);
2679 if (r < 0) {
66141164 2680 log_link_warning_errno(link, r, "Could not process link message: %m");
0d411b7f
YW
2681 link_enter_failed(link);
2682 return 0;
2683 }
40695457
YW
2684 if (r > 0) {
2685 r = link_reconfigure_impl(link, /* force = */ false);
2686 if (r < 0) {
2687 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
2688 link_enter_failed(link);
2689 return 0;
2690 }
2691 }
3be9d62a 2692 }
3be9d62a
YW
2693 break;
2694
2695 case RTM_DELLINK:
2696 link_drop(link);
2697 netdev_drop(netdev);
3be9d62a
YW
2698 break;
2699
2700 default:
04499a70 2701 assert_not_reached();
3be9d62a
YW
2702 }
2703
2704 return 1;
2705}
2706
79c6e114
YW
2707int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
2708 uint16_t message_type;
2709 int r;
2710
2711 assert(m);
2712 assert(link);
2713 assert(error_msg);
2714
2715 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2716 return 0;
2717
2718 r = sd_netlink_message_get_errno(m);
2719 if (r < 0) {
2720 log_link_message_warning_errno(link, m, r, error_msg);
2721 link_enter_failed(link);
2722 return 0;
2723 }
2724
2725 r = sd_netlink_message_get_type(m, &message_type);
2726 if (r < 0) {
2727 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
2728 return 0;
2729 }
2730 if (message_type != RTM_NEWLINK) {
2731 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
2732 return 0;
2733 }
2734
2735 r = link_update(link, m);
2736 if (r < 0) {
2737 link_enter_failed(link);
2738 return 0;
2739 }
2740
2741 return 1;
2742}
2743
2744int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
2745 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
2746 int r;
2747
2748 assert(link);
2749 assert(link->manager);
2750 assert(link->manager->rtnl);
2751 assert(callback);
2752
2753 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2754 if (r < 0)
2755 return r;
2756
2757 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
2758 link_netlink_destroy_callback, link);
2759 if (r < 0)
2760 return r;
2761
2762 link_ref(link);
2763 return 0;
2764}
2765
fe8db0c5 2766static const char* const link_state_table[_LINK_STATE_MAX] = {
0580badc 2767 [LINK_STATE_PENDING] = "pending",
bd08ce56 2768 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 2769 [LINK_STATE_CONFIGURING] = "configuring",
0580badc
ZJS
2770 [LINK_STATE_CONFIGURED] = "configured",
2771 [LINK_STATE_UNMANAGED] = "unmanaged",
2772 [LINK_STATE_FAILED] = "failed",
2773 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
2774};
2775
2776DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
7ae55b78
YW
2777
2778int link_flags_to_string_alloc(uint32_t flags, char **ret) {
2779 _cleanup_free_ char *str = NULL;
19aa7eeb
ZJS
2780 static const char* map[] = {
2781 [LOG2U(IFF_UP)] = "up", /* interface is up. */
0b75493d 2782 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid. */
19aa7eeb
ZJS
2783 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
2784 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
2785 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
2786 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
2787 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
2788 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
2789 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
2790 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
2791 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
2792 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
0b75493d 2793 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast. */
19aa7eeb
ZJS
2794 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
2795 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
2796 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
2797 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
2798 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
2799 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
7ae55b78
YW
2800 };
2801
2802 assert(ret);
2803
2804 for (size_t i = 0; i < ELEMENTSOF(map); i++)
19aa7eeb
ZJS
2805 if (FLAGS_SET(flags, 1 << i) && map[i])
2806 if (!strextend_with_separator(&str, ",", map[i]))
2807 return -ENOMEM;
7ae55b78
YW
2808
2809 *ret = TAKE_PTR(str);
2810 return 0;
2811}
2812
2813static const char * const kernel_operstate_table[] = {
2814 [IF_OPER_UNKNOWN] = "unknown",
2815 [IF_OPER_NOTPRESENT] = "not-present",
2816 [IF_OPER_DOWN] = "down",
2817 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
2818 [IF_OPER_TESTING] = "testing",
2819 [IF_OPER_DORMANT] = "dormant",
2820 [IF_OPER_UP] = "up",
2821};
2822
2823DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);