]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-link.c
network: dhcp4: release old lease after the new address become ready
[thirdparty/systemd.git] / src / network / networkd-link.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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>
4cc7a82c 7#include <unistd.h>
f579559b 8
b5efdb8a 9#include "alloc-util.h"
737f1405
YW
10#include "bond.h"
11#include "bridge.h"
1346b1f0 12#include "bus-util.h"
27dfc982 13#include "dhcp-identifier.h"
bd91b83e 14#include "dhcp-lease-internal.h"
686d13b9 15#include "env-file.h"
4bb7cc82 16#include "ethtool-util.h"
3ffd4af2 17#include "fd-util.h"
cf1d700d 18#include "fileio.h"
737f1405 19#include "ipvlan.h"
ef118d00 20#include "missing_network.h"
cf1d700d 21#include "netlink-util.h"
c6f7c917 22#include "network-internal.h"
3ddcbeea 23#include "networkd-can.h"
8fcf1d61 24#include "networkd-dhcp-server.h"
ca5ad760
YW
25#include "networkd-dhcp4.h"
26#include "networkd-dhcp6.h"
27#include "networkd-ipv4ll.h"
a0e5c15d 28#include "networkd-ipv6-proxy-ndp.h"
6a1af3d4
YW
29#include "networkd-link-bus.h"
30#include "networkd-link.h"
8e1ad1ea 31#include "networkd-lldp-tx.h"
23f53b99 32#include "networkd-manager.h"
1e7a0e21 33#include "networkd-ndisc.h"
e4a71bf3 34#include "networkd-neighbor.h"
518cd6b5 35#include "networkd-sriov.h"
7465dd22 36#include "networkd-radv.h"
bce67bbe 37#include "networkd-routing-policy-rule.h"
8d968fdd 38#include "networkd-wifi.h"
cf1d700d
TG
39#include "set.h"
40#include "socket-util.h"
bf331d87 41#include "stat-util.h"
15a5e950 42#include "stdio-util.h"
8b43440b 43#include "string-table.h"
51517f9e 44#include "strv.h"
62e021a9 45#include "sysctl-util.h"
34658df2 46#include "tc.h"
e4de7287 47#include "tmpfile-util.h"
299ad32d 48#include "udev-util.h"
cf1d700d 49#include "util.h"
737f1405 50#include "vrf.h"
fc2f9534 51
bdb9f580
YW
52uint32_t link_get_vrf_table(Link *link) {
53 return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
54}
55
56uint32_t link_get_dhcp_route_table(Link *link) {
57 /* When the interface is part of an VRF use the VRFs routing table, unless
58 * another table is explicitly specified. */
59 if (link->network->dhcp_route_table_set)
60 return link->network->dhcp_route_table;
61 return link_get_vrf_table(link);
62}
63
64uint32_t link_get_ipv6_accept_ra_route_table(Link *link) {
65 if (link->network->ipv6_accept_ra_route_table_set)
66 return link->network->ipv6_accept_ra_route_table;
67 return link_get_vrf_table(link);
68}
69
f24648a6
YW
70DUID* link_get_duid(Link *link) {
71 if (link->network->duid.type != _DUID_TYPE_INVALID)
72 return &link->network->duid;
73 else
74 return &link->manager->duid;
75}
76
b9d74c40
LP
77static bool link_dhcp6_enabled(Link *link) {
78 assert(link);
79
fa709992
LP
80 if (!socket_ipv6_is_supported())
81 return false;
82
78c958f8
TG
83 if (link->flags & IFF_LOOPBACK)
84 return false;
85
86 if (!link->network)
87 return false;
88
f2bfcdb9
YW
89 if (link->network->bond)
90 return false;
91
500b96eb 92 if (link->iftype == ARPHRD_CAN)
af9ba57a
YW
93 return false;
94
e0ee46f2 95 return link->network->dhcp & ADDRESS_FAMILY_IPV6;
78c958f8
TG
96}
97
b9d74c40
LP
98static bool link_dhcp4_enabled(Link *link) {
99 assert(link);
100
78c958f8
TG
101 if (link->flags & IFF_LOOPBACK)
102 return false;
103
104 if (!link->network)
105 return false;
106
f2bfcdb9
YW
107 if (link->network->bond)
108 return false;
109
500b96eb 110 if (link->iftype == ARPHRD_CAN)
af9ba57a
YW
111 return false;
112
e0ee46f2 113 return link->network->dhcp & ADDRESS_FAMILY_IPV4;
78c958f8
TG
114}
115
b9d74c40
LP
116static bool link_dhcp4_server_enabled(Link *link) {
117 assert(link);
118
78c958f8
TG
119 if (link->flags & IFF_LOOPBACK)
120 return false;
121
122 if (!link->network)
123 return false;
124
f2bfcdb9
YW
125 if (link->network->bond)
126 return false;
127
500b96eb 128 if (link->iftype == ARPHRD_CAN)
af9ba57a
YW
129 return false;
130
78c958f8
TG
131 return link->network->dhcp_server;
132}
133
2d792895 134bool link_ipv4ll_enabled(Link *link, AddressFamily mask) {
b9d74c40 135 assert(link);
910feb78 136 assert((mask & ~(ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) == 0);
b9d74c40 137
78c958f8
TG
138 if (link->flags & IFF_LOOPBACK)
139 return false;
140
141 if (!link->network)
142 return false;
143
500b96eb 144 if (link->iftype == ARPHRD_CAN)
c6ac3729
YW
145 return false;
146
98d20a17 147 if (STRPTR_IN_SET(link->kind,
148 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
c6ac3729 149 "vti6", "nlmon", "xfrm"))
a8f5bba6
JD
150 return false;
151
f410d463
YW
152 /* L3 or L3S mode do not support ARP. */
153 if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
154 return false;
155
f2bfcdb9
YW
156 if (link->network->bond)
157 return false;
158
910feb78 159 return link->network->link_local & mask;
8bc17bb3
SS
160}
161
b9d74c40
LP
162static bool link_ipv6ll_enabled(Link *link) {
163 assert(link);
164
fa709992
LP
165 if (!socket_ipv6_is_supported())
166 return false;
167
d0d6a4cd
TG
168 if (link->flags & IFF_LOOPBACK)
169 return false;
170
171 if (!link->network)
172 return false;
173
500b96eb 174 if (link->iftype == ARPHRD_CAN)
c6ac3729
YW
175 return false;
176
177 if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
a8f5bba6
JD
178 return false;
179
f2bfcdb9
YW
180 if (link->network->bond)
181 return false;
182
e0ee46f2 183 return link->network->link_local & ADDRESS_FAMILY_IPV6;
78c958f8
TG
184}
185
439689c6
SS
186static bool link_ipv6_enabled(Link *link) {
187 assert(link);
188
189 if (!socket_ipv6_is_supported())
190 return false;
191
b102cdca 192 if (link->network->bond)
2b00a4e0
TY
193 return false;
194
500b96eb 195 if (link->iftype == ARPHRD_CAN)
af9ba57a
YW
196 return false;
197
4cef7fe3 198 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
adfeee49
YW
199 if (link_ipv6ll_enabled(link))
200 return true;
201
202 if (network_has_static_ipv6_configurations(link->network))
203 return true;
204
205 return false;
439689c6
SS
206}
207
7465dd22
PF
208static bool link_radv_enabled(Link *link) {
209 assert(link);
210
211 if (!link_ipv6ll_enabled(link))
212 return false;
213
55a7c78b 214 return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE;
7465dd22
PF
215}
216
769d324c 217static bool link_ipv4_forward_enabled(Link *link) {
b9d74c40
LP
218 assert(link);
219
5a8bcb67
LP
220 if (link->flags & IFF_LOOPBACK)
221 return false;
222
223 if (!link->network)
224 return false;
225
2d792895 226 if (link->network->ip_forward == _ADDRESS_FAMILY_INVALID)
765afd5c
LP
227 return false;
228
e0ee46f2 229 return link->network->ip_forward & ADDRESS_FAMILY_IPV4;
769d324c
LP
230}
231
232static bool link_ipv6_forward_enabled(Link *link) {
b9d74c40 233 assert(link);
765afd5c
LP
234
235 if (!socket_ipv6_is_supported())
236 return false;
237
769d324c
LP
238 if (link->flags & IFF_LOOPBACK)
239 return false;
240
241 if (!link->network)
242 return false;
243
2d792895 244 if (link->network->ip_forward == _ADDRESS_FAMILY_INVALID)
765afd5c
LP
245 return false;
246
e0ee46f2 247 return link->network->ip_forward & ADDRESS_FAMILY_IPV6;
5a8bcb67
LP
248}
249
23d8b221
SS
250static bool link_proxy_arp_enabled(Link *link) {
251 assert(link);
252
253 if (link->flags & IFF_LOOPBACK)
254 return false;
255
256 if (!link->network)
257 return false;
258
259 if (link->network->proxy_arp < 0)
260 return false;
261
262 return true;
263}
264
b9d74c40
LP
265static bool link_ipv6_accept_ra_enabled(Link *link) {
266 assert(link);
267
fa709992
LP
268 if (!socket_ipv6_is_supported())
269 return false;
270
f5a8c43f
TG
271 if (link->flags & IFF_LOOPBACK)
272 return false;
273
274 if (!link->network)
275 return false;
276
702c979f
SS
277 if (!link_ipv6ll_enabled(link))
278 return false;
279
f5a8c43f
TG
280 /* If unset use system default (enabled if local forwarding is disabled.
281 * disabled if local forwarding is enabled).
282 * If set, ignore or enforce RA independent of local forwarding state.
283 */
284 if (link->network->ipv6_accept_ra < 0)
285 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
286 return !link_ipv6_forward_enabled(link);
287 else if (link->network->ipv6_accept_ra > 0)
288 /* accept RA even if ip_forward is enabled */
289 return true;
290 else
291 /* ignore RA */
292 return false;
293}
294
1f0d9695 295static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) {
fa709992 296 assert(link);
d68e2e59
LP
297
298 if (!socket_ipv6_is_supported())
299 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
300
49092e22 301 if (link->flags & IFF_LOOPBACK)
1f0d9695 302 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
49092e22
SS
303
304 if (!link->network)
1f0d9695 305 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
49092e22
SS
306
307 return link->network->ipv6_privacy_extensions;
308}
309
57ad7607 310static int link_update_ipv6_sysctl(Link *link) {
482efedc 311 bool enabled;
439689c6
SS
312 int r;
313
314 if (link->flags & IFF_LOOPBACK)
315 return 0;
316
482efedc
SS
317 enabled = link_ipv6_enabled(link);
318 if (enabled) {
319 r = sysctl_write_ip_property_boolean(AF_INET6, link->ifname, "disable_ipv6", false);
320 if (r < 0)
57ad7607
ZJS
321 return log_link_warning_errno(link, r, "Cannot enable IPv6: %m");
322
323 log_link_info(link, "IPv6 successfully enabled");
482efedc 324 }
439689c6
SS
325
326 return 0;
327}
328
34bf3c00
YW
329static bool link_is_enslaved(Link *link) {
330 if (link->flags & IFF_SLAVE)
331 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
332 return true;
333
34bf3c00
YW
334 if (!link->network)
335 return false;
336
bb262ef0 337 if (link->master_ifindex > 0 && link->network->bridge)
34bf3c00
YW
338 return true;
339
bb262ef0
YW
340 /* TODO: add conditions for other netdevs. */
341
34bf3c00
YW
342 return false;
343}
344
45e11abf
YW
345static void link_update_master_operstate(Link *link, NetDev *netdev) {
346 Link *master;
347
348 if (!netdev)
349 return;
350
7936917e
YW
351 if (netdev->ifindex <= 0)
352 return;
353
45e11abf
YW
354 if (link_get(link->manager, netdev->ifindex, &master) < 0)
355 return;
356
357 link_update_operstate(master, true);
358}
359
360void link_update_operstate(Link *link, bool also_update_master) {
84de38c5 361 LinkOperationalState operstate;
1678fbb3
YW
362 LinkCarrierState carrier_state;
363 LinkAddressState address_state;
35c5a9ca 364 _cleanup_strv_free_ char **p = NULL;
1678fbb3 365 uint8_t scope = RT_SCOPE_NOWHERE;
35c5a9ca 366 bool changed = false;
1678fbb3 367 Address *address;
45e11abf 368 Iterator i;
14153d1b 369
84de38c5
TG
370 assert(link);
371
372 if (link->kernel_operstate == IF_OPER_DORMANT)
1678fbb3 373 carrier_state = LINK_CARRIER_STATE_DORMANT;
84de38c5 374 else if (link_has_carrier(link)) {
1678fbb3
YW
375 if (link_is_enslaved(link))
376 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
84de38c5 377 else
1678fbb3 378 carrier_state = LINK_CARRIER_STATE_CARRIER;
84de38c5 379 } else if (link->flags & IFF_UP)
1678fbb3 380 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
84de38c5 381 else
1678fbb3 382 carrier_state = LINK_CARRIER_STATE_OFF;
84de38c5 383
1678fbb3 384 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
959f65d3
YW
385 Link *slave;
386
5f707e12 387 SET_FOREACH(slave, link->slaves, i) {
959f65d3 388 link_update_operstate(slave, false);
84de38c5 389
1678fbb3
YW
390 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
391 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
84de38c5 392 }
959f65d3 393 }
84de38c5 394
1678fbb3
YW
395 SET_FOREACH(address, link->addresses, i) {
396 if (!address_is_ready(address))
397 continue;
84de38c5 398
1678fbb3
YW
399 if (address->scope < scope)
400 scope = address->scope;
401 }
84de38c5 402
1678fbb3
YW
403 /* for operstate we also take foreign addresses into account */
404 SET_FOREACH(address, link->addresses_foreign, i) {
405 if (!address_is_ready(address))
406 continue;
407
408 if (address->scope < scope)
409 scope = address->scope;
410 }
411
412 if (scope < RT_SCOPE_SITE)
413 /* universally accessible addresses found */
414 address_state = LINK_ADDRESS_STATE_ROUTABLE;
415 else if (scope < RT_SCOPE_HOST)
416 /* only link or site local addresses found */
417 address_state = LINK_ADDRESS_STATE_DEGRADED;
84de38c5 418 else
1678fbb3
YW
419 /* no useful addresses found */
420 address_state = LINK_ADDRESS_STATE_OFF;
421
422 /* Mapping of address and carrier state vs operational state
423 * carrier state
424 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
425 * ------------------------------------------------------------------------------
426 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
427 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
428 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
429 */
84de38c5 430
1678fbb3
YW
431 if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
432 operstate = (LinkOperationalState) carrier_state;
433 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
434 operstate = LINK_OPERSTATE_ROUTABLE;
435 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
436 operstate = LINK_OPERSTATE_DEGRADED;
437 else
14153d1b
YW
438 operstate = LINK_OPERSTATE_ENSLAVED;
439
35c5a9ca
YW
440 if (link->carrier_state != carrier_state) {
441 link->carrier_state = carrier_state;
442 changed = true;
443 if (strv_extend(&p, "CarrierState") < 0)
444 log_oom();
445 }
959f65d3 446
35c5a9ca
YW
447 if (link->address_state != address_state) {
448 link->address_state = address_state;
449 changed = true;
450 if (strv_extend(&p, "AddressState") < 0)
451 log_oom();
959f65d3
YW
452 }
453
84de38c5
TG
454 if (link->operstate != operstate) {
455 link->operstate = operstate;
35c5a9ca
YW
456 changed = true;
457 if (strv_extend(&p, "OperationalState") < 0)
458 log_oom();
84de38c5 459 }
959f65d3 460
35c5a9ca
YW
461 if (p)
462 link_send_changed_strv(link, p);
463 if (changed)
464 link_dirty(link);
465
45e11abf
YW
466 if (also_update_master && link->network) {
467 link_update_master_operstate(link, link->network->bond);
468 link_update_master_operstate(link, link->network->bridge);
959f65d3 469 }
84de38c5
TG
470}
471
51d18171
TG
472#define FLAG_STRING(string, flag, old, new) \
473 (((old ^ new) & flag) \
474 ? ((old & flag) ? (" -" string) : (" +" string)) \
475 : "")
476
bb262ef0 477static int link_update_flags(Link *link, sd_netlink_message *m, bool force_update_operstate) {
51d18171
TG
478 unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
479 uint8_t operstate;
480 int r;
481
482 assert(link);
483
484 r = sd_rtnl_message_link_get_flags(m, &flags);
6a7a4e4d
LP
485 if (r < 0)
486 return log_link_warning_errno(link, r, "Could not get link flags: %m");
51d18171 487
1c4baffc 488 r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate);
51d18171
TG
489 if (r < 0)
490 /* if we got a message without operstate, take it to mean
491 the state was unchanged */
492 operstate = link->kernel_operstate;
493
bb262ef0 494 if (!force_update_operstate && (link->flags == flags) && (link->kernel_operstate == operstate))
51d18171
TG
495 return 0;
496
497 if (link->flags != flags) {
6a7a4e4d 498 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",
51d18171
TG
499 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
500 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
501 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
502 FLAG_STRING("UP", IFF_UP, link->flags, flags),
503 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
504 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
505 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
506 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
507 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
508 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
509 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
510 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
511 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
512 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
513 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
514 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
515 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
516 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
517 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
518
519 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
520 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
521 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
522 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
523 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
524 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
525 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
526 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
527
528 /* link flags are currently at most 18 bits, let's align to
529 * printing 20 */
530 if (unknown_flags_added)
79008bdd 531 log_link_debug(link,
6a7a4e4d 532 "Unknown link flags gained: %#.5x (ignoring)",
51d18171
TG
533 unknown_flags_added);
534
535 if (unknown_flags_removed)
79008bdd 536 log_link_debug(link,
6a7a4e4d 537 "Unknown link flags lost: %#.5x (ignoring)",
51d18171
TG
538 unknown_flags_removed);
539 }
540
541 link->flags = flags;
542 link->kernel_operstate = operstate;
543
959f65d3 544 link_update_operstate(link, true);
51d18171
TG
545
546 return 0;
547}
548
1c4baffc 549static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
8e766630 550 _cleanup_(link_unrefp) Link *link = NULL;
6cad256d 551 const char *ifname, *kind = NULL;
b710e6b6 552 unsigned short iftype;
572b21d9
YW
553 int r, ifindex;
554 uint16_t type;
f579559b 555
0c2f9b84 556 assert(manager);
505f8da7 557 assert(message);
f579559b
TG
558 assert(ret);
559
6cad256d
TJ
560 /* check for link kind */
561 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
562 if (r == 0) {
db2f8a2e 563 (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind);
6cad256d
TJ
564 r = sd_netlink_message_exit_container(message);
565 if (r < 0)
566 return r;
567 }
568
1c4baffc 569 r = sd_netlink_message_get_type(message, &type);
505f8da7
TG
570 if (r < 0)
571 return r;
572 else if (type != RTM_NEWLINK)
573 return -EINVAL;
574
575 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
576 if (r < 0)
577 return r;
578 else if (ifindex <= 0)
579 return -EINVAL;
580
b710e6b6
LP
581 r = sd_rtnl_message_link_get_type(message, &iftype);
582 if (r < 0)
583 return r;
584
1c4baffc 585 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
505f8da7
TG
586 if (r < 0)
587 return r;
588
17f9c355 589 link = new(Link, 1);
f579559b
TG
590 if (!link)
591 return -ENOMEM;
592
17f9c355
YW
593 *link = (Link) {
594 .n_ref = 1,
595 .manager = manager,
596 .state = LINK_STATE_PENDING,
17f9c355
YW
597 .ifindex = ifindex,
598 .iftype = iftype,
15761549
YW
599
600 .n_dns = (unsigned) -1,
601 .dns_default_route = -1,
602 .llmnr = _RESOLVE_SUPPORT_INVALID,
603 .mdns = _RESOLVE_SUPPORT_INVALID,
604 .dnssec_mode = _DNSSEC_MODE_INVALID,
605 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
17f9c355
YW
606 };
607
505f8da7
TG
608 link->ifname = strdup(ifname);
609 if (!link->ifname)
610 return -ENOMEM;
f579559b 611
6cad256d
TJ
612 if (kind) {
613 link->kind = strdup(kind);
614 if (!link->kind)
615 return -ENOMEM;
616 }
617
cbff7170
TJ
618 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex);
619 if (r < 0)
620 log_link_debug_errno(link, r, "New device has no master, continuing without");
621
1c4baffc 622 r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
512922f8 623 if (r < 0)
34437b4f 624 log_link_debug_errno(link, r, "MAC address not found for new device, continuing without");
512922f8 625
c643bda5 626 r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac);
4bb7cc82
YW
627 if (r < 0)
628 log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m");
629
c643bda5
YW
630 r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver);
631 if (r < 0)
632 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
633
572b21d9
YW
634 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &link->alternative_names);
635 if (r < 0 && r != -ENODATA)
636 return r;
637
34437b4f 638 if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0)
315db1a8 639 return -ENOMEM;
fe8db0c5 640
34437b4f 641 if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0)
68a8723c
TG
642 return -ENOMEM;
643
34437b4f 644 if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
49699bac
SS
645 return -ENOMEM;
646
d5099efc 647 r = hashmap_ensure_allocated(&manager->links, NULL);
ae06ab10
TG
648 if (r < 0)
649 return r;
650
651 r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
f579559b
TG
652 if (r < 0)
653 return r;
654
bb262ef0 655 r = link_update_flags(link, message, false);
51d18171
TG
656 if (r < 0)
657 return r;
658
1cc6c93a 659 *ret = TAKE_PTR(link);
f579559b
TG
660
661 return 0;
662}
663
15761549
YW
664void link_ntp_settings_clear(Link *link) {
665 link->ntp = strv_free(link->ntp);
666}
667
668void link_dns_settings_clear(Link *link) {
e77bd3fd
YW
669 if (link->n_dns != (unsigned) -1)
670 for (unsigned i = 0; i < link->n_dns; i++)
671 in_addr_full_free(link->dns[i]);
15761549
YW
672 link->dns = mfree(link->dns);
673 link->n_dns = (unsigned) -1;
674
675 link->search_domains = ordered_set_free_free(link->search_domains);
676 link->route_domains = ordered_set_free_free(link->route_domains);
677
678 link->dns_default_route = -1;
679 link->llmnr = _RESOLVE_SUPPORT_INVALID;
680 link->mdns = _RESOLVE_SUPPORT_INVALID;
681 link->dnssec_mode = _DNSSEC_MODE_INVALID;
682 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
683
684 link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
685}
686
1a6bb31f
YW
687static void link_free_engines(Link *link) {
688 if (!link)
689 return;
690
691 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
692 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
693 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
1a6bb31f
YW
694
695 link->lldp = sd_lldp_unref(link->lldp);
696
697 ndisc_flush(link);
698
699 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
700 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
701 link->ndisc = sd_ndisc_unref(link->ndisc);
702 link->radv = sd_radv_unref(link->radv);
703}
704
8301aa0b 705static Link *link_free(Link *link) {
428fd0a7
TG
706 Address *address;
707
8301aa0b 708 assert(link);
f579559b 709
15761549
YW
710 link_ntp_settings_clear(link);
711 link_dns_settings_clear(link);
712
8eec0b9d
YW
713 link->routes = set_free(link->routes);
714 link->routes_foreign = set_free(link->routes_foreign);
6e537f62
YW
715 link->dhcp_routes = set_free(link->dhcp_routes);
716 link->dhcp_routes_old = set_free(link->dhcp_routes_old);
adda1ed9 717
8eec0b9d
YW
718 link->nexthops = set_free(link->nexthops);
719 link->nexthops_foreign = set_free(link->nexthops_foreign);
c16c7808 720
8eec0b9d
YW
721 link->neighbors = set_free(link->neighbors);
722 link->neighbors_foreign = set_free(link->neighbors_foreign);
d1bdafd2 723
8eec0b9d
YW
724 link->addresses = set_free(link->addresses);
725 link->addresses_foreign = set_free(link->addresses_foreign);
adda1ed9 726
11bf3cce
LP
727 while ((address = link->pool_addresses)) {
728 LIST_REMOVE(addresses, link->pool_addresses, address);
729 address_free(address);
730 }
731
7272b25e 732 link_lldp_emit_stop(link);
1a6bb31f 733 link_free_engines(link);
68a8723c 734 free(link->lease_file);
49699bac
SS
735 free(link->lldp_file);
736
c166a070 737 free(link->ifname);
572b21d9 738 strv_free(link->alternative_names);
ceac4078 739 free(link->kind);
8d968fdd 740 free(link->ssid);
c643bda5 741 free(link->driver);
6cad256d 742
db2f8a2e 743 (void) unlink(link->state_file);
fe8db0c5 744 free(link->state_file);
c166a070 745
51517f9e 746 sd_device_unref(link->sd_device);
b5db00e5 747
0d4ad91d 748 hashmap_free(link->bound_to_links);
0d4ad91d
AR
749 hashmap_free(link->bound_by_links);
750
5f707e12 751 set_free_with_destructor(link->slaves, link_unref);
033295c1 752
c9c908a6
YW
753 network_unref(link->network);
754
8301aa0b 755 return mfree(link);
14b746f7
TG
756}
757
8301aa0b 758DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
14b746f7 759
11a7f229
TG
760int link_get(Manager *m, int ifindex, Link **ret) {
761 Link *link;
11a7f229
TG
762
763 assert(m);
e856ed00 764 assert(ifindex > 0);
11a7f229
TG
765 assert(ret);
766
ae06ab10 767 link = hashmap_get(m->links, INT_TO_PTR(ifindex));
11a7f229
TG
768 if (!link)
769 return -ENODEV;
770
771 *ret = link;
772
773 return 0;
774}
775
af9ba57a 776void link_set_state(Link *link, LinkState state) {
e331e246
TG
777 assert(link);
778
779 if (link->state == state)
780 return;
781
0beb9542
YW
782 log_link_debug(link, "State changed: %s -> %s",
783 link_state_to_string(link->state),
784 link_state_to_string(state));
785
e331e246
TG
786 link->state = state;
787
788 link_send_changed(link, "AdministrativeState", NULL);
e331e246
TG
789}
790
57bd6899
TG
791static void link_enter_unmanaged(Link *link) {
792 assert(link);
793
e331e246 794 link_set_state(link, LINK_STATE_UNMANAGED);
57bd6899 795
84de38c5 796 link_dirty(link);
57bd6899
TG
797}
798
95355a28 799int link_stop_clients(Link *link, bool may_keep_dhcp) {
111bb8f9 800 int r = 0, k;
051e77ca 801 Address *ad;
111bb8f9
TG
802
803 assert(link);
804 assert(link->manager);
805 assert(link->manager->event);
806
80060352
ZJS
807 bool keep_dhcp = may_keep_dhcp &&
808 link->network &&
809 (link->manager->restarting ||
810 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
811
812 if (link->dhcp_client && !keep_dhcp) {
111bb8f9 813 k = sd_dhcp_client_stop(link->dhcp_client);
6a7a4e4d 814 if (k < 0)
36c7d709 815 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m");
111bb8f9
TG
816 }
817
ba179154 818 if (link->ipv4ll) {
111bb8f9 819 k = sd_ipv4ll_stop(link->ipv4ll);
6a7a4e4d 820 if (k < 0)
36c7d709 821 r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m");
dd43110f
TG
822 }
823
051e77ca
SS
824 if (link->network)
825 LIST_FOREACH(addresses, ad, link->network->static_addresses)
826 if (ad->acd && sd_ipv4acd_is_running(ad->acd) == 0) {
827 k = sd_ipv4acd_stop(ad->acd);
828 if (k < 0)
829 r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m");
830 }
831
f5a8c43f
TG
832 if (link->dhcp6_client) {
833 k = sd_dhcp6_client_stop(link->dhcp6_client);
834 if (k < 0)
36c7d709 835 r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m");
f5a8c43f 836 }
4138fb2c 837
1e7a0e21
LP
838 if (link->ndisc) {
839 k = sd_ndisc_stop(link->ndisc);
6a7a4e4d 840 if (k < 0)
36c7d709 841 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m");
4138fb2c
PF
842 }
843
7465dd22 844 if (link->radv) {
290696e5 845 k = sd_radv_stop(link->radv);
7465dd22
PF
846 if (k < 0)
847 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m");
848 }
849
7272b25e 850 link_lldp_emit_stop(link);
111bb8f9
TG
851 return r;
852}
853
b22d8a00 854void link_enter_failed(Link *link) {
ef1ba606 855 assert(link);
f882c247 856
370e9930 857 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2139694e
TG
858 return;
859
6a7a4e4d 860 log_link_warning(link, "Failed");
449f7554 861
e331e246 862 link_set_state(link, LINK_STATE_FAILED);
fe8db0c5 863
95355a28 864 link_stop_clients(link, false);
111bb8f9 865
84de38c5 866 link_dirty(link);
f882c247
TG
867}
868
7033af49
YW
869static int link_join_netdevs_after_configured(Link *link) {
870 NetDev *netdev;
871 Iterator i;
872 int r;
873
874 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
875 if (netdev->ifindex > 0)
876 /* Assume already enslaved. */
877 continue;
878
879 if (netdev_get_create_type(netdev) != NETDEV_CREATE_AFTER_CONFIGURED)
880 continue;
881
882 log_struct(LOG_DEBUG,
883 LOG_LINK_INTERFACE(link),
884 LOG_NETDEV_INTERFACE(netdev),
885 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
886
887 r = netdev_join(netdev, link, NULL);
888 if (r < 0)
889 return log_struct_errno(LOG_WARNING, r,
890 LOG_LINK_INTERFACE(link),
891 LOG_NETDEV_INTERFACE(netdev),
892 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
893 }
894
895 return 0;
896}
897
e3a7b048 898static void link_enter_configured(Link *link) {
dd43110f
TG
899 assert(link);
900 assert(link->network);
e3a7b048 901
289e6774 902 if (link->state != LINK_STATE_CONFIGURING)
e3a7b048 903 return;
dd43110f 904
e331e246 905 link_set_state(link, LINK_STATE_CONFIGURED);
dd43110f 906
7033af49
YW
907 (void) link_join_netdevs_after_configured(link);
908
84de38c5 909 link_dirty(link);
dd43110f
TG
910}
911
47079967 912static int link_request_set_routing_policy_rule(Link *link) {
8a9b3a23
SS
913 RoutingPolicyRule *rule, *rrule = NULL;
914 int r;
915
916 assert(link);
917 assert(link->network);
918
2428613f
YW
919 link->routing_policy_rules_configured = false;
920
8a9b3a23 921 LIST_FOREACH(rules, rule, link->network->rules) {
b80a511b 922 r = routing_policy_rule_get(link->manager, rule, &rrule);
ff14e2eb
YW
923 if (r >= 0) {
924 if (r == 0)
925 (void) routing_policy_rule_make_local(link->manager, rrule);
8a9b3a23
SS
926 continue;
927 }
928
9f08a578 929 r = routing_policy_rule_configure(rule, link, NULL);
4ff296b0
YW
930 if (r < 0)
931 return log_link_warning_errno(link, r, "Could not set routing policy rules: %m");
7ef7e550
YW
932 if (r > 0)
933 link->routing_policy_rule_messages++;
8a9b3a23
SS
934 }
935
936 routing_policy_rule_purge(link->manager, link);
5d976f5f 937 if (link->routing_policy_rule_messages == 0)
7715629e 938 link->routing_policy_rules_configured = true;
5d976f5f 939 else {
7715629e 940 log_link_debug(link, "Setting routing policy rules");
b425c3ae
YW
941 link_set_state(link, LINK_STATE_CONFIGURING);
942 }
8a9b3a23
SS
943
944 return 0;
945}
946
c16c7808
SS
947static int nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
948 int r;
949
950 assert(link);
951 assert(link->nexthop_messages > 0);
952 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
953 LINK_STATE_FAILED, LINK_STATE_LINGER));
954
955 link->nexthop_messages--;
956
957 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
958 return 1;
959
960 r = sd_netlink_message_get_errno(m);
961 if (r < 0 && r != -EEXIST) {
5ecb131d 962 log_link_message_warning_errno(link, m, r, "Could not set nexthop");
c16c7808
SS
963 link_enter_failed(link);
964 return 1;
965 }
966
967 if (link->nexthop_messages == 0) {
968 log_link_debug(link, "Nexthop set");
969 link->static_nexthops_configured = true;
970 link_check_ready(link);
971 }
972
973 return 1;
974}
975
eb01a2df 976static int link_request_set_nexthop(Link *link) {
c16c7808
SS
977 NextHop *nh;
978 int r;
979
0c816fcc
YW
980 link->static_nexthops_configured = false;
981
c16c7808
SS
982 LIST_FOREACH(nexthops, nh, link->network->static_nexthops) {
983 r = nexthop_configure(nh, link, nexthop_handler);
984 if (r < 0)
985 return log_link_warning_errno(link, r, "Could not set nexthop: %m");
986 if (r > 0)
987 link->nexthop_messages++;
988 }
989
990 if (link->nexthop_messages == 0) {
991 link->static_nexthops_configured = true;
992 link_check_ready(link);
993 } else {
994 log_link_debug(link, "Setting nexthop");
995 link_set_state(link, LINK_STATE_CONFIGURING);
996 }
997
998 return 1;
999}
1000
302a796f 1001static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
f882c247
TG
1002 int r;
1003
1046bf9b 1004 assert(link);
7715629e 1005 assert(link->route_messages > 0);
289e6774
WKI
1006 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
1007 LINK_STATE_FAILED, LINK_STATE_LINGER));
f882c247 1008
7715629e 1009 link->route_messages--;
f882c247 1010
77a008c0 1011 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
f882c247
TG
1012 return 1;
1013
1c4baffc 1014 r = sd_netlink_message_get_errno(m);
4ff296b0 1015 if (r < 0 && r != -EEXIST) {
5ecb131d 1016 log_link_message_warning_errno(link, m, r, "Could not set route");
4ff296b0
YW
1017 link_enter_failed(link);
1018 return 1;
1019 }
f882c247 1020
7715629e 1021 if (link->route_messages == 0) {
6a7a4e4d 1022 log_link_debug(link, "Routes set");
7715629e 1023 link->static_routes_configured = true;
0c816fcc 1024 link_request_set_nexthop(link);
dd3efc09 1025 }
f882c247
TG
1026
1027 return 1;
1028}
1029
b5799eeb 1030int link_request_set_routes(Link *link) {
bf61b05a
LP
1031 enum {
1032 PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */
1033 PHASE_GATEWAY, /* Second phase: Routes with a gateway */
1034 _PHASE_MAX
1035 } phase;
a6cc569e 1036 Route *rt;
f882c247
TG
1037 int r;
1038
1039 assert(link);
1040 assert(link->network);
c42ff3a1
WKI
1041 assert(link->addresses_configured);
1042 assert(link->address_messages == 0);
289e6774 1043 assert(link->state != _LINK_STATE_INVALID);
f882c247 1044
2428613f 1045 link->static_routes_configured = false;
27c34f73 1046
4e2ef9d9
YW
1047 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
1048 /* During configuring addresses, the link lost its carrier. As networkd is dropping
1049 * the addresses now, let's not configure the routes either. */
1050 return 0;
1051
47079967 1052 r = link_request_set_routing_policy_rule(link);
f3ef324d
YW
1053 if (r < 0)
1054 return r;
f882c247 1055
bf61b05a
LP
1056 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
1057 for (phase = 0; phase < _PHASE_MAX; phase++)
1058 LIST_FOREACH(routes, rt, link->network->static_routes) {
1985c54f
YW
1059 if (rt->gateway_from_dhcp)
1060 continue;
0d34228f 1061
6ff5cc6b 1062 if ((in_addr_is_null(rt->family, &rt->gw) && ordered_set_isempty(rt->multipath_routes)) != (phase == PHASE_NON_GATEWAY))
bf61b05a 1063 continue;
a6cc569e 1064
80b0e860 1065 r = route_configure(rt, link, route_handler, NULL);
4ff296b0
YW
1066 if (r < 0)
1067 return log_link_warning_errno(link, r, "Could not set routes: %m");
c4423317
YW
1068 if (r > 0)
1069 link->route_messages++;
0d34228f 1070 }
f5be5601 1071
7715629e
ST
1072 if (link->route_messages == 0) {
1073 link->static_routes_configured = true;
0c816fcc 1074 link_request_set_nexthop(link);
b425c3ae 1075 } else {
6a7a4e4d 1076 log_link_debug(link, "Setting routes");
b425c3ae
YW
1077 link_set_state(link, LINK_STATE_CONFIGURING);
1078 }
f882c247
TG
1079
1080 return 0;
1081}
1082
6accfd31
DA
1083void link_check_ready(Link *link) {
1084 Address *a;
1085 Iterator i;
4ff296b0 1086 int r;
6accfd31
DA
1087
1088 assert(link);
1089
5f58af25
YW
1090 if (link->state == LINK_STATE_CONFIGURED)
1091 return;
1092
1093 if (link->state != LINK_STATE_CONFIGURING) {
1094 log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
6accfd31 1095 return;
39373cb9 1096 }
6accfd31
DA
1097
1098 if (!link->network)
1099 return;
1100
39373cb9
YW
1101 if (!link->addresses_configured) {
1102 log_link_debug(link, "%s(): static addresses are not configured.", __func__);
6accfd31 1103 return;
39373cb9 1104 }
6accfd31 1105
39373cb9
YW
1106 if (!link->neighbors_configured) {
1107 log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
6accfd31 1108 return;
39373cb9 1109 }
6accfd31 1110
6aa5773b 1111 SET_FOREACH(a, link->addresses, i)
39373cb9
YW
1112 if (!address_is_ready(a)) {
1113 _cleanup_free_ char *str = NULL;
1114
1115 (void) in_addr_to_string(a->family, &a->in_addr, &str);
1116 log_link_debug(link, "%s(): an address %s/%d is not ready.", __func__, strnull(str), a->prefixlen);
6aa5773b 1117 return;
39373cb9 1118 }
6aa5773b
DA
1119
1120 if (!link->addresses_ready) {
1121 link->addresses_ready = true;
4ff296b0
YW
1122 r = link_request_set_routes(link);
1123 if (r < 0)
1124 link_enter_failed(link);
39373cb9 1125 log_link_debug(link, "%s(): static addresses are configured. Configuring static routes.", __func__);
710ce9e5 1126 return;
6aa5773b
DA
1127 }
1128
39373cb9
YW
1129 if (!link->static_routes_configured) {
1130 log_link_debug(link, "%s(): static routes are not configured.", __func__);
6accfd31 1131 return;
39373cb9 1132 }
6accfd31 1133
39373cb9
YW
1134 if (!link->static_nexthops_configured) {
1135 log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
c16c7808 1136 return;
39373cb9 1137 }
c16c7808 1138
39373cb9
YW
1139 if (!link->routing_policy_rules_configured) {
1140 log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
6accfd31 1141 return;
39373cb9 1142 }
6accfd31 1143
39373cb9
YW
1144 if (!link->tc_configured) {
1145 log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
4ecdcb07 1146 return;
39373cb9 1147 }
4ecdcb07 1148
39373cb9
YW
1149 if (!link->sr_iov_configured) {
1150 log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
518cd6b5 1151 return;
39373cb9 1152 }
518cd6b5 1153
463797c1 1154 if (link_has_carrier(link) || !link->network->configure_without_carrier) {
6accfd31 1155
39373cb9
YW
1156 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4) && !link->ipv4ll_address) {
1157 log_link_debug(link, "%s(): IPv4LL is not configured.", __func__);
463797c1 1158 return;
39373cb9 1159 }
6accfd31 1160
463797c1 1161 if (link_ipv6ll_enabled(link) &&
39373cb9
YW
1162 in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
1163 log_link_debug(link, "%s(): IPv6LL is not configured.", __func__);
463797c1 1164 return;
39373cb9 1165 }
6accfd31 1166
659ad3a0
YW
1167 if ((link_dhcp4_enabled(link) || link_dhcp6_enabled(link)) && set_isempty(link->addresses)) {
1168 log_link_debug(link, "%s(): DHCP4 or DHCP6 is enabled but no address is assigned yet.", __func__);
1169 return;
1170 }
1171
39373cb9
YW
1172 if (link_dhcp4_enabled(link) || link_dhcp6_enabled(link) || dhcp6_get_prefix_delegation(link) || link_ipv6_accept_ra_enabled(link)) {
1173 if (!link->dhcp4_configured &&
1174 !(link->dhcp6_address_configured && link->dhcp6_route_configured) &&
1175 !(link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured) &&
d98c546d 1176 !(link->ndisc_addresses_configured && link->ndisc_routes_configured) &&
39373cb9
YW
1177 !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address)) {
1178 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
1179 * an IPv4ll fallback address must be configured. */
1180 log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__);
1181 return;
1182 }
1183
d98c546d 1184 log_link_debug(link, "%s(): dhcp4:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc_addresses:%s ndisc_routes:%s",
39373cb9
YW
1185 __func__,
1186 yes_no(link->dhcp4_configured),
1187 yes_no(link->dhcp6_address_configured),
1188 yes_no(link->dhcp6_route_configured),
1189 yes_no(link->dhcp6_pd_address_configured),
1190 yes_no(link->dhcp6_pd_route_configured),
d98c546d
YW
1191 yes_no(link->ndisc_addresses_configured),
1192 yes_no(link->ndisc_routes_configured));
39373cb9 1193 }
463797c1 1194 }
6accfd31 1195
5f58af25 1196 link_enter_configured(link);
6accfd31
DA
1197
1198 return;
1199}
1200
e4a71bf3
WKI
1201static int link_request_set_neighbors(Link *link) {
1202 Neighbor *neighbor;
1203 int r;
1204
1205 assert(link);
1206 assert(link->network);
1207 assert(link->state != _LINK_STATE_INVALID);
1208
2428613f 1209 link->neighbors_configured = false;
e4a71bf3
WKI
1210
1211 LIST_FOREACH(neighbors, neighbor, link->network->neighbors) {
1212 r = neighbor_configure(neighbor, link, NULL);
4ff296b0
YW
1213 if (r < 0)
1214 return log_link_warning_errno(link, r, "Could not set neighbor: %m");
e4a71bf3
WKI
1215 }
1216
1217 if (link->neighbor_messages == 0) {
1218 link->neighbors_configured = true;
1219 link_check_ready(link);
b425c3ae 1220 } else {
e4a71bf3 1221 log_link_debug(link, "Setting neighbors");
b425c3ae
YW
1222 link_set_state(link, LINK_STATE_CONFIGURING);
1223 }
e4a71bf3
WKI
1224
1225 return 0;
1226}
1227
3650173f 1228static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
f882c247
TG
1229 int r;
1230
4958aee4 1231 assert(rtnl);
f5be5601
TG
1232 assert(m);
1233 assert(link);
1234 assert(link->ifname);
7715629e 1235 assert(link->address_messages > 0);
289e6774 1236 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
370e9930 1237 LINK_STATE_FAILED, LINK_STATE_LINGER));
f882c247 1238
7715629e 1239 link->address_messages--;
f882c247 1240
5da8149f 1241 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
f882c247
TG
1242 return 1;
1243
1c4baffc 1244 r = sd_netlink_message_get_errno(m);
4ff296b0 1245 if (r < 0 && r != -EEXIST) {
5ecb131d 1246 log_link_message_warning_errno(link, m, r, "Could not set address");
4ff296b0
YW
1247 link_enter_failed(link);
1248 return 1;
1249 } else if (r >= 0)
1250 (void) manager_rtnl_process_address(rtnl, m, link->manager);
f882c247 1251
7715629e 1252 if (link->address_messages == 0) {
6a7a4e4d 1253 log_link_debug(link, "Addresses set");
c42ff3a1 1254 link->addresses_configured = true;
6aa5773b 1255 link_check_ready(link);
dd3efc09 1256 }
f882c247
TG
1257
1258 return 1;
1259}
1260
f6bb7ac5
TJ
1261static int link_set_bridge_fdb(Link *link) {
1262 FdbEntry *fdb_entry;
197e2809 1263 int r;
f6bb7ac5
TJ
1264
1265 LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) {
1266 r = fdb_entry_configure(link, fdb_entry);
197e2809 1267 if (r < 0)
f6bb7ac5 1268 return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m");
f6bb7ac5
TJ
1269 }
1270
197e2809 1271 return 0;
f6bb7ac5
TJ
1272}
1273
289e6774 1274static int link_request_set_addresses(Link *link) {
95b74ef6 1275 AddressLabel *label;
a6cc569e 1276 Address *ad;
bd6379ec 1277 Prefix *p;
f882c247
TG
1278 int r;
1279
1280 assert(link);
1281 assert(link->network);
f5be5601 1282 assert(link->state != _LINK_STATE_INVALID);
f882c247 1283
2428613f
YW
1284 /* Reset all *_configured flags we are configuring. */
1285 link->addresses_configured = false;
6aa5773b 1286 link->addresses_ready = false;
2428613f
YW
1287 link->neighbors_configured = false;
1288 link->static_routes_configured = false;
c16c7808 1289 link->static_nexthops_configured = false;
2428613f
YW
1290 link->routing_policy_rules_configured = false;
1291
f6bb7ac5
TJ
1292 r = link_set_bridge_fdb(link);
1293 if (r < 0)
1294 return r;
1295
f3ef324d
YW
1296 r = link_request_set_neighbors(link);
1297 if (r < 0)
1298 return r;
e4a71bf3 1299
3d3d4255 1300 LIST_FOREACH(addresses, ad, link->network->static_addresses) {
a47a6dae
YW
1301 bool update;
1302
6f5d73ab
YW
1303 if (ad->family == AF_INET6 && !in_addr_is_null(ad->family, &ad->in_addr_peer))
1304 update = address_get(link, ad->family, &ad->in_addr_peer, ad->prefixlen, NULL) > 0;
1305 else
1306 update = address_get(link, ad->family, &ad->in_addr, ad->prefixlen, NULL) > 0;
a47a6dae 1307
80b0e860 1308 r = address_configure(ad, link, address_handler, update, NULL);
4ff296b0
YW
1309 if (r < 0)
1310 return log_link_warning_errno(link, r, "Could not set addresses: %m");
54a1a535
YW
1311 if (r > 0)
1312 link->address_messages++;
95b74ef6
SS
1313 }
1314
bd6379ec
SS
1315 if (IN_SET(link->network->router_prefix_delegation,
1316 RADV_PREFIX_DELEGATION_STATIC,
1317 RADV_PREFIX_DELEGATION_BOTH))
1318 LIST_FOREACH(prefixes, p, link->network->static_prefixes) {
1319 _cleanup_(address_freep) Address *address = NULL;
1320
1321 if (!p->assign)
1322 continue;
1323
1324 r = address_new(&address);
1325 if (r < 0)
1326 return log_link_error_errno(link, r, "Could not allocate address: %m");
1327
1328 r = sd_radv_prefix_get_prefix(p->radv_prefix, &address->in_addr.in6, &address->prefixlen);
1329 if (r < 0)
1330 return r;
1331
1332 r = generate_ipv6_eui_64_address(link, &address->in_addr.in6);
1333 if (r < 0)
1334 return r;
1335
1336 address->family = AF_INET6;
80b0e860 1337 r = address_configure(address, link, address_handler, true, NULL);
bd6379ec
SS
1338 if (r < 0)
1339 return log_link_warning_errno(link, r, "Could not set addresses: %m");
1340 if (r > 0)
1341 link->address_messages++;
1342 }
1343
95b74ef6 1344 LIST_FOREACH(labels, label, link->network->address_labels) {
cccf9517 1345 r = address_label_configure(label, link, NULL, false);
4ff296b0
YW
1346 if (r < 0)
1347 return log_link_warning_errno(link, r, "Could not set address label: %m");
f5be5601 1348
7715629e 1349 link->address_label_messages++;
f882c247
TG
1350 }
1351
d4cdbea5
TG
1352 /* now that we can figure out a default address for the dhcp server,
1353 start it */
708c425d 1354 if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) {
8fcf1d61 1355 r = dhcp4_server_configure(link);
4ff296b0 1356 if (r < 0)
d4cdbea5 1357 return r;
6a7a4e4d 1358 log_link_debug(link, "Offering DHCPv4 leases");
d4cdbea5
TG
1359 }
1360
c42ff3a1
WKI
1361 if (link->address_messages == 0) {
1362 link->addresses_configured = true;
6aa5773b 1363 link_check_ready(link);
b425c3ae 1364 } else {
6a7a4e4d 1365 log_link_debug(link, "Setting addresses");
b425c3ae
YW
1366 link_set_state(link, LINK_STATE_CONFIGURING);
1367 }
431ca2ce 1368
f882c247
TG
1369 return 0;
1370}
1371
13b498f9 1372static int link_set_bridge_vlan(Link *link) {
86e2be7b 1373 int r;
13b498f9
TJ
1374
1375 r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap);
1376 if (r < 0)
1377 log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m");
1378
1379 return r;
1380}
1381
a60a720c 1382static int link_set_proxy_arp(Link *link) {
23d8b221
SS
1383 int r;
1384
1385 if (!link_proxy_arp_enabled(link))
1386 return 0;
1387
62e021a9 1388 r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "proxy_arp", link->network->proxy_arp > 0);
23d8b221
SS
1389 if (r < 0)
1390 log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m");
1391
1392 return 0;
1393}
1394
3a390124 1395static int link_configure_continue(Link *link);
55dc8c4a 1396
302a796f 1397static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4f882b2a
TG
1398 int r;
1399
1400 assert(m);
1401 assert(link);
1402 assert(link->ifname);
1403
55dc8c4a
YW
1404 link->setting_mtu = false;
1405
5da8149f 1406 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4f882b2a
TG
1407 return 1;
1408
1c4baffc 1409 r = sd_netlink_message_get_errno(m);
4ff296b0 1410 if (r < 0)
5ecb131d 1411 log_link_message_warning_errno(link, m, r, "Could not set MTU, ignoring");
4ff296b0
YW
1412 else
1413 log_link_debug(link, "Setting MTU done.");
55dc8c4a 1414
4ff296b0 1415 if (link->state == LINK_STATE_INITIALIZED) {
3a390124 1416 r = link_configure_continue(link);
4ff296b0
YW
1417 if (r < 0)
1418 link_enter_failed(link);
1419 }
4f882b2a
TG
1420
1421 return 1;
1422}
1423
933c70a0 1424int link_set_mtu(Link *link, uint32_t mtu) {
4afd3348 1425 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
4f882b2a
TG
1426 int r;
1427
1428 assert(link);
1429 assert(link->manager);
1430 assert(link->manager->rtnl);
1431
40288ece
YW
1432 if (mtu == 0 || link->setting_mtu)
1433 return 0;
1434
933c70a0 1435 if (link->mtu == mtu)
ee493106
YW
1436 return 0;
1437
6a7a4e4d 1438 log_link_debug(link, "Setting MTU: %" PRIu32, mtu);
4f882b2a 1439
6a7a4e4d
LP
1440 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1441 if (r < 0)
1442 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
4f882b2a 1443
44b598a1 1444 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
72c2500b
YW
1445 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1446 if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) {
44b598a1
SS
1447
1448 log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as "
d236718c 1449 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes");
44b598a1 1450
72c2500b 1451 mtu = IPV6_MIN_MTU;
44b598a1
SS
1452 }
1453
1c4baffc 1454 r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu);
6a7a4e4d
LP
1455 if (r < 0)
1456 return log_link_error_errno(link, r, "Could not append MTU: %m");
4f882b2a 1457
302a796f
YW
1458 r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler,
1459 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1460 if (r < 0)
1461 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
4f882b2a 1462
ae941762 1463 link_ref(link);
1046bf9b 1464 link->setting_mtu = true;
b226d99b 1465
4f882b2a
TG
1466 return 0;
1467}
1468
f6fcc1c2
YW
1469static bool link_reduces_vlan_mtu(Link *link) {
1470 /* See netif_reduces_vlan_mtu() in kernel. */
1471 return streq_ptr(link->kind, "macsec");
1472}
1473
1474static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) {
1475 uint32_t mtu = 0;
1476 NetDev *dev;
1477 Iterator i;
1478
1479 HASHMAP_FOREACH(dev, link->network->stacked_netdevs, i)
1480 if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0)
1481 /* See vlan_dev_change_mtu() in kernel. */
1482 mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu);
1483
1484 else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu)
1485 /* See macvlan_change_mtu() in kernel. */
1486 mtu = dev->mtu;
1487
1488 return mtu;
1489}
1490
1491static int link_configure_mtu(Link *link) {
1492 uint32_t mtu;
1493
1494 assert(link);
1495 assert(link->network);
1496
1497 if (link->network->mtu > 0)
1498 return link_set_mtu(link, link->network->mtu);
1499
1500 mtu = link_get_requested_mtu_by_stacked_netdevs(link);
1501 if (link->mtu >= mtu)
1502 return 0;
1503
1504 log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. "
1505 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1506 link->mtu, mtu);
1507
1508 return link_set_mtu(link, mtu);
1509}
1510
302a796f 1511static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
99d2baa2
SS
1512 int r;
1513
1514 assert(m);
1515 assert(link);
1516 assert(link->ifname);
1517
1518 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1519 return 1;
1520
1521 r = sd_netlink_message_get_errno(m);
1522 if (r < 0)
5ecb131d 1523 log_link_message_warning_errno(link, m, r, "Could not set link flags, ignoring");
99d2baa2
SS
1524
1525 return 1;
1526}
1527
1528static int link_set_flags(Link *link) {
1529 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1530 unsigned ifi_change = 0;
1531 unsigned ifi_flags = 0;
1532 int r;
1533
1534 assert(link);
1535 assert(link->manager);
1536 assert(link->manager->rtnl);
1537
1538 if (link->flags & IFF_LOOPBACK)
1539 return 0;
1540
1541 if (!link->network)
1542 return 0;
1543
866e6b7a 1544 if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0)
99d2baa2
SS
1545 return 0;
1546
1547 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1548 if (r < 0)
1549 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1550
1551 if (link->network->arp >= 0) {
1552 ifi_change |= IFF_NOARP;
e6ebebbe
SS
1553 SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
1554 }
1555
1556 if (link->network->multicast >= 0) {
1557 ifi_change |= IFF_MULTICAST;
1558 SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
99d2baa2
SS
1559 }
1560
866e6b7a
SS
1561 if (link->network->allmulticast >= 0) {
1562 ifi_change |= IFF_ALLMULTI;
1563 SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
1564 }
1565
99d2baa2
SS
1566 r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
1567 if (r < 0)
1568 return log_link_error_errno(link, r, "Could not set link flags: %m");
1569
302a796f
YW
1570 r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler,
1571 link_netlink_destroy_callback, link);
99d2baa2
SS
1572 if (r < 0)
1573 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1574
1575 link_ref(link);
1576
1577 return 0;
1578}
1579
e7ab854c
TG
1580static int link_acquire_ipv6_conf(Link *link) {
1581 int r;
1582
1583 assert(link);
1584
e7ab854c 1585 if (link_ipv6_accept_ra_enabled(link)) {
1e7a0e21 1586 assert(link->ndisc);
e7ab854c
TG
1587
1588 log_link_debug(link, "Discovering IPv6 routers");
1589
1e7a0e21 1590 r = sd_ndisc_start(link->ndisc);
63348d13 1591 if (r < 0 && r != -EBUSY)
e7ab854c
TG
1592 return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m");
1593 }
1594
7465dd22
PF
1595 if (link_radv_enabled(link)) {
1596 assert(link->radv);
1597 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1598
1599 log_link_debug(link, "Starting IPv6 Router Advertisements");
1600
fd3ef936
YW
1601 r = radv_emit_dns(link);
1602 if (r < 0)
1603 return log_link_warning_errno(link, r, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1604
7465dd22
PF
1605 r = sd_radv_start(link->radv);
1606 if (r < 0 && r != -EBUSY)
1607 return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m");
1608 }
1609
838d39af
SS
1610 if (link_dhcp6_enabled(link) && IN_SET(link->network->dhcp6_without_ra,
1611 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST,
1612 DHCP6_CLIENT_START_MODE_SOLICIT)) {
cd305af1
SS
1613 assert(link->dhcp6_client);
1614 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1615
838d39af 1616 r = dhcp6_request_address(link, link->network->dhcp6_without_ra == DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST);
cd305af1
SS
1617 if (r < 0 && r != -EBUSY)
1618 return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m");
1619 else
1620 log_link_debug(link, "Acquiring DHCPv6 lease");
1621 }
1622
10752343
PF
1623 (void) dhcp6_request_prefix_delegation(link);
1624
e7ab854c
TG
1625 return 0;
1626}
1627
6fc25497 1628static int link_acquire_ipv4_conf(Link *link) {
ff254138
TG
1629 int r;
1630
1631 assert(link);
ff254138
TG
1632 assert(link->manager);
1633 assert(link->manager->event);
1634
910feb78 1635 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) {
eb34d4af 1636 assert(link->ipv4ll);
ff254138 1637
6a7a4e4d 1638 log_link_debug(link, "Acquiring IPv4 link-local address");
5c1d3fc9
UTL
1639
1640 r = sd_ipv4ll_start(link->ipv4ll);
6a7a4e4d
LP
1641 if (r < 0)
1642 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
5c1d3fc9
UTL
1643 }
1644
78c958f8 1645 if (link_dhcp4_enabled(link)) {
eb34d4af 1646 assert(link->dhcp_client);
ff254138 1647
6a7a4e4d 1648 log_link_debug(link, "Acquiring DHCPv4 lease");
ab47d620 1649
5c1d3fc9 1650 r = sd_dhcp_client_start(link->dhcp_client);
6a7a4e4d
LP
1651 if (r < 0)
1652 return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m");
5c1d3fc9 1653 }
ff254138 1654
6fc25497
SS
1655 return 0;
1656}
1657
1658static int link_acquire_conf(Link *link) {
1659 int r;
1660
1661 assert(link);
1662
1663 r = link_acquire_ipv4_conf(link);
1664 if (r < 0)
1665 return r;
1666
3f7cc080 1667 if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
6fc25497
SS
1668 r = link_acquire_ipv6_conf(link);
1669 if (r < 0)
1670 return r;
1671 }
1672
7272b25e
LP
1673 if (link_lldp_emit_enabled(link)) {
1674 r = link_lldp_emit_start(link);
8e1ad1ea
LP
1675 if (r < 0)
1676 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
1677 }
1678
ff254138
TG
1679 return 0;
1680}
1681
a61bb41c 1682bool link_has_carrier(Link *link) {
deb2e523
TG
1683 /* see Documentation/networking/operstates.txt in the kernel sources */
1684
a61bb41c 1685 if (link->kernel_operstate == IF_OPER_UP)
deb2e523
TG
1686 return true;
1687
a61bb41c 1688 if (link->kernel_operstate == IF_OPER_UNKNOWN)
deb2e523 1689 /* operstate may not be implemented, so fall back to flags */
b26ea308
YW
1690 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1691 !FLAGS_SET(link->flags, IFF_DORMANT))
deb2e523
TG
1692 return true;
1693
1694 return false;
1695}
1696
0e2fdb83
SS
1697static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1698 int r;
1699
1700 assert(link);
1701
9524014e
DS
1702 link->setting_genmode = false;
1703
0e2fdb83
SS
1704 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1705 return 1;
1706
1707 r = sd_netlink_message_get_errno(m);
1708 if (r < 0)
5ecb131d 1709 log_link_message_warning_errno(link, m, r, "Could not set address genmode for interface, ignoring");
9524014e
DS
1710 else
1711 log_link_debug(link, "Setting address genmode done.");
1712
1713 if (link->state == LINK_STATE_INITIALIZED) {
1714 r = link_configure_continue(link);
1715 if (r < 0)
1716 link_enter_failed(link);
1717 }
0e2fdb83
SS
1718
1719 return 1;
1720}
1721
1722static int link_configure_addrgen_mode(Link *link) {
1723 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1724 uint8_t ipv6ll_mode;
1725 int r;
1726
1727 assert(link);
1728 assert(link->network);
1729 assert(link->manager);
1730 assert(link->manager->rtnl);
1731
9524014e 1732 if (!socket_ipv6_is_supported() || link->setting_genmode)
9f6e82e6
YW
1733 return 0;
1734
0e2fdb83
SS
1735 log_link_debug(link, "Setting address genmode for link");
1736
1737 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1738 if (r < 0)
1739 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1740
1741 r = sd_netlink_message_open_container(req, IFLA_AF_SPEC);
1742 if (r < 0)
1743 return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m");
1744
1745 r = sd_netlink_message_open_container(req, AF_INET6);
1746 if (r < 0)
1747 return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m");
1748
1749 if (!link_ipv6ll_enabled(link))
1750 ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE;
6f6296b9 1751 else if (link->network->ipv6ll_address_gen_mode < 0) {
a6f1848a
SS
1752 r = sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL);
1753 if (r < 0) {
1754 /* The file may not exist. And even if it exists, when stable_secret is unset,
1755 * reading the file fails with EIO. */
1756 log_link_debug_errno(link, r, "Failed to read sysctl property stable_secret: %m");
1757
1758 ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64;
1759 } else
1760 ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
1761 } else
6f6296b9 1762 ipv6ll_mode = link->network->ipv6ll_address_gen_mode;
0e2fdb83
SS
1763
1764 r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode);
1765 if (r < 0)
1766 return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1767
1768 r = sd_netlink_message_close_container(req);
1769 if (r < 0)
1770 return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m");
1771
1772 r = sd_netlink_message_close_container(req);
1773 if (r < 0)
1774 return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m");
1775
1776 r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler,
1777 link_netlink_destroy_callback, link);
1778 if (r < 0)
1779 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1780
1781 link_ref(link);
9524014e 1782 link->setting_genmode = true;
0e2fdb83
SS
1783
1784 return 0;
1785}
1786
302a796f 1787static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
dd3efc09
TG
1788 int r;
1789
1746cf2a
TG
1790 assert(link);
1791
5da8149f 1792 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1746cf2a
TG
1793 return 1;
1794
1c4baffc 1795 r = sd_netlink_message_get_errno(m);
6a7a4e4d 1796 if (r < 0)
26d6b214 1797 /* we warn but don't fail the link, as it may be brought up later */
5ecb131d 1798 log_link_message_warning_errno(link, m, r, "Could not bring up interface");
45ad2c13 1799
f882c247
TG
1800 return 1;
1801}
1802
1cc84f3b 1803static int link_up(Link *link) {
4afd3348 1804 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
1805 int r;
1806
f882c247 1807 assert(link);
c106cc36 1808 assert(link->network);
f882c247
TG
1809 assert(link->manager);
1810 assert(link->manager->rtnl);
1811
6a7a4e4d 1812 log_link_debug(link, "Bringing link up");
449f7554 1813
6a7a4e4d
LP
1814 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1815 if (r < 0)
1816 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
f579559b 1817
2b2d8603 1818 /* set it free if not enslaved with networkd */
6cb955c6 1819 if (!link->network->bridge && !link->network->bond && !link->network->vrf) {
2b2d8603
TY
1820 r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0);
1821 if (r < 0)
1822 return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m");
1823 }
1824
5d4795f3 1825 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
6a7a4e4d
LP
1826 if (r < 0)
1827 return log_link_error_errno(link, r, "Could not set link flags: %m");
fc25d7f8 1828
c106cc36 1829 if (link->network->mac) {
1c4baffc 1830 r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
6a7a4e4d
LP
1831 if (r < 0)
1832 return log_link_error_errno(link, r, "Could not set MAC address: %m");
c106cc36
TG
1833 }
1834
302a796f
YW
1835 r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
1836 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1837 if (r < 0)
1838 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
f579559b 1839
b226d99b
TG
1840 link_ref(link);
1841
f882c247
TG
1842 return 0;
1843}
1844
302a796f 1845static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
0d4ad91d
AR
1846 int r;
1847
1848 assert(link);
1849
1850 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1851 return 1;
1852
1c4baffc 1853 r = sd_netlink_message_get_errno(m);
0d4ad91d 1854 if (r < 0)
5ecb131d 1855 log_link_message_warning_errno(link, m, r, "Could not bring down interface");
0d4ad91d
AR
1856
1857 return 1;
1858}
1859
8e54db83 1860int link_down(Link *link, link_netlink_message_handler_t callback) {
4afd3348 1861 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
0d4ad91d
AR
1862 int r;
1863
1864 assert(link);
1865 assert(link->manager);
1866 assert(link->manager->rtnl);
1867
6a7a4e4d 1868 log_link_debug(link, "Bringing link down");
0d4ad91d
AR
1869
1870 r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1871 RTM_SETLINK, link->ifindex);
6a7a4e4d
LP
1872 if (r < 0)
1873 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
0d4ad91d
AR
1874
1875 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
6a7a4e4d
LP
1876 if (r < 0)
1877 return log_link_error_errno(link, r, "Could not set link flags: %m");
0d4ad91d 1878
8e54db83
YW
1879 r = netlink_call_async(link->manager->rtnl, NULL, req,
1880 callback ?: link_down_handler,
302a796f 1881 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1882 if (r < 0)
1883 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
0d4ad91d
AR
1884
1885 link_ref(link);
1886
1887 return 0;
1888}
1889
89fe6535
SS
1890static int link_group_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1891 int r;
1892
1893 assert(link);
1894
1895 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1896 return 1;
1897
1898 r = sd_netlink_message_get_errno(m);
1899 if (r < 0)
1900 log_link_message_warning_errno(link, m, r, "Could not set group for the interface");
1901
1902 return 1;
1903}
1904
1905static int link_set_group(Link *link) {
1906 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1907 int r;
1908
1909 assert(link);
1910 assert(link->network);
1911 assert(link->manager);
1912 assert(link->manager->rtnl);
1913
1914 if (link->network->group <= 0)
1915 return 0;
1916
1917 log_link_debug(link, "Setting group");
1918
1919 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1920 if (r < 0)
1921 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1922
1923 r = sd_netlink_message_append_u32(req, IFLA_GROUP, link->network->group);
1924 if (r < 0)
1925 return log_link_error_errno(link, r, "Could not set link group: %m");
1926
1927 r = netlink_call_async(link->manager->rtnl, NULL, req, link_group_handler,
1928 link_netlink_destroy_callback, link);
1929 if (r < 0)
1930 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1931
1932 link_ref(link);
1933
1934 return 0;
1935}
1936
0d4ad91d
AR
1937static int link_handle_bound_to_list(Link *link) {
1938 Link *l;
1939 Iterator i;
1940 int r;
1941 bool required_up = false;
1942 bool link_is_up = false;
1943
1944 assert(link);
1945
1946 if (hashmap_isempty(link->bound_to_links))
1947 return 0;
1948
1949 if (link->flags & IFF_UP)
1950 link_is_up = true;
1951
1952 HASHMAP_FOREACH (l, link->bound_to_links, i)
1953 if (link_has_carrier(l)) {
1954 required_up = true;
1955 break;
1956 }
1957
1958 if (!required_up && link_is_up) {
8e54db83 1959 r = link_down(link, NULL);
0d4ad91d
AR
1960 if (r < 0)
1961 return r;
1962 } else if (required_up && !link_is_up) {
1963 r = link_up(link);
1964 if (r < 0)
1965 return r;
1966 }
1967
1968 return 0;
1969}
1970
1971static int link_handle_bound_by_list(Link *link) {
1972 Iterator i;
1973 Link *l;
1974 int r;
1975
1976 assert(link);
1977
1978 if (hashmap_isempty(link->bound_by_links))
1979 return 0;
1980
1981 HASHMAP_FOREACH (l, link->bound_by_links, i) {
1982 r = link_handle_bound_to_list(l);
1983 if (r < 0)
1984 return r;
1985 }
1986
1987 return 0;
1988}
1989
1990static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
1991 int r;
1992
1993 assert(link);
1994 assert(carrier);
1995
1996 if (link == carrier)
1997 return 0;
1998
1999 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
2000 return 0;
2001
2002 r = hashmap_ensure_allocated(h, NULL);
2003 if (r < 0)
2004 return r;
2005
2006 r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
2007 if (r < 0)
2008 return r;
2009
2010 return 0;
2011}
2012
2013static int link_new_bound_by_list(Link *link) {
2014 Manager *m;
2015 Link *carrier;
2016 Iterator i;
2017 int r;
2018 bool list_updated = false;
2019
2020 assert(link);
2021 assert(link->manager);
2022
2023 m = link->manager;
2024
b295beea 2025 HASHMAP_FOREACH(carrier, m->links, i) {
0d4ad91d
AR
2026 if (!carrier->network)
2027 continue;
2028
2029 if (strv_isempty(carrier->network->bind_carrier))
2030 continue;
2031
191a3f16 2032 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
0d4ad91d
AR
2033 r = link_put_carrier(link, carrier, &link->bound_by_links);
2034 if (r < 0)
2035 return r;
2036
2037 list_updated = true;
2038 }
2039 }
2040
2041 if (list_updated)
84de38c5 2042 link_dirty(link);
0d4ad91d 2043
b295beea 2044 HASHMAP_FOREACH(carrier, link->bound_by_links, i) {
0d4ad91d
AR
2045 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
2046 if (r < 0)
2047 return r;
2048
84de38c5 2049 link_dirty(carrier);
0d4ad91d
AR
2050 }
2051
2052 return 0;
2053}
2054
2055static int link_new_bound_to_list(Link *link) {
2056 Manager *m;
2057 Link *carrier;
2058 Iterator i;
2059 int r;
2060 bool list_updated = false;
2061
2062 assert(link);
2063 assert(link->manager);
2064
2065 if (!link->network)
2066 return 0;
2067
2068 if (strv_isempty(link->network->bind_carrier))
2069 return 0;
2070
2071 m = link->manager;
2072
2073 HASHMAP_FOREACH (carrier, m->links, i) {
191a3f16 2074 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
0d4ad91d
AR
2075 r = link_put_carrier(link, carrier, &link->bound_to_links);
2076 if (r < 0)
2077 return r;
2078
2079 list_updated = true;
2080 }
2081 }
2082
2083 if (list_updated)
84de38c5 2084 link_dirty(link);
0d4ad91d
AR
2085
2086 HASHMAP_FOREACH (carrier, link->bound_to_links, i) {
2087 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
2088 if (r < 0)
2089 return r;
2090
84de38c5 2091 link_dirty(carrier);
0d4ad91d
AR
2092 }
2093
2094 return 0;
2095}
2096
2097static int link_new_carrier_maps(Link *link) {
2098 int r;
2099
2100 r = link_new_bound_by_list(link);
2101 if (r < 0)
2102 return r;
2103
2104 r = link_handle_bound_by_list(link);
2105 if (r < 0)
2106 return r;
2107
2108 r = link_new_bound_to_list(link);
2109 if (r < 0)
2110 return r;
2111
2112 r = link_handle_bound_to_list(link);
2113 if (r < 0)
2114 return r;
2115
2116 return 0;
2117}
2118
2119static void link_free_bound_to_list(Link *link) {
2120 Link *bound_to;
2121 Iterator i;
2122
2123 HASHMAP_FOREACH (bound_to, link->bound_to_links, i) {
2124 hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex));
2125
2126 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
84de38c5 2127 link_dirty(bound_to);
0d4ad91d
AR
2128 }
2129
2130 return;
2131}
2132
2133static void link_free_bound_by_list(Link *link) {
2134 Link *bound_by;
2135 Iterator i;
2136
2137 HASHMAP_FOREACH (bound_by, link->bound_by_links, i) {
2138 hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex));
2139
2140 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
84de38c5 2141 link_dirty(bound_by);
0d4ad91d
AR
2142 link_handle_bound_to_list(bound_by);
2143 }
2144 }
2145
2146 return;
2147}
2148
2149static void link_free_carrier_maps(Link *link) {
2150 bool list_updated = false;
2151
2152 assert(link);
2153
2154 if (!hashmap_isempty(link->bound_to_links)) {
2155 link_free_bound_to_list(link);
2156 list_updated = true;
2157 }
2158
2159 if (!hashmap_isempty(link->bound_by_links)) {
2160 link_free_bound_by_list(link);
2161 list_updated = true;
2162 }
2163
2164 if (list_updated)
84de38c5 2165 link_dirty(link);
0d4ad91d
AR
2166
2167 return;
2168}
2169
5f707e12
YW
2170static int link_append_to_master(Link *link, NetDev *netdev) {
2171 Link *master;
2172 int r;
2173
2174 assert(link);
2175 assert(netdev);
2176
2177 r = link_get(link->manager, netdev->ifindex, &master);
2178 if (r < 0)
2179 return r;
2180
de7fef4b 2181 r = set_ensure_put(&master->slaves, NULL, link);
38288f0b 2182 if (r <= 0)
5f707e12
YW
2183 return r;
2184
2185 link_ref(link);
2186 return 0;
2187}
2188
2189static void link_drop_from_master(Link *link, NetDev *netdev) {
2190 Link *master;
2191
2192 assert(link);
2193
2194 if (!link->manager || !netdev)
2195 return;
2196
2197 if (link_get(link->manager, netdev->ifindex, &master) < 0)
2198 return;
2199
2200 link_unref(set_remove(master->slaves, link));
2201}
2202
2203static void link_detach_from_manager(Link *link) {
2204 if (!link || !link->manager)
2205 return;
2206
2207 link_unref(set_remove(link->manager->links_requesting_uuid, link));
2208 link_clean(link);
2209
2210 /* The following must be called at last. */
2211 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
2212 link_unref(link);
2213}
2214
0d4ad91d
AR
2215void link_drop(Link *link) {
2216 if (!link || link->state == LINK_STATE_LINGER)
2217 return;
2218
2219 link_set_state(link, LINK_STATE_LINGER);
2220
2221 link_free_carrier_maps(link);
2222
5f707e12
YW
2223 if (link->network) {
2224 link_drop_from_master(link, link->network->bridge);
2225 link_drop_from_master(link, link->network->bond);
2226 }
2227
6a7a4e4d 2228 log_link_debug(link, "Link removed");
0d4ad91d 2229
db2f8a2e 2230 (void) unlink(link->state_file);
c4397d94 2231 link_detach_from_manager(link);
0d4ad91d
AR
2232}
2233
3f265037 2234static int link_joined(Link *link) {
f882c247
TG
2235 int r;
2236
ef1ba606 2237 assert(link);
f5be5601 2238 assert(link->network);
dd3efc09 2239
0d4ad91d
AR
2240 if (!hashmap_isempty(link->bound_to_links)) {
2241 r = link_handle_bound_to_list(link);
2242 if (r < 0)
2243 return r;
2244 } else if (!(link->flags & IFF_UP)) {
505f8da7
TG
2245 r = link_up(link);
2246 if (r < 0) {
2247 link_enter_failed(link);
2248 return r;
2249 }
ef1ba606 2250 }
f882c247 2251
9ed794a3 2252 if (link->network->bridge) {
e1853b00 2253 r = link_set_bridge(link);
6a7a4e4d
LP
2254 if (r < 0)
2255 log_link_error_errno(link, r, "Could not set bridge message: %m");
45e11abf
YW
2256
2257 r = link_append_to_master(link, link->network->bridge);
2258 if (r < 0)
2259 log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m");
e1853b00
SS
2260 }
2261
eb64b435 2262 if (link->network->bond) {
7fcee284 2263 r = link_set_bond(link);
eb64b435
SS
2264 if (r < 0)
2265 log_link_error_errno(link, r, "Could not set bond message: %m");
959f65d3 2266
45e11abf 2267 r = link_append_to_master(link, link->network->bond);
959f65d3
YW
2268 if (r < 0)
2269 log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m");
eb64b435
SS
2270 }
2271
ffff9abe
TJ
2272 if (link->network->use_br_vlan &&
2273 (link->network->bridge || streq_ptr("bridge", link->kind))) {
13b498f9
TJ
2274 r = link_set_bridge_vlan(link);
2275 if (r < 0)
2276 log_link_error_errno(link, r, "Could not set bridge vlan: %m");
2277 }
2278
c1835a42
RM
2279 /* Skip setting up addresses until it gets carrier,
2280 or it would try to set addresses twice,
2281 which is bad for non-idempotent steps. */
dad2d78e 2282 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
c1835a42
RM
2283 return 0;
2284
b425c3ae 2285 link_set_state(link, LINK_STATE_CONFIGURING);
289e6774 2286 return link_request_set_addresses(link);
02b59d57
TG
2287}
2288
302a796f 2289static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
02b59d57
TG
2290 int r;
2291
1746cf2a 2292 assert(link);
ef1ba606 2293 assert(link->network);
34bf3c00 2294 assert(link->enslaving > 0);
02b59d57 2295
313cefa1 2296 link->enslaving--;
52433f6b 2297
5da8149f 2298 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
02b59d57
TG
2299 return 1;
2300
1c4baffc 2301 r = sd_netlink_message_get_errno(m);
856f962c 2302 if (r < 0 && r != -EEXIST) {
5ecb131d 2303 log_link_message_warning_errno(link, m, r, "Could not join netdev");
ef1ba606
TG
2304 link_enter_failed(link);
2305 return 1;
4ff296b0
YW
2306 }
2307
2308 log_link_debug(link, "Joined netdev");
02b59d57 2309
34bf3c00 2310 if (link->enslaving == 0) {
4ff296b0
YW
2311 r = link_joined(link);
2312 if (r < 0)
2313 link_enter_failed(link);
34bf3c00 2314 }
02b59d57
TG
2315
2316 return 1;
2317}
2318
3f265037 2319static int link_enter_join_netdev(Link *link) {
6a0a2f86 2320 NetDev *netdev;
672682a6 2321 Iterator i;
02b59d57
TG
2322 int r;
2323
2324 assert(link);
2325 assert(link->network);
bd08ce56 2326 assert(link->state == LINK_STATE_INITIALIZED);
02b59d57 2327
289e6774 2328 link_set_state(link, LINK_STATE_CONFIGURING);
02b59d57 2329
84de38c5 2330 link_dirty(link);
34bf3c00 2331 link->enslaving = 0;
02b59d57 2332
d9c67ea1 2333 if (link->network->bond) {
cbff7170
TJ
2334 if (link->network->bond->state == NETDEV_STATE_READY &&
2335 link->network->bond->ifindex == link->master_ifindex)
2336 return link_joined(link);
2337
f2341e0a
LP
2338 log_struct(LOG_DEBUG,
2339 LOG_LINK_INTERFACE(link),
2340 LOG_NETDEV_INTERFACE(link->network->bond),
a1230ff9 2341 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname));
f2341e0a 2342
34bf3c00
YW
2343 link->enslaving++;
2344
f2341e0a 2345 r = netdev_join(link->network->bond, link, netdev_join_handler);
52433f6b 2346 if (r < 0) {
f2341e0a
LP
2347 log_struct_errno(LOG_WARNING, r,
2348 LOG_LINK_INTERFACE(link),
2349 LOG_NETDEV_INTERFACE(link->network->bond),
a1230ff9 2350 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname));
52433f6b
TG
2351 link_enter_failed(link);
2352 return r;
2353 }
0ad6148e
MO
2354 }
2355
d9c67ea1 2356 if (link->network->bridge) {
f2341e0a
LP
2357 log_struct(LOG_DEBUG,
2358 LOG_LINK_INTERFACE(link),
2359 LOG_NETDEV_INTERFACE(link->network->bridge),
a1230ff9 2360 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname));
f2341e0a 2361
34bf3c00
YW
2362 link->enslaving++;
2363
f2341e0a 2364 r = netdev_join(link->network->bridge, link, netdev_join_handler);
0ad6148e 2365 if (r < 0) {
f2341e0a
LP
2366 log_struct_errno(LOG_WARNING, r,
2367 LOG_LINK_INTERFACE(link),
2368 LOG_NETDEV_INTERFACE(link->network->bridge),
a1230ff9 2369 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname));
0ad6148e
MO
2370 link_enter_failed(link);
2371 return r;
2372 }
52433f6b
TG
2373 }
2374
6cb955c6
AR
2375 if (link->network->vrf) {
2376 log_struct(LOG_DEBUG,
2377 LOG_LINK_INTERFACE(link),
2378 LOG_NETDEV_INTERFACE(link->network->vrf),
a1230ff9
ZJS
2379 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname));
2380
34bf3c00
YW
2381 link->enslaving++;
2382
6cb955c6
AR
2383 r = netdev_join(link->network->vrf, link, netdev_join_handler);
2384 if (r < 0) {
2385 log_struct_errno(LOG_WARNING, r,
2386 LOG_LINK_INTERFACE(link),
2387 LOG_NETDEV_INTERFACE(link->network->vrf),
a1230ff9 2388 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname));
6cb955c6
AR
2389 link_enter_failed(link);
2390 return r;
2391 }
6cb955c6
AR
2392 }
2393
6a0a2f86 2394 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
7951dea2 2395
2b6db913
YW
2396 if (netdev->ifindex > 0)
2397 /* Assume already enslaved. */
a63e5daa 2398 continue;
a63e5daa 2399
7033af49
YW
2400 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
2401 continue;
2402
f2341e0a
LP
2403 log_struct(LOG_DEBUG,
2404 LOG_LINK_INTERFACE(link),
2405 LOG_NETDEV_INTERFACE(netdev),
a1230ff9 2406 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
f2341e0a 2407
34bf3c00
YW
2408 link->enslaving++;
2409
f2341e0a 2410 r = netdev_join(netdev, link, netdev_join_handler);
7951dea2 2411 if (r < 0) {
f2341e0a
LP
2412 log_struct_errno(LOG_WARNING, r,
2413 LOG_LINK_INTERFACE(link),
2414 LOG_NETDEV_INTERFACE(netdev),
a1230ff9 2415 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
326cb406
SS
2416 link_enter_failed(link);
2417 return r;
2418 }
326cb406
SS
2419 }
2420
34bf3c00
YW
2421 if (link->enslaving == 0)
2422 return link_joined(link);
2423
ef1ba606
TG
2424 return 0;
2425}
2426
769d324c 2427static int link_set_ipv4_forward(Link *link) {
5a8bcb67
LP
2428 int r;
2429
765afd5c 2430 if (!link_ipv4_forward_enabled(link))
15dee3f0
LP
2431 return 0;
2432
765afd5c
LP
2433 /* We propagate the forwarding flag from one interface to the
2434 * global setting one way. This means: as long as at least one
2435 * interface was configured at any time that had IP forwarding
2436 * enabled the setting will stay on for good. We do this
2437 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2438 * somewhat in sync (see below). */
15dee3f0 2439
62e021a9 2440 r = sysctl_write_ip_property(AF_INET, NULL, "ip_forward", "1");
eb3da901 2441 if (r < 0)
765afd5c 2442 log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
43c6d5ab 2443
769d324c
LP
2444 return 0;
2445}
2446
2447static int link_set_ipv6_forward(Link *link) {
769d324c
LP
2448 int r;
2449
765afd5c 2450 if (!link_ipv6_forward_enabled(link))
8add5f79
NO
2451 return 0;
2452
61233823 2453 /* On Linux, the IPv6 stack does not know a per-interface
765afd5c
LP
2454 * packet forwarding setting: either packet forwarding is on
2455 * for all, or off for all. We hence don't bother with a
2456 * per-interface setting, but simply propagate the interface
2457 * flag, if it is set, to the global flag, one-way. Note that
2458 * while IPv4 would allow a per-interface flag, we expose the
2459 * same behaviour there and also propagate the setting from
2460 * one to all, to keep things simple (see above). */
15dee3f0 2461
62e021a9 2462 r = sysctl_write_ip_property(AF_INET6, "all", "forwarding", "1");
eb3da901 2463 if (r < 0)
765afd5c 2464 log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m");
5a8bcb67
LP
2465
2466 return 0;
2467}
2468
49092e22 2469static int link_set_ipv6_privacy_extensions(Link *link) {
1f0d9695 2470 IPv6PrivacyExtensions s;
49092e22
SS
2471 int r;
2472
1f0d9695 2473 s = link_ipv6_privacy_extensions(link);
66a6bd68 2474 if (s < 0)
49092e22
SS
2475 return 0;
2476
62e021a9 2477 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "use_tempaddr", (int) link->network->ipv6_privacy_extensions);
eb3da901 2478 if (r < 0)
49092e22
SS
2479 log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m");
2480
2481 return 0;
2482}
2483
4f2e437a 2484static int link_set_ipv6_accept_ra(Link *link) {
4f2e437a
SS
2485 int r;
2486
2487 /* Make this a NOP if IPv6 is not available */
2488 if (!socket_ipv6_is_supported())
2489 return 0;
2490
2491 if (link->flags & IFF_LOOPBACK)
2492 return 0;
2493
d68e2e59
LP
2494 if (!link->network)
2495 return 0;
2496
62e021a9 2497 r = sysctl_write_ip_property(AF_INET6, link->ifname, "accept_ra", "0");
eb3da901 2498 if (r < 0)
fe307276 2499 log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m");
4f2e437a
SS
2500
2501 return 0;
2502}
2503
8749cbcd 2504static int link_set_ipv6_dad_transmits(Link *link) {
8749cbcd
SS
2505 int r;
2506
2507 /* Make this a NOP if IPv6 is not available */
2508 if (!socket_ipv6_is_supported())
2509 return 0;
2510
2511 if (link->flags & IFF_LOOPBACK)
2512 return 0;
2513
d68e2e59
LP
2514 if (!link->network)
2515 return 0;
2516
8749cbcd
SS
2517 if (link->network->ipv6_dad_transmits < 0)
2518 return 0;
2519
62e021a9 2520 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "dad_transmits", link->network->ipv6_dad_transmits);
eb3da901 2521 if (r < 0)
8749cbcd 2522 log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m");
8749cbcd
SS
2523
2524 return 0;
2525}
2526
b69c3180 2527static int link_set_ipv6_hop_limit(Link *link) {
b69c3180
SS
2528 int r;
2529
2530 /* Make this a NOP if IPv6 is not available */
2531 if (!socket_ipv6_is_supported())
2532 return 0;
2533
2534 if (link->flags & IFF_LOOPBACK)
2535 return 0;
2536
d68e2e59
LP
2537 if (!link->network)
2538 return 0;
2539
b69c3180
SS
2540 if (link->network->ipv6_hop_limit < 0)
2541 return 0;
2542
62e021a9 2543 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "hop_limit", link->network->ipv6_hop_limit);
eb3da901 2544 if (r < 0)
b69c3180 2545 log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m");
b69c3180
SS
2546
2547 return 0;
2548}
2549
11102cba 2550static int link_set_ipv6_mtu(Link *link) {
11102cba
SS
2551 int r;
2552
2553 /* Make this a NOP if IPv6 is not available */
2554 if (!socket_ipv6_is_supported())
2555 return 0;
2556
2557 if (link->flags & IFF_LOOPBACK)
2558 return 0;
2559
2560 if (link->network->ipv6_mtu == 0)
2561 return 0;
2562
d236718c
DS
2563 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2564 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2565 if (link->network->ipv6_mtu < IPV6_MIN_MTU) {
2566 log_link_notice(link, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU)" byte minimum required");
2567 link->network->ipv6_mtu = IPV6_MIN_MTU;
2568 }
2569
62e021a9 2570 r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "mtu", link->network->ipv6_mtu);
d236718c
DS
2571 if (r < 0) {
2572 if (link->mtu < link->network->ipv6_mtu)
2573 log_link_warning(link, "Cannot set IPv6 MTU %"PRIu32" higher than device MTU %"PRIu32,
2574 link->network->ipv6_mtu, link->mtu);
2575 else
2576 log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m");
2577 }
2578
2579 link->ipv6_mtu_set = true;
11102cba
SS
2580
2581 return 0;
2582}
2583
94d76d07
YW
2584static int link_set_ipv4_accept_local(Link *link) {
2585 int r;
2586
2587 if (link->flags & IFF_LOOPBACK)
2588 return 0;
2589
2590 if (link->network->ipv4_accept_local < 0)
2591 return 0;
2592
2593 r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "accept_local", link->network->ipv4_accept_local);
2594 if (r < 0)
2595 log_link_warning_errno(link, r, "Cannot set IPv4 accept_local flag for interface: %m");
2596
2597 return 0;
2598}
2599
30226d27
TJ
2600static bool link_is_static_address_configured(Link *link, Address *address) {
2601 Address *net_address;
2602
2603 assert(link);
2604 assert(address);
2605
2606 if (!link->network)
2607 return false;
2608
2609 LIST_FOREACH(addresses, net_address, link->network->static_addresses)
2610 if (address_equal(net_address, address))
2611 return true;
6f5d73ab
YW
2612 else if (address->family == AF_INET6 && net_address->family == AF_INET6 &&
2613 in_addr_equal(AF_INET6, &address->in_addr, &net_address->in_addr_peer) > 0)
2614 return true;
30226d27
TJ
2615
2616 return false;
2617}
2618
d1bdafd2
WKI
2619static bool link_is_neighbor_configured(Link *link, Neighbor *neighbor) {
2620 Neighbor *net_neighbor;
2621
2622 assert(link);
2623 assert(neighbor);
2624
2625 if (!link->network)
2626 return false;
2627
2628 LIST_FOREACH(neighbors, net_neighbor, link->network->neighbors)
2629 if (neighbor_equal(net_neighbor, neighbor))
2630 return true;
2631
2632 return false;
2633}
2634
7ecf0c3e
TJ
2635static bool link_is_static_route_configured(Link *link, Route *route) {
2636 Route *net_route;
2637
2638 assert(link);
2639 assert(route);
2640
2641 if (!link->network)
2642 return false;
2643
2644 LIST_FOREACH(routes, net_route, link->network->static_routes)
2645 if (route_equal(net_route, route))
2646 return true;
2647
2648 return false;
2649}
2650
db51778f
YW
2651static bool link_address_is_dynamic(Link *link, Address *address) {
2652 Route *route;
2653 Iterator i;
2654
2655 assert(link);
2656 assert(address);
2657
2658 if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
2659 return true;
2660
2661 /* Even when the address is leased from a DHCP server, networkd assign the address
2662 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2663 * corresponding routes with RTPROT_DHCP. */
2664 SET_FOREACH(route, link->routes_foreign, i) {
2665 if (route->protocol != RTPROT_DHCP)
2666 continue;
2667
2668 if (address->family != route->family)
2669 continue;
2670
2671 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
2672 return true;
2673 }
2674
2675 return false;
2676}
2677
0917a271
DS
2678static int link_enumerate_ipv6_tentative_addresses(Link *link) {
2679 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2680 sd_netlink_message *addr;
2681 int r;
2682
2683 assert(link);
2684 assert(link->manager);
2685 assert(link->manager->rtnl);
2686
2687 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, 0, AF_INET6);
2688 if (r < 0)
2689 return r;
2690
2691 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
2692 if (r < 0)
2693 return r;
2694
2695 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
2696 unsigned char flags;
2697 int ifindex;
2698
2699 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
2700 if (r < 0) {
2701 log_link_warning_errno(link, r, "rtnl: invalid ifindex, ignoring: %m");
2702 continue;
2703 } else if (link->ifindex != ifindex)
2704 continue;
2705
2706 r = sd_rtnl_message_addr_get_flags(addr, &flags);
2707 if (r < 0) {
2708 log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m");
2709 continue;
2710 } else if (!(flags & IFA_F_TENTATIVE))
2711 continue;
2712
2713 log_link_debug(link, "Found tentative ipv6 link-local address");
2714 (void) manager_rtnl_process_address(link->manager->rtnl, addr, link->manager);
2715 }
2716
2717 return 0;
2718}
2719
5e5b137a
TG
2720static int link_drop_foreign_config(Link *link) {
2721 Address *address;
d1bdafd2 2722 Neighbor *neighbor;
5e5b137a
TG
2723 Route *route;
2724 Iterator i;
2725 int r;
2726
0917a271
DS
2727 /* The kernel doesn't notify us about tentative addresses;
2728 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2729 if (!link_ipv6ll_enabled(link)) {
2730 r = link_enumerate_ipv6_tentative_addresses(link);
2731 if (r < 0)
2732 return r;
2733 }
2734
5e5b137a 2735 SET_FOREACH(address, link->addresses_foreign, i) {
fe307276 2736 /* we consider IPv6LL addresses to be managed by the kernel */
5539fc0f 2737 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link))
5e5b137a
TG
2738 continue;
2739
db51778f 2740 if (link_address_is_dynamic(link, address)) {
b1b0b42e 2741 if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
db51778f 2742 continue;
b1b0b42e 2743 } else if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
db51778f
YW
2744 continue;
2745
30226d27
TJ
2746 if (link_is_static_address_configured(link, address)) {
2747 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
2748 if (r < 0)
2749 return log_link_error_errno(link, r, "Failed to add address: %m");
2750 } else {
63ae0569 2751 r = address_remove(address, link, NULL);
30226d27
TJ
2752 if (r < 0)
2753 return r;
2754 }
5e5b137a
TG
2755 }
2756
d1bdafd2
WKI
2757 SET_FOREACH(neighbor, link->neighbors_foreign, i) {
2758 if (link_is_neighbor_configured(link, neighbor)) {
2759 r = neighbor_add(link, neighbor->family, &neighbor->in_addr, &neighbor->lladdr, neighbor->lladdr_size, NULL);
2760 if (r < 0)
2761 return r;
2762 } else {
2763 r = neighbor_remove(neighbor, link, NULL);
2764 if (r < 0)
2765 return r;
2766 }
2767 }
2768
5e5b137a 2769 SET_FOREACH(route, link->routes_foreign, i) {
fe307276 2770 /* do not touch routes managed by the kernel */
5e5b137a
TG
2771 if (route->protocol == RTPROT_KERNEL)
2772 continue;
2773
bd7d6cec
YW
2774 /* do not touch multicast route added by kernel */
2775 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2776 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2777 if (route->protocol == RTPROT_BOOT &&
2778 route->family == AF_INET6 &&
2779 route->dst_prefixlen == 8 &&
2780 in_addr_equal(AF_INET6, &route->dst, &(union in_addr_union) { .in6 = {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} }))
2781 continue;
2782
b1b0b42e 2783 if (route->protocol == RTPROT_STATIC && link->network &&
db51778f
YW
2784 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
2785 continue;
2786
b1b0b42e 2787 if (route->protocol == RTPROT_DHCP && link->network &&
db51778f
YW
2788 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
2789 continue;
2790
7ecf0c3e 2791 if (link_is_static_route_configured(link, route)) {
f1368755 2792 r = route_add(link, route, NULL);
7ecf0c3e
TJ
2793 if (r < 0)
2794 return r;
2795 } else {
4645ad47 2796 r = route_remove(route, link, NULL);
7ecf0c3e
TJ
2797 if (r < 0)
2798 return r;
2799 }
5e5b137a
TG
2800 }
2801
2802 return 0;
2803}
2804
3104883d 2805static int link_drop_config(Link *link) {
410a7f15 2806 Address *address, *pool_address;
d1bdafd2 2807 Neighbor *neighbor;
3104883d
SS
2808 Route *route;
2809 Iterator i;
2810 int r;
2811
2812 SET_FOREACH(address, link->addresses, i) {
2813 /* we consider IPv6LL addresses to be managed by the kernel */
5539fc0f 2814 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link))
3104883d
SS
2815 continue;
2816
63ae0569 2817 r = address_remove(address, link, NULL);
3104883d
SS
2818 if (r < 0)
2819 return r;
410a7f15
RM
2820
2821 /* If this address came from an address pool, clean up the pool */
2822 LIST_FOREACH(addresses, pool_address, link->pool_addresses) {
2823 if (address_equal(address, pool_address)) {
2824 LIST_REMOVE(addresses, link->pool_addresses, pool_address);
2825 address_free(pool_address);
2826 break;
2827 }
2828 }
3104883d
SS
2829 }
2830
d1bdafd2
WKI
2831 SET_FOREACH(neighbor, link->neighbors, i) {
2832 r = neighbor_remove(neighbor, link, NULL);
2833 if (r < 0)
2834 return r;
2835 }
2836
3104883d
SS
2837 SET_FOREACH(route, link->routes, i) {
2838 /* do not touch routes managed by the kernel */
2839 if (route->protocol == RTPROT_KERNEL)
2840 continue;
2841
4645ad47 2842 r = route_remove(route, link, NULL);
3104883d
SS
2843 if (r < 0)
2844 return r;
2845 }
2846
c69305ff
LP
2847 ndisc_flush(link);
2848
3104883d
SS
2849 return 0;
2850}
2851
051e77ca
SS
2852static int link_configure_ipv4_dad(Link *link) {
2853 Address *address;
2854 int r;
2855
2856 assert(link);
2857 assert(link->network);
2858
2859 LIST_FOREACH(addresses, address, link->network->static_addresses)
2860 if (address->family == AF_INET &&
2861 FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4)) {
2862 r = configure_ipv4_duplicate_address_detection(link, address);
2863 if (r < 0)
2864 return log_link_error_errno(link, r, "Failed to configure IPv4ACD: %m");
2865 }
2866
2867 return 0;
2868}
2869
34658df2
YW
2870static int link_configure_traffic_control(Link *link) {
2871 TrafficControl *tc;
0f5bd7fe 2872 Iterator i;
02b59d57
TG
2873 int r;
2874
34658df2
YW
2875 link->tc_configured = false;
2876 link->tc_messages = 0;
4ecdcb07 2877
34658df2
YW
2878 ORDERED_HASHMAP_FOREACH(tc, link->network->tc_by_section, i) {
2879 r = traffic_control_configure(link, tc);
4ecdcb07
YW
2880 if (r < 0)
2881 return r;
2882 }
2883
34658df2
YW
2884 if (link->tc_messages == 0)
2885 link->tc_configured = true;
4ecdcb07 2886 else
34658df2 2887 log_link_debug(link, "Configuring traffic control");
4ecdcb07
YW
2888
2889 return 0;
2890}
2891
518cd6b5
SS
2892static int link_configure_sr_iov(Link *link) {
2893 SRIOV *sr_iov;
2894 Iterator i;
2895 int r;
2896
2897 link->sr_iov_configured = false;
2898 link->sr_iov_messages = 0;
2899
2900 ORDERED_HASHMAP_FOREACH(sr_iov, link->network->sr_iov_by_section, i) {
2901 r = sr_iov_configure(link, sr_iov);
2902 if (r < 0)
2903 return r;
2904 }
2905
2906 if (link->sr_iov_messages == 0)
2907 link->sr_iov_configured = true;
2908 else
2909 log_link_debug(link, "Configuring SR-IOV");
2910
2911 return 0;
2912}
2913
4ecdcb07
YW
2914static int link_configure(Link *link) {
2915 int r;
2916
ef1ba606 2917 assert(link);
b22d8a00 2918 assert(link->network);
bd08ce56 2919 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 2920
34658df2 2921 r = link_configure_traffic_control(link);
4ecdcb07
YW
2922 if (r < 0)
2923 return r;
2924
518cd6b5
SS
2925 r = link_configure_sr_iov(link);
2926 if (r < 0)
2927 return r;
2928
500b96eb 2929 if (link->iftype == ARPHRD_CAN)
93ea7750 2930 return link_configure_can(link);
92c918b0 2931
482efedc
SS
2932 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2933 * for this interface, then enable IPv6 */
57ad7607 2934 (void) link_update_ipv6_sysctl(link);
482efedc 2935
23d8b221
SS
2936 r = link_set_proxy_arp(link);
2937 if (r < 0)
2938 return r;
2939
a0e5c15d
FK
2940 r = ipv6_proxy_ndp_addresses_configure(link);
2941 if (r < 0)
2942 return r;
2943
769d324c
LP
2944 r = link_set_ipv4_forward(link);
2945 if (r < 0)
2946 return r;
2947
2948 r = link_set_ipv6_forward(link);
5a8bcb67
LP
2949 if (r < 0)
2950 return r;
2951
49092e22
SS
2952 r = link_set_ipv6_privacy_extensions(link);
2953 if (r < 0)
2954 return r;
2955
4f2e437a
SS
2956 r = link_set_ipv6_accept_ra(link);
2957 if (r < 0)
2958 return r;
2959
8749cbcd
SS
2960 r = link_set_ipv6_dad_transmits(link);
2961 if (r < 0)
2962 return r;
b69c3180
SS
2963
2964 r = link_set_ipv6_hop_limit(link);
2965 if (r < 0)
2966 return r;
8749cbcd 2967
94d76d07
YW
2968 r = link_set_ipv4_accept_local(link);
2969 if (r < 0)
2970 return r;
2971
99d2baa2
SS
2972 r = link_set_flags(link);
2973 if (r < 0)
2974 return r;
2975
89fe6535
SS
2976 r = link_set_group(link);
2977 if (r < 0)
2978 return r;
2979
910feb78 2980 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) {
b22d8a00 2981 r = ipv4ll_configure(link);
eb34d4af
TG
2982 if (r < 0)
2983 return r;
2984 }
2985
78c958f8 2986 if (link_dhcp4_enabled(link)) {
64b21ece
MV
2987 r = dhcp4_set_promote_secondaries(link);
2988 if (r < 0)
2989 return r;
2990
3c9b8860 2991 r = dhcp4_configure(link);
eb34d4af
TG
2992 if (r < 0)
2993 return r;
eb34d4af
TG
2994 }
2995
78c958f8 2996 if (link_dhcp4_server_enabled(link)) {
dd43110f
TG
2997 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
2998 if (r < 0)
2999 return r;
3000
3001 r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
3002 if (r < 0)
3003 return r;
dd43110f
TG
3004 }
3005
62379e88
TG
3006 if (link_dhcp6_enabled(link) ||
3007 link_ipv6_accept_ra_enabled(link)) {
f5a8c43f
TG
3008 r = dhcp6_configure(link);
3009 if (r < 0)
3010 return r;
3011 }
3012
3013 if (link_ipv6_accept_ra_enabled(link)) {
de1e9928 3014 r = ndisc_configure(link);
4138fb2c
PF
3015 if (r < 0)
3016 return r;
3017 }
3018
7465dd22
PF
3019 if (link_radv_enabled(link)) {
3020 r = radv_configure(link);
3021 if (r < 0)
3022 return r;
3023 }
3024
8e1ad1ea 3025 if (link_lldp_rx_enabled(link)) {
7f853950 3026 r = link_lldp_rx_configure(link);
273eec24
LP
3027 if (r < 0)
3028 return r;
ce43e484
SS
3029 }
3030
f6fcc1c2 3031 r = link_configure_mtu(link);
40288ece
YW
3032 if (r < 0)
3033 return r;
44b598a1 3034
9f6e82e6
YW
3035 r = link_configure_addrgen_mode(link);
3036 if (r < 0)
3037 return r;
0e2fdb83 3038
051e77ca
SS
3039 r = link_configure_ipv4_dad(link);
3040 if (r < 0)
3041 return r;
3042
3a390124
DS
3043 return link_configure_continue(link);
3044}
3045
3046/* The configuration continues in this separate function, instead of
3047 * including this in the above link_configure() function, for two
3048 * reasons:
3049 * 1) some devices reset the link when the mtu is set, which caused
3050 * an infinite loop here in networkd; see:
3051 * https://github.com/systemd/systemd/issues/6593
3052 * https://github.com/systemd/systemd/issues/9831
3053 * 2) if ipv6ll is disabled, then bringing the interface up must be
3054 * delayed until after we get confirmation from the kernel that
3055 * the addr_gen_mode parameter has been set (via netlink), see:
3056 * https://github.com/systemd/systemd/issues/13882
3057 */
3058static int link_configure_continue(Link *link) {
55dc8c4a
YW
3059 int r;
3060
3061 assert(link);
3062 assert(link->network);
bd08ce56 3063 assert(link->state == LINK_STATE_INITIALIZED);
55dc8c4a 3064
9524014e 3065 if (link->setting_mtu || link->setting_genmode)
55dc8c4a
YW
3066 return 0;
3067
4c649652
DS
3068 /* Drop foreign config, but ignore loopback or critical devices.
3069 * We do not want to remove loopback address or addresses used for root NFS. */
3070 if (!(link->flags & IFF_LOOPBACK) &&
3071 link->network->keep_configuration != KEEP_CONFIGURATION_YES) {
3072 r = link_drop_foreign_config(link);
3073 if (r < 0)
3074 return r;
3075 }
3076
d236718c
DS
3077 /* The kernel resets ipv6 mtu after changing device mtu;
3078 * we must set this here, after we've set device mtu */
3079 r = link_set_ipv6_mtu(link);
3080 if (r < 0)
3081 return r;
3082
dad2d78e 3083 if (link_has_carrier(link) || link->network->configure_without_carrier) {
1e9be60b
TG
3084 r = link_acquire_conf(link);
3085 if (r < 0)
3086 return r;
cc544d5f 3087 }
1e9be60b 3088
3f265037 3089 return link_enter_join_netdev(link);
505f8da7
TG
3090}
3091
27dfc982
YW
3092static int duid_set_uuid(DUID *duid, sd_id128_t uuid) {
3093 assert(duid);
3094
3095 if (duid->raw_data_len > 0)
3096 return 0;
3097
3098 if (duid->type != DUID_TYPE_UUID)
3099 return -EINVAL;
3100
3101 memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t));
3102 duid->raw_data_len = sizeof(sd_id128_t);
3103
3104 return 1;
3105}
3106
3107int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
3108 Manager *manager = userdata;
3109 const sd_bus_error *e;
3110 const void *a;
3111 size_t sz;
3112 DUID *duid;
3113 Link *link;
3114 int r;
3115
3116 assert(m);
3117 assert(manager);
3118
3119 e = sd_bus_message_get_error(m);
3120 if (e) {
3121 log_error_errno(sd_bus_error_get_errno(e),
ab4dd984 3122 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
27dfc982
YW
3123 e->message);
3124 goto configure;
3125 }
3126
3127 r = sd_bus_message_read_array(m, 'y', &a, &sz);
3128 if (r < 0)
3129 goto configure;
3130
3131 if (sz != sizeof(sd_id128_t)) {
ab4dd984 3132 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
27dfc982
YW
3133 goto configure;
3134 }
3135
3136 memcpy(&manager->product_uuid, a, sz);
3137 while ((duid = set_steal_first(manager->duids_requesting_uuid)))
3138 (void) duid_set_uuid(duid, manager->product_uuid);
3139
3140 manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid);
3141
3142configure:
3143 while ((link = set_steal_first(manager->links_requesting_uuid))) {
3dc2e7af
YW
3144 link_unref(link);
3145
27dfc982
YW
3146 r = link_configure(link);
3147 if (r < 0)
4ff296b0 3148 link_enter_failed(link);
27dfc982
YW
3149 }
3150
3151 manager->links_requesting_uuid = set_free(manager->links_requesting_uuid);
3152
3153 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3154 * even if the method fails. */
3155 manager->has_product_uuid = true;
3156
3157 return 1;
3158}
3159
3160static bool link_requires_uuid(Link *link) {
3161 const DUID *duid;
3162
3163 assert(link);
3164 assert(link->manager);
3165 assert(link->network);
3166
3167 duid = link_get_duid(link);
3168 if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0)
3169 return false;
3170
3171 if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY))
3172 return true;
3173
3174 if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link))
3175 return true;
3176
3177 return false;
3178}
3179
3180static int link_configure_duid(Link *link) {
3181 Manager *m;
3182 DUID *duid;
3183 int r;
3184
3185 assert(link);
3186 assert(link->manager);
3187 assert(link->network);
3188
3189 m = link->manager;
3190 duid = link_get_duid(link);
3191
3192 if (!link_requires_uuid(link))
3193 return 1;
3194
3195 if (m->has_product_uuid) {
3196 (void) duid_set_uuid(duid, m->product_uuid);
3197 return 1;
3198 }
3199
3200 if (!m->links_requesting_uuid) {
3201 r = manager_request_product_uuid(m, link);
3202 if (r < 0) {
3203 if (r == -ENOMEM)
3204 return r;
3205
ab4dd984
ZJS
3206 log_link_warning_errno(link, r,
3207 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
27dfc982
YW
3208 return 1;
3209 }
3210 } else {
3211 r = set_put(m->links_requesting_uuid, link);
3212 if (r < 0)
3213 return log_oom();
39dbd0c7
ZJS
3214 if (r > 0)
3215 link_ref(link);
27dfc982
YW
3216
3217 r = set_put(m->duids_requesting_uuid, duid);
3218 if (r < 0)
3219 return log_oom();
3220 }
3221
3222 return 0;
3223}
3224
572b21d9 3225static int link_reconfigure_internal(Link *link, sd_netlink_message *m, bool force) {
ad932b15
YW
3226 Network *network;
3227 int r;
3228
572b21d9
YW
3229 if (m) {
3230 _cleanup_strv_free_ char **s = NULL;
3231
3232 r = sd_netlink_message_get_errno(m);
3233 if (r < 0)
3234 return r;
3235
3236 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3237 if (r < 0 && r != -ENODATA)
3238 return r;
3239
3240 strv_free_and_replace(link->alternative_names, s);
3241 }
3242
c643bda5
YW
3243 r = network_get(link->manager, link->iftype, link->sd_device,
3244 link->ifname, link->alternative_names, link->driver,
3245 &link->mac, &link->permanent_mac,
3246 link->wlan_iftype, link->ssid, &link->bssid, &network);
ad932b15
YW
3247 if (r == -ENOENT) {
3248 link_enter_unmanaged(link);
3249 return 0;
3250 } else if (r == 0 && network->unmanaged) {
3251 link_enter_unmanaged(link);
3252 return 0;
3253 } else if (r < 0)
3254 return r;
3255
99b8517c 3256 if (link->network == network && !force)
ad932b15
YW
3257 return 0;
3258
3259 log_link_info(link, "Re-configuring with %s", network->filename);
3260
3261 /* Dropping old .network file */
3262 r = link_stop_clients(link, false);
572b21d9 3263 if (r < 0)
ad932b15 3264 return r;
ad932b15
YW
3265
3266 if (link_dhcp4_server_enabled(link))
3267 (void) sd_dhcp_server_stop(link->dhcp_server);
3268
3269 r = link_drop_config(link);
3270 if (r < 0)
3271 return r;
3272
2c7b826d 3273 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
ad932b15
YW
3274 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
3275 r = link_drop_foreign_config(link);
3276 if (r < 0)
3277 return r;
3278 }
3279
3280 link_free_carrier_maps(link);
3281 link_free_engines(link);
3282 link->network = network_unref(link->network);
3283
3284 /* Then, apply new .network file */
3285 r = network_apply(network, link);
3286 if (r < 0)
3287 return r;
3288
3289 r = link_new_carrier_maps(link);
3290 if (r < 0)
3291 return r;
3292
3293 link_set_state(link, LINK_STATE_INITIALIZED);
8ae7b8a1 3294 link_dirty(link);
ad932b15
YW
3295
3296 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3297 * link_configure() is called later asynchronously. */
3298 r = link_configure_duid(link);
3299 if (r <= 0)
3300 return r;
3301
3302 r = link_configure(link);
3303 if (r < 0)
3304 return r;
3305
3306 return 0;
3307}
3308
572b21d9
YW
3309static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3310 int r;
3311
3312 r = link_reconfigure_internal(link, m, false);
3313 if (r < 0)
3314 link_enter_failed(link);
3315
3316 return 1;
3317}
3318
3319static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
3320 int r;
3321
3322 r = link_reconfigure_internal(link, m, true);
3323 if (r < 0)
3324 link_enter_failed(link);
3325
3326 return 1;
3327}
3328
3329int link_reconfigure(Link *link, bool force) {
3330 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3331 int r;
3332
2c0d7ed3
YW
3333 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
3334 return 0;
3335
572b21d9
YW
3336 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
3337 link->ifindex);
3338 if (r < 0)
3339 return r;
3340
3341 r = netlink_call_async(link->manager->rtnl, NULL, req,
3342 force ? link_force_reconfigure_handler : link_reconfigure_handler,
3343 link_netlink_destroy_callback, link);
3344 if (r < 0)
3345 return r;
3346
3347 link_ref(link);
3348
3349 return 0;
3350}
3351
e6bf7774 3352static int link_initialized_and_synced(Link *link) {
505f8da7 3353 Network *network;
505f8da7
TG
3354 int r;
3355
3356 assert(link);
3357 assert(link->ifname);
3358 assert(link->manager);
3359
bd08ce56
YW
3360 /* We may get called either from the asynchronous netlink callback,
3361 * or directly for link_add() if running in a container. See link_add(). */
3362 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
4232cf04 3363 return 0;
505f8da7 3364
6a7a4e4d 3365 log_link_debug(link, "Link state is up-to-date");
bd08ce56 3366 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 3367
0d4ad91d
AR
3368 r = link_new_bound_by_list(link);
3369 if (r < 0)
3370 return r;
3371
3372 r = link_handle_bound_by_list(link);
3373 if (r < 0)
3374 return r;
3375
c4a03a56 3376 if (!link->network) {
0894cfe4 3377 r = wifi_get_info(link);
8d968fdd
YW
3378 if (r < 0)
3379 return r;
3380
c643bda5
YW
3381 r = network_get(link->manager, link->iftype, link->sd_device,
3382 link->ifname, link->alternative_names, link->driver,
3383 &link->mac, &link->permanent_mac,
3384 link->wlan_iftype, link->ssid, &link->bssid, &network);
c4a03a56
TG
3385 if (r == -ENOENT) {
3386 link_enter_unmanaged(link);
4232cf04 3387 return 0;
a09dc546
DM
3388 } else if (r == 0 && network->unmanaged) {
3389 link_enter_unmanaged(link);
3390 return 0;
c4a03a56
TG
3391 } else if (r < 0)
3392 return r;
505f8da7 3393
c4a03a56
TG
3394 if (link->flags & IFF_LOOPBACK) {
3395 if (network->link_local != ADDRESS_FAMILY_NO)
3396 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
78c958f8 3397
c4a03a56
TG
3398 if (network->dhcp != ADDRESS_FAMILY_NO)
3399 log_link_debug(link, "Ignoring DHCP clients for loopback link");
78c958f8 3400
c4a03a56
TG
3401 if (network->dhcp_server)
3402 log_link_debug(link, "Ignoring DHCP server for loopback link");
3403 }
bd2efe92 3404
7d342c03 3405 r = network_apply(network, link);
c4a03a56
TG
3406 if (r < 0)
3407 return r;
3408 }
505f8da7 3409
0d4ad91d
AR
3410 r = link_new_bound_to_list(link);
3411 if (r < 0)
3412 return r;
3413
27dfc982
YW
3414 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3415 * link_configure() is called later asynchronously. */
3416 r = link_configure_duid(link);
3417 if (r <= 0)
3418 return r;
3419
a748b692
TG
3420 r = link_configure(link);
3421 if (r < 0)
3422 return r;
3423
4232cf04 3424 return 0;
505f8da7
TG
3425}
3426
302a796f 3427static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
572b21d9 3428 _cleanup_strv_free_ char **s = NULL;
4ff296b0
YW
3429 int r;
3430
572b21d9
YW
3431 r = sd_netlink_message_get_errno(m);
3432 if (r < 0) {
40681e5c 3433 log_link_warning_errno(link, r, "Failed to wait for the interface to be initialized: %m");
572b21d9
YW
3434 link_enter_failed(link);
3435 return 0;
3436 }
3437
3438 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3439 if (r < 0 && r != -ENODATA) {
3440 link_enter_failed(link);
3441 return 0;
3442 }
3443
3444 strv_free_and_replace(link->alternative_names, s);
3445
4ff296b0
YW
3446 r = link_initialized_and_synced(link);
3447 if (r < 0)
3448 link_enter_failed(link);
e6bf7774
YW
3449 return 1;
3450}
3451
51517f9e 3452int link_initialized(Link *link, sd_device *device) {
4afd3348 3453 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
4f561e8e
TG
3454 int r;
3455
3456 assert(link);
3457 assert(link->manager);
3458 assert(link->manager->rtnl);
3459 assert(device);
3460
8434fd5c 3461 if (link->state != LINK_STATE_PENDING)
4f561e8e
TG
3462 return 0;
3463
51517f9e 3464 if (link->sd_device)
679b3605
TG
3465 return 0;
3466
79008bdd 3467 log_link_debug(link, "udev initialized link");
bd08ce56 3468 link_set_state(link, LINK_STATE_INITIALIZED);
4f561e8e 3469
51517f9e 3470 link->sd_device = sd_device_ref(device);
4f561e8e 3471
3c9b8860
TG
3472 /* udev has initialized the link, but we don't know if we have yet
3473 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3474 * when it returns we know that the pending NEWLINKs have already been
3475 * processed and that we are up-to-date */
4f561e8e 3476
3c9b8860
TG
3477 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
3478 link->ifindex);
4f561e8e
TG
3479 if (r < 0)
3480 return r;
3481
302a796f
YW
3482 r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler,
3483 link_netlink_destroy_callback, link);
4f561e8e
TG
3484 if (r < 0)
3485 return r;
3486
5da8149f
TG
3487 link_ref(link);
3488
4f561e8e
TG
3489 return 0;
3490}
3491
c4a03a56 3492static int link_load(Link *link) {
0bc70f1d
TG
3493 _cleanup_free_ char *network_file = NULL,
3494 *addresses = NULL,
f703cc2c 3495 *routes = NULL,
0bc70f1d
TG
3496 *dhcp4_address = NULL,
3497 *ipv4ll_address = NULL;
3498 union in_addr_union address;
c4a03a56
TG
3499 int r;
3500
3501 assert(link);
3502
aa8fbc74 3503 r = parse_env_file(NULL, link->state_file,
c4a03a56
TG
3504 "NETWORK_FILE", &network_file,
3505 "ADDRESSES", &addresses,
f703cc2c 3506 "ROUTES", &routes,
0bc70f1d 3507 "DHCP4_ADDRESS", &dhcp4_address,
13df9c39 3508 "IPV4LL_ADDRESS", &ipv4ll_address);
c4a03a56
TG
3509 if (r < 0 && r != -ENOENT)
3510 return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file);
3511
3512 if (network_file) {
3513 Network *network;
3514 char *suffix;
3515
3516 /* drop suffix */
3517 suffix = strrchr(network_file, '.');
3518 if (!suffix) {
3519 log_link_debug(link, "Failed to get network name from %s", network_file);
3520 goto network_file_fail;
3521 }
3522 *suffix = '\0';
3523
3524 r = network_get_by_name(link->manager, basename(network_file), &network);
3525 if (r < 0) {
3526 log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file));
3527 goto network_file_fail;
3528 }
3529
7d342c03 3530 r = network_apply(network, link);
c4a03a56
TG
3531 if (r < 0)
3532 return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file));
3533 }
3534
3535network_file_fail:
3536
1f68f772
ZJS
3537 for (const char *p = addresses; p; ) {
3538 _cleanup_free_ char *address_str = NULL;
3539 char *prefixlen_str;
3540 int family;
3541 unsigned char prefixlen;
c4a03a56 3542
1f68f772
ZJS
3543 r = extract_first_word(&p, &address_str, NULL, 0);
3544 if (r < 0)
3545 log_link_warning_errno(link, r, "failed to parse ADDRESSES: %m");
3546 if (r <= 0)
3547 break;
c4a03a56 3548
1f68f772
ZJS
3549 prefixlen_str = strchr(address_str, '/');
3550 if (!prefixlen_str) {
3551 log_link_debug(link, "Failed to parse address and prefix length %s", address_str);
3552 continue;
3553 }
3554 *prefixlen_str++ = '\0';
c4a03a56 3555
1f68f772
ZJS
3556 r = sscanf(prefixlen_str, "%hhu", &prefixlen);
3557 if (r != 1) {
3558 log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str);
3559 continue;
3560 }
c4a03a56 3561
1f68f772
ZJS
3562 r = in_addr_from_string_auto(address_str, &family, &address);
3563 if (r < 0) {
3564 log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str);
3565 continue;
c4a03a56 3566 }
c4a03a56 3567
1f68f772
ZJS
3568 r = address_add(link, family, &address, prefixlen, NULL);
3569 if (r < 0)
3570 return log_link_error_errno(link, r, "Failed to add address: %m");
3571 }
74544b4e 3572
1f68f772
ZJS
3573 for (const char *p = routes; p; ) {
3574 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
3575 _cleanup_(route_freep) Route *tmp = NULL;
3576 _cleanup_free_ char *route_str = NULL;
3577 char *prefixlen_str;
3578 Route *route;
f703cc2c 3579
1f68f772
ZJS
3580 r = extract_first_word(&p, &route_str, NULL, 0);
3581 if (r < 0)
3582 log_link_debug_errno(link, r, "failed to parse ROUTES: %m");
3583 if (r <= 0)
3584 break;
c598ac76 3585
1f68f772
ZJS
3586 prefixlen_str = strchr(route_str, '/');
3587 if (!prefixlen_str) {
3588 log_link_debug(link, "Failed to parse route %s", route_str);
3589 continue;
3590 }
3591 *prefixlen_str++ = '\0';
f703cc2c 3592
1f68f772
ZJS
3593 r = route_new(&tmp);
3594 if (r < 0)
3595 return log_oom();
f703cc2c 3596
1f68f772
ZJS
3597 r = sscanf(prefixlen_str,
3598 "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT,
3599 &tmp->dst_prefixlen,
3600 &tmp->tos,
3601 &tmp->priority,
3602 &tmp->table,
3603 &tmp->lifetime);
3604 if (r != 5) {
3605 log_link_debug(link,
3606 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3607 prefixlen_str);
3608 continue;
3609 }
f1368755 3610
1f68f772
ZJS
3611 r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst);
3612 if (r < 0) {
3613 log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str);
3614 continue;
3615 }
f703cc2c 3616
1f68f772
ZJS
3617 r = route_add(link, tmp, &route);
3618 if (r < 0)
3619 return log_link_error_errno(link, r, "Failed to add route: %m");
f703cc2c 3620
1f68f772
ZJS
3621 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
3622 r = sd_event_add_time(link->manager->event, &expire,
3623 clock_boottime_or_monotonic(),
3624 route->lifetime, 0, route_expire_handler, route);
f703cc2c 3625 if (r < 0)
1f68f772 3626 log_link_warning_errno(link, r, "Could not arm route expiration handler: %m");
f703cc2c 3627 }
1f68f772
ZJS
3628
3629 sd_event_source_unref(route->expire);
3630 route->expire = TAKE_PTR(expire);
f703cc2c
TG
3631 }
3632
0bc70f1d
TG
3633 if (dhcp4_address) {
3634 r = in_addr_from_string(AF_INET, dhcp4_address, &address);
3635 if (r < 0) {
b68d26b8 3636 log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address);
0bc70f1d
TG
3637 goto dhcp4_address_fail;
3638 }
3639
b1c626f6 3640 r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0);
0bc70f1d 3641 if (r < 0)
b68d26b8 3642 return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m");
0bc70f1d 3643
1b43e246
DS
3644 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
3645 if (r < 0)
3646 return log_link_error_errno(link, r, "Failed to attach DHCPv4 event: %m");
3647
0bc70f1d
TG
3648 r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in);
3649 if (r < 0)
b68d26b8 3650 return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address);
0bc70f1d
TG
3651 }
3652
3653dhcp4_address_fail:
3654
3655 if (ipv4ll_address) {
3656 r = in_addr_from_string(AF_INET, ipv4ll_address, &address);
3657 if (r < 0) {
b68d26b8 3658 log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address);
0bc70f1d
TG
3659 goto ipv4ll_address_fail;
3660 }
3661
3662 r = sd_ipv4ll_new(&link->ipv4ll);
3663 if (r < 0)
b68d26b8 3664 return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m");
0bc70f1d 3665
1b43e246
DS
3666 r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
3667 if (r < 0)
3668 return log_link_error_errno(link, r, "Failed to attach IPv4LL event: %m");
3669
0bc70f1d
TG
3670 r = sd_ipv4ll_set_address(link->ipv4ll, &address.in);
3671 if (r < 0)
b68d26b8 3672 return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address);
0bc70f1d
TG
3673 }
3674
3675ipv4ll_address_fail:
3676
c4a03a56
TG
3677 return 0;
3678}
3679
1c4baffc 3680int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
51517f9e 3681 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
505f8da7 3682 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
51517f9e 3683 Link *link;
5a937ea2 3684 int r;
505f8da7
TG
3685
3686 assert(m);
fbbeb65a 3687 assert(m->rtnl);
505f8da7
TG
3688 assert(message);
3689 assert(ret);
3690
3691 r = link_new(m, message, ret);
3692 if (r < 0)
3693 return r;
3694
3695 link = *ret;
3696
6a7a4e4d 3697 log_link_debug(link, "Link %d added", link->ifindex);
505f8da7 3698
c4a03a56
TG
3699 r = link_load(link);
3700 if (r < 0)
3701 return r;
3702
bf331d87
YW
3703 if (path_is_read_only_fs("/sys") <= 0) {
3704 /* udev should be around */
ae06ab10 3705 sprintf(ifindex_str, "n%d", link->ifindex);
51517f9e
YW
3706 r = sd_device_new_from_device_id(&device, ifindex_str);
3707 if (r < 0) {
4d59e14f
YW
3708 log_link_warning_errno(link, r, "Could not find device, waiting for device initialization: %m");
3709 return 0;
5c416fc4 3710 }
505f8da7 3711
5a937ea2 3712 r = sd_device_get_is_initialized(device);
51517f9e 3713 if (r < 0) {
bf331d87 3714 log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m");
51517f9e
YW
3715 goto failed;
3716 }
5a937ea2 3717 if (r == 0) {
505f8da7 3718 /* not yet ready */
79008bdd 3719 log_link_debug(link, "link pending udev initialization...");
505f8da7 3720 return 0;
3c4cb064 3721 }
505f8da7 3722
299ad32d
YW
3723 r = device_is_renaming(device);
3724 if (r < 0) {
bf331d87 3725 log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
299ad32d
YW
3726 goto failed;
3727 }
3728 if (r > 0) {
bf331d87 3729 log_link_debug(link, "Interface is being renamed, pending initialization.");
299ad32d
YW
3730 return 0;
3731 }
3732
4f561e8e
TG
3733 r = link_initialized(link, device);
3734 if (r < 0)
5c416fc4 3735 goto failed;
4f561e8e 3736 } else {
e6bf7774 3737 r = link_initialized_and_synced(link);
4f561e8e 3738 if (r < 0)
5c416fc4 3739 goto failed;
4f561e8e 3740 }
505f8da7 3741
a748b692 3742 return 0;
5c416fc4
TG
3743failed:
3744 link_enter_failed(link);
3745 return r;
a748b692
TG
3746}
3747
c601ebf7 3748int link_ipv6ll_gained(Link *link, const struct in6_addr *address) {
e7ab854c
TG
3749 int r;
3750
3751 assert(link);
3752
3753 log_link_info(link, "Gained IPv6LL");
3754
c601ebf7 3755 link->ipv6ll_address = *address;
e7ab854c
TG
3756 link_check_ready(link);
3757
b9ea3d2e 3758 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
e7ab854c
TG
3759 r = link_acquire_ipv6_conf(link);
3760 if (r < 0) {
3761 link_enter_failed(link);
3762 return r;
3763 }
3764 }
3765
3766 return 0;
3767}
3768
9c0a72f9
TG
3769static int link_carrier_gained(Link *link) {
3770 int r;
3771
3772 assert(link);
3773
0894cfe4 3774 r = wifi_get_info(link);
8d968fdd
YW
3775 if (r < 0)
3776 return r;
3777 if (r > 0) {
572b21d9
YW
3778 r = link_reconfigure_internal(link, NULL, false);
3779 if (r < 0) {
3780 link_enter_failed(link);
8d968fdd 3781 return r;
572b21d9 3782 }
8d968fdd
YW
3783 }
3784
b9ea3d2e 3785 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
9c0a72f9
TG
3786 r = link_acquire_conf(link);
3787 if (r < 0) {
3788 link_enter_failed(link);
3789 return r;
3790 }
6fc25497 3791
b425c3ae 3792 link_set_state(link, LINK_STATE_CONFIGURING);
289e6774 3793 r = link_request_set_addresses(link);
6fc25497
SS
3794 if (r < 0)
3795 return r;
9c0a72f9
TG
3796 }
3797
0d4ad91d
AR
3798 r = link_handle_bound_by_list(link);
3799 if (r < 0)
3800 return r;
3801
9c0a72f9
TG
3802 return 0;
3803}
3804
3805static int link_carrier_lost(Link *link) {
3806 int r;
3807
3808 assert(link);
3809
25e992ba 3810 if (link->network && link->network->ignore_carrier_loss)
93b4dab5
SS
3811 return 0;
3812
fbdb6605 3813 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
55dc8c4a
YW
3814 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3815 * configuration and stop the clients as well. */
97e7fb39
SS
3816 if (link->setting_mtu)
3817 return 0;
3818
95355a28 3819 r = link_stop_clients(link, false);
9c0a72f9
TG
3820 if (r < 0) {
3821 link_enter_failed(link);
3822 return r;
3823 }
3824
28464ae0
SS
3825 if (link_dhcp4_server_enabled(link))
3826 (void) sd_dhcp_server_stop(link->dhcp_server);
45a9eac9 3827
3104883d
SS
3828 r = link_drop_config(link);
3829 if (r < 0)
3830 return r;
3831
2c7b826d 3832 if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) {
f258e948 3833 log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state));
c436d553
MM
3834 r = link_drop_foreign_config(link);
3835 if (r < 0)
3836 return r;
3837 }
3104883d 3838
0d4ad91d
AR
3839 r = link_handle_bound_by_list(link);
3840 if (r < 0)
3841 return r;
3842
9c0a72f9
TG
3843 return 0;
3844}
3845
3846int link_carrier_reset(Link *link) {
3847 int r;
3848
3849 assert(link);
3850
3851 if (link_has_carrier(link)) {
3852 r = link_carrier_lost(link);
3853 if (r < 0)
3854 return r;
3855
3856 r = link_carrier_gained(link);
3857 if (r < 0)
3858 return r;
3859
6a7a4e4d 3860 log_link_info(link, "Reset carrier");
9c0a72f9
TG
3861 }
3862
3863 return 0;
3864}
3865
d236718c
DS
3866/* This is called every time an interface admin state changes to up;
3867 * specifically, when IFF_UP flag changes from unset to set */
3868static int link_admin_state_up(Link *link) {
3869 int r;
3870
3871 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3872 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3873 * ipv6_mtu_set prevents this from trying to set it too early before
3874 * the link->network has been setup; we only need to reset it
3875 * here if we've already set it during normal initialization. */
3876 if (link->ipv6_mtu_set) {
3877 r = link_set_ipv6_mtu(link);
3878 if (r < 0)
3879 return r;
3880 }
3881
3882 return 0;
3883}
3884
1c4baffc 3885int link_update(Link *link, sd_netlink_message *m) {
572b21d9 3886 _cleanup_strv_free_ char **s = NULL;
c49b33ac 3887 struct ether_addr mac;
ca4e095a 3888 const char *ifname;
afe7fd56 3889 uint32_t mtu;
d236718c 3890 bool had_carrier, carrier_gained, carrier_lost, link_was_admin_up;
bb262ef0 3891 int old_master, r;
22936833 3892
dd3efc09 3893 assert(link);
b8941f74 3894 assert(link->ifname);
22936833
TG
3895 assert(m);
3896
7619683b 3897 if (link->state == LINK_STATE_LINGER) {
5238e957 3898 log_link_info(link, "Link re-added");
289e6774 3899 link_set_state(link, LINK_STATE_CONFIGURING);
0d4ad91d
AR
3900
3901 r = link_new_carrier_maps(link);
3902 if (r < 0)
3903 return r;
7619683b
TG
3904 }
3905
1c4baffc 3906 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
b8941f74 3907 if (r >= 0 && !streq(ifname, link->ifname)) {
30de2b89 3908 Manager *manager = link->manager;
b8941f74 3909
30de2b89 3910 log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname);
0d4ad91d 3911
30de2b89
YW
3912 link_drop(link);
3913 r = link_add(manager, m, &link);
3914 if (r < 0)
3915 return r;
b8941f74
TG
3916 }
3917
572b21d9
YW
3918 r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s);
3919 if (r >= 0)
3920 strv_free_and_replace(link->alternative_names, s);
3921
1c4baffc 3922 r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu);
afe7fd56
TG
3923 if (r >= 0 && mtu > 0) {
3924 link->mtu = mtu;
4e964aa0 3925 if (link->original_mtu == 0) {
afe7fd56 3926 link->original_mtu = mtu;
6a7a4e4d 3927 log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu);
afe7fd56
TG
3928 }
3929
3930 if (link->dhcp_client) {
3c9b8860
TG
3931 r = sd_dhcp_client_set_mtu(link->dhcp_client,
3932 link->mtu);
fc95c359
YW
3933 if (r < 0)
3934 return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m");
afe7fd56 3935 }
7465dd22
PF
3936
3937 if (link->radv) {
3938 r = sd_radv_set_mtu(link->radv, link->mtu);
3939 if (r < 0)
3940 return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m");
3941 }
9842de0d 3942 }
69629de9 3943
e9189a1f
TG
3944 /* The kernel may broadcast NEWLINK messages without the MAC address
3945 set, simply ignore them. */
1c4baffc 3946 r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
807667f7
YW
3947 if (r >= 0 && memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN) != 0) {
3948
3949 memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
3950
3951 log_link_debug(link, "Gained new MAC address: "
3952 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3953 mac.ether_addr_octet[0],
3954 mac.ether_addr_octet[1],
3955 mac.ether_addr_octet[2],
3956 mac.ether_addr_octet[3],
3957 mac.ether_addr_octet[4],
3958 mac.ether_addr_octet[5]);
3959
3960 if (link->ipv4ll) {
3961 bool restart = sd_ipv4ll_is_running(link->ipv4ll) > 0;
3962
3963 if (restart) {
3964 r = sd_ipv4ll_stop(link->ipv4ll);
6a7a4e4d 3965 if (r < 0)
807667f7 3966 return log_link_warning_errno(link, r, "Could not stop IPv4LL client: %m");
c49b33ac 3967 }
c49b33ac 3968
807667f7
YW
3969 r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
3970 if (r < 0)
3971 return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
3972
3973 if (restart) {
3974 r = sd_ipv4ll_start(link->ipv4ll);
6a7a4e4d 3975 if (r < 0)
807667f7
YW
3976 return log_link_warning_errno(link, r, "Could not restart IPv4LL client: %m");
3977 }
3978 }
3979
3980 if (link->dhcp_client) {
3981 r = sd_dhcp_client_set_mac(link->dhcp_client,
3982 (const uint8_t *) &link->mac,
3983 sizeof (link->mac),
3984 ARPHRD_ETHER);
3985 if (r < 0)
3986 return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m");
3987
3988 r = dhcp4_set_client_identifier(link);
3989 if (r < 0)
3990 return log_link_warning_errno(link, r, "Could not set DHCP client identifier: %m");
3991 }
413708d1 3992
807667f7
YW
3993 if (link->dhcp6_client) {
3994 const DUID* duid = link_get_duid(link);
3995 bool restart = sd_dhcp6_client_is_running(link->dhcp6_client) > 0;
3996
3997 if (restart) {
3998 r = sd_dhcp6_client_stop(link->dhcp6_client);
fff1f40c 3999 if (r < 0)
807667f7 4000 return log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m");
c49b33ac 4001 }
4138fb2c 4002
807667f7
YW
4003 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
4004 (const uint8_t *) &link->mac,
4005 sizeof (link->mac),
4006 ARPHRD_ETHER);
4007 if (r < 0)
4008 return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
8341a5c3 4009
807667f7
YW
4010 if (link->network->iaid_set) {
4011 r = sd_dhcp6_client_set_iaid(link->dhcp6_client, link->network->iaid);
6a7a4e4d 4012 if (r < 0)
807667f7
YW
4013 return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m");
4014 }
4015
4016 r = sd_dhcp6_client_set_duid(link->dhcp6_client,
4017 duid->type,
4018 duid->raw_data_len > 0 ? duid->raw_data : NULL,
4019 duid->raw_data_len);
4020 if (r < 0)
4021 return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m");
4022
4023 if (restart) {
4024 r = sd_dhcp6_client_start(link->dhcp6_client);
413708d1 4025 if (r < 0)
807667f7 4026 return log_link_warning_errno(link, r, "Could not restart DHCPv6 client: %m");
4138fb2c 4027 }
807667f7 4028 }
7465dd22 4029
807667f7
YW
4030 if (link->radv) {
4031 bool restart = sd_radv_is_running(link->radv);
4032
4033 if (restart) {
4034 r = sd_radv_stop(link->radv);
7465dd22 4035 if (r < 0)
807667f7 4036 return log_link_warning_errno(link, r, "Could not stop Router Advertisement: %m");
7465dd22 4037 }
420d2058 4038
807667f7
YW
4039 r = sd_radv_set_mac(link->radv, &link->mac);
4040 if (r < 0)
4041 return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m");
4042
4043 if (restart) {
4044 r = sd_radv_start(link->radv);
420d2058 4045 if (r < 0)
807667f7 4046 return log_link_warning_errno(link, r, "Could not restart Router Advertisement: %m");
420d2058 4047 }
c49b33ac 4048 }
807667f7
YW
4049
4050 if (link->ndisc) {
4051 r = sd_ndisc_set_mac(link->ndisc, &link->mac);
4052 if (r < 0)
4053 return log_link_warning_errno(link, r, "Could not update MAC for NDisc: %m");
4054 }
4f882b2a
TG
4055 }
4056
bb262ef0
YW
4057 old_master = link->master_ifindex;
4058 (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex);
4059
d236718c 4060 link_was_admin_up = link->flags & IFF_UP;
a61bb41c
TG
4061 had_carrier = link_has_carrier(link);
4062
bb262ef0 4063 r = link_update_flags(link, m, old_master != link->master_ifindex);
a61bb41c
TG
4064 if (r < 0)
4065 return r;
4066
d236718c
DS
4067 if (!link_was_admin_up && (link->flags & IFF_UP)) {
4068 log_link_info(link, "Link UP");
4069
4070 r = link_admin_state_up(link);
4071 if (r < 0)
4072 return r;
4073 } else if (link_was_admin_up && !(link->flags & IFF_UP))
4074 log_link_info(link, "Link DOWN");
4075
273eec24
LP
4076 r = link_update_lldp(link);
4077 if (r < 0)
4078 return r;
4079
a61bb41c
TG
4080 carrier_gained = !had_carrier && link_has_carrier(link);
4081 carrier_lost = had_carrier && !link_has_carrier(link);
4082
4083 if (carrier_gained) {
6a7a4e4d 4084 log_link_info(link, "Gained carrier");
a61bb41c 4085
9c0a72f9
TG
4086 r = link_carrier_gained(link);
4087 if (r < 0)
4088 return r;
a61bb41c 4089 } else if (carrier_lost) {
6a7a4e4d 4090 log_link_info(link, "Lost carrier");
a61bb41c 4091
9c0a72f9
TG
4092 r = link_carrier_lost(link);
4093 if (r < 0)
a61bb41c 4094 return r;
a61bb41c
TG
4095 }
4096
4097 return 0;
dd3efc09 4098}
fe8db0c5 4099
b295beea
LP
4100static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) {
4101 bool space = false;
4102 Iterator i;
4103 Link *link;
4104
4105 assert(f);
4106 assert(prefix);
4107
4108 if (hashmap_isempty(h))
4109 return;
4110
0d536673 4111 fputs(prefix, f);
b295beea
LP
4112 HASHMAP_FOREACH(link, h, i) {
4113 if (space)
0d536673 4114 fputc(' ', f);
b295beea
LP
4115
4116 fprintf(f, "%i", link->ifindex);
4117 space = true;
4118 }
4119
0d536673 4120 fputc('\n', f);
b295beea
LP
4121}
4122
64581765 4123static void link_save_dns(Link *link, FILE *f, struct in_addr_full **dns, unsigned n_dns, bool *space) {
dddc8d1e 4124 for (unsigned j = 0; j < n_dns; j++) {
e77bd3fd 4125 const char *str;
15761549 4126
64581765
YW
4127 if (dns[j]->ifindex != 0 && dns[j]->ifindex != link->ifindex)
4128 continue;
4129
e77bd3fd
YW
4130 str = in_addr_full_to_string(dns[j]);
4131 if (!str)
15761549 4132 continue;
15761549
YW
4133
4134 if (*space)
4135 fputc(' ', f);
e77bd3fd 4136 fputs(str, f);
15761549
YW
4137 *space = true;
4138 }
4139}
4140
d5e172d2
ZJS
4141static void serialize_addresses(
4142 FILE *f,
4143 const char *lvalue,
4144 bool *space,
4145 char **addresses,
4146 sd_dhcp_lease *lease,
4147 bool conditional,
ddb82ec2 4148 sd_dhcp_lease_server_type what,
d5e172d2
ZJS
4149 sd_dhcp6_lease *lease6,
4150 bool conditional6,
4151 int (*lease6_get_addr)(sd_dhcp6_lease*, const struct in6_addr**),
4152 int (*lease6_get_fqdn)(sd_dhcp6_lease*, char ***)) {
4153 int r;
4154
4155 bool _space = false;
4156 if (!space)
4157 space = &_space;
4158
4159 if (lvalue)
4160 fprintf(f, "%s=", lvalue);
4161 fputstrv(f, addresses, NULL, space);
4162
4163 if (lease && conditional) {
4164 const struct in_addr *lease_addresses;
4165
4166 r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses);
4167 if (r > 0)
d8bff5cc 4168 serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local);
d5e172d2
ZJS
4169 }
4170
4171 if (lease6 && conditional6 && lease6_get_addr) {
4172 const struct in6_addr *in6_addrs;
4173
4174 r = lease6_get_addr(lease6, &in6_addrs);
d8bff5cc
ZJS
4175 if (r > 0)
4176 serialize_in6_addrs(f, in6_addrs, r, space);
d5e172d2
ZJS
4177 }
4178
4179 if (lease6 && conditional6 && lease6_get_fqdn) {
4180 char **in6_hosts;
4181
4182 r = lease6_get_fqdn(lease6, &in6_hosts);
4183 if (r > 0)
4184 fputstrv(f, in6_hosts, NULL, space);
4185 }
4186
4187 if (lvalue)
4188 fputc('\n', f);
4189}
4190
fe8db0c5 4191int link_save(Link *link) {
d361b373 4192 const char *admin_state, *oper_state, *carrier_state, *address_state;
68a8723c 4193 _cleanup_free_ char *temp_path = NULL;
fe8db0c5 4194 _cleanup_fclose_ FILE *f = NULL;
c1eb9872 4195 Route *route;
18d8a2cf 4196 Address *a;
e7780c8d 4197 Iterator i;
fe8db0c5
TG
4198 int r;
4199
4200 assert(link);
4201 assert(link->state_file);
68a8723c 4202 assert(link->lease_file);
bbf7c048
TG
4203 assert(link->manager);
4204
370e9930 4205 if (link->state == LINK_STATE_LINGER) {
6990fb6b 4206 (void) unlink(link->state_file);
370e9930
TG
4207 return 0;
4208 }
4209
34437b4f
LP
4210 link_lldp_save(link);
4211
deb2e523
TG
4212 admin_state = link_state_to_string(link->state);
4213 assert(admin_state);
4214
e375dcde
TG
4215 oper_state = link_operstate_to_string(link->operstate);
4216 assert(oper_state);
deb2e523 4217
ac999bf0
YW
4218 carrier_state = link_carrier_state_to_string(link->carrier_state);
4219 assert(carrier_state);
4220
4221 address_state = link_address_state_to_string(link->address_state);
4222 assert(address_state);
4223
fe8db0c5
TG
4224 r = fopen_temporary(link->state_file, &f, &temp_path);
4225 if (r < 0)
6a7a4e4d 4226 goto fail;
fe8db0c5 4227
5512a963 4228 (void) fchmod(fileno(f), 0644);
fe8db0c5
TG
4229
4230 fprintf(f,
4231 "# This is private data. Do not parse.\n"
deb2e523 4232 "ADMIN_STATE=%s\n"
ac999bf0
YW
4233 "OPER_STATE=%s\n"
4234 "CARRIER_STATE=%s\n"
4235 "ADDRESS_STATE=%s\n",
4236 admin_state, oper_state, carrier_state, address_state);
fe8db0c5 4237
bcb7a07e 4238 if (link->network) {
fe0e16db
YW
4239 char **dhcp6_domains = NULL, **dhcp_domains = NULL;
4240 const char *dhcp_domainname = NULL, *p;
07bdc70d 4241 sd_dhcp6_lease *dhcp6_lease = NULL;
fe0e16db 4242 bool space;
07bdc70d 4243
c1a38904
MTL
4244 fprintf(f, "REQUIRED_FOR_ONLINE=%s\n",
4245 yes_no(link->network->required_for_online));
4246
d5e172d2
ZJS
4247 LinkOperationalStateRange st = link->network->required_operstate_for_online;
4248 fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4249 strempty(link_operstate_to_string(st.min)),
4250 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? ":" : "",
4251 st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? strempty(link_operstate_to_string(st.max)) : "");
4ac77d63 4252
07bdc70d 4253 if (link->dhcp6_client) {
4058d339
TG
4254 r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease);
4255 if (r < 0 && r != -ENOMSG)
2206aa5c 4256 log_link_debug_errno(link, r, "Failed to get DHCPv6 lease: %m");
07bdc70d 4257 }
b0e39c82 4258
adc5b2e2
TG
4259 fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
4260
d5e172d2
ZJS
4261 /************************************************************/
4262
0d536673 4263 fputs("DNS=", f);
ea352b40 4264 space = false;
15761549 4265 if (link->n_dns != (unsigned) -1)
64581765 4266 link_save_dns(link, f, link->dns, link->n_dns, &space);
15761549 4267 else
64581765 4268 link_save_dns(link, f, link->network->dns, link->network->n_dns, &space);
d5314fff 4269
d5e172d2
ZJS
4270 serialize_addresses(f, NULL, &space,
4271 NULL,
4272 link->dhcp_lease,
4273 link->network->dhcp_use_dns,
ddb82ec2 4274 SD_DHCP_LEASE_DNS,
d5e172d2
ZJS
4275 dhcp6_lease,
4276 link->network->dhcp6_use_dns,
4277 sd_dhcp6_lease_get_dns,
4278 NULL);
1e7a0e21 4279
a8c10331 4280 /* Make sure to flush out old entries before we use the NDisc data */
1e7a0e21
LP
4281 ndisc_vacuum(link);
4282
b296797f 4283 if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) {
1e7a0e21
LP
4284 NDiscRDNSS *dd;
4285
d8bff5cc
ZJS
4286 SET_FOREACH(dd, link->ndisc_rdnss, i)
4287 serialize_in6_addrs(f, &dd->address, 1, &space);
299d578f
SS
4288 }
4289
4290 fputc('\n', f);
4291
d5e172d2
ZJS
4292 /************************************************************/
4293
4294 serialize_addresses(f, "NTP", NULL,
4295 link->ntp ?: link->network->ntp,
4296 link->dhcp_lease,
4297 link->network->dhcp_use_ntp,
ddb82ec2 4298 SD_DHCP_LEASE_NTP,
d5e172d2
ZJS
4299 dhcp6_lease,
4300 link->network->dhcp6_use_ntp,
4301 sd_dhcp6_lease_get_ntp_addrs,
4302 sd_dhcp6_lease_get_ntp_fqdn);
4303
4304 serialize_addresses(f, "SIP", NULL,
2a71d57f 4305 NULL,
d5e172d2
ZJS
4306 link->dhcp_lease,
4307 link->network->dhcp_use_sip,
ddb82ec2 4308 SD_DHCP_LEASE_SIP,
2a71d57f 4309 NULL, false, NULL, NULL);
d5e172d2
ZJS
4310
4311 /************************************************************/
bd8f6538 4312
b2a81c0b 4313 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
b85bc551 4314 if (link->dhcp_lease) {
b2a81c0b 4315 (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname);
b85bc551
DW
4316 (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains);
4317 }
b2a81c0b
LP
4318 if (dhcp6_lease)
4319 (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains);
07bdc70d
PF
4320 }
4321
fe0e16db
YW
4322 fputs("DOMAINS=", f);
4323 space = false;
15761549 4324 ORDERED_SET_FOREACH(p, link->search_domains ?: link->network->search_domains, i)
fe0e16db 4325 fputs_with_space(f, p, NULL, &space);
07bdc70d 4326
1e7a0e21 4327 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) {
1e7a0e21
LP
4328 if (dhcp_domainname)
4329 fputs_with_space(f, dhcp_domainname, NULL, &space);
b85bc551
DW
4330 if (dhcp_domains)
4331 fputstrv(f, dhcp_domains, NULL, &space);
1e7a0e21
LP
4332 if (dhcp6_domains)
4333 fputstrv(f, dhcp6_domains, NULL, &space);
a0cd6da5
YW
4334 }
4335
4336 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_YES) {
4337 NDiscDNSSL *dd;
1e7a0e21
LP
4338
4339 SET_FOREACH(dd, link->ndisc_dnssl, i)
4340 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
4341 }
b2a81c0b 4342
0d536673 4343 fputc('\n', f);
6192b846 4344
d5e172d2
ZJS
4345 /************************************************************/
4346
fe0e16db
YW
4347 fputs("ROUTE_DOMAINS=", f);
4348 space = false;
15761549 4349 ORDERED_SET_FOREACH(p, link->route_domains ?: link->network->route_domains, i)
fe0e16db 4350 fputs_with_space(f, p, NULL, &space);
b2a81c0b 4351
1e7a0e21 4352 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) {
1e7a0e21
LP
4353 if (dhcp_domainname)
4354 fputs_with_space(f, dhcp_domainname, NULL, &space);
b85bc551
DW
4355 if (dhcp_domains)
4356 fputstrv(f, dhcp_domains, NULL, &space);
1e7a0e21
LP
4357 if (dhcp6_domains)
4358 fputstrv(f, dhcp6_domains, NULL, &space);
a0cd6da5
YW
4359 }
4360
4361 if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_ROUTE) {
4362 NDiscDNSSL *dd;
1e7a0e21
LP
4363
4364 SET_FOREACH(dd, link->ndisc_dnssl, i)
4365 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
4366 }
b2a81c0b 4367
0d536673 4368 fputc('\n', f);
67272d15 4369
d5e172d2
ZJS
4370 /************************************************************/
4371
3c9b8860 4372 fprintf(f, "LLMNR=%s\n",
15761549 4373 resolve_support_to_string(link->llmnr >= 0 ? link->llmnr : link->network->llmnr));
d5e172d2
ZJS
4374
4375 /************************************************************/
4376
aaa297d4 4377 fprintf(f, "MDNS=%s\n",
15761549 4378 resolve_support_to_string(link->mdns >= 0 ? link->mdns : link->network->mdns));
15761549 4379
d5e172d2
ZJS
4380 /************************************************************/
4381
4382 int dns_default_route =
4383 link->dns_default_route >= 0 ? link->dns_default_route :
4384 link->network->dns_default_route;
4385 if (dns_default_route >= 0)
4386 fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route));
4387
4388 /************************************************************/
4389
4390 DnsOverTlsMode dns_over_tls_mode =
4391 link->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID ? link->dns_over_tls_mode :
4392 link->network->dns_over_tls_mode;
4393 if (dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID)
4394 fprintf(f, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode));
4395
4396 /************************************************************/
4397
4398 DnssecMode dnssec_mode =
4399 link->dnssec_mode != _DNSSEC_MODE_INVALID ? link->dnssec_mode :
4400 link->network->dnssec_mode;
4401 if (dnssec_mode != _DNSSEC_MODE_INVALID)
4402 fprintf(f, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode));
4403
4404 /************************************************************/
4405
4406 Set *nta_anchors = link->dnssec_negative_trust_anchors;
4407 if (set_isempty(nta_anchors))
4408 nta_anchors = link->network->dnssec_negative_trust_anchors;
4409
4410 if (!set_isempty(nta_anchors)) {
8a516214
LP
4411 const char *n;
4412
0d536673 4413 fputs("DNSSEC_NTA=", f);
8a516214 4414 space = false;
d5e172d2 4415 SET_FOREACH(n, nta_anchors, i)
d390f8ef 4416 fputs_with_space(f, n, NULL, &space);
0d536673 4417 fputc('\n', f);
8a516214
LP
4418 }
4419
d5e172d2
ZJS
4420 /************************************************************/
4421
0d536673 4422 fputs("ADDRESSES=", f);
e7780c8d
TG
4423 space = false;
4424 SET_FOREACH(a, link->addresses, i) {
4425 _cleanup_free_ char *address_str = NULL;
4426
4427 r = in_addr_to_string(a->family, &a->in_addr, &address_str);
4428 if (r < 0)
4429 goto fail;
4430
e7780c8d
TG
4431 fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen);
4432 space = true;
4433 }
0d536673 4434 fputc('\n', f);
c1eb9872 4435
d5e172d2
ZJS
4436 /************************************************************/
4437
0d536673 4438 fputs("ROUTES=", f);
c1eb9872
TG
4439 space = false;
4440 SET_FOREACH(route, link->routes, i) {
4441 _cleanup_free_ char *route_str = NULL;
4442
4443 r = in_addr_to_string(route->family, &route->dst, &route_str);
4444 if (r < 0)
4445 goto fail;
4446
ecc3f340
ZJS
4447 fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT,
4448 space ? " " : "", route_str,
f833694d 4449 route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime);
c1eb9872
TG
4450 space = true;
4451 }
4452
0d536673 4453 fputc('\n', f);
bcb7a07e 4454 }
7374f9d8 4455
b295beea
LP
4456 print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links);
4457 print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links);
0d4ad91d 4458
8eb9058d 4459 if (link->dhcp_lease) {
bd91b83e 4460 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
fe8db0c5 4461 if (r < 0)
c2d6bd61 4462 goto fail;
fe8db0c5 4463
7374f9d8 4464 fprintf(f,
b0e39c82
TG
4465 "DHCP_LEASE=%s\n",
4466 link->lease_file);
deb2e523 4467 } else
6990fb6b 4468 (void) unlink(link->lease_file);
fe8db0c5 4469
0bc70f1d
TG
4470 if (link->ipv4ll) {
4471 struct in_addr address;
4472
4473 r = sd_ipv4ll_get_address(link->ipv4ll, &address);
4474 if (r >= 0) {
0d536673 4475 fputs("IPV4LL_ADDRESS=", f);
072320ea 4476 serialize_in_addrs(f, &address, 1, false, NULL);
0d536673 4477 fputc('\n', f);
0bc70f1d
TG
4478 }
4479 }
4480
7e738e7b
SS
4481 if (link->dhcp6_client) {
4482 _cleanup_free_ char *duid = NULL;
4483 uint32_t iaid;
4484
4485 r = sd_dhcp6_client_get_iaid(link->dhcp6_client, &iaid);
4486 if (r >= 0)
4487 fprintf(f, "DHCP6_CLIENT_IAID=0x%x\n", iaid);
4488
4489 r = sd_dhcp6_client_duid_as_string(link->dhcp6_client, &duid);
4490 if (r >= 0)
4491 fprintf(f, "DHCP6_CLIENT_DUID=%s\n", duid);
4492 }
18d8a2cf 4493
c2d6bd61
LP
4494 r = fflush_and_check(f);
4495 if (r < 0)
4496 goto fail;
fe8db0c5 4497
c2d6bd61 4498 if (rename(temp_path, link->state_file) < 0) {
fe8db0c5 4499 r = -errno;
c2d6bd61 4500 goto fail;
fe8db0c5
TG
4501 }
4502
c2d6bd61 4503 return 0;
dacd6cee 4504
c2d6bd61 4505fail:
6a7a4e4d 4506 (void) unlink(link->state_file);
6a7a4e4d
LP
4507 if (temp_path)
4508 (void) unlink(temp_path);
4509
dacd6cee 4510 return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file);
fe8db0c5
TG
4511}
4512
84de38c5
TG
4513/* The serialized state in /run is no longer up-to-date. */
4514void link_dirty(Link *link) {
4515 int r;
4516
4517 assert(link);
4518
0c241a37
SS
4519 /* mark manager dirty as link is dirty */
4520 manager_dirty(link->manager);
4521
de7fef4b 4522 r = set_ensure_put(&link->manager->dirty_links, NULL, link);
0c241a37 4523 if (r <= 0)
de7fef4b 4524 /* Ignore allocation errors and don't take another ref if the link was already dirty */
84de38c5 4525 return;
84de38c5
TG
4526 link_ref(link);
4527}
4528
4529/* The serialized state in /run is up-to-date */
4530void link_clean(Link *link) {
4531 assert(link);
4532 assert(link->manager);
4533
1046bf9b 4534 link_unref(set_remove(link->manager->dirty_links, link));
84de38c5
TG
4535}
4536
c2a65950
YW
4537int link_save_and_clean(Link *link) {
4538 int r;
4539
4540 r = link_save(link);
4541 if (r < 0)
4542 return r;
4543
4544 link_clean(link);
4545 return 0;
4546}
4547
fe8db0c5 4548static const char* const link_state_table[_LINK_STATE_MAX] = {
8434fd5c 4549 [LINK_STATE_PENDING] = "pending",
bd08ce56 4550 [LINK_STATE_INITIALIZED] = "initialized",
289e6774 4551 [LINK_STATE_CONFIGURING] = "configuring",
fe8db0c5 4552 [LINK_STATE_CONFIGURED] = "configured",
57bd6899 4553 [LINK_STATE_UNMANAGED] = "unmanaged",
fe8db0c5 4554 [LINK_STATE_FAILED] = "failed",
370e9930 4555 [LINK_STATE_LINGER] = "linger",
fe8db0c5
TG
4556};
4557
4558DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
5ecb131d
YW
4559
4560int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg) {
4561 const char *err_msg = NULL;
4562
4563 (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg);
ab119e63
YW
4564 return log_link_full(link, level, err,
4565 "%s: %s%s%s%m",
4566 msg,
4567 strempty(err_msg),
4568 err_msg && !endswith(err_msg, ".") ? "." : "",
4569 err_msg ? " " : "");
5ecb131d 4570}