1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
8 #include "alloc-util.h"
10 #include "dhcp-identifier.h"
11 #include "dhcp-lease-internal.h"
15 #include "missing_network.h"
16 #include "netdev/bond.h"
17 #include "netdev/bridge.h"
18 #include "netdev/ipvlan.h"
19 #include "netdev/vrf.h"
20 #include "netlink-util.h"
21 #include "network-internal.h"
22 #include "networkd-can.h"
23 #include "networkd-dhcp-server.h"
24 #include "networkd-dhcp4.h"
25 #include "networkd-dhcp6.h"
26 #include "networkd-ipv4ll.h"
27 #include "networkd-ipv6-proxy-ndp.h"
28 #include "networkd-link-bus.h"
29 #include "networkd-link.h"
30 #include "networkd-lldp-tx.h"
31 #include "networkd-manager.h"
32 #include "networkd-ndisc.h"
33 #include "networkd-neighbor.h"
34 #include "networkd-radv.h"
35 #include "networkd-routing-policy-rule.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
41 #include "sysctl-util.h"
42 #include "tmpfile-util.h"
43 #include "udev-util.h"
47 uint32_t link_get_vrf_table(Link
*link
) {
48 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
51 uint32_t link_get_dhcp_route_table(Link
*link
) {
52 /* When the interface is part of an VRF use the VRFs routing table, unless
53 * another table is explicitly specified. */
54 if (link
->network
->dhcp_route_table_set
)
55 return link
->network
->dhcp_route_table
;
56 return link_get_vrf_table(link
);
59 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
60 if (link
->network
->ipv6_accept_ra_route_table_set
)
61 return link
->network
->ipv6_accept_ra_route_table
;
62 return link_get_vrf_table(link
);
65 DUID
* link_get_duid(Link
*link
) {
66 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
67 return &link
->network
->duid
;
69 return &link
->manager
->duid
;
72 int link_sysctl_ipv6_enabled(Link
*link
) {
73 _cleanup_free_
char *value
= NULL
;
79 if (link
->sysctl_ipv6_enabled
>= 0)
80 return link
->sysctl_ipv6_enabled
;
82 const char *ifname
= link
->ifname
; /* work around bogus gcc warning */
83 r
= sysctl_read_ip_property(AF_INET6
, ifname
, "disable_ipv6", &value
);
85 return log_link_warning_errno(link
, r
,
86 "Failed to read net.ipv6.conf.%s.disable_ipv6 sysctl property: %m",
89 link
->sysctl_ipv6_enabled
= value
[0] == '0';
90 return link
->sysctl_ipv6_enabled
;
93 static bool link_dhcp6_enabled(Link
*link
) {
96 if (!socket_ipv6_is_supported())
99 if (link
->flags
& IFF_LOOPBACK
)
105 if (link
->network
->bond
)
108 if (link
->iftype
== ARPHRD_CAN
)
111 if (link_sysctl_ipv6_enabled(link
) == 0)
114 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
117 static bool link_dhcp4_enabled(Link
*link
) {
120 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->network
->bond
)
129 if (link
->iftype
== ARPHRD_CAN
)
132 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
135 static bool link_dhcp4_server_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->network
->bond
)
147 if (link
->iftype
== ARPHRD_CAN
)
150 return link
->network
->dhcp_server
;
153 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
155 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->iftype
== ARPHRD_CAN
)
166 if (STRPTR_IN_SET(link
->kind
,
167 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
168 "vti6", "nlmon", "xfrm"))
171 /* L3 or L3S mode do not support ARP. */
172 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
175 if (link
->network
->bond
)
178 return link
->network
->link_local
& mask
;
181 static bool link_ipv6ll_enabled(Link
*link
) {
184 if (!socket_ipv6_is_supported())
187 if (link
->flags
& IFF_LOOPBACK
)
193 if (link
->iftype
== ARPHRD_CAN
)
196 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
199 if (link
->network
->bond
)
202 if (link_sysctl_ipv6_enabled(link
) == 0)
205 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
208 static bool link_ipv6_enabled(Link
*link
) {
211 if (!socket_ipv6_is_supported())
214 if (link
->network
->bond
)
217 if (link_sysctl_ipv6_enabled(link
) == 0)
220 if (link
->iftype
== ARPHRD_CAN
)
223 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
224 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
227 static bool link_radv_enabled(Link
*link
) {
230 if (!link_ipv6ll_enabled(link
))
233 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
236 static bool link_ipv4_forward_enabled(Link
*link
) {
239 if (link
->flags
& IFF_LOOPBACK
)
245 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
248 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
251 static bool link_ipv6_forward_enabled(Link
*link
) {
254 if (!socket_ipv6_is_supported())
257 if (link
->flags
& IFF_LOOPBACK
)
263 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
266 if (link_sysctl_ipv6_enabled(link
) == 0)
269 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
272 static bool link_proxy_arp_enabled(Link
*link
) {
275 if (link
->flags
& IFF_LOOPBACK
)
281 if (link
->network
->proxy_arp
< 0)
287 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
290 if (!socket_ipv6_is_supported())
293 if (link
->flags
& IFF_LOOPBACK
)
299 if (!link_ipv6ll_enabled(link
))
302 /* If unset use system default (enabled if local forwarding is disabled.
303 * disabled if local forwarding is enabled).
304 * If set, ignore or enforce RA independent of local forwarding state.
306 if (link
->network
->ipv6_accept_ra
< 0)
307 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
308 return !link_ipv6_forward_enabled(link
);
309 else if (link
->network
->ipv6_accept_ra
> 0)
310 /* accept RA even if ip_forward is enabled */
317 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
320 if (!socket_ipv6_is_supported())
321 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
323 if (link
->flags
& IFF_LOOPBACK
)
324 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
327 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
329 return link
->network
->ipv6_privacy_extensions
;
332 static int link_enable_ipv6(Link
*link
) {
336 if (link
->flags
& IFF_LOOPBACK
)
339 enabled
= link_ipv6_enabled(link
);
341 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", false);
343 log_link_warning_errno(link
, r
, "Cannot enable IPv6: %m");
345 log_link_info(link
, "IPv6 successfully enabled");
351 static bool link_is_enslaved(Link
*link
) {
352 if (link
->flags
& IFF_SLAVE
)
353 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
359 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
362 /* TODO: add conditions for other netdevs. */
367 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
373 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
376 link_update_operstate(master
, true);
379 void link_update_operstate(Link
*link
, bool also_update_master
) {
380 LinkOperationalState operstate
;
381 LinkCarrierState carrier_state
;
382 LinkAddressState address_state
;
383 _cleanup_strv_free_
char **p
= NULL
;
384 uint8_t scope
= RT_SCOPE_NOWHERE
;
385 bool changed
= false;
391 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
392 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
393 else if (link_has_carrier(link
)) {
394 if (link_is_enslaved(link
))
395 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
397 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
398 } else if (link
->flags
& IFF_UP
)
399 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
401 carrier_state
= LINK_CARRIER_STATE_OFF
;
403 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
406 SET_FOREACH(slave
, link
->slaves
, i
) {
407 link_update_operstate(slave
, false);
409 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
410 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
414 SET_FOREACH(address
, link
->addresses
, i
) {
415 if (!address_is_ready(address
))
418 if (address
->scope
< scope
)
419 scope
= address
->scope
;
422 /* for operstate we also take foreign addresses into account */
423 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
424 if (!address_is_ready(address
))
427 if (address
->scope
< scope
)
428 scope
= address
->scope
;
431 if (scope
< RT_SCOPE_SITE
)
432 /* universally accessible addresses found */
433 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
434 else if (scope
< RT_SCOPE_HOST
)
435 /* only link or site local addresses found */
436 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
438 /* no useful addresses found */
439 address_state
= LINK_ADDRESS_STATE_OFF
;
441 /* Mapping of address and carrier state vs operational state
443 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
444 * ------------------------------------------------------------------------------
445 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
446 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
447 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
450 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
451 operstate
= (LinkOperationalState
) carrier_state
;
452 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
453 operstate
= LINK_OPERSTATE_ROUTABLE
;
454 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
455 operstate
= LINK_OPERSTATE_DEGRADED
;
457 operstate
= LINK_OPERSTATE_ENSLAVED
;
459 if (link
->carrier_state
!= carrier_state
) {
460 link
->carrier_state
= carrier_state
;
462 if (strv_extend(&p
, "CarrierState") < 0)
466 if (link
->address_state
!= address_state
) {
467 link
->address_state
= address_state
;
469 if (strv_extend(&p
, "AddressState") < 0)
473 if (link
->operstate
!= operstate
) {
474 link
->operstate
= operstate
;
476 if (strv_extend(&p
, "OperationalState") < 0)
481 link_send_changed_strv(link
, p
);
485 if (also_update_master
&& link
->network
) {
486 link_update_master_operstate(link
, link
->network
->bond
);
487 link_update_master_operstate(link
, link
->network
->bridge
);
491 #define FLAG_STRING(string, flag, old, new) \
492 (((old ^ new) & flag) \
493 ? ((old & flag) ? (" -" string) : (" +" string)) \
496 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
497 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
503 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
505 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
507 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
509 /* if we got a message without operstate, take it to mean
510 the state was unchanged */
511 operstate
= link
->kernel_operstate
;
513 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
516 if (link
->flags
!= flags
) {
517 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",
518 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
519 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
520 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
521 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
522 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
523 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
524 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
525 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
526 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
527 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
528 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
529 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
530 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
531 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
532 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
533 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
534 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
535 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
536 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
538 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
539 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
540 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
541 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
542 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
543 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
544 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
545 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
547 /* link flags are currently at most 18 bits, let's align to
549 if (unknown_flags_added
)
551 "Unknown link flags gained: %#.5x (ignoring)",
552 unknown_flags_added
);
554 if (unknown_flags_removed
)
556 "Unknown link flags lost: %#.5x (ignoring)",
557 unknown_flags_removed
);
561 link
->kernel_operstate
= operstate
;
563 link_update_operstate(link
, true);
568 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
569 _cleanup_(link_unrefp
) Link
*link
= NULL
;
571 const char *ifname
, *kind
= NULL
;
573 unsigned short iftype
;
579 /* check for link kind */
580 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
582 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
583 r
= sd_netlink_message_exit_container(message
);
588 r
= sd_netlink_message_get_type(message
, &type
);
591 else if (type
!= RTM_NEWLINK
)
594 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
597 else if (ifindex
<= 0)
600 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
604 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
615 .state
= LINK_STATE_PENDING
,
618 .sysctl_ipv6_enabled
= -1,
620 .n_dns
= (unsigned) -1,
621 .dns_default_route
= -1,
622 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
623 .mdns
= _RESOLVE_SUPPORT_INVALID
,
624 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
625 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
628 link
->ifname
= strdup(ifname
);
633 link
->kind
= strdup(kind
);
638 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
640 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
642 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
644 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
646 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
649 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
652 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
655 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
659 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
663 r
= link_update_flags(link
, message
, false);
667 *ret
= TAKE_PTR(link
);
672 void link_ntp_settings_clear(Link
*link
) {
673 link
->ntp
= strv_free(link
->ntp
);
676 void link_dns_settings_clear(Link
*link
) {
677 link
->dns
= mfree(link
->dns
);
678 link
->n_dns
= (unsigned) -1;
680 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
681 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
683 link
->dns_default_route
= -1;
684 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
685 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
686 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
687 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
689 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
692 static Link
*link_free(Link
*link
) {
697 link_ntp_settings_clear(link
);
698 link_dns_settings_clear(link
);
700 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
701 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
703 link
->neighbors
= set_free_with_destructor(link
->neighbors
, neighbor_free
);
704 link
->neighbors_foreign
= set_free_with_destructor(link
->neighbors_foreign
, neighbor_free
);
706 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
707 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
709 while ((address
= link
->pool_addresses
)) {
710 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
711 address_free(address
);
714 sd_dhcp_server_unref(link
->dhcp_server
);
715 sd_dhcp_client_unref(link
->dhcp_client
);
716 sd_dhcp_lease_unref(link
->dhcp_lease
);
717 set_free(link
->dhcp_routes
);
719 link_lldp_emit_stop(link
);
721 free(link
->lease_file
);
723 sd_lldp_unref(link
->lldp
);
724 free(link
->lldp_file
);
728 sd_ipv4ll_unref(link
->ipv4ll
);
729 sd_dhcp6_client_unref(link
->dhcp6_client
);
730 sd_ndisc_unref(link
->ndisc
);
731 sd_radv_unref(link
->radv
);
736 (void) unlink(link
->state_file
);
737 free(link
->state_file
);
739 sd_device_unref(link
->sd_device
);
741 hashmap_free(link
->bound_to_links
);
742 hashmap_free(link
->bound_by_links
);
744 set_free_with_destructor(link
->slaves
, link_unref
);
746 network_unref(link
->network
);
751 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
753 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
760 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
769 void link_set_state(Link
*link
, LinkState state
) {
772 if (link
->state
== state
)
775 log_link_debug(link
, "State changed: %s -> %s",
776 link_state_to_string(link
->state
),
777 link_state_to_string(state
));
781 link_send_changed(link
, "AdministrativeState", NULL
);
784 static void link_enter_unmanaged(Link
*link
) {
787 link_set_state(link
, LINK_STATE_UNMANAGED
);
792 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
796 assert(link
->manager
);
797 assert(link
->manager
->event
);
799 dhcp4_release_old_lease(link
);
801 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
802 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
803 k
= sd_dhcp_client_stop(link
->dhcp_client
);
805 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
809 k
= sd_ipv4ll_stop(link
->ipv4ll
);
811 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
814 if (link
->dhcp6_client
) {
815 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
817 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
821 k
= sd_ndisc_stop(link
->ndisc
);
823 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
827 k
= sd_radv_stop(link
->radv
);
829 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
832 link_lldp_emit_stop(link
);
836 void link_enter_failed(Link
*link
) {
839 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
842 log_link_warning(link
, "Failed");
844 link_set_state(link
, LINK_STATE_FAILED
);
846 link_stop_clients(link
, false);
851 static int link_join_netdevs_after_configured(Link
*link
) {
856 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
857 if (netdev
->ifindex
> 0)
858 /* Assume already enslaved. */
861 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
864 log_struct(LOG_DEBUG
,
865 LOG_LINK_INTERFACE(link
),
866 LOG_NETDEV_INTERFACE(netdev
),
867 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
869 r
= netdev_join(netdev
, link
, NULL
);
871 return log_struct_errno(LOG_WARNING
, r
,
872 LOG_LINK_INTERFACE(link
),
873 LOG_NETDEV_INTERFACE(netdev
),
874 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
880 static void link_enter_configured(Link
*link
) {
882 assert(link
->network
);
884 if (link
->state
!= LINK_STATE_CONFIGURING
)
887 log_link_info(link
, "Configured");
889 link_set_state(link
, LINK_STATE_CONFIGURED
);
891 (void) link_join_netdevs_after_configured(link
);
896 static int link_request_set_routing_policy_rule(Link
*link
) {
897 RoutingPolicyRule
*rule
, *rrule
= NULL
;
901 assert(link
->network
);
903 link
->routing_policy_rules_configured
= false;
905 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
906 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
909 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
913 r
= routing_policy_rule_configure(rule
, link
, NULL
);
915 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
917 link
->routing_policy_rule_messages
++;
920 routing_policy_rule_purge(link
->manager
, link
);
921 if (link
->routing_policy_rule_messages
== 0) {
922 link
->routing_policy_rules_configured
= true;
923 link_check_ready(link
);
925 log_link_debug(link
, "Setting routing policy rules");
926 link_set_state(link
, LINK_STATE_CONFIGURING
);
932 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
936 assert(link
->route_messages
> 0);
937 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
938 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
940 link
->route_messages
--;
942 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
945 r
= sd_netlink_message_get_errno(m
);
946 if (r
< 0 && r
!= -EEXIST
) {
947 log_link_warning_errno(link
, r
, "Could not set route: %m");
948 link_enter_failed(link
);
952 if (link
->route_messages
== 0) {
953 log_link_debug(link
, "Routes set");
954 link
->static_routes_configured
= true;
955 link_check_ready(link
);
961 int link_request_set_routes(Link
*link
) {
963 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
964 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
971 assert(link
->network
);
972 assert(link
->addresses_configured
);
973 assert(link
->address_messages
== 0);
974 assert(link
->state
!= _LINK_STATE_INVALID
);
976 link
->static_routes_configured
= false;
978 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
979 /* During configuring addresses, the link lost its carrier. As networkd is dropping
980 * the addresses now, let's not configure the routes either. */
983 r
= link_request_set_routing_policy_rule(link
);
987 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
988 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
989 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
991 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
994 r
= route_configure(rt
, link
, route_handler
);
996 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
998 link
->route_messages
++;
1001 if (link
->route_messages
== 0) {
1002 link
->static_routes_configured
= true;
1003 link_check_ready(link
);
1005 log_link_debug(link
, "Setting routes");
1006 link_set_state(link
, LINK_STATE_CONFIGURING
);
1012 void link_check_ready(Link
*link
) {
1019 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1025 if (!link
->addresses_configured
)
1028 if (!link
->neighbors_configured
)
1031 SET_FOREACH(a
, link
->addresses
, i
)
1032 if (!address_is_ready(a
))
1035 if (!link
->addresses_ready
) {
1036 link
->addresses_ready
= true;
1037 r
= link_request_set_routes(link
);
1039 link_enter_failed(link
);
1043 if (!link
->static_routes_configured
)
1046 if (!link
->routing_policy_rules_configured
)
1049 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1051 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
1054 if (link_ipv6ll_enabled(link
) &&
1055 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1058 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1059 !link
->dhcp4_configured
&&
1060 !link
->dhcp6_configured
&&
1061 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1062 /* When DHCP is enabled, at least one protocol must provide an address, or
1063 * an IPv4ll fallback address must be configured. */
1066 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1070 if (link
->state
!= LINK_STATE_CONFIGURED
)
1071 link_enter_configured(link
);
1076 static int link_request_set_neighbors(Link
*link
) {
1081 assert(link
->network
);
1082 assert(link
->state
!= _LINK_STATE_INVALID
);
1084 link
->neighbors_configured
= false;
1086 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1087 r
= neighbor_configure(neighbor
, link
, NULL
);
1089 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1092 if (link
->neighbor_messages
== 0) {
1093 link
->neighbors_configured
= true;
1094 link_check_ready(link
);
1096 log_link_debug(link
, "Setting neighbors");
1097 link_set_state(link
, LINK_STATE_CONFIGURING
);
1103 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1109 assert(link
->ifname
);
1110 assert(link
->address_messages
> 0);
1111 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1112 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1114 link
->address_messages
--;
1116 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1119 r
= sd_netlink_message_get_errno(m
);
1120 if (r
< 0 && r
!= -EEXIST
) {
1121 log_link_warning_errno(link
, r
, "could not set address: %m");
1122 link_enter_failed(link
);
1125 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1127 if (link
->address_messages
== 0) {
1128 log_link_debug(link
, "Addresses set");
1129 link
->addresses_configured
= true;
1130 link_check_ready(link
);
1136 static int link_set_bridge_fdb(Link
*link
) {
1137 FdbEntry
*fdb_entry
;
1140 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1141 r
= fdb_entry_configure(link
, fdb_entry
);
1143 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1149 static int link_request_set_addresses(Link
*link
) {
1150 AddressLabel
*label
;
1155 assert(link
->network
);
1156 assert(link
->state
!= _LINK_STATE_INVALID
);
1158 /* Reset all *_configured flags we are configuring. */
1159 link
->addresses_configured
= false;
1160 link
->addresses_ready
= false;
1161 link
->neighbors_configured
= false;
1162 link
->static_routes_configured
= false;
1163 link
->routing_policy_rules_configured
= false;
1165 r
= link_set_bridge_fdb(link
);
1169 r
= link_request_set_neighbors(link
);
1173 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1176 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1178 r
= address_configure(ad
, link
, address_handler
, update
);
1180 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1182 link
->address_messages
++;
1185 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1186 r
= address_label_configure(label
, link
, NULL
, false);
1188 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1190 link
->address_label_messages
++;
1193 /* now that we can figure out a default address for the dhcp server,
1195 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1196 r
= dhcp4_server_configure(link
);
1199 log_link_debug(link
, "Offering DHCPv4 leases");
1202 if (link
->address_messages
== 0) {
1203 link
->addresses_configured
= true;
1204 link_check_ready(link
);
1206 log_link_debug(link
, "Setting addresses");
1207 link_set_state(link
, LINK_STATE_CONFIGURING
);
1213 static int link_set_bridge_vlan(Link
*link
) {
1216 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1218 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1223 static int link_set_proxy_arp(Link
*link
) {
1226 if (!link_proxy_arp_enabled(link
))
1229 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1231 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1236 static int link_configure_after_setting_mtu(Link
*link
);
1238 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1243 assert(link
->ifname
);
1245 link
->setting_mtu
= false;
1247 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1250 r
= sd_netlink_message_get_errno(m
);
1252 log_link_warning_errno(link
, r
, "Could not set MTU, ignoring: %m");
1254 log_link_debug(link
, "Setting MTU done.");
1256 if (link
->state
== LINK_STATE_INITIALIZED
) {
1257 r
= link_configure_after_setting_mtu(link
);
1259 link_enter_failed(link
);
1265 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1266 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1270 assert(link
->manager
);
1271 assert(link
->manager
->rtnl
);
1273 if (mtu
== 0 || link
->setting_mtu
)
1276 if (link
->mtu
== mtu
)
1279 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1281 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1283 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1285 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1286 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1287 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1289 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1290 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1295 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1297 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1299 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1300 link_netlink_destroy_callback
, link
);
1302 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1305 link
->setting_mtu
= true;
1310 static bool link_reduces_vlan_mtu(Link
*link
) {
1311 /* See netif_reduces_vlan_mtu() in kernel. */
1312 return streq_ptr(link
->kind
, "macsec");
1315 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1320 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1321 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1322 /* See vlan_dev_change_mtu() in kernel. */
1323 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1325 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1326 /* See macvlan_change_mtu() in kernel. */
1332 static int link_configure_mtu(Link
*link
) {
1336 assert(link
->network
);
1338 if (link
->network
->mtu
> 0)
1339 return link_set_mtu(link
, link
->network
->mtu
);
1341 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1342 if (link
->mtu
>= mtu
)
1345 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1346 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1349 return link_set_mtu(link
, mtu
);
1352 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1357 assert(link
->ifname
);
1359 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1362 r
= sd_netlink_message_get_errno(m
);
1364 log_link_warning_errno(link
, r
, "Could not set link flags, ignoring: %m");
1369 static int link_set_flags(Link
*link
) {
1370 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1371 unsigned ifi_change
= 0;
1372 unsigned ifi_flags
= 0;
1376 assert(link
->manager
);
1377 assert(link
->manager
->rtnl
);
1379 if (link
->flags
& IFF_LOOPBACK
)
1385 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1388 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1390 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1392 if (link
->network
->arp
>= 0) {
1393 ifi_change
|= IFF_NOARP
;
1394 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1397 if (link
->network
->multicast
>= 0) {
1398 ifi_change
|= IFF_MULTICAST
;
1399 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1402 if (link
->network
->allmulticast
>= 0) {
1403 ifi_change
|= IFF_ALLMULTI
;
1404 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1407 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1409 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1411 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1412 link_netlink_destroy_callback
, link
);
1414 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1421 static int link_acquire_ipv6_conf(Link
*link
) {
1426 if (link_ipv6_accept_ra_enabled(link
)) {
1427 assert(link
->ndisc
);
1429 log_link_debug(link
, "Discovering IPv6 routers");
1431 r
= sd_ndisc_start(link
->ndisc
);
1432 if (r
< 0 && r
!= -EBUSY
)
1433 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1436 if (link_radv_enabled(link
)) {
1438 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1440 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1442 r
= sd_radv_start(link
->radv
);
1443 if (r
< 0 && r
!= -EBUSY
)
1444 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1447 (void) dhcp6_request_prefix_delegation(link
);
1452 static int link_acquire_ipv4_conf(Link
*link
) {
1456 assert(link
->manager
);
1457 assert(link
->manager
->event
);
1459 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1460 assert(link
->ipv4ll
);
1462 log_link_debug(link
, "Acquiring IPv4 link-local address");
1464 r
= sd_ipv4ll_start(link
->ipv4ll
);
1466 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1469 if (link_dhcp4_enabled(link
)) {
1470 assert(link
->dhcp_client
);
1472 log_link_debug(link
, "Acquiring DHCPv4 lease");
1474 r
= sd_dhcp_client_start(link
->dhcp_client
);
1476 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1482 static int link_acquire_conf(Link
*link
) {
1487 r
= link_acquire_ipv4_conf(link
);
1491 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1492 r
= link_acquire_ipv6_conf(link
);
1497 if (link_lldp_emit_enabled(link
)) {
1498 r
= link_lldp_emit_start(link
);
1500 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1506 bool link_has_carrier(Link
*link
) {
1507 /* see Documentation/networking/operstates.txt in the kernel sources */
1509 if (link
->kernel_operstate
== IF_OPER_UP
)
1512 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1513 /* operstate may not be implemented, so fall back to flags */
1514 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1515 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1521 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1526 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1529 r
= sd_netlink_message_get_errno(m
);
1531 log_link_warning_errno(link
, r
, "Could not set address genmode for interface, ignoring: %m");
1536 static int link_configure_addrgen_mode(Link
*link
) {
1537 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1538 uint8_t ipv6ll_mode
;
1542 assert(link
->network
);
1543 assert(link
->manager
);
1544 assert(link
->manager
->rtnl
);
1546 if (!socket_ipv6_is_supported())
1549 log_link_debug(link
, "Setting address genmode for link");
1551 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1553 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1555 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1557 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1559 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1561 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1563 if (!link_ipv6ll_enabled(link
))
1564 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1565 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1566 /* The file may not exist. And event if it exists, when stable_secret is unset,
1567 * reading the file fails with EIO. */
1568 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1570 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1572 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1574 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1576 r
= sd_netlink_message_close_container(req
);
1578 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1580 r
= sd_netlink_message_close_container(req
);
1582 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1584 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1585 link_netlink_destroy_callback
, link
);
1587 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1594 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1599 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1602 r
= sd_netlink_message_get_errno(m
);
1604 /* we warn but don't fail the link, as it may be brought up later */
1605 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1610 static int link_up(Link
*link
) {
1611 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1615 assert(link
->network
);
1616 assert(link
->manager
);
1617 assert(link
->manager
->rtnl
);
1619 log_link_debug(link
, "Bringing link up");
1621 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1623 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1625 /* set it free if not enslaved with networkd */
1626 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1627 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1629 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1632 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1634 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1636 if (link
->network
->mac
) {
1637 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1639 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1642 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1643 link_netlink_destroy_callback
, link
);
1645 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1652 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1657 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1660 r
= sd_netlink_message_get_errno(m
);
1662 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1667 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1668 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1672 assert(link
->manager
);
1673 assert(link
->manager
->rtnl
);
1675 log_link_debug(link
, "Bringing link down");
1677 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1678 RTM_SETLINK
, link
->ifindex
);
1680 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1682 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1684 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1686 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1687 callback
?: link_down_handler
,
1688 link_netlink_destroy_callback
, link
);
1690 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1697 static int link_handle_bound_to_list(Link
*link
) {
1701 bool required_up
= false;
1702 bool link_is_up
= false;
1706 if (hashmap_isempty(link
->bound_to_links
))
1709 if (link
->flags
& IFF_UP
)
1712 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1713 if (link_has_carrier(l
)) {
1718 if (!required_up
&& link_is_up
) {
1719 r
= link_down(link
, NULL
);
1722 } else if (required_up
&& !link_is_up
) {
1731 static int link_handle_bound_by_list(Link
*link
) {
1738 if (hashmap_isempty(link
->bound_by_links
))
1741 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1742 r
= link_handle_bound_to_list(l
);
1750 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1756 if (link
== carrier
)
1759 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1762 r
= hashmap_ensure_allocated(h
, NULL
);
1766 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1773 static int link_new_bound_by_list(Link
*link
) {
1778 bool list_updated
= false;
1781 assert(link
->manager
);
1785 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1786 if (!carrier
->network
)
1789 if (strv_isempty(carrier
->network
->bind_carrier
))
1792 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1793 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1797 list_updated
= true;
1804 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1805 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1809 link_dirty(carrier
);
1815 static int link_new_bound_to_list(Link
*link
) {
1820 bool list_updated
= false;
1823 assert(link
->manager
);
1828 if (strv_isempty(link
->network
->bind_carrier
))
1833 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1834 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1835 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1839 list_updated
= true;
1846 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1847 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1851 link_dirty(carrier
);
1857 static int link_new_carrier_maps(Link
*link
) {
1860 r
= link_new_bound_by_list(link
);
1864 r
= link_handle_bound_by_list(link
);
1868 r
= link_new_bound_to_list(link
);
1872 r
= link_handle_bound_to_list(link
);
1879 static void link_free_bound_to_list(Link
*link
) {
1883 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1884 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1886 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1887 link_dirty(bound_to
);
1893 static void link_free_bound_by_list(Link
*link
) {
1897 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1898 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1900 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1901 link_dirty(bound_by
);
1902 link_handle_bound_to_list(bound_by
);
1909 static void link_free_carrier_maps(Link
*link
) {
1910 bool list_updated
= false;
1914 if (!hashmap_isempty(link
->bound_to_links
)) {
1915 link_free_bound_to_list(link
);
1916 list_updated
= true;
1919 if (!hashmap_isempty(link
->bound_by_links
)) {
1920 link_free_bound_by_list(link
);
1921 list_updated
= true;
1930 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1937 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1941 r
= set_ensure_allocated(&master
->slaves
, NULL
);
1945 r
= set_put(master
->slaves
, link
);
1955 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1960 if (!link
->manager
|| !netdev
)
1963 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1966 link_unref(set_remove(master
->slaves
, link
));
1969 static void link_detach_from_manager(Link
*link
) {
1970 if (!link
|| !link
->manager
)
1973 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1976 /* The following must be called at last. */
1977 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1981 void link_drop(Link
*link
) {
1982 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1985 link_set_state(link
, LINK_STATE_LINGER
);
1987 link_free_carrier_maps(link
);
1989 if (link
->network
) {
1990 link_drop_from_master(link
, link
->network
->bridge
);
1991 link_drop_from_master(link
, link
->network
->bond
);
1994 log_link_debug(link
, "Link removed");
1996 (void) unlink(link
->state_file
);
1997 link_detach_from_manager(link
);
2000 static int link_joined(Link
*link
) {
2004 assert(link
->network
);
2006 if (!hashmap_isempty(link
->bound_to_links
)) {
2007 r
= link_handle_bound_to_list(link
);
2010 } else if (!(link
->flags
& IFF_UP
)) {
2013 link_enter_failed(link
);
2018 if (link
->network
->bridge
) {
2019 r
= link_set_bridge(link
);
2021 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2023 r
= link_append_to_master(link
, link
->network
->bridge
);
2025 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2028 if (link
->network
->bond
) {
2029 r
= link_set_bond(link
);
2031 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2033 r
= link_append_to_master(link
, link
->network
->bond
);
2035 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2038 if (link
->network
->use_br_vlan
&&
2039 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2040 r
= link_set_bridge_vlan(link
);
2042 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2045 /* Skip setting up addresses until it gets carrier,
2046 or it would try to set addresses twice,
2047 which is bad for non-idempotent steps. */
2048 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2051 link_set_state(link
, LINK_STATE_CONFIGURING
);
2052 return link_request_set_addresses(link
);
2055 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2059 assert(link
->network
);
2060 assert(link
->enslaving
> 0);
2064 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2067 r
= sd_netlink_message_get_errno(m
);
2068 if (r
< 0 && r
!= -EEXIST
) {
2069 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2070 link_enter_failed(link
);
2074 log_link_debug(link
, "Joined netdev");
2076 if (link
->enslaving
== 0) {
2077 r
= link_joined(link
);
2079 link_enter_failed(link
);
2085 static int link_enter_join_netdev(Link
*link
) {
2091 assert(link
->network
);
2092 assert(link
->state
== LINK_STATE_INITIALIZED
);
2094 link_set_state(link
, LINK_STATE_CONFIGURING
);
2097 link
->enslaving
= 0;
2099 if (link
->network
->bond
) {
2100 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2101 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2102 return link_joined(link
);
2104 log_struct(LOG_DEBUG
,
2105 LOG_LINK_INTERFACE(link
),
2106 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2107 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2111 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2113 log_struct_errno(LOG_WARNING
, r
,
2114 LOG_LINK_INTERFACE(link
),
2115 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2116 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2117 link_enter_failed(link
);
2122 if (link
->network
->bridge
) {
2123 log_struct(LOG_DEBUG
,
2124 LOG_LINK_INTERFACE(link
),
2125 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2126 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2130 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2132 log_struct_errno(LOG_WARNING
, r
,
2133 LOG_LINK_INTERFACE(link
),
2134 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2135 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2136 link_enter_failed(link
);
2141 if (link
->network
->vrf
) {
2142 log_struct(LOG_DEBUG
,
2143 LOG_LINK_INTERFACE(link
),
2144 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2145 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2149 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2151 log_struct_errno(LOG_WARNING
, r
,
2152 LOG_LINK_INTERFACE(link
),
2153 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2154 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2155 link_enter_failed(link
);
2160 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2162 if (netdev
->ifindex
> 0)
2163 /* Assume already enslaved. */
2166 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2169 log_struct(LOG_DEBUG
,
2170 LOG_LINK_INTERFACE(link
),
2171 LOG_NETDEV_INTERFACE(netdev
),
2172 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2176 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2178 log_struct_errno(LOG_WARNING
, r
,
2179 LOG_LINK_INTERFACE(link
),
2180 LOG_NETDEV_INTERFACE(netdev
),
2181 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2182 link_enter_failed(link
);
2187 if (link
->enslaving
== 0)
2188 return link_joined(link
);
2193 static int link_set_ipv4_forward(Link
*link
) {
2196 if (!link_ipv4_forward_enabled(link
))
2199 /* We propagate the forwarding flag from one interface to the
2200 * global setting one way. This means: as long as at least one
2201 * interface was configured at any time that had IP forwarding
2202 * enabled the setting will stay on for good. We do this
2203 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2204 * somewhat in sync (see below). */
2206 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2208 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2213 static int link_set_ipv6_forward(Link
*link
) {
2216 if (!link_ipv6_forward_enabled(link
))
2219 /* On Linux, the IPv6 stack does not know a per-interface
2220 * packet forwarding setting: either packet forwarding is on
2221 * for all, or off for all. We hence don't bother with a
2222 * per-interface setting, but simply propagate the interface
2223 * flag, if it is set, to the global flag, one-way. Note that
2224 * while IPv4 would allow a per-interface flag, we expose the
2225 * same behaviour there and also propagate the setting from
2226 * one to all, to keep things simple (see above). */
2228 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2230 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2235 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2236 IPv6PrivacyExtensions s
;
2239 s
= link_ipv6_privacy_extensions(link
);
2243 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2245 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2250 static int link_set_ipv6_accept_ra(Link
*link
) {
2253 /* Make this a NOP if IPv6 is not available */
2254 if (!socket_ipv6_is_supported())
2257 if (link
->flags
& IFF_LOOPBACK
)
2263 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2265 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2270 static int link_set_ipv6_dad_transmits(Link
*link
) {
2273 /* Make this a NOP if IPv6 is not available */
2274 if (!socket_ipv6_is_supported())
2277 if (link
->flags
& IFF_LOOPBACK
)
2283 if (link
->network
->ipv6_dad_transmits
< 0)
2286 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2288 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2293 static int link_set_ipv6_hop_limit(Link
*link
) {
2296 /* Make this a NOP if IPv6 is not available */
2297 if (!socket_ipv6_is_supported())
2300 if (link
->flags
& IFF_LOOPBACK
)
2306 if (link
->network
->ipv6_hop_limit
< 0)
2309 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2311 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2316 static int link_set_ipv6_mtu(Link
*link
) {
2319 /* Make this a NOP if IPv6 is not available */
2320 if (!socket_ipv6_is_supported())
2323 if (link
->flags
& IFF_LOOPBACK
)
2326 if (link
->network
->ipv6_mtu
== 0)
2329 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2331 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2336 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2337 Address
*net_address
;
2345 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2346 if (address_equal(net_address
, address
))
2352 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2353 Neighbor
*net_neighbor
;
2361 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2362 if (neighbor_equal(net_neighbor
, neighbor
))
2368 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2377 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2378 if (route_equal(net_route
, route
))
2384 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2391 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2394 /* Even when the address is leased from a DHCP server, networkd assign the address
2395 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2396 * corresponding routes with RTPROT_DHCP. */
2397 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2398 if (route
->protocol
!= RTPROT_DHCP
)
2401 if (address
->family
!= route
->family
)
2404 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2411 static int link_drop_foreign_config(Link
*link
) {
2418 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2419 /* we consider IPv6LL addresses to be managed by the kernel */
2420 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2423 if (link_address_is_dynamic(link
, address
)) {
2424 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2426 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2429 if (link_is_static_address_configured(link
, address
)) {
2430 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2432 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2434 r
= address_remove(address
, link
, NULL
);
2440 SET_FOREACH(neighbor
, link
->neighbors_foreign
, i
) {
2441 if (link_is_neighbor_configured(link
, neighbor
)) {
2442 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2446 r
= neighbor_remove(neighbor
, link
, NULL
);
2452 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2453 /* do not touch routes managed by the kernel */
2454 if (route
->protocol
== RTPROT_KERNEL
)
2457 /* do not touch multicast route added by kernel */
2458 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2459 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2460 if (route
->protocol
== RTPROT_BOOT
&&
2461 route
->family
== AF_INET6
&&
2462 route
->dst_prefixlen
== 8 &&
2463 in_addr_equal(AF_INET6
, &route
->dst
, &(union in_addr_union
) { .in6
= {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} }))
2466 if (route
->protocol
== RTPROT_STATIC
&&
2467 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2470 if (route
->protocol
== RTPROT_DHCP
&&
2471 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2474 if (link_is_static_route_configured(link
, route
)) {
2475 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, NULL
);
2479 r
= route_remove(route
, link
, NULL
);
2488 static int link_drop_config(Link
*link
) {
2489 Address
*address
, *pool_address
;
2495 SET_FOREACH(address
, link
->addresses
, i
) {
2496 /* we consider IPv6LL addresses to be managed by the kernel */
2497 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2500 r
= address_remove(address
, link
, NULL
);
2504 /* If this address came from an address pool, clean up the pool */
2505 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2506 if (address_equal(address
, pool_address
)) {
2507 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2508 address_free(pool_address
);
2514 SET_FOREACH(neighbor
, link
->neighbors
, i
) {
2515 r
= neighbor_remove(neighbor
, link
, NULL
);
2520 SET_FOREACH(route
, link
->routes
, i
) {
2521 /* do not touch routes managed by the kernel */
2522 if (route
->protocol
== RTPROT_KERNEL
)
2525 r
= route_remove(route
, link
, NULL
);
2535 static int link_configure(Link
*link
) {
2539 assert(link
->network
);
2540 assert(link
->state
== LINK_STATE_INITIALIZED
);
2542 if (link
->iftype
== ARPHRD_CAN
)
2543 return link_configure_can(link
);
2545 /* Drop foreign config, but ignore loopback or critical devices.
2546 * We do not want to remove loopback address or addresses used for root NFS. */
2547 if (!(link
->flags
& IFF_LOOPBACK
) &&
2548 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2549 r
= link_drop_foreign_config(link
);
2554 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2555 * for this interface, then enable IPv6 */
2556 (void) link_enable_ipv6(link
);
2558 r
= link_set_proxy_arp(link
);
2562 r
= ipv6_proxy_ndp_addresses_configure(link
);
2566 r
= link_set_ipv4_forward(link
);
2570 r
= link_set_ipv6_forward(link
);
2574 r
= link_set_ipv6_privacy_extensions(link
);
2578 r
= link_set_ipv6_accept_ra(link
);
2582 r
= link_set_ipv6_dad_transmits(link
);
2586 r
= link_set_ipv6_hop_limit(link
);
2590 r
= link_set_flags(link
);
2594 r
= link_set_ipv6_mtu(link
);
2598 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2599 r
= ipv4ll_configure(link
);
2604 if (link_dhcp4_enabled(link
)) {
2605 r
= dhcp4_set_promote_secondaries(link
);
2609 r
= dhcp4_configure(link
);
2614 if (link_dhcp4_server_enabled(link
)) {
2615 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2619 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2624 if (link_dhcp6_enabled(link
) ||
2625 link_ipv6_accept_ra_enabled(link
)) {
2626 r
= dhcp6_configure(link
);
2631 if (link_ipv6_accept_ra_enabled(link
)) {
2632 r
= ndisc_configure(link
);
2637 if (link_radv_enabled(link
)) {
2638 r
= radv_configure(link
);
2643 if (link_lldp_rx_enabled(link
)) {
2644 r
= link_lldp_rx_configure(link
);
2649 r
= link_configure_mtu(link
);
2653 r
= link_configure_addrgen_mode(link
);
2657 return link_configure_after_setting_mtu(link
);
2660 static int link_configure_after_setting_mtu(Link
*link
) {
2664 assert(link
->network
);
2665 assert(link
->state
== LINK_STATE_INITIALIZED
);
2667 if (link
->setting_mtu
)
2670 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2671 r
= link_acquire_conf(link
);
2676 return link_enter_join_netdev(link
);
2679 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2682 if (duid
->raw_data_len
> 0)
2685 if (duid
->type
!= DUID_TYPE_UUID
)
2688 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2689 duid
->raw_data_len
= sizeof(sd_id128_t
);
2694 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2695 Manager
*manager
= userdata
;
2696 const sd_bus_error
*e
;
2706 e
= sd_bus_message_get_error(m
);
2708 log_error_errno(sd_bus_error_get_errno(e
),
2709 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2714 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2718 if (sz
!= sizeof(sd_id128_t
)) {
2719 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2723 memcpy(&manager
->product_uuid
, a
, sz
);
2724 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2725 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2727 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2730 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2733 r
= link_configure(link
);
2735 link_enter_failed(link
);
2738 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2740 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2741 * even if the method fails. */
2742 manager
->has_product_uuid
= true;
2747 static bool link_requires_uuid(Link
*link
) {
2751 assert(link
->manager
);
2752 assert(link
->network
);
2754 duid
= link_get_duid(link
);
2755 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2758 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2761 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2767 static int link_configure_duid(Link
*link
) {
2773 assert(link
->manager
);
2774 assert(link
->network
);
2777 duid
= link_get_duid(link
);
2779 if (!link_requires_uuid(link
))
2782 if (m
->has_product_uuid
) {
2783 (void) duid_set_uuid(duid
, m
->product_uuid
);
2787 if (!m
->links_requesting_uuid
) {
2788 r
= manager_request_product_uuid(m
, link
);
2793 log_link_warning_errno(link
, r
,
2794 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2798 r
= set_put(m
->links_requesting_uuid
, link
);
2802 r
= set_put(m
->duids_requesting_uuid
, duid
);
2812 static int link_initialized_and_synced(Link
*link
) {
2817 assert(link
->ifname
);
2818 assert(link
->manager
);
2820 /* We may get called either from the asynchronous netlink callback,
2821 * or directly for link_add() if running in a container. See link_add(). */
2822 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2825 log_link_debug(link
, "Link state is up-to-date");
2826 link_set_state(link
, LINK_STATE_INITIALIZED
);
2828 r
= link_new_bound_by_list(link
);
2832 r
= link_handle_bound_by_list(link
);
2836 if (!link
->network
) {
2837 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2838 &link
->mac
, &network
);
2840 link_enter_unmanaged(link
);
2842 } else if (r
== 0 && network
->unmanaged
) {
2843 link_enter_unmanaged(link
);
2848 if (link
->flags
& IFF_LOOPBACK
) {
2849 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2850 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2852 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2853 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2855 if (network
->dhcp_server
)
2856 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2859 r
= network_apply(network
, link
);
2864 r
= link_new_bound_to_list(link
);
2868 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2869 * link_configure() is called later asynchronously. */
2870 r
= link_configure_duid(link
);
2874 r
= link_configure(link
);
2881 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2884 r
= link_initialized_and_synced(link
);
2886 link_enter_failed(link
);
2890 int link_initialized(Link
*link
, sd_device
*device
) {
2891 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2895 assert(link
->manager
);
2896 assert(link
->manager
->rtnl
);
2899 if (link
->state
!= LINK_STATE_PENDING
)
2902 if (link
->sd_device
)
2905 log_link_debug(link
, "udev initialized link");
2906 link_set_state(link
, LINK_STATE_INITIALIZED
);
2908 link
->sd_device
= sd_device_ref(device
);
2910 /* udev has initialized the link, but we don't know if we have yet
2911 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2912 * when it returns we know that the pending NEWLINKs have already been
2913 * processed and that we are up-to-date */
2915 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2920 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2921 link_netlink_destroy_callback
, link
);
2930 static int link_load(Link
*link
) {
2931 _cleanup_free_
char *network_file
= NULL
,
2934 *dhcp4_address
= NULL
,
2935 *ipv4ll_address
= NULL
;
2936 union in_addr_union address
;
2937 union in_addr_union route_dst
;
2943 r
= parse_env_file(NULL
, link
->state_file
,
2944 "NETWORK_FILE", &network_file
,
2945 "ADDRESSES", &addresses
,
2947 "DHCP4_ADDRESS", &dhcp4_address
,
2948 "IPV4LL_ADDRESS", &ipv4ll_address
);
2949 if (r
< 0 && r
!= -ENOENT
)
2950 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2957 suffix
= strrchr(network_file
, '.');
2959 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2960 goto network_file_fail
;
2964 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2966 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2967 goto network_file_fail
;
2970 r
= network_apply(network
, link
);
2972 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2981 _cleanup_free_
char *address_str
= NULL
;
2982 char *prefixlen_str
;
2984 unsigned char prefixlen
;
2986 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2988 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2994 prefixlen_str
= strchr(address_str
, '/');
2995 if (!prefixlen_str
) {
2996 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3000 *prefixlen_str
++ = '\0';
3002 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3004 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3008 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3010 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3014 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3016 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3025 _cleanup_free_
char *route_str
= NULL
;
3026 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3028 char *prefixlen_str
;
3030 unsigned char prefixlen
, tos
, table
;
3033 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3035 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3041 prefixlen_str
= strchr(route_str
, '/');
3042 if (!prefixlen_str
) {
3043 log_link_debug(link
, "Failed to parse route %s", route_str
);
3047 *prefixlen_str
++ = '\0';
3049 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3051 log_link_debug(link
,
3052 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3057 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3059 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3063 r
= route_add(link
, family
, &route_dst
, prefixlen
, NULL
, tos
, priority
, table
, &route
);
3065 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3067 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3068 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3069 0, route_expire_handler
, route
);
3071 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3074 route
->lifetime
= lifetime
;
3075 sd_event_source_unref(route
->expire
);
3076 route
->expire
= TAKE_PTR(expire
);
3080 if (dhcp4_address
) {
3081 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3083 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3084 goto dhcp4_address_fail
;
3087 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3089 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3091 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3093 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3098 if (ipv4ll_address
) {
3099 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3101 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3102 goto ipv4ll_address_fail
;
3105 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3107 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3109 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3111 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3114 ipv4ll_address_fail
:
3119 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3120 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3121 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3130 r
= link_new(m
, message
, ret
);
3136 log_link_debug(link
, "Link %d added", link
->ifindex
);
3138 r
= link_load(link
);
3142 if (detect_container() <= 0) {
3143 /* not in a container, udev will be around */
3144 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3145 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3147 log_link_warning_errno(link
, r
, "Could not find device: %m");
3151 r
= sd_device_get_is_initialized(device
);
3153 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3158 log_link_debug(link
, "link pending udev initialization...");
3162 r
= device_is_renaming(device
);
3164 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3168 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3172 r
= link_initialized(link
, device
);
3176 r
= link_initialized_and_synced(link
);
3183 link_enter_failed(link
);
3187 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3192 log_link_info(link
, "Gained IPv6LL");
3194 link
->ipv6ll_address
= *address
;
3195 link_check_ready(link
);
3197 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3198 r
= link_acquire_ipv6_conf(link
);
3200 link_enter_failed(link
);
3208 static int link_carrier_gained(Link
*link
) {
3213 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3214 r
= link_acquire_conf(link
);
3216 link_enter_failed(link
);
3220 link_set_state(link
, LINK_STATE_CONFIGURING
);
3221 r
= link_request_set_addresses(link
);
3226 r
= link_handle_bound_by_list(link
);
3233 static int link_carrier_lost(Link
*link
) {
3238 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3241 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3242 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3243 * configuration and stop the clients as well. */
3244 if (link
->setting_mtu
)
3247 r
= link_stop_clients(link
, false);
3249 link_enter_failed(link
);
3253 if (link_dhcp4_server_enabled(link
))
3254 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3256 r
= link_drop_config(link
);
3260 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3261 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3262 r
= link_drop_foreign_config(link
);
3267 r
= link_handle_bound_by_list(link
);
3274 int link_carrier_reset(Link
*link
) {
3279 if (link_has_carrier(link
)) {
3280 r
= link_carrier_lost(link
);
3284 r
= link_carrier_gained(link
);
3288 log_link_info(link
, "Reset carrier");
3294 int link_update(Link
*link
, sd_netlink_message
*m
) {
3295 struct ether_addr mac
;
3298 bool had_carrier
, carrier_gained
, carrier_lost
;
3302 assert(link
->ifname
);
3305 if (link
->state
== LINK_STATE_LINGER
) {
3306 log_link_info(link
, "Link re-added");
3307 link_set_state(link
, LINK_STATE_CONFIGURING
);
3309 r
= link_new_carrier_maps(link
);
3314 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3315 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3316 Manager
*manager
= link
->manager
;
3318 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3321 r
= link_add(manager
, m
, &link
);
3326 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3327 if (r
>= 0 && mtu
> 0) {
3329 if (link
->original_mtu
== 0) {
3330 link
->original_mtu
= mtu
;
3331 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3334 if (link
->dhcp_client
) {
3335 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3338 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3342 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3344 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3348 /* The kernel may broadcast NEWLINK messages without the MAC address
3349 set, simply ignore them. */
3350 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3352 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3355 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3358 log_link_debug(link
, "MAC address: "
3359 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3360 mac
.ether_addr_octet
[0],
3361 mac
.ether_addr_octet
[1],
3362 mac
.ether_addr_octet
[2],
3363 mac
.ether_addr_octet
[3],
3364 mac
.ether_addr_octet
[4],
3365 mac
.ether_addr_octet
[5]);
3368 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3370 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3373 if (link
->dhcp_client
) {
3374 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3375 (const uint8_t *) &link
->mac
,
3379 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3381 r
= dhcp4_set_client_identifier(link
);
3386 if (link
->dhcp6_client
) {
3387 const DUID
* duid
= link_get_duid(link
);
3389 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3390 (const uint8_t *) &link
->mac
,
3394 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3396 if (link
->network
->iaid_set
) {
3397 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3398 link
->network
->iaid
);
3400 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3403 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3405 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3406 duid
->raw_data_len
);
3408 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3412 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3414 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3418 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3420 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3425 old_master
= link
->master_ifindex
;
3426 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3428 had_carrier
= link_has_carrier(link
);
3430 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3434 r
= link_update_lldp(link
);
3438 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3439 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3441 if (carrier_gained
) {
3442 log_link_info(link
, "Gained carrier");
3444 r
= link_carrier_gained(link
);
3447 } else if (carrier_lost
) {
3448 log_link_info(link
, "Lost carrier");
3450 r
= link_carrier_lost(link
);
3458 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3466 if (hashmap_isempty(h
))
3470 HASHMAP_FOREACH(link
, h
, i
) {
3474 fprintf(f
, "%i", link
->ifindex
);
3481 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
3485 for (j
= 0; j
< n_dns
; j
++) {
3486 _cleanup_free_
char *b
= NULL
;
3488 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
3490 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3501 int link_save(Link
*link
) {
3502 _cleanup_free_
char *temp_path
= NULL
;
3503 _cleanup_fclose_
FILE *f
= NULL
;
3504 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
3511 assert(link
->state_file
);
3512 assert(link
->lease_file
);
3513 assert(link
->manager
);
3515 if (link
->state
== LINK_STATE_LINGER
) {
3516 (void) unlink(link
->state_file
);
3520 link_lldp_save(link
);
3522 admin_state
= link_state_to_string(link
->state
);
3523 assert(admin_state
);
3525 oper_state
= link_operstate_to_string(link
->operstate
);
3528 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
3529 assert(carrier_state
);
3531 address_state
= link_address_state_to_string(link
->address_state
);
3532 assert(address_state
);
3534 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3538 (void) fchmod(fileno(f
), 0644);
3541 "# This is private data. Do not parse.\n"
3544 "CARRIER_STATE=%s\n"
3545 "ADDRESS_STATE=%s\n",
3546 admin_state
, oper_state
, carrier_state
, address_state
);
3548 if (link
->network
) {
3549 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3550 const char *dhcp_domainname
= NULL
, *p
;
3551 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3554 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3555 yes_no(link
->network
->required_for_online
));
3557 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3558 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3560 if (link
->dhcp6_client
) {
3561 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3562 if (r
< 0 && r
!= -ENOMSG
)
3563 log_link_debug(link
, "No DHCPv6 lease");
3566 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3571 if (link
->n_dns
!= (unsigned) -1)
3572 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
3574 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
3576 if (link
->network
->dhcp_use_dns
&&
3578 const struct in_addr
*addresses
;
3580 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3582 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3586 if (link
->network
->dhcp6_use_dns
&& dhcp6_lease
) {
3587 struct in6_addr
*in6_addrs
;
3589 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3593 serialize_in6_addrs(f
, in6_addrs
, r
);
3598 /* Make sure to flush out old entries before we use the NDISC data */
3601 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3604 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3608 serialize_in6_addrs(f
, &dd
->address
, 1);
3617 fputstrv(f
, link
->ntp
?: link
->network
->ntp
, NULL
, &space
);
3619 if (link
->network
->dhcp_use_ntp
&&
3621 const struct in_addr
*addresses
;
3623 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3625 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3629 if (link
->network
->dhcp6_use_ntp
&& dhcp6_lease
) {
3630 struct in6_addr
*in6_addrs
;
3633 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3638 serialize_in6_addrs(f
, in6_addrs
, r
);
3642 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3644 fputstrv(f
, hosts
, NULL
, &space
);
3649 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3650 if (link
->dhcp_lease
) {
3651 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3652 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3655 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3658 fputs("DOMAINS=", f
);
3660 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
3661 fputs_with_space(f
, p
, NULL
, &space
);
3663 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3664 if (dhcp_domainname
)
3665 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3667 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3669 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3672 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
3675 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3676 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3681 fputs("ROUTE_DOMAINS=", f
);
3683 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
3684 fputs_with_space(f
, p
, NULL
, &space
);
3686 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3687 if (dhcp_domainname
)
3688 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3690 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3692 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3695 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3698 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3699 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3704 fprintf(f
, "LLMNR=%s\n",
3705 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
3706 fprintf(f
, "MDNS=%s\n",
3707 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
3708 if (link
->dns_default_route
>= 0)
3709 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->dns_default_route
));
3710 else if (link
->network
->dns_default_route
>= 0)
3711 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3713 if (link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3714 fprintf(f
, "DNS_OVER_TLS=%s\n",
3715 dns_over_tls_mode_to_string(link
->dns_over_tls_mode
));
3716 else if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3717 fprintf(f
, "DNS_OVER_TLS=%s\n",
3718 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3720 if (link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3721 fprintf(f
, "DNSSEC=%s\n",
3722 dnssec_mode_to_string(link
->dnssec_mode
));
3723 else if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3724 fprintf(f
, "DNSSEC=%s\n",
3725 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3727 if (!set_isempty(link
->dnssec_negative_trust_anchors
)) {
3730 fputs("DNSSEC_NTA=", f
);
3732 SET_FOREACH(n
, link
->dnssec_negative_trust_anchors
, i
)
3733 fputs_with_space(f
, n
, NULL
, &space
);
3735 } else if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3738 fputs("DNSSEC_NTA=", f
);
3740 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3741 fputs_with_space(f
, n
, NULL
, &space
);
3745 fputs("ADDRESSES=", f
);
3747 SET_FOREACH(a
, link
->addresses
, i
) {
3748 _cleanup_free_
char *address_str
= NULL
;
3750 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3754 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3759 fputs("ROUTES=", f
);
3761 SET_FOREACH(route
, link
->routes
, i
) {
3762 _cleanup_free_
char *route_str
= NULL
;
3764 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3768 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3769 space
? " " : "", route_str
,
3770 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3777 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3778 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3780 if (link
->dhcp_lease
) {
3781 struct in_addr address
;
3782 const char *tz
= NULL
;
3784 assert(link
->network
);
3786 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3788 fprintf(f
, "TIMEZONE=%s\n", tz
);
3790 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3792 fputs("DHCP4_ADDRESS=", f
);
3793 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3797 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3805 (void) unlink(link
->lease_file
);
3808 struct in_addr address
;
3810 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3812 fputs("IPV4LL_ADDRESS=", f
);
3813 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3818 r
= fflush_and_check(f
);
3822 if (rename(temp_path
, link
->state_file
) < 0) {
3830 (void) unlink(link
->state_file
);
3832 (void) unlink(temp_path
);
3834 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3837 /* The serialized state in /run is no longer up-to-date. */
3838 void link_dirty(Link
*link
) {
3843 /* mark manager dirty as link is dirty */
3844 manager_dirty(link
->manager
);
3846 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3848 /* allocation errors are ignored */
3851 r
= set_put(link
->manager
->dirty_links
, link
);
3853 /* don't take another ref if the link was already dirty */
3859 /* The serialized state in /run is up-to-date */
3860 void link_clean(Link
*link
) {
3862 assert(link
->manager
);
3864 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3867 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3868 [LINK_STATE_PENDING
] = "pending",
3869 [LINK_STATE_INITIALIZED
] = "initialized",
3870 [LINK_STATE_CONFIGURING
] = "configuring",
3871 [LINK_STATE_CONFIGURED
] = "configured",
3872 [LINK_STATE_UNMANAGED
] = "unmanaged",
3873 [LINK_STATE_FAILED
] = "failed",
3874 [LINK_STATE_LINGER
] = "linger",
3877 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);