1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
7 #include "alloc-util.h"
9 #include "dhcp-identifier.h"
10 #include "dhcp-lease-internal.h"
14 #include "missing_network.h"
15 #include "netdev/bond.h"
16 #include "netdev/bridge.h"
17 #include "netdev/ipvlan.h"
18 #include "netdev/vrf.h"
19 #include "netlink-util.h"
20 #include "network-internal.h"
21 #include "networkd-can.h"
22 #include "networkd-dhcp-server.h"
23 #include "networkd-dhcp4.h"
24 #include "networkd-dhcp6.h"
25 #include "networkd-ipv4ll.h"
26 #include "networkd-ipv6-proxy-ndp.h"
27 #include "networkd-link-bus.h"
28 #include "networkd-link.h"
29 #include "networkd-lldp-tx.h"
30 #include "networkd-manager.h"
31 #include "networkd-ndisc.h"
32 #include "networkd-neighbor.h"
33 #include "networkd-radv.h"
34 #include "networkd-routing-policy-rule.h"
36 #include "socket-util.h"
37 #include "stdio-util.h"
38 #include "string-table.h"
40 #include "sysctl-util.h"
41 #include "tmpfile-util.h"
42 #include "udev-util.h"
46 uint32_t link_get_vrf_table(Link
*link
) {
47 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
50 uint32_t link_get_dhcp_route_table(Link
*link
) {
51 /* When the interface is part of an VRF use the VRFs routing table, unless
52 * another table is explicitly specified. */
53 if (link
->network
->dhcp_route_table_set
)
54 return link
->network
->dhcp_route_table
;
55 return link_get_vrf_table(link
);
58 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
59 if (link
->network
->ipv6_accept_ra_route_table_set
)
60 return link
->network
->ipv6_accept_ra_route_table
;
61 return link_get_vrf_table(link
);
64 DUID
* link_get_duid(Link
*link
) {
65 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
66 return &link
->network
->duid
;
68 return &link
->manager
->duid
;
71 int link_sysctl_ipv6_enabled(Link
*link
) {
72 _cleanup_free_
char *value
= NULL
;
78 if (link
->sysctl_ipv6_enabled
>= 0)
79 return link
->sysctl_ipv6_enabled
;
81 const char *ifname
= link
->ifname
; /* work around bogus gcc warning */
82 r
= sysctl_read_ip_property(AF_INET6
, ifname
, "disable_ipv6", &value
);
84 return log_link_warning_errno(link
, r
,
85 "Failed to read net.ipv6.conf.%s.disable_ipv6 sysctl property: %m",
88 link
->sysctl_ipv6_enabled
= value
[0] == '0';
89 return link
->sysctl_ipv6_enabled
;
92 static bool link_dhcp6_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 if (link
->network
->bond
)
107 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
110 if (link_sysctl_ipv6_enabled(link
) == 0)
113 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
116 static bool link_dhcp4_enabled(Link
*link
) {
119 if (link
->flags
& IFF_LOOPBACK
)
125 if (link
->network
->bond
)
128 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
131 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
134 static bool link_dhcp4_server_enabled(Link
*link
) {
137 if (link
->flags
& IFF_LOOPBACK
)
143 if (link
->network
->bond
)
146 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
149 return link
->network
->dhcp_server
;
152 bool link_ipv4ll_enabled(Link
*link
, AddressFamilyBoolean mask
) {
154 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
156 if (link
->flags
& IFF_LOOPBACK
)
162 if (STRPTR_IN_SET(link
->kind
,
163 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
164 "vti6", "can", "vcan", "vxcan", "nlmon", "xfrm"))
167 /* L3 or L3S mode do not support ARP. */
168 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
171 if (link
->network
->bond
)
174 return link
->network
->link_local
& mask
;
177 static bool link_ipv6ll_enabled(Link
*link
) {
180 if (!socket_ipv6_is_supported())
183 if (link
->flags
& IFF_LOOPBACK
)
189 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "can", "vcan", "vxcan", "nlmon"))
192 if (link
->network
->bond
)
195 if (link_sysctl_ipv6_enabled(link
) == 0)
198 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
201 static bool link_ipv6_enabled(Link
*link
) {
204 if (!socket_ipv6_is_supported())
207 if (link
->network
->bond
)
210 if (link_sysctl_ipv6_enabled(link
) == 0)
213 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
216 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
217 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
220 static bool link_radv_enabled(Link
*link
) {
223 if (!link_ipv6ll_enabled(link
))
226 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
229 static bool link_ipv4_forward_enabled(Link
*link
) {
232 if (link
->flags
& IFF_LOOPBACK
)
238 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
241 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
244 static bool link_ipv6_forward_enabled(Link
*link
) {
247 if (!socket_ipv6_is_supported())
250 if (link
->flags
& IFF_LOOPBACK
)
256 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
259 if (link_sysctl_ipv6_enabled(link
) == 0)
262 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
265 static bool link_proxy_arp_enabled(Link
*link
) {
268 if (link
->flags
& IFF_LOOPBACK
)
274 if (link
->network
->proxy_arp
< 0)
280 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
283 if (!socket_ipv6_is_supported())
286 if (link
->flags
& IFF_LOOPBACK
)
292 if (!link_ipv6ll_enabled(link
))
295 /* If unset use system default (enabled if local forwarding is disabled.
296 * disabled if local forwarding is enabled).
297 * If set, ignore or enforce RA independent of local forwarding state.
299 if (link
->network
->ipv6_accept_ra
< 0)
300 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
301 return !link_ipv6_forward_enabled(link
);
302 else if (link
->network
->ipv6_accept_ra
> 0)
303 /* accept RA even if ip_forward is enabled */
310 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
313 if (!socket_ipv6_is_supported())
314 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
316 if (link
->flags
& IFF_LOOPBACK
)
317 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
320 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
322 return link
->network
->ipv6_privacy_extensions
;
325 static int link_enable_ipv6(Link
*link
) {
329 if (link
->flags
& IFF_LOOPBACK
)
332 disabled
= !link_ipv6_enabled(link
);
334 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
336 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
338 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
343 static bool link_is_enslaved(Link
*link
) {
344 if (link
->flags
& IFF_SLAVE
)
345 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
351 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
354 /* TODO: add conditions for other netdevs. */
359 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
365 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
368 link_update_operstate(master
, true);
371 void link_update_operstate(Link
*link
, bool also_update_master
) {
372 LinkOperationalState operstate
;
373 LinkCarrierState carrier_state
;
374 LinkAddressState address_state
;
375 _cleanup_strv_free_
char **p
= NULL
;
376 uint8_t scope
= RT_SCOPE_NOWHERE
;
377 bool changed
= false;
383 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
384 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
385 else if (link_has_carrier(link
)) {
386 if (link_is_enslaved(link
))
387 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
389 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
390 } else if (link
->flags
& IFF_UP
)
391 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
393 carrier_state
= LINK_CARRIER_STATE_OFF
;
395 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
398 SET_FOREACH(slave
, link
->slaves
, i
) {
399 link_update_operstate(slave
, false);
401 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
402 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
406 SET_FOREACH(address
, link
->addresses
, i
) {
407 if (!address_is_ready(address
))
410 if (address
->scope
< scope
)
411 scope
= address
->scope
;
414 /* for operstate we also take foreign addresses into account */
415 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
416 if (!address_is_ready(address
))
419 if (address
->scope
< scope
)
420 scope
= address
->scope
;
423 if (scope
< RT_SCOPE_SITE
)
424 /* universally accessible addresses found */
425 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
426 else if (scope
< RT_SCOPE_HOST
)
427 /* only link or site local addresses found */
428 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
430 /* no useful addresses found */
431 address_state
= LINK_ADDRESS_STATE_OFF
;
433 /* Mapping of address and carrier state vs operational state
435 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
436 * ------------------------------------------------------------------------------
437 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
438 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
439 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
442 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
443 operstate
= (LinkOperationalState
) carrier_state
;
444 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
445 operstate
= LINK_OPERSTATE_ROUTABLE
;
446 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
447 operstate
= LINK_OPERSTATE_DEGRADED
;
449 operstate
= LINK_OPERSTATE_ENSLAVED
;
451 if (link
->carrier_state
!= carrier_state
) {
452 link
->carrier_state
= carrier_state
;
454 if (strv_extend(&p
, "CarrierState") < 0)
458 if (link
->address_state
!= address_state
) {
459 link
->address_state
= address_state
;
461 if (strv_extend(&p
, "AddressState") < 0)
465 if (link
->operstate
!= operstate
) {
466 link
->operstate
= operstate
;
468 if (strv_extend(&p
, "OperationalState") < 0)
473 link_send_changed_strv(link
, p
);
477 if (also_update_master
&& link
->network
) {
478 link_update_master_operstate(link
, link
->network
->bond
);
479 link_update_master_operstate(link
, link
->network
->bridge
);
483 #define FLAG_STRING(string, flag, old, new) \
484 (((old ^ new) & flag) \
485 ? ((old & flag) ? (" -" string) : (" +" string)) \
488 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
489 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
495 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
497 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
499 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
501 /* if we got a message without operstate, take it to mean
502 the state was unchanged */
503 operstate
= link
->kernel_operstate
;
505 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
508 if (link
->flags
!= flags
) {
509 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",
510 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
511 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
512 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
513 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
514 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
515 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
516 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
517 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
518 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
519 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
520 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
521 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
522 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
523 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
524 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
525 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
526 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
527 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
528 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
530 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
531 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
532 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
533 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
534 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
535 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
536 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
537 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
539 /* link flags are currently at most 18 bits, let's align to
541 if (unknown_flags_added
)
543 "Unknown link flags gained: %#.5x (ignoring)",
544 unknown_flags_added
);
546 if (unknown_flags_removed
)
548 "Unknown link flags lost: %#.5x (ignoring)",
549 unknown_flags_removed
);
553 link
->kernel_operstate
= operstate
;
555 link_update_operstate(link
, true);
560 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
561 _cleanup_(link_unrefp
) Link
*link
= NULL
;
563 const char *ifname
, *kind
= NULL
;
565 unsigned short iftype
;
571 /* check for link kind */
572 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
574 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
575 r
= sd_netlink_message_exit_container(message
);
580 r
= sd_netlink_message_get_type(message
, &type
);
583 else if (type
!= RTM_NEWLINK
)
586 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
589 else if (ifindex
<= 0)
592 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
596 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
607 .state
= LINK_STATE_PENDING
,
608 .rtnl_extended_attrs
= true,
611 .sysctl_ipv6_enabled
= -1,
614 link
->ifname
= strdup(ifname
);
619 link
->kind
= strdup(kind
);
624 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
626 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
628 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
630 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
632 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
635 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
638 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
641 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
645 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
649 r
= link_update_flags(link
, message
, false);
653 *ret
= TAKE_PTR(link
);
658 static Link
*link_free(Link
*link
) {
663 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
664 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
666 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
667 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
669 while ((address
= link
->pool_addresses
)) {
670 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
671 address_free(address
);
674 sd_dhcp_server_unref(link
->dhcp_server
);
675 sd_dhcp_client_unref(link
->dhcp_client
);
676 sd_dhcp_lease_unref(link
->dhcp_lease
);
678 link_lldp_emit_stop(link
);
680 free(link
->lease_file
);
682 sd_lldp_unref(link
->lldp
);
683 free(link
->lldp_file
);
687 sd_ipv4ll_unref(link
->ipv4ll
);
688 sd_dhcp6_client_unref(link
->dhcp6_client
);
689 sd_ndisc_unref(link
->ndisc
);
690 sd_radv_unref(link
->radv
);
695 (void) unlink(link
->state_file
);
696 free(link
->state_file
);
698 sd_device_unref(link
->sd_device
);
700 hashmap_free(link
->bound_to_links
);
701 hashmap_free(link
->bound_by_links
);
703 set_free_with_destructor(link
->slaves
, link_unref
);
705 network_unref(link
->network
);
710 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
712 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
719 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
728 void link_set_state(Link
*link
, LinkState state
) {
731 if (link
->state
== state
)
734 log_link_debug(link
, "State changed: %s -> %s",
735 link_state_to_string(link
->state
),
736 link_state_to_string(state
));
740 link_send_changed(link
, "AdministrativeState", NULL
);
743 static void link_enter_unmanaged(Link
*link
) {
746 link_set_state(link
, LINK_STATE_UNMANAGED
);
751 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
755 assert(link
->manager
);
756 assert(link
->manager
->event
);
758 dhcp4_release_old_lease(link
);
760 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
761 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
762 k
= sd_dhcp_client_stop(link
->dhcp_client
);
764 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
768 k
= sd_ipv4ll_stop(link
->ipv4ll
);
770 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
773 if (link
->dhcp6_client
) {
774 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
776 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
780 k
= sd_ndisc_stop(link
->ndisc
);
782 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
786 k
= sd_radv_stop(link
->radv
);
788 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
791 link_lldp_emit_stop(link
);
795 void link_enter_failed(Link
*link
) {
798 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
801 log_link_warning(link
, "Failed");
803 link_set_state(link
, LINK_STATE_FAILED
);
805 link_stop_clients(link
, false);
810 static int link_join_netdevs_after_configured(Link
*link
) {
815 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
816 if (netdev
->ifindex
> 0)
817 /* Assume already enslaved. */
820 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
823 log_struct(LOG_DEBUG
,
824 LOG_LINK_INTERFACE(link
),
825 LOG_NETDEV_INTERFACE(netdev
),
826 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
828 r
= netdev_join(netdev
, link
, NULL
);
830 return log_struct_errno(LOG_WARNING
, r
,
831 LOG_LINK_INTERFACE(link
),
832 LOG_NETDEV_INTERFACE(netdev
),
833 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
839 static void link_enter_configured(Link
*link
) {
841 assert(link
->network
);
843 if (link
->state
!= LINK_STATE_CONFIGURING
)
846 log_link_info(link
, "Configured");
848 link_set_state(link
, LINK_STATE_CONFIGURED
);
850 (void) link_join_netdevs_after_configured(link
);
855 static int link_request_set_routing_policy_rule(Link
*link
) {
856 RoutingPolicyRule
*rule
, *rrule
= NULL
;
860 assert(link
->network
);
862 link
->routing_policy_rules_configured
= false;
864 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
865 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
868 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
872 r
= routing_policy_rule_configure(rule
, link
, NULL
);
874 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
875 link_enter_failed(link
);
879 link
->routing_policy_rule_messages
++;
882 routing_policy_rule_purge(link
->manager
, link
);
883 if (link
->routing_policy_rule_messages
== 0) {
884 link
->routing_policy_rules_configured
= true;
885 link_check_ready(link
);
887 log_link_debug(link
, "Setting routing policy rules");
888 link_set_state(link
, LINK_STATE_CONFIGURING
);
894 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
898 assert(link
->route_messages
> 0);
899 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
900 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
902 link
->route_messages
--;
904 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
907 r
= sd_netlink_message_get_errno(m
);
908 if (r
< 0 && r
!= -EEXIST
)
909 log_link_warning_errno(link
, r
, "Could not set route: %m");
911 if (link
->route_messages
== 0) {
912 log_link_debug(link
, "Routes set");
913 link
->static_routes_configured
= true;
914 link_check_ready(link
);
920 int link_request_set_routes(Link
*link
) {
922 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
923 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
930 assert(link
->network
);
931 assert(link
->addresses_configured
);
932 assert(link
->address_messages
== 0);
933 assert(link
->state
!= _LINK_STATE_INVALID
);
935 link
->static_routes_configured
= false;
937 r
= link_request_set_routing_policy_rule(link
);
941 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
942 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
943 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
945 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
948 r
= route_configure(rt
, link
, route_handler
);
950 log_link_warning_errno(link
, r
, "Could not set routes: %m");
951 link_enter_failed(link
);
955 link
->route_messages
++;
958 if (link
->route_messages
== 0) {
959 link
->static_routes_configured
= true;
960 link_check_ready(link
);
962 log_link_debug(link
, "Setting routes");
963 link_set_state(link
, LINK_STATE_CONFIGURING
);
969 void link_check_ready(Link
*link
) {
975 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
981 if (!link
->addresses_configured
)
984 if (!link
->neighbors_configured
)
987 SET_FOREACH(a
, link
->addresses
, i
)
988 if (!address_is_ready(a
))
991 if (!link
->addresses_ready
) {
992 link
->addresses_ready
= true;
993 link_request_set_routes(link
);
997 if (!link
->static_routes_configured
)
1000 if (!link
->routing_policy_rules_configured
)
1003 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1005 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
1008 if (link_ipv6ll_enabled(link
) &&
1009 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1012 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1013 !link
->dhcp4_configured
&&
1014 !link
->dhcp6_configured
&&
1015 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1016 /* When DHCP is enabled, at least one protocol must provide an address, or
1017 * an IPv4ll fallback address must be configured. */
1020 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1024 if (link
->state
!= LINK_STATE_CONFIGURED
)
1025 link_enter_configured(link
);
1030 static int link_request_set_neighbors(Link
*link
) {
1035 assert(link
->network
);
1036 assert(link
->state
!= _LINK_STATE_INVALID
);
1038 link
->neighbors_configured
= false;
1040 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1041 r
= neighbor_configure(neighbor
, link
, NULL
);
1043 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1044 link_enter_failed(link
);
1049 if (link
->neighbor_messages
== 0) {
1050 link
->neighbors_configured
= true;
1051 link_check_ready(link
);
1053 log_link_debug(link
, "Setting neighbors");
1054 link_set_state(link
, LINK_STATE_CONFIGURING
);
1060 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1066 assert(link
->ifname
);
1067 assert(link
->address_messages
> 0);
1068 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1069 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1071 link
->address_messages
--;
1073 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1076 r
= sd_netlink_message_get_errno(m
);
1077 if (r
< 0 && r
!= -EEXIST
)
1078 log_link_warning_errno(link
, r
, "could not set address: %m");
1080 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1082 if (link
->address_messages
== 0) {
1083 log_link_debug(link
, "Addresses set");
1084 link
->addresses_configured
= true;
1085 link_check_ready(link
);
1091 static int link_set_bridge_fdb(Link
*link
) {
1092 FdbEntry
*fdb_entry
;
1095 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1096 r
= fdb_entry_configure(link
, fdb_entry
);
1098 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1104 static int link_request_set_addresses(Link
*link
) {
1105 AddressLabel
*label
;
1110 assert(link
->network
);
1111 assert(link
->state
!= _LINK_STATE_INVALID
);
1113 /* Reset all *_configured flags we are configuring. */
1114 link
->addresses_configured
= false;
1115 link
->addresses_ready
= false;
1116 link
->neighbors_configured
= false;
1117 link
->static_routes_configured
= false;
1118 link
->routing_policy_rules_configured
= false;
1120 r
= link_set_bridge_fdb(link
);
1124 r
= link_request_set_neighbors(link
);
1128 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1131 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1133 r
= address_configure(ad
, link
, address_handler
, update
);
1135 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1136 link_enter_failed(link
);
1140 link
->address_messages
++;
1143 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1144 r
= address_label_configure(label
, link
, NULL
, false);
1146 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1147 link_enter_failed(link
);
1151 link
->address_label_messages
++;
1154 /* now that we can figure out a default address for the dhcp server,
1156 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1157 r
= dhcp4_server_configure(link
);
1159 link_enter_failed(link
);
1162 log_link_debug(link
, "Offering DHCPv4 leases");
1165 if (link
->address_messages
== 0) {
1166 link
->addresses_configured
= true;
1167 link_check_ready(link
);
1169 log_link_debug(link
, "Setting addresses");
1170 link_set_state(link
, LINK_STATE_CONFIGURING
);
1176 static int link_set_bridge_vlan(Link
*link
) {
1179 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1181 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1186 static int link_set_proxy_arp(Link
*link
) {
1189 if (!link_proxy_arp_enabled(link
))
1192 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1194 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1199 static int link_configure_after_setting_mtu(Link
*link
);
1201 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1206 assert(link
->ifname
);
1208 link
->setting_mtu
= false;
1210 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1213 r
= sd_netlink_message_get_errno(m
);
1215 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1219 log_link_debug(link
, "Setting MTU done.");
1221 if (link
->state
== LINK_STATE_INITIALIZED
)
1222 (void) link_configure_after_setting_mtu(link
);
1227 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1228 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1232 assert(link
->manager
);
1233 assert(link
->manager
->rtnl
);
1235 if (mtu
== 0 || link
->setting_mtu
)
1238 if (link
->mtu
== mtu
)
1241 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1243 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1245 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1247 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1248 * for this interface, then disable IPv6 else enable it. */
1249 (void) link_enable_ipv6(link
);
1251 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1252 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1253 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1255 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1256 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1261 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1263 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1265 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1266 link_netlink_destroy_callback
, link
);
1268 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1271 link
->setting_mtu
= true;
1276 static bool link_reduces_vlan_mtu(Link
*link
) {
1277 /* See netif_reduces_vlan_mtu() in kernel. */
1278 return streq_ptr(link
->kind
, "macsec");
1281 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1286 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1287 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1288 /* See vlan_dev_change_mtu() in kernel. */
1289 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1291 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1292 /* See macvlan_change_mtu() in kernel. */
1298 static int link_configure_mtu(Link
*link
) {
1302 assert(link
->network
);
1304 if (link
->network
->mtu
> 0)
1305 return link_set_mtu(link
, link
->network
->mtu
);
1307 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1308 if (link
->mtu
>= mtu
)
1311 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1312 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1315 return link_set_mtu(link
, mtu
);
1318 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1323 assert(link
->ifname
);
1325 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1328 r
= sd_netlink_message_get_errno(m
);
1330 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1335 static int link_set_flags(Link
*link
) {
1336 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1337 unsigned ifi_change
= 0;
1338 unsigned ifi_flags
= 0;
1342 assert(link
->manager
);
1343 assert(link
->manager
->rtnl
);
1345 if (link
->flags
& IFF_LOOPBACK
)
1351 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1354 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1356 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1358 if (link
->network
->arp
>= 0) {
1359 ifi_change
|= IFF_NOARP
;
1360 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1363 if (link
->network
->multicast
>= 0) {
1364 ifi_change
|= IFF_MULTICAST
;
1365 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1368 if (link
->network
->allmulticast
>= 0) {
1369 ifi_change
|= IFF_ALLMULTI
;
1370 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1373 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1375 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1377 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1378 link_netlink_destroy_callback
, link
);
1380 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1387 static int link_acquire_ipv6_conf(Link
*link
) {
1392 if (link_ipv6_accept_ra_enabled(link
)) {
1393 assert(link
->ndisc
);
1395 log_link_debug(link
, "Discovering IPv6 routers");
1397 r
= sd_ndisc_start(link
->ndisc
);
1398 if (r
< 0 && r
!= -EBUSY
)
1399 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1402 if (link_radv_enabled(link
)) {
1404 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1406 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1408 r
= sd_radv_start(link
->radv
);
1409 if (r
< 0 && r
!= -EBUSY
)
1410 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1413 (void) dhcp6_request_prefix_delegation(link
);
1418 static int link_acquire_ipv4_conf(Link
*link
) {
1422 assert(link
->manager
);
1423 assert(link
->manager
->event
);
1425 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1426 assert(link
->ipv4ll
);
1428 log_link_debug(link
, "Acquiring IPv4 link-local address");
1430 r
= sd_ipv4ll_start(link
->ipv4ll
);
1432 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1435 if (link_dhcp4_enabled(link
)) {
1436 assert(link
->dhcp_client
);
1438 log_link_debug(link
, "Acquiring DHCPv4 lease");
1440 r
= sd_dhcp_client_start(link
->dhcp_client
);
1442 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1448 static int link_acquire_conf(Link
*link
) {
1453 r
= link_acquire_ipv4_conf(link
);
1457 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1458 r
= link_acquire_ipv6_conf(link
);
1463 if (link_lldp_emit_enabled(link
)) {
1464 r
= link_lldp_emit_start(link
);
1466 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1472 bool link_has_carrier(Link
*link
) {
1473 /* see Documentation/networking/operstates.txt in the kernel sources */
1475 if (link
->kernel_operstate
== IF_OPER_UP
)
1478 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1479 /* operstate may not be implemented, so fall back to flags */
1480 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1481 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1487 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1492 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1495 r
= sd_netlink_message_get_errno(m
);
1497 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1502 static int link_configure_addrgen_mode(Link
*link
) {
1503 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1504 uint8_t ipv6ll_mode
;
1508 assert(link
->network
);
1509 assert(link
->manager
);
1510 assert(link
->manager
->rtnl
);
1512 if (!socket_ipv6_is_supported())
1515 log_link_debug(link
, "Setting address genmode for link");
1517 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1519 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1521 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1523 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1525 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1527 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1529 if (!link_ipv6ll_enabled(link
))
1530 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1531 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1532 /* The file may not exist. And event if it exists, when stable_secret is unset,
1533 * reading the file fails with EIO. */
1534 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1536 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1538 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1540 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1542 r
= sd_netlink_message_close_container(req
);
1544 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1546 r
= sd_netlink_message_close_container(req
);
1548 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1550 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1551 link_netlink_destroy_callback
, link
);
1553 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1560 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1565 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1568 r
= sd_netlink_message_get_errno(m
);
1570 /* we warn but don't fail the link, as it may be brought up later */
1571 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1576 static int link_up(Link
*link
) {
1577 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1581 assert(link
->network
);
1582 assert(link
->manager
);
1583 assert(link
->manager
->rtnl
);
1585 log_link_debug(link
, "Bringing link up");
1587 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1589 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1591 /* set it free if not enslaved with networkd */
1592 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1593 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1595 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1598 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1600 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1602 if (link
->network
->mac
) {
1603 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1605 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1608 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1609 link_netlink_destroy_callback
, link
);
1611 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1618 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1623 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1626 r
= sd_netlink_message_get_errno(m
);
1628 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1633 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1634 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1638 assert(link
->manager
);
1639 assert(link
->manager
->rtnl
);
1641 log_link_debug(link
, "Bringing link down");
1643 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1644 RTM_SETLINK
, link
->ifindex
);
1646 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1648 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1650 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1652 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1653 callback
?: link_down_handler
,
1654 link_netlink_destroy_callback
, link
);
1656 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1663 static int link_handle_bound_to_list(Link
*link
) {
1667 bool required_up
= false;
1668 bool link_is_up
= false;
1672 if (hashmap_isempty(link
->bound_to_links
))
1675 if (link
->flags
& IFF_UP
)
1678 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1679 if (link_has_carrier(l
)) {
1684 if (!required_up
&& link_is_up
) {
1685 r
= link_down(link
, NULL
);
1688 } else if (required_up
&& !link_is_up
) {
1697 static int link_handle_bound_by_list(Link
*link
) {
1704 if (hashmap_isempty(link
->bound_by_links
))
1707 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1708 r
= link_handle_bound_to_list(l
);
1716 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1722 if (link
== carrier
)
1725 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1728 r
= hashmap_ensure_allocated(h
, NULL
);
1732 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1739 static int link_new_bound_by_list(Link
*link
) {
1744 bool list_updated
= false;
1747 assert(link
->manager
);
1751 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1752 if (!carrier
->network
)
1755 if (strv_isempty(carrier
->network
->bind_carrier
))
1758 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1759 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1763 list_updated
= true;
1770 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1771 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1775 link_dirty(carrier
);
1781 static int link_new_bound_to_list(Link
*link
) {
1786 bool list_updated
= false;
1789 assert(link
->manager
);
1794 if (strv_isempty(link
->network
->bind_carrier
))
1799 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1800 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1801 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1805 list_updated
= true;
1812 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1813 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1817 link_dirty(carrier
);
1823 static int link_new_carrier_maps(Link
*link
) {
1826 r
= link_new_bound_by_list(link
);
1830 r
= link_handle_bound_by_list(link
);
1834 r
= link_new_bound_to_list(link
);
1838 r
= link_handle_bound_to_list(link
);
1845 static void link_free_bound_to_list(Link
*link
) {
1849 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1850 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1852 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1853 link_dirty(bound_to
);
1859 static void link_free_bound_by_list(Link
*link
) {
1863 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1864 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1866 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1867 link_dirty(bound_by
);
1868 link_handle_bound_to_list(bound_by
);
1875 static void link_free_carrier_maps(Link
*link
) {
1876 bool list_updated
= false;
1880 if (!hashmap_isempty(link
->bound_to_links
)) {
1881 link_free_bound_to_list(link
);
1882 list_updated
= true;
1885 if (!hashmap_isempty(link
->bound_by_links
)) {
1886 link_free_bound_by_list(link
);
1887 list_updated
= true;
1896 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1903 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1907 r
= set_ensure_allocated(&master
->slaves
, NULL
);
1911 r
= set_put(master
->slaves
, link
);
1919 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1924 if (!link
->manager
|| !netdev
)
1927 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1930 link_unref(set_remove(master
->slaves
, link
));
1933 static void link_detach_from_manager(Link
*link
) {
1934 if (!link
|| !link
->manager
)
1937 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1940 /* The following must be called at last. */
1941 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1945 void link_drop(Link
*link
) {
1946 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1949 link_set_state(link
, LINK_STATE_LINGER
);
1951 link_free_carrier_maps(link
);
1953 if (link
->network
) {
1954 link_drop_from_master(link
, link
->network
->bridge
);
1955 link_drop_from_master(link
, link
->network
->bond
);
1958 log_link_debug(link
, "Link removed");
1960 (void) unlink(link
->state_file
);
1961 link_detach_from_manager(link
);
1964 static int link_joined(Link
*link
) {
1968 assert(link
->network
);
1970 if (!hashmap_isempty(link
->bound_to_links
)) {
1971 r
= link_handle_bound_to_list(link
);
1974 } else if (!(link
->flags
& IFF_UP
)) {
1977 link_enter_failed(link
);
1982 if (link
->network
->bridge
) {
1983 r
= link_set_bridge(link
);
1985 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1987 r
= link_append_to_master(link
, link
->network
->bridge
);
1989 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
1992 if (link
->network
->bond
) {
1993 r
= link_set_bond(link
);
1995 log_link_error_errno(link
, r
, "Could not set bond message: %m");
1997 r
= link_append_to_master(link
, link
->network
->bond
);
1999 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2002 if (link
->network
->use_br_vlan
&&
2003 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2004 r
= link_set_bridge_vlan(link
);
2006 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2009 /* Skip setting up addresses until it gets carrier,
2010 or it would try to set addresses twice,
2011 which is bad for non-idempotent steps. */
2012 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2015 link_set_state(link
, LINK_STATE_CONFIGURING
);
2016 return link_request_set_addresses(link
);
2019 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2023 assert(link
->network
);
2024 assert(link
->enslaving
> 0);
2028 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2031 r
= sd_netlink_message_get_errno(m
);
2032 if (r
< 0 && r
!= -EEXIST
) {
2033 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2034 link_enter_failed(link
);
2037 log_link_debug(link
, "Joined netdev");
2039 if (link
->enslaving
== 0) {
2046 static int link_enter_join_netdev(Link
*link
) {
2052 assert(link
->network
);
2053 assert(link
->state
== LINK_STATE_INITIALIZED
);
2055 link_set_state(link
, LINK_STATE_CONFIGURING
);
2058 link
->enslaving
= 0;
2060 if (link
->network
->bond
) {
2061 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2062 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2063 return link_joined(link
);
2065 log_struct(LOG_DEBUG
,
2066 LOG_LINK_INTERFACE(link
),
2067 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2068 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2072 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2074 log_struct_errno(LOG_WARNING
, r
,
2075 LOG_LINK_INTERFACE(link
),
2076 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2077 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2078 link_enter_failed(link
);
2083 if (link
->network
->bridge
) {
2084 log_struct(LOG_DEBUG
,
2085 LOG_LINK_INTERFACE(link
),
2086 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2087 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2091 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2093 log_struct_errno(LOG_WARNING
, r
,
2094 LOG_LINK_INTERFACE(link
),
2095 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2096 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2097 link_enter_failed(link
);
2102 if (link
->network
->vrf
) {
2103 log_struct(LOG_DEBUG
,
2104 LOG_LINK_INTERFACE(link
),
2105 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2106 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2110 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2112 log_struct_errno(LOG_WARNING
, r
,
2113 LOG_LINK_INTERFACE(link
),
2114 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2115 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2116 link_enter_failed(link
);
2121 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2123 if (netdev
->ifindex
> 0)
2124 /* Assume already enslaved. */
2127 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2130 log_struct(LOG_DEBUG
,
2131 LOG_LINK_INTERFACE(link
),
2132 LOG_NETDEV_INTERFACE(netdev
),
2133 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2137 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2139 log_struct_errno(LOG_WARNING
, r
,
2140 LOG_LINK_INTERFACE(link
),
2141 LOG_NETDEV_INTERFACE(netdev
),
2142 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2143 link_enter_failed(link
);
2148 if (link
->enslaving
== 0)
2149 return link_joined(link
);
2154 static int link_set_ipv4_forward(Link
*link
) {
2157 if (!link_ipv4_forward_enabled(link
))
2160 /* We propagate the forwarding flag from one interface to the
2161 * global setting one way. This means: as long as at least one
2162 * interface was configured at any time that had IP forwarding
2163 * enabled the setting will stay on for good. We do this
2164 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2165 * somewhat in sync (see below). */
2167 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2169 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2174 static int link_set_ipv6_forward(Link
*link
) {
2177 if (!link_ipv6_forward_enabled(link
))
2180 /* On Linux, the IPv6 stack does not know a per-interface
2181 * packet forwarding setting: either packet forwarding is on
2182 * for all, or off for all. We hence don't bother with a
2183 * per-interface setting, but simply propagate the interface
2184 * flag, if it is set, to the global flag, one-way. Note that
2185 * while IPv4 would allow a per-interface flag, we expose the
2186 * same behaviour there and also propagate the setting from
2187 * one to all, to keep things simple (see above). */
2189 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2191 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2196 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2197 IPv6PrivacyExtensions s
;
2200 s
= link_ipv6_privacy_extensions(link
);
2204 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2206 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2211 static int link_set_ipv6_accept_ra(Link
*link
) {
2214 /* Make this a NOP if IPv6 is not available */
2215 if (!socket_ipv6_is_supported())
2218 if (link
->flags
& IFF_LOOPBACK
)
2224 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2226 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2231 static int link_set_ipv6_dad_transmits(Link
*link
) {
2234 /* Make this a NOP if IPv6 is not available */
2235 if (!socket_ipv6_is_supported())
2238 if (link
->flags
& IFF_LOOPBACK
)
2244 if (link
->network
->ipv6_dad_transmits
< 0)
2247 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2249 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2254 static int link_set_ipv6_hop_limit(Link
*link
) {
2257 /* Make this a NOP if IPv6 is not available */
2258 if (!socket_ipv6_is_supported())
2261 if (link
->flags
& IFF_LOOPBACK
)
2267 if (link
->network
->ipv6_hop_limit
< 0)
2270 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2272 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2277 static int link_set_ipv6_mtu(Link
*link
) {
2280 /* Make this a NOP if IPv6 is not available */
2281 if (!socket_ipv6_is_supported())
2284 if (link
->flags
& IFF_LOOPBACK
)
2287 if (link
->network
->ipv6_mtu
== 0)
2290 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2292 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2297 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2298 Address
*net_address
;
2306 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2307 if (address_equal(net_address
, address
))
2313 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2322 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2323 if (route_equal(net_route
, route
))
2329 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2336 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2339 /* Even when the address is leased from a DHCP server, networkd assign the address
2340 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2341 * corresponding routes with RTPROT_DHCP. */
2342 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2343 if (route
->protocol
!= RTPROT_DHCP
)
2346 if (address
->family
!= route
->family
)
2349 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2356 static int link_drop_foreign_config(Link
*link
) {
2362 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2363 /* we consider IPv6LL addresses to be managed by the kernel */
2364 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2367 if (link_address_is_dynamic(link
, address
)) {
2368 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2370 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2373 if (link_is_static_address_configured(link
, address
)) {
2374 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2376 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2378 r
= address_remove(address
, link
, NULL
);
2384 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2385 /* do not touch routes managed by the kernel */
2386 if (route
->protocol
== RTPROT_KERNEL
)
2389 if (route
->protocol
== RTPROT_STATIC
&&
2390 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2393 if (route
->protocol
== RTPROT_DHCP
&&
2394 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2397 if (link_is_static_route_configured(link
, route
)) {
2398 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2402 r
= route_remove(route
, link
, NULL
);
2411 static int link_drop_config(Link
*link
) {
2412 Address
*address
, *pool_address
;
2417 SET_FOREACH(address
, link
->addresses
, i
) {
2418 /* we consider IPv6LL addresses to be managed by the kernel */
2419 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2422 r
= address_remove(address
, link
, NULL
);
2426 /* If this address came from an address pool, clean up the pool */
2427 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2428 if (address_equal(address
, pool_address
)) {
2429 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2430 address_free(pool_address
);
2436 SET_FOREACH(route
, link
->routes
, i
) {
2437 /* do not touch routes managed by the kernel */
2438 if (route
->protocol
== RTPROT_KERNEL
)
2441 r
= route_remove(route
, link
, NULL
);
2451 static int link_configure(Link
*link
) {
2455 assert(link
->network
);
2456 assert(link
->state
== LINK_STATE_INITIALIZED
);
2458 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
2459 return link_configure_can(link
);
2461 /* Drop foreign config, but ignore loopback or critical devices.
2462 * We do not want to remove loopback address or addresses used for root NFS. */
2463 if (!(link
->flags
& IFF_LOOPBACK
) &&
2464 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2465 r
= link_drop_foreign_config(link
);
2470 r
= link_set_proxy_arp(link
);
2474 r
= ipv6_proxy_ndp_addresses_configure(link
);
2478 r
= link_set_ipv4_forward(link
);
2482 r
= link_set_ipv6_forward(link
);
2486 r
= link_set_ipv6_privacy_extensions(link
);
2490 r
= link_set_ipv6_accept_ra(link
);
2494 r
= link_set_ipv6_dad_transmits(link
);
2498 r
= link_set_ipv6_hop_limit(link
);
2502 r
= link_set_flags(link
);
2506 r
= link_set_ipv6_mtu(link
);
2510 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2511 r
= ipv4ll_configure(link
);
2516 if (link_dhcp4_enabled(link
)) {
2517 r
= dhcp4_set_promote_secondaries(link
);
2521 r
= dhcp4_configure(link
);
2526 if (link_dhcp4_server_enabled(link
)) {
2527 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2531 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2536 if (link_dhcp6_enabled(link
) ||
2537 link_ipv6_accept_ra_enabled(link
)) {
2538 r
= dhcp6_configure(link
);
2543 if (link_ipv6_accept_ra_enabled(link
)) {
2544 r
= ndisc_configure(link
);
2549 if (link_radv_enabled(link
)) {
2550 r
= radv_configure(link
);
2555 if (link_lldp_rx_enabled(link
)) {
2556 r
= link_lldp_rx_configure(link
);
2561 r
= link_configure_mtu(link
);
2565 r
= link_configure_addrgen_mode(link
);
2569 return link_configure_after_setting_mtu(link
);
2572 static int link_configure_after_setting_mtu(Link
*link
) {
2576 assert(link
->network
);
2577 assert(link
->state
== LINK_STATE_INITIALIZED
);
2579 if (link
->setting_mtu
)
2582 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2583 r
= link_acquire_conf(link
);
2588 return link_enter_join_netdev(link
);
2591 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2594 if (duid
->raw_data_len
> 0)
2597 if (duid
->type
!= DUID_TYPE_UUID
)
2600 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2601 duid
->raw_data_len
= sizeof(sd_id128_t
);
2606 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2607 Manager
*manager
= userdata
;
2608 const sd_bus_error
*e
;
2618 e
= sd_bus_message_get_error(m
);
2620 log_error_errno(sd_bus_error_get_errno(e
),
2621 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2626 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2630 if (sz
!= sizeof(sd_id128_t
)) {
2631 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2635 memcpy(&manager
->product_uuid
, a
, sz
);
2636 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2637 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2639 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2642 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2643 r
= link_configure(link
);
2645 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2648 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2650 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2651 * even if the method fails. */
2652 manager
->has_product_uuid
= true;
2657 static bool link_requires_uuid(Link
*link
) {
2661 assert(link
->manager
);
2662 assert(link
->network
);
2664 duid
= link_get_duid(link
);
2665 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2668 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2671 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2677 static int link_configure_duid(Link
*link
) {
2683 assert(link
->manager
);
2684 assert(link
->network
);
2687 duid
= link_get_duid(link
);
2689 if (!link_requires_uuid(link
))
2692 if (m
->has_product_uuid
) {
2693 (void) duid_set_uuid(duid
, m
->product_uuid
);
2697 if (!m
->links_requesting_uuid
) {
2698 r
= manager_request_product_uuid(m
, link
);
2703 log_link_warning_errno(link
, r
,
2704 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2708 r
= set_put(m
->links_requesting_uuid
, link
);
2712 r
= set_put(m
->duids_requesting_uuid
, duid
);
2720 static int link_initialized_and_synced(Link
*link
) {
2725 assert(link
->ifname
);
2726 assert(link
->manager
);
2728 /* We may get called either from the asynchronous netlink callback,
2729 * or directly for link_add() if running in a container. See link_add(). */
2730 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2733 log_link_debug(link
, "Link state is up-to-date");
2734 link_set_state(link
, LINK_STATE_INITIALIZED
);
2736 r
= link_new_bound_by_list(link
);
2740 r
= link_handle_bound_by_list(link
);
2744 if (!link
->network
) {
2745 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2746 &link
->mac
, &network
);
2748 link_enter_unmanaged(link
);
2750 } else if (r
== 0 && network
->unmanaged
) {
2751 link_enter_unmanaged(link
);
2756 if (link
->flags
& IFF_LOOPBACK
) {
2757 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2758 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2760 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2761 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2763 if (network
->dhcp_server
)
2764 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2767 r
= network_apply(network
, link
);
2772 r
= link_new_bound_to_list(link
);
2776 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2777 * link_configure() is called later asynchronously. */
2778 r
= link_configure_duid(link
);
2782 r
= link_configure(link
);
2789 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2790 (void) link_initialized_and_synced(link
);
2794 int link_initialized(Link
*link
, sd_device
*device
) {
2795 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2799 assert(link
->manager
);
2800 assert(link
->manager
->rtnl
);
2803 if (link
->state
!= LINK_STATE_PENDING
)
2806 if (link
->sd_device
)
2809 log_link_debug(link
, "udev initialized link");
2810 link_set_state(link
, LINK_STATE_INITIALIZED
);
2812 link
->sd_device
= sd_device_ref(device
);
2814 /* udev has initialized the link, but we don't know if we have yet
2815 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2816 * when it returns we know that the pending NEWLINKs have already been
2817 * processed and that we are up-to-date */
2819 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2824 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2825 link_netlink_destroy_callback
, link
);
2834 static int link_load(Link
*link
) {
2835 _cleanup_free_
char *network_file
= NULL
,
2838 *dhcp4_address
= NULL
,
2839 *ipv4ll_address
= NULL
;
2840 union in_addr_union address
;
2841 union in_addr_union route_dst
;
2847 r
= parse_env_file(NULL
, link
->state_file
,
2848 "NETWORK_FILE", &network_file
,
2849 "ADDRESSES", &addresses
,
2851 "DHCP4_ADDRESS", &dhcp4_address
,
2852 "IPV4LL_ADDRESS", &ipv4ll_address
);
2853 if (r
< 0 && r
!= -ENOENT
)
2854 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2861 suffix
= strrchr(network_file
, '.');
2863 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2864 goto network_file_fail
;
2868 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2870 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2871 goto network_file_fail
;
2874 r
= network_apply(network
, link
);
2876 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2885 _cleanup_free_
char *address_str
= NULL
;
2886 char *prefixlen_str
;
2888 unsigned char prefixlen
;
2890 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2892 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2898 prefixlen_str
= strchr(address_str
, '/');
2899 if (!prefixlen_str
) {
2900 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2904 *prefixlen_str
++ = '\0';
2906 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2908 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2912 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2914 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2918 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2920 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2929 _cleanup_free_
char *route_str
= NULL
;
2930 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2932 char *prefixlen_str
;
2934 unsigned char prefixlen
, tos
, table
;
2937 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2939 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2945 prefixlen_str
= strchr(route_str
, '/');
2946 if (!prefixlen_str
) {
2947 log_link_debug(link
, "Failed to parse route %s", route_str
);
2951 *prefixlen_str
++ = '\0';
2953 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2955 log_link_debug(link
,
2956 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2961 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2963 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2967 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2969 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2971 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
2972 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2973 0, route_expire_handler
, route
);
2975 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2978 route
->lifetime
= lifetime
;
2979 sd_event_source_unref(route
->expire
);
2980 route
->expire
= TAKE_PTR(expire
);
2984 if (dhcp4_address
) {
2985 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2987 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2988 goto dhcp4_address_fail
;
2991 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
2993 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2995 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2997 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3002 if (ipv4ll_address
) {
3003 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3005 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3006 goto ipv4ll_address_fail
;
3009 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3011 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3013 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3015 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3018 ipv4ll_address_fail
:
3023 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3024 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3025 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3034 r
= link_new(m
, message
, ret
);
3040 log_link_debug(link
, "Link %d added", link
->ifindex
);
3042 r
= link_load(link
);
3046 if (detect_container() <= 0) {
3047 /* not in a container, udev will be around */
3048 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3049 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3051 log_link_warning_errno(link
, r
, "Could not find device: %m");
3055 r
= sd_device_get_is_initialized(device
);
3057 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3062 log_link_debug(link
, "link pending udev initialization...");
3066 r
= device_is_renaming(device
);
3068 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3072 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3076 r
= link_initialized(link
, device
);
3080 r
= link_initialized_and_synced(link
);
3087 link_enter_failed(link
);
3091 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3096 log_link_info(link
, "Gained IPv6LL");
3098 link
->ipv6ll_address
= *address
;
3099 link_check_ready(link
);
3101 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3102 r
= link_acquire_ipv6_conf(link
);
3104 link_enter_failed(link
);
3112 static int link_carrier_gained(Link
*link
) {
3117 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3118 r
= link_acquire_conf(link
);
3120 link_enter_failed(link
);
3124 link_set_state(link
, LINK_STATE_CONFIGURING
);
3125 r
= link_request_set_addresses(link
);
3130 r
= link_handle_bound_by_list(link
);
3137 static int link_carrier_lost(Link
*link
) {
3142 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3145 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3146 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3147 * configuration and stop the clients as well. */
3148 if (link
->setting_mtu
)
3151 r
= link_stop_clients(link
, false);
3153 link_enter_failed(link
);
3157 if (link_dhcp4_server_enabled(link
))
3158 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3160 r
= link_drop_config(link
);
3164 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3165 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3166 r
= link_drop_foreign_config(link
);
3171 r
= link_handle_bound_by_list(link
);
3178 int link_carrier_reset(Link
*link
) {
3183 if (link_has_carrier(link
)) {
3184 r
= link_carrier_lost(link
);
3188 r
= link_carrier_gained(link
);
3192 log_link_info(link
, "Reset carrier");
3198 int link_update(Link
*link
, sd_netlink_message
*m
) {
3199 struct ether_addr mac
;
3202 bool had_carrier
, carrier_gained
, carrier_lost
;
3206 assert(link
->ifname
);
3209 if (link
->state
== LINK_STATE_LINGER
) {
3210 log_link_info(link
, "Link re-added");
3211 link_set_state(link
, LINK_STATE_CONFIGURING
);
3213 r
= link_new_carrier_maps(link
);
3218 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3219 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3220 Manager
*manager
= link
->manager
;
3222 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3225 r
= link_add(manager
, m
, &link
);
3230 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3231 if (r
>= 0 && mtu
> 0) {
3233 if (link
->original_mtu
== 0) {
3234 link
->original_mtu
= mtu
;
3235 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3238 if (link
->dhcp_client
) {
3239 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3242 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3246 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3248 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3252 /* The kernel may broadcast NEWLINK messages without the MAC address
3253 set, simply ignore them. */
3254 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3256 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3259 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3262 log_link_debug(link
, "MAC address: "
3263 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3264 mac
.ether_addr_octet
[0],
3265 mac
.ether_addr_octet
[1],
3266 mac
.ether_addr_octet
[2],
3267 mac
.ether_addr_octet
[3],
3268 mac
.ether_addr_octet
[4],
3269 mac
.ether_addr_octet
[5]);
3272 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3274 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3277 if (link
->dhcp_client
) {
3278 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3279 (const uint8_t *) &link
->mac
,
3283 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3285 r
= dhcp4_set_client_identifier(link
);
3290 if (link
->dhcp6_client
) {
3291 const DUID
* duid
= link_get_duid(link
);
3293 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3294 (const uint8_t *) &link
->mac
,
3298 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3300 if (link
->network
->iaid_set
) {
3301 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3302 link
->network
->iaid
);
3304 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3307 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3309 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3310 duid
->raw_data_len
);
3312 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3316 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3318 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3322 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3324 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3329 old_master
= link
->master_ifindex
;
3330 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3332 had_carrier
= link_has_carrier(link
);
3334 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3338 r
= link_update_lldp(link
);
3342 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3343 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3345 if (carrier_gained
) {
3346 log_link_info(link
, "Gained carrier");
3348 r
= link_carrier_gained(link
);
3351 } else if (carrier_lost
) {
3352 log_link_info(link
, "Lost carrier");
3354 r
= link_carrier_lost(link
);
3362 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3370 if (hashmap_isempty(h
))
3374 HASHMAP_FOREACH(link
, h
, i
) {
3378 fprintf(f
, "%i", link
->ifindex
);
3385 int link_save(Link
*link
) {
3386 _cleanup_free_
char *temp_path
= NULL
;
3387 _cleanup_fclose_
FILE *f
= NULL
;
3388 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
3395 assert(link
->state_file
);
3396 assert(link
->lease_file
);
3397 assert(link
->manager
);
3399 if (link
->state
== LINK_STATE_LINGER
) {
3400 (void) unlink(link
->state_file
);
3404 link_lldp_save(link
);
3406 admin_state
= link_state_to_string(link
->state
);
3407 assert(admin_state
);
3409 oper_state
= link_operstate_to_string(link
->operstate
);
3412 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
3413 assert(carrier_state
);
3415 address_state
= link_address_state_to_string(link
->address_state
);
3416 assert(address_state
);
3418 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3422 (void) fchmod(fileno(f
), 0644);
3425 "# This is private data. Do not parse.\n"
3428 "CARRIER_STATE=%s\n"
3429 "ADDRESS_STATE=%s\n",
3430 admin_state
, oper_state
, carrier_state
, address_state
);
3432 if (link
->network
) {
3433 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3434 const char *dhcp_domainname
= NULL
, *p
;
3435 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3439 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3440 yes_no(link
->network
->required_for_online
));
3442 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3443 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3445 if (link
->dhcp6_client
) {
3446 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3447 if (r
< 0 && r
!= -ENOMSG
)
3448 log_link_debug(link
, "No DHCPv6 lease");
3451 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3456 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3457 _cleanup_free_
char *b
= NULL
;
3459 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3460 &link
->network
->dns
[j
].address
, &b
);
3462 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3472 if (link
->network
->dhcp_use_dns
&&
3474 const struct in_addr
*addresses
;
3476 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3478 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3482 if (link
->network
->dhcp6_use_dns
&& dhcp6_lease
) {
3483 struct in6_addr
*in6_addrs
;
3485 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3489 serialize_in6_addrs(f
, in6_addrs
, r
);
3494 /* Make sure to flush out old entries before we use the NDISC data */
3497 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3500 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3504 serialize_in6_addrs(f
, &dd
->address
, 1);
3513 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3515 if (link
->network
->dhcp_use_ntp
&&
3517 const struct in_addr
*addresses
;
3519 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3521 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3525 if (link
->network
->dhcp6_use_ntp
&& dhcp6_lease
) {
3526 struct in6_addr
*in6_addrs
;
3529 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3534 serialize_in6_addrs(f
, in6_addrs
, r
);
3538 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3540 fputstrv(f
, hosts
, NULL
, &space
);
3545 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3546 if (link
->dhcp_lease
) {
3547 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3548 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3551 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3554 fputs("DOMAINS=", f
);
3556 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3557 fputs_with_space(f
, p
, NULL
, &space
);
3559 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3562 if (dhcp_domainname
)
3563 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3565 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3567 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3569 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3570 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3575 fputs("ROUTE_DOMAINS=", f
);
3577 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3578 fputs_with_space(f
, p
, NULL
, &space
);
3580 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3583 if (dhcp_domainname
)
3584 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3586 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3588 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3590 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3591 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3596 fprintf(f
, "LLMNR=%s\n",
3597 resolve_support_to_string(link
->network
->llmnr
));
3598 fprintf(f
, "MDNS=%s\n",
3599 resolve_support_to_string(link
->network
->mdns
));
3600 if (link
->network
->dns_default_route
>= 0)
3601 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3603 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3604 fprintf(f
, "DNS_OVER_TLS=%s\n",
3605 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3607 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3608 fprintf(f
, "DNSSEC=%s\n",
3609 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3611 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3614 fputs("DNSSEC_NTA=", f
);
3616 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3617 fputs_with_space(f
, n
, NULL
, &space
);
3621 fputs("ADDRESSES=", f
);
3623 SET_FOREACH(a
, link
->addresses
, i
) {
3624 _cleanup_free_
char *address_str
= NULL
;
3626 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3630 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3635 fputs("ROUTES=", f
);
3637 SET_FOREACH(route
, link
->routes
, i
) {
3638 _cleanup_free_
char *route_str
= NULL
;
3640 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3644 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3645 space
? " " : "", route_str
,
3646 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3653 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3654 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3656 if (link
->dhcp_lease
) {
3657 struct in_addr address
;
3658 const char *tz
= NULL
;
3660 assert(link
->network
);
3662 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3664 fprintf(f
, "TIMEZONE=%s\n", tz
);
3666 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3668 fputs("DHCP4_ADDRESS=", f
);
3669 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3673 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3681 (void) unlink(link
->lease_file
);
3684 struct in_addr address
;
3686 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3688 fputs("IPV4LL_ADDRESS=", f
);
3689 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3694 r
= fflush_and_check(f
);
3698 if (rename(temp_path
, link
->state_file
) < 0) {
3706 (void) unlink(link
->state_file
);
3708 (void) unlink(temp_path
);
3710 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3713 /* The serialized state in /run is no longer up-to-date. */
3714 void link_dirty(Link
*link
) {
3719 /* mark manager dirty as link is dirty */
3720 manager_dirty(link
->manager
);
3722 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3724 /* allocation errors are ignored */
3727 r
= set_put(link
->manager
->dirty_links
, link
);
3729 /* don't take another ref if the link was already dirty */
3735 /* The serialized state in /run is up-to-date */
3736 void link_clean(Link
*link
) {
3738 assert(link
->manager
);
3740 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3743 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3744 [LINK_STATE_PENDING
] = "pending",
3745 [LINK_STATE_INITIALIZED
] = "initialized",
3746 [LINK_STATE_CONFIGURING
] = "configuring",
3747 [LINK_STATE_CONFIGURED
] = "configured",
3748 [LINK_STATE_UNMANAGED
] = "unmanaged",
3749 [LINK_STATE_FAILED
] = "failed",
3750 [LINK_STATE_LINGER
] = "linger",
3753 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);