1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
8 #include "alloc-util.h"
10 #include "dhcp-identifier.h"
11 #include "dhcp-lease-internal.h"
15 #include "missing_network.h"
16 #include "netdev/bond.h"
17 #include "netdev/bridge.h"
18 #include "netdev/ipvlan.h"
19 #include "netdev/vrf.h"
20 #include "netlink-util.h"
21 #include "network-internal.h"
22 #include "networkd-can.h"
23 #include "networkd-dhcp-server.h"
24 #include "networkd-dhcp4.h"
25 #include "networkd-dhcp6.h"
26 #include "networkd-ipv4ll.h"
27 #include "networkd-ipv6-proxy-ndp.h"
28 #include "networkd-link-bus.h"
29 #include "networkd-link.h"
30 #include "networkd-lldp-tx.h"
31 #include "networkd-manager.h"
32 #include "networkd-ndisc.h"
33 #include "networkd-neighbor.h"
34 #include "networkd-radv.h"
35 #include "networkd-routing-policy-rule.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
41 #include "sysctl-util.h"
42 #include "tmpfile-util.h"
43 #include "udev-util.h"
47 uint32_t link_get_vrf_table(Link
*link
) {
48 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
51 uint32_t link_get_dhcp_route_table(Link
*link
) {
52 /* When the interface is part of an VRF use the VRFs routing table, unless
53 * another table is explicitly specified. */
54 if (link
->network
->dhcp_route_table_set
)
55 return link
->network
->dhcp_route_table
;
56 return link_get_vrf_table(link
);
59 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
60 if (link
->network
->ipv6_accept_ra_route_table_set
)
61 return link
->network
->ipv6_accept_ra_route_table
;
62 return link_get_vrf_table(link
);
65 DUID
* link_get_duid(Link
*link
) {
66 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
67 return &link
->network
->duid
;
69 return &link
->manager
->duid
;
72 int link_sysctl_ipv6_enabled(Link
*link
) {
73 _cleanup_free_
char *value
= NULL
;
79 if (link
->sysctl_ipv6_enabled
>= 0)
80 return link
->sysctl_ipv6_enabled
;
82 const char *ifname
= link
->ifname
; /* work around bogus gcc warning */
83 r
= sysctl_read_ip_property(AF_INET6
, ifname
, "disable_ipv6", &value
);
85 return log_link_warning_errno(link
, r
,
86 "Failed to read net.ipv6.conf.%s.disable_ipv6 sysctl property: %m",
89 link
->sysctl_ipv6_enabled
= value
[0] == '0';
90 return link
->sysctl_ipv6_enabled
;
93 static bool link_dhcp6_enabled(Link
*link
) {
96 if (!socket_ipv6_is_supported())
99 if (link
->flags
& IFF_LOOPBACK
)
105 if (link
->network
->bond
)
108 if (link
->iftype
== ARPHRD_CAN
)
111 if (link_sysctl_ipv6_enabled(link
) == 0)
114 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
117 static bool link_dhcp4_enabled(Link
*link
) {
120 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->network
->bond
)
129 if (link
->iftype
== ARPHRD_CAN
)
132 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
135 static bool link_dhcp4_server_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->network
->bond
)
147 if (link
->iftype
== ARPHRD_CAN
)
150 return link
->network
->dhcp_server
;
153 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
155 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->iftype
== ARPHRD_CAN
)
166 if (STRPTR_IN_SET(link
->kind
,
167 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
168 "vti6", "nlmon", "xfrm"))
171 /* L3 or L3S mode do not support ARP. */
172 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
175 if (link
->network
->bond
)
178 return link
->network
->link_local
& mask
;
181 static bool link_ipv6ll_enabled(Link
*link
) {
184 if (!socket_ipv6_is_supported())
187 if (link
->flags
& IFF_LOOPBACK
)
193 if (link
->iftype
== ARPHRD_CAN
)
196 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
199 if (link
->network
->bond
)
202 if (link_sysctl_ipv6_enabled(link
) == 0)
205 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
208 static bool link_ipv6_enabled(Link
*link
) {
211 if (!socket_ipv6_is_supported())
214 if (link
->network
->bond
)
217 if (link_sysctl_ipv6_enabled(link
) == 0)
220 if (link
->iftype
== ARPHRD_CAN
)
223 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
224 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
227 static bool link_radv_enabled(Link
*link
) {
230 if (!link_ipv6ll_enabled(link
))
233 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
236 static bool link_ipv4_forward_enabled(Link
*link
) {
239 if (link
->flags
& IFF_LOOPBACK
)
245 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
248 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
251 static bool link_ipv6_forward_enabled(Link
*link
) {
254 if (!socket_ipv6_is_supported())
257 if (link
->flags
& IFF_LOOPBACK
)
263 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
266 if (link_sysctl_ipv6_enabled(link
) == 0)
269 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
272 static bool link_proxy_arp_enabled(Link
*link
) {
275 if (link
->flags
& IFF_LOOPBACK
)
281 if (link
->network
->proxy_arp
< 0)
287 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
290 if (!socket_ipv6_is_supported())
293 if (link
->flags
& IFF_LOOPBACK
)
299 if (!link_ipv6ll_enabled(link
))
302 /* If unset use system default (enabled if local forwarding is disabled.
303 * disabled if local forwarding is enabled).
304 * If set, ignore or enforce RA independent of local forwarding state.
306 if (link
->network
->ipv6_accept_ra
< 0)
307 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
308 return !link_ipv6_forward_enabled(link
);
309 else if (link
->network
->ipv6_accept_ra
> 0)
310 /* accept RA even if ip_forward is enabled */
317 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
320 if (!socket_ipv6_is_supported())
321 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
323 if (link
->flags
& IFF_LOOPBACK
)
324 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
327 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
329 return link
->network
->ipv6_privacy_extensions
;
332 static int link_enable_ipv6(Link
*link
) {
336 if (link
->flags
& IFF_LOOPBACK
)
339 disabled
= !link_ipv6_enabled(link
);
341 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
343 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
345 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
350 static bool link_is_enslaved(Link
*link
) {
351 if (link
->flags
& IFF_SLAVE
)
352 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
358 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
361 /* TODO: add conditions for other netdevs. */
366 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
372 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
375 link_update_operstate(master
, true);
378 void link_update_operstate(Link
*link
, bool also_update_master
) {
379 LinkOperationalState operstate
;
380 LinkCarrierState carrier_state
;
381 LinkAddressState address_state
;
382 _cleanup_strv_free_
char **p
= NULL
;
383 uint8_t scope
= RT_SCOPE_NOWHERE
;
384 bool changed
= false;
390 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
391 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
392 else if (link_has_carrier(link
)) {
393 if (link_is_enslaved(link
))
394 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
396 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
397 } else if (link
->flags
& IFF_UP
)
398 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
400 carrier_state
= LINK_CARRIER_STATE_OFF
;
402 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
405 SET_FOREACH(slave
, link
->slaves
, i
) {
406 link_update_operstate(slave
, false);
408 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
409 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
413 SET_FOREACH(address
, link
->addresses
, i
) {
414 if (!address_is_ready(address
))
417 if (address
->scope
< scope
)
418 scope
= address
->scope
;
421 /* for operstate we also take foreign addresses into account */
422 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
423 if (!address_is_ready(address
))
426 if (address
->scope
< scope
)
427 scope
= address
->scope
;
430 if (scope
< RT_SCOPE_SITE
)
431 /* universally accessible addresses found */
432 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
433 else if (scope
< RT_SCOPE_HOST
)
434 /* only link or site local addresses found */
435 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
437 /* no useful addresses found */
438 address_state
= LINK_ADDRESS_STATE_OFF
;
440 /* Mapping of address and carrier state vs operational state
442 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
443 * ------------------------------------------------------------------------------
444 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
445 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
446 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
449 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
450 operstate
= (LinkOperationalState
) carrier_state
;
451 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
452 operstate
= LINK_OPERSTATE_ROUTABLE
;
453 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
454 operstate
= LINK_OPERSTATE_DEGRADED
;
456 operstate
= LINK_OPERSTATE_ENSLAVED
;
458 if (link
->carrier_state
!= carrier_state
) {
459 link
->carrier_state
= carrier_state
;
461 if (strv_extend(&p
, "CarrierState") < 0)
465 if (link
->address_state
!= address_state
) {
466 link
->address_state
= address_state
;
468 if (strv_extend(&p
, "AddressState") < 0)
472 if (link
->operstate
!= operstate
) {
473 link
->operstate
= operstate
;
475 if (strv_extend(&p
, "OperationalState") < 0)
480 link_send_changed_strv(link
, p
);
484 if (also_update_master
&& link
->network
) {
485 link_update_master_operstate(link
, link
->network
->bond
);
486 link_update_master_operstate(link
, link
->network
->bridge
);
490 #define FLAG_STRING(string, flag, old, new) \
491 (((old ^ new) & flag) \
492 ? ((old & flag) ? (" -" string) : (" +" string)) \
495 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
496 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
502 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
504 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
506 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
508 /* if we got a message without operstate, take it to mean
509 the state was unchanged */
510 operstate
= link
->kernel_operstate
;
512 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
515 if (link
->flags
!= flags
) {
516 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",
517 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
518 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
519 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
520 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
521 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
522 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
523 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
524 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
525 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
526 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
527 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
528 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
529 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
530 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
531 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
532 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
533 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
534 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
535 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
537 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
538 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
539 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
540 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
541 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
542 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
543 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
544 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
546 /* link flags are currently at most 18 bits, let's align to
548 if (unknown_flags_added
)
550 "Unknown link flags gained: %#.5x (ignoring)",
551 unknown_flags_added
);
553 if (unknown_flags_removed
)
555 "Unknown link flags lost: %#.5x (ignoring)",
556 unknown_flags_removed
);
560 link
->kernel_operstate
= operstate
;
562 link_update_operstate(link
, true);
567 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
568 _cleanup_(link_unrefp
) Link
*link
= NULL
;
570 const char *ifname
, *kind
= NULL
;
572 unsigned short iftype
;
578 /* check for link kind */
579 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
581 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
582 r
= sd_netlink_message_exit_container(message
);
587 r
= sd_netlink_message_get_type(message
, &type
);
590 else if (type
!= RTM_NEWLINK
)
593 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
596 else if (ifindex
<= 0)
599 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
603 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
614 .state
= LINK_STATE_PENDING
,
617 .sysctl_ipv6_enabled
= -1,
619 .n_dns
= (unsigned) -1,
620 .dns_default_route
= -1,
621 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
622 .mdns
= _RESOLVE_SUPPORT_INVALID
,
623 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
624 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
627 link
->ifname
= strdup(ifname
);
632 link
->kind
= strdup(kind
);
637 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
639 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
641 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
643 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
645 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
648 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
651 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
654 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
658 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
662 r
= link_update_flags(link
, message
, false);
666 *ret
= TAKE_PTR(link
);
671 void link_ntp_settings_clear(Link
*link
) {
672 link
->ntp
= strv_free(link
->ntp
);
675 void link_dns_settings_clear(Link
*link
) {
676 link
->dns
= mfree(link
->dns
);
677 link
->n_dns
= (unsigned) -1;
679 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
680 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
682 link
->dns_default_route
= -1;
683 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
684 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
685 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
686 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
688 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
691 static Link
*link_free(Link
*link
) {
696 link_ntp_settings_clear(link
);
697 link_dns_settings_clear(link
);
699 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
700 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
702 link
->neighbors
= set_free_with_destructor(link
->neighbors
, neighbor_free
);
703 link
->neighbors_foreign
= set_free_with_destructor(link
->neighbors_foreign
, neighbor_free
);
705 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
706 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
708 while ((address
= link
->pool_addresses
)) {
709 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
710 address_free(address
);
713 sd_dhcp_server_unref(link
->dhcp_server
);
714 sd_dhcp_client_unref(link
->dhcp_client
);
715 sd_dhcp_lease_unref(link
->dhcp_lease
);
716 set_free(link
->dhcp_routes
);
718 link_lldp_emit_stop(link
);
720 free(link
->lease_file
);
722 sd_lldp_unref(link
->lldp
);
723 free(link
->lldp_file
);
727 sd_ipv4ll_unref(link
->ipv4ll
);
728 sd_dhcp6_client_unref(link
->dhcp6_client
);
729 sd_ndisc_unref(link
->ndisc
);
730 sd_radv_unref(link
->radv
);
735 (void) unlink(link
->state_file
);
736 free(link
->state_file
);
738 sd_device_unref(link
->sd_device
);
740 hashmap_free(link
->bound_to_links
);
741 hashmap_free(link
->bound_by_links
);
743 set_free_with_destructor(link
->slaves
, link_unref
);
745 network_unref(link
->network
);
750 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
752 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
759 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
768 void link_set_state(Link
*link
, LinkState state
) {
771 if (link
->state
== state
)
774 log_link_debug(link
, "State changed: %s -> %s",
775 link_state_to_string(link
->state
),
776 link_state_to_string(state
));
780 link_send_changed(link
, "AdministrativeState", NULL
);
783 static void link_enter_unmanaged(Link
*link
) {
786 link_set_state(link
, LINK_STATE_UNMANAGED
);
791 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
795 assert(link
->manager
);
796 assert(link
->manager
->event
);
798 dhcp4_release_old_lease(link
);
800 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
801 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
802 k
= sd_dhcp_client_stop(link
->dhcp_client
);
804 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
808 k
= sd_ipv4ll_stop(link
->ipv4ll
);
810 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
813 if (link
->dhcp6_client
) {
814 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
816 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
820 k
= sd_ndisc_stop(link
->ndisc
);
822 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
826 k
= sd_radv_stop(link
->radv
);
828 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
831 link_lldp_emit_stop(link
);
835 void link_enter_failed(Link
*link
) {
838 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
841 log_link_warning(link
, "Failed");
843 link_set_state(link
, LINK_STATE_FAILED
);
845 link_stop_clients(link
, false);
850 static int link_join_netdevs_after_configured(Link
*link
) {
855 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
856 if (netdev
->ifindex
> 0)
857 /* Assume already enslaved. */
860 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
863 log_struct(LOG_DEBUG
,
864 LOG_LINK_INTERFACE(link
),
865 LOG_NETDEV_INTERFACE(netdev
),
866 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
868 r
= netdev_join(netdev
, link
, NULL
);
870 return log_struct_errno(LOG_WARNING
, r
,
871 LOG_LINK_INTERFACE(link
),
872 LOG_NETDEV_INTERFACE(netdev
),
873 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
879 static void link_enter_configured(Link
*link
) {
881 assert(link
->network
);
883 if (link
->state
!= LINK_STATE_CONFIGURING
)
886 log_link_info(link
, "Configured");
888 link_set_state(link
, LINK_STATE_CONFIGURED
);
890 (void) link_join_netdevs_after_configured(link
);
895 static int link_request_set_routing_policy_rule(Link
*link
) {
896 RoutingPolicyRule
*rule
, *rrule
= NULL
;
900 assert(link
->network
);
902 link
->routing_policy_rules_configured
= false;
904 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
905 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
908 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
912 r
= routing_policy_rule_configure(rule
, link
, NULL
);
914 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
916 link
->routing_policy_rule_messages
++;
919 routing_policy_rule_purge(link
->manager
, link
);
920 if (link
->routing_policy_rule_messages
== 0) {
921 link
->routing_policy_rules_configured
= true;
922 link_check_ready(link
);
924 log_link_debug(link
, "Setting routing policy rules");
925 link_set_state(link
, LINK_STATE_CONFIGURING
);
931 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
935 assert(link
->route_messages
> 0);
936 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
937 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
939 link
->route_messages
--;
941 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
944 r
= sd_netlink_message_get_errno(m
);
945 if (r
< 0 && r
!= -EEXIST
) {
946 log_link_warning_errno(link
, r
, "Could not set route: %m");
947 link_enter_failed(link
);
951 if (link
->route_messages
== 0) {
952 log_link_debug(link
, "Routes set");
953 link
->static_routes_configured
= true;
954 link_check_ready(link
);
960 int link_request_set_routes(Link
*link
) {
962 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
963 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
970 assert(link
->network
);
971 assert(link
->addresses_configured
);
972 assert(link
->address_messages
== 0);
973 assert(link
->state
!= _LINK_STATE_INVALID
);
975 link
->static_routes_configured
= false;
977 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
978 /* During configuring addresses, the link lost its carrier. As networkd is dropping
979 * the addresses now, let's not configure the routes either. */
982 r
= link_request_set_routing_policy_rule(link
);
986 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
987 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
988 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
990 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
993 r
= route_configure(rt
, link
, route_handler
);
995 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
997 link
->route_messages
++;
1000 if (link
->route_messages
== 0) {
1001 link
->static_routes_configured
= true;
1002 link_check_ready(link
);
1004 log_link_debug(link
, "Setting routes");
1005 link_set_state(link
, LINK_STATE_CONFIGURING
);
1011 void link_check_ready(Link
*link
) {
1018 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1024 if (!link
->addresses_configured
)
1027 if (!link
->neighbors_configured
)
1030 SET_FOREACH(a
, link
->addresses
, i
)
1031 if (!address_is_ready(a
))
1034 if (!link
->addresses_ready
) {
1035 link
->addresses_ready
= true;
1036 r
= link_request_set_routes(link
);
1038 link_enter_failed(link
);
1042 if (!link
->static_routes_configured
)
1045 if (!link
->routing_policy_rules_configured
)
1048 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1050 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
1053 if (link_ipv6ll_enabled(link
) &&
1054 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1057 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1058 !link
->dhcp4_configured
&&
1059 !link
->dhcp6_configured
&&
1060 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1061 /* When DHCP is enabled, at least one protocol must provide an address, or
1062 * an IPv4ll fallback address must be configured. */
1065 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1069 if (link
->state
!= LINK_STATE_CONFIGURED
)
1070 link_enter_configured(link
);
1075 static int link_request_set_neighbors(Link
*link
) {
1080 assert(link
->network
);
1081 assert(link
->state
!= _LINK_STATE_INVALID
);
1083 link
->neighbors_configured
= false;
1085 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1086 r
= neighbor_configure(neighbor
, link
, NULL
);
1088 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1091 if (link
->neighbor_messages
== 0) {
1092 link
->neighbors_configured
= true;
1093 link_check_ready(link
);
1095 log_link_debug(link
, "Setting neighbors");
1096 link_set_state(link
, LINK_STATE_CONFIGURING
);
1102 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1108 assert(link
->ifname
);
1109 assert(link
->address_messages
> 0);
1110 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1111 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1113 link
->address_messages
--;
1115 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1118 r
= sd_netlink_message_get_errno(m
);
1119 if (r
< 0 && r
!= -EEXIST
) {
1120 log_link_warning_errno(link
, r
, "could not set address: %m");
1121 link_enter_failed(link
);
1124 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1126 if (link
->address_messages
== 0) {
1127 log_link_debug(link
, "Addresses set");
1128 link
->addresses_configured
= true;
1129 link_check_ready(link
);
1135 static int link_set_bridge_fdb(Link
*link
) {
1136 FdbEntry
*fdb_entry
;
1139 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1140 r
= fdb_entry_configure(link
, fdb_entry
);
1142 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1148 static int link_request_set_addresses(Link
*link
) {
1149 AddressLabel
*label
;
1154 assert(link
->network
);
1155 assert(link
->state
!= _LINK_STATE_INVALID
);
1157 /* Reset all *_configured flags we are configuring. */
1158 link
->addresses_configured
= false;
1159 link
->addresses_ready
= false;
1160 link
->neighbors_configured
= false;
1161 link
->static_routes_configured
= false;
1162 link
->routing_policy_rules_configured
= false;
1164 r
= link_set_bridge_fdb(link
);
1168 r
= link_request_set_neighbors(link
);
1172 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1175 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1177 r
= address_configure(ad
, link
, address_handler
, update
);
1179 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1181 link
->address_messages
++;
1184 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1185 r
= address_label_configure(label
, link
, NULL
, false);
1187 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1189 link
->address_label_messages
++;
1192 /* now that we can figure out a default address for the dhcp server,
1194 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1195 r
= dhcp4_server_configure(link
);
1198 log_link_debug(link
, "Offering DHCPv4 leases");
1201 if (link
->address_messages
== 0) {
1202 link
->addresses_configured
= true;
1203 link_check_ready(link
);
1205 log_link_debug(link
, "Setting addresses");
1206 link_set_state(link
, LINK_STATE_CONFIGURING
);
1212 static int link_set_bridge_vlan(Link
*link
) {
1215 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1217 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1222 static int link_set_proxy_arp(Link
*link
) {
1225 if (!link_proxy_arp_enabled(link
))
1228 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1230 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1235 static int link_configure_after_setting_mtu(Link
*link
);
1237 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1242 assert(link
->ifname
);
1244 link
->setting_mtu
= false;
1246 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1249 r
= sd_netlink_message_get_errno(m
);
1251 log_link_warning_errno(link
, r
, "Could not set MTU, ignoring: %m");
1253 log_link_debug(link
, "Setting MTU done.");
1255 if (link
->state
== LINK_STATE_INITIALIZED
) {
1256 r
= link_configure_after_setting_mtu(link
);
1258 link_enter_failed(link
);
1264 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1265 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1269 assert(link
->manager
);
1270 assert(link
->manager
->rtnl
);
1272 if (mtu
== 0 || link
->setting_mtu
)
1275 if (link
->mtu
== mtu
)
1278 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1280 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1282 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1284 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1285 * for this interface, then disable IPv6 else enable it. */
1286 (void) link_enable_ipv6(link
);
1288 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1289 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1290 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1292 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1293 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1298 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1300 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1302 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1303 link_netlink_destroy_callback
, link
);
1305 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1308 link
->setting_mtu
= true;
1313 static bool link_reduces_vlan_mtu(Link
*link
) {
1314 /* See netif_reduces_vlan_mtu() in kernel. */
1315 return streq_ptr(link
->kind
, "macsec");
1318 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1323 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1324 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1325 /* See vlan_dev_change_mtu() in kernel. */
1326 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1328 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1329 /* See macvlan_change_mtu() in kernel. */
1335 static int link_configure_mtu(Link
*link
) {
1339 assert(link
->network
);
1341 if (link
->network
->mtu
> 0)
1342 return link_set_mtu(link
, link
->network
->mtu
);
1344 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1345 if (link
->mtu
>= mtu
)
1348 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1349 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1352 return link_set_mtu(link
, mtu
);
1355 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1360 assert(link
->ifname
);
1362 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1365 r
= sd_netlink_message_get_errno(m
);
1367 log_link_warning_errno(link
, r
, "Could not set link flags, ignoring: %m");
1372 static int link_set_flags(Link
*link
) {
1373 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1374 unsigned ifi_change
= 0;
1375 unsigned ifi_flags
= 0;
1379 assert(link
->manager
);
1380 assert(link
->manager
->rtnl
);
1382 if (link
->flags
& IFF_LOOPBACK
)
1388 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1391 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1393 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1395 if (link
->network
->arp
>= 0) {
1396 ifi_change
|= IFF_NOARP
;
1397 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1400 if (link
->network
->multicast
>= 0) {
1401 ifi_change
|= IFF_MULTICAST
;
1402 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1405 if (link
->network
->allmulticast
>= 0) {
1406 ifi_change
|= IFF_ALLMULTI
;
1407 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1410 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1412 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1414 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1415 link_netlink_destroy_callback
, link
);
1417 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1424 static int link_acquire_ipv6_conf(Link
*link
) {
1429 if (link_ipv6_accept_ra_enabled(link
)) {
1430 assert(link
->ndisc
);
1432 log_link_debug(link
, "Discovering IPv6 routers");
1434 r
= sd_ndisc_start(link
->ndisc
);
1435 if (r
< 0 && r
!= -EBUSY
)
1436 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1439 if (link_radv_enabled(link
)) {
1441 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1443 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1445 r
= sd_radv_start(link
->radv
);
1446 if (r
< 0 && r
!= -EBUSY
)
1447 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1450 (void) dhcp6_request_prefix_delegation(link
);
1455 static int link_acquire_ipv4_conf(Link
*link
) {
1459 assert(link
->manager
);
1460 assert(link
->manager
->event
);
1462 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1463 assert(link
->ipv4ll
);
1465 log_link_debug(link
, "Acquiring IPv4 link-local address");
1467 r
= sd_ipv4ll_start(link
->ipv4ll
);
1469 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1472 if (link_dhcp4_enabled(link
)) {
1473 assert(link
->dhcp_client
);
1475 log_link_debug(link
, "Acquiring DHCPv4 lease");
1477 r
= sd_dhcp_client_start(link
->dhcp_client
);
1479 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1485 static int link_acquire_conf(Link
*link
) {
1490 r
= link_acquire_ipv4_conf(link
);
1494 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1495 r
= link_acquire_ipv6_conf(link
);
1500 if (link_lldp_emit_enabled(link
)) {
1501 r
= link_lldp_emit_start(link
);
1503 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1509 bool link_has_carrier(Link
*link
) {
1510 /* see Documentation/networking/operstates.txt in the kernel sources */
1512 if (link
->kernel_operstate
== IF_OPER_UP
)
1515 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1516 /* operstate may not be implemented, so fall back to flags */
1517 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1518 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1524 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1529 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1532 r
= sd_netlink_message_get_errno(m
);
1534 log_link_warning_errno(link
, r
, "Could not set address genmode for interface, ignoring: %m");
1539 static int link_configure_addrgen_mode(Link
*link
) {
1540 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1541 uint8_t ipv6ll_mode
;
1545 assert(link
->network
);
1546 assert(link
->manager
);
1547 assert(link
->manager
->rtnl
);
1549 if (!socket_ipv6_is_supported())
1552 log_link_debug(link
, "Setting address genmode for link");
1554 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1556 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1558 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1560 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1562 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1564 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1566 if (!link_ipv6ll_enabled(link
))
1567 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1568 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1569 /* The file may not exist. And event if it exists, when stable_secret is unset,
1570 * reading the file fails with EIO. */
1571 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1573 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1575 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1577 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1579 r
= sd_netlink_message_close_container(req
);
1581 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1583 r
= sd_netlink_message_close_container(req
);
1585 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1587 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1588 link_netlink_destroy_callback
, link
);
1590 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1597 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1602 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1605 r
= sd_netlink_message_get_errno(m
);
1607 /* we warn but don't fail the link, as it may be brought up later */
1608 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1613 static int link_up(Link
*link
) {
1614 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1618 assert(link
->network
);
1619 assert(link
->manager
);
1620 assert(link
->manager
->rtnl
);
1622 log_link_debug(link
, "Bringing link up");
1624 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1626 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1628 /* set it free if not enslaved with networkd */
1629 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1630 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1632 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1635 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1637 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1639 if (link
->network
->mac
) {
1640 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1642 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1645 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1646 link_netlink_destroy_callback
, link
);
1648 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1655 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1660 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1663 r
= sd_netlink_message_get_errno(m
);
1665 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1670 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1671 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1675 assert(link
->manager
);
1676 assert(link
->manager
->rtnl
);
1678 log_link_debug(link
, "Bringing link down");
1680 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1681 RTM_SETLINK
, link
->ifindex
);
1683 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1685 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1687 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1689 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1690 callback
?: link_down_handler
,
1691 link_netlink_destroy_callback
, link
);
1693 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1700 static int link_handle_bound_to_list(Link
*link
) {
1704 bool required_up
= false;
1705 bool link_is_up
= false;
1709 if (hashmap_isempty(link
->bound_to_links
))
1712 if (link
->flags
& IFF_UP
)
1715 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1716 if (link_has_carrier(l
)) {
1721 if (!required_up
&& link_is_up
) {
1722 r
= link_down(link
, NULL
);
1725 } else if (required_up
&& !link_is_up
) {
1734 static int link_handle_bound_by_list(Link
*link
) {
1741 if (hashmap_isempty(link
->bound_by_links
))
1744 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1745 r
= link_handle_bound_to_list(l
);
1753 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1759 if (link
== carrier
)
1762 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1765 r
= hashmap_ensure_allocated(h
, NULL
);
1769 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1776 static int link_new_bound_by_list(Link
*link
) {
1781 bool list_updated
= false;
1784 assert(link
->manager
);
1788 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1789 if (!carrier
->network
)
1792 if (strv_isempty(carrier
->network
->bind_carrier
))
1795 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1796 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1800 list_updated
= true;
1807 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1808 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1812 link_dirty(carrier
);
1818 static int link_new_bound_to_list(Link
*link
) {
1823 bool list_updated
= false;
1826 assert(link
->manager
);
1831 if (strv_isempty(link
->network
->bind_carrier
))
1836 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1837 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1838 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1842 list_updated
= true;
1849 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1850 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1854 link_dirty(carrier
);
1860 static int link_new_carrier_maps(Link
*link
) {
1863 r
= link_new_bound_by_list(link
);
1867 r
= link_handle_bound_by_list(link
);
1871 r
= link_new_bound_to_list(link
);
1875 r
= link_handle_bound_to_list(link
);
1882 static void link_free_bound_to_list(Link
*link
) {
1886 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1887 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1889 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1890 link_dirty(bound_to
);
1896 static void link_free_bound_by_list(Link
*link
) {
1900 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1901 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1903 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1904 link_dirty(bound_by
);
1905 link_handle_bound_to_list(bound_by
);
1912 static void link_free_carrier_maps(Link
*link
) {
1913 bool list_updated
= false;
1917 if (!hashmap_isempty(link
->bound_to_links
)) {
1918 link_free_bound_to_list(link
);
1919 list_updated
= true;
1922 if (!hashmap_isempty(link
->bound_by_links
)) {
1923 link_free_bound_by_list(link
);
1924 list_updated
= true;
1933 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1940 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1944 r
= set_ensure_allocated(&master
->slaves
, NULL
);
1948 r
= set_put(master
->slaves
, link
);
1958 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1963 if (!link
->manager
|| !netdev
)
1966 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1969 link_unref(set_remove(master
->slaves
, link
));
1972 static void link_detach_from_manager(Link
*link
) {
1973 if (!link
|| !link
->manager
)
1976 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1979 /* The following must be called at last. */
1980 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1984 void link_drop(Link
*link
) {
1985 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1988 link_set_state(link
, LINK_STATE_LINGER
);
1990 link_free_carrier_maps(link
);
1992 if (link
->network
) {
1993 link_drop_from_master(link
, link
->network
->bridge
);
1994 link_drop_from_master(link
, link
->network
->bond
);
1997 log_link_debug(link
, "Link removed");
1999 (void) unlink(link
->state_file
);
2000 link_detach_from_manager(link
);
2003 static int link_joined(Link
*link
) {
2007 assert(link
->network
);
2009 if (!hashmap_isempty(link
->bound_to_links
)) {
2010 r
= link_handle_bound_to_list(link
);
2013 } else if (!(link
->flags
& IFF_UP
)) {
2016 link_enter_failed(link
);
2021 if (link
->network
->bridge
) {
2022 r
= link_set_bridge(link
);
2024 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2026 r
= link_append_to_master(link
, link
->network
->bridge
);
2028 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2031 if (link
->network
->bond
) {
2032 r
= link_set_bond(link
);
2034 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2036 r
= link_append_to_master(link
, link
->network
->bond
);
2038 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2041 if (link
->network
->use_br_vlan
&&
2042 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2043 r
= link_set_bridge_vlan(link
);
2045 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2048 /* Skip setting up addresses until it gets carrier,
2049 or it would try to set addresses twice,
2050 which is bad for non-idempotent steps. */
2051 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2054 link_set_state(link
, LINK_STATE_CONFIGURING
);
2055 return link_request_set_addresses(link
);
2058 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2062 assert(link
->network
);
2063 assert(link
->enslaving
> 0);
2067 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2070 r
= sd_netlink_message_get_errno(m
);
2071 if (r
< 0 && r
!= -EEXIST
) {
2072 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2073 link_enter_failed(link
);
2077 log_link_debug(link
, "Joined netdev");
2079 if (link
->enslaving
== 0) {
2080 r
= link_joined(link
);
2082 link_enter_failed(link
);
2088 static int link_enter_join_netdev(Link
*link
) {
2094 assert(link
->network
);
2095 assert(link
->state
== LINK_STATE_INITIALIZED
);
2097 link_set_state(link
, LINK_STATE_CONFIGURING
);
2100 link
->enslaving
= 0;
2102 if (link
->network
->bond
) {
2103 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2104 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2105 return link_joined(link
);
2107 log_struct(LOG_DEBUG
,
2108 LOG_LINK_INTERFACE(link
),
2109 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2110 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2114 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2116 log_struct_errno(LOG_WARNING
, r
,
2117 LOG_LINK_INTERFACE(link
),
2118 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2119 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2120 link_enter_failed(link
);
2125 if (link
->network
->bridge
) {
2126 log_struct(LOG_DEBUG
,
2127 LOG_LINK_INTERFACE(link
),
2128 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2129 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2133 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2135 log_struct_errno(LOG_WARNING
, r
,
2136 LOG_LINK_INTERFACE(link
),
2137 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2138 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2139 link_enter_failed(link
);
2144 if (link
->network
->vrf
) {
2145 log_struct(LOG_DEBUG
,
2146 LOG_LINK_INTERFACE(link
),
2147 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2148 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2152 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2154 log_struct_errno(LOG_WARNING
, r
,
2155 LOG_LINK_INTERFACE(link
),
2156 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2157 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2158 link_enter_failed(link
);
2163 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2165 if (netdev
->ifindex
> 0)
2166 /* Assume already enslaved. */
2169 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2172 log_struct(LOG_DEBUG
,
2173 LOG_LINK_INTERFACE(link
),
2174 LOG_NETDEV_INTERFACE(netdev
),
2175 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2179 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2181 log_struct_errno(LOG_WARNING
, r
,
2182 LOG_LINK_INTERFACE(link
),
2183 LOG_NETDEV_INTERFACE(netdev
),
2184 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2185 link_enter_failed(link
);
2190 if (link
->enslaving
== 0)
2191 return link_joined(link
);
2196 static int link_set_ipv4_forward(Link
*link
) {
2199 if (!link_ipv4_forward_enabled(link
))
2202 /* We propagate the forwarding flag from one interface to the
2203 * global setting one way. This means: as long as at least one
2204 * interface was configured at any time that had IP forwarding
2205 * enabled the setting will stay on for good. We do this
2206 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2207 * somewhat in sync (see below). */
2209 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2211 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2216 static int link_set_ipv6_forward(Link
*link
) {
2219 if (!link_ipv6_forward_enabled(link
))
2222 /* On Linux, the IPv6 stack does not know a per-interface
2223 * packet forwarding setting: either packet forwarding is on
2224 * for all, or off for all. We hence don't bother with a
2225 * per-interface setting, but simply propagate the interface
2226 * flag, if it is set, to the global flag, one-way. Note that
2227 * while IPv4 would allow a per-interface flag, we expose the
2228 * same behaviour there and also propagate the setting from
2229 * one to all, to keep things simple (see above). */
2231 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2233 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2238 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2239 IPv6PrivacyExtensions s
;
2242 s
= link_ipv6_privacy_extensions(link
);
2246 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2248 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2253 static int link_set_ipv6_accept_ra(Link
*link
) {
2256 /* Make this a NOP if IPv6 is not available */
2257 if (!socket_ipv6_is_supported())
2260 if (link
->flags
& IFF_LOOPBACK
)
2266 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2268 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2273 static int link_set_ipv6_dad_transmits(Link
*link
) {
2276 /* Make this a NOP if IPv6 is not available */
2277 if (!socket_ipv6_is_supported())
2280 if (link
->flags
& IFF_LOOPBACK
)
2286 if (link
->network
->ipv6_dad_transmits
< 0)
2289 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2291 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2296 static int link_set_ipv6_hop_limit(Link
*link
) {
2299 /* Make this a NOP if IPv6 is not available */
2300 if (!socket_ipv6_is_supported())
2303 if (link
->flags
& IFF_LOOPBACK
)
2309 if (link
->network
->ipv6_hop_limit
< 0)
2312 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2314 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2319 static int link_set_ipv6_mtu(Link
*link
) {
2322 /* Make this a NOP if IPv6 is not available */
2323 if (!socket_ipv6_is_supported())
2326 if (link
->flags
& IFF_LOOPBACK
)
2329 if (link
->network
->ipv6_mtu
== 0)
2332 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2334 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2339 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2340 Address
*net_address
;
2348 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2349 if (address_equal(net_address
, address
))
2355 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2356 Neighbor
*net_neighbor
;
2364 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2365 if (neighbor_equal(net_neighbor
, neighbor
))
2371 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2380 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2381 if (route_equal(net_route
, route
))
2387 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2394 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2397 /* Even when the address is leased from a DHCP server, networkd assign the address
2398 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2399 * corresponding routes with RTPROT_DHCP. */
2400 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2401 if (route
->protocol
!= RTPROT_DHCP
)
2404 if (address
->family
!= route
->family
)
2407 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2414 static int link_drop_foreign_config(Link
*link
) {
2421 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2422 /* we consider IPv6LL addresses to be managed by the kernel */
2423 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2426 if (link_address_is_dynamic(link
, address
)) {
2427 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2429 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2432 if (link_is_static_address_configured(link
, address
)) {
2433 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2435 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2437 r
= address_remove(address
, link
, NULL
);
2443 SET_FOREACH(neighbor
, link
->neighbors_foreign
, i
) {
2444 if (link_is_neighbor_configured(link
, neighbor
)) {
2445 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2449 r
= neighbor_remove(neighbor
, link
, NULL
);
2455 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2456 /* do not touch routes managed by the kernel */
2457 if (route
->protocol
== RTPROT_KERNEL
)
2460 /* do not touch multicast route added by kernel */
2461 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2462 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2463 if (route
->protocol
== RTPROT_BOOT
&&
2464 route
->family
== AF_INET6
&&
2465 route
->dst_prefixlen
== 8 &&
2466 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 }}} }))
2469 if (route
->protocol
== RTPROT_STATIC
&&
2470 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2473 if (route
->protocol
== RTPROT_DHCP
&&
2474 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2477 if (link_is_static_route_configured(link
, route
)) {
2478 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, NULL
);
2482 r
= route_remove(route
, link
, NULL
);
2491 static int link_drop_config(Link
*link
) {
2492 Address
*address
, *pool_address
;
2498 SET_FOREACH(address
, link
->addresses
, i
) {
2499 /* we consider IPv6LL addresses to be managed by the kernel */
2500 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2503 r
= address_remove(address
, link
, NULL
);
2507 /* If this address came from an address pool, clean up the pool */
2508 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2509 if (address_equal(address
, pool_address
)) {
2510 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2511 address_free(pool_address
);
2517 SET_FOREACH(neighbor
, link
->neighbors
, i
) {
2518 r
= neighbor_remove(neighbor
, link
, NULL
);
2523 SET_FOREACH(route
, link
->routes
, i
) {
2524 /* do not touch routes managed by the kernel */
2525 if (route
->protocol
== RTPROT_KERNEL
)
2528 r
= route_remove(route
, link
, NULL
);
2538 static int link_configure(Link
*link
) {
2542 assert(link
->network
);
2543 assert(link
->state
== LINK_STATE_INITIALIZED
);
2545 if (link
->iftype
== ARPHRD_CAN
)
2546 return link_configure_can(link
);
2548 /* Drop foreign config, but ignore loopback or critical devices.
2549 * We do not want to remove loopback address or addresses used for root NFS. */
2550 if (!(link
->flags
& IFF_LOOPBACK
) &&
2551 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2552 r
= link_drop_foreign_config(link
);
2557 r
= link_set_proxy_arp(link
);
2561 r
= ipv6_proxy_ndp_addresses_configure(link
);
2565 r
= link_set_ipv4_forward(link
);
2569 r
= link_set_ipv6_forward(link
);
2573 r
= link_set_ipv6_privacy_extensions(link
);
2577 r
= link_set_ipv6_accept_ra(link
);
2581 r
= link_set_ipv6_dad_transmits(link
);
2585 r
= link_set_ipv6_hop_limit(link
);
2589 r
= link_set_flags(link
);
2593 r
= link_set_ipv6_mtu(link
);
2597 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2598 r
= ipv4ll_configure(link
);
2603 if (link_dhcp4_enabled(link
)) {
2604 r
= dhcp4_set_promote_secondaries(link
);
2608 r
= dhcp4_configure(link
);
2613 if (link_dhcp4_server_enabled(link
)) {
2614 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2618 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2623 if (link_dhcp6_enabled(link
) ||
2624 link_ipv6_accept_ra_enabled(link
)) {
2625 r
= dhcp6_configure(link
);
2630 if (link_ipv6_accept_ra_enabled(link
)) {
2631 r
= ndisc_configure(link
);
2636 if (link_radv_enabled(link
)) {
2637 r
= radv_configure(link
);
2642 if (link_lldp_rx_enabled(link
)) {
2643 r
= link_lldp_rx_configure(link
);
2648 r
= link_configure_mtu(link
);
2652 r
= link_configure_addrgen_mode(link
);
2656 return link_configure_after_setting_mtu(link
);
2659 static int link_configure_after_setting_mtu(Link
*link
) {
2663 assert(link
->network
);
2664 assert(link
->state
== LINK_STATE_INITIALIZED
);
2666 if (link
->setting_mtu
)
2669 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2670 r
= link_acquire_conf(link
);
2675 return link_enter_join_netdev(link
);
2678 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2681 if (duid
->raw_data_len
> 0)
2684 if (duid
->type
!= DUID_TYPE_UUID
)
2687 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2688 duid
->raw_data_len
= sizeof(sd_id128_t
);
2693 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2694 Manager
*manager
= userdata
;
2695 const sd_bus_error
*e
;
2705 e
= sd_bus_message_get_error(m
);
2707 log_error_errno(sd_bus_error_get_errno(e
),
2708 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2713 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2717 if (sz
!= sizeof(sd_id128_t
)) {
2718 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2722 memcpy(&manager
->product_uuid
, a
, sz
);
2723 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2724 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2726 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2729 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2732 r
= link_configure(link
);
2734 link_enter_failed(link
);
2737 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2739 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2740 * even if the method fails. */
2741 manager
->has_product_uuid
= true;
2746 static bool link_requires_uuid(Link
*link
) {
2750 assert(link
->manager
);
2751 assert(link
->network
);
2753 duid
= link_get_duid(link
);
2754 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2757 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2760 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2766 static int link_configure_duid(Link
*link
) {
2772 assert(link
->manager
);
2773 assert(link
->network
);
2776 duid
= link_get_duid(link
);
2778 if (!link_requires_uuid(link
))
2781 if (m
->has_product_uuid
) {
2782 (void) duid_set_uuid(duid
, m
->product_uuid
);
2786 if (!m
->links_requesting_uuid
) {
2787 r
= manager_request_product_uuid(m
, link
);
2792 log_link_warning_errno(link
, r
,
2793 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2797 r
= set_put(m
->links_requesting_uuid
, link
);
2801 r
= set_put(m
->duids_requesting_uuid
, duid
);
2811 static int link_initialized_and_synced(Link
*link
) {
2816 assert(link
->ifname
);
2817 assert(link
->manager
);
2819 /* We may get called either from the asynchronous netlink callback,
2820 * or directly for link_add() if running in a container. See link_add(). */
2821 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2824 log_link_debug(link
, "Link state is up-to-date");
2825 link_set_state(link
, LINK_STATE_INITIALIZED
);
2827 r
= link_new_bound_by_list(link
);
2831 r
= link_handle_bound_by_list(link
);
2835 if (!link
->network
) {
2836 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2837 &link
->mac
, &network
);
2839 link_enter_unmanaged(link
);
2841 } else if (r
== 0 && network
->unmanaged
) {
2842 link_enter_unmanaged(link
);
2847 if (link
->flags
& IFF_LOOPBACK
) {
2848 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2849 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2851 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2852 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2854 if (network
->dhcp_server
)
2855 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2858 r
= network_apply(network
, link
);
2863 r
= link_new_bound_to_list(link
);
2867 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2868 * link_configure() is called later asynchronously. */
2869 r
= link_configure_duid(link
);
2873 r
= link_configure(link
);
2880 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2883 r
= link_initialized_and_synced(link
);
2885 link_enter_failed(link
);
2889 int link_initialized(Link
*link
, sd_device
*device
) {
2890 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2894 assert(link
->manager
);
2895 assert(link
->manager
->rtnl
);
2898 if (link
->state
!= LINK_STATE_PENDING
)
2901 if (link
->sd_device
)
2904 log_link_debug(link
, "udev initialized link");
2905 link_set_state(link
, LINK_STATE_INITIALIZED
);
2907 link
->sd_device
= sd_device_ref(device
);
2909 /* udev has initialized the link, but we don't know if we have yet
2910 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2911 * when it returns we know that the pending NEWLINKs have already been
2912 * processed and that we are up-to-date */
2914 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2919 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2920 link_netlink_destroy_callback
, link
);
2929 static int link_load(Link
*link
) {
2930 _cleanup_free_
char *network_file
= NULL
,
2933 *dhcp4_address
= NULL
,
2934 *ipv4ll_address
= NULL
;
2935 union in_addr_union address
;
2936 union in_addr_union route_dst
;
2942 r
= parse_env_file(NULL
, link
->state_file
,
2943 "NETWORK_FILE", &network_file
,
2944 "ADDRESSES", &addresses
,
2946 "DHCP4_ADDRESS", &dhcp4_address
,
2947 "IPV4LL_ADDRESS", &ipv4ll_address
);
2948 if (r
< 0 && r
!= -ENOENT
)
2949 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2956 suffix
= strrchr(network_file
, '.');
2958 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2959 goto network_file_fail
;
2963 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2965 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2966 goto network_file_fail
;
2969 r
= network_apply(network
, link
);
2971 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2980 _cleanup_free_
char *address_str
= NULL
;
2981 char *prefixlen_str
;
2983 unsigned char prefixlen
;
2985 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2987 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2993 prefixlen_str
= strchr(address_str
, '/');
2994 if (!prefixlen_str
) {
2995 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2999 *prefixlen_str
++ = '\0';
3001 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3003 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3007 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3009 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3013 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3015 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3024 _cleanup_free_
char *route_str
= NULL
;
3025 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3027 char *prefixlen_str
;
3029 unsigned char prefixlen
, tos
, table
;
3032 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3034 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3040 prefixlen_str
= strchr(route_str
, '/');
3041 if (!prefixlen_str
) {
3042 log_link_debug(link
, "Failed to parse route %s", route_str
);
3046 *prefixlen_str
++ = '\0';
3048 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3050 log_link_debug(link
,
3051 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3056 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3058 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3062 r
= route_add(link
, family
, &route_dst
, prefixlen
, NULL
, tos
, priority
, table
, &route
);
3064 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3066 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3067 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3068 0, route_expire_handler
, route
);
3070 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3073 route
->lifetime
= lifetime
;
3074 sd_event_source_unref(route
->expire
);
3075 route
->expire
= TAKE_PTR(expire
);
3079 if (dhcp4_address
) {
3080 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3082 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3083 goto dhcp4_address_fail
;
3086 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3088 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3090 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3092 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3097 if (ipv4ll_address
) {
3098 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3100 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3101 goto ipv4ll_address_fail
;
3104 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3106 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3108 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3110 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3113 ipv4ll_address_fail
:
3118 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3119 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3120 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3129 r
= link_new(m
, message
, ret
);
3135 log_link_debug(link
, "Link %d added", link
->ifindex
);
3137 r
= link_load(link
);
3141 if (detect_container() <= 0) {
3142 /* not in a container, udev will be around */
3143 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3144 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3146 log_link_warning_errno(link
, r
, "Could not find device: %m");
3150 r
= sd_device_get_is_initialized(device
);
3152 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3157 log_link_debug(link
, "link pending udev initialization...");
3161 r
= device_is_renaming(device
);
3163 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3167 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3171 r
= link_initialized(link
, device
);
3175 r
= link_initialized_and_synced(link
);
3182 link_enter_failed(link
);
3186 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3191 log_link_info(link
, "Gained IPv6LL");
3193 link
->ipv6ll_address
= *address
;
3194 link_check_ready(link
);
3196 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3197 r
= link_acquire_ipv6_conf(link
);
3199 link_enter_failed(link
);
3207 static int link_carrier_gained(Link
*link
) {
3212 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3213 r
= link_acquire_conf(link
);
3215 link_enter_failed(link
);
3219 link_set_state(link
, LINK_STATE_CONFIGURING
);
3220 r
= link_request_set_addresses(link
);
3225 r
= link_handle_bound_by_list(link
);
3232 static int link_carrier_lost(Link
*link
) {
3237 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3240 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3241 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3242 * configuration and stop the clients as well. */
3243 if (link
->setting_mtu
)
3246 r
= link_stop_clients(link
, false);
3248 link_enter_failed(link
);
3252 if (link_dhcp4_server_enabled(link
))
3253 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3255 r
= link_drop_config(link
);
3259 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3260 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3261 r
= link_drop_foreign_config(link
);
3266 r
= link_handle_bound_by_list(link
);
3273 int link_carrier_reset(Link
*link
) {
3278 if (link_has_carrier(link
)) {
3279 r
= link_carrier_lost(link
);
3283 r
= link_carrier_gained(link
);
3287 log_link_info(link
, "Reset carrier");
3293 int link_update(Link
*link
, sd_netlink_message
*m
) {
3294 struct ether_addr mac
;
3297 bool had_carrier
, carrier_gained
, carrier_lost
;
3301 assert(link
->ifname
);
3304 if (link
->state
== LINK_STATE_LINGER
) {
3305 log_link_info(link
, "Link re-added");
3306 link_set_state(link
, LINK_STATE_CONFIGURING
);
3308 r
= link_new_carrier_maps(link
);
3313 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3314 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3315 Manager
*manager
= link
->manager
;
3317 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3320 r
= link_add(manager
, m
, &link
);
3325 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3326 if (r
>= 0 && mtu
> 0) {
3328 if (link
->original_mtu
== 0) {
3329 link
->original_mtu
= mtu
;
3330 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3333 if (link
->dhcp_client
) {
3334 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3337 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3341 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3343 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3347 /* The kernel may broadcast NEWLINK messages without the MAC address
3348 set, simply ignore them. */
3349 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3351 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3354 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3357 log_link_debug(link
, "MAC address: "
3358 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3359 mac
.ether_addr_octet
[0],
3360 mac
.ether_addr_octet
[1],
3361 mac
.ether_addr_octet
[2],
3362 mac
.ether_addr_octet
[3],
3363 mac
.ether_addr_octet
[4],
3364 mac
.ether_addr_octet
[5]);
3367 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3369 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3372 if (link
->dhcp_client
) {
3373 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3374 (const uint8_t *) &link
->mac
,
3378 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3380 r
= dhcp4_set_client_identifier(link
);
3385 if (link
->dhcp6_client
) {
3386 const DUID
* duid
= link_get_duid(link
);
3388 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3389 (const uint8_t *) &link
->mac
,
3393 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3395 if (link
->network
->iaid_set
) {
3396 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3397 link
->network
->iaid
);
3399 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3402 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3404 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3405 duid
->raw_data_len
);
3407 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3411 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3413 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3417 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3419 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3424 old_master
= link
->master_ifindex
;
3425 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3427 had_carrier
= link_has_carrier(link
);
3429 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3433 r
= link_update_lldp(link
);
3437 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3438 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3440 if (carrier_gained
) {
3441 log_link_info(link
, "Gained carrier");
3443 r
= link_carrier_gained(link
);
3446 } else if (carrier_lost
) {
3447 log_link_info(link
, "Lost carrier");
3449 r
= link_carrier_lost(link
);
3457 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3465 if (hashmap_isempty(h
))
3469 HASHMAP_FOREACH(link
, h
, i
) {
3473 fprintf(f
, "%i", link
->ifindex
);
3480 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
3484 for (j
= 0; j
< n_dns
; j
++) {
3485 _cleanup_free_
char *b
= NULL
;
3487 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
3489 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3500 int link_save(Link
*link
) {
3501 _cleanup_free_
char *temp_path
= NULL
;
3502 _cleanup_fclose_
FILE *f
= NULL
;
3503 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
3510 assert(link
->state_file
);
3511 assert(link
->lease_file
);
3512 assert(link
->manager
);
3514 if (link
->state
== LINK_STATE_LINGER
) {
3515 (void) unlink(link
->state_file
);
3519 link_lldp_save(link
);
3521 admin_state
= link_state_to_string(link
->state
);
3522 assert(admin_state
);
3524 oper_state
= link_operstate_to_string(link
->operstate
);
3527 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
3528 assert(carrier_state
);
3530 address_state
= link_address_state_to_string(link
->address_state
);
3531 assert(address_state
);
3533 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3537 (void) fchmod(fileno(f
), 0644);
3540 "# This is private data. Do not parse.\n"
3543 "CARRIER_STATE=%s\n"
3544 "ADDRESS_STATE=%s\n",
3545 admin_state
, oper_state
, carrier_state
, address_state
);
3547 if (link
->network
) {
3548 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3549 const char *dhcp_domainname
= NULL
, *p
;
3550 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3553 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3554 yes_no(link
->network
->required_for_online
));
3556 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3557 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3559 if (link
->dhcp6_client
) {
3560 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3561 if (r
< 0 && r
!= -ENOMSG
)
3562 log_link_debug(link
, "No DHCPv6 lease");
3565 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3570 if (link
->n_dns
!= (unsigned) -1)
3571 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
3573 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
3575 if (link
->network
->dhcp_use_dns
&&
3577 const struct in_addr
*addresses
;
3579 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3581 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3585 if (link
->network
->dhcp6_use_dns
&& dhcp6_lease
) {
3586 struct in6_addr
*in6_addrs
;
3588 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3592 serialize_in6_addrs(f
, in6_addrs
, r
);
3597 /* Make sure to flush out old entries before we use the NDISC data */
3600 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3603 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3607 serialize_in6_addrs(f
, &dd
->address
, 1);
3616 fputstrv(f
, link
->ntp
?: link
->network
->ntp
, NULL
, &space
);
3618 if (link
->network
->dhcp_use_ntp
&&
3620 const struct in_addr
*addresses
;
3622 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3624 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3628 if (link
->network
->dhcp6_use_ntp
&& dhcp6_lease
) {
3629 struct in6_addr
*in6_addrs
;
3632 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3637 serialize_in6_addrs(f
, in6_addrs
, r
);
3641 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3643 fputstrv(f
, hosts
, NULL
, &space
);
3648 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3649 if (link
->dhcp_lease
) {
3650 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3651 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3654 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3657 fputs("DOMAINS=", f
);
3659 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
3660 fputs_with_space(f
, p
, NULL
, &space
);
3662 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3663 if (dhcp_domainname
)
3664 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3666 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3668 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3671 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
3674 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3675 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3680 fputs("ROUTE_DOMAINS=", f
);
3682 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
3683 fputs_with_space(f
, p
, NULL
, &space
);
3685 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3686 if (dhcp_domainname
)
3687 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3689 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3691 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3694 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3697 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3698 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3703 fprintf(f
, "LLMNR=%s\n",
3704 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
3705 fprintf(f
, "MDNS=%s\n",
3706 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
3707 if (link
->dns_default_route
>= 0)
3708 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->dns_default_route
));
3709 else if (link
->network
->dns_default_route
>= 0)
3710 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3712 if (link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3713 fprintf(f
, "DNS_OVER_TLS=%s\n",
3714 dns_over_tls_mode_to_string(link
->dns_over_tls_mode
));
3715 else if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3716 fprintf(f
, "DNS_OVER_TLS=%s\n",
3717 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3719 if (link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3720 fprintf(f
, "DNSSEC=%s\n",
3721 dnssec_mode_to_string(link
->dnssec_mode
));
3722 else if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3723 fprintf(f
, "DNSSEC=%s\n",
3724 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3726 if (!set_isempty(link
->dnssec_negative_trust_anchors
)) {
3729 fputs("DNSSEC_NTA=", f
);
3731 SET_FOREACH(n
, link
->dnssec_negative_trust_anchors
, i
)
3732 fputs_with_space(f
, n
, NULL
, &space
);
3734 } else if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3737 fputs("DNSSEC_NTA=", f
);
3739 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3740 fputs_with_space(f
, n
, NULL
, &space
);
3744 fputs("ADDRESSES=", f
);
3746 SET_FOREACH(a
, link
->addresses
, i
) {
3747 _cleanup_free_
char *address_str
= NULL
;
3749 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3753 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3758 fputs("ROUTES=", f
);
3760 SET_FOREACH(route
, link
->routes
, i
) {
3761 _cleanup_free_
char *route_str
= NULL
;
3763 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3767 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3768 space
? " " : "", route_str
,
3769 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3776 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3777 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3779 if (link
->dhcp_lease
) {
3780 struct in_addr address
;
3781 const char *tz
= NULL
;
3783 assert(link
->network
);
3785 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3787 fprintf(f
, "TIMEZONE=%s\n", tz
);
3789 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3791 fputs("DHCP4_ADDRESS=", f
);
3792 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3796 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3804 (void) unlink(link
->lease_file
);
3807 struct in_addr address
;
3809 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3811 fputs("IPV4LL_ADDRESS=", f
);
3812 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3817 r
= fflush_and_check(f
);
3821 if (rename(temp_path
, link
->state_file
) < 0) {
3829 (void) unlink(link
->state_file
);
3831 (void) unlink(temp_path
);
3833 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3836 /* The serialized state in /run is no longer up-to-date. */
3837 void link_dirty(Link
*link
) {
3842 /* mark manager dirty as link is dirty */
3843 manager_dirty(link
->manager
);
3845 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3847 /* allocation errors are ignored */
3850 r
= set_put(link
->manager
->dirty_links
, link
);
3852 /* don't take another ref if the link was already dirty */
3858 /* The serialized state in /run is up-to-date */
3859 void link_clean(Link
*link
) {
3861 assert(link
->manager
);
3863 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3866 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3867 [LINK_STATE_PENDING
] = "pending",
3868 [LINK_STATE_INITIALIZED
] = "initialized",
3869 [LINK_STATE_CONFIGURING
] = "configuring",
3870 [LINK_STATE_CONFIGURED
] = "configured",
3871 [LINK_STATE_UNMANAGED
] = "unmanaged",
3872 [LINK_STATE_FAILED
] = "failed",
3873 [LINK_STATE_LINGER
] = "linger",
3876 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);