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