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,
613 .n_dns
= (unsigned) -1,
614 .dns_default_route
= -1,
615 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
616 .mdns
= _RESOLVE_SUPPORT_INVALID
,
617 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
618 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
621 link
->ifname
= strdup(ifname
);
626 link
->kind
= strdup(kind
);
631 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
633 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
635 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
637 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
639 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
642 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
645 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
648 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
652 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
656 r
= link_update_flags(link
, message
, false);
660 *ret
= TAKE_PTR(link
);
665 void link_ntp_settings_clear(Link
*link
) {
666 link
->ntp
= strv_free(link
->ntp
);
669 void link_dns_settings_clear(Link
*link
) {
670 link
->dns
= mfree(link
->dns
);
671 link
->n_dns
= (unsigned) -1;
673 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
674 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
676 link
->dns_default_route
= -1;
677 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
678 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
679 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
680 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
682 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
685 static Link
*link_free(Link
*link
) {
690 link_ntp_settings_clear(link
);
691 link_dns_settings_clear(link
);
693 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
694 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
696 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
697 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
699 while ((address
= link
->pool_addresses
)) {
700 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
701 address_free(address
);
704 sd_dhcp_server_unref(link
->dhcp_server
);
705 sd_dhcp_client_unref(link
->dhcp_client
);
706 sd_dhcp_lease_unref(link
->dhcp_lease
);
708 link_lldp_emit_stop(link
);
710 free(link
->lease_file
);
712 sd_lldp_unref(link
->lldp
);
713 free(link
->lldp_file
);
717 sd_ipv4ll_unref(link
->ipv4ll
);
718 sd_dhcp6_client_unref(link
->dhcp6_client
);
719 sd_ndisc_unref(link
->ndisc
);
720 sd_radv_unref(link
->radv
);
725 (void) unlink(link
->state_file
);
726 free(link
->state_file
);
728 sd_device_unref(link
->sd_device
);
730 hashmap_free(link
->bound_to_links
);
731 hashmap_free(link
->bound_by_links
);
733 set_free_with_destructor(link
->slaves
, link_unref
);
735 network_unref(link
->network
);
740 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
742 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
749 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
758 void link_set_state(Link
*link
, LinkState state
) {
761 if (link
->state
== state
)
764 log_link_debug(link
, "State changed: %s -> %s",
765 link_state_to_string(link
->state
),
766 link_state_to_string(state
));
770 link_send_changed(link
, "AdministrativeState", NULL
);
773 static void link_enter_unmanaged(Link
*link
) {
776 link_set_state(link
, LINK_STATE_UNMANAGED
);
781 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
785 assert(link
->manager
);
786 assert(link
->manager
->event
);
788 dhcp4_release_old_lease(link
);
790 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
791 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
792 k
= sd_dhcp_client_stop(link
->dhcp_client
);
794 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
798 k
= sd_ipv4ll_stop(link
->ipv4ll
);
800 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
803 if (link
->dhcp6_client
) {
804 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
806 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
810 k
= sd_ndisc_stop(link
->ndisc
);
812 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
816 k
= sd_radv_stop(link
->radv
);
818 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
821 link_lldp_emit_stop(link
);
825 void link_enter_failed(Link
*link
) {
828 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
831 log_link_warning(link
, "Failed");
833 link_set_state(link
, LINK_STATE_FAILED
);
835 link_stop_clients(link
, false);
840 static int link_join_netdevs_after_configured(Link
*link
) {
845 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
846 if (netdev
->ifindex
> 0)
847 /* Assume already enslaved. */
850 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
853 log_struct(LOG_DEBUG
,
854 LOG_LINK_INTERFACE(link
),
855 LOG_NETDEV_INTERFACE(netdev
),
856 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
858 r
= netdev_join(netdev
, link
, NULL
);
860 return log_struct_errno(LOG_WARNING
, r
,
861 LOG_LINK_INTERFACE(link
),
862 LOG_NETDEV_INTERFACE(netdev
),
863 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
869 static void link_enter_configured(Link
*link
) {
871 assert(link
->network
);
873 if (link
->state
!= LINK_STATE_CONFIGURING
)
876 log_link_info(link
, "Configured");
878 link_set_state(link
, LINK_STATE_CONFIGURED
);
880 (void) link_join_netdevs_after_configured(link
);
885 static int link_request_set_routing_policy_rule(Link
*link
) {
886 RoutingPolicyRule
*rule
, *rrule
= NULL
;
890 assert(link
->network
);
892 link
->routing_policy_rules_configured
= false;
894 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
895 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
898 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
902 r
= routing_policy_rule_configure(rule
, link
, NULL
);
904 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
906 link
->routing_policy_rule_messages
++;
909 routing_policy_rule_purge(link
->manager
, link
);
910 if (link
->routing_policy_rule_messages
== 0) {
911 link
->routing_policy_rules_configured
= true;
912 link_check_ready(link
);
914 log_link_debug(link
, "Setting routing policy rules");
915 link_set_state(link
, LINK_STATE_CONFIGURING
);
921 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
925 assert(link
->route_messages
> 0);
926 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
927 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
929 link
->route_messages
--;
931 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
934 r
= sd_netlink_message_get_errno(m
);
935 if (r
< 0 && r
!= -EEXIST
) {
936 log_link_warning_errno(link
, r
, "Could not set route: %m");
937 link_enter_failed(link
);
941 if (link
->route_messages
== 0) {
942 log_link_debug(link
, "Routes set");
943 link
->static_routes_configured
= true;
944 link_check_ready(link
);
950 int link_request_set_routes(Link
*link
) {
952 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
953 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
960 assert(link
->network
);
961 assert(link
->addresses_configured
);
962 assert(link
->address_messages
== 0);
963 assert(link
->state
!= _LINK_STATE_INVALID
);
965 link
->static_routes_configured
= false;
967 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
968 /* During configuring addresses, the link lost its carrier. As networkd is dropping
969 * the addresses now, let's not configure the routes either. */
972 r
= link_request_set_routing_policy_rule(link
);
976 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
977 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
978 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
980 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
983 r
= route_configure(rt
, link
, route_handler
);
985 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
987 link
->route_messages
++;
990 if (link
->route_messages
== 0) {
991 link
->static_routes_configured
= true;
992 link_check_ready(link
);
994 log_link_debug(link
, "Setting routes");
995 link_set_state(link
, LINK_STATE_CONFIGURING
);
1001 void link_check_ready(Link
*link
) {
1008 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1014 if (!link
->addresses_configured
)
1017 if (!link
->neighbors_configured
)
1020 SET_FOREACH(a
, link
->addresses
, i
)
1021 if (!address_is_ready(a
))
1024 if (!link
->addresses_ready
) {
1025 link
->addresses_ready
= true;
1026 r
= link_request_set_routes(link
);
1028 link_enter_failed(link
);
1032 if (!link
->static_routes_configured
)
1035 if (!link
->routing_policy_rules_configured
)
1038 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1040 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
1043 if (link_ipv6ll_enabled(link
) &&
1044 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1047 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1048 !link
->dhcp4_configured
&&
1049 !link
->dhcp6_configured
&&
1050 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1051 /* When DHCP is enabled, at least one protocol must provide an address, or
1052 * an IPv4ll fallback address must be configured. */
1055 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1059 if (link
->state
!= LINK_STATE_CONFIGURED
)
1060 link_enter_configured(link
);
1065 static int link_request_set_neighbors(Link
*link
) {
1070 assert(link
->network
);
1071 assert(link
->state
!= _LINK_STATE_INVALID
);
1073 link
->neighbors_configured
= false;
1075 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1076 r
= neighbor_configure(neighbor
, link
, NULL
);
1078 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1081 if (link
->neighbor_messages
== 0) {
1082 link
->neighbors_configured
= true;
1083 link_check_ready(link
);
1085 log_link_debug(link
, "Setting neighbors");
1086 link_set_state(link
, LINK_STATE_CONFIGURING
);
1092 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1098 assert(link
->ifname
);
1099 assert(link
->address_messages
> 0);
1100 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1101 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1103 link
->address_messages
--;
1105 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1108 r
= sd_netlink_message_get_errno(m
);
1109 if (r
< 0 && r
!= -EEXIST
) {
1110 log_link_warning_errno(link
, r
, "could not set address: %m");
1111 link_enter_failed(link
);
1114 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1116 if (link
->address_messages
== 0) {
1117 log_link_debug(link
, "Addresses set");
1118 link
->addresses_configured
= true;
1119 link_check_ready(link
);
1125 static int link_set_bridge_fdb(Link
*link
) {
1126 FdbEntry
*fdb_entry
;
1129 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1130 r
= fdb_entry_configure(link
, fdb_entry
);
1132 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1138 static int link_request_set_addresses(Link
*link
) {
1139 AddressLabel
*label
;
1144 assert(link
->network
);
1145 assert(link
->state
!= _LINK_STATE_INVALID
);
1147 /* Reset all *_configured flags we are configuring. */
1148 link
->addresses_configured
= false;
1149 link
->addresses_ready
= false;
1150 link
->neighbors_configured
= false;
1151 link
->static_routes_configured
= false;
1152 link
->routing_policy_rules_configured
= false;
1154 r
= link_set_bridge_fdb(link
);
1158 r
= link_request_set_neighbors(link
);
1162 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1165 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1167 r
= address_configure(ad
, link
, address_handler
, update
);
1169 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1171 link
->address_messages
++;
1174 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1175 r
= address_label_configure(label
, link
, NULL
, false);
1177 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1179 link
->address_label_messages
++;
1182 /* now that we can figure out a default address for the dhcp server,
1184 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1185 r
= dhcp4_server_configure(link
);
1188 log_link_debug(link
, "Offering DHCPv4 leases");
1191 if (link
->address_messages
== 0) {
1192 link
->addresses_configured
= true;
1193 link_check_ready(link
);
1195 log_link_debug(link
, "Setting addresses");
1196 link_set_state(link
, LINK_STATE_CONFIGURING
);
1202 static int link_set_bridge_vlan(Link
*link
) {
1205 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1207 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1212 static int link_set_proxy_arp(Link
*link
) {
1215 if (!link_proxy_arp_enabled(link
))
1218 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1220 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1225 static int link_configure_after_setting_mtu(Link
*link
);
1227 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1232 assert(link
->ifname
);
1234 link
->setting_mtu
= false;
1236 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1239 r
= sd_netlink_message_get_errno(m
);
1241 log_link_warning_errno(link
, r
, "Could not set MTU, ignoring: %m");
1243 log_link_debug(link
, "Setting MTU done.");
1245 if (link
->state
== LINK_STATE_INITIALIZED
) {
1246 r
= link_configure_after_setting_mtu(link
);
1248 link_enter_failed(link
);
1254 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1255 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1259 assert(link
->manager
);
1260 assert(link
->manager
->rtnl
);
1262 if (mtu
== 0 || link
->setting_mtu
)
1265 if (link
->mtu
== mtu
)
1268 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1270 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1272 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1274 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1275 * for this interface, then disable IPv6 else enable it. */
1276 (void) link_enable_ipv6(link
);
1278 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1279 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1280 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1282 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1283 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1288 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1290 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1292 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1293 link_netlink_destroy_callback
, link
);
1295 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1298 link
->setting_mtu
= true;
1303 static bool link_reduces_vlan_mtu(Link
*link
) {
1304 /* See netif_reduces_vlan_mtu() in kernel. */
1305 return streq_ptr(link
->kind
, "macsec");
1308 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1313 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1314 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1315 /* See vlan_dev_change_mtu() in kernel. */
1316 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1318 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1319 /* See macvlan_change_mtu() in kernel. */
1325 static int link_configure_mtu(Link
*link
) {
1329 assert(link
->network
);
1331 if (link
->network
->mtu
> 0)
1332 return link_set_mtu(link
, link
->network
->mtu
);
1334 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1335 if (link
->mtu
>= mtu
)
1338 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1339 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1342 return link_set_mtu(link
, mtu
);
1345 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1350 assert(link
->ifname
);
1352 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1355 r
= sd_netlink_message_get_errno(m
);
1357 log_link_warning_errno(link
, r
, "Could not set link flags, ignoring: %m");
1362 static int link_set_flags(Link
*link
) {
1363 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1364 unsigned ifi_change
= 0;
1365 unsigned ifi_flags
= 0;
1369 assert(link
->manager
);
1370 assert(link
->manager
->rtnl
);
1372 if (link
->flags
& IFF_LOOPBACK
)
1378 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1381 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1383 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1385 if (link
->network
->arp
>= 0) {
1386 ifi_change
|= IFF_NOARP
;
1387 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1390 if (link
->network
->multicast
>= 0) {
1391 ifi_change
|= IFF_MULTICAST
;
1392 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1395 if (link
->network
->allmulticast
>= 0) {
1396 ifi_change
|= IFF_ALLMULTI
;
1397 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1400 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1402 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1404 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1405 link_netlink_destroy_callback
, link
);
1407 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1414 static int link_acquire_ipv6_conf(Link
*link
) {
1419 if (link_ipv6_accept_ra_enabled(link
)) {
1420 assert(link
->ndisc
);
1422 log_link_debug(link
, "Discovering IPv6 routers");
1424 r
= sd_ndisc_start(link
->ndisc
);
1425 if (r
< 0 && r
!= -EBUSY
)
1426 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1429 if (link_radv_enabled(link
)) {
1431 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1433 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1435 r
= sd_radv_start(link
->radv
);
1436 if (r
< 0 && r
!= -EBUSY
)
1437 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1440 (void) dhcp6_request_prefix_delegation(link
);
1445 static int link_acquire_ipv4_conf(Link
*link
) {
1449 assert(link
->manager
);
1450 assert(link
->manager
->event
);
1452 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1453 assert(link
->ipv4ll
);
1455 log_link_debug(link
, "Acquiring IPv4 link-local address");
1457 r
= sd_ipv4ll_start(link
->ipv4ll
);
1459 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1462 if (link_dhcp4_enabled(link
)) {
1463 assert(link
->dhcp_client
);
1465 log_link_debug(link
, "Acquiring DHCPv4 lease");
1467 r
= sd_dhcp_client_start(link
->dhcp_client
);
1469 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1475 static int link_acquire_conf(Link
*link
) {
1480 r
= link_acquire_ipv4_conf(link
);
1484 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1485 r
= link_acquire_ipv6_conf(link
);
1490 if (link_lldp_emit_enabled(link
)) {
1491 r
= link_lldp_emit_start(link
);
1493 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1499 bool link_has_carrier(Link
*link
) {
1500 /* see Documentation/networking/operstates.txt in the kernel sources */
1502 if (link
->kernel_operstate
== IF_OPER_UP
)
1505 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1506 /* operstate may not be implemented, so fall back to flags */
1507 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1508 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1514 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1519 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1522 r
= sd_netlink_message_get_errno(m
);
1524 log_link_warning_errno(link
, r
, "Could not set address genmode for interface, ignoring: %m");
1529 static int link_configure_addrgen_mode(Link
*link
) {
1530 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1531 uint8_t ipv6ll_mode
;
1535 assert(link
->network
);
1536 assert(link
->manager
);
1537 assert(link
->manager
->rtnl
);
1539 if (!socket_ipv6_is_supported())
1542 log_link_debug(link
, "Setting address genmode for link");
1544 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1546 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1548 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1550 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1552 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1554 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1556 if (!link_ipv6ll_enabled(link
))
1557 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1558 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1559 /* The file may not exist. And event if it exists, when stable_secret is unset,
1560 * reading the file fails with EIO. */
1561 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1563 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1565 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1567 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1569 r
= sd_netlink_message_close_container(req
);
1571 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1573 r
= sd_netlink_message_close_container(req
);
1575 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1577 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1578 link_netlink_destroy_callback
, link
);
1580 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1587 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1592 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1595 r
= sd_netlink_message_get_errno(m
);
1597 /* we warn but don't fail the link, as it may be brought up later */
1598 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1603 static int link_up(Link
*link
) {
1604 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1608 assert(link
->network
);
1609 assert(link
->manager
);
1610 assert(link
->manager
->rtnl
);
1612 log_link_debug(link
, "Bringing link up");
1614 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1616 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1618 /* set it free if not enslaved with networkd */
1619 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1620 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1622 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1625 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1627 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1629 if (link
->network
->mac
) {
1630 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1632 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1635 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1636 link_netlink_destroy_callback
, link
);
1638 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1645 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1650 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1653 r
= sd_netlink_message_get_errno(m
);
1655 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1660 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1661 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1665 assert(link
->manager
);
1666 assert(link
->manager
->rtnl
);
1668 log_link_debug(link
, "Bringing link down");
1670 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1671 RTM_SETLINK
, link
->ifindex
);
1673 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1675 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1677 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1679 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1680 callback
?: link_down_handler
,
1681 link_netlink_destroy_callback
, link
);
1683 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1690 static int link_handle_bound_to_list(Link
*link
) {
1694 bool required_up
= false;
1695 bool link_is_up
= false;
1699 if (hashmap_isempty(link
->bound_to_links
))
1702 if (link
->flags
& IFF_UP
)
1705 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1706 if (link_has_carrier(l
)) {
1711 if (!required_up
&& link_is_up
) {
1712 r
= link_down(link
, NULL
);
1715 } else if (required_up
&& !link_is_up
) {
1724 static int link_handle_bound_by_list(Link
*link
) {
1731 if (hashmap_isempty(link
->bound_by_links
))
1734 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1735 r
= link_handle_bound_to_list(l
);
1743 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1749 if (link
== carrier
)
1752 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1755 r
= hashmap_ensure_allocated(h
, NULL
);
1759 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1766 static int link_new_bound_by_list(Link
*link
) {
1771 bool list_updated
= false;
1774 assert(link
->manager
);
1778 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1779 if (!carrier
->network
)
1782 if (strv_isempty(carrier
->network
->bind_carrier
))
1785 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1786 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1790 list_updated
= true;
1797 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1798 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1802 link_dirty(carrier
);
1808 static int link_new_bound_to_list(Link
*link
) {
1813 bool list_updated
= false;
1816 assert(link
->manager
);
1821 if (strv_isempty(link
->network
->bind_carrier
))
1826 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1827 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1828 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1832 list_updated
= true;
1839 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1840 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1844 link_dirty(carrier
);
1850 static int link_new_carrier_maps(Link
*link
) {
1853 r
= link_new_bound_by_list(link
);
1857 r
= link_handle_bound_by_list(link
);
1861 r
= link_new_bound_to_list(link
);
1865 r
= link_handle_bound_to_list(link
);
1872 static void link_free_bound_to_list(Link
*link
) {
1876 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1877 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1879 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1880 link_dirty(bound_to
);
1886 static void link_free_bound_by_list(Link
*link
) {
1890 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1891 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1893 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1894 link_dirty(bound_by
);
1895 link_handle_bound_to_list(bound_by
);
1902 static void link_free_carrier_maps(Link
*link
) {
1903 bool list_updated
= false;
1907 if (!hashmap_isempty(link
->bound_to_links
)) {
1908 link_free_bound_to_list(link
);
1909 list_updated
= true;
1912 if (!hashmap_isempty(link
->bound_by_links
)) {
1913 link_free_bound_by_list(link
);
1914 list_updated
= true;
1923 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1930 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1934 r
= set_ensure_allocated(&master
->slaves
, NULL
);
1938 r
= set_put(master
->slaves
, link
);
1946 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1951 if (!link
->manager
|| !netdev
)
1954 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1957 link_unref(set_remove(master
->slaves
, link
));
1960 static void link_detach_from_manager(Link
*link
) {
1961 if (!link
|| !link
->manager
)
1964 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1967 /* The following must be called at last. */
1968 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1972 void link_drop(Link
*link
) {
1973 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1976 link_set_state(link
, LINK_STATE_LINGER
);
1978 link_free_carrier_maps(link
);
1980 if (link
->network
) {
1981 link_drop_from_master(link
, link
->network
->bridge
);
1982 link_drop_from_master(link
, link
->network
->bond
);
1985 log_link_debug(link
, "Link removed");
1987 (void) unlink(link
->state_file
);
1988 link_detach_from_manager(link
);
1991 static int link_joined(Link
*link
) {
1995 assert(link
->network
);
1997 if (!hashmap_isempty(link
->bound_to_links
)) {
1998 r
= link_handle_bound_to_list(link
);
2001 } else if (!(link
->flags
& IFF_UP
)) {
2004 link_enter_failed(link
);
2009 if (link
->network
->bridge
) {
2010 r
= link_set_bridge(link
);
2012 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2014 r
= link_append_to_master(link
, link
->network
->bridge
);
2016 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2019 if (link
->network
->bond
) {
2020 r
= link_set_bond(link
);
2022 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2024 r
= link_append_to_master(link
, link
->network
->bond
);
2026 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2029 if (link
->network
->use_br_vlan
&&
2030 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2031 r
= link_set_bridge_vlan(link
);
2033 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2036 /* Skip setting up addresses until it gets carrier,
2037 or it would try to set addresses twice,
2038 which is bad for non-idempotent steps. */
2039 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2042 link_set_state(link
, LINK_STATE_CONFIGURING
);
2043 return link_request_set_addresses(link
);
2046 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2050 assert(link
->network
);
2051 assert(link
->enslaving
> 0);
2055 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2058 r
= sd_netlink_message_get_errno(m
);
2059 if (r
< 0 && r
!= -EEXIST
) {
2060 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2061 link_enter_failed(link
);
2065 log_link_debug(link
, "Joined netdev");
2067 if (link
->enslaving
== 0) {
2068 r
= link_joined(link
);
2070 link_enter_failed(link
);
2076 static int link_enter_join_netdev(Link
*link
) {
2082 assert(link
->network
);
2083 assert(link
->state
== LINK_STATE_INITIALIZED
);
2085 link_set_state(link
, LINK_STATE_CONFIGURING
);
2088 link
->enslaving
= 0;
2090 if (link
->network
->bond
) {
2091 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2092 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2093 return link_joined(link
);
2095 log_struct(LOG_DEBUG
,
2096 LOG_LINK_INTERFACE(link
),
2097 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2098 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2102 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2104 log_struct_errno(LOG_WARNING
, r
,
2105 LOG_LINK_INTERFACE(link
),
2106 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2107 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2108 link_enter_failed(link
);
2113 if (link
->network
->bridge
) {
2114 log_struct(LOG_DEBUG
,
2115 LOG_LINK_INTERFACE(link
),
2116 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2117 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2121 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2123 log_struct_errno(LOG_WARNING
, r
,
2124 LOG_LINK_INTERFACE(link
),
2125 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2126 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2127 link_enter_failed(link
);
2132 if (link
->network
->vrf
) {
2133 log_struct(LOG_DEBUG
,
2134 LOG_LINK_INTERFACE(link
),
2135 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2136 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2140 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2142 log_struct_errno(LOG_WARNING
, r
,
2143 LOG_LINK_INTERFACE(link
),
2144 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2145 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2146 link_enter_failed(link
);
2151 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2153 if (netdev
->ifindex
> 0)
2154 /* Assume already enslaved. */
2157 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2160 log_struct(LOG_DEBUG
,
2161 LOG_LINK_INTERFACE(link
),
2162 LOG_NETDEV_INTERFACE(netdev
),
2163 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2167 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2169 log_struct_errno(LOG_WARNING
, r
,
2170 LOG_LINK_INTERFACE(link
),
2171 LOG_NETDEV_INTERFACE(netdev
),
2172 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2173 link_enter_failed(link
);
2178 if (link
->enslaving
== 0)
2179 return link_joined(link
);
2184 static int link_set_ipv4_forward(Link
*link
) {
2187 if (!link_ipv4_forward_enabled(link
))
2190 /* We propagate the forwarding flag from one interface to the
2191 * global setting one way. This means: as long as at least one
2192 * interface was configured at any time that had IP forwarding
2193 * enabled the setting will stay on for good. We do this
2194 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2195 * somewhat in sync (see below). */
2197 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2199 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2204 static int link_set_ipv6_forward(Link
*link
) {
2207 if (!link_ipv6_forward_enabled(link
))
2210 /* On Linux, the IPv6 stack does not know a per-interface
2211 * packet forwarding setting: either packet forwarding is on
2212 * for all, or off for all. We hence don't bother with a
2213 * per-interface setting, but simply propagate the interface
2214 * flag, if it is set, to the global flag, one-way. Note that
2215 * while IPv4 would allow a per-interface flag, we expose the
2216 * same behaviour there and also propagate the setting from
2217 * one to all, to keep things simple (see above). */
2219 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2221 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2226 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2227 IPv6PrivacyExtensions s
;
2230 s
= link_ipv6_privacy_extensions(link
);
2234 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2236 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2241 static int link_set_ipv6_accept_ra(Link
*link
) {
2244 /* Make this a NOP if IPv6 is not available */
2245 if (!socket_ipv6_is_supported())
2248 if (link
->flags
& IFF_LOOPBACK
)
2254 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2256 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2261 static int link_set_ipv6_dad_transmits(Link
*link
) {
2264 /* Make this a NOP if IPv6 is not available */
2265 if (!socket_ipv6_is_supported())
2268 if (link
->flags
& IFF_LOOPBACK
)
2274 if (link
->network
->ipv6_dad_transmits
< 0)
2277 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2279 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2284 static int link_set_ipv6_hop_limit(Link
*link
) {
2287 /* Make this a NOP if IPv6 is not available */
2288 if (!socket_ipv6_is_supported())
2291 if (link
->flags
& IFF_LOOPBACK
)
2297 if (link
->network
->ipv6_hop_limit
< 0)
2300 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2302 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2307 static int link_set_ipv6_mtu(Link
*link
) {
2310 /* Make this a NOP if IPv6 is not available */
2311 if (!socket_ipv6_is_supported())
2314 if (link
->flags
& IFF_LOOPBACK
)
2317 if (link
->network
->ipv6_mtu
== 0)
2320 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2322 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2327 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2328 Address
*net_address
;
2336 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2337 if (address_equal(net_address
, address
))
2343 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2352 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2353 if (route_equal(net_route
, route
))
2359 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2366 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2369 /* Even when the address is leased from a DHCP server, networkd assign the address
2370 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2371 * corresponding routes with RTPROT_DHCP. */
2372 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2373 if (route
->protocol
!= RTPROT_DHCP
)
2376 if (address
->family
!= route
->family
)
2379 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2386 static int link_drop_foreign_config(Link
*link
) {
2392 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2393 /* we consider IPv6LL addresses to be managed by the kernel */
2394 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2397 if (link_address_is_dynamic(link
, address
)) {
2398 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2400 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2403 if (link_is_static_address_configured(link
, address
)) {
2404 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2406 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2408 r
= address_remove(address
, link
, NULL
);
2414 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2415 /* do not touch routes managed by the kernel */
2416 if (route
->protocol
== RTPROT_KERNEL
)
2419 if (route
->protocol
== RTPROT_STATIC
&&
2420 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2423 if (route
->protocol
== RTPROT_DHCP
&&
2424 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2427 if (link_is_static_route_configured(link
, route
)) {
2428 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2432 r
= route_remove(route
, link
, NULL
);
2441 static int link_drop_config(Link
*link
) {
2442 Address
*address
, *pool_address
;
2447 SET_FOREACH(address
, link
->addresses
, i
) {
2448 /* we consider IPv6LL addresses to be managed by the kernel */
2449 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2452 r
= address_remove(address
, link
, NULL
);
2456 /* If this address came from an address pool, clean up the pool */
2457 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2458 if (address_equal(address
, pool_address
)) {
2459 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2460 address_free(pool_address
);
2466 SET_FOREACH(route
, link
->routes
, i
) {
2467 /* do not touch routes managed by the kernel */
2468 if (route
->protocol
== RTPROT_KERNEL
)
2471 r
= route_remove(route
, link
, NULL
);
2481 static int link_configure(Link
*link
) {
2485 assert(link
->network
);
2486 assert(link
->state
== LINK_STATE_INITIALIZED
);
2488 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
2489 return link_configure_can(link
);
2491 /* Drop foreign config, but ignore loopback or critical devices.
2492 * We do not want to remove loopback address or addresses used for root NFS. */
2493 if (!(link
->flags
& IFF_LOOPBACK
) &&
2494 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2495 r
= link_drop_foreign_config(link
);
2500 r
= link_set_proxy_arp(link
);
2504 r
= ipv6_proxy_ndp_addresses_configure(link
);
2508 r
= link_set_ipv4_forward(link
);
2512 r
= link_set_ipv6_forward(link
);
2516 r
= link_set_ipv6_privacy_extensions(link
);
2520 r
= link_set_ipv6_accept_ra(link
);
2524 r
= link_set_ipv6_dad_transmits(link
);
2528 r
= link_set_ipv6_hop_limit(link
);
2532 r
= link_set_flags(link
);
2536 r
= link_set_ipv6_mtu(link
);
2540 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2541 r
= ipv4ll_configure(link
);
2546 if (link_dhcp4_enabled(link
)) {
2547 r
= dhcp4_set_promote_secondaries(link
);
2551 r
= dhcp4_configure(link
);
2556 if (link_dhcp4_server_enabled(link
)) {
2557 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2561 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2566 if (link_dhcp6_enabled(link
) ||
2567 link_ipv6_accept_ra_enabled(link
)) {
2568 r
= dhcp6_configure(link
);
2573 if (link_ipv6_accept_ra_enabled(link
)) {
2574 r
= ndisc_configure(link
);
2579 if (link_radv_enabled(link
)) {
2580 r
= radv_configure(link
);
2585 if (link_lldp_rx_enabled(link
)) {
2586 r
= link_lldp_rx_configure(link
);
2591 r
= link_configure_mtu(link
);
2595 r
= link_configure_addrgen_mode(link
);
2599 return link_configure_after_setting_mtu(link
);
2602 static int link_configure_after_setting_mtu(Link
*link
) {
2606 assert(link
->network
);
2607 assert(link
->state
== LINK_STATE_INITIALIZED
);
2609 if (link
->setting_mtu
)
2612 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2613 r
= link_acquire_conf(link
);
2618 return link_enter_join_netdev(link
);
2621 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2624 if (duid
->raw_data_len
> 0)
2627 if (duid
->type
!= DUID_TYPE_UUID
)
2630 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2631 duid
->raw_data_len
= sizeof(sd_id128_t
);
2636 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2637 Manager
*manager
= userdata
;
2638 const sd_bus_error
*e
;
2648 e
= sd_bus_message_get_error(m
);
2650 log_error_errno(sd_bus_error_get_errno(e
),
2651 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2656 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2660 if (sz
!= sizeof(sd_id128_t
)) {
2661 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2665 memcpy(&manager
->product_uuid
, a
, sz
);
2666 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2667 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2669 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2672 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2673 r
= link_configure(link
);
2675 link_enter_failed(link
);
2678 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2680 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2681 * even if the method fails. */
2682 manager
->has_product_uuid
= true;
2687 static bool link_requires_uuid(Link
*link
) {
2691 assert(link
->manager
);
2692 assert(link
->network
);
2694 duid
= link_get_duid(link
);
2695 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2698 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2701 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2707 static int link_configure_duid(Link
*link
) {
2713 assert(link
->manager
);
2714 assert(link
->network
);
2717 duid
= link_get_duid(link
);
2719 if (!link_requires_uuid(link
))
2722 if (m
->has_product_uuid
) {
2723 (void) duid_set_uuid(duid
, m
->product_uuid
);
2727 if (!m
->links_requesting_uuid
) {
2728 r
= manager_request_product_uuid(m
, link
);
2733 log_link_warning_errno(link
, r
,
2734 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2738 r
= set_put(m
->links_requesting_uuid
, link
);
2742 r
= set_put(m
->duids_requesting_uuid
, duid
);
2750 static int link_initialized_and_synced(Link
*link
) {
2755 assert(link
->ifname
);
2756 assert(link
->manager
);
2758 /* We may get called either from the asynchronous netlink callback,
2759 * or directly for link_add() if running in a container. See link_add(). */
2760 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2763 log_link_debug(link
, "Link state is up-to-date");
2764 link_set_state(link
, LINK_STATE_INITIALIZED
);
2766 r
= link_new_bound_by_list(link
);
2770 r
= link_handle_bound_by_list(link
);
2774 if (!link
->network
) {
2775 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2776 &link
->mac
, &network
);
2778 link_enter_unmanaged(link
);
2780 } else if (r
== 0 && network
->unmanaged
) {
2781 link_enter_unmanaged(link
);
2786 if (link
->flags
& IFF_LOOPBACK
) {
2787 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2788 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2790 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2791 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2793 if (network
->dhcp_server
)
2794 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2797 r
= network_apply(network
, link
);
2802 r
= link_new_bound_to_list(link
);
2806 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2807 * link_configure() is called later asynchronously. */
2808 r
= link_configure_duid(link
);
2812 r
= link_configure(link
);
2819 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2822 r
= link_initialized_and_synced(link
);
2824 link_enter_failed(link
);
2828 int link_initialized(Link
*link
, sd_device
*device
) {
2829 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2833 assert(link
->manager
);
2834 assert(link
->manager
->rtnl
);
2837 if (link
->state
!= LINK_STATE_PENDING
)
2840 if (link
->sd_device
)
2843 log_link_debug(link
, "udev initialized link");
2844 link_set_state(link
, LINK_STATE_INITIALIZED
);
2846 link
->sd_device
= sd_device_ref(device
);
2848 /* udev has initialized the link, but we don't know if we have yet
2849 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2850 * when it returns we know that the pending NEWLINKs have already been
2851 * processed and that we are up-to-date */
2853 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2858 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2859 link_netlink_destroy_callback
, link
);
2868 static int link_load(Link
*link
) {
2869 _cleanup_free_
char *network_file
= NULL
,
2872 *dhcp4_address
= NULL
,
2873 *ipv4ll_address
= NULL
;
2874 union in_addr_union address
;
2875 union in_addr_union route_dst
;
2881 r
= parse_env_file(NULL
, link
->state_file
,
2882 "NETWORK_FILE", &network_file
,
2883 "ADDRESSES", &addresses
,
2885 "DHCP4_ADDRESS", &dhcp4_address
,
2886 "IPV4LL_ADDRESS", &ipv4ll_address
);
2887 if (r
< 0 && r
!= -ENOENT
)
2888 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2895 suffix
= strrchr(network_file
, '.');
2897 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2898 goto network_file_fail
;
2902 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2904 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2905 goto network_file_fail
;
2908 r
= network_apply(network
, link
);
2910 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2919 _cleanup_free_
char *address_str
= NULL
;
2920 char *prefixlen_str
;
2922 unsigned char prefixlen
;
2924 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2926 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2932 prefixlen_str
= strchr(address_str
, '/');
2933 if (!prefixlen_str
) {
2934 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2938 *prefixlen_str
++ = '\0';
2940 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2942 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2946 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2948 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2952 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2954 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2963 _cleanup_free_
char *route_str
= NULL
;
2964 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2966 char *prefixlen_str
;
2968 unsigned char prefixlen
, tos
, table
;
2971 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2973 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2979 prefixlen_str
= strchr(route_str
, '/');
2980 if (!prefixlen_str
) {
2981 log_link_debug(link
, "Failed to parse route %s", route_str
);
2985 *prefixlen_str
++ = '\0';
2987 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2989 log_link_debug(link
,
2990 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2995 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2997 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3001 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3003 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3005 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3006 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3007 0, route_expire_handler
, route
);
3009 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3012 route
->lifetime
= lifetime
;
3013 sd_event_source_unref(route
->expire
);
3014 route
->expire
= TAKE_PTR(expire
);
3018 if (dhcp4_address
) {
3019 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3021 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3022 goto dhcp4_address_fail
;
3025 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3027 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3029 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3031 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3036 if (ipv4ll_address
) {
3037 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3039 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3040 goto ipv4ll_address_fail
;
3043 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3045 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3047 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3049 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3052 ipv4ll_address_fail
:
3057 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3058 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3059 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3068 r
= link_new(m
, message
, ret
);
3074 log_link_debug(link
, "Link %d added", link
->ifindex
);
3076 r
= link_load(link
);
3080 if (detect_container() <= 0) {
3081 /* not in a container, udev will be around */
3082 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3083 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3085 log_link_warning_errno(link
, r
, "Could not find device: %m");
3089 r
= sd_device_get_is_initialized(device
);
3091 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3096 log_link_debug(link
, "link pending udev initialization...");
3100 r
= device_is_renaming(device
);
3102 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3106 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3110 r
= link_initialized(link
, device
);
3114 r
= link_initialized_and_synced(link
);
3121 link_enter_failed(link
);
3125 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3130 log_link_info(link
, "Gained IPv6LL");
3132 link
->ipv6ll_address
= *address
;
3133 link_check_ready(link
);
3135 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3136 r
= link_acquire_ipv6_conf(link
);
3138 link_enter_failed(link
);
3146 static int link_carrier_gained(Link
*link
) {
3151 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3152 r
= link_acquire_conf(link
);
3154 link_enter_failed(link
);
3158 link_set_state(link
, LINK_STATE_CONFIGURING
);
3159 r
= link_request_set_addresses(link
);
3164 r
= link_handle_bound_by_list(link
);
3171 static int link_carrier_lost(Link
*link
) {
3176 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3179 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3180 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3181 * configuration and stop the clients as well. */
3182 if (link
->setting_mtu
)
3185 r
= link_stop_clients(link
, false);
3187 link_enter_failed(link
);
3191 if (link_dhcp4_server_enabled(link
))
3192 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3194 r
= link_drop_config(link
);
3198 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3199 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3200 r
= link_drop_foreign_config(link
);
3205 r
= link_handle_bound_by_list(link
);
3212 int link_carrier_reset(Link
*link
) {
3217 if (link_has_carrier(link
)) {
3218 r
= link_carrier_lost(link
);
3222 r
= link_carrier_gained(link
);
3226 log_link_info(link
, "Reset carrier");
3232 int link_update(Link
*link
, sd_netlink_message
*m
) {
3233 struct ether_addr mac
;
3236 bool had_carrier
, carrier_gained
, carrier_lost
;
3240 assert(link
->ifname
);
3243 if (link
->state
== LINK_STATE_LINGER
) {
3244 log_link_info(link
, "Link re-added");
3245 link_set_state(link
, LINK_STATE_CONFIGURING
);
3247 r
= link_new_carrier_maps(link
);
3252 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3253 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3254 Manager
*manager
= link
->manager
;
3256 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3259 r
= link_add(manager
, m
, &link
);
3264 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3265 if (r
>= 0 && mtu
> 0) {
3267 if (link
->original_mtu
== 0) {
3268 link
->original_mtu
= mtu
;
3269 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3272 if (link
->dhcp_client
) {
3273 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3276 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3280 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3282 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3286 /* The kernel may broadcast NEWLINK messages without the MAC address
3287 set, simply ignore them. */
3288 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3290 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3293 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3296 log_link_debug(link
, "MAC address: "
3297 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3298 mac
.ether_addr_octet
[0],
3299 mac
.ether_addr_octet
[1],
3300 mac
.ether_addr_octet
[2],
3301 mac
.ether_addr_octet
[3],
3302 mac
.ether_addr_octet
[4],
3303 mac
.ether_addr_octet
[5]);
3306 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3308 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3311 if (link
->dhcp_client
) {
3312 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3313 (const uint8_t *) &link
->mac
,
3317 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3319 r
= dhcp4_set_client_identifier(link
);
3324 if (link
->dhcp6_client
) {
3325 const DUID
* duid
= link_get_duid(link
);
3327 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3328 (const uint8_t *) &link
->mac
,
3332 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3334 if (link
->network
->iaid_set
) {
3335 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3336 link
->network
->iaid
);
3338 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3341 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3343 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3344 duid
->raw_data_len
);
3346 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3350 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3352 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3356 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3358 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3363 old_master
= link
->master_ifindex
;
3364 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3366 had_carrier
= link_has_carrier(link
);
3368 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3372 r
= link_update_lldp(link
);
3376 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3377 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3379 if (carrier_gained
) {
3380 log_link_info(link
, "Gained carrier");
3382 r
= link_carrier_gained(link
);
3385 } else if (carrier_lost
) {
3386 log_link_info(link
, "Lost carrier");
3388 r
= link_carrier_lost(link
);
3396 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3404 if (hashmap_isempty(h
))
3408 HASHMAP_FOREACH(link
, h
, i
) {
3412 fprintf(f
, "%i", link
->ifindex
);
3419 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
3423 for (j
= 0; j
< n_dns
; j
++) {
3424 _cleanup_free_
char *b
= NULL
;
3426 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
3428 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3439 int link_save(Link
*link
) {
3440 _cleanup_free_
char *temp_path
= NULL
;
3441 _cleanup_fclose_
FILE *f
= NULL
;
3442 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
3449 assert(link
->state_file
);
3450 assert(link
->lease_file
);
3451 assert(link
->manager
);
3453 if (link
->state
== LINK_STATE_LINGER
) {
3454 (void) unlink(link
->state_file
);
3458 link_lldp_save(link
);
3460 admin_state
= link_state_to_string(link
->state
);
3461 assert(admin_state
);
3463 oper_state
= link_operstate_to_string(link
->operstate
);
3466 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
3467 assert(carrier_state
);
3469 address_state
= link_address_state_to_string(link
->address_state
);
3470 assert(address_state
);
3472 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3476 (void) fchmod(fileno(f
), 0644);
3479 "# This is private data. Do not parse.\n"
3482 "CARRIER_STATE=%s\n"
3483 "ADDRESS_STATE=%s\n",
3484 admin_state
, oper_state
, carrier_state
, address_state
);
3486 if (link
->network
) {
3487 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3488 const char *dhcp_domainname
= NULL
, *p
;
3489 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3492 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3493 yes_no(link
->network
->required_for_online
));
3495 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3496 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3498 if (link
->dhcp6_client
) {
3499 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3500 if (r
< 0 && r
!= -ENOMSG
)
3501 log_link_debug(link
, "No DHCPv6 lease");
3504 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3509 if (link
->n_dns
!= (unsigned) -1)
3510 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
3512 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
3514 if (link
->network
->dhcp_use_dns
&&
3516 const struct in_addr
*addresses
;
3518 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3520 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3524 if (link
->network
->dhcp6_use_dns
&& dhcp6_lease
) {
3525 struct in6_addr
*in6_addrs
;
3527 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3531 serialize_in6_addrs(f
, in6_addrs
, r
);
3536 /* Make sure to flush out old entries before we use the NDISC data */
3539 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3542 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3546 serialize_in6_addrs(f
, &dd
->address
, 1);
3555 fputstrv(f
, link
->ntp
?: link
->network
->ntp
, NULL
, &space
);
3557 if (link
->network
->dhcp_use_ntp
&&
3559 const struct in_addr
*addresses
;
3561 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3563 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3567 if (link
->network
->dhcp6_use_ntp
&& dhcp6_lease
) {
3568 struct in6_addr
*in6_addrs
;
3571 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3576 serialize_in6_addrs(f
, in6_addrs
, r
);
3580 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3582 fputstrv(f
, hosts
, NULL
, &space
);
3587 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3588 if (link
->dhcp_lease
) {
3589 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3590 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3593 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3596 fputs("DOMAINS=", f
);
3598 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
3599 fputs_with_space(f
, p
, NULL
, &space
);
3601 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3602 if (dhcp_domainname
)
3603 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3605 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3607 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3610 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
3613 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3614 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3619 fputs("ROUTE_DOMAINS=", f
);
3621 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
3622 fputs_with_space(f
, p
, NULL
, &space
);
3624 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3625 if (dhcp_domainname
)
3626 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3628 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3630 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3633 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3636 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3637 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3642 fprintf(f
, "LLMNR=%s\n",
3643 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
3644 fprintf(f
, "MDNS=%s\n",
3645 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
3646 if (link
->dns_default_route
>= 0)
3647 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->dns_default_route
));
3648 else if (link
->network
->dns_default_route
>= 0)
3649 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3651 if (link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3652 fprintf(f
, "DNS_OVER_TLS=%s\n",
3653 dns_over_tls_mode_to_string(link
->dns_over_tls_mode
));
3654 else if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3655 fprintf(f
, "DNS_OVER_TLS=%s\n",
3656 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3658 if (link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3659 fprintf(f
, "DNSSEC=%s\n",
3660 dnssec_mode_to_string(link
->dnssec_mode
));
3661 else if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3662 fprintf(f
, "DNSSEC=%s\n",
3663 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3665 if (!set_isempty(link
->dnssec_negative_trust_anchors
)) {
3668 fputs("DNSSEC_NTA=", f
);
3670 SET_FOREACH(n
, link
->dnssec_negative_trust_anchors
, i
)
3671 fputs_with_space(f
, n
, NULL
, &space
);
3673 } else if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3676 fputs("DNSSEC_NTA=", f
);
3678 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3679 fputs_with_space(f
, n
, NULL
, &space
);
3683 fputs("ADDRESSES=", f
);
3685 SET_FOREACH(a
, link
->addresses
, i
) {
3686 _cleanup_free_
char *address_str
= NULL
;
3688 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3692 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3697 fputs("ROUTES=", f
);
3699 SET_FOREACH(route
, link
->routes
, i
) {
3700 _cleanup_free_
char *route_str
= NULL
;
3702 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3706 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3707 space
? " " : "", route_str
,
3708 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3715 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3716 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3718 if (link
->dhcp_lease
) {
3719 struct in_addr address
;
3720 const char *tz
= NULL
;
3722 assert(link
->network
);
3724 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3726 fprintf(f
, "TIMEZONE=%s\n", tz
);
3728 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3730 fputs("DHCP4_ADDRESS=", f
);
3731 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3735 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3743 (void) unlink(link
->lease_file
);
3746 struct in_addr address
;
3748 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3750 fputs("IPV4LL_ADDRESS=", f
);
3751 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3756 r
= fflush_and_check(f
);
3760 if (rename(temp_path
, link
->state_file
) < 0) {
3768 (void) unlink(link
->state_file
);
3770 (void) unlink(temp_path
);
3772 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3775 /* The serialized state in /run is no longer up-to-date. */
3776 void link_dirty(Link
*link
) {
3781 /* mark manager dirty as link is dirty */
3782 manager_dirty(link
->manager
);
3784 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3786 /* allocation errors are ignored */
3789 r
= set_put(link
->manager
->dirty_links
, link
);
3791 /* don't take another ref if the link was already dirty */
3797 /* The serialized state in /run is up-to-date */
3798 void link_clean(Link
*link
) {
3800 assert(link
->manager
);
3802 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3805 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3806 [LINK_STATE_PENDING
] = "pending",
3807 [LINK_STATE_INITIALIZED
] = "initialized",
3808 [LINK_STATE_CONFIGURING
] = "configuring",
3809 [LINK_STATE_CONFIGURED
] = "configured",
3810 [LINK_STATE_UNMANAGED
] = "unmanaged",
3811 [LINK_STATE_FAILED
] = "failed",
3812 [LINK_STATE_LINGER
] = "linger",
3815 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);