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