1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
7 #include "alloc-util.h"
9 #include "dhcp-identifier.h"
10 #include "dhcp-lease-internal.h"
14 #include "missing_network.h"
15 #include "netdev/bond.h"
16 #include "netdev/bridge.h"
17 #include "netdev/ipvlan.h"
18 #include "netdev/vrf.h"
19 #include "netlink-util.h"
20 #include "network-internal.h"
21 #include "networkd-can.h"
22 #include "networkd-ipv6-proxy-ndp.h"
23 #include "networkd-lldp-tx.h"
24 #include "networkd-manager.h"
25 #include "networkd-ndisc.h"
26 #include "networkd-neighbor.h"
27 #include "networkd-radv.h"
28 #include "networkd-routing-policy-rule.h"
30 #include "socket-util.h"
31 #include "stdio-util.h"
32 #include "string-table.h"
34 #include "sysctl-util.h"
35 #include "tmpfile-util.h"
36 #include "udev-util.h"
40 uint32_t link_get_vrf_table(Link
*link
) {
41 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
44 uint32_t link_get_dhcp_route_table(Link
*link
) {
45 /* When the interface is part of an VRF use the VRFs routing table, unless
46 * another table is explicitly specified. */
47 if (link
->network
->dhcp_route_table_set
)
48 return link
->network
->dhcp_route_table
;
49 return link_get_vrf_table(link
);
52 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
53 if (link
->network
->ipv6_accept_ra_route_table_set
)
54 return link
->network
->ipv6_accept_ra_route_table
;
55 return link_get_vrf_table(link
);
58 DUID
* link_get_duid(Link
*link
) {
59 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
60 return &link
->network
->duid
;
62 return &link
->manager
->duid
;
65 static bool link_dhcp6_enabled(Link
*link
) {
68 if (!socket_ipv6_is_supported())
71 if (link
->flags
& IFF_LOOPBACK
)
77 if (link
->network
->bond
)
80 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
83 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
86 static bool link_dhcp4_enabled(Link
*link
) {
89 if (link
->flags
& IFF_LOOPBACK
)
95 if (link
->network
->bond
)
98 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
101 static bool link_dhcp4_server_enabled(Link
*link
) {
104 if (link
->flags
& IFF_LOOPBACK
)
110 if (link
->network
->bond
)
113 return link
->network
->dhcp_server
;
116 bool link_ipv4ll_enabled(Link
*link
) {
119 if (link
->flags
& IFF_LOOPBACK
)
125 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6"))
128 /* L3 or L3S mode do not support ARP. */
129 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
132 if (link
->network
->bond
)
135 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
138 bool link_ipv4ll_fallback_enabled(Link
*link
) {
141 if (link
->flags
& IFF_LOOPBACK
)
147 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6"))
150 /* L3 or L3S mode do not support ARP. */
151 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
154 if (link
->network
->bond
)
157 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
160 static bool link_ipv6ll_enabled(Link
*link
) {
163 if (!socket_ipv6_is_supported())
166 if (link
->flags
& IFF_LOOPBACK
)
172 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti"))
175 if (link
->network
->bond
)
178 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
181 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
184 static bool link_ipv6_enabled(Link
*link
) {
187 if (!socket_ipv6_is_supported())
190 if (link
->network
->bond
)
193 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
196 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
197 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
200 static bool link_radv_enabled(Link
*link
) {
203 if (!link_ipv6ll_enabled(link
))
206 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
209 static bool link_ipv4_forward_enabled(Link
*link
) {
212 if (link
->flags
& IFF_LOOPBACK
)
218 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
221 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
224 static bool link_ipv6_forward_enabled(Link
*link
) {
227 if (!socket_ipv6_is_supported())
230 if (link
->flags
& IFF_LOOPBACK
)
236 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
239 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
242 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
245 static bool link_proxy_arp_enabled(Link
*link
) {
248 if (link
->flags
& IFF_LOOPBACK
)
254 if (link
->network
->proxy_arp
< 0)
260 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
263 if (!socket_ipv6_is_supported())
266 if (link
->flags
& IFF_LOOPBACK
)
272 if (!link_ipv6ll_enabled(link
))
275 /* If unset use system default (enabled if local forwarding is disabled.
276 * disabled if local forwarding is enabled).
277 * If set, ignore or enforce RA independent of local forwarding state.
279 if (link
->network
->ipv6_accept_ra
< 0)
280 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
281 return !link_ipv6_forward_enabled(link
);
282 else if (link
->network
->ipv6_accept_ra
> 0)
283 /* accept RA even if ip_forward is enabled */
290 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
293 if (!socket_ipv6_is_supported())
294 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
296 if (link
->flags
& IFF_LOOPBACK
)
297 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
300 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
302 return link
->network
->ipv6_privacy_extensions
;
305 static int link_enable_ipv6(Link
*link
) {
309 if (link
->flags
& IFF_LOOPBACK
)
312 disabled
= !link_ipv6_enabled(link
);
314 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
316 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
318 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
323 static bool link_is_enslaved(Link
*link
) {
324 if (link
->flags
& IFF_SLAVE
)
325 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
328 if (!link
->enslaved_raw
)
334 if (link
->network
->bridge
)
335 /* TODO: support the case when link is not managed by networkd. */
341 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
347 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
350 link_update_operstate(master
, true);
353 void link_update_operstate(Link
*link
, bool also_update_master
) {
354 LinkOperationalState operstate
;
359 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
360 operstate
= LINK_OPERSTATE_DORMANT
;
361 else if (link_has_carrier(link
)) {
363 uint8_t scope
= RT_SCOPE_NOWHERE
;
365 /* if we have carrier, check what addresses we have */
366 SET_FOREACH(address
, link
->addresses
, i
) {
367 if (!address_is_ready(address
))
370 if (address
->scope
< scope
)
371 scope
= address
->scope
;
374 /* for operstate we also take foreign addresses into account */
375 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
376 if (!address_is_ready(address
))
379 if (address
->scope
< scope
)
380 scope
= address
->scope
;
383 if (scope
< RT_SCOPE_SITE
)
384 /* universally accessible addresses found */
385 operstate
= LINK_OPERSTATE_ROUTABLE
;
386 else if (scope
< RT_SCOPE_HOST
)
387 /* only link or site local addresses found */
388 operstate
= LINK_OPERSTATE_DEGRADED
;
390 /* no useful addresses found */
391 operstate
= LINK_OPERSTATE_CARRIER
;
392 } else if (link
->flags
& IFF_UP
)
393 operstate
= LINK_OPERSTATE_NO_CARRIER
;
395 operstate
= LINK_OPERSTATE_OFF
;
397 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
398 link_is_enslaved(link
))
399 operstate
= LINK_OPERSTATE_ENSLAVED
;
401 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
404 SET_FOREACH(slave
, link
->slaves
, i
) {
405 link_update_operstate(slave
, false);
407 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
408 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
412 if (link
->operstate
!= operstate
) {
413 link
->operstate
= operstate
;
414 link_send_changed(link
, "OperationalState", NULL
);
418 if (also_update_master
&& link
->network
) {
419 link_update_master_operstate(link
, link
->network
->bond
);
420 link_update_master_operstate(link
, link
->network
->bridge
);
424 #define FLAG_STRING(string, flag, old, new) \
425 (((old ^ new) & flag) \
426 ? ((old & flag) ? (" -" string) : (" +" string)) \
429 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
430 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
436 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
438 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
440 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
442 /* if we got a message without operstate, take it to mean
443 the state was unchanged */
444 operstate
= link
->kernel_operstate
;
446 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
449 if (link
->flags
!= flags
) {
450 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",
451 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
452 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
453 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
454 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
455 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
456 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
457 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
458 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
459 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
460 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
461 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
462 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
463 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
464 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
465 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
466 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
467 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
468 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
469 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
471 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
472 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
473 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
474 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
475 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
476 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
477 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
478 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
480 /* link flags are currently at most 18 bits, let's align to
482 if (unknown_flags_added
)
484 "Unknown link flags gained: %#.5x (ignoring)",
485 unknown_flags_added
);
487 if (unknown_flags_removed
)
489 "Unknown link flags lost: %#.5x (ignoring)",
490 unknown_flags_removed
);
494 link
->kernel_operstate
= operstate
;
496 link_update_operstate(link
, true);
501 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
502 _cleanup_(link_unrefp
) Link
*link
= NULL
;
504 const char *ifname
, *kind
= NULL
;
506 unsigned short iftype
;
512 /* check for link kind */
513 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
515 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
516 r
= sd_netlink_message_exit_container(message
);
521 r
= sd_netlink_message_get_type(message
, &type
);
524 else if (type
!= RTM_NEWLINK
)
527 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
530 else if (ifindex
<= 0)
533 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
537 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
548 .state
= LINK_STATE_PENDING
,
549 .rtnl_extended_attrs
= true,
554 link
->ifname
= strdup(ifname
);
559 link
->kind
= strdup(kind
);
564 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
566 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
568 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
570 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
572 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
575 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
578 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
581 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
585 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
589 r
= link_update_flags(link
, message
);
593 *ret
= TAKE_PTR(link
);
598 static Link
*link_free(Link
*link
) {
603 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
604 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
606 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
607 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
609 while ((address
= link
->pool_addresses
)) {
610 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
611 address_free(address
);
614 sd_dhcp_server_unref(link
->dhcp_server
);
615 sd_dhcp_client_unref(link
->dhcp_client
);
616 sd_dhcp_lease_unref(link
->dhcp_lease
);
618 link_lldp_emit_stop(link
);
620 free(link
->lease_file
);
622 sd_lldp_unref(link
->lldp
);
623 free(link
->lldp_file
);
627 sd_ipv4ll_unref(link
->ipv4ll
);
628 sd_dhcp6_client_unref(link
->dhcp6_client
);
629 sd_ndisc_unref(link
->ndisc
);
630 sd_radv_unref(link
->radv
);
635 (void) unlink(link
->state_file
);
636 free(link
->state_file
);
638 sd_device_unref(link
->sd_device
);
640 hashmap_free(link
->bound_to_links
);
641 hashmap_free(link
->bound_by_links
);
643 set_free_with_destructor(link
->slaves
, link_unref
);
645 network_unref(link
->network
);
650 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
652 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
659 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
668 static void link_set_state(Link
*link
, LinkState state
) {
671 if (link
->state
== state
)
674 log_link_debug(link
, "State changed: %s -> %s",
675 link_state_to_string(link
->state
),
676 link_state_to_string(state
));
680 link_send_changed(link
, "AdministrativeState", NULL
);
683 static void link_enter_unmanaged(Link
*link
) {
686 log_link_debug(link
, "Unmanaged");
688 link_set_state(link
, LINK_STATE_UNMANAGED
);
693 int link_stop_clients(Link
*link
) {
697 assert(link
->manager
);
698 assert(link
->manager
->event
);
700 if (link
->dhcp_client
) {
701 k
= sd_dhcp_client_stop(link
->dhcp_client
);
703 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
707 k
= sd_ipv4ll_stop(link
->ipv4ll
);
709 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
712 if (link
->dhcp6_client
) {
713 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
715 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
719 k
= sd_ndisc_stop(link
->ndisc
);
721 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
725 k
= sd_radv_stop(link
->radv
);
727 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
730 link_lldp_emit_stop(link
);
734 void link_enter_failed(Link
*link
) {
737 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
740 log_link_warning(link
, "Failed");
742 link_set_state(link
, LINK_STATE_FAILED
);
744 link_stop_clients(link
);
749 static Address
* link_find_dhcp_server_address(Link
*link
) {
753 assert(link
->network
);
755 /* The first statically configured address if there is any */
756 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
758 if (address
->family
!= AF_INET
)
761 if (in_addr_is_null(address
->family
, &address
->in_addr
))
767 /* If that didn't work, find a suitable address we got from the pool */
768 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
769 if (address
->family
!= AF_INET
)
778 static int link_join_netdevs_after_configured(Link
*link
) {
783 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
784 if (netdev
->ifindex
> 0)
785 /* Assume already enslaved. */
788 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
791 log_struct(LOG_DEBUG
,
792 LOG_LINK_INTERFACE(link
),
793 LOG_NETDEV_INTERFACE(netdev
),
794 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
796 r
= netdev_join(netdev
, link
, NULL
);
798 return log_struct_errno(LOG_WARNING
, r
,
799 LOG_LINK_INTERFACE(link
),
800 LOG_NETDEV_INTERFACE(netdev
),
801 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
807 static void link_enter_configured(Link
*link
) {
809 assert(link
->network
);
811 if (link
->state
!= LINK_STATE_CONFIGURING
)
814 log_link_info(link
, "Configured");
816 link_set_state(link
, LINK_STATE_CONFIGURED
);
818 (void) link_join_netdevs_after_configured(link
);
823 static int link_request_set_routing_policy_rule(Link
*link
) {
824 RoutingPolicyRule
*rule
, *rrule
= NULL
;
828 assert(link
->network
);
830 link_set_state(link
, LINK_STATE_CONFIGURING
);
831 link
->routing_policy_rules_configured
= false;
833 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
834 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
835 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
836 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
838 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
842 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
844 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
845 link_enter_failed(link
);
849 link
->routing_policy_rule_messages
++;
852 routing_policy_rule_purge(link
->manager
, link
);
853 if (link
->routing_policy_rule_messages
== 0) {
854 link
->routing_policy_rules_configured
= true;
855 link_check_ready(link
);
857 log_link_debug(link
, "Setting routing policy rules");
862 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
866 assert(link
->route_messages
> 0);
867 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
868 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
870 link
->route_messages
--;
872 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
875 r
= sd_netlink_message_get_errno(m
);
876 if (r
< 0 && r
!= -EEXIST
)
877 log_link_warning_errno(link
, r
, "Could not set route: %m");
879 if (link
->route_messages
== 0) {
880 log_link_debug(link
, "Routes set");
881 link
->static_routes_configured
= true;
882 link_check_ready(link
);
888 int link_request_set_routes(Link
*link
) {
890 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
891 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
898 assert(link
->network
);
899 assert(link
->addresses_configured
);
900 assert(link
->address_messages
== 0);
901 assert(link
->state
!= _LINK_STATE_INVALID
);
903 link_set_state(link
, LINK_STATE_CONFIGURING
);
904 link
->static_routes_configured
= false;
906 r
= link_request_set_routing_policy_rule(link
);
910 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
911 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
912 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
914 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
917 r
= route_configure(rt
, link
, route_handler
);
919 log_link_warning_errno(link
, r
, "Could not set routes: %m");
920 link_enter_failed(link
);
924 link
->route_messages
++;
927 if (link
->route_messages
== 0) {
928 link
->static_routes_configured
= true;
929 link_check_ready(link
);
931 log_link_debug(link
, "Setting routes");
936 void link_check_ready(Link
*link
) {
942 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
948 if (!link
->addresses_configured
)
951 if (!link
->neighbors_configured
)
954 SET_FOREACH(a
, link
->addresses
, i
)
955 if (!address_is_ready(a
))
958 if (!link
->addresses_ready
) {
959 link
->addresses_ready
= true;
960 link_request_set_routes(link
);
964 if (!link
->static_routes_configured
)
967 if (!link
->routing_policy_rules_configured
)
970 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
973 if (link_ipv6ll_enabled(link
) &&
974 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
977 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
978 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
979 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
980 /* When DHCP is enabled, at least one protocol must provide an address, or
981 * an IPv4ll fallback address must be configured. */
984 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
987 if (link
->state
!= LINK_STATE_CONFIGURED
)
988 link_enter_configured(link
);
993 static int link_request_set_neighbors(Link
*link
) {
998 assert(link
->network
);
999 assert(link
->state
!= _LINK_STATE_INVALID
);
1001 link_set_state(link
, LINK_STATE_CONFIGURING
);
1002 link
->neighbors_configured
= false;
1004 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1005 r
= neighbor_configure(neighbor
, link
, NULL
);
1007 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1008 link_enter_failed(link
);
1013 if (link
->neighbor_messages
== 0) {
1014 link
->neighbors_configured
= true;
1015 link_check_ready(link
);
1017 log_link_debug(link
, "Setting neighbors");
1022 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1028 assert(link
->ifname
);
1029 assert(link
->address_messages
> 0);
1030 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1031 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1033 link
->address_messages
--;
1035 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1038 r
= sd_netlink_message_get_errno(m
);
1039 if (r
< 0 && r
!= -EEXIST
)
1040 log_link_warning_errno(link
, r
, "could not set address: %m");
1042 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1044 if (link
->address_messages
== 0) {
1045 log_link_debug(link
, "Addresses set");
1046 link
->addresses_configured
= true;
1047 link_check_ready(link
);
1053 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1054 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1055 size_t n_addresses
= 0, n_allocated
= 0;
1058 log_debug("Copying DNS server information from %s", link
->ifname
);
1063 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1066 /* Only look for IPv4 addresses */
1067 if (link
->network
->dns
[i
].family
!= AF_INET
)
1070 ia
= link
->network
->dns
[i
].address
.in
;
1072 /* Never propagate obviously borked data */
1073 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1076 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1079 addresses
[n_addresses
++] = ia
;
1082 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1083 const struct in_addr
*da
= NULL
;
1086 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1089 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1092 for (j
= 0; j
< n
; j
++)
1093 if (in4_addr_is_non_local(&da
[j
]))
1094 addresses
[n_addresses
++] = da
[j
];
1098 if (n_addresses
<= 0)
1101 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1104 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1105 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1106 size_t n_addresses
= 0, n_allocated
= 0;
1112 log_debug("Copying NTP server information from %s", link
->ifname
);
1114 STRV_FOREACH(a
, link
->network
->ntp
) {
1115 union in_addr_union ia
;
1117 /* Only look for IPv4 addresses */
1118 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1121 /* Never propagate obviously borked data */
1122 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1125 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1128 addresses
[n_addresses
++] = ia
.in
;
1131 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1132 const struct in_addr
*da
= NULL
;
1135 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1138 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1141 for (j
= 0; j
< n
; j
++)
1142 if (in4_addr_is_non_local(&da
[j
]))
1143 addresses
[n_addresses
++] = da
[j
];
1147 if (n_addresses
<= 0)
1150 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1153 static int link_set_bridge_fdb(Link
*link
) {
1154 FdbEntry
*fdb_entry
;
1157 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1158 r
= fdb_entry_configure(link
, fdb_entry
);
1160 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1166 static int link_request_set_addresses(Link
*link
) {
1167 AddressLabel
*label
;
1172 assert(link
->network
);
1173 assert(link
->state
!= _LINK_STATE_INVALID
);
1175 link_set_state(link
, LINK_STATE_CONFIGURING
);
1177 /* Reset all *_configured flags we are configuring. */
1178 link
->addresses_configured
= false;
1179 link
->addresses_ready
= false;
1180 link
->neighbors_configured
= false;
1181 link
->static_routes_configured
= false;
1182 link
->routing_policy_rules_configured
= false;
1184 r
= link_set_bridge_fdb(link
);
1188 r
= link_request_set_neighbors(link
);
1192 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1195 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1197 r
= address_configure(ad
, link
, address_handler
, update
);
1199 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1200 link_enter_failed(link
);
1204 link
->address_messages
++;
1207 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1208 r
= address_label_configure(label
, link
, NULL
, false);
1210 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1211 link_enter_failed(link
);
1215 link
->address_label_messages
++;
1218 /* now that we can figure out a default address for the dhcp server,
1220 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1222 Link
*uplink
= NULL
;
1223 bool acquired_uplink
= false;
1225 address
= link_find_dhcp_server_address(link
);
1227 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1228 link_enter_failed(link
);
1232 /* use the server address' subnet as the pool */
1233 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1234 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1239 r = sd_dhcp_server_set_router(link->dhcp_server,
1240 &main_address->in_addr.in);
1245 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1246 r
= sd_dhcp_server_set_max_lease_time(
1248 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1253 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1254 r
= sd_dhcp_server_set_default_lease_time(
1256 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1261 if (link
->network
->dhcp_server_emit_dns
) {
1263 if (link
->network
->n_dhcp_server_dns
> 0)
1264 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1266 uplink
= manager_find_uplink(link
->manager
, link
);
1267 acquired_uplink
= true;
1270 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1273 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1276 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1279 if (link
->network
->dhcp_server_emit_ntp
) {
1281 if (link
->network
->n_dhcp_server_ntp
> 0)
1282 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1284 if (!acquired_uplink
)
1285 uplink
= manager_find_uplink(link
->manager
, link
);
1288 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1291 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1295 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1298 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1300 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1302 if (link
->network
->dhcp_server_emit_timezone
) {
1303 _cleanup_free_
char *buffer
= NULL
;
1304 const char *tz
= NULL
;
1306 if (link
->network
->dhcp_server_timezone
)
1307 tz
= link
->network
->dhcp_server_timezone
;
1309 r
= get_timezone(&buffer
);
1311 log_warning_errno(r
, "Failed to determine timezone: %m");
1317 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1322 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1323 r
= sd_dhcp_server_start(link
->dhcp_server
);
1325 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1327 link_enter_failed(link
);
1333 log_link_debug(link
, "Offering DHCPv4 leases");
1336 if (link
->address_messages
== 0) {
1337 link
->addresses_configured
= true;
1338 link_check_ready(link
);
1340 log_link_debug(link
, "Setting addresses");
1345 static int link_set_bridge_vlan(Link
*link
) {
1348 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1350 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1355 static int link_set_proxy_arp(Link
*link
) {
1358 if (!link_proxy_arp_enabled(link
))
1361 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1363 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1368 static int link_configure_after_setting_mtu(Link
*link
);
1370 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1375 assert(link
->ifname
);
1377 link
->setting_mtu
= false;
1379 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1382 r
= sd_netlink_message_get_errno(m
);
1384 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1388 log_link_debug(link
, "Setting MTU done.");
1390 if (link
->state
== LINK_STATE_INITIALIZED
)
1391 (void) link_configure_after_setting_mtu(link
);
1396 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1397 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1401 assert(link
->manager
);
1402 assert(link
->manager
->rtnl
);
1404 if (mtu
== 0 || link
->setting_mtu
)
1407 if (link
->mtu
== mtu
)
1410 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1412 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1414 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1416 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1417 * for this interface, then disable IPv6 else enable it. */
1418 (void) link_enable_ipv6(link
);
1420 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1421 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1422 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1424 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1425 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1430 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1432 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1434 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1435 link_netlink_destroy_callback
, link
);
1437 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1440 link
->setting_mtu
= true;
1445 static bool link_reduces_vlan_mtu(Link
*link
) {
1446 /* See netif_reduces_vlan_mtu() in kernel. */
1447 return streq_ptr(link
->kind
, "macsec");
1450 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1455 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1456 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1457 /* See vlan_dev_change_mtu() in kernel. */
1458 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1460 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1461 /* See macvlan_change_mtu() in kernel. */
1467 static int link_configure_mtu(Link
*link
) {
1471 assert(link
->network
);
1473 if (link
->network
->mtu
> 0)
1474 return link_set_mtu(link
, link
->network
->mtu
);
1476 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1477 if (link
->mtu
>= mtu
)
1480 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1481 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1484 return link_set_mtu(link
, mtu
);
1487 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1492 assert(link
->ifname
);
1494 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1497 r
= sd_netlink_message_get_errno(m
);
1499 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1504 static int link_set_flags(Link
*link
) {
1505 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1506 unsigned ifi_change
= 0;
1507 unsigned ifi_flags
= 0;
1511 assert(link
->manager
);
1512 assert(link
->manager
->rtnl
);
1514 if (link
->flags
& IFF_LOOPBACK
)
1520 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1523 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1525 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1527 if (link
->network
->arp
>= 0) {
1528 ifi_change
|= IFF_NOARP
;
1529 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1532 if (link
->network
->multicast
>= 0) {
1533 ifi_change
|= IFF_MULTICAST
;
1534 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1537 if (link
->network
->allmulticast
>= 0) {
1538 ifi_change
|= IFF_ALLMULTI
;
1539 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1542 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1544 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1546 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1547 link_netlink_destroy_callback
, link
);
1549 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1556 static int link_acquire_ipv6_conf(Link
*link
) {
1561 if (link_ipv6_accept_ra_enabled(link
)) {
1562 assert(link
->ndisc
);
1564 log_link_debug(link
, "Discovering IPv6 routers");
1566 r
= sd_ndisc_start(link
->ndisc
);
1567 if (r
< 0 && r
!= -EBUSY
)
1568 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1571 if (link_radv_enabled(link
)) {
1573 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1575 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1577 r
= sd_radv_start(link
->radv
);
1578 if (r
< 0 && r
!= -EBUSY
)
1579 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1582 (void) dhcp6_request_prefix_delegation(link
);
1587 static int link_acquire_ipv4_conf(Link
*link
) {
1591 assert(link
->manager
);
1592 assert(link
->manager
->event
);
1594 if (link_ipv4ll_enabled(link
)) {
1595 assert(link
->ipv4ll
);
1597 log_link_debug(link
, "Acquiring IPv4 link-local address");
1599 r
= sd_ipv4ll_start(link
->ipv4ll
);
1601 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1604 if (link_dhcp4_enabled(link
)) {
1605 assert(link
->dhcp_client
);
1607 log_link_debug(link
, "Acquiring DHCPv4 lease");
1609 r
= sd_dhcp_client_start(link
->dhcp_client
);
1611 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1617 static int link_acquire_conf(Link
*link
) {
1622 r
= link_acquire_ipv4_conf(link
);
1626 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1627 r
= link_acquire_ipv6_conf(link
);
1632 if (link_lldp_emit_enabled(link
)) {
1633 r
= link_lldp_emit_start(link
);
1635 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1641 bool link_has_carrier(Link
*link
) {
1642 /* see Documentation/networking/operstates.txt in the kernel sources */
1644 if (link
->kernel_operstate
== IF_OPER_UP
)
1647 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1648 /* operstate may not be implemented, so fall back to flags */
1649 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1650 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1656 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1661 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1664 r
= sd_netlink_message_get_errno(m
);
1666 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1671 static int link_configure_addrgen_mode(Link
*link
) {
1672 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1673 uint8_t ipv6ll_mode
;
1677 assert(link
->network
);
1678 assert(link
->manager
);
1679 assert(link
->manager
->rtnl
);
1681 if (!socket_ipv6_is_supported())
1684 log_link_debug(link
, "Setting address genmode for link");
1686 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1688 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1690 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1692 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1694 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1696 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1698 if (!link_ipv6ll_enabled(link
))
1699 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1700 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1701 /* The file may not exist. And event if it exists, when stable_secret is unset,
1702 * reading the file fails with EIO. */
1703 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1705 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1707 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1709 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1711 r
= sd_netlink_message_close_container(req
);
1713 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1715 r
= sd_netlink_message_close_container(req
);
1717 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1719 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1720 link_netlink_destroy_callback
, link
);
1722 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1729 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1734 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1737 r
= sd_netlink_message_get_errno(m
);
1739 /* we warn but don't fail the link, as it may be brought up later */
1740 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1745 static int link_up(Link
*link
) {
1746 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1750 assert(link
->network
);
1751 assert(link
->manager
);
1752 assert(link
->manager
->rtnl
);
1754 log_link_debug(link
, "Bringing link up");
1756 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1758 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1760 /* set it free if not enslaved with networkd */
1761 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1762 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1764 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1767 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1769 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1771 if (link
->network
->mac
) {
1772 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1774 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1777 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1778 link_netlink_destroy_callback
, link
);
1780 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1787 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1792 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1795 r
= sd_netlink_message_get_errno(m
);
1797 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1802 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1803 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1807 assert(link
->manager
);
1808 assert(link
->manager
->rtnl
);
1810 log_link_debug(link
, "Bringing link down");
1812 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1813 RTM_SETLINK
, link
->ifindex
);
1815 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1817 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1819 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1821 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1822 callback
?: link_down_handler
,
1823 link_netlink_destroy_callback
, link
);
1825 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1832 static int link_handle_bound_to_list(Link
*link
) {
1836 bool required_up
= false;
1837 bool link_is_up
= false;
1841 if (hashmap_isempty(link
->bound_to_links
))
1844 if (link
->flags
& IFF_UP
)
1847 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1848 if (link_has_carrier(l
)) {
1853 if (!required_up
&& link_is_up
) {
1854 r
= link_down(link
, NULL
);
1857 } else if (required_up
&& !link_is_up
) {
1866 static int link_handle_bound_by_list(Link
*link
) {
1873 if (hashmap_isempty(link
->bound_by_links
))
1876 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1877 r
= link_handle_bound_to_list(l
);
1885 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1891 if (link
== carrier
)
1894 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1897 r
= hashmap_ensure_allocated(h
, NULL
);
1901 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1908 static int link_new_bound_by_list(Link
*link
) {
1913 bool list_updated
= false;
1916 assert(link
->manager
);
1920 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1921 if (!carrier
->network
)
1924 if (strv_isempty(carrier
->network
->bind_carrier
))
1927 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1928 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1932 list_updated
= true;
1939 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1940 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1944 link_dirty(carrier
);
1950 static int link_new_bound_to_list(Link
*link
) {
1955 bool list_updated
= false;
1958 assert(link
->manager
);
1963 if (strv_isempty(link
->network
->bind_carrier
))
1968 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1969 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1970 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1974 list_updated
= true;
1981 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1982 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1986 link_dirty(carrier
);
1992 static int link_new_carrier_maps(Link
*link
) {
1995 r
= link_new_bound_by_list(link
);
1999 r
= link_handle_bound_by_list(link
);
2003 r
= link_new_bound_to_list(link
);
2007 r
= link_handle_bound_to_list(link
);
2014 static void link_free_bound_to_list(Link
*link
) {
2018 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2019 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2021 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2022 link_dirty(bound_to
);
2028 static void link_free_bound_by_list(Link
*link
) {
2032 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2033 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2035 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2036 link_dirty(bound_by
);
2037 link_handle_bound_to_list(bound_by
);
2044 static void link_free_carrier_maps(Link
*link
) {
2045 bool list_updated
= false;
2049 if (!hashmap_isempty(link
->bound_to_links
)) {
2050 link_free_bound_to_list(link
);
2051 list_updated
= true;
2054 if (!hashmap_isempty(link
->bound_by_links
)) {
2055 link_free_bound_by_list(link
);
2056 list_updated
= true;
2065 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2072 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2076 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2080 r
= set_put(master
->slaves
, link
);
2088 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2093 if (!link
->manager
|| !netdev
)
2096 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2099 link_unref(set_remove(master
->slaves
, link
));
2102 static void link_detach_from_manager(Link
*link
) {
2103 if (!link
|| !link
->manager
)
2106 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2109 /* The following must be called at last. */
2110 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2114 void link_drop(Link
*link
) {
2115 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2118 link_set_state(link
, LINK_STATE_LINGER
);
2120 link_free_carrier_maps(link
);
2122 if (link
->network
) {
2123 link_drop_from_master(link
, link
->network
->bridge
);
2124 link_drop_from_master(link
, link
->network
->bond
);
2127 log_link_debug(link
, "Link removed");
2129 (void) unlink(link
->state_file
);
2130 link_detach_from_manager(link
);
2133 static int link_joined(Link
*link
) {
2137 assert(link
->network
);
2139 if (!hashmap_isempty(link
->bound_to_links
)) {
2140 r
= link_handle_bound_to_list(link
);
2143 } else if (!(link
->flags
& IFF_UP
)) {
2146 link_enter_failed(link
);
2151 if (link
->network
->bridge
) {
2152 r
= link_set_bridge(link
);
2154 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2156 r
= link_append_to_master(link
, link
->network
->bridge
);
2158 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2161 if (link
->network
->bond
) {
2162 r
= link_set_bond(link
);
2164 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2166 r
= link_append_to_master(link
, link
->network
->bond
);
2168 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2171 if (link
->network
->use_br_vlan
&&
2172 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2173 r
= link_set_bridge_vlan(link
);
2175 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2178 /* Skip setting up addresses until it gets carrier,
2179 or it would try to set addresses twice,
2180 which is bad for non-idempotent steps. */
2181 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2184 return link_request_set_addresses(link
);
2187 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2191 assert(link
->network
);
2192 assert(link
->enslaving
> 0);
2193 assert(!link
->enslaved_raw
);
2197 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2200 r
= sd_netlink_message_get_errno(m
);
2201 if (r
< 0 && r
!= -EEXIST
) {
2202 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2203 link_enter_failed(link
);
2206 log_link_debug(link
, "Joined netdev");
2208 if (link
->enslaving
== 0) {
2209 link
->enslaved_raw
= true;
2216 static int link_enter_join_netdev(Link
*link
) {
2222 assert(link
->network
);
2223 assert(link
->state
== LINK_STATE_INITIALIZED
);
2225 link_set_state(link
, LINK_STATE_CONFIGURING
);
2228 link
->enslaving
= 0;
2229 link
->enslaved_raw
= false;
2231 if (link
->network
->bond
) {
2232 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2233 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2234 return link_joined(link
);
2236 log_struct(LOG_DEBUG
,
2237 LOG_LINK_INTERFACE(link
),
2238 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2239 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2243 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2245 log_struct_errno(LOG_WARNING
, r
,
2246 LOG_LINK_INTERFACE(link
),
2247 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2248 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2249 link_enter_failed(link
);
2254 if (link
->network
->bridge
) {
2255 log_struct(LOG_DEBUG
,
2256 LOG_LINK_INTERFACE(link
),
2257 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2258 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2262 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2264 log_struct_errno(LOG_WARNING
, r
,
2265 LOG_LINK_INTERFACE(link
),
2266 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2267 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2268 link_enter_failed(link
);
2273 if (link
->network
->vrf
) {
2274 log_struct(LOG_DEBUG
,
2275 LOG_LINK_INTERFACE(link
),
2276 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2277 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2281 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2283 log_struct_errno(LOG_WARNING
, r
,
2284 LOG_LINK_INTERFACE(link
),
2285 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2286 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2287 link_enter_failed(link
);
2292 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2294 if (netdev
->ifindex
> 0)
2295 /* Assume already enslaved. */
2298 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2301 log_struct(LOG_DEBUG
,
2302 LOG_LINK_INTERFACE(link
),
2303 LOG_NETDEV_INTERFACE(netdev
),
2304 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2308 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2310 log_struct_errno(LOG_WARNING
, r
,
2311 LOG_LINK_INTERFACE(link
),
2312 LOG_NETDEV_INTERFACE(netdev
),
2313 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2314 link_enter_failed(link
);
2319 if (link
->enslaving
== 0)
2320 return link_joined(link
);
2325 static int link_set_ipv4_forward(Link
*link
) {
2328 if (!link_ipv4_forward_enabled(link
))
2331 /* We propagate the forwarding flag from one interface to the
2332 * global setting one way. This means: as long as at least one
2333 * interface was configured at any time that had IP forwarding
2334 * enabled the setting will stay on for good. We do this
2335 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2336 * somewhat in sync (see below). */
2338 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2340 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2345 static int link_set_ipv6_forward(Link
*link
) {
2348 if (!link_ipv6_forward_enabled(link
))
2351 /* On Linux, the IPv6 stack does not know a per-interface
2352 * packet forwarding setting: either packet forwarding is on
2353 * for all, or off for all. We hence don't bother with a
2354 * per-interface setting, but simply propagate the interface
2355 * flag, if it is set, to the global flag, one-way. Note that
2356 * while IPv4 would allow a per-interface flag, we expose the
2357 * same behaviour there and also propagate the setting from
2358 * one to all, to keep things simple (see above). */
2360 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2362 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2367 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2368 IPv6PrivacyExtensions s
;
2371 s
= link_ipv6_privacy_extensions(link
);
2375 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2377 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2382 static int link_set_ipv6_accept_ra(Link
*link
) {
2385 /* Make this a NOP if IPv6 is not available */
2386 if (!socket_ipv6_is_supported())
2389 if (link
->flags
& IFF_LOOPBACK
)
2395 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2397 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2402 static int link_set_ipv6_dad_transmits(Link
*link
) {
2405 /* Make this a NOP if IPv6 is not available */
2406 if (!socket_ipv6_is_supported())
2409 if (link
->flags
& IFF_LOOPBACK
)
2415 if (link
->network
->ipv6_dad_transmits
< 0)
2418 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2420 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2425 static int link_set_ipv6_hop_limit(Link
*link
) {
2428 /* Make this a NOP if IPv6 is not available */
2429 if (!socket_ipv6_is_supported())
2432 if (link
->flags
& IFF_LOOPBACK
)
2438 if (link
->network
->ipv6_hop_limit
< 0)
2441 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2443 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2448 static int link_set_ipv6_mtu(Link
*link
) {
2451 /* Make this a NOP if IPv6 is not available */
2452 if (!socket_ipv6_is_supported())
2455 if (link
->flags
& IFF_LOOPBACK
)
2458 if (link
->network
->ipv6_mtu
== 0)
2461 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2463 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2468 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2469 Address
*net_address
;
2477 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2478 if (address_equal(net_address
, address
))
2484 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2493 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2494 if (route_equal(net_route
, route
))
2500 static int link_drop_foreign_config(Link
*link
) {
2506 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2507 /* we consider IPv6LL addresses to be managed by the kernel */
2508 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2511 if (link_is_static_address_configured(link
, address
)) {
2512 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2514 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2516 r
= address_remove(address
, link
, NULL
);
2522 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2523 /* do not touch routes managed by the kernel */
2524 if (route
->protocol
== RTPROT_KERNEL
)
2527 if (link_is_static_route_configured(link
, route
)) {
2528 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2532 r
= route_remove(route
, link
, NULL
);
2541 static int link_drop_config(Link
*link
) {
2542 Address
*address
, *pool_address
;
2547 SET_FOREACH(address
, link
->addresses
, i
) {
2548 /* we consider IPv6LL addresses to be managed by the kernel */
2549 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2552 r
= address_remove(address
, link
, NULL
);
2556 /* If this address came from an address pool, clean up the pool */
2557 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2558 if (address_equal(address
, pool_address
)) {
2559 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2560 address_free(pool_address
);
2566 SET_FOREACH(route
, link
->routes
, i
) {
2567 /* do not touch routes managed by the kernel */
2568 if (route
->protocol
== RTPROT_KERNEL
)
2571 r
= route_remove(route
, link
, NULL
);
2581 static int link_configure(Link
*link
) {
2585 assert(link
->network
);
2586 assert(link
->state
== LINK_STATE_INITIALIZED
);
2588 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2589 return link_configure_can(link
);
2591 /* Drop foreign config, but ignore loopback or critical devices.
2592 * We do not want to remove loopback address or addresses used for root NFS. */
2593 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2594 r
= link_drop_foreign_config(link
);
2599 r
= link_set_proxy_arp(link
);
2603 r
= ipv6_proxy_ndp_addresses_configure(link
);
2607 r
= link_set_ipv4_forward(link
);
2611 r
= link_set_ipv6_forward(link
);
2615 r
= link_set_ipv6_privacy_extensions(link
);
2619 r
= link_set_ipv6_accept_ra(link
);
2623 r
= link_set_ipv6_dad_transmits(link
);
2627 r
= link_set_ipv6_hop_limit(link
);
2631 r
= link_set_flags(link
);
2635 r
= link_set_ipv6_mtu(link
);
2639 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2640 r
= ipv4ll_configure(link
);
2645 if (link_dhcp4_enabled(link
)) {
2646 r
= dhcp4_set_promote_secondaries(link
);
2650 r
= dhcp4_configure(link
);
2655 if (link_dhcp4_server_enabled(link
)) {
2656 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2660 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2665 if (link_dhcp6_enabled(link
) ||
2666 link_ipv6_accept_ra_enabled(link
)) {
2667 r
= dhcp6_configure(link
);
2672 if (link_ipv6_accept_ra_enabled(link
)) {
2673 r
= ndisc_configure(link
);
2678 if (link_radv_enabled(link
)) {
2679 r
= radv_configure(link
);
2684 if (link_lldp_rx_enabled(link
)) {
2685 r
= link_lldp_rx_configure(link
);
2690 r
= link_configure_mtu(link
);
2694 r
= link_configure_addrgen_mode(link
);
2698 return link_configure_after_setting_mtu(link
);
2701 static int link_configure_after_setting_mtu(Link
*link
) {
2705 assert(link
->network
);
2706 assert(link
->state
== LINK_STATE_INITIALIZED
);
2708 if (link
->setting_mtu
)
2711 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2712 r
= link_acquire_conf(link
);
2717 return link_enter_join_netdev(link
);
2720 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2723 if (duid
->raw_data_len
> 0)
2726 if (duid
->type
!= DUID_TYPE_UUID
)
2729 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2730 duid
->raw_data_len
= sizeof(sd_id128_t
);
2735 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2736 Manager
*manager
= userdata
;
2737 const sd_bus_error
*e
;
2747 e
= sd_bus_message_get_error(m
);
2749 log_error_errno(sd_bus_error_get_errno(e
),
2750 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2755 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2759 if (sz
!= sizeof(sd_id128_t
)) {
2760 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2764 memcpy(&manager
->product_uuid
, a
, sz
);
2765 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2766 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2768 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2771 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2772 r
= link_configure(link
);
2774 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2777 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2779 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2780 * even if the method fails. */
2781 manager
->has_product_uuid
= true;
2786 static bool link_requires_uuid(Link
*link
) {
2790 assert(link
->manager
);
2791 assert(link
->network
);
2793 duid
= link_get_duid(link
);
2794 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2797 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2800 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2806 static int link_configure_duid(Link
*link
) {
2812 assert(link
->manager
);
2813 assert(link
->network
);
2816 duid
= link_get_duid(link
);
2818 if (!link_requires_uuid(link
))
2821 if (m
->has_product_uuid
) {
2822 (void) duid_set_uuid(duid
, m
->product_uuid
);
2826 if (!m
->links_requesting_uuid
) {
2827 r
= manager_request_product_uuid(m
, link
);
2832 log_link_warning_errno(link
, r
,
2833 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2837 r
= set_put(m
->links_requesting_uuid
, link
);
2841 r
= set_put(m
->duids_requesting_uuid
, duid
);
2849 static int link_initialized_and_synced(Link
*link
) {
2854 assert(link
->ifname
);
2855 assert(link
->manager
);
2857 /* We may get called either from the asynchronous netlink callback,
2858 * or directly for link_add() if running in a container. See link_add(). */
2859 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2862 log_link_debug(link
, "Link state is up-to-date");
2863 link_set_state(link
, LINK_STATE_INITIALIZED
);
2865 r
= link_new_bound_by_list(link
);
2869 r
= link_handle_bound_by_list(link
);
2873 if (!link
->network
) {
2874 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2875 &link
->mac
, &network
);
2877 link_enter_unmanaged(link
);
2879 } else if (r
== 0 && network
->unmanaged
) {
2880 link_enter_unmanaged(link
);
2885 if (link
->flags
& IFF_LOOPBACK
) {
2886 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2887 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2889 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2890 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2892 if (network
->dhcp_server
)
2893 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2896 r
= network_apply(network
, link
);
2901 r
= link_new_bound_to_list(link
);
2905 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2906 * link_configure() is called later asynchronously. */
2907 r
= link_configure_duid(link
);
2911 r
= link_configure(link
);
2918 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2919 (void) link_initialized_and_synced(link
);
2923 int link_initialized(Link
*link
, sd_device
*device
) {
2924 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2928 assert(link
->manager
);
2929 assert(link
->manager
->rtnl
);
2932 if (link
->state
!= LINK_STATE_PENDING
)
2935 if (link
->sd_device
)
2938 log_link_debug(link
, "udev initialized link");
2939 link_set_state(link
, LINK_STATE_INITIALIZED
);
2941 link
->sd_device
= sd_device_ref(device
);
2943 /* udev has initialized the link, but we don't know if we have yet
2944 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2945 * when it returns we know that the pending NEWLINKs have already been
2946 * processed and that we are up-to-date */
2948 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2953 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2954 link_netlink_destroy_callback
, link
);
2963 static int link_load(Link
*link
) {
2964 _cleanup_free_
char *network_file
= NULL
,
2967 *dhcp4_address
= NULL
,
2968 *ipv4ll_address
= NULL
;
2969 union in_addr_union address
;
2970 union in_addr_union route_dst
;
2976 r
= parse_env_file(NULL
, link
->state_file
,
2977 "NETWORK_FILE", &network_file
,
2978 "ADDRESSES", &addresses
,
2980 "DHCP4_ADDRESS", &dhcp4_address
,
2981 "IPV4LL_ADDRESS", &ipv4ll_address
);
2982 if (r
< 0 && r
!= -ENOENT
)
2983 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2990 suffix
= strrchr(network_file
, '.');
2992 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2993 goto network_file_fail
;
2997 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2999 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3000 goto network_file_fail
;
3003 r
= network_apply(network
, link
);
3005 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3014 _cleanup_free_
char *address_str
= NULL
;
3015 char *prefixlen_str
;
3017 unsigned char prefixlen
;
3019 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3021 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3027 prefixlen_str
= strchr(address_str
, '/');
3028 if (!prefixlen_str
) {
3029 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3033 *prefixlen_str
++ = '\0';
3035 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3037 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3041 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3043 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3047 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3049 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3058 _cleanup_free_
char *route_str
= NULL
;
3059 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3061 char *prefixlen_str
;
3063 unsigned char prefixlen
, tos
, table
;
3066 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3068 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3074 prefixlen_str
= strchr(route_str
, '/');
3075 if (!prefixlen_str
) {
3076 log_link_debug(link
, "Failed to parse route %s", route_str
);
3080 *prefixlen_str
++ = '\0';
3082 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3084 log_link_debug(link
,
3085 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3090 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3092 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3096 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3098 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3100 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3101 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3102 0, route_expire_handler
, route
);
3104 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3107 route
->lifetime
= lifetime
;
3108 sd_event_source_unref(route
->expire
);
3109 route
->expire
= TAKE_PTR(expire
);
3113 if (dhcp4_address
) {
3114 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3116 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3117 goto dhcp4_address_fail
;
3120 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3122 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3124 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3126 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3131 if (ipv4ll_address
) {
3132 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3134 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3135 goto ipv4ll_address_fail
;
3138 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3140 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3142 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3144 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3147 ipv4ll_address_fail
:
3152 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3153 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3154 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3163 r
= link_new(m
, message
, ret
);
3169 log_link_debug(link
, "Link %d added", link
->ifindex
);
3171 r
= link_load(link
);
3175 if (detect_container() <= 0) {
3176 /* not in a container, udev will be around */
3177 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3178 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3180 log_link_warning_errno(link
, r
, "Could not find device: %m");
3184 r
= sd_device_get_is_initialized(device
);
3186 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3191 log_link_debug(link
, "link pending udev initialization...");
3195 r
= device_is_renaming(device
);
3197 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3201 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3205 r
= link_initialized(link
, device
);
3209 r
= link_initialized_and_synced(link
);
3216 link_enter_failed(link
);
3220 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3225 log_link_info(link
, "Gained IPv6LL");
3227 link
->ipv6ll_address
= *address
;
3228 link_check_ready(link
);
3230 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3231 r
= link_acquire_ipv6_conf(link
);
3233 link_enter_failed(link
);
3241 static int link_carrier_gained(Link
*link
) {
3246 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3247 r
= link_acquire_conf(link
);
3249 link_enter_failed(link
);
3253 r
= link_request_set_addresses(link
);
3258 r
= link_handle_bound_by_list(link
);
3265 static int link_carrier_lost(Link
*link
) {
3270 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3273 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3274 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3275 * configuration and stop the clients as well. */
3276 if (link
->setting_mtu
)
3279 r
= link_stop_clients(link
);
3281 link_enter_failed(link
);
3285 if (link_dhcp4_server_enabled(link
))
3286 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3288 r
= link_drop_config(link
);
3292 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3293 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3294 r
= link_drop_foreign_config(link
);
3299 r
= link_handle_bound_by_list(link
);
3306 int link_carrier_reset(Link
*link
) {
3311 if (link_has_carrier(link
)) {
3312 r
= link_carrier_lost(link
);
3316 r
= link_carrier_gained(link
);
3320 log_link_info(link
, "Reset carrier");
3326 int link_update(Link
*link
, sd_netlink_message
*m
) {
3327 struct ether_addr mac
;
3330 bool had_carrier
, carrier_gained
, carrier_lost
;
3334 assert(link
->ifname
);
3337 if (link
->state
== LINK_STATE_LINGER
) {
3338 log_link_info(link
, "Link re-added");
3339 link_set_state(link
, LINK_STATE_CONFIGURING
);
3341 r
= link_new_carrier_maps(link
);
3346 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3347 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3348 Manager
*manager
= link
->manager
;
3350 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3353 r
= link_add(manager
, m
, &link
);
3358 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3359 if (r
>= 0 && mtu
> 0) {
3361 if (link
->original_mtu
== 0) {
3362 link
->original_mtu
= mtu
;
3363 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3366 if (link
->dhcp_client
) {
3367 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3370 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3374 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3376 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3380 /* The kernel may broadcast NEWLINK messages without the MAC address
3381 set, simply ignore them. */
3382 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3384 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3387 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3390 log_link_debug(link
, "MAC address: "
3391 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3392 mac
.ether_addr_octet
[0],
3393 mac
.ether_addr_octet
[1],
3394 mac
.ether_addr_octet
[2],
3395 mac
.ether_addr_octet
[3],
3396 mac
.ether_addr_octet
[4],
3397 mac
.ether_addr_octet
[5]);
3400 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3402 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3405 if (link
->dhcp_client
) {
3406 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3407 (const uint8_t *) &link
->mac
,
3411 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3413 r
= dhcp4_set_client_identifier(link
);
3418 if (link
->dhcp6_client
) {
3419 const DUID
* duid
= link_get_duid(link
);
3421 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3422 (const uint8_t *) &link
->mac
,
3426 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3428 if (link
->network
->iaid_set
) {
3429 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3430 link
->network
->iaid
);
3432 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3435 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3437 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3438 duid
->raw_data_len
);
3440 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3444 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3446 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3450 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3452 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3457 had_carrier
= link_has_carrier(link
);
3459 r
= link_update_flags(link
, m
);
3463 r
= link_update_lldp(link
);
3467 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3468 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3470 if (carrier_gained
) {
3471 log_link_info(link
, "Gained carrier");
3473 r
= link_carrier_gained(link
);
3476 } else if (carrier_lost
) {
3477 log_link_info(link
, "Lost carrier");
3479 r
= link_carrier_lost(link
);
3487 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3495 if (hashmap_isempty(h
))
3499 HASHMAP_FOREACH(link
, h
, i
) {
3503 fprintf(f
, "%i", link
->ifindex
);
3510 int link_save(Link
*link
) {
3511 _cleanup_free_
char *temp_path
= NULL
;
3512 _cleanup_fclose_
FILE *f
= NULL
;
3513 const char *admin_state
, *oper_state
;
3520 assert(link
->state_file
);
3521 assert(link
->lease_file
);
3522 assert(link
->manager
);
3524 if (link
->state
== LINK_STATE_LINGER
) {
3525 (void) unlink(link
->state_file
);
3529 link_lldp_save(link
);
3531 admin_state
= link_state_to_string(link
->state
);
3532 assert(admin_state
);
3534 oper_state
= link_operstate_to_string(link
->operstate
);
3537 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3541 (void) fchmod(fileno(f
), 0644);
3544 "# This is private data. Do not parse.\n"
3547 admin_state
, oper_state
);
3549 if (link
->network
) {
3550 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3551 const char *dhcp_domainname
= NULL
, *p
;
3552 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3556 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3557 yes_no(link
->network
->required_for_online
));
3559 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3560 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3562 if (link
->dhcp6_client
) {
3563 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3564 if (r
< 0 && r
!= -ENOMSG
)
3565 log_link_debug(link
, "No DHCPv6 lease");
3568 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3573 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3574 _cleanup_free_
char *b
= NULL
;
3576 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3577 &link
->network
->dns
[j
].address
, &b
);
3579 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3589 if (link
->network
->dhcp_use_dns
&&
3591 const struct in_addr
*addresses
;
3593 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3595 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3599 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3600 struct in6_addr
*in6_addrs
;
3602 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3606 serialize_in6_addrs(f
, in6_addrs
, r
);
3611 /* Make sure to flush out old entries before we use the NDISC data */
3614 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3617 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3621 serialize_in6_addrs(f
, &dd
->address
, 1);
3630 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3632 if (link
->network
->dhcp_use_ntp
&&
3634 const struct in_addr
*addresses
;
3636 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3638 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3642 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3643 struct in6_addr
*in6_addrs
;
3646 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3651 serialize_in6_addrs(f
, in6_addrs
, r
);
3655 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3657 fputstrv(f
, hosts
, NULL
, &space
);
3662 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3663 if (link
->dhcp_lease
) {
3664 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3665 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3668 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3671 fputs("DOMAINS=", f
);
3673 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3674 fputs_with_space(f
, p
, NULL
, &space
);
3676 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3679 if (dhcp_domainname
)
3680 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3682 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3684 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3686 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3687 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3692 fputs("ROUTE_DOMAINS=", f
);
3694 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3695 fputs_with_space(f
, p
, NULL
, &space
);
3697 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3700 if (dhcp_domainname
)
3701 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3703 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3705 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3707 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3708 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3713 fprintf(f
, "LLMNR=%s\n",
3714 resolve_support_to_string(link
->network
->llmnr
));
3715 fprintf(f
, "MDNS=%s\n",
3716 resolve_support_to_string(link
->network
->mdns
));
3717 if (link
->network
->dns_default_route
>= 0)
3718 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3720 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3721 fprintf(f
, "DNS_OVER_TLS=%s\n",
3722 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3724 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3725 fprintf(f
, "DNSSEC=%s\n",
3726 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3728 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3731 fputs("DNSSEC_NTA=", f
);
3733 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3734 fputs_with_space(f
, n
, NULL
, &space
);
3738 fputs("ADDRESSES=", f
);
3740 SET_FOREACH(a
, link
->addresses
, i
) {
3741 _cleanup_free_
char *address_str
= NULL
;
3743 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3747 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3752 fputs("ROUTES=", f
);
3754 SET_FOREACH(route
, link
->routes
, i
) {
3755 _cleanup_free_
char *route_str
= NULL
;
3757 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3761 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3762 space
? " " : "", route_str
,
3763 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3770 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3771 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3773 if (link
->dhcp_lease
) {
3774 struct in_addr address
;
3775 const char *tz
= NULL
;
3777 assert(link
->network
);
3779 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3781 fprintf(f
, "TIMEZONE=%s\n", tz
);
3783 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3785 fputs("DHCP4_ADDRESS=", f
);
3786 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3790 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3798 (void) unlink(link
->lease_file
);
3801 struct in_addr address
;
3803 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3805 fputs("IPV4LL_ADDRESS=", f
);
3806 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3811 r
= fflush_and_check(f
);
3815 if (rename(temp_path
, link
->state_file
) < 0) {
3823 (void) unlink(link
->state_file
);
3825 (void) unlink(temp_path
);
3827 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3830 /* The serialized state in /run is no longer up-to-date. */
3831 void link_dirty(Link
*link
) {
3836 /* mark manager dirty as link is dirty */
3837 manager_dirty(link
->manager
);
3839 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3841 /* allocation errors are ignored */
3844 r
= set_put(link
->manager
->dirty_links
, link
);
3846 /* don't take another ref if the link was already dirty */
3852 /* The serialized state in /run is up-to-date */
3853 void link_clean(Link
*link
) {
3855 assert(link
->manager
);
3857 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3860 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3861 [LINK_STATE_PENDING
] = "pending",
3862 [LINK_STATE_INITIALIZED
] = "initialized",
3863 [LINK_STATE_CONFIGURING
] = "configuring",
3864 [LINK_STATE_CONFIGURED
] = "configured",
3865 [LINK_STATE_UNMANAGED
] = "unmanaged",
3866 [LINK_STATE_FAILED
] = "failed",
3867 [LINK_STATE_LINGER
] = "linger",
3870 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);