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