network: skip to check dynamic addresses when ConfigureWithoutCarrier=yes
[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>
4cc7a82c 5#include <unistd.h>
f579559b 6
b5efdb8a 7#include "alloc-util.h"
1346b1f0 8#include "bus-util.h"
27dfc982 9#include "dhcp-identifier.h"
bd91b83e 10#include "dhcp-lease-internal.h"
686d13b9 11#include "env-file.h"
3ffd4af2 12#include "fd-util.h"
cf1d700d 13#include "fileio.h"
ef118d00 14#include "missing_network.h"
4799f19e 15#include "netdev/bond.h"
9a81f119 16#include "netdev/bridge.h"
f410d463 17#include "netdev/ipvlan.h"
bdb9f580 18#include "netdev/vrf.h"
cf1d700d 19#include "netlink-util.h"
c6f7c917 20#include "network-internal.h"
3ddcbeea 21#include "networkd-can.h"
a0e5c15d 22#include "networkd-ipv6-proxy-ndp.h"
8e1ad1ea 23#include "networkd-lldp-tx.h"
23f53b99 24#include "networkd-manager.h"
1e7a0e21 25#include "networkd-ndisc.h"
e4a71bf3 26#include "networkd-neighbor.h"
7465dd22 27#include "networkd-radv.h"
bce67bbe 28#include "networkd-routing-policy-rule.h"
cf1d700d
TG
29#include "set.h"
30#include "socket-util.h"
15a5e950 31#include "stdio-util.h"
8b43440b 32#include "string-table.h"
51517f9e 33#include "strv.h"
62e021a9 34#include "sysctl-util.h"
e4de7287 35#include "tmpfile-util.h"
299ad32d 36#include "udev-util.h"
cf1d700d
TG
37#include "util.h"
38#include "virt.h"
fc2f9534 39
bdb9f580
YW
40uint32_t link_get_vrf_table(Link *link) {
41 return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
42}
43
44uint32_t link_get_dhcp_route_table(Link *link) {
45 /* When the interface is part of an VRF use the VRFs routing table, unless
46 * another table is explicitly specified. */
47 if (link->network->dhcp_route_table_set)
48 return link->network->dhcp_route_table;
49 return link_get_vrf_table(link);
50}
51
52uint32_t link_get_ipv6_accept_ra_route_table(Link *link) {
53 if (link->network->ipv6_accept_ra_route_table_set)
54 return link->network->ipv6_accept_ra_route_table;
55 return link_get_vrf_table(link);
56}
57
f24648a6
YW
58DUID* link_get_duid(Link *link) {
59 if (link->network->duid.type != _DUID_TYPE_INVALID)
60 return &link->network->duid;
61 else
62 return &link->manager->duid;
63}
64
b9d74c40
LP
65static bool link_dhcp6_enabled(Link *link) {
66 assert(link);
67
fa709992
LP
68 if (!socket_ipv6_is_supported())
69 return false;
70
78c958f8
TG
71 if (link->flags & IFF_LOOPBACK)
72 return false;
73
74 if (!link->network)
75 return false;
76
f2bfcdb9
YW
77 if (link->network->bond)
78 return false;
79
cab042b8 80 if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan"))
af9ba57a
YW
81 return false;
82
4b600505
YW
83 if (manager_sysctl_ipv6_enabled(link->manager) == 0)
84 return false;
85
e0ee46f2 86 return link->network->dhcp & ADDRESS_FAMILY_IPV6;
78c958f8
TG
87}
88
b9d74c40
LP
89static bool link_dhcp4_enabled(Link *link) {
90 assert(link);
91
78c958f8
TG
92 if (link->flags & IFF_LOOPBACK)
93 return false;
94
95 if (!link->network)
96 return false;
97
f2bfcdb9
YW
98 if (link->network->bond)
99 return false;
100
cab042b8 101 if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan"))
af9ba57a
YW
102 return false;
103
e0ee46f2 104 return link->network->dhcp & ADDRESS_FAMILY_IPV4;
78c958f8
TG
105}
106
b9d74c40
LP
107static bool link_dhcp4_server_enabled(Link *link) {
108 assert(link);
109
78c958f8
TG
110 if (link->flags & IFF_LOOPBACK)
111 return false;
112
113 if (!link->network)
114 return false;
115
f2bfcdb9
YW
116 if (link->network->bond)
117 return false;
118
cab042b8 119 if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan"))
af9ba57a
YW
120 return false;
121
78c958f8
TG
122 return link->network->dhcp_server;
123}
124
910feb78 125bool link_ipv4ll_enabled(Link *link, AddressFamilyBoolean mask) {
b9d74c40 126 assert(link);
910feb78 127 assert((mask & ~(ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) == 0);
b9d74c40 128
78c958f8
TG
129 if (link->flags & IFF_LOOPBACK)
130 return false;
131
132 if (!link->network)
133 return false;
134
d61e4c5b 135 if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6", "can", "vcan", "vxcan", "nlmon"))
a8f5bba6
JD
136 return false;
137
f410d463
YW
138 /* L3 or L3S mode do not support ARP. */
139 if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
140 return false;
141
f2bfcdb9
YW
142 if (link->network->bond)
143 return false;
144
d61e4c5b
YW
145 if (link->network->bond)
146 return false;
147
910feb78 148 return link->network->link_local & mask;
8bc17bb3
SS
149}
150
b9d74c40
LP
151static bool link_ipv6ll_enabled(Link *link) {
152 assert(link);
153
fa709992
LP
154 if (!socket_ipv6_is_supported())
155 return false;
156
d0d6a4cd
TG
157 if (link->flags & IFF_LOOPBACK)
158 return false;
159
160 if (!link->network)
161 return false;
162
d61e4c5b 163 if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "can", "vcan", "vxcan", "nlmon"))
a8f5bba6
JD
164 return false;
165
f2bfcdb9
YW
166 if (link->network->bond)
167 return false;
168
4b600505
YW
169 if (manager_sysctl_ipv6_enabled(link->manager) == 0)
170 return false;
171
e0ee46f2 172 return link->network->link_local & ADDRESS_FAMILY_IPV6;
78c958f8
TG
173}
174
439689c6
SS
175static bool link_ipv6_enabled(Link *link) {
176 assert(link);
177
178 if (!socket_ipv6_is_supported())
179 return false;
180
b102cdca 181 if (link->network->bond)
2b00a4e0
TY
182 return false;
183
4b600505
YW
184 if (manager_sysctl_ipv6_enabled(link->manager) == 0)
185 return false;
186
cab042b8 187 if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan"))
af9ba57a
YW
188 return false;
189
4cef7fe3
TY
190 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
191 return link_ipv6ll_enabled(link) || network_has_static_ipv6_addresses(link->network);
439689c6
SS
192}
193
7465dd22
PF
194static bool link_radv_enabled(Link *link) {
195 assert(link);
196
197 if (!link_ipv6ll_enabled(link))
198 return false;
199
55a7c78b 200 return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE;
7465dd22
PF
201}
202
769d324c 203static bool link_ipv4_forward_enabled(Link *link) {
b9d74c40
LP
204 assert(link);
205
5a8bcb67
LP
206 if (link->flags & IFF_LOOPBACK)
207 return false;
208
209 if (!link->network)
210 return false;
211
765afd5c
LP
212 if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID)
213 return false;
214
e0ee46f2 215 return link->network->ip_forward & ADDRESS_FAMILY_IPV4;
769d324c
LP
216}
217
218static bool link_ipv6_forward_enabled(Link *link) {
b9d74c40 219 assert(link);
765afd5c
LP
220
221 if (!socket_ipv6_is_supported())
222 return false;
223
769d324c
LP
224 if (link->flags & IFF_LOOPBACK)
225 return false;
226
227 if (!link->network)
228 return false;
229
765afd5c
LP
230 if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID)
231 return false;
232
4b600505
YW
233 if (manager_sysctl_ipv6_enabled(link->manager) == 0)
234 return false;
235
e0ee46f2 236 return link->network->ip_forward & ADDRESS_FAMILY_IPV6;
5a8bcb67
LP
237}
238
23d8b221
SS
239static bool link_proxy_arp_enabled(Link *link) {
240 assert(link);
241
242 if (link->flags & IFF_LOOPBACK)
243 return false;
244
245 if (!link->network)
246 return false;
247
248 if (link->network->proxy_arp < 0)
249 return false;
250
251 return true;
252}
253
b9d74c40
LP
254static bool link_ipv6_accept_ra_enabled(Link *link) {
255 assert(link);
256
fa709992
LP
257 if (!socket_ipv6_is_supported())
258 return false;
259
f5a8c43f
TG
260 if (link->flags & IFF_LOOPBACK)
261 return false;
262
263 if (!link->network)
264 return false;
265
702c979f
SS
266 if (!link_ipv6ll_enabled(link))
267 return false;
268
f5a8c43f
TG
269 /* If unset use system default (enabled if local forwarding is disabled.
270 * disabled if local forwarding is enabled).
271 * If set, ignore or enforce RA independent of local forwarding state.
272 */
273 if (link->network->ipv6_accept_ra < 0)
274 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
275 return !link_ipv6_forward_enabled(link);
276 else if (link->network->ipv6_accept_ra > 0)
277 /* accept RA even if ip_forward is enabled */
278 return true;
279 else
280 /* ignore RA */
281 return false;
282}
283
1f0d9695 284static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) {
fa709992 285 assert(link);
d68e2e59
LP
286
287 if (!socket_ipv6_is_supported())
288 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
289
49092e22 290 if (link->flags & IFF_LOOPBACK)
1f0d9695 291 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
49092e22
SS
292
293 if (!link->network)
1f0d9695 294 return _IPV6_PRIVACY_EXTENSIONS_INVALID;
49092e22
SS
295
296 return link->network->ipv6_privacy_extensions;
297}
298
439689c6 299static int link_enable_ipv6(Link *link) {
439689c6
SS
300 bool disabled;
301 int r;
302
303 if (link->flags & IFF_LOOPBACK)
304 return 0;
305
306 disabled = !link_ipv6_enabled(link);
307
62e021a9 308 r = sysctl_write_ip_property_boolean(AF_INET6, link->ifname, "disable_ipv6", disabled);
439689c6 309 if (r < 0)
c98b3545 310 log_link_warning_errno(link, r, "Cannot %s IPv6: %m", enable_disable(!disabled));
2d37cd53 311 else
b106c586 312 log_link_info(link, "IPv6 successfully %sd", enable_disable(!disabled));
439689c6
SS
313
314 return 0;
315}
316
34bf3c00
YW
317static bool link_is_enslaved(Link *link) {
318 if (link->flags & IFF_SLAVE)
319 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
320 return true;
321
34bf3c00
YW
322 if (!link->network)
323 return false;
324
bb262ef0 325 if (link->master_ifindex > 0 && link->network->bridge)
34bf3c00
YW
326 return true;
327
bb262ef0
YW
328 /* TODO: add conditions for other netdevs. */
329
34bf3c00
YW
330 return false;
331}
332
45e11abf
YW
333static void link_update_master_operstate(Link *link, NetDev *netdev) {
334 Link *master;
335
336 if (!netdev)
337 return;
338
339 if (link_get(link->manager, netdev->ifindex, &master) < 0)
340 return;
341
342 link_update_operstate(master, true);
343}
344
345void link_update_operstate(Link *link, bool also_update_master) {
84de38c5 346 LinkOperationalState operstate;
45e11abf 347 Iterator i;
14153d1b 348
84de38c5
TG
349 assert(link);
350
351 if (link->kernel_operstate == IF_OPER_DORMANT)
352 operstate = LINK_OPERSTATE_DORMANT;
353 else if (link_has_carrier(link)) {
354 Address *address;
355 uint8_t scope = RT_SCOPE_NOWHERE;
84de38c5
TG
356
357 /* if we have carrier, check what addresses we have */
358 SET_FOREACH(address, link->addresses, i) {
359 if (!address_is_ready(address))
360 continue;
361
362 if (address->scope < scope)
363 scope = address->scope;
364 }
365
366 /* for operstate we also take foreign addresses into account */
367 SET_FOREACH(address, link->addresses_foreign, i) {
368 if (!address_is_ready(address))
369 continue;
370
371 if (address->scope < scope)
372 scope = address->scope;
373 }
374
375 if (scope < RT_SCOPE_SITE)
376 /* universally accessible addresses found */
377 operstate = LINK_OPERSTATE_ROUTABLE;
378 else if (scope < RT_SCOPE_HOST)
379 /* only link or site local addresses found */
380 operstate = LINK_OPERSTATE_DEGRADED;
381 else
382 /* no useful addresses found */
383 operstate = LINK_OPERSTATE_CARRIER;
384 } else if (link->flags & IFF_UP)
385 operstate = LINK_OPERSTATE_NO_CARRIER;
386 else
387 operstate = LINK_OPERSTATE_OFF;
388
14153d1b 389 if (IN_SET(operstate, LINK_OPERSTATE_DEGRADED, LINK_OPERSTATE_CARRIER) &&
34bf3c00 390 link_is_enslaved(link))
14153d1b
YW
391 operstate = LINK_OPERSTATE_ENSLAVED;
392
c9cc0383 393 if (operstate >= LINK_OPERSTATE_CARRIER) {
959f65d3
YW
394 Link *slave;
395
5f707e12 396 SET_FOREACH(slave, link->slaves, i) {
959f65d3
YW
397 link_update_operstate(slave, false);
398
c9cc0383
YW
399 if (slave->operstate < LINK_OPERSTATE_CARRIER)
400 operstate = LINK_OPERSTATE_DEGRADED_CARRIER;
959f65d3
YW
401 }
402 }
403
84de38c5
TG
404 if (link->operstate != operstate) {
405 link->operstate = operstate;
406 link_send_changed(link, "OperationalState", NULL);
407 link_dirty(link);
84de38c5 408 }
959f65d3 409
45e11abf
YW
410 if (also_update_master && link->network) {
411 link_update_master_operstate(link, link->network->bond);
412 link_update_master_operstate(link, link->network->bridge);
959f65d3 413 }
84de38c5
TG
414}
415
51d18171
TG
416#define FLAG_STRING(string, flag, old, new) \
417 (((old ^ new) & flag) \
418 ? ((old & flag) ? (" -" string) : (" +" string)) \
419 : "")
420
bb262ef0 421static int link_update_flags(Link *link, sd_netlink_message *m, bool force_update_operstate) {
51d18171
TG
422 unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
423 uint8_t operstate;
424 int r;
425
426 assert(link);
427
428 r = sd_rtnl_message_link_get_flags(m, &flags);
6a7a4e4d
LP
429 if (r < 0)
430 return log_link_warning_errno(link, r, "Could not get link flags: %m");
51d18171 431
1c4baffc 432 r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate);
51d18171
TG
433 if (r < 0)
434 /* if we got a message without operstate, take it to mean
435 the state was unchanged */
436 operstate = link->kernel_operstate;
437
bb262ef0 438 if (!force_update_operstate && (link->flags == flags) && (link->kernel_operstate == operstate))
51d18171
TG
439 return 0;
440
441 if (link->flags != flags) {
6a7a4e4d 442 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
443 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
444 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
445 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
446 FLAG_STRING("UP", IFF_UP, link->flags, flags),
447 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
448 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
449 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
450 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
451 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
452 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
453 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
454 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
455 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
456 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
457 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
458 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
459 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
460 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
461 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
462
463 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
464 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
465 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
466 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
467 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
468 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
469 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
470 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
471
472 /* link flags are currently at most 18 bits, let's align to
473 * printing 20 */
474 if (unknown_flags_added)
79008bdd 475 log_link_debug(link,
6a7a4e4d 476 "Unknown link flags gained: %#.5x (ignoring)",
51d18171
TG
477 unknown_flags_added);
478
479 if (unknown_flags_removed)
79008bdd 480 log_link_debug(link,
6a7a4e4d 481 "Unknown link flags lost: %#.5x (ignoring)",
51d18171
TG
482 unknown_flags_removed);
483 }
484
485 link->flags = flags;
486 link->kernel_operstate = operstate;
487
959f65d3 488 link_update_operstate(link, true);
51d18171
TG
489
490 return 0;
491}
492
1c4baffc 493static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
8e766630 494 _cleanup_(link_unrefp) Link *link = NULL;
505f8da7 495 uint16_t type;
6cad256d 496 const char *ifname, *kind = NULL;
505f8da7 497 int r, ifindex;
b710e6b6 498 unsigned short iftype;
f579559b 499
0c2f9b84 500 assert(manager);
505f8da7 501 assert(message);
f579559b
TG
502 assert(ret);
503
6cad256d
TJ
504 /* check for link kind */
505 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
506 if (r == 0) {
db2f8a2e 507 (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind);
6cad256d
TJ
508 r = sd_netlink_message_exit_container(message);
509 if (r < 0)
510 return r;
511 }
512
1c4baffc 513 r = sd_netlink_message_get_type(message, &type);
505f8da7
TG
514 if (r < 0)
515 return r;
516 else if (type != RTM_NEWLINK)
517 return -EINVAL;
518
519 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
520 if (r < 0)
521 return r;
522 else if (ifindex <= 0)
523 return -EINVAL;
524
b710e6b6
LP
525 r = sd_rtnl_message_link_get_type(message, &iftype);
526 if (r < 0)
527 return r;
528
1c4baffc 529 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
505f8da7
TG
530 if (r < 0)
531 return r;
532
17f9c355 533 link = new(Link, 1);
f579559b
TG
534 if (!link)
535 return -ENOMEM;
536
17f9c355
YW
537 *link = (Link) {
538 .n_ref = 1,
539 .manager = manager,
540 .state = LINK_STATE_PENDING,
541 .rtnl_extended_attrs = true,
542 .ifindex = ifindex,
543 .iftype = iftype,
544 };
545
505f8da7
TG
546 link->ifname = strdup(ifname);
547 if (!link->ifname)
548 return -ENOMEM;
f579559b 549
6cad256d
TJ
550 if (kind) {
551 link->kind = strdup(kind);
552 if (!link->kind)
553 return -ENOMEM;
554 }
555
cbff7170
TJ
556 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex);
557 if (r < 0)
558 log_link_debug_errno(link, r, "New device has no master, continuing without");
559
1c4baffc 560 r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
512922f8 561 if (r < 0)
34437b4f 562 log_link_debug_errno(link, r, "MAC address not found for new device, continuing without");
512922f8 563
34437b4f 564 if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0)
315db1a8 565 return -ENOMEM;
fe8db0c5 566
34437b4f 567 if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0)
68a8723c
TG
568 return -ENOMEM;
569
34437b4f 570 if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
49699bac
SS
571 return -ENOMEM;
572
d5099efc 573 r = hashmap_ensure_allocated(&manager->links, NULL);
ae06ab10
TG
574 if (r < 0)
575 return r;
576
577 r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
f579559b
TG
578 if (r < 0)
579 return r;
580
bb262ef0 581 r = link_update_flags(link, message, false);
51d18171
TG
582 if (r < 0)
583 return r;
584
1cc6c93a 585 *ret = TAKE_PTR(link);
f579559b
TG
586
587 return 0;
588}
589
8301aa0b 590static Link *link_free(Link *link) {
428fd0a7
TG
591 Address *address;
592
8301aa0b 593 assert(link);
f579559b 594
f535e354
YW
595 link->routes = set_free_with_destructor(link->routes, route_free);
596 link->routes_foreign = set_free_with_destructor(link->routes_foreign, route_free);
adda1ed9 597
f535e354
YW
598 link->addresses = set_free_with_destructor(link->addresses, address_free);
599 link->addresses_foreign = set_free_with_destructor(link->addresses_foreign, address_free);
adda1ed9 600
11bf3cce
LP
601 while ((address = link->pool_addresses)) {
602 LIST_REMOVE(addresses, link->pool_addresses, address);
603 address_free(address);
604 }
605
bfcdba8d 606 sd_dhcp_server_unref(link->dhcp_server);
e5b04c8d 607 sd_dhcp_client_unref(link->dhcp_client);
a6cc569e 608 sd_dhcp_lease_unref(link->dhcp_lease);
f5be5601 609
7272b25e 610 link_lldp_emit_stop(link);
8e1ad1ea 611
68a8723c
TG
612 free(link->lease_file);
613
4afd3348 614 sd_lldp_unref(link->lldp);
49699bac
SS
615 free(link->lldp_file);
616
c69305ff
LP
617 ndisc_flush(link);
618
56cd007a 619 sd_ipv4ll_unref(link->ipv4ll);
4138fb2c 620 sd_dhcp6_client_unref(link->dhcp6_client);
1e7a0e21 621 sd_ndisc_unref(link->ndisc);
7465dd22 622 sd_radv_unref(link->radv);
1e7a0e21 623
c166a070 624 free(link->ifname);
ceac4078 625 free(link->kind);
6cad256d 626
db2f8a2e 627 (void) unlink(link->state_file);
fe8db0c5 628 free(link->state_file);
c166a070 629
51517f9e 630 sd_device_unref(link->sd_device);
b5db00e5 631
0d4ad91d 632 hashmap_free(link->bound_to_links);
0d4ad91d
AR
633 hashmap_free(link->bound_by_links);
634
5f707e12 635 set_free_with_destructor(link->slaves, link_unref);
033295c1 636
c9c908a6
YW
637 network_unref(link->network);
638
8301aa0b 639 return mfree(link);
14b746f7
TG
640}
641
8301aa0b 642DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
14b746f7 643
11a7f229
TG
644int link_get(Manager *m, int ifindex, Link **ret) {
645 Link *link;
11a7f229
TG
646
647 assert(m);
11a7f229
TG
648 assert(ifindex);
649 assert(ret);
650
ae06ab10 651 link = hashmap_get(m->links, INT_TO_PTR(ifindex));
11a7f229
TG
652 if (!link)
653 return -ENODEV;
654
655 *ret = link;
656
657 return 0;
658}
659
af9ba57a 660void link_set_state(Link *link, LinkState state) {
e331e246
TG
661 assert(link);
662
663 if (link->state == state)
664 return;
665
0beb9542
YW
666 log_link_debug(link, "State changed: %s -> %s",
667 link_state_to_string(link->state),
668 link_state_to_string(state));
669
e331e246
TG
670 link->state = state;
671
672 link_send_changed(link, "AdministrativeState", NULL);
e331e246
TG
673}
674
57bd6899
TG
675static void link_enter_unmanaged(Link *link) {
676 assert(link);
677
e331e246 678 link_set_state(link, LINK_STATE_UNMANAGED);
57bd6899 679
84de38c5 680 link_dirty(link);
57bd6899
TG
681}
682
95355a28 683int link_stop_clients(Link *link, bool may_keep_dhcp) {
111bb8f9
TG
684 int r = 0, k;
685
686 assert(link);
687 assert(link->manager);
688 assert(link->manager->event);
689
d03073dd
YW
690 dhcp4_release_old_lease(link);
691
95355a28
YW
692 if (link->dhcp_client && (!may_keep_dhcp || !link->network ||
693 !FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP))) {
111bb8f9 694 k = sd_dhcp_client_stop(link->dhcp_client);
6a7a4e4d 695 if (k < 0)
36c7d709 696 r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m");
111bb8f9
TG
697 }
698
ba179154 699 if (link->ipv4ll) {
111bb8f9 700 k = sd_ipv4ll_stop(link->ipv4ll);
6a7a4e4d 701 if (k < 0)
36c7d709 702 r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m");
dd43110f
TG
703 }
704
f5a8c43f
TG
705 if (link->dhcp6_client) {
706 k = sd_dhcp6_client_stop(link->dhcp6_client);
707 if (k < 0)
36c7d709 708 r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m");
f5a8c43f 709 }
4138fb2c 710
1e7a0e21
LP
711 if (link->ndisc) {
712 k = sd_ndisc_stop(link->ndisc);
6a7a4e4d 713 if (k < 0)
36c7d709 714 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m");
4138fb2c
PF
715 }
716
7465dd22
PF
717 if (link->radv) {
718 k = sd_radv_stop(link->radv);
719 if (k < 0)
720 r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m");
721 }
722
7272b25e 723 link_lldp_emit_stop(link);
111bb8f9
TG
724 return r;
725}
726
b22d8a00 727void link_enter_failed(Link *link) {
ef1ba606 728 assert(link);
f882c247 729
370e9930 730 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
2139694e
TG
731 return;
732
6a7a4e4d 733 log_link_warning(link, "Failed");
449f7554 734
e331e246 735 link_set_state(link, LINK_STATE_FAILED);
fe8db0c5 736
95355a28 737 link_stop_clients(link, false);
111bb8f9 738
84de38c5 739 link_dirty(link);
f882c247
TG
740}
741
4f434938
LP
742static Address* link_find_dhcp_server_address(Link *link) {
743 Address *address;
744
745 assert(link);
746 assert(link->network);
747
d4cdbea5 748 /* The first statically configured address if there is any */
4f434938
LP
749 LIST_FOREACH(addresses, address, link->network->static_addresses) {
750
751 if (address->family != AF_INET)
752 continue;
753
af93291c 754 if (in_addr_is_null(address->family, &address->in_addr))
4f434938
LP
755 continue;
756
757 return address;
758 }
759
760 /* If that didn't work, find a suitable address we got from the pool */
761 LIST_FOREACH(addresses, address, link->pool_addresses) {
762 if (address->family != AF_INET)
763 continue;
764
765 return address;
766 }
767
768 return NULL;
769}
770
7033af49
YW
771static int link_join_netdevs_after_configured(Link *link) {
772 NetDev *netdev;
773 Iterator i;
774 int r;
775
776 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
777 if (netdev->ifindex > 0)
778 /* Assume already enslaved. */
779 continue;
780
781 if (netdev_get_create_type(netdev) != NETDEV_CREATE_AFTER_CONFIGURED)
782 continue;
783
784 log_struct(LOG_DEBUG,
785 LOG_LINK_INTERFACE(link),
786 LOG_NETDEV_INTERFACE(netdev),
787 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
788
789 r = netdev_join(netdev, link, NULL);
790 if (r < 0)
791 return log_struct_errno(LOG_WARNING, r,
792 LOG_LINK_INTERFACE(link),
793 LOG_NETDEV_INTERFACE(netdev),
794 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
795 }
796
797 return 0;
798}
799
e3a7b048 800static void link_enter_configured(Link *link) {
dd43110f
TG
801 assert(link);
802 assert(link->network);
e3a7b048 803
289e6774 804 if (link->state != LINK_STATE_CONFIGURING)
e3a7b048 805 return;
dd43110f 806
6a7a4e4d 807 log_link_info(link, "Configured");
dd43110f 808
e331e246 809 link_set_state(link, LINK_STATE_CONFIGURED);
dd43110f 810
7033af49
YW
811 (void) link_join_netdevs_after_configured(link);
812
84de38c5 813 link_dirty(link);
dd43110f
TG
814}
815
47079967 816static int link_request_set_routing_policy_rule(Link *link) {
8a9b3a23
SS
817 RoutingPolicyRule *rule, *rrule = NULL;
818 int r;
819
820 assert(link);
821 assert(link->network);
822
2428613f
YW
823 link_set_state(link, LINK_STATE_CONFIGURING);
824 link->routing_policy_rules_configured = false;
825
8a9b3a23
SS
826 LIST_FOREACH(rules, rule, link->network->rules) {
827 r = routing_policy_rule_get(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to,
926062f0
SS
828 rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif,
829 rule->protocol, &rule->sport, &rule->dport, &rrule);
e6b65ab7 830 if (r == 0) {
8a9b3a23
SS
831 (void) routing_policy_rule_make_local(link->manager, rrule);
832 continue;
833 }
834
9f08a578 835 r = routing_policy_rule_configure(rule, link, NULL);
8a9b3a23
SS
836 if (r < 0) {
837 log_link_warning_errno(link, r, "Could not set routing policy rules: %m");
838 link_enter_failed(link);
839 return r;
840 }
7ef7e550
YW
841 if (r > 0)
842 link->routing_policy_rule_messages++;
8a9b3a23
SS
843 }
844
845 routing_policy_rule_purge(link->manager, link);
7715629e
ST
846 if (link->routing_policy_rule_messages == 0) {
847 link->routing_policy_rules_configured = true;
848 link_check_ready(link);
849 } else
850 log_link_debug(link, "Setting routing policy rules");
8a9b3a23
SS
851
852 return 0;
853}
854
302a796f 855static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
f882c247
TG
856 int r;
857
1046bf9b 858 assert(link);
7715629e 859 assert(link->route_messages > 0);
289e6774
WKI
860 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
861 LINK_STATE_FAILED, LINK_STATE_LINGER));
f882c247 862
7715629e 863 link->route_messages--;
f882c247 864
77a008c0 865 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
f882c247
TG
866 return 1;
867
1c4baffc 868 r = sd_netlink_message_get_errno(m);
c166a070 869 if (r < 0 && r != -EEXIST)
a2fae7bb 870 log_link_warning_errno(link, r, "Could not set route: %m");
f882c247 871
7715629e 872 if (link->route_messages == 0) {
6a7a4e4d 873 log_link_debug(link, "Routes set");
7715629e 874 link->static_routes_configured = true;
8012cd39 875 link_check_ready(link);
dd3efc09 876 }
f882c247
TG
877
878 return 1;
879}
880
b5799eeb 881int link_request_set_routes(Link *link) {
bf61b05a
LP
882 enum {
883 PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */
884 PHASE_GATEWAY, /* Second phase: Routes with a gateway */
885 _PHASE_MAX
886 } phase;
a6cc569e 887 Route *rt;
f882c247
TG
888 int r;
889
890 assert(link);
891 assert(link->network);
c42ff3a1
WKI
892 assert(link->addresses_configured);
893 assert(link->address_messages == 0);
289e6774 894 assert(link->state != _LINK_STATE_INVALID);
f882c247 895
289e6774 896 link_set_state(link, LINK_STATE_CONFIGURING);
2428613f 897 link->static_routes_configured = false;
27c34f73 898
47079967 899 r = link_request_set_routing_policy_rule(link);
f3ef324d
YW
900 if (r < 0)
901 return r;
f882c247 902
bf61b05a
LP
903 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
904 for (phase = 0; phase < _PHASE_MAX; phase++)
905 LIST_FOREACH(routes, rt, link->network->static_routes) {
0d34228f 906
bf61b05a
LP
907 if (in_addr_is_null(rt->family, &rt->gw) != (phase == PHASE_NON_GATEWAY))
908 continue;
a6cc569e 909
0d34228f
SS
910 r = route_configure(rt, link, route_handler);
911 if (r < 0) {
912 log_link_warning_errno(link, r, "Could not set routes: %m");
913 link_enter_failed(link);
914 return r;
915 }
c4423317
YW
916 if (r > 0)
917 link->route_messages++;
0d34228f 918 }
f5be5601 919
7715629e
ST
920 if (link->route_messages == 0) {
921 link->static_routes_configured = true;
8012cd39 922 link_check_ready(link);
431ca2ce 923 } else
6a7a4e4d 924 log_link_debug(link, "Setting routes");
f882c247
TG
925
926 return 0;
927}
928
6accfd31
DA
929void link_check_ready(Link *link) {
930 Address *a;
931 Iterator i;
932
933 assert(link);
934
935 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
936 return;
937
938 if (!link->network)
939 return;
940
941 if (!link->addresses_configured)
942 return;
943
944 if (!link->neighbors_configured)
945 return;
946
6aa5773b
DA
947 SET_FOREACH(a, link->addresses, i)
948 if (!address_is_ready(a))
949 return;
950
951 if (!link->addresses_ready) {
952 link->addresses_ready = true;
953 link_request_set_routes(link);
710ce9e5 954 return;
6aa5773b
DA
955 }
956
6accfd31
DA
957 if (!link->static_routes_configured)
958 return;
959
960 if (!link->routing_policy_rules_configured)
961 return;
962
463797c1 963 if (link_has_carrier(link) || !link->network->configure_without_carrier) {
6accfd31 964
463797c1
YW
965 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4) && !(link->ipv4ll_address && link->ipv4ll_route))
966 return;
6accfd31 967
463797c1
YW
968 if (link_ipv6ll_enabled(link) &&
969 in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address))
970 return;
6accfd31 971
463797c1
YW
972 if ((link_dhcp4_enabled(link) || link_dhcp6_enabled(link)) &&
973 !link->dhcp4_configured &&
974 !link->dhcp6_configured &&
975 !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address && link->ipv4ll_route))
976 /* When DHCP is enabled, at least one protocol must provide an address, or
977 * an IPv4ll fallback address must be configured. */
978 return;
979
980 if (link_ipv6_accept_ra_enabled(link) && !link->ndisc_configured)
981 return;
982 }
6accfd31 983
6accfd31
DA
984 if (link->state != LINK_STATE_CONFIGURED)
985 link_enter_configured(link);
986
987 return;
988}
989
e4a71bf3
WKI
990static int link_request_set_neighbors(Link *link) {
991 Neighbor *neighbor;
992 int r;
993
994 assert(link);
995 assert(link->network);
996 assert(link->state != _LINK_STATE_INVALID);
997
998 link_set_state(link, LINK_STATE_CONFIGURING);
2428613f 999 link->neighbors_configured = false;
e4a71bf3
WKI
1000
1001 LIST_FOREACH(neighbors, neighbor, link->network->neighbors) {
1002 r = neighbor_configure(neighbor, link, NULL);
1003 if (r < 0) {
1004 log_link_warning_errno(link, r, "Could not set neighbor: %m");
1005 link_enter_failed(link);
1006 return r;
1007 }
1008 }
1009
1010 if (link->neighbor_messages == 0) {
1011 link->neighbors_configured = true;
1012 link_check_ready(link);
1013 } else
1014 log_link_debug(link, "Setting neighbors");
1015
1016 return 0;
1017}
1018
302a796f 1019static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
f882c247
TG
1020 int r;
1021
4958aee4 1022 assert(rtnl);
f5be5601
TG
1023 assert(m);
1024 assert(link);
1025 assert(link->ifname);
7715629e 1026 assert(link->address_messages > 0);
289e6774 1027 assert(IN_SET(link->state, LINK_STATE_CONFIGURING,
370e9930 1028 LINK_STATE_FAILED, LINK_STATE_LINGER));
f882c247 1029
7715629e 1030 link->address_messages--;
f882c247 1031
5da8149f 1032 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
f882c247
TG
1033 return 1;
1034
1c4baffc 1035 r = sd_netlink_message_get_errno(m);
c166a070 1036 if (r < 0 && r != -EEXIST)
a2fae7bb 1037 log_link_warning_errno(link, r, "could not set address: %m");
45af44d4 1038 else if (r >= 0)
200a0868 1039 manager_rtnl_process_address(rtnl, m, link->manager);
f882c247 1040
7715629e 1041 if (link->address_messages == 0) {
6a7a4e4d 1042 log_link_debug(link, "Addresses set");
c42ff3a1 1043 link->addresses_configured = true;
6aa5773b 1044 link_check_ready(link);
dd3efc09 1045 }
f882c247
TG
1046
1047 return 1;
1048}
1049
a380b2d4 1050static int link_push_uplink_dns_to_dhcp_server(Link *link, sd_dhcp_server *s) {
4f5f911e
LP
1051 _cleanup_free_ struct in_addr *addresses = NULL;
1052 size_t n_addresses = 0, n_allocated = 0;
5512a963 1053 unsigned i;
4f5f911e
LP
1054
1055 log_debug("Copying DNS server information from %s", link->ifname);
1056
1057 if (!link->network)
1058 return 0;
1059
5512a963 1060 for (i = 0; i < link->network->n_dns; i++) {
49ad6829 1061 struct in_addr ia;
4f5f911e
LP
1062
1063 /* Only look for IPv4 addresses */
5512a963 1064 if (link->network->dns[i].family != AF_INET)
4f5f911e
LP
1065 continue;
1066
49ad6829
LP
1067 ia = link->network->dns[i].address.in;
1068
1069 /* Never propagate obviously borked data */
1070 if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia))
1071 continue;
1072
4f5f911e
LP
1073 if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
1074 return log_oom();
1075
49ad6829 1076 addresses[n_addresses++] = ia;
4f5f911e
LP
1077 }
1078
5512a963 1079 if (link->network->dhcp_use_dns && link->dhcp_lease) {
4f5f911e 1080 const struct in_addr *da = NULL;
072320ea 1081 int j, n;
4f5f911e
LP
1082
1083 n = sd_dhcp_lease_get_dns(link->dhcp_lease, &da);
1084 if (n > 0) {
1085
1086 if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n))
1087 return log_oom();
1088
072320ea
TH
1089 for (j = 0; j < n; j++)
1090 if (in4_addr_is_non_local(&da[j]))
1091 addresses[n_addresses++] = da[j];
4f5f911e
LP
1092 }
1093 }
1094
1095 if (n_addresses <= 0)
1096 return 0;
1097
1098 return sd_dhcp_server_set_dns(s, addresses, n_addresses);
1099}
1100
a380b2d4 1101static int link_push_uplink_ntp_to_dhcp_server(Link *link, sd_dhcp_server *s) {
4f5f911e
LP
1102 _cleanup_free_ struct in_addr *addresses = NULL;
1103 size_t n_addresses = 0, n_allocated = 0;
1104 char **a;
1105
1106 if (!link->network)
1107 return 0;
1108
1109 log_debug("Copying NTP server information from %s", link->ifname);
1110
1111 STRV_FOREACH(a, link->network->ntp) {
004aadca 1112 union in_addr_union ia;
4f5f911e
LP
1113
1114 /* Only look for IPv4 addresses */
004aadca 1115 if (in_addr_from_string(AF_INET, *a, &ia) <= 0)
4f5f911e 1116 continue;
49ad6829
LP
1117
1118 /* Never propagate obviously borked data */
004aadca 1119 if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
49ad6829 1120 continue;
4f5f911e
LP
1121
1122 if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
1123 return log_oom();
1124
004aadca 1125 addresses[n_addresses++] = ia.in;
4f5f911e
LP
1126 }
1127
5512a963 1128 if (link->network->dhcp_use_ntp && link->dhcp_lease) {
4f5f911e 1129 const struct in_addr *da = NULL;
072320ea 1130 int j, n;
4f5f911e
LP
1131
1132 n = sd_dhcp_lease_get_ntp(link->dhcp_lease, &da);
1133 if (n > 0) {
1134
1135 if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n))
1136 return log_oom();
1137
072320ea
TH
1138 for (j = 0; j < n; j++)
1139 if (in4_addr_is_non_local(&da[j]))
1140 addresses[n_addresses++] = da[j];
4f5f911e
LP
1141 }
1142 }
1143
1144 if (n_addresses <= 0)
1145 return 0;
1146
1147 return sd_dhcp_server_set_ntp(s, addresses, n_addresses);
1148}
1149
f6bb7ac5
TJ
1150static int link_set_bridge_fdb(Link *link) {
1151 FdbEntry *fdb_entry;
197e2809 1152 int r;
f6bb7ac5
TJ
1153
1154 LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) {
1155 r = fdb_entry_configure(link, fdb_entry);
197e2809 1156 if (r < 0)
f6bb7ac5 1157 return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m");
f6bb7ac5
TJ
1158 }
1159
197e2809 1160 return 0;
f6bb7ac5
TJ
1161}
1162
289e6774 1163static int link_request_set_addresses(Link *link) {
95b74ef6 1164 AddressLabel *label;
a6cc569e 1165 Address *ad;
f882c247
TG
1166 int r;
1167
1168 assert(link);
1169 assert(link->network);
f5be5601 1170 assert(link->state != _LINK_STATE_INVALID);
f882c247 1171
2428613f
YW
1172 link_set_state(link, LINK_STATE_CONFIGURING);
1173
1174 /* Reset all *_configured flags we are configuring. */
1175 link->addresses_configured = false;
6aa5773b 1176 link->addresses_ready = false;
2428613f
YW
1177 link->neighbors_configured = false;
1178 link->static_routes_configured = false;
1179 link->routing_policy_rules_configured = false;
1180
f6bb7ac5
TJ
1181 r = link_set_bridge_fdb(link);
1182 if (r < 0)
1183 return r;
1184
f3ef324d
YW
1185 r = link_request_set_neighbors(link);
1186 if (r < 0)
1187 return r;
e4a71bf3 1188
3d3d4255 1189 LIST_FOREACH(addresses, ad, link->network->static_addresses) {
a47a6dae
YW
1190 bool update;
1191
1192 update = address_get(link, ad->family, &ad->in_addr, ad->prefixlen, NULL) > 0;
1193
1194 r = address_configure(ad, link, address_handler, update);
dd3efc09 1195 if (r < 0) {
5a8bcb67 1196 log_link_warning_errno(link, r, "Could not set addresses: %m");
f5be5601 1197 link_enter_failed(link);
95b74ef6
SS
1198 return r;
1199 }
54a1a535
YW
1200 if (r > 0)
1201 link->address_messages++;
95b74ef6
SS
1202 }
1203
1204 LIST_FOREACH(labels, label, link->network->address_labels) {
cccf9517 1205 r = address_label_configure(label, link, NULL, false);
95b74ef6
SS
1206 if (r < 0) {
1207 log_link_warning_errno(link, r, "Could not set address label: %m");
1208 link_enter_failed(link);
f5be5601
TG
1209 return r;
1210 }
1211
7715629e 1212 link->address_label_messages++;
f882c247
TG
1213 }
1214
d4cdbea5
TG
1215 /* now that we can figure out a default address for the dhcp server,
1216 start it */
708c425d 1217 if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) {
d4cdbea5 1218 Address *address;
4f5f911e
LP
1219 Link *uplink = NULL;
1220 bool acquired_uplink = false;
d4cdbea5
TG
1221
1222 address = link_find_dhcp_server_address(link);
1223 if (!address) {
6a7a4e4d 1224 log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance.");
d4cdbea5
TG
1225 link_enter_failed(link);
1226 return 0;
1227 }
1228
61986155 1229 /* use the server address' subnet as the pool */
9b3a67c5
TG
1230 r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen,
1231 link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size);
d4cdbea5
TG
1232 if (r < 0)
1233 return r;
1234
1235 /* TODO:
1236 r = sd_dhcp_server_set_router(link->dhcp_server,
1237 &main_address->in_addr.in);
1238 if (r < 0)
1239 return r;
d4cdbea5
TG
1240 */
1241
586ac6f7
LP
1242 if (link->network->dhcp_server_max_lease_time_usec > 0) {
1243 r = sd_dhcp_server_set_max_lease_time(
1244 link->dhcp_server,
1245 DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC));
1246 if (r < 0)
1247 return r;
1248 }
1249
1250 if (link->network->dhcp_server_default_lease_time_usec > 0) {
1251 r = sd_dhcp_server_set_default_lease_time(
1252 link->dhcp_server,
1253 DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC));
1254 if (r < 0)
1255 return r;
1256 }
1257
1a04db0f
LP
1258 if (link->network->dhcp_server_emit_dns) {
1259
4f5f911e 1260 if (link->network->n_dhcp_server_dns > 0)
1a04db0f 1261 r = sd_dhcp_server_set_dns(link->dhcp_server, link->network->dhcp_server_dns, link->network->n_dhcp_server_dns);
4f5f911e
LP
1262 else {
1263 uplink = manager_find_uplink(link->manager, link);
1264 acquired_uplink = true;
1265
1266 if (!uplink) {
1267 log_link_debug(link, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1268 r = 0;
1269 } else
a380b2d4 1270 r = link_push_uplink_dns_to_dhcp_server(uplink, link->dhcp_server);
4f5f911e
LP
1271 }
1272 if (r < 0)
1273 log_link_warning_errno(link, r, "Failed to set DNS server for DHCP server, ignoring: %m");
1a04db0f
LP
1274 }
1275
1a04db0f
LP
1276 if (link->network->dhcp_server_emit_ntp) {
1277
4f5f911e 1278 if (link->network->n_dhcp_server_ntp > 0)
1a04db0f 1279 r = sd_dhcp_server_set_ntp(link->dhcp_server, link->network->dhcp_server_ntp, link->network->n_dhcp_server_ntp);
4f5f911e
LP
1280 else {
1281 if (!acquired_uplink)
1282 uplink = manager_find_uplink(link->manager, link);
1283
1284 if (!uplink) {
1285 log_link_debug(link, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1286 r = 0;
1287 } else
a380b2d4 1288 r = link_push_uplink_ntp_to_dhcp_server(uplink, link->dhcp_server);
4f5f911e
LP
1289
1290 }
1291 if (r < 0)
1292 log_link_warning_errno(link, r, "Failed to set NTP server for DHCP server, ignoring: %m");
1a04db0f
LP
1293 }
1294
77ff6022 1295 r = sd_dhcp_server_set_emit_router(link->dhcp_server, link->network->dhcp_server_emit_router);
fc95c359
YW
1296 if (r < 0)
1297 return log_link_warning_errno(link, r, "Failed to set router emission for DHCP server: %m");
77ff6022 1298
8eb9058d
LP
1299 if (link->network->dhcp_server_emit_timezone) {
1300 _cleanup_free_ char *buffer = NULL;
0ab8a1b6 1301 const char *tz = NULL;
8eb9058d
LP
1302
1303 if (link->network->dhcp_server_timezone)
1304 tz = link->network->dhcp_server_timezone;
1305 else {
1306 r = get_timezone(&buffer);
1307 if (r < 0)
1308 log_warning_errno(r, "Failed to determine timezone: %m");
1309 else
1310 tz = buffer;
1311 }
1312
1313 if (tz) {
1314 r = sd_dhcp_server_set_timezone(link->dhcp_server, tz);
1315 if (r < 0)
1316 return r;
1317 }
1318 }
7da7340a
RJ
1319 if (!sd_dhcp_server_is_running(link->dhcp_server)) {
1320 r = sd_dhcp_server_start(link->dhcp_server);
1321 if (r < 0) {
1322 log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m");
8eb9058d 1323
7da7340a 1324 link_enter_failed(link);
d4cdbea5 1325
7da7340a
RJ
1326 return 0;
1327 }
f3ef324d 1328 }
d4cdbea5 1329
6a7a4e4d 1330 log_link_debug(link, "Offering DHCPv4 leases");
d4cdbea5
TG
1331 }
1332
c42ff3a1
WKI
1333 if (link->address_messages == 0) {
1334 link->addresses_configured = true;
6aa5773b 1335 link_check_ready(link);
c42ff3a1 1336 } else
6a7a4e4d 1337 log_link_debug(link, "Setting addresses");
431ca2ce 1338
f882c247
TG
1339 return 0;
1340}
1341
13b498f9 1342static int link_set_bridge_vlan(Link *link) {
86e2be7b 1343 int r;
13b498f9
TJ
1344
1345 r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap);
1346 if (r < 0)
1347 log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m");
1348
1349 return r;
1350}
1351
a60a720c 1352static int link_set_proxy_arp(Link *link) {
23d8b221
SS
1353 int r;
1354
1355 if (!link_proxy_arp_enabled(link))
1356 return 0;
1357
62e021a9 1358 r = sysctl_write_ip_property_boolean(AF_INET, link->ifname, "proxy_arp", link->network->proxy_arp > 0);
23d8b221
SS
1359 if (r < 0)
1360 log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m");
1361
1362 return 0;
1363}
1364
55dc8c4a
YW
1365static int link_configure_after_setting_mtu(Link *link);
1366
302a796f 1367static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
4f882b2a
TG
1368 int r;
1369
1370 assert(m);
1371 assert(link);
1372 assert(link->ifname);
1373
55dc8c4a
YW
1374 link->setting_mtu = false;
1375
5da8149f 1376 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
4f882b2a
TG
1377 return 1;
1378
1c4baffc 1379 r = sd_netlink_message_get_errno(m);
55dc8c4a 1380 if (r < 0) {
a2fae7bb 1381 log_link_warning_errno(link, r, "Could not set MTU: %m");
55dc8c4a
YW
1382 return 1;
1383 }
1384
1385 log_link_debug(link, "Setting MTU done.");
1386
bd08ce56 1387 if (link->state == LINK_STATE_INITIALIZED)
55dc8c4a 1388 (void) link_configure_after_setting_mtu(link);
4f882b2a
TG
1389
1390 return 1;
1391}
1392
933c70a0 1393int link_set_mtu(Link *link, uint32_t mtu) {
4afd3348 1394 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
4f882b2a
TG
1395 int r;
1396
1397 assert(link);
1398 assert(link->manager);
1399 assert(link->manager->rtnl);
1400
40288ece
YW
1401 if (mtu == 0 || link->setting_mtu)
1402 return 0;
1403
933c70a0 1404 if (link->mtu == mtu)
ee493106
YW
1405 return 0;
1406
6a7a4e4d 1407 log_link_debug(link, "Setting MTU: %" PRIu32, mtu);
4f882b2a 1408
6a7a4e4d
LP
1409 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1410 if (r < 0)
1411 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
4f882b2a 1412
44b598a1 1413 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
b102cdca 1414 * for this interface, then disable IPv6 else enable it. */
44b598a1
SS
1415 (void) link_enable_ipv6(link);
1416
1417 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
72c2500b
YW
1418 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1419 if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) {
44b598a1
SS
1420
1421 log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as "
1422 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m");
1423
72c2500b 1424 mtu = IPV6_MIN_MTU;
44b598a1
SS
1425 }
1426
1c4baffc 1427 r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu);
6a7a4e4d
LP
1428 if (r < 0)
1429 return log_link_error_errno(link, r, "Could not append MTU: %m");
4f882b2a 1430
302a796f
YW
1431 r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler,
1432 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1433 if (r < 0)
1434 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
4f882b2a 1435
ae941762 1436 link_ref(link);
1046bf9b 1437 link->setting_mtu = true;
b226d99b 1438
4f882b2a
TG
1439 return 0;
1440}
1441
f6fcc1c2
YW
1442static bool link_reduces_vlan_mtu(Link *link) {
1443 /* See netif_reduces_vlan_mtu() in kernel. */
1444 return streq_ptr(link->kind, "macsec");
1445}
1446
1447static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) {
1448 uint32_t mtu = 0;
1449 NetDev *dev;
1450 Iterator i;
1451
1452 HASHMAP_FOREACH(dev, link->network->stacked_netdevs, i)
1453 if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0)
1454 /* See vlan_dev_change_mtu() in kernel. */
1455 mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu);
1456
1457 else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu)
1458 /* See macvlan_change_mtu() in kernel. */
1459 mtu = dev->mtu;
1460
1461 return mtu;
1462}
1463
1464static int link_configure_mtu(Link *link) {
1465 uint32_t mtu;
1466
1467 assert(link);
1468 assert(link->network);
1469
1470 if (link->network->mtu > 0)
1471 return link_set_mtu(link, link->network->mtu);
1472
1473 mtu = link_get_requested_mtu_by_stacked_netdevs(link);
1474 if (link->mtu >= mtu)
1475 return 0;
1476
1477 log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. "
1478 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1479 link->mtu, mtu);
1480
1481 return link_set_mtu(link, mtu);
1482}
1483
302a796f 1484static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
99d2baa2
SS
1485 int r;
1486
1487 assert(m);
1488 assert(link);
1489 assert(link->ifname);
1490
1491 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1492 return 1;
1493
1494 r = sd_netlink_message_get_errno(m);
1495 if (r < 0)
1496 log_link_warning_errno(link, r, "Could not set link flags: %m");
1497
1498 return 1;
1499}
1500
1501static int link_set_flags(Link *link) {
1502 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1503 unsigned ifi_change = 0;
1504 unsigned ifi_flags = 0;
1505 int r;
1506
1507 assert(link);
1508 assert(link->manager);
1509 assert(link->manager->rtnl);
1510
1511 if (link->flags & IFF_LOOPBACK)
1512 return 0;
1513
1514 if (!link->network)
1515 return 0;
1516
866e6b7a 1517 if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0)
99d2baa2
SS
1518 return 0;
1519
1520 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1521 if (r < 0)
1522 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1523
1524 if (link->network->arp >= 0) {
1525 ifi_change |= IFF_NOARP;
e6ebebbe
SS
1526 SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
1527 }
1528
1529 if (link->network->multicast >= 0) {
1530 ifi_change |= IFF_MULTICAST;
1531 SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
99d2baa2
SS
1532 }
1533
866e6b7a
SS
1534 if (link->network->allmulticast >= 0) {
1535 ifi_change |= IFF_ALLMULTI;
1536 SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
1537 }
1538
99d2baa2
SS
1539 r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
1540 if (r < 0)
1541 return log_link_error_errno(link, r, "Could not set link flags: %m");
1542
302a796f
YW
1543 r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler,
1544 link_netlink_destroy_callback, link);
99d2baa2
SS
1545 if (r < 0)
1546 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1547
1548 link_ref(link);
1549
1550 return 0;
1551}
1552
e7ab854c
TG
1553static int link_acquire_ipv6_conf(Link *link) {
1554 int r;
1555
1556 assert(link);
1557
e7ab854c 1558 if (link_ipv6_accept_ra_enabled(link)) {
1e7a0e21 1559 assert(link->ndisc);
e7ab854c
TG
1560
1561 log_link_debug(link, "Discovering IPv6 routers");
1562
1e7a0e21 1563 r = sd_ndisc_start(link->ndisc);
63348d13 1564 if (r < 0 && r != -EBUSY)
e7ab854c
TG
1565 return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m");
1566 }
1567
7465dd22
PF
1568 if (link_radv_enabled(link)) {
1569 assert(link->radv);
1570 assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0);
1571
1572 log_link_debug(link, "Starting IPv6 Router Advertisements");
1573
1574 r = sd_radv_start(link->radv);
1575 if (r < 0 && r != -EBUSY)
1576 return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m");
1577 }
1578
10752343
PF
1579 (void) dhcp6_request_prefix_delegation(link);
1580
e7ab854c
TG
1581 return 0;
1582}
1583
6fc25497 1584static int link_acquire_ipv4_conf(Link *link) {
ff254138
TG
1585 int r;
1586
1587 assert(link);
ff254138
TG
1588 assert(link->manager);
1589 assert(link->manager->event);
1590
910feb78 1591 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) {
eb34d4af 1592 assert(link->ipv4ll);
ff254138 1593
6a7a4e4d 1594 log_link_debug(link, "Acquiring IPv4 link-local address");
5c1d3fc9
UTL
1595
1596 r = sd_ipv4ll_start(link->ipv4ll);
6a7a4e4d
LP
1597 if (r < 0)
1598 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
5c1d3fc9
UTL
1599 }
1600
78c958f8 1601 if (link_dhcp4_enabled(link)) {
eb34d4af 1602 assert(link->dhcp_client);
ff254138 1603
6a7a4e4d 1604 log_link_debug(link, "Acquiring DHCPv4 lease");
ab47d620 1605
5c1d3fc9 1606 r = sd_dhcp_client_start(link->dhcp_client);
6a7a4e4d
LP
1607 if (r < 0)
1608 return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m");
5c1d3fc9 1609 }
ff254138 1610
6fc25497
SS
1611 return 0;
1612}
1613
1614static int link_acquire_conf(Link *link) {
1615 int r;
1616
1617 assert(link);
1618
1619 r = link_acquire_ipv4_conf(link);
1620 if (r < 0)
1621 return r;
1622
3f7cc080 1623 if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) {
6fc25497
SS
1624 r = link_acquire_ipv6_conf(link);
1625 if (r < 0)
1626 return r;
1627 }
1628
7272b25e
LP
1629 if (link_lldp_emit_enabled(link)) {
1630 r = link_lldp_emit_start(link);
8e1ad1ea
LP
1631 if (r < 0)
1632 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
1633 }
1634
ff254138
TG
1635 return 0;
1636}
1637
a61bb41c 1638bool link_has_carrier(Link *link) {
deb2e523
TG
1639 /* see Documentation/networking/operstates.txt in the kernel sources */
1640
a61bb41c 1641 if (link->kernel_operstate == IF_OPER_UP)
deb2e523
TG
1642 return true;
1643
a61bb41c 1644 if (link->kernel_operstate == IF_OPER_UNKNOWN)
deb2e523 1645 /* operstate may not be implemented, so fall back to flags */
b26ea308
YW
1646 if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) &&
1647 !FLAGS_SET(link->flags, IFF_DORMANT))
deb2e523
TG
1648 return true;
1649
1650 return false;
1651}
1652
0e2fdb83
SS
1653static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1654 int r;
1655
1656 assert(link);
1657
1658 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1659 return 1;
1660
1661 r = sd_netlink_message_get_errno(m);
1662 if (r < 0)
1663 log_link_warning_errno(link, r, "Could not set address genmode for interface: %m");
1664
1665 return 1;
1666}
1667
1668static int link_configure_addrgen_mode(Link *link) {
1669 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1670 uint8_t ipv6ll_mode;
1671 int r;
1672
1673 assert(link);
1674 assert(link->network);
1675 assert(link->manager);
1676 assert(link->manager->rtnl);
1677
9f6e82e6
YW
1678 if (!socket_ipv6_is_supported())
1679 return 0;
1680
0e2fdb83
SS
1681 log_link_debug(link, "Setting address genmode for link");
1682
1683 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1684 if (r < 0)
1685 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
1686
1687 r = sd_netlink_message_open_container(req, IFLA_AF_SPEC);
1688 if (r < 0)
1689 return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m");
1690
1691 r = sd_netlink_message_open_container(req, AF_INET6);
1692 if (r < 0)
1693 return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m");
1694
1695 if (!link_ipv6ll_enabled(link))
1696 ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE;
826a3602 1697 else if (sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL) < 0)
0e2fdb83 1698 /* The file may not exist. And event if it exists, when stable_secret is unset,
826a3602
YW
1699 * reading the file fails with EIO. */
1700 ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64;
1701 else
1702 ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
0e2fdb83
SS
1703
1704 r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode);
1705 if (r < 0)
1706 return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1707
1708 r = sd_netlink_message_close_container(req);
1709 if (r < 0)
1710 return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m");
1711
1712 r = sd_netlink_message_close_container(req);
1713 if (r < 0)
1714 return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m");
1715
1716 r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler,
1717 link_netlink_destroy_callback, link);
1718 if (r < 0)
1719 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
1720
1721 link_ref(link);
1722
1723 return 0;
1724}
1725
302a796f 1726static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
dd3efc09
TG
1727 int r;
1728
1746cf2a
TG
1729 assert(link);
1730
5da8149f 1731 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1746cf2a
TG
1732 return 1;
1733
1c4baffc 1734 r = sd_netlink_message_get_errno(m);
6a7a4e4d 1735 if (r < 0)
26d6b214 1736 /* we warn but don't fail the link, as it may be brought up later */
a2fae7bb 1737 log_link_warning_errno(link, r, "Could not bring up interface: %m");
45ad2c13 1738
f882c247
TG
1739 return 1;
1740}
1741
1cc84f3b 1742static int link_up(Link *link) {
4afd3348 1743 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
1744 int r;
1745
f882c247 1746 assert(link);
c106cc36 1747 assert(link->network);
f882c247
TG
1748 assert(link->manager);
1749 assert(link->manager->rtnl);
1750
6a7a4e4d 1751 log_link_debug(link, "Bringing link up");
449f7554 1752
6a7a4e4d
LP
1753 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
1754 if (r < 0)
1755 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
f579559b 1756
2b2d8603 1757 /* set it free if not enslaved with networkd */
6cb955c6 1758 if (!link->network->bridge && !link->network->bond && !link->network->vrf) {
2b2d8603
TY
1759 r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0);
1760 if (r < 0)
1761 return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m");
1762 }
1763
5d4795f3 1764 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
6a7a4e4d
LP
1765 if (r < 0)
1766 return log_link_error_errno(link, r, "Could not set link flags: %m");
fc25d7f8 1767
c106cc36 1768 if (link->network->mac) {
1c4baffc 1769 r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
6a7a4e4d
LP
1770 if (r < 0)
1771 return log_link_error_errno(link, r, "Could not set MAC address: %m");
c106cc36
TG
1772 }
1773
302a796f
YW
1774 r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
1775 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1776 if (r < 0)
1777 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
f579559b 1778
b226d99b
TG
1779 link_ref(link);
1780
f882c247
TG
1781 return 0;
1782}
1783
302a796f 1784static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
0d4ad91d
AR
1785 int r;
1786
1787 assert(link);
1788
1789 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1790 return 1;
1791
1c4baffc 1792 r = sd_netlink_message_get_errno(m);
0d4ad91d 1793 if (r < 0)
a2fae7bb 1794 log_link_warning_errno(link, r, "Could not bring down interface: %m");
0d4ad91d
AR
1795
1796 return 1;
1797}
1798
8e54db83 1799int link_down(Link *link, link_netlink_message_handler_t callback) {
4afd3348 1800 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
0d4ad91d
AR
1801 int r;
1802
1803 assert(link);
1804 assert(link->manager);
1805 assert(link->manager->rtnl);
1806
6a7a4e4d 1807 log_link_debug(link, "Bringing link down");
0d4ad91d
AR
1808
1809 r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1810 RTM_SETLINK, link->ifindex);
6a7a4e4d
LP
1811 if (r < 0)
1812 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
0d4ad91d
AR
1813
1814 r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP);
6a7a4e4d
LP
1815 if (r < 0)
1816 return log_link_error_errno(link, r, "Could not set link flags: %m");
0d4ad91d 1817
8e54db83
YW
1818 r = netlink_call_async(link->manager->rtnl, NULL, req,
1819 callback ?: link_down_handler,
302a796f 1820 link_netlink_destroy_callback, link);
6a7a4e4d
LP
1821 if (r < 0)
1822 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
0d4ad91d
AR
1823
1824 link_ref(link);
1825
1826 return 0;
1827}
1828
1829static int link_handle_bound_to_list(Link *link) {
1830 Link *l;
1831 Iterator i;
1832 int r;
1833 bool required_up = false;
1834 bool link_is_up = false;
1835
1836 assert(link);
1837
1838 if (hashmap_isempty(link->bound_to_links))
1839 return 0;
1840
1841 if (link->flags & IFF_UP)
1842 link_is_up = true;
1843
1844 HASHMAP_FOREACH (l, link->bound_to_links, i)
1845 if (link_has_carrier(l)) {
1846 required_up = true;
1847 break;
1848 }
1849
1850 if (!required_up && link_is_up) {
8e54db83 1851 r = link_down(link, NULL);
0d4ad91d
AR
1852 if (r < 0)
1853 return r;
1854 } else if (required_up && !link_is_up) {
1855 r = link_up(link);
1856 if (r < 0)
1857 return r;
1858 }
1859
1860 return 0;
1861}
1862
1863static int link_handle_bound_by_list(Link *link) {
1864 Iterator i;
1865 Link *l;
1866 int r;
1867
1868 assert(link);
1869
1870 if (hashmap_isempty(link->bound_by_links))
1871 return 0;
1872
1873 HASHMAP_FOREACH (l, link->bound_by_links, i) {
1874 r = link_handle_bound_to_list(l);
1875 if (r < 0)
1876 return r;
1877 }
1878
1879 return 0;
1880}
1881
1882static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
1883 int r;
1884
1885 assert(link);
1886 assert(carrier);
1887
1888 if (link == carrier)
1889 return 0;
1890
1891 if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
1892 return 0;
1893
1894 r = hashmap_ensure_allocated(h, NULL);
1895 if (r < 0)
1896 return r;
1897
1898 r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
1899 if (r < 0)
1900 return r;
1901
1902 return 0;
1903}
1904
1905static int link_new_bound_by_list(Link *link) {
1906 Manager *m;
1907 Link *carrier;
1908 Iterator i;
1909 int r;
1910 bool list_updated = false;
1911
1912 assert(link);
1913 assert(link->manager);
1914
1915 m = link->manager;
1916
b295beea 1917 HASHMAP_FOREACH(carrier, m->links, i) {
0d4ad91d
AR
1918 if (!carrier->network)
1919 continue;
1920
1921 if (strv_isempty(carrier->network->bind_carrier))
1922 continue;
1923
1924 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) {
1925 r = link_put_carrier(link, carrier, &link->bound_by_links);
1926 if (r < 0)
1927 return r;
1928
1929 list_updated = true;
1930 }
1931 }
1932
1933 if (list_updated)
84de38c5 1934 link_dirty(link);
0d4ad91d 1935
b295beea 1936 HASHMAP_FOREACH(carrier, link->bound_by_links, i) {
0d4ad91d
AR
1937 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
1938 if (r < 0)
1939 return r;
1940
84de38c5 1941 link_dirty(carrier);
0d4ad91d
AR
1942 }
1943
1944 return 0;
1945}
1946
1947static int link_new_bound_to_list(Link *link) {
1948 Manager *m;
1949 Link *carrier;
1950 Iterator i;
1951 int r;
1952 bool list_updated = false;
1953
1954 assert(link);
1955 assert(link->manager);
1956
1957 if (!link->network)
1958 return 0;
1959
1960 if (strv_isempty(link->network->bind_carrier))
1961 return 0;
1962
1963 m = link->manager;
1964
1965 HASHMAP_FOREACH (carrier, m->links, i) {
1966 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) {
1967 r = link_put_carrier(link, carrier, &link->bound_to_links);
1968 if (r < 0)
1969 return r;
1970
1971 list_updated = true;
1972 }
1973 }
1974
1975 if (list_updated)
84de38c5 1976 link_dirty(link);
0d4ad91d
AR
1977
1978 HASHMAP_FOREACH (carrier, link->bound_to_links, i) {
1979 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
1980 if (r < 0)
1981 return r;
1982
84de38c5 1983 link_dirty(carrier);
0d4ad91d
AR
1984 }
1985
1986 return 0;
1987}
1988
1989static int link_new_carrier_maps(Link *link) {
1990 int r;
1991
1992 r = link_new_bound_by_list(link);
1993 if (r < 0)
1994 return r;
1995
1996 r = link_handle_bound_by_list(link);
1997 if (r < 0)
1998 return r;
1999
2000 r = link_new_bound_to_list(link);
2001 if (r < 0)
2002 return r;
2003
2004 r = link_handle_bound_to_list(link);
2005 if (r < 0)
2006 return r;
2007
2008 return 0;
2009}
2010
2011static void link_free_bound_to_list(Link *link) {
2012 Link *bound_to;
2013 Iterator i;
2014
2015 HASHMAP_FOREACH (bound_to, link->bound_to_links, i) {
2016 hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex));
2017
2018 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
84de38c5 2019 link_dirty(bound_to);
0d4ad91d
AR
2020 }
2021
2022 return;
2023}
2024
2025static void link_free_bound_by_list(Link *link) {
2026 Link *bound_by;
2027 Iterator i;
2028
2029 HASHMAP_FOREACH (bound_by, link->bound_by_links, i) {
2030 hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex));
2031
2032 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
84de38c5 2033 link_dirty(bound_by);
0d4ad91d
AR
2034 link_handle_bound_to_list(bound_by);
2035 }
2036 }
2037
2038 return;
2039}
2040
2041static void link_free_carrier_maps(Link *link) {
2042 bool list_updated = false;
2043
2044 assert(link);
2045
2046 if (!hashmap_isempty(link->bound_to_links)) {
2047 link_free_bound_to_list(link);
2048 list_updated = true;
2049 }
2050
2051 if (!hashmap_isempty(link->bound_by_links)) {
2052 link_free_bound_by_list(link);
2053 list_updated = true;
2054 }
2055
2056 if (list_updated)
84de38c5 2057 link_dirty(link);
0d4ad91d
AR
2058
2059 return;
2060}
2061
5f707e12
YW
2062static int link_append_to_master(Link *link, NetDev *netdev) {
2063 Link *master;
2064 int r;
2065
2066 assert(link);
2067 assert(netdev);
2068
2069 r = link_get(link->manager, netdev->ifindex, &master);
2070 if (r < 0)
2071 return r;
2072
2073 r = set_ensure_allocated(&master->slaves, NULL);
2074 if (r < 0)
2075 return r;
2076
2077 r = set_put(master->slaves, link);
2078 if (r < 0)
2079 return r;
2080
2081 link_ref(link);
2082 return 0;
2083}
2084
2085static void link_drop_from_master(Link *link, NetDev *netdev) {
2086 Link *master;
2087
2088 assert(link);
2089
2090 if (!link->manager || !netdev)
2091 return;
2092
2093 if (link_get(link->manager, netdev->ifindex, &master) < 0)
2094 return;
2095
2096 link_unref(set_remove(master->slaves, link));
2097}
2098
2099static void link_detach_from_manager(Link *link) {
2100 if (!link || !link->manager)
2101 return;
2102
2103 link_unref(set_remove(link->manager->links_requesting_uuid, link));
2104 link_clean(link);
2105
2106 /* The following must be called at last. */
2107 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
2108 link_unref(link);
2109}
2110
0d4ad91d
AR
2111void link_drop(Link *link) {
2112 if (!link || link->state == LINK_STATE_LINGER)
2113 return;
2114
2115 link_set_state(link, LINK_STATE_LINGER);
2116
2117 link_free_carrier_maps(link);
2118
5f707e12
YW
2119 if (link->network) {
2120 link_drop_from_master(link, link->network->bridge);
2121 link_drop_from_master(link, link->network->bond);
2122 }
2123
6a7a4e4d 2124 log_link_debug(link, "Link removed");
0d4ad91d 2125
db2f8a2e 2126 (void) unlink(link->state_file);
c4397d94 2127 link_detach_from_manager(link);
0d4ad91d
AR
2128}
2129
3f265037 2130static int link_joined(Link *link) {
f882c247
TG
2131 int r;
2132
ef1ba606 2133 assert(link);
f5be5601 2134 assert(link->network);
dd3efc09 2135
0d4ad91d
AR
2136 if (!hashmap_isempty(link->bound_to_links)) {
2137 r = link_handle_bound_to_list(link);
2138 if (r < 0)
2139 return r;
2140 } else if (!(link->flags & IFF_UP)) {
505f8da7
TG
2141 r = link_up(link);
2142 if (r < 0) {
2143 link_enter_failed(link);
2144 return r;
2145 }
ef1ba606 2146 }
f882c247 2147
9ed794a3 2148 if (link->network->bridge) {
e1853b00 2149 r = link_set_bridge(link);
6a7a4e4d
LP
2150 if (r < 0)
2151 log_link_error_errno(link, r, "Could not set bridge message: %m");
45e11abf
YW
2152
2153 r = link_append_to_master(link, link->network->bridge);
2154 if (r < 0)
2155 log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m");
e1853b00
SS
2156 }
2157
eb64b435 2158 if (link->network->bond) {
7fcee284 2159 r = link_set_bond(link);
eb64b435
SS
2160 if (r < 0)
2161 log_link_error_errno(link, r, "Could not set bond message: %m");
959f65d3 2162
45e11abf 2163 r = link_append_to_master(link, link->network->bond);
959f65d3
YW
2164 if (r < 0)
2165 log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m");
eb64b435
SS
2166 }
2167
ffff9abe
TJ
2168 if (link->network->use_br_vlan &&
2169 (link->network->bridge || streq_ptr("bridge", link->kind))) {
13b498f9
TJ
2170 r = link_set_bridge_vlan(link);
2171 if (r < 0)
2172 log_link_error_errno(link, r, "Could not set bridge vlan: %m");
2173 }
2174
c1835a42
RM
2175 /* Skip setting up addresses until it gets carrier,
2176 or it would try to set addresses twice,
2177 which is bad for non-idempotent steps. */
dad2d78e 2178 if (!link_has_carrier(link) && !link->network->configure_without_carrier)
c1835a42
RM
2179 return 0;
2180
289e6774 2181 return link_request_set_addresses(link);
02b59d57
TG
2182}
2183
302a796f 2184static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
02b59d57
TG
2185 int r;
2186
1746cf2a 2187 assert(link);
ef1ba606 2188 assert(link->network);
34bf3c00 2189 assert(link->enslaving > 0);
02b59d57 2190
313cefa1 2191 link->enslaving--;
52433f6b 2192
5da8149f 2193 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
02b59d57
TG
2194 return 1;
2195
1c4baffc 2196 r = sd_netlink_message_get_errno(m);
856f962c 2197 if (r < 0 && r != -EEXIST) {
a2fae7bb 2198 log_link_error_errno(link, r, "Could not join netdev: %m");
ef1ba606
TG
2199 link_enter_failed(link);
2200 return 1;
ba179154 2201 } else
6a7a4e4d 2202 log_link_debug(link, "Joined netdev");
02b59d57 2203
34bf3c00 2204 if (link->enslaving == 0) {
3f265037 2205 link_joined(link);
34bf3c00 2206 }
02b59d57
TG
2207
2208 return 1;
2209}
2210
3f265037 2211static int link_enter_join_netdev(Link *link) {
6a0a2f86 2212 NetDev *netdev;
672682a6 2213 Iterator i;
02b59d57
TG
2214 int r;
2215
2216 assert(link);
2217 assert(link->network);
bd08ce56 2218 assert(link->state == LINK_STATE_INITIALIZED);
02b59d57 2219
289e6774 2220 link_set_state(link, LINK_STATE_CONFIGURING);
02b59d57 2221
84de38c5 2222 link_dirty(link);
34bf3c00 2223 link->enslaving = 0;
02b59d57 2224
d9c67ea1 2225 if (link->network->bond) {
cbff7170
TJ
2226 if (link->network->bond->state == NETDEV_STATE_READY &&
2227 link->network->bond->ifindex == link->master_ifindex)
2228 return link_joined(link);
2229
f2341e0a
LP
2230 log_struct(LOG_DEBUG,
2231 LOG_LINK_INTERFACE(link),
2232 LOG_NETDEV_INTERFACE(link->network->bond),
a1230ff9 2233 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname));
f2341e0a 2234
34bf3c00
YW
2235 link->enslaving++;
2236
f2341e0a 2237 r = netdev_join(link->network->bond, link, netdev_join_handler);
52433f6b 2238 if (r < 0) {
f2341e0a
LP
2239 log_struct_errno(LOG_WARNING, r,
2240 LOG_LINK_INTERFACE(link),
2241 LOG_NETDEV_INTERFACE(link->network->bond),
a1230ff9 2242 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname));
52433f6b
TG
2243 link_enter_failed(link);
2244 return r;
2245 }
0ad6148e
MO
2246 }
2247
d9c67ea1 2248 if (link->network->bridge) {
f2341e0a
LP
2249 log_struct(LOG_DEBUG,
2250 LOG_LINK_INTERFACE(link),
2251 LOG_NETDEV_INTERFACE(link->network->bridge),
a1230ff9 2252 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname));
f2341e0a 2253
34bf3c00
YW
2254 link->enslaving++;
2255
f2341e0a 2256 r = netdev_join(link->network->bridge, link, netdev_join_handler);
0ad6148e 2257 if (r < 0) {
f2341e0a
LP
2258 log_struct_errno(LOG_WARNING, r,
2259 LOG_LINK_INTERFACE(link),
2260 LOG_NETDEV_INTERFACE(link->network->bridge),
a1230ff9 2261 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname));
0ad6148e
MO
2262 link_enter_failed(link);
2263 return r;
2264 }
52433f6b
TG
2265 }
2266
6cb955c6
AR
2267 if (link->network->vrf) {
2268 log_struct(LOG_DEBUG,
2269 LOG_LINK_INTERFACE(link),
2270 LOG_NETDEV_INTERFACE(link->network->vrf),
a1230ff9
ZJS
2271 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname));
2272
34bf3c00
YW
2273 link->enslaving++;
2274
6cb955c6
AR
2275 r = netdev_join(link->network->vrf, link, netdev_join_handler);
2276 if (r < 0) {
2277 log_struct_errno(LOG_WARNING, r,
2278 LOG_LINK_INTERFACE(link),
2279 LOG_NETDEV_INTERFACE(link->network->vrf),
a1230ff9 2280 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname));
6cb955c6
AR
2281 link_enter_failed(link);
2282 return r;
2283 }
6cb955c6
AR
2284 }
2285
6a0a2f86 2286 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
7951dea2 2287
2b6db913
YW
2288 if (netdev->ifindex > 0)
2289 /* Assume already enslaved. */
a63e5daa 2290 continue;
a63e5daa 2291
7033af49
YW
2292 if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED)
2293 continue;
2294
f2341e0a
LP
2295 log_struct(LOG_DEBUG,
2296 LOG_LINK_INTERFACE(link),
2297 LOG_NETDEV_INTERFACE(netdev),
a1230ff9 2298 LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname));
f2341e0a 2299
34bf3c00
YW
2300 link->enslaving++;
2301
f2341e0a 2302 r = netdev_join(netdev, link, netdev_join_handler);
7951dea2 2303 if (r < 0) {
f2341e0a
LP
2304 log_struct_errno(LOG_WARNING, r,
2305 LOG_LINK_INTERFACE(link),
2306 LOG_NETDEV_INTERFACE(netdev),
a1230ff9 2307 LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname));
326cb406
SS
2308 link_enter_failed(link);
2309 return r;
2310 }
326cb406
SS
2311 }
2312
34bf3c00
YW
2313 if (link->enslaving == 0)
2314 return link_joined(link);
2315
ef1ba606
TG
2316 return 0;
2317}
2318
769d324c 2319static int link_set_ipv4_forward(Link *link) {
5a8bcb67
LP
2320 int r;
2321
765afd5c 2322 if (!link_ipv4_forward_enabled(link))
15dee3f0
LP
2323 return 0;
2324
765afd5c
LP
2325 /* We propagate the forwarding flag from one interface to the
2326 * global setting one way. This means: as long as at least one
2327 * interface was configured at any time that had IP forwarding
2328 * enabled the setting will stay on for good. We do this
2329 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2330 * somewhat in sync (see below). */
15dee3f0 2331
62e021a9 2332 r = sysctl_write_ip_property(AF_INET, NULL, "ip_forward", "1");
eb3da901 2333 if (r < 0)
765afd5c 2334 log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
43c6d5ab 2335
769d324c
LP
2336 return 0;
2337}
2338
2339static int link_set_ipv6_forward(Link *link) {
769d324c
LP
2340 int r;
2341
765afd5c 2342 if (!link_ipv6_forward_enabled(link))
8add5f79
NO
2343 return 0;
2344
61233823 2345 /* On Linux, the IPv6 stack does not know a per-interface
765afd5c
LP
2346 * packet forwarding setting: either packet forwarding is on
2347 * for all, or off for all. We hence don't bother with a
2348 * per-interface setting, but simply propagate the interface
2349 * flag, if it is set, to the global flag, one-way. Note that
2350 * while IPv4 would allow a per-interface flag, we expose the
2351 * same behaviour there and also propagate the setting from
2352 * one to all, to keep things simple (see above). */
15dee3f0 2353
62e021a9 2354 r = sysctl_write_ip_property(AF_INET6, "all", "forwarding", "1");
eb3da901 2355 if (r < 0)
765afd5c 2356 log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m");
5a8bcb67
LP
2357
2358 return 0;
2359}
2360
49092e22 2361static int link_set_ipv6_privacy_extensions(Link *link) {
1f0d9695 2362 IPv6PrivacyExtensions s;
49092e22
SS
2363 int r;
2364
1f0d9695 2365 s = link_ipv6_privacy_extensions(link);
66a6bd68 2366 if (s < 0)
49092e22
SS
2367 return 0;
2368
62e021a9 2369 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "use_tempaddr", (int) link->network->ipv6_privacy_extensions);
eb3da901 2370 if (r < 0)
49092e22
SS
2371 log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m");
2372
2373 return 0;
2374}
2375
4f2e437a 2376static int link_set_ipv6_accept_ra(Link *link) {
4f2e437a
SS
2377 int r;
2378
2379 /* Make this a NOP if IPv6 is not available */
2380 if (!socket_ipv6_is_supported())
2381 return 0;
2382
2383 if (link->flags & IFF_LOOPBACK)
2384 return 0;
2385
d68e2e59
LP
2386 if (!link->network)
2387 return 0;
2388
62e021a9 2389 r = sysctl_write_ip_property(AF_INET6, link->ifname, "accept_ra", "0");
eb3da901 2390 if (r < 0)
fe307276 2391 log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m");
4f2e437a
SS
2392
2393 return 0;
2394}
2395
8749cbcd 2396static int link_set_ipv6_dad_transmits(Link *link) {
8749cbcd
SS
2397 int r;
2398
2399 /* Make this a NOP if IPv6 is not available */
2400 if (!socket_ipv6_is_supported())
2401 return 0;
2402
2403 if (link->flags & IFF_LOOPBACK)
2404 return 0;
2405
d68e2e59
LP
2406 if (!link->network)
2407 return 0;
2408
8749cbcd
SS
2409 if (link->network->ipv6_dad_transmits < 0)
2410 return 0;
2411
62e021a9 2412 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "dad_transmits", link->network->ipv6_dad_transmits);
eb3da901 2413 if (r < 0)
8749cbcd 2414 log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m");
8749cbcd
SS
2415
2416 return 0;
2417}
2418
b69c3180 2419static int link_set_ipv6_hop_limit(Link *link) {
b69c3180
SS
2420 int r;
2421
2422 /* Make this a NOP if IPv6 is not available */
2423 if (!socket_ipv6_is_supported())
2424 return 0;
2425
2426 if (link->flags & IFF_LOOPBACK)
2427 return 0;
2428
d68e2e59
LP
2429 if (!link->network)
2430 return 0;
2431
b69c3180
SS
2432 if (link->network->ipv6_hop_limit < 0)
2433 return 0;
2434
62e021a9 2435 r = sysctl_write_ip_property_int(AF_INET6, link->ifname, "hop_limit", link->network->ipv6_hop_limit);
eb3da901 2436 if (r < 0)
b69c3180 2437 log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m");
b69c3180
SS
2438
2439 return 0;
2440}
2441
11102cba 2442static int link_set_ipv6_mtu(Link *link) {
11102cba
SS
2443 int r;
2444
2445 /* Make this a NOP if IPv6 is not available */
2446 if (!socket_ipv6_is_supported())
2447 return 0;
2448
2449 if (link->flags & IFF_LOOPBACK)
2450 return 0;
2451
2452 if (link->network->ipv6_mtu == 0)
2453 return 0;
2454
62e021a9 2455 r = sysctl_write_ip_property_uint32(AF_INET6, link->ifname, "mtu", link->network->ipv6_mtu);
11102cba
SS
2456 if (r < 0)
2457 log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m");
2458
2459 return 0;
2460}
2461
30226d27
TJ
2462static bool link_is_static_address_configured(Link *link, Address *address) {
2463 Address *net_address;
2464
2465 assert(link);
2466 assert(address);
2467
2468 if (!link->network)
2469 return false;
2470
2471 LIST_FOREACH(addresses, net_address, link->network->static_addresses)
2472 if (address_equal(net_address, address))
2473 return true;
2474
2475 return false;
2476}
2477
7ecf0c3e
TJ
2478static bool link_is_static_route_configured(Link *link, Route *route) {
2479 Route *net_route;
2480
2481 assert(link);
2482 assert(route);
2483
2484 if (!link->network)
2485 return false;
2486
2487 LIST_FOREACH(routes, net_route, link->network->static_routes)
2488 if (route_equal(net_route, route))
2489 return true;
2490
2491 return false;
2492}
2493
db51778f
YW
2494static bool link_address_is_dynamic(Link *link, Address *address) {
2495 Route *route;
2496 Iterator i;
2497
2498 assert(link);
2499 assert(address);
2500
2501 if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME)
2502 return true;
2503
2504 /* Even when the address is leased from a DHCP server, networkd assign the address
2505 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2506 * corresponding routes with RTPROT_DHCP. */
2507 SET_FOREACH(route, link->routes_foreign, i) {
2508 if (route->protocol != RTPROT_DHCP)
2509 continue;
2510
2511 if (address->family != route->family)
2512 continue;
2513
2514 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
2515 return true;
2516 }
2517
2518 return false;
2519}
2520
5e5b137a
TG
2521static int link_drop_foreign_config(Link *link) {
2522 Address *address;
2523 Route *route;
2524 Iterator i;
2525 int r;
2526
2527 SET_FOREACH(address, link->addresses_foreign, i) {
fe307276 2528 /* we consider IPv6LL addresses to be managed by the kernel */
5e5b137a
TG
2529 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1)
2530 continue;
2531
db51778f
YW
2532 if (link_address_is_dynamic(link, address)) {
2533 if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
2534 continue;
2535 } else if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
2536 continue;
2537
30226d27
TJ
2538 if (link_is_static_address_configured(link, address)) {
2539 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
2540 if (r < 0)
2541 return log_link_error_errno(link, r, "Failed to add address: %m");
2542 } else {
63ae0569 2543 r = address_remove(address, link, NULL);
30226d27
TJ
2544 if (r < 0)
2545 return r;
2546 }
5e5b137a
TG
2547 }
2548
2549 SET_FOREACH(route, link->routes_foreign, i) {
fe307276 2550 /* do not touch routes managed by the kernel */
5e5b137a
TG
2551 if (route->protocol == RTPROT_KERNEL)
2552 continue;
2553
db51778f
YW
2554 if (route->protocol == RTPROT_STATIC &&
2555 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
2556 continue;
2557
2558 if (route->protocol == RTPROT_DHCP &&
2559 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
2560 continue;
2561
7ecf0c3e
TJ
2562 if (link_is_static_route_configured(link, route)) {
2563 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL);
2564 if (r < 0)
2565 return r;
2566 } else {
4645ad47 2567 r = route_remove(route, link, NULL);
7ecf0c3e
TJ
2568 if (r < 0)
2569 return r;
2570 }
5e5b137a
TG
2571 }
2572
2573 return 0;
2574}
2575
3104883d 2576static int link_drop_config(Link *link) {
410a7f15 2577 Address *address, *pool_address;
3104883d
SS
2578 Route *route;
2579 Iterator i;
2580 int r;
2581
2582 SET_FOREACH(address, link->addresses, i) {
2583 /* we consider IPv6LL addresses to be managed by the kernel */
2584 if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1)
2585 continue;
2586
63ae0569 2587 r = address_remove(address, link, NULL);
3104883d
SS
2588 if (r < 0)
2589 return r;
410a7f15
RM
2590
2591 /* If this address came from an address pool, clean up the pool */
2592 LIST_FOREACH(addresses, pool_address, link->pool_addresses) {
2593 if (address_equal(address, pool_address)) {
2594 LIST_REMOVE(addresses, link->pool_addresses, pool_address);
2595 address_free(pool_address);
2596 break;
2597 }
2598 }
3104883d
SS
2599 }
2600
2601 SET_FOREACH(route, link->routes, i) {
2602 /* do not touch routes managed by the kernel */
2603 if (route->protocol == RTPROT_KERNEL)
2604 continue;
2605
4645ad47 2606 r = route_remove(route, link, NULL);
3104883d
SS
2607 if (r < 0)
2608 return r;
2609 }
2610
c69305ff
LP
2611 ndisc_flush(link);
2612
3104883d
SS
2613 return 0;
2614}
2615
a748b692 2616static int link_configure(Link *link) {
02b59d57
TG
2617 int r;
2618
ef1ba606 2619 assert(link);
b22d8a00 2620 assert(link->network);
bd08ce56 2621 assert(link->state == LINK_STATE_INITIALIZED);
a748b692 2622
cab042b8 2623 if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan"))
93ea7750 2624 return link_configure_can(link);
92c918b0 2625
02e28621
DG
2626 /* Drop foreign config, but ignore loopback or critical devices.
2627 * We do not want to remove loopback address or addresses used for root NFS. */
7da377ef 2628 if (!(link->flags & IFF_LOOPBACK) &&
db51778f 2629 link->network->keep_configuration != KEEP_CONFIGURATION_YES) {
e5d44b34
CH
2630 r = link_drop_foreign_config(link);
2631 if (r < 0)
2632 return r;
2633 }
5e5b137a 2634
23d8b221
SS
2635 r = link_set_proxy_arp(link);
2636 if (r < 0)
2637 return r;
2638
a0e5c15d
FK
2639 r = ipv6_proxy_ndp_addresses_configure(link);
2640 if (r < 0)
2641 return r;
2642
769d324c
LP
2643 r = link_set_ipv4_forward(link);
2644 if (r < 0)
2645 return r;
2646
2647 r = link_set_ipv6_forward(link);
5a8bcb67
LP
2648 if (r < 0)
2649 return r;
2650
49092e22
SS
2651 r = link_set_ipv6_privacy_extensions(link);
2652 if (r < 0)
2653 return r;
2654
4f2e437a
SS
2655 r = link_set_ipv6_accept_ra(link);
2656 if (r < 0)
2657 return r;
2658
8749cbcd
SS
2659 r = link_set_ipv6_dad_transmits(link);
2660 if (r < 0)
2661 return r;
b69c3180
SS
2662
2663 r = link_set_ipv6_hop_limit(link);
2664 if (r < 0)
2665 return r;
8749cbcd 2666
99d2baa2
SS
2667 r = link_set_flags(link);
2668 if (r < 0)
2669 return r;
2670
11102cba
SS
2671 r = link_set_ipv6_mtu(link);
2672 if (r < 0)
2673 return r;
2674
910feb78 2675 if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) {
b22d8a00 2676 r = ipv4ll_configure(link);
eb34d4af
TG
2677 if (r < 0)
2678 return r;
2679 }
2680
78c958f8 2681 if (link_dhcp4_enabled(link)) {
64b21ece
MV
2682 r = dhcp4_set_promote_secondaries(link);
2683 if (r < 0)
2684 return r;
2685
3c9b8860 2686 r = dhcp4_configure(link);
eb34d4af
TG
2687 if (r < 0)
2688 return r;
eb34d4af
TG
2689 }
2690
78c958f8 2691 if (link_dhcp4_server_enabled(link)) {
dd43110f
TG
2692 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
2693 if (r < 0)
2694 return r;
2695
2696 r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
2697 if (r < 0)
2698 return r;
dd43110f
TG
2699 }
2700
62379e88
TG
2701 if (link_dhcp6_enabled(link) ||
2702 link_ipv6_accept_ra_enabled(link)) {
f5a8c43f
TG
2703 r = dhcp6_configure(link);
2704 if (r < 0)
2705 return r;
2706 }
2707
2708 if (link_ipv6_accept_ra_enabled(link)) {
de1e9928 2709 r = ndisc_configure(link);
4138fb2c
PF
2710 if (r < 0)
2711 return r;
2712 }
2713
7465dd22
PF
2714 if (link_radv_enabled(link)) {
2715 r = radv_configure(link);
2716 if (r < 0)
2717 return r;
2718 }
2719
8e1ad1ea 2720 if (link_lldp_rx_enabled(link)) {
7f853950 2721 r = link_lldp_rx_configure(link);
273eec24
LP
2722 if (r < 0)
2723 return r;
ce43e484
SS
2724 }
2725
f6fcc1c2 2726 r = link_configure_mtu(link);
40288ece
YW
2727 if (r < 0)
2728 return r;
44b598a1 2729
9f6e82e6
YW
2730 r = link_configure_addrgen_mode(link);
2731 if (r < 0)
2732 return r;
0e2fdb83 2733
55dc8c4a
YW
2734 return link_configure_after_setting_mtu(link);
2735}
2736
2737static int link_configure_after_setting_mtu(Link *link) {
2738 int r;
2739
2740 assert(link);
2741 assert(link->network);
bd08ce56 2742 assert(link->state == LINK_STATE_INITIALIZED);
55dc8c4a
YW
2743
2744 if (link->setting_mtu)
2745 return 0;
2746
dad2d78e 2747 if (link_has_carrier(link) || link->network->configure_without_carrier) {
1e9be60b
TG
2748 r = link_acquire_conf(link);
2749 if (r < 0)
2750 return r;
cc544d5f 2751 }
1e9be60b 2752
3f265037 2753 return link_enter_join_netdev(link);
505f8da7
TG
2754}
2755
27dfc982
YW
2756static int duid_set_uuid(DUID *duid, sd_id128_t uuid) {
2757 assert(duid);
2758
2759 if (duid->raw_data_len > 0)
2760 return 0;
2761
2762 if (duid->type != DUID_TYPE_UUID)
2763 return -EINVAL;
2764
2765 memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t));
2766 duid->raw_data_len = sizeof(sd_id128_t);
2767
2768 return 1;
2769}
2770
2771int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2772 Manager *manager = userdata;
2773 const sd_bus_error *e;
2774 const void *a;
2775 size_t sz;
2776 DUID *duid;
2777 Link *link;
2778 int r;
2779
2780 assert(m);
2781 assert(manager);
2782
2783 e = sd_bus_message_get_error(m);
2784 if (e) {
2785 log_error_errno(sd_bus_error_get_errno(e),
ab4dd984 2786 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
27dfc982
YW
2787 e->message);
2788 goto configure;
2789 }
2790
2791 r = sd_bus_message_read_array(m, 'y', &a, &sz);
2792 if (r < 0)
2793 goto configure;
2794
2795 if (sz != sizeof(sd_id128_t)) {
ab4dd984 2796 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
27dfc982
YW
2797 goto configure;
2798 }
2799
2800 memcpy(&manager->product_uuid, a, sz);
2801 while ((duid = set_steal_first(manager->duids_requesting_uuid)))
2802 (void) duid_set_uuid(duid, manager->product_uuid);
2803
2804 manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid);
2805
2806configure:
2807 while ((link = set_steal_first(manager->links_requesting_uuid))) {
2808 r = link_configure(link);
2809 if (r < 0)
2810 log_link_error_errno(link, r, "Failed to configure link: %m");
2811 }
2812
2813 manager->links_requesting_uuid = set_free(manager->links_requesting_uuid);
2814
2815 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2816 * even if the method fails. */
2817 manager->has_product_uuid = true;
2818
2819 return 1;
2820}
2821
2822static bool link_requires_uuid(Link *link) {
2823 const DUID *duid;
2824
2825 assert(link);
2826 assert(link->manager);
2827 assert(link->network);
2828
2829 duid = link_get_duid(link);
2830 if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0)
2831 return false;
2832
2833 if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY))
2834 return true;
2835
2836 if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link))
2837 return true;
2838
2839 return false;
2840}
2841
2842static int link_configure_duid(Link *link) {
2843 Manager *m;
2844 DUID *duid;
2845 int r;
2846
2847 assert(link);
2848 assert(link->manager);
2849 assert(link->network);
2850
2851 m = link->manager;
2852 duid = link_get_duid(link);
2853
2854 if (!link_requires_uuid(link))
2855 return 1;
2856
2857 if (m->has_product_uuid) {
2858 (void) duid_set_uuid(duid, m->product_uuid);
2859 return 1;
2860 }
2861
2862 if (!m->links_requesting_uuid) {
2863 r = manager_request_product_uuid(m, link);
2864 if (r < 0) {
2865 if (r == -ENOMEM)
2866 return r;
2867
ab4dd984
ZJS
2868 log_link_warning_errno(link, r,
2869 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
27dfc982
YW
2870 return 1;
2871 }
2872 } else {
2873 r = set_put(m->links_requesting_uuid, link);
2874 if (r < 0)
2875 return log_oom();
2876
2877 r = set_put(m->duids_requesting_uuid, duid);
2878 if (r < 0)
2879 return log_oom();
2880 }
2881
2882 return 0;
2883}
2884
e6bf7774 2885static int link_initialized_and_synced(Link *link) {
505f8da7 2886 Network *network;
505f8da7
TG
2887 int r;
2888
2889 assert(link);
2890 assert(link->ifname);
2891 assert(link->manager);
2892
bd08ce56
YW
2893 /* We may get called either from the asynchronous netlink callback,
2894 * or directly for link_add() if running in a container. See link_add(). */
2895 if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
4232cf04 2896 return 0;
505f8da7 2897
6a7a4e4d 2898 log_link_debug(link, "Link state is up-to-date");
bd08ce56 2899 link_set_state(link, LINK_STATE_INITIALIZED);
505f8da7 2900
0d4ad91d
AR
2901 r = link_new_bound_by_list(link);
2902 if (r < 0)
2903 return r;
2904
2905 r = link_handle_bound_by_list(link);
2906 if (r < 0)
2907 return r;
2908
c4a03a56 2909 if (!link->network) {
51517f9e 2910 r = network_get(link->manager, link->sd_device, link->ifname,
c4a03a56
TG
2911 &link->mac, &network);
2912 if (r == -ENOENT) {
2913 link_enter_unmanaged(link);
4232cf04 2914 return 0;
a09dc546
DM
2915 } else if (r == 0 && network->unmanaged) {
2916 link_enter_unmanaged(link);
2917 return 0;
c4a03a56
TG
2918 } else if (r < 0)
2919 return r;
505f8da7 2920
c4a03a56
TG
2921 if (link->flags & IFF_LOOPBACK) {
2922 if (network->link_local != ADDRESS_FAMILY_NO)
2923 log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link");
78c958f8 2924
c4a03a56
TG
2925 if (network->dhcp != ADDRESS_FAMILY_NO)
2926 log_link_debug(link, "Ignoring DHCP clients for loopback link");
78c958f8 2927
c4a03a56
TG
2928 if (network->dhcp_server)
2929 log_link_debug(link, "Ignoring DHCP server for loopback link");
2930 }
bd2efe92 2931
7d342c03 2932 r = network_apply(network, link);
c4a03a56
TG
2933 if (r < 0)
2934 return r;
2935 }
505f8da7 2936
0d4ad91d
AR
2937 r = link_new_bound_to_list(link);
2938 if (r < 0)
2939 return r;
2940
27dfc982
YW
2941 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2942 * link_configure() is called later asynchronously. */
2943 r = link_configure_duid(link);
2944 if (r <= 0)
2945 return r;
2946
a748b692
TG
2947 r = link_configure(link);
2948 if (r < 0)
2949 return r;
2950
4232cf04 2951 return 0;
505f8da7
TG
2952}
2953
302a796f
YW
2954static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
2955 (void) link_initialized_and_synced(link);
e6bf7774
YW
2956 return 1;
2957}
2958
51517f9e 2959int link_initialized(Link *link, sd_device *device) {
4afd3348 2960 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
4f561e8e
TG
2961 int r;
2962
2963 assert(link);
2964 assert(link->manager);
2965 assert(link->manager->rtnl);
2966 assert(device);
2967
8434fd5c 2968 if (link->state != LINK_STATE_PENDING)
4f561e8e
TG
2969 return 0;
2970
51517f9e 2971 if (link->sd_device)
679b3605
TG
2972 return 0;
2973
79008bdd 2974 log_link_debug(link, "udev initialized link");
bd08ce56 2975 link_set_state(link, LINK_STATE_INITIALIZED);
4f561e8e 2976
51517f9e 2977 link->sd_device = sd_device_ref(device);
4f561e8e 2978
3c9b8860
TG
2979 /* udev has initialized the link, but we don't know if we have yet
2980 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2981 * when it returns we know that the pending NEWLINKs have already been
2982 * processed and that we are up-to-date */
4f561e8e 2983
3c9b8860
TG
2984 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
2985 link->ifindex);
4f561e8e
TG
2986 if (r < 0)
2987 return r;
2988
302a796f
YW
2989 r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler,
2990 link_netlink_destroy_callback, link);
4f561e8e
TG
2991 if (r < 0)
2992 return r;
2993
5da8149f
TG
2994 link_ref(link);
2995
4f561e8e
TG
2996 return 0;
2997}
2998
c4a03a56 2999static int link_load(Link *link) {
0bc70f1d
TG
3000 _cleanup_free_ char *network_file = NULL,
3001 *addresses = NULL,
f703cc2c 3002 *routes = NULL,
0bc70f1d
TG
3003 *dhcp4_address = NULL,
3004 *ipv4ll_address = NULL;
3005 union in_addr_union address;
f703cc2c 3006 union in_addr_union route_dst;
c598ac76 3007 const char *p;
c4a03a56
TG
3008 int r;
3009
3010 assert(link);
3011
aa8fbc74 3012 r = parse_env_file(NULL, link->state_file,
c4a03a56
TG
3013 "NETWORK_FILE", &network_file,
3014 "ADDRESSES", &addresses,
f703cc2c 3015 "ROUTES", &routes,
0bc70f1d 3016 "DHCP4_ADDRESS", &dhcp4_address,
13df9c39 3017 "IPV4LL_ADDRESS", &ipv4ll_address);
c4a03a56
TG
3018 if (r < 0 && r != -ENOENT)
3019 return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file);
3020
3021 if (network_file) {
3022 Network *network;
3023 char *suffix;
3024
3025 /* drop suffix */
3026 suffix = strrchr(network_file, '.');
3027 if (!suffix) {
3028 log_link_debug(link, "Failed to get network name from %s", network_file);
3029 goto network_file_fail;
3030 }
3031 *suffix = '\0';
3032
3033 r = network_get_by_name(link->manager, basename(network_file), &network);
3034 if (r < 0) {
3035 log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file));
3036 goto network_file_fail;
3037 }
3038
7d342c03 3039 r = network_apply(network, link);
c4a03a56
TG
3040 if (r < 0)
3041 return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file));
3042 }
3043
3044network_file_fail:
3045
3046 if (addresses) {
c598ac76 3047 p = addresses;
c4a03a56 3048
c598ac76
TG
3049 for (;;) {
3050 _cleanup_free_ char *address_str = NULL;
c4a03a56
TG
3051 char *prefixlen_str;
3052 int family;
3053 unsigned char prefixlen;
c4a03a56 3054
c598ac76
TG
3055 r = extract_first_word(&p, &address_str, NULL, 0);
3056 if (r < 0) {
3057 log_link_debug_errno(link, r, "Failed to extract next address string: %m");
3058 continue;
928bc597
TA
3059 }
3060 if (r == 0)
c598ac76
TG
3061 break;
3062
3063 prefixlen_str = strchr(address_str, '/');
c4a03a56 3064 if (!prefixlen_str) {
c598ac76 3065 log_link_debug(link, "Failed to parse address and prefix length %s", address_str);
c4a03a56
TG
3066 continue;
3067 }
3068
313cefa1 3069 *prefixlen_str++ = '\0';
c4a03a56
TG
3070
3071 r = sscanf(prefixlen_str, "%hhu", &prefixlen);
3072 if (r != 1) {
3073 log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str);
3074 continue;
3075 }
3076
c598ac76 3077 r = in_addr_from_string_auto(address_str, &family, &address);
c4a03a56 3078 if (r < 0) {
c598ac76 3079 log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str);
c4a03a56
TG
3080 continue;
3081 }
3082
3083 r = address_add(link, family, &address, prefixlen, NULL);
3084 if (r < 0)
3085 return log_link_error_errno(link, r, "Failed to add address: %m");
3086 }
3087 }
3088
f703cc2c 3089 if (routes) {
74544b4e
TA
3090 p = routes;
3091
c598ac76 3092 for (;;) {
f833694d 3093 Route *route;
c598ac76 3094 _cleanup_free_ char *route_str = NULL;
4afd3348 3095 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;