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_is_can(Link
*link
) {
96 return link
->iftype
== ARPHRD_CAN
||
97 STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan");
100 static bool link_dhcp6_enabled(Link
*link
) {
103 if (!socket_ipv6_is_supported())
106 if (link
->flags
& IFF_LOOPBACK
)
112 if (link
->network
->bond
)
115 if (link_is_can(link
))
118 if (link_sysctl_ipv6_enabled(link
) == 0)
121 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
124 static bool link_dhcp4_enabled(Link
*link
) {
127 if (link
->flags
& IFF_LOOPBACK
)
133 if (link
->network
->bond
)
136 if (link_is_can(link
))
139 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
142 static bool link_dhcp4_server_enabled(Link
*link
) {
145 if (link
->flags
& IFF_LOOPBACK
)
151 if (link
->network
->bond
)
154 if (link_is_can(link
))
157 return link
->network
->dhcp_server
;
160 bool link_ipv4ll_enabled(Link
*link
, AddressFamilyBoolean mask
) {
162 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
164 if (link
->flags
& IFF_LOOPBACK
)
170 if (link_is_can(link
))
173 if (STRPTR_IN_SET(link
->kind
,
174 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
175 "vti6", "nlmon", "xfrm"))
178 /* L3 or L3S mode do not support ARP. */
179 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
182 if (link
->network
->bond
)
185 return link
->network
->link_local
& mask
;
188 static bool link_ipv6ll_enabled(Link
*link
) {
191 if (!socket_ipv6_is_supported())
194 if (link
->flags
& IFF_LOOPBACK
)
200 if (link_is_can(link
))
203 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
206 if (link
->network
->bond
)
209 if (link_sysctl_ipv6_enabled(link
) == 0)
212 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
215 static bool link_ipv6_enabled(Link
*link
) {
218 if (!socket_ipv6_is_supported())
221 if (link
->network
->bond
)
224 if (link_sysctl_ipv6_enabled(link
) == 0)
227 if (link_is_can(link
))
230 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
231 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
234 static bool link_radv_enabled(Link
*link
) {
237 if (!link_ipv6ll_enabled(link
))
240 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
243 static bool link_ipv4_forward_enabled(Link
*link
) {
246 if (link
->flags
& IFF_LOOPBACK
)
252 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
255 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
258 static bool link_ipv6_forward_enabled(Link
*link
) {
261 if (!socket_ipv6_is_supported())
264 if (link
->flags
& IFF_LOOPBACK
)
270 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
273 if (link_sysctl_ipv6_enabled(link
) == 0)
276 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
279 static bool link_proxy_arp_enabled(Link
*link
) {
282 if (link
->flags
& IFF_LOOPBACK
)
288 if (link
->network
->proxy_arp
< 0)
294 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
297 if (!socket_ipv6_is_supported())
300 if (link
->flags
& IFF_LOOPBACK
)
306 if (!link_ipv6ll_enabled(link
))
309 /* If unset use system default (enabled if local forwarding is disabled.
310 * disabled if local forwarding is enabled).
311 * If set, ignore or enforce RA independent of local forwarding state.
313 if (link
->network
->ipv6_accept_ra
< 0)
314 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
315 return !link_ipv6_forward_enabled(link
);
316 else if (link
->network
->ipv6_accept_ra
> 0)
317 /* accept RA even if ip_forward is enabled */
324 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
327 if (!socket_ipv6_is_supported())
328 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
330 if (link
->flags
& IFF_LOOPBACK
)
331 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
334 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
336 return link
->network
->ipv6_privacy_extensions
;
339 static int link_enable_ipv6(Link
*link
) {
343 if (link
->flags
& IFF_LOOPBACK
)
346 disabled
= !link_ipv6_enabled(link
);
348 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
350 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
352 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
357 static bool link_is_enslaved(Link
*link
) {
358 if (link
->flags
& IFF_SLAVE
)
359 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
365 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
368 /* TODO: add conditions for other netdevs. */
373 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
379 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
382 link_update_operstate(master
, true);
385 void link_update_operstate(Link
*link
, bool also_update_master
) {
386 LinkOperationalState operstate
;
387 LinkCarrierState carrier_state
;
388 LinkAddressState address_state
;
389 _cleanup_strv_free_
char **p
= NULL
;
390 uint8_t scope
= RT_SCOPE_NOWHERE
;
391 bool changed
= false;
397 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
398 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
399 else if (link_has_carrier(link
)) {
400 if (link_is_enslaved(link
))
401 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
403 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
404 } else if (link
->flags
& IFF_UP
)
405 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
407 carrier_state
= LINK_CARRIER_STATE_OFF
;
409 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
412 SET_FOREACH(slave
, link
->slaves
, i
) {
413 link_update_operstate(slave
, false);
415 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
416 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
420 SET_FOREACH(address
, link
->addresses
, i
) {
421 if (!address_is_ready(address
))
424 if (address
->scope
< scope
)
425 scope
= address
->scope
;
428 /* for operstate we also take foreign addresses into account */
429 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
430 if (!address_is_ready(address
))
433 if (address
->scope
< scope
)
434 scope
= address
->scope
;
437 if (scope
< RT_SCOPE_SITE
)
438 /* universally accessible addresses found */
439 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
440 else if (scope
< RT_SCOPE_HOST
)
441 /* only link or site local addresses found */
442 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
444 /* no useful addresses found */
445 address_state
= LINK_ADDRESS_STATE_OFF
;
447 /* Mapping of address and carrier state vs operational state
449 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
450 * ------------------------------------------------------------------------------
451 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
452 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
453 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
456 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
457 operstate
= (LinkOperationalState
) carrier_state
;
458 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
459 operstate
= LINK_OPERSTATE_ROUTABLE
;
460 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
461 operstate
= LINK_OPERSTATE_DEGRADED
;
463 operstate
= LINK_OPERSTATE_ENSLAVED
;
465 if (link
->carrier_state
!= carrier_state
) {
466 link
->carrier_state
= carrier_state
;
468 if (strv_extend(&p
, "CarrierState") < 0)
472 if (link
->address_state
!= address_state
) {
473 link
->address_state
= address_state
;
475 if (strv_extend(&p
, "AddressState") < 0)
479 if (link
->operstate
!= operstate
) {
480 link
->operstate
= operstate
;
482 if (strv_extend(&p
, "OperationalState") < 0)
487 link_send_changed_strv(link
, p
);
491 if (also_update_master
&& link
->network
) {
492 link_update_master_operstate(link
, link
->network
->bond
);
493 link_update_master_operstate(link
, link
->network
->bridge
);
497 #define FLAG_STRING(string, flag, old, new) \
498 (((old ^ new) & flag) \
499 ? ((old & flag) ? (" -" string) : (" +" string)) \
502 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
503 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
509 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
511 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
513 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
515 /* if we got a message without operstate, take it to mean
516 the state was unchanged */
517 operstate
= link
->kernel_operstate
;
519 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
522 if (link
->flags
!= flags
) {
523 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",
524 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
525 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
526 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
527 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
528 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
529 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
530 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
531 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
532 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
533 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
534 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
535 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
536 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
537 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
538 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
539 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
540 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
541 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
542 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
544 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
545 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
546 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
547 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
548 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
549 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
550 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
551 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
553 /* link flags are currently at most 18 bits, let's align to
555 if (unknown_flags_added
)
557 "Unknown link flags gained: %#.5x (ignoring)",
558 unknown_flags_added
);
560 if (unknown_flags_removed
)
562 "Unknown link flags lost: %#.5x (ignoring)",
563 unknown_flags_removed
);
567 link
->kernel_operstate
= operstate
;
569 link_update_operstate(link
, true);
574 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
575 _cleanup_(link_unrefp
) Link
*link
= NULL
;
577 const char *ifname
, *kind
= NULL
;
579 unsigned short iftype
;
585 /* check for link kind */
586 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
588 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
589 r
= sd_netlink_message_exit_container(message
);
594 r
= sd_netlink_message_get_type(message
, &type
);
597 else if (type
!= RTM_NEWLINK
)
600 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
603 else if (ifindex
<= 0)
606 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
610 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
621 .state
= LINK_STATE_PENDING
,
624 .sysctl_ipv6_enabled
= -1,
626 .n_dns
= (unsigned) -1,
627 .dns_default_route
= -1,
628 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
629 .mdns
= _RESOLVE_SUPPORT_INVALID
,
630 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
631 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
634 link
->ifname
= strdup(ifname
);
639 link
->kind
= strdup(kind
);
644 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
646 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
648 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
650 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
652 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
655 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
658 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
661 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
665 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
669 r
= link_update_flags(link
, message
, false);
673 *ret
= TAKE_PTR(link
);
678 void link_ntp_settings_clear(Link
*link
) {
679 link
->ntp
= strv_free(link
->ntp
);
682 void link_dns_settings_clear(Link
*link
) {
683 link
->dns
= mfree(link
->dns
);
684 link
->n_dns
= (unsigned) -1;
686 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
687 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
689 link
->dns_default_route
= -1;
690 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
691 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
692 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
693 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
695 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
698 static Link
*link_free(Link
*link
) {
703 link_ntp_settings_clear(link
);
704 link_dns_settings_clear(link
);
706 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
707 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
709 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
710 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
712 while ((address
= link
->pool_addresses
)) {
713 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
714 address_free(address
);
717 sd_dhcp_server_unref(link
->dhcp_server
);
718 sd_dhcp_client_unref(link
->dhcp_client
);
719 sd_dhcp_lease_unref(link
->dhcp_lease
);
720 set_free(link
->dhcp_routes
);
722 link_lldp_emit_stop(link
);
724 free(link
->lease_file
);
726 sd_lldp_unref(link
->lldp
);
727 free(link
->lldp_file
);
731 sd_ipv4ll_unref(link
->ipv4ll
);
732 sd_dhcp6_client_unref(link
->dhcp6_client
);
733 sd_ndisc_unref(link
->ndisc
);
734 sd_radv_unref(link
->radv
);
739 (void) unlink(link
->state_file
);
740 free(link
->state_file
);
742 sd_device_unref(link
->sd_device
);
744 hashmap_free(link
->bound_to_links
);
745 hashmap_free(link
->bound_by_links
);
747 set_free_with_destructor(link
->slaves
, link_unref
);
749 network_unref(link
->network
);
754 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
756 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
763 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
772 void link_set_state(Link
*link
, LinkState state
) {
775 if (link
->state
== state
)
778 log_link_debug(link
, "State changed: %s -> %s",
779 link_state_to_string(link
->state
),
780 link_state_to_string(state
));
784 link_send_changed(link
, "AdministrativeState", NULL
);
787 static void link_enter_unmanaged(Link
*link
) {
790 link_set_state(link
, LINK_STATE_UNMANAGED
);
795 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
799 assert(link
->manager
);
800 assert(link
->manager
->event
);
802 dhcp4_release_old_lease(link
);
804 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
805 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
806 k
= sd_dhcp_client_stop(link
->dhcp_client
);
808 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
812 k
= sd_ipv4ll_stop(link
->ipv4ll
);
814 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
817 if (link
->dhcp6_client
) {
818 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
820 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
824 k
= sd_ndisc_stop(link
->ndisc
);
826 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
830 k
= sd_radv_stop(link
->radv
);
832 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
835 link_lldp_emit_stop(link
);
839 void link_enter_failed(Link
*link
) {
842 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
845 log_link_warning(link
, "Failed");
847 link_set_state(link
, LINK_STATE_FAILED
);
849 link_stop_clients(link
, false);
854 static int link_join_netdevs_after_configured(Link
*link
) {
859 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
860 if (netdev
->ifindex
> 0)
861 /* Assume already enslaved. */
864 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
867 log_struct(LOG_DEBUG
,
868 LOG_LINK_INTERFACE(link
),
869 LOG_NETDEV_INTERFACE(netdev
),
870 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
872 r
= netdev_join(netdev
, link
, NULL
);
874 return log_struct_errno(LOG_WARNING
, r
,
875 LOG_LINK_INTERFACE(link
),
876 LOG_NETDEV_INTERFACE(netdev
),
877 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
883 static void link_enter_configured(Link
*link
) {
885 assert(link
->network
);
887 if (link
->state
!= LINK_STATE_CONFIGURING
)
890 log_link_info(link
, "Configured");
892 link_set_state(link
, LINK_STATE_CONFIGURED
);
894 (void) link_join_netdevs_after_configured(link
);
899 static int link_request_set_routing_policy_rule(Link
*link
) {
900 RoutingPolicyRule
*rule
, *rrule
= NULL
;
904 assert(link
->network
);
906 link
->routing_policy_rules_configured
= false;
908 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
909 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
912 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
916 r
= routing_policy_rule_configure(rule
, link
, NULL
);
918 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
920 link
->routing_policy_rule_messages
++;
923 routing_policy_rule_purge(link
->manager
, link
);
924 if (link
->routing_policy_rule_messages
== 0) {
925 link
->routing_policy_rules_configured
= true;
926 link_check_ready(link
);
928 log_link_debug(link
, "Setting routing policy rules");
929 link_set_state(link
, LINK_STATE_CONFIGURING
);
935 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
939 assert(link
->route_messages
> 0);
940 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
941 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
943 link
->route_messages
--;
945 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
948 r
= sd_netlink_message_get_errno(m
);
949 if (r
< 0 && r
!= -EEXIST
) {
950 log_link_warning_errno(link
, r
, "Could not set route: %m");
951 link_enter_failed(link
);
955 if (link
->route_messages
== 0) {
956 log_link_debug(link
, "Routes set");
957 link
->static_routes_configured
= true;
958 link_check_ready(link
);
964 int link_request_set_routes(Link
*link
) {
966 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
967 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
974 assert(link
->network
);
975 assert(link
->addresses_configured
);
976 assert(link
->address_messages
== 0);
977 assert(link
->state
!= _LINK_STATE_INVALID
);
979 link
->static_routes_configured
= false;
981 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
982 /* During configuring addresses, the link lost its carrier. As networkd is dropping
983 * the addresses now, let's not configure the routes either. */
986 r
= link_request_set_routing_policy_rule(link
);
990 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
991 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
992 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
994 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
997 r
= route_configure(rt
, link
, route_handler
);
999 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
1001 link
->route_messages
++;
1004 if (link
->route_messages
== 0) {
1005 link
->static_routes_configured
= true;
1006 link_check_ready(link
);
1008 log_link_debug(link
, "Setting routes");
1009 link_set_state(link
, LINK_STATE_CONFIGURING
);
1015 void link_check_ready(Link
*link
) {
1022 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1028 if (!link
->addresses_configured
)
1031 if (!link
->neighbors_configured
)
1034 SET_FOREACH(a
, link
->addresses
, i
)
1035 if (!address_is_ready(a
))
1038 if (!link
->addresses_ready
) {
1039 link
->addresses_ready
= true;
1040 r
= link_request_set_routes(link
);
1042 link_enter_failed(link
);
1046 if (!link
->static_routes_configured
)
1049 if (!link
->routing_policy_rules_configured
)
1052 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1054 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
1057 if (link_ipv6ll_enabled(link
) &&
1058 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1061 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1062 !link
->dhcp4_configured
&&
1063 !link
->dhcp6_configured
&&
1064 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1065 /* When DHCP is enabled, at least one protocol must provide an address, or
1066 * an IPv4ll fallback address must be configured. */
1069 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1073 if (link
->state
!= LINK_STATE_CONFIGURED
)
1074 link_enter_configured(link
);
1079 static int link_request_set_neighbors(Link
*link
) {
1084 assert(link
->network
);
1085 assert(link
->state
!= _LINK_STATE_INVALID
);
1087 link
->neighbors_configured
= false;
1089 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1090 r
= neighbor_configure(neighbor
, link
, NULL
);
1092 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1095 if (link
->neighbor_messages
== 0) {
1096 link
->neighbors_configured
= true;
1097 link_check_ready(link
);
1099 log_link_debug(link
, "Setting neighbors");
1100 link_set_state(link
, LINK_STATE_CONFIGURING
);
1106 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1112 assert(link
->ifname
);
1113 assert(link
->address_messages
> 0);
1114 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1115 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1117 link
->address_messages
--;
1119 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1122 r
= sd_netlink_message_get_errno(m
);
1123 if (r
< 0 && r
!= -EEXIST
) {
1124 log_link_warning_errno(link
, r
, "could not set address: %m");
1125 link_enter_failed(link
);
1128 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1130 if (link
->address_messages
== 0) {
1131 log_link_debug(link
, "Addresses set");
1132 link
->addresses_configured
= true;
1133 link_check_ready(link
);
1139 static int link_set_bridge_fdb(Link
*link
) {
1140 FdbEntry
*fdb_entry
;
1143 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1144 r
= fdb_entry_configure(link
, fdb_entry
);
1146 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1152 static int link_request_set_addresses(Link
*link
) {
1153 AddressLabel
*label
;
1158 assert(link
->network
);
1159 assert(link
->state
!= _LINK_STATE_INVALID
);
1161 /* Reset all *_configured flags we are configuring. */
1162 link
->addresses_configured
= false;
1163 link
->addresses_ready
= false;
1164 link
->neighbors_configured
= false;
1165 link
->static_routes_configured
= false;
1166 link
->routing_policy_rules_configured
= false;
1168 r
= link_set_bridge_fdb(link
);
1172 r
= link_request_set_neighbors(link
);
1176 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1179 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1181 r
= address_configure(ad
, link
, address_handler
, update
);
1183 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1185 link
->address_messages
++;
1188 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1189 r
= address_label_configure(label
, link
, NULL
, false);
1191 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1193 link
->address_label_messages
++;
1196 /* now that we can figure out a default address for the dhcp server,
1198 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1199 r
= dhcp4_server_configure(link
);
1202 log_link_debug(link
, "Offering DHCPv4 leases");
1205 if (link
->address_messages
== 0) {
1206 link
->addresses_configured
= true;
1207 link_check_ready(link
);
1209 log_link_debug(link
, "Setting addresses");
1210 link_set_state(link
, LINK_STATE_CONFIGURING
);
1216 static int link_set_bridge_vlan(Link
*link
) {
1219 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1221 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1226 static int link_set_proxy_arp(Link
*link
) {
1229 if (!link_proxy_arp_enabled(link
))
1232 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1234 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1239 static int link_configure_after_setting_mtu(Link
*link
);
1241 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1246 assert(link
->ifname
);
1248 link
->setting_mtu
= false;
1250 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1253 r
= sd_netlink_message_get_errno(m
);
1255 log_link_warning_errno(link
, r
, "Could not set MTU, ignoring: %m");
1257 log_link_debug(link
, "Setting MTU done.");
1259 if (link
->state
== LINK_STATE_INITIALIZED
) {
1260 r
= link_configure_after_setting_mtu(link
);
1262 link_enter_failed(link
);
1268 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1269 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1273 assert(link
->manager
);
1274 assert(link
->manager
->rtnl
);
1276 if (mtu
== 0 || link
->setting_mtu
)
1279 if (link
->mtu
== mtu
)
1282 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1284 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1286 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1288 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1289 * for this interface, then disable IPv6 else enable it. */
1290 (void) link_enable_ipv6(link
);
1292 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1293 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1294 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1296 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1297 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1302 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1304 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1306 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1307 link_netlink_destroy_callback
, link
);
1309 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1312 link
->setting_mtu
= true;
1317 static bool link_reduces_vlan_mtu(Link
*link
) {
1318 /* See netif_reduces_vlan_mtu() in kernel. */
1319 return streq_ptr(link
->kind
, "macsec");
1322 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1327 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1328 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1329 /* See vlan_dev_change_mtu() in kernel. */
1330 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1332 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1333 /* See macvlan_change_mtu() in kernel. */
1339 static int link_configure_mtu(Link
*link
) {
1343 assert(link
->network
);
1345 if (link
->network
->mtu
> 0)
1346 return link_set_mtu(link
, link
->network
->mtu
);
1348 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1349 if (link
->mtu
>= mtu
)
1352 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1353 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1356 return link_set_mtu(link
, mtu
);
1359 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1364 assert(link
->ifname
);
1366 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1369 r
= sd_netlink_message_get_errno(m
);
1371 log_link_warning_errno(link
, r
, "Could not set link flags, ignoring: %m");
1376 static int link_set_flags(Link
*link
) {
1377 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1378 unsigned ifi_change
= 0;
1379 unsigned ifi_flags
= 0;
1383 assert(link
->manager
);
1384 assert(link
->manager
->rtnl
);
1386 if (link
->flags
& IFF_LOOPBACK
)
1392 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1395 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1397 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1399 if (link
->network
->arp
>= 0) {
1400 ifi_change
|= IFF_NOARP
;
1401 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1404 if (link
->network
->multicast
>= 0) {
1405 ifi_change
|= IFF_MULTICAST
;
1406 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1409 if (link
->network
->allmulticast
>= 0) {
1410 ifi_change
|= IFF_ALLMULTI
;
1411 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1414 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1416 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1418 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1419 link_netlink_destroy_callback
, link
);
1421 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1428 static int link_acquire_ipv6_conf(Link
*link
) {
1433 if (link_ipv6_accept_ra_enabled(link
)) {
1434 assert(link
->ndisc
);
1436 log_link_debug(link
, "Discovering IPv6 routers");
1438 r
= sd_ndisc_start(link
->ndisc
);
1439 if (r
< 0 && r
!= -EBUSY
)
1440 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1443 if (link_radv_enabled(link
)) {
1445 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1447 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1449 r
= sd_radv_start(link
->radv
);
1450 if (r
< 0 && r
!= -EBUSY
)
1451 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1454 (void) dhcp6_request_prefix_delegation(link
);
1459 static int link_acquire_ipv4_conf(Link
*link
) {
1463 assert(link
->manager
);
1464 assert(link
->manager
->event
);
1466 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1467 assert(link
->ipv4ll
);
1469 log_link_debug(link
, "Acquiring IPv4 link-local address");
1471 r
= sd_ipv4ll_start(link
->ipv4ll
);
1473 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1476 if (link_dhcp4_enabled(link
)) {
1477 assert(link
->dhcp_client
);
1479 log_link_debug(link
, "Acquiring DHCPv4 lease");
1481 r
= sd_dhcp_client_start(link
->dhcp_client
);
1483 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1489 static int link_acquire_conf(Link
*link
) {
1494 r
= link_acquire_ipv4_conf(link
);
1498 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1499 r
= link_acquire_ipv6_conf(link
);
1504 if (link_lldp_emit_enabled(link
)) {
1505 r
= link_lldp_emit_start(link
);
1507 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1513 bool link_has_carrier(Link
*link
) {
1514 /* see Documentation/networking/operstates.txt in the kernel sources */
1516 if (link
->kernel_operstate
== IF_OPER_UP
)
1519 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1520 /* operstate may not be implemented, so fall back to flags */
1521 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1522 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1528 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1533 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1536 r
= sd_netlink_message_get_errno(m
);
1538 log_link_warning_errno(link
, r
, "Could not set address genmode for interface, ignoring: %m");
1543 static int link_configure_addrgen_mode(Link
*link
) {
1544 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1545 uint8_t ipv6ll_mode
;
1549 assert(link
->network
);
1550 assert(link
->manager
);
1551 assert(link
->manager
->rtnl
);
1553 if (!socket_ipv6_is_supported())
1556 log_link_debug(link
, "Setting address genmode for link");
1558 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1560 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1562 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1564 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1566 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1568 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1570 if (!link_ipv6ll_enabled(link
))
1571 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1572 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1573 /* The file may not exist. And event if it exists, when stable_secret is unset,
1574 * reading the file fails with EIO. */
1575 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1577 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1579 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1581 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1583 r
= sd_netlink_message_close_container(req
);
1585 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1587 r
= sd_netlink_message_close_container(req
);
1589 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1591 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1592 link_netlink_destroy_callback
, link
);
1594 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1601 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1606 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1609 r
= sd_netlink_message_get_errno(m
);
1611 /* we warn but don't fail the link, as it may be brought up later */
1612 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1617 static int link_up(Link
*link
) {
1618 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1622 assert(link
->network
);
1623 assert(link
->manager
);
1624 assert(link
->manager
->rtnl
);
1626 log_link_debug(link
, "Bringing link up");
1628 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1630 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1632 /* set it free if not enslaved with networkd */
1633 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1634 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1636 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1639 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1641 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1643 if (link
->network
->mac
) {
1644 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1646 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1649 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1650 link_netlink_destroy_callback
, link
);
1652 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1659 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1664 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1667 r
= sd_netlink_message_get_errno(m
);
1669 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1674 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1675 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1679 assert(link
->manager
);
1680 assert(link
->manager
->rtnl
);
1682 log_link_debug(link
, "Bringing link down");
1684 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1685 RTM_SETLINK
, link
->ifindex
);
1687 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1689 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1691 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1693 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1694 callback
?: link_down_handler
,
1695 link_netlink_destroy_callback
, link
);
1697 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1704 static int link_handle_bound_to_list(Link
*link
) {
1708 bool required_up
= false;
1709 bool link_is_up
= false;
1713 if (hashmap_isempty(link
->bound_to_links
))
1716 if (link
->flags
& IFF_UP
)
1719 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1720 if (link_has_carrier(l
)) {
1725 if (!required_up
&& link_is_up
) {
1726 r
= link_down(link
, NULL
);
1729 } else if (required_up
&& !link_is_up
) {
1738 static int link_handle_bound_by_list(Link
*link
) {
1745 if (hashmap_isempty(link
->bound_by_links
))
1748 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1749 r
= link_handle_bound_to_list(l
);
1757 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1763 if (link
== carrier
)
1766 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1769 r
= hashmap_ensure_allocated(h
, NULL
);
1773 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1780 static int link_new_bound_by_list(Link
*link
) {
1785 bool list_updated
= false;
1788 assert(link
->manager
);
1792 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1793 if (!carrier
->network
)
1796 if (strv_isempty(carrier
->network
->bind_carrier
))
1799 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1800 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1804 list_updated
= true;
1811 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1812 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1816 link_dirty(carrier
);
1822 static int link_new_bound_to_list(Link
*link
) {
1827 bool list_updated
= false;
1830 assert(link
->manager
);
1835 if (strv_isempty(link
->network
->bind_carrier
))
1840 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1841 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1842 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1846 list_updated
= true;
1853 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1854 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1858 link_dirty(carrier
);
1864 static int link_new_carrier_maps(Link
*link
) {
1867 r
= link_new_bound_by_list(link
);
1871 r
= link_handle_bound_by_list(link
);
1875 r
= link_new_bound_to_list(link
);
1879 r
= link_handle_bound_to_list(link
);
1886 static void link_free_bound_to_list(Link
*link
) {
1890 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1891 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1893 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1894 link_dirty(bound_to
);
1900 static void link_free_bound_by_list(Link
*link
) {
1904 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1905 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1907 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1908 link_dirty(bound_by
);
1909 link_handle_bound_to_list(bound_by
);
1916 static void link_free_carrier_maps(Link
*link
) {
1917 bool list_updated
= false;
1921 if (!hashmap_isempty(link
->bound_to_links
)) {
1922 link_free_bound_to_list(link
);
1923 list_updated
= true;
1926 if (!hashmap_isempty(link
->bound_by_links
)) {
1927 link_free_bound_by_list(link
);
1928 list_updated
= true;
1937 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1944 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1948 r
= set_ensure_allocated(&master
->slaves
, NULL
);
1952 r
= set_put(master
->slaves
, link
);
1960 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1965 if (!link
->manager
|| !netdev
)
1968 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1971 link_unref(set_remove(master
->slaves
, link
));
1974 static void link_detach_from_manager(Link
*link
) {
1975 if (!link
|| !link
->manager
)
1978 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1981 /* The following must be called at last. */
1982 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1986 void link_drop(Link
*link
) {
1987 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1990 link_set_state(link
, LINK_STATE_LINGER
);
1992 link_free_carrier_maps(link
);
1994 if (link
->network
) {
1995 link_drop_from_master(link
, link
->network
->bridge
);
1996 link_drop_from_master(link
, link
->network
->bond
);
1999 log_link_debug(link
, "Link removed");
2001 (void) unlink(link
->state_file
);
2002 link_detach_from_manager(link
);
2005 static int link_joined(Link
*link
) {
2009 assert(link
->network
);
2011 if (!hashmap_isempty(link
->bound_to_links
)) {
2012 r
= link_handle_bound_to_list(link
);
2015 } else if (!(link
->flags
& IFF_UP
)) {
2018 link_enter_failed(link
);
2023 if (link
->network
->bridge
) {
2024 r
= link_set_bridge(link
);
2026 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2028 r
= link_append_to_master(link
, link
->network
->bridge
);
2030 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2033 if (link
->network
->bond
) {
2034 r
= link_set_bond(link
);
2036 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2038 r
= link_append_to_master(link
, link
->network
->bond
);
2040 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2043 if (link
->network
->use_br_vlan
&&
2044 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2045 r
= link_set_bridge_vlan(link
);
2047 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2050 /* Skip setting up addresses until it gets carrier,
2051 or it would try to set addresses twice,
2052 which is bad for non-idempotent steps. */
2053 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2056 link_set_state(link
, LINK_STATE_CONFIGURING
);
2057 return link_request_set_addresses(link
);
2060 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2064 assert(link
->network
);
2065 assert(link
->enslaving
> 0);
2069 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2072 r
= sd_netlink_message_get_errno(m
);
2073 if (r
< 0 && r
!= -EEXIST
) {
2074 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2075 link_enter_failed(link
);
2079 log_link_debug(link
, "Joined netdev");
2081 if (link
->enslaving
== 0) {
2082 r
= link_joined(link
);
2084 link_enter_failed(link
);
2090 static int link_enter_join_netdev(Link
*link
) {
2096 assert(link
->network
);
2097 assert(link
->state
== LINK_STATE_INITIALIZED
);
2099 link_set_state(link
, LINK_STATE_CONFIGURING
);
2102 link
->enslaving
= 0;
2104 if (link
->network
->bond
) {
2105 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2106 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2107 return link_joined(link
);
2109 log_struct(LOG_DEBUG
,
2110 LOG_LINK_INTERFACE(link
),
2111 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2112 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2116 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2118 log_struct_errno(LOG_WARNING
, r
,
2119 LOG_LINK_INTERFACE(link
),
2120 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2121 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2122 link_enter_failed(link
);
2127 if (link
->network
->bridge
) {
2128 log_struct(LOG_DEBUG
,
2129 LOG_LINK_INTERFACE(link
),
2130 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2131 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2135 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2137 log_struct_errno(LOG_WARNING
, r
,
2138 LOG_LINK_INTERFACE(link
),
2139 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2140 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2141 link_enter_failed(link
);
2146 if (link
->network
->vrf
) {
2147 log_struct(LOG_DEBUG
,
2148 LOG_LINK_INTERFACE(link
),
2149 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2150 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2154 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2156 log_struct_errno(LOG_WARNING
, r
,
2157 LOG_LINK_INTERFACE(link
),
2158 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2159 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2160 link_enter_failed(link
);
2165 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2167 if (netdev
->ifindex
> 0)
2168 /* Assume already enslaved. */
2171 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2174 log_struct(LOG_DEBUG
,
2175 LOG_LINK_INTERFACE(link
),
2176 LOG_NETDEV_INTERFACE(netdev
),
2177 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2181 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2183 log_struct_errno(LOG_WARNING
, r
,
2184 LOG_LINK_INTERFACE(link
),
2185 LOG_NETDEV_INTERFACE(netdev
),
2186 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2187 link_enter_failed(link
);
2192 if (link
->enslaving
== 0)
2193 return link_joined(link
);
2198 static int link_set_ipv4_forward(Link
*link
) {
2201 if (!link_ipv4_forward_enabled(link
))
2204 /* We propagate the forwarding flag from one interface to the
2205 * global setting one way. This means: as long as at least one
2206 * interface was configured at any time that had IP forwarding
2207 * enabled the setting will stay on for good. We do this
2208 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2209 * somewhat in sync (see below). */
2211 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2213 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2218 static int link_set_ipv6_forward(Link
*link
) {
2221 if (!link_ipv6_forward_enabled(link
))
2224 /* On Linux, the IPv6 stack does not know a per-interface
2225 * packet forwarding setting: either packet forwarding is on
2226 * for all, or off for all. We hence don't bother with a
2227 * per-interface setting, but simply propagate the interface
2228 * flag, if it is set, to the global flag, one-way. Note that
2229 * while IPv4 would allow a per-interface flag, we expose the
2230 * same behaviour there and also propagate the setting from
2231 * one to all, to keep things simple (see above). */
2233 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2235 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2240 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2241 IPv6PrivacyExtensions s
;
2244 s
= link_ipv6_privacy_extensions(link
);
2248 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2250 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2255 static int link_set_ipv6_accept_ra(Link
*link
) {
2258 /* Make this a NOP if IPv6 is not available */
2259 if (!socket_ipv6_is_supported())
2262 if (link
->flags
& IFF_LOOPBACK
)
2268 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2270 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2275 static int link_set_ipv6_dad_transmits(Link
*link
) {
2278 /* Make this a NOP if IPv6 is not available */
2279 if (!socket_ipv6_is_supported())
2282 if (link
->flags
& IFF_LOOPBACK
)
2288 if (link
->network
->ipv6_dad_transmits
< 0)
2291 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2293 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2298 static int link_set_ipv6_hop_limit(Link
*link
) {
2301 /* Make this a NOP if IPv6 is not available */
2302 if (!socket_ipv6_is_supported())
2305 if (link
->flags
& IFF_LOOPBACK
)
2311 if (link
->network
->ipv6_hop_limit
< 0)
2314 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2316 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2321 static int link_set_ipv6_mtu(Link
*link
) {
2324 /* Make this a NOP if IPv6 is not available */
2325 if (!socket_ipv6_is_supported())
2328 if (link
->flags
& IFF_LOOPBACK
)
2331 if (link
->network
->ipv6_mtu
== 0)
2334 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2336 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2341 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2342 Address
*net_address
;
2350 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2351 if (address_equal(net_address
, address
))
2357 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2366 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2367 if (route_equal(net_route
, route
))
2373 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2380 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2383 /* Even when the address is leased from a DHCP server, networkd assign the address
2384 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2385 * corresponding routes with RTPROT_DHCP. */
2386 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2387 if (route
->protocol
!= RTPROT_DHCP
)
2390 if (address
->family
!= route
->family
)
2393 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2400 static int link_drop_foreign_config(Link
*link
) {
2406 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2407 /* we consider IPv6LL addresses to be managed by the kernel */
2408 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2411 if (link_address_is_dynamic(link
, address
)) {
2412 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2414 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2417 if (link_is_static_address_configured(link
, address
)) {
2418 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2420 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2422 r
= address_remove(address
, link
, NULL
);
2428 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2429 /* do not touch routes managed by the kernel */
2430 if (route
->protocol
== RTPROT_KERNEL
)
2433 /* do not touch multicast route added by kernel */
2434 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2435 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2436 if (route
->protocol
== RTPROT_BOOT
&&
2437 route
->family
== AF_INET6
&&
2438 route
->dst_prefixlen
== 8 &&
2439 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 }}} }))
2442 if (route
->protocol
== RTPROT_STATIC
&&
2443 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2446 if (route
->protocol
== RTPROT_DHCP
&&
2447 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2450 if (link_is_static_route_configured(link
, route
)) {
2451 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2455 r
= route_remove(route
, link
, NULL
);
2464 static int link_drop_config(Link
*link
) {
2465 Address
*address
, *pool_address
;
2470 SET_FOREACH(address
, link
->addresses
, i
) {
2471 /* we consider IPv6LL addresses to be managed by the kernel */
2472 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2475 r
= address_remove(address
, link
, NULL
);
2479 /* If this address came from an address pool, clean up the pool */
2480 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2481 if (address_equal(address
, pool_address
)) {
2482 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2483 address_free(pool_address
);
2489 SET_FOREACH(route
, link
->routes
, i
) {
2490 /* do not touch routes managed by the kernel */
2491 if (route
->protocol
== RTPROT_KERNEL
)
2494 r
= route_remove(route
, link
, NULL
);
2504 static int link_configure(Link
*link
) {
2508 assert(link
->network
);
2509 assert(link
->state
== LINK_STATE_INITIALIZED
);
2511 if (link_is_can(link
))
2512 return link_configure_can(link
);
2514 /* Drop foreign config, but ignore loopback or critical devices.
2515 * We do not want to remove loopback address or addresses used for root NFS. */
2516 if (!(link
->flags
& IFF_LOOPBACK
) &&
2517 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2518 r
= link_drop_foreign_config(link
);
2523 r
= link_set_proxy_arp(link
);
2527 r
= ipv6_proxy_ndp_addresses_configure(link
);
2531 r
= link_set_ipv4_forward(link
);
2535 r
= link_set_ipv6_forward(link
);
2539 r
= link_set_ipv6_privacy_extensions(link
);
2543 r
= link_set_ipv6_accept_ra(link
);
2547 r
= link_set_ipv6_dad_transmits(link
);
2551 r
= link_set_ipv6_hop_limit(link
);
2555 r
= link_set_flags(link
);
2559 r
= link_set_ipv6_mtu(link
);
2563 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2564 r
= ipv4ll_configure(link
);
2569 if (link_dhcp4_enabled(link
)) {
2570 r
= dhcp4_set_promote_secondaries(link
);
2574 r
= dhcp4_configure(link
);
2579 if (link_dhcp4_server_enabled(link
)) {
2580 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2584 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2589 if (link_dhcp6_enabled(link
) ||
2590 link_ipv6_accept_ra_enabled(link
)) {
2591 r
= dhcp6_configure(link
);
2596 if (link_ipv6_accept_ra_enabled(link
)) {
2597 r
= ndisc_configure(link
);
2602 if (link_radv_enabled(link
)) {
2603 r
= radv_configure(link
);
2608 if (link_lldp_rx_enabled(link
)) {
2609 r
= link_lldp_rx_configure(link
);
2614 r
= link_configure_mtu(link
);
2618 r
= link_configure_addrgen_mode(link
);
2622 return link_configure_after_setting_mtu(link
);
2625 static int link_configure_after_setting_mtu(Link
*link
) {
2629 assert(link
->network
);
2630 assert(link
->state
== LINK_STATE_INITIALIZED
);
2632 if (link
->setting_mtu
)
2635 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2636 r
= link_acquire_conf(link
);
2641 return link_enter_join_netdev(link
);
2644 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2647 if (duid
->raw_data_len
> 0)
2650 if (duid
->type
!= DUID_TYPE_UUID
)
2653 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2654 duid
->raw_data_len
= sizeof(sd_id128_t
);
2659 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2660 Manager
*manager
= userdata
;
2661 const sd_bus_error
*e
;
2671 e
= sd_bus_message_get_error(m
);
2673 log_error_errno(sd_bus_error_get_errno(e
),
2674 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2679 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2683 if (sz
!= sizeof(sd_id128_t
)) {
2684 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2688 memcpy(&manager
->product_uuid
, a
, sz
);
2689 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2690 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2692 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2695 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2696 r
= link_configure(link
);
2698 link_enter_failed(link
);
2701 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2703 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2704 * even if the method fails. */
2705 manager
->has_product_uuid
= true;
2710 static bool link_requires_uuid(Link
*link
) {
2714 assert(link
->manager
);
2715 assert(link
->network
);
2717 duid
= link_get_duid(link
);
2718 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2721 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2724 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2730 static int link_configure_duid(Link
*link
) {
2736 assert(link
->manager
);
2737 assert(link
->network
);
2740 duid
= link_get_duid(link
);
2742 if (!link_requires_uuid(link
))
2745 if (m
->has_product_uuid
) {
2746 (void) duid_set_uuid(duid
, m
->product_uuid
);
2750 if (!m
->links_requesting_uuid
) {
2751 r
= manager_request_product_uuid(m
, link
);
2756 log_link_warning_errno(link
, r
,
2757 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2761 r
= set_put(m
->links_requesting_uuid
, link
);
2765 r
= set_put(m
->duids_requesting_uuid
, duid
);
2773 static int link_initialized_and_synced(Link
*link
) {
2778 assert(link
->ifname
);
2779 assert(link
->manager
);
2781 /* We may get called either from the asynchronous netlink callback,
2782 * or directly for link_add() if running in a container. See link_add(). */
2783 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2786 log_link_debug(link
, "Link state is up-to-date");
2787 link_set_state(link
, LINK_STATE_INITIALIZED
);
2789 r
= link_new_bound_by_list(link
);
2793 r
= link_handle_bound_by_list(link
);
2797 if (!link
->network
) {
2798 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2799 &link
->mac
, &network
);
2801 link_enter_unmanaged(link
);
2803 } else if (r
== 0 && network
->unmanaged
) {
2804 link_enter_unmanaged(link
);
2809 if (link
->flags
& IFF_LOOPBACK
) {
2810 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2811 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2813 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2814 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2816 if (network
->dhcp_server
)
2817 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2820 r
= network_apply(network
, link
);
2825 r
= link_new_bound_to_list(link
);
2829 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2830 * link_configure() is called later asynchronously. */
2831 r
= link_configure_duid(link
);
2835 r
= link_configure(link
);
2842 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2845 r
= link_initialized_and_synced(link
);
2847 link_enter_failed(link
);
2851 int link_initialized(Link
*link
, sd_device
*device
) {
2852 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2856 assert(link
->manager
);
2857 assert(link
->manager
->rtnl
);
2860 if (link
->state
!= LINK_STATE_PENDING
)
2863 if (link
->sd_device
)
2866 log_link_debug(link
, "udev initialized link");
2867 link_set_state(link
, LINK_STATE_INITIALIZED
);
2869 link
->sd_device
= sd_device_ref(device
);
2871 /* udev has initialized the link, but we don't know if we have yet
2872 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2873 * when it returns we know that the pending NEWLINKs have already been
2874 * processed and that we are up-to-date */
2876 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2881 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2882 link_netlink_destroy_callback
, link
);
2891 static int link_load(Link
*link
) {
2892 _cleanup_free_
char *network_file
= NULL
,
2895 *dhcp4_address
= NULL
,
2896 *ipv4ll_address
= NULL
;
2897 union in_addr_union address
;
2898 union in_addr_union route_dst
;
2904 r
= parse_env_file(NULL
, link
->state_file
,
2905 "NETWORK_FILE", &network_file
,
2906 "ADDRESSES", &addresses
,
2908 "DHCP4_ADDRESS", &dhcp4_address
,
2909 "IPV4LL_ADDRESS", &ipv4ll_address
);
2910 if (r
< 0 && r
!= -ENOENT
)
2911 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2918 suffix
= strrchr(network_file
, '.');
2920 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2921 goto network_file_fail
;
2925 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2927 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2928 goto network_file_fail
;
2931 r
= network_apply(network
, link
);
2933 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2942 _cleanup_free_
char *address_str
= NULL
;
2943 char *prefixlen_str
;
2945 unsigned char prefixlen
;
2947 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2949 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2955 prefixlen_str
= strchr(address_str
, '/');
2956 if (!prefixlen_str
) {
2957 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2961 *prefixlen_str
++ = '\0';
2963 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2965 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2969 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2971 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2975 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2977 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2986 _cleanup_free_
char *route_str
= NULL
;
2987 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2989 char *prefixlen_str
;
2991 unsigned char prefixlen
, tos
, table
;
2994 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2996 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3002 prefixlen_str
= strchr(route_str
, '/');
3003 if (!prefixlen_str
) {
3004 log_link_debug(link
, "Failed to parse route %s", route_str
);
3008 *prefixlen_str
++ = '\0';
3010 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3012 log_link_debug(link
,
3013 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3018 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3020 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3024 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3026 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3028 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3029 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3030 0, route_expire_handler
, route
);
3032 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3035 route
->lifetime
= lifetime
;
3036 sd_event_source_unref(route
->expire
);
3037 route
->expire
= TAKE_PTR(expire
);
3041 if (dhcp4_address
) {
3042 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3044 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3045 goto dhcp4_address_fail
;
3048 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3050 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3052 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3054 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3059 if (ipv4ll_address
) {
3060 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3062 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3063 goto ipv4ll_address_fail
;
3066 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3068 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3070 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3072 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3075 ipv4ll_address_fail
:
3080 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3081 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3082 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3091 r
= link_new(m
, message
, ret
);
3097 log_link_debug(link
, "Link %d added", link
->ifindex
);
3099 r
= link_load(link
);
3103 if (detect_container() <= 0) {
3104 /* not in a container, udev will be around */
3105 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3106 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3108 log_link_warning_errno(link
, r
, "Could not find device: %m");
3112 r
= sd_device_get_is_initialized(device
);
3114 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3119 log_link_debug(link
, "link pending udev initialization...");
3123 r
= device_is_renaming(device
);
3125 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3129 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3133 r
= link_initialized(link
, device
);
3137 r
= link_initialized_and_synced(link
);
3144 link_enter_failed(link
);
3148 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3153 log_link_info(link
, "Gained IPv6LL");
3155 link
->ipv6ll_address
= *address
;
3156 link_check_ready(link
);
3158 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3159 r
= link_acquire_ipv6_conf(link
);
3161 link_enter_failed(link
);
3169 static int link_carrier_gained(Link
*link
) {
3174 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3175 r
= link_acquire_conf(link
);
3177 link_enter_failed(link
);
3181 link_set_state(link
, LINK_STATE_CONFIGURING
);
3182 r
= link_request_set_addresses(link
);
3187 r
= link_handle_bound_by_list(link
);
3194 static int link_carrier_lost(Link
*link
) {
3199 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3202 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3203 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3204 * configuration and stop the clients as well. */
3205 if (link
->setting_mtu
)
3208 r
= link_stop_clients(link
, false);
3210 link_enter_failed(link
);
3214 if (link_dhcp4_server_enabled(link
))
3215 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3217 r
= link_drop_config(link
);
3221 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3222 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3223 r
= link_drop_foreign_config(link
);
3228 r
= link_handle_bound_by_list(link
);
3235 int link_carrier_reset(Link
*link
) {
3240 if (link_has_carrier(link
)) {
3241 r
= link_carrier_lost(link
);
3245 r
= link_carrier_gained(link
);
3249 log_link_info(link
, "Reset carrier");
3255 int link_update(Link
*link
, sd_netlink_message
*m
) {
3256 struct ether_addr mac
;
3259 bool had_carrier
, carrier_gained
, carrier_lost
;
3263 assert(link
->ifname
);
3266 if (link
->state
== LINK_STATE_LINGER
) {
3267 log_link_info(link
, "Link re-added");
3268 link_set_state(link
, LINK_STATE_CONFIGURING
);
3270 r
= link_new_carrier_maps(link
);
3275 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3276 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3277 Manager
*manager
= link
->manager
;
3279 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3282 r
= link_add(manager
, m
, &link
);
3287 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3288 if (r
>= 0 && mtu
> 0) {
3290 if (link
->original_mtu
== 0) {
3291 link
->original_mtu
= mtu
;
3292 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3295 if (link
->dhcp_client
) {
3296 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3299 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3303 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3305 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3309 /* The kernel may broadcast NEWLINK messages without the MAC address
3310 set, simply ignore them. */
3311 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3313 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3316 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3319 log_link_debug(link
, "MAC address: "
3320 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3321 mac
.ether_addr_octet
[0],
3322 mac
.ether_addr_octet
[1],
3323 mac
.ether_addr_octet
[2],
3324 mac
.ether_addr_octet
[3],
3325 mac
.ether_addr_octet
[4],
3326 mac
.ether_addr_octet
[5]);
3329 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3331 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3334 if (link
->dhcp_client
) {
3335 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3336 (const uint8_t *) &link
->mac
,
3340 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3342 r
= dhcp4_set_client_identifier(link
);
3347 if (link
->dhcp6_client
) {
3348 const DUID
* duid
= link_get_duid(link
);
3350 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3351 (const uint8_t *) &link
->mac
,
3355 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3357 if (link
->network
->iaid_set
) {
3358 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3359 link
->network
->iaid
);
3361 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3364 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3366 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3367 duid
->raw_data_len
);
3369 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3373 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3375 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3379 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3381 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3386 old_master
= link
->master_ifindex
;
3387 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3389 had_carrier
= link_has_carrier(link
);
3391 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3395 r
= link_update_lldp(link
);
3399 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3400 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3402 if (carrier_gained
) {
3403 log_link_info(link
, "Gained carrier");
3405 r
= link_carrier_gained(link
);
3408 } else if (carrier_lost
) {
3409 log_link_info(link
, "Lost carrier");
3411 r
= link_carrier_lost(link
);
3419 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3427 if (hashmap_isempty(h
))
3431 HASHMAP_FOREACH(link
, h
, i
) {
3435 fprintf(f
, "%i", link
->ifindex
);
3442 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
3446 for (j
= 0; j
< n_dns
; j
++) {
3447 _cleanup_free_
char *b
= NULL
;
3449 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
3451 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3462 int link_save(Link
*link
) {
3463 _cleanup_free_
char *temp_path
= NULL
;
3464 _cleanup_fclose_
FILE *f
= NULL
;
3465 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
3472 assert(link
->state_file
);
3473 assert(link
->lease_file
);
3474 assert(link
->manager
);
3476 if (link
->state
== LINK_STATE_LINGER
) {
3477 (void) unlink(link
->state_file
);
3481 link_lldp_save(link
);
3483 admin_state
= link_state_to_string(link
->state
);
3484 assert(admin_state
);
3486 oper_state
= link_operstate_to_string(link
->operstate
);
3489 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
3490 assert(carrier_state
);
3492 address_state
= link_address_state_to_string(link
->address_state
);
3493 assert(address_state
);
3495 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3499 (void) fchmod(fileno(f
), 0644);
3502 "# This is private data. Do not parse.\n"
3505 "CARRIER_STATE=%s\n"
3506 "ADDRESS_STATE=%s\n",
3507 admin_state
, oper_state
, carrier_state
, address_state
);
3509 if (link
->network
) {
3510 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3511 const char *dhcp_domainname
= NULL
, *p
;
3512 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3515 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3516 yes_no(link
->network
->required_for_online
));
3518 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3519 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3521 if (link
->dhcp6_client
) {
3522 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3523 if (r
< 0 && r
!= -ENOMSG
)
3524 log_link_debug(link
, "No DHCPv6 lease");
3527 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3532 if (link
->n_dns
!= (unsigned) -1)
3533 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
3535 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
3537 if (link
->network
->dhcp_use_dns
&&
3539 const struct in_addr
*addresses
;
3541 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3543 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3547 if (link
->network
->dhcp6_use_dns
&& dhcp6_lease
) {
3548 struct in6_addr
*in6_addrs
;
3550 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3554 serialize_in6_addrs(f
, in6_addrs
, r
);
3559 /* Make sure to flush out old entries before we use the NDISC data */
3562 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3565 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3569 serialize_in6_addrs(f
, &dd
->address
, 1);
3578 fputstrv(f
, link
->ntp
?: link
->network
->ntp
, NULL
, &space
);
3580 if (link
->network
->dhcp_use_ntp
&&
3582 const struct in_addr
*addresses
;
3584 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3586 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3590 if (link
->network
->dhcp6_use_ntp
&& dhcp6_lease
) {
3591 struct in6_addr
*in6_addrs
;
3594 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3599 serialize_in6_addrs(f
, in6_addrs
, r
);
3603 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3605 fputstrv(f
, hosts
, NULL
, &space
);
3610 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3611 if (link
->dhcp_lease
) {
3612 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3613 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3616 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3619 fputs("DOMAINS=", f
);
3621 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
3622 fputs_with_space(f
, p
, NULL
, &space
);
3624 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
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_YES
) {
3636 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3637 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3642 fputs("ROUTE_DOMAINS=", f
);
3644 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
3645 fputs_with_space(f
, p
, NULL
, &space
);
3647 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3648 if (dhcp_domainname
)
3649 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3651 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3653 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3656 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3659 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3660 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3665 fprintf(f
, "LLMNR=%s\n",
3666 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
3667 fprintf(f
, "MDNS=%s\n",
3668 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
3669 if (link
->dns_default_route
>= 0)
3670 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->dns_default_route
));
3671 else if (link
->network
->dns_default_route
>= 0)
3672 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3674 if (link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3675 fprintf(f
, "DNS_OVER_TLS=%s\n",
3676 dns_over_tls_mode_to_string(link
->dns_over_tls_mode
));
3677 else if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3678 fprintf(f
, "DNS_OVER_TLS=%s\n",
3679 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3681 if (link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3682 fprintf(f
, "DNSSEC=%s\n",
3683 dnssec_mode_to_string(link
->dnssec_mode
));
3684 else if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3685 fprintf(f
, "DNSSEC=%s\n",
3686 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3688 if (!set_isempty(link
->dnssec_negative_trust_anchors
)) {
3691 fputs("DNSSEC_NTA=", f
);
3693 SET_FOREACH(n
, link
->dnssec_negative_trust_anchors
, i
)
3694 fputs_with_space(f
, n
, NULL
, &space
);
3696 } else if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3699 fputs("DNSSEC_NTA=", f
);
3701 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3702 fputs_with_space(f
, n
, NULL
, &space
);
3706 fputs("ADDRESSES=", f
);
3708 SET_FOREACH(a
, link
->addresses
, i
) {
3709 _cleanup_free_
char *address_str
= NULL
;
3711 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3715 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3720 fputs("ROUTES=", f
);
3722 SET_FOREACH(route
, link
->routes
, i
) {
3723 _cleanup_free_
char *route_str
= NULL
;
3725 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3729 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3730 space
? " " : "", route_str
,
3731 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3738 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3739 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3741 if (link
->dhcp_lease
) {
3742 struct in_addr address
;
3743 const char *tz
= NULL
;
3745 assert(link
->network
);
3747 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3749 fprintf(f
, "TIMEZONE=%s\n", tz
);
3751 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3753 fputs("DHCP4_ADDRESS=", f
);
3754 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3758 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3766 (void) unlink(link
->lease_file
);
3769 struct in_addr address
;
3771 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3773 fputs("IPV4LL_ADDRESS=", f
);
3774 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3779 r
= fflush_and_check(f
);
3783 if (rename(temp_path
, link
->state_file
) < 0) {
3791 (void) unlink(link
->state_file
);
3793 (void) unlink(temp_path
);
3795 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3798 /* The serialized state in /run is no longer up-to-date. */
3799 void link_dirty(Link
*link
) {
3804 /* mark manager dirty as link is dirty */
3805 manager_dirty(link
->manager
);
3807 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3809 /* allocation errors are ignored */
3812 r
= set_put(link
->manager
->dirty_links
, link
);
3814 /* don't take another ref if the link was already dirty */
3820 /* The serialized state in /run is up-to-date */
3821 void link_clean(Link
*link
) {
3823 assert(link
->manager
);
3825 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3828 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3829 [LINK_STATE_PENDING
] = "pending",
3830 [LINK_STATE_INITIALIZED
] = "initialized",
3831 [LINK_STATE_CONFIGURING
] = "configuring",
3832 [LINK_STATE_CONFIGURED
] = "configured",
3833 [LINK_STATE_UNMANAGED
] = "unmanaged",
3834 [LINK_STATE_FAILED
] = "failed",
3835 [LINK_STATE_LINGER
] = "linger",
3838 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);