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 int link_sysctl_ipv6_enabled(Link
*link
) {
66 _cleanup_free_
char *value
= NULL
;
69 r
= sysctl_read_ip_property(AF_INET6
, link
->ifname
, "disable_ipv6", &value
);
71 return log_link_warning_errno(link
, r
,
72 "Failed to read net.ipv6.conf.%s.disable_ipv6 sysctl property: %m",
75 link
->sysctl_ipv6_enabled
= value
[0] == '0';
76 return link
->sysctl_ipv6_enabled
;
79 static bool link_dhcp6_enabled(Link
*link
) {
82 if (!socket_ipv6_is_supported())
85 if (link
->flags
& IFF_LOOPBACK
)
91 if (link
->network
->bond
)
94 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
97 if (link_sysctl_ipv6_enabled(link
) == 0)
100 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
103 static bool link_dhcp4_enabled(Link
*link
) {
106 if (link
->flags
& IFF_LOOPBACK
)
112 if (link
->network
->bond
)
115 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
118 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
121 static bool link_dhcp4_server_enabled(Link
*link
) {
124 if (link
->flags
& IFF_LOOPBACK
)
130 if (link
->network
->bond
)
133 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
136 return link
->network
->dhcp_server
;
139 bool link_ipv4ll_enabled(Link
*link
, AddressFamilyBoolean mask
) {
141 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
143 if (link
->flags
& IFF_LOOPBACK
)
149 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6", "can", "vcan", "vxcan", "nlmon"))
152 /* L3 or L3S mode do not support ARP. */
153 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
156 if (link
->network
->bond
)
159 if (link
->network
->bond
)
162 return link
->network
->link_local
& mask
;
165 static bool link_ipv6ll_enabled(Link
*link
) {
168 if (!socket_ipv6_is_supported())
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "can", "vcan", "vxcan", "nlmon"))
180 if (link
->network
->bond
)
183 if (link_sysctl_ipv6_enabled(link
) == 0)
186 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
189 static bool link_ipv6_enabled(Link
*link
) {
192 if (!socket_ipv6_is_supported())
195 if (link
->network
->bond
)
198 if (link_sysctl_ipv6_enabled(link
) == 0)
201 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
204 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
205 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
208 static bool link_radv_enabled(Link
*link
) {
211 if (!link_ipv6ll_enabled(link
))
214 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
217 static bool link_ipv4_forward_enabled(Link
*link
) {
220 if (link
->flags
& IFF_LOOPBACK
)
226 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
229 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
232 static bool link_ipv6_forward_enabled(Link
*link
) {
235 if (!socket_ipv6_is_supported())
238 if (link
->flags
& IFF_LOOPBACK
)
244 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
247 if (link_sysctl_ipv6_enabled(link
) == 0)
250 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
253 static bool link_proxy_arp_enabled(Link
*link
) {
256 if (link
->flags
& IFF_LOOPBACK
)
262 if (link
->network
->proxy_arp
< 0)
268 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
271 if (!socket_ipv6_is_supported())
274 if (link
->flags
& IFF_LOOPBACK
)
280 if (!link_ipv6ll_enabled(link
))
283 /* If unset use system default (enabled if local forwarding is disabled.
284 * disabled if local forwarding is enabled).
285 * If set, ignore or enforce RA independent of local forwarding state.
287 if (link
->network
->ipv6_accept_ra
< 0)
288 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
289 return !link_ipv6_forward_enabled(link
);
290 else if (link
->network
->ipv6_accept_ra
> 0)
291 /* accept RA even if ip_forward is enabled */
298 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
301 if (!socket_ipv6_is_supported())
302 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
304 if (link
->flags
& IFF_LOOPBACK
)
305 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
308 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
310 return link
->network
->ipv6_privacy_extensions
;
313 static int link_enable_ipv6(Link
*link
) {
317 if (link
->flags
& IFF_LOOPBACK
)
320 disabled
= !link_ipv6_enabled(link
);
322 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
324 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
326 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
331 static bool link_is_enslaved(Link
*link
) {
332 if (link
->flags
& IFF_SLAVE
)
333 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
339 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
342 /* TODO: add conditions for other netdevs. */
347 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
353 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
356 link_update_operstate(master
, true);
359 void link_update_operstate(Link
*link
, bool also_update_master
) {
360 LinkOperationalState operstate
;
365 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
366 operstate
= LINK_OPERSTATE_DORMANT
;
367 else if (link_has_carrier(link
)) {
369 uint8_t scope
= RT_SCOPE_NOWHERE
;
371 /* if we have carrier, check what addresses we have */
372 SET_FOREACH(address
, link
->addresses
, i
) {
373 if (!address_is_ready(address
))
376 if (address
->scope
< scope
)
377 scope
= address
->scope
;
380 /* for operstate we also take foreign addresses into account */
381 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
382 if (!address_is_ready(address
))
385 if (address
->scope
< scope
)
386 scope
= address
->scope
;
389 if (scope
< RT_SCOPE_SITE
)
390 /* universally accessible addresses found */
391 operstate
= LINK_OPERSTATE_ROUTABLE
;
392 else if (scope
< RT_SCOPE_HOST
)
393 /* only link or site local addresses found */
394 operstate
= LINK_OPERSTATE_DEGRADED
;
396 /* no useful addresses found */
397 operstate
= LINK_OPERSTATE_CARRIER
;
398 } else if (link
->flags
& IFF_UP
)
399 operstate
= LINK_OPERSTATE_NO_CARRIER
;
401 operstate
= LINK_OPERSTATE_OFF
;
403 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
404 link_is_enslaved(link
))
405 operstate
= LINK_OPERSTATE_ENSLAVED
;
407 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
410 SET_FOREACH(slave
, link
->slaves
, i
) {
411 link_update_operstate(slave
, false);
413 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
414 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
418 if (link
->operstate
!= operstate
) {
419 link
->operstate
= operstate
;
420 link_send_changed(link
, "OperationalState", NULL
);
424 if (also_update_master
&& link
->network
) {
425 link_update_master_operstate(link
, link
->network
->bond
);
426 link_update_master_operstate(link
, link
->network
->bridge
);
430 #define FLAG_STRING(string, flag, old, new) \
431 (((old ^ new) & flag) \
432 ? ((old & flag) ? (" -" string) : (" +" string)) \
435 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
436 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
442 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
444 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
446 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
448 /* if we got a message without operstate, take it to mean
449 the state was unchanged */
450 operstate
= link
->kernel_operstate
;
452 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
455 if (link
->flags
!= flags
) {
456 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",
457 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
458 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
459 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
460 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
461 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
462 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
463 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
464 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
465 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
466 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
467 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
468 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
469 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
470 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
471 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
472 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
473 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
474 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
475 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
477 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
478 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
479 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
480 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
481 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
482 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
483 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
484 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
486 /* link flags are currently at most 18 bits, let's align to
488 if (unknown_flags_added
)
490 "Unknown link flags gained: %#.5x (ignoring)",
491 unknown_flags_added
);
493 if (unknown_flags_removed
)
495 "Unknown link flags lost: %#.5x (ignoring)",
496 unknown_flags_removed
);
500 link
->kernel_operstate
= operstate
;
502 link_update_operstate(link
, true);
507 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
508 _cleanup_(link_unrefp
) Link
*link
= NULL
;
510 const char *ifname
, *kind
= NULL
;
512 unsigned short iftype
;
518 /* check for link kind */
519 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
521 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
522 r
= sd_netlink_message_exit_container(message
);
527 r
= sd_netlink_message_get_type(message
, &type
);
530 else if (type
!= RTM_NEWLINK
)
533 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
536 else if (ifindex
<= 0)
539 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
543 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
554 .state
= LINK_STATE_PENDING
,
555 .rtnl_extended_attrs
= true,
558 .sysctl_ipv6_enabled
= -1,
561 link
->ifname
= strdup(ifname
);
566 link
->kind
= strdup(kind
);
571 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
573 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
575 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
577 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
579 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
582 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
585 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
588 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
592 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
596 r
= link_update_flags(link
, message
, false);
600 *ret
= TAKE_PTR(link
);
605 static Link
*link_free(Link
*link
) {
610 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
611 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
613 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
614 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
616 while ((address
= link
->pool_addresses
)) {
617 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
618 address_free(address
);
621 sd_dhcp_server_unref(link
->dhcp_server
);
622 sd_dhcp_client_unref(link
->dhcp_client
);
623 sd_dhcp_lease_unref(link
->dhcp_lease
);
625 link_lldp_emit_stop(link
);
627 free(link
->lease_file
);
629 sd_lldp_unref(link
->lldp
);
630 free(link
->lldp_file
);
634 sd_ipv4ll_unref(link
->ipv4ll
);
635 sd_dhcp6_client_unref(link
->dhcp6_client
);
636 sd_ndisc_unref(link
->ndisc
);
637 sd_radv_unref(link
->radv
);
642 (void) unlink(link
->state_file
);
643 free(link
->state_file
);
645 sd_device_unref(link
->sd_device
);
647 hashmap_free(link
->bound_to_links
);
648 hashmap_free(link
->bound_by_links
);
650 set_free_with_destructor(link
->slaves
, link_unref
);
652 network_unref(link
->network
);
657 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
659 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
666 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
675 void link_set_state(Link
*link
, LinkState state
) {
678 if (link
->state
== state
)
681 log_link_debug(link
, "State changed: %s -> %s",
682 link_state_to_string(link
->state
),
683 link_state_to_string(state
));
687 link_send_changed(link
, "AdministrativeState", NULL
);
690 static void link_enter_unmanaged(Link
*link
) {
693 link_set_state(link
, LINK_STATE_UNMANAGED
);
698 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
702 assert(link
->manager
);
703 assert(link
->manager
->event
);
705 dhcp4_release_old_lease(link
);
707 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
708 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
709 k
= sd_dhcp_client_stop(link
->dhcp_client
);
711 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
715 k
= sd_ipv4ll_stop(link
->ipv4ll
);
717 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
720 if (link
->dhcp6_client
) {
721 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
723 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
727 k
= sd_ndisc_stop(link
->ndisc
);
729 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
733 k
= sd_radv_stop(link
->radv
);
735 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
738 link_lldp_emit_stop(link
);
742 void link_enter_failed(Link
*link
) {
745 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
748 log_link_warning(link
, "Failed");
750 link_set_state(link
, LINK_STATE_FAILED
);
752 link_stop_clients(link
, false);
757 static Address
* link_find_dhcp_server_address(Link
*link
) {
761 assert(link
->network
);
763 /* The first statically configured address if there is any */
764 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
766 if (address
->family
!= AF_INET
)
769 if (in_addr_is_null(address
->family
, &address
->in_addr
))
775 /* If that didn't work, find a suitable address we got from the pool */
776 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
777 if (address
->family
!= AF_INET
)
786 static int link_join_netdevs_after_configured(Link
*link
) {
791 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
792 if (netdev
->ifindex
> 0)
793 /* Assume already enslaved. */
796 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
799 log_struct(LOG_DEBUG
,
800 LOG_LINK_INTERFACE(link
),
801 LOG_NETDEV_INTERFACE(netdev
),
802 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
804 r
= netdev_join(netdev
, link
, NULL
);
806 return log_struct_errno(LOG_WARNING
, r
,
807 LOG_LINK_INTERFACE(link
),
808 LOG_NETDEV_INTERFACE(netdev
),
809 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
815 static void link_enter_configured(Link
*link
) {
817 assert(link
->network
);
819 if (link
->state
!= LINK_STATE_CONFIGURING
)
822 log_link_info(link
, "Configured");
824 link_set_state(link
, LINK_STATE_CONFIGURED
);
826 (void) link_join_netdevs_after_configured(link
);
831 static int link_request_set_routing_policy_rule(Link
*link
) {
832 RoutingPolicyRule
*rule
, *rrule
= NULL
;
836 assert(link
->network
);
838 link_set_state(link
, LINK_STATE_CONFIGURING
);
839 link
->routing_policy_rules_configured
= false;
841 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
842 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
843 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
844 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
846 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
850 r
= routing_policy_rule_configure(rule
, link
, NULL
);
852 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
853 link_enter_failed(link
);
857 link
->routing_policy_rule_messages
++;
860 routing_policy_rule_purge(link
->manager
, link
);
861 if (link
->routing_policy_rule_messages
== 0) {
862 link
->routing_policy_rules_configured
= true;
863 link_check_ready(link
);
865 log_link_debug(link
, "Setting routing policy rules");
870 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
874 assert(link
->route_messages
> 0);
875 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
876 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
878 link
->route_messages
--;
880 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
883 r
= sd_netlink_message_get_errno(m
);
884 if (r
< 0 && r
!= -EEXIST
)
885 log_link_warning_errno(link
, r
, "Could not set route: %m");
887 if (link
->route_messages
== 0) {
888 log_link_debug(link
, "Routes set");
889 link
->static_routes_configured
= true;
890 link_check_ready(link
);
896 int link_request_set_routes(Link
*link
) {
898 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
899 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
906 assert(link
->network
);
907 assert(link
->addresses_configured
);
908 assert(link
->address_messages
== 0);
909 assert(link
->state
!= _LINK_STATE_INVALID
);
911 link_set_state(link
, LINK_STATE_CONFIGURING
);
912 link
->static_routes_configured
= false;
914 r
= link_request_set_routing_policy_rule(link
);
918 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
919 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
920 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
922 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
925 r
= route_configure(rt
, link
, route_handler
);
927 log_link_warning_errno(link
, r
, "Could not set routes: %m");
928 link_enter_failed(link
);
932 link
->route_messages
++;
935 if (link
->route_messages
== 0) {
936 link
->static_routes_configured
= true;
937 link_check_ready(link
);
939 log_link_debug(link
, "Setting routes");
944 void link_check_ready(Link
*link
) {
950 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
956 if (!link
->addresses_configured
)
959 if (!link
->neighbors_configured
)
962 SET_FOREACH(a
, link
->addresses
, i
)
963 if (!address_is_ready(a
))
966 if (!link
->addresses_ready
) {
967 link
->addresses_ready
= true;
968 link_request_set_routes(link
);
972 if (!link
->static_routes_configured
)
975 if (!link
->routing_policy_rules_configured
)
978 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
980 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
983 if (link_ipv6ll_enabled(link
) &&
984 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
987 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
988 !link
->dhcp4_configured
&&
989 !link
->dhcp6_configured
&&
990 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
991 /* When DHCP is enabled, at least one protocol must provide an address, or
992 * an IPv4ll fallback address must be configured. */
995 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
999 if (link
->state
!= LINK_STATE_CONFIGURED
)
1000 link_enter_configured(link
);
1005 static int link_request_set_neighbors(Link
*link
) {
1010 assert(link
->network
);
1011 assert(link
->state
!= _LINK_STATE_INVALID
);
1013 link_set_state(link
, LINK_STATE_CONFIGURING
);
1014 link
->neighbors_configured
= false;
1016 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1017 r
= neighbor_configure(neighbor
, link
, NULL
);
1019 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1020 link_enter_failed(link
);
1025 if (link
->neighbor_messages
== 0) {
1026 link
->neighbors_configured
= true;
1027 link_check_ready(link
);
1029 log_link_debug(link
, "Setting neighbors");
1034 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1040 assert(link
->ifname
);
1041 assert(link
->address_messages
> 0);
1042 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1043 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1045 link
->address_messages
--;
1047 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1050 r
= sd_netlink_message_get_errno(m
);
1051 if (r
< 0 && r
!= -EEXIST
)
1052 log_link_warning_errno(link
, r
, "could not set address: %m");
1054 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1056 if (link
->address_messages
== 0) {
1057 log_link_debug(link
, "Addresses set");
1058 link
->addresses_configured
= true;
1059 link_check_ready(link
);
1065 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1066 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1067 size_t n_addresses
= 0, n_allocated
= 0;
1070 log_debug("Copying DNS server information from %s", link
->ifname
);
1075 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1078 /* Only look for IPv4 addresses */
1079 if (link
->network
->dns
[i
].family
!= AF_INET
)
1082 ia
= link
->network
->dns
[i
].address
.in
;
1084 /* Never propagate obviously borked data */
1085 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1088 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1091 addresses
[n_addresses
++] = ia
;
1094 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1095 const struct in_addr
*da
= NULL
;
1098 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1101 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1104 for (j
= 0; j
< n
; j
++)
1105 if (in4_addr_is_non_local(&da
[j
]))
1106 addresses
[n_addresses
++] = da
[j
];
1110 if (n_addresses
<= 0)
1113 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1116 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1117 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1118 size_t n_addresses
= 0, n_allocated
= 0;
1124 log_debug("Copying NTP server information from %s", link
->ifname
);
1126 STRV_FOREACH(a
, link
->network
->ntp
) {
1127 union in_addr_union ia
;
1129 /* Only look for IPv4 addresses */
1130 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1133 /* Never propagate obviously borked data */
1134 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1137 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1140 addresses
[n_addresses
++] = ia
.in
;
1143 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1144 const struct in_addr
*da
= NULL
;
1147 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1150 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1153 for (j
= 0; j
< n
; j
++)
1154 if (in4_addr_is_non_local(&da
[j
]))
1155 addresses
[n_addresses
++] = da
[j
];
1159 if (n_addresses
<= 0)
1162 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1165 static int link_set_bridge_fdb(Link
*link
) {
1166 FdbEntry
*fdb_entry
;
1169 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1170 r
= fdb_entry_configure(link
, fdb_entry
);
1172 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1178 static int link_request_set_addresses(Link
*link
) {
1179 AddressLabel
*label
;
1184 assert(link
->network
);
1185 assert(link
->state
!= _LINK_STATE_INVALID
);
1187 link_set_state(link
, LINK_STATE_CONFIGURING
);
1189 /* Reset all *_configured flags we are configuring. */
1190 link
->addresses_configured
= false;
1191 link
->addresses_ready
= false;
1192 link
->neighbors_configured
= false;
1193 link
->static_routes_configured
= false;
1194 link
->routing_policy_rules_configured
= false;
1196 r
= link_set_bridge_fdb(link
);
1200 r
= link_request_set_neighbors(link
);
1204 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1207 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1209 r
= address_configure(ad
, link
, address_handler
, update
);
1211 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1212 link_enter_failed(link
);
1216 link
->address_messages
++;
1219 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1220 r
= address_label_configure(label
, link
, NULL
, false);
1222 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1223 link_enter_failed(link
);
1227 link
->address_label_messages
++;
1230 /* now that we can figure out a default address for the dhcp server,
1232 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1234 Link
*uplink
= NULL
;
1235 bool acquired_uplink
= false;
1237 address
= link_find_dhcp_server_address(link
);
1239 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1240 link_enter_failed(link
);
1244 /* use the server address' subnet as the pool */
1245 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1246 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1251 r = sd_dhcp_server_set_router(link->dhcp_server,
1252 &main_address->in_addr.in);
1257 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1258 r
= sd_dhcp_server_set_max_lease_time(
1260 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1265 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1266 r
= sd_dhcp_server_set_default_lease_time(
1268 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1273 if (link
->network
->dhcp_server_emit_dns
) {
1275 if (link
->network
->n_dhcp_server_dns
> 0)
1276 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1278 uplink
= manager_find_uplink(link
->manager
, link
);
1279 acquired_uplink
= true;
1282 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1285 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1288 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1291 if (link
->network
->dhcp_server_emit_ntp
) {
1293 if (link
->network
->n_dhcp_server_ntp
> 0)
1294 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1296 if (!acquired_uplink
)
1297 uplink
= manager_find_uplink(link
->manager
, link
);
1300 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1303 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1307 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1310 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1312 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1314 if (link
->network
->dhcp_server_emit_timezone
) {
1315 _cleanup_free_
char *buffer
= NULL
;
1316 const char *tz
= NULL
;
1318 if (link
->network
->dhcp_server_timezone
)
1319 tz
= link
->network
->dhcp_server_timezone
;
1321 r
= get_timezone(&buffer
);
1323 log_warning_errno(r
, "Failed to determine timezone: %m");
1329 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1334 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1335 r
= sd_dhcp_server_start(link
->dhcp_server
);
1337 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1339 link_enter_failed(link
);
1345 log_link_debug(link
, "Offering DHCPv4 leases");
1348 if (link
->address_messages
== 0) {
1349 link
->addresses_configured
= true;
1350 link_check_ready(link
);
1352 log_link_debug(link
, "Setting addresses");
1357 static int link_set_bridge_vlan(Link
*link
) {
1360 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1362 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1367 static int link_set_proxy_arp(Link
*link
) {
1370 if (!link_proxy_arp_enabled(link
))
1373 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1375 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1380 static int link_configure_after_setting_mtu(Link
*link
);
1382 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1387 assert(link
->ifname
);
1389 link
->setting_mtu
= false;
1391 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1394 r
= sd_netlink_message_get_errno(m
);
1396 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1400 log_link_debug(link
, "Setting MTU done.");
1402 if (link
->state
== LINK_STATE_INITIALIZED
)
1403 (void) link_configure_after_setting_mtu(link
);
1408 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1409 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1413 assert(link
->manager
);
1414 assert(link
->manager
->rtnl
);
1416 if (mtu
== 0 || link
->setting_mtu
)
1419 if (link
->mtu
== mtu
)
1422 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1424 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1426 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1428 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1429 * for this interface, then disable IPv6 else enable it. */
1430 (void) link_enable_ipv6(link
);
1432 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1433 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1434 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1436 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1437 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1442 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1444 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1446 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1447 link_netlink_destroy_callback
, link
);
1449 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1452 link
->setting_mtu
= true;
1457 static bool link_reduces_vlan_mtu(Link
*link
) {
1458 /* See netif_reduces_vlan_mtu() in kernel. */
1459 return streq_ptr(link
->kind
, "macsec");
1462 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1467 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1468 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1469 /* See vlan_dev_change_mtu() in kernel. */
1470 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1472 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1473 /* See macvlan_change_mtu() in kernel. */
1479 static int link_configure_mtu(Link
*link
) {
1483 assert(link
->network
);
1485 if (link
->network
->mtu
> 0)
1486 return link_set_mtu(link
, link
->network
->mtu
);
1488 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1489 if (link
->mtu
>= mtu
)
1492 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1493 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1496 return link_set_mtu(link
, mtu
);
1499 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1504 assert(link
->ifname
);
1506 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1509 r
= sd_netlink_message_get_errno(m
);
1511 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1516 static int link_set_flags(Link
*link
) {
1517 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1518 unsigned ifi_change
= 0;
1519 unsigned ifi_flags
= 0;
1523 assert(link
->manager
);
1524 assert(link
->manager
->rtnl
);
1526 if (link
->flags
& IFF_LOOPBACK
)
1532 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1535 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1537 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1539 if (link
->network
->arp
>= 0) {
1540 ifi_change
|= IFF_NOARP
;
1541 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1544 if (link
->network
->multicast
>= 0) {
1545 ifi_change
|= IFF_MULTICAST
;
1546 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1549 if (link
->network
->allmulticast
>= 0) {
1550 ifi_change
|= IFF_ALLMULTI
;
1551 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1554 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1556 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1558 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1559 link_netlink_destroy_callback
, link
);
1561 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1568 static int link_acquire_ipv6_conf(Link
*link
) {
1573 if (link_ipv6_accept_ra_enabled(link
)) {
1574 assert(link
->ndisc
);
1576 log_link_debug(link
, "Discovering IPv6 routers");
1578 r
= sd_ndisc_start(link
->ndisc
);
1579 if (r
< 0 && r
!= -EBUSY
)
1580 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1583 if (link_radv_enabled(link
)) {
1585 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1587 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1589 r
= sd_radv_start(link
->radv
);
1590 if (r
< 0 && r
!= -EBUSY
)
1591 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1594 (void) dhcp6_request_prefix_delegation(link
);
1599 static int link_acquire_ipv4_conf(Link
*link
) {
1603 assert(link
->manager
);
1604 assert(link
->manager
->event
);
1606 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1607 assert(link
->ipv4ll
);
1609 log_link_debug(link
, "Acquiring IPv4 link-local address");
1611 r
= sd_ipv4ll_start(link
->ipv4ll
);
1613 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1616 if (link_dhcp4_enabled(link
)) {
1617 assert(link
->dhcp_client
);
1619 log_link_debug(link
, "Acquiring DHCPv4 lease");
1621 r
= sd_dhcp_client_start(link
->dhcp_client
);
1623 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1629 static int link_acquire_conf(Link
*link
) {
1634 r
= link_acquire_ipv4_conf(link
);
1638 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1639 r
= link_acquire_ipv6_conf(link
);
1644 if (link_lldp_emit_enabled(link
)) {
1645 r
= link_lldp_emit_start(link
);
1647 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1653 bool link_has_carrier(Link
*link
) {
1654 /* see Documentation/networking/operstates.txt in the kernel sources */
1656 if (link
->kernel_operstate
== IF_OPER_UP
)
1659 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1660 /* operstate may not be implemented, so fall back to flags */
1661 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1662 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1668 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1673 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1676 r
= sd_netlink_message_get_errno(m
);
1678 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1683 static int link_configure_addrgen_mode(Link
*link
) {
1684 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1685 uint8_t ipv6ll_mode
;
1689 assert(link
->network
);
1690 assert(link
->manager
);
1691 assert(link
->manager
->rtnl
);
1693 if (!socket_ipv6_is_supported())
1696 log_link_debug(link
, "Setting address genmode for link");
1698 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1700 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1702 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1704 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1706 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1708 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1710 if (!link_ipv6ll_enabled(link
))
1711 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1712 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1713 /* The file may not exist. And event if it exists, when stable_secret is unset,
1714 * reading the file fails with EIO. */
1715 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1717 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1719 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1721 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1723 r
= sd_netlink_message_close_container(req
);
1725 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1727 r
= sd_netlink_message_close_container(req
);
1729 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1731 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1732 link_netlink_destroy_callback
, link
);
1734 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1741 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1746 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1749 r
= sd_netlink_message_get_errno(m
);
1751 /* we warn but don't fail the link, as it may be brought up later */
1752 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1757 static int link_up(Link
*link
) {
1758 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1762 assert(link
->network
);
1763 assert(link
->manager
);
1764 assert(link
->manager
->rtnl
);
1766 log_link_debug(link
, "Bringing link up");
1768 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1770 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1772 /* set it free if not enslaved with networkd */
1773 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1774 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1776 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1779 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1781 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1783 if (link
->network
->mac
) {
1784 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1786 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1789 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1790 link_netlink_destroy_callback
, link
);
1792 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1799 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1804 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1807 r
= sd_netlink_message_get_errno(m
);
1809 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1814 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1815 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1819 assert(link
->manager
);
1820 assert(link
->manager
->rtnl
);
1822 log_link_debug(link
, "Bringing link down");
1824 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1825 RTM_SETLINK
, link
->ifindex
);
1827 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1829 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1831 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1833 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1834 callback
?: link_down_handler
,
1835 link_netlink_destroy_callback
, link
);
1837 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1844 static int link_handle_bound_to_list(Link
*link
) {
1848 bool required_up
= false;
1849 bool link_is_up
= false;
1853 if (hashmap_isempty(link
->bound_to_links
))
1856 if (link
->flags
& IFF_UP
)
1859 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1860 if (link_has_carrier(l
)) {
1865 if (!required_up
&& link_is_up
) {
1866 r
= link_down(link
, NULL
);
1869 } else if (required_up
&& !link_is_up
) {
1878 static int link_handle_bound_by_list(Link
*link
) {
1885 if (hashmap_isempty(link
->bound_by_links
))
1888 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1889 r
= link_handle_bound_to_list(l
);
1897 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1903 if (link
== carrier
)
1906 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1909 r
= hashmap_ensure_allocated(h
, NULL
);
1913 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1920 static int link_new_bound_by_list(Link
*link
) {
1925 bool list_updated
= false;
1928 assert(link
->manager
);
1932 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1933 if (!carrier
->network
)
1936 if (strv_isempty(carrier
->network
->bind_carrier
))
1939 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1940 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1944 list_updated
= true;
1951 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1952 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1956 link_dirty(carrier
);
1962 static int link_new_bound_to_list(Link
*link
) {
1967 bool list_updated
= false;
1970 assert(link
->manager
);
1975 if (strv_isempty(link
->network
->bind_carrier
))
1980 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1981 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1982 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1986 list_updated
= true;
1993 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1994 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1998 link_dirty(carrier
);
2004 static int link_new_carrier_maps(Link
*link
) {
2007 r
= link_new_bound_by_list(link
);
2011 r
= link_handle_bound_by_list(link
);
2015 r
= link_new_bound_to_list(link
);
2019 r
= link_handle_bound_to_list(link
);
2026 static void link_free_bound_to_list(Link
*link
) {
2030 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2031 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2033 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2034 link_dirty(bound_to
);
2040 static void link_free_bound_by_list(Link
*link
) {
2044 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2045 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2047 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2048 link_dirty(bound_by
);
2049 link_handle_bound_to_list(bound_by
);
2056 static void link_free_carrier_maps(Link
*link
) {
2057 bool list_updated
= false;
2061 if (!hashmap_isempty(link
->bound_to_links
)) {
2062 link_free_bound_to_list(link
);
2063 list_updated
= true;
2066 if (!hashmap_isempty(link
->bound_by_links
)) {
2067 link_free_bound_by_list(link
);
2068 list_updated
= true;
2077 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2084 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2088 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2092 r
= set_put(master
->slaves
, link
);
2100 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2105 if (!link
->manager
|| !netdev
)
2108 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2111 link_unref(set_remove(master
->slaves
, link
));
2114 static void link_detach_from_manager(Link
*link
) {
2115 if (!link
|| !link
->manager
)
2118 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2121 /* The following must be called at last. */
2122 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2126 void link_drop(Link
*link
) {
2127 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2130 link_set_state(link
, LINK_STATE_LINGER
);
2132 link_free_carrier_maps(link
);
2134 if (link
->network
) {
2135 link_drop_from_master(link
, link
->network
->bridge
);
2136 link_drop_from_master(link
, link
->network
->bond
);
2139 log_link_debug(link
, "Link removed");
2141 (void) unlink(link
->state_file
);
2142 link_detach_from_manager(link
);
2145 static int link_joined(Link
*link
) {
2149 assert(link
->network
);
2151 if (!hashmap_isempty(link
->bound_to_links
)) {
2152 r
= link_handle_bound_to_list(link
);
2155 } else if (!(link
->flags
& IFF_UP
)) {
2158 link_enter_failed(link
);
2163 if (link
->network
->bridge
) {
2164 r
= link_set_bridge(link
);
2166 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2168 r
= link_append_to_master(link
, link
->network
->bridge
);
2170 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2173 if (link
->network
->bond
) {
2174 r
= link_set_bond(link
);
2176 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2178 r
= link_append_to_master(link
, link
->network
->bond
);
2180 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2183 if (link
->network
->use_br_vlan
&&
2184 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2185 r
= link_set_bridge_vlan(link
);
2187 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2190 /* Skip setting up addresses until it gets carrier,
2191 or it would try to set addresses twice,
2192 which is bad for non-idempotent steps. */
2193 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2196 return link_request_set_addresses(link
);
2199 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2203 assert(link
->network
);
2204 assert(link
->enslaving
> 0);
2208 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2211 r
= sd_netlink_message_get_errno(m
);
2212 if (r
< 0 && r
!= -EEXIST
) {
2213 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2214 link_enter_failed(link
);
2217 log_link_debug(link
, "Joined netdev");
2219 if (link
->enslaving
== 0) {
2226 static int link_enter_join_netdev(Link
*link
) {
2232 assert(link
->network
);
2233 assert(link
->state
== LINK_STATE_INITIALIZED
);
2235 link_set_state(link
, LINK_STATE_CONFIGURING
);
2238 link
->enslaving
= 0;
2240 if (link
->network
->bond
) {
2241 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2242 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2243 return link_joined(link
);
2245 log_struct(LOG_DEBUG
,
2246 LOG_LINK_INTERFACE(link
),
2247 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2248 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2252 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2254 log_struct_errno(LOG_WARNING
, r
,
2255 LOG_LINK_INTERFACE(link
),
2256 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2257 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2258 link_enter_failed(link
);
2263 if (link
->network
->bridge
) {
2264 log_struct(LOG_DEBUG
,
2265 LOG_LINK_INTERFACE(link
),
2266 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2267 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2271 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2273 log_struct_errno(LOG_WARNING
, r
,
2274 LOG_LINK_INTERFACE(link
),
2275 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2276 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2277 link_enter_failed(link
);
2282 if (link
->network
->vrf
) {
2283 log_struct(LOG_DEBUG
,
2284 LOG_LINK_INTERFACE(link
),
2285 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2286 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2290 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2292 log_struct_errno(LOG_WARNING
, r
,
2293 LOG_LINK_INTERFACE(link
),
2294 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2295 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2296 link_enter_failed(link
);
2301 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2303 if (netdev
->ifindex
> 0)
2304 /* Assume already enslaved. */
2307 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2310 log_struct(LOG_DEBUG
,
2311 LOG_LINK_INTERFACE(link
),
2312 LOG_NETDEV_INTERFACE(netdev
),
2313 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2317 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2319 log_struct_errno(LOG_WARNING
, r
,
2320 LOG_LINK_INTERFACE(link
),
2321 LOG_NETDEV_INTERFACE(netdev
),
2322 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2323 link_enter_failed(link
);
2328 if (link
->enslaving
== 0)
2329 return link_joined(link
);
2334 static int link_set_ipv4_forward(Link
*link
) {
2337 if (!link_ipv4_forward_enabled(link
))
2340 /* We propagate the forwarding flag from one interface to the
2341 * global setting one way. This means: as long as at least one
2342 * interface was configured at any time that had IP forwarding
2343 * enabled the setting will stay on for good. We do this
2344 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2345 * somewhat in sync (see below). */
2347 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2349 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2354 static int link_set_ipv6_forward(Link
*link
) {
2357 if (!link_ipv6_forward_enabled(link
))
2360 /* On Linux, the IPv6 stack does not know a per-interface
2361 * packet forwarding setting: either packet forwarding is on
2362 * for all, or off for all. We hence don't bother with a
2363 * per-interface setting, but simply propagate the interface
2364 * flag, if it is set, to the global flag, one-way. Note that
2365 * while IPv4 would allow a per-interface flag, we expose the
2366 * same behaviour there and also propagate the setting from
2367 * one to all, to keep things simple (see above). */
2369 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2371 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2376 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2377 IPv6PrivacyExtensions s
;
2380 s
= link_ipv6_privacy_extensions(link
);
2384 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2386 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2391 static int link_set_ipv6_accept_ra(Link
*link
) {
2394 /* Make this a NOP if IPv6 is not available */
2395 if (!socket_ipv6_is_supported())
2398 if (link
->flags
& IFF_LOOPBACK
)
2404 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2406 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2411 static int link_set_ipv6_dad_transmits(Link
*link
) {
2414 /* Make this a NOP if IPv6 is not available */
2415 if (!socket_ipv6_is_supported())
2418 if (link
->flags
& IFF_LOOPBACK
)
2424 if (link
->network
->ipv6_dad_transmits
< 0)
2427 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2429 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2434 static int link_set_ipv6_hop_limit(Link
*link
) {
2437 /* Make this a NOP if IPv6 is not available */
2438 if (!socket_ipv6_is_supported())
2441 if (link
->flags
& IFF_LOOPBACK
)
2447 if (link
->network
->ipv6_hop_limit
< 0)
2450 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2452 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2457 static int link_set_ipv6_mtu(Link
*link
) {
2460 /* Make this a NOP if IPv6 is not available */
2461 if (!socket_ipv6_is_supported())
2464 if (link
->flags
& IFF_LOOPBACK
)
2467 if (link
->network
->ipv6_mtu
== 0)
2470 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2472 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2477 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2478 Address
*net_address
;
2486 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2487 if (address_equal(net_address
, address
))
2493 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2502 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2503 if (route_equal(net_route
, route
))
2509 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2516 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2519 /* Even when the address is leased from a DHCP server, networkd assign the address
2520 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2521 * corresponding routes with RTPROT_DHCP. */
2522 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2523 if (route
->protocol
!= RTPROT_DHCP
)
2526 if (address
->family
!= route
->family
)
2529 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2536 static int link_drop_foreign_config(Link
*link
) {
2542 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2543 /* we consider IPv6LL addresses to be managed by the kernel */
2544 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2547 if (link_address_is_dynamic(link
, address
)) {
2548 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2550 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2553 if (link_is_static_address_configured(link
, address
)) {
2554 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2556 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2558 r
= address_remove(address
, link
, NULL
);
2564 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2565 /* do not touch routes managed by the kernel */
2566 if (route
->protocol
== RTPROT_KERNEL
)
2569 if (route
->protocol
== RTPROT_STATIC
&&
2570 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2573 if (route
->protocol
== RTPROT_DHCP
&&
2574 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2577 if (link_is_static_route_configured(link
, route
)) {
2578 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2582 r
= route_remove(route
, link
, NULL
);
2591 static int link_drop_config(Link
*link
) {
2592 Address
*address
, *pool_address
;
2597 SET_FOREACH(address
, link
->addresses
, i
) {
2598 /* we consider IPv6LL addresses to be managed by the kernel */
2599 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2602 r
= address_remove(address
, link
, NULL
);
2606 /* If this address came from an address pool, clean up the pool */
2607 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2608 if (address_equal(address
, pool_address
)) {
2609 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2610 address_free(pool_address
);
2616 SET_FOREACH(route
, link
->routes
, i
) {
2617 /* do not touch routes managed by the kernel */
2618 if (route
->protocol
== RTPROT_KERNEL
)
2621 r
= route_remove(route
, link
, NULL
);
2631 static int link_configure(Link
*link
) {
2635 assert(link
->network
);
2636 assert(link
->state
== LINK_STATE_INITIALIZED
);
2638 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
2639 return link_configure_can(link
);
2641 /* Drop foreign config, but ignore loopback or critical devices.
2642 * We do not want to remove loopback address or addresses used for root NFS. */
2643 if (!(link
->flags
& IFF_LOOPBACK
) &&
2644 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2645 r
= link_drop_foreign_config(link
);
2650 r
= link_set_proxy_arp(link
);
2654 r
= ipv6_proxy_ndp_addresses_configure(link
);
2658 r
= link_set_ipv4_forward(link
);
2662 r
= link_set_ipv6_forward(link
);
2666 r
= link_set_ipv6_privacy_extensions(link
);
2670 r
= link_set_ipv6_accept_ra(link
);
2674 r
= link_set_ipv6_dad_transmits(link
);
2678 r
= link_set_ipv6_hop_limit(link
);
2682 r
= link_set_flags(link
);
2686 r
= link_set_ipv6_mtu(link
);
2690 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2691 r
= ipv4ll_configure(link
);
2696 if (link_dhcp4_enabled(link
)) {
2697 r
= dhcp4_set_promote_secondaries(link
);
2701 r
= dhcp4_configure(link
);
2706 if (link_dhcp4_server_enabled(link
)) {
2707 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2711 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2716 if (link_dhcp6_enabled(link
) ||
2717 link_ipv6_accept_ra_enabled(link
)) {
2718 r
= dhcp6_configure(link
);
2723 if (link_ipv6_accept_ra_enabled(link
)) {
2724 r
= ndisc_configure(link
);
2729 if (link_radv_enabled(link
)) {
2730 r
= radv_configure(link
);
2735 if (link_lldp_rx_enabled(link
)) {
2736 r
= link_lldp_rx_configure(link
);
2741 r
= link_configure_mtu(link
);
2745 r
= link_configure_addrgen_mode(link
);
2749 return link_configure_after_setting_mtu(link
);
2752 static int link_configure_after_setting_mtu(Link
*link
) {
2756 assert(link
->network
);
2757 assert(link
->state
== LINK_STATE_INITIALIZED
);
2759 if (link
->setting_mtu
)
2762 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2763 r
= link_acquire_conf(link
);
2768 return link_enter_join_netdev(link
);
2771 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2774 if (duid
->raw_data_len
> 0)
2777 if (duid
->type
!= DUID_TYPE_UUID
)
2780 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2781 duid
->raw_data_len
= sizeof(sd_id128_t
);
2786 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2787 Manager
*manager
= userdata
;
2788 const sd_bus_error
*e
;
2798 e
= sd_bus_message_get_error(m
);
2800 log_error_errno(sd_bus_error_get_errno(e
),
2801 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2806 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2810 if (sz
!= sizeof(sd_id128_t
)) {
2811 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2815 memcpy(&manager
->product_uuid
, a
, sz
);
2816 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2817 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2819 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2822 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2823 r
= link_configure(link
);
2825 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2828 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2830 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2831 * even if the method fails. */
2832 manager
->has_product_uuid
= true;
2837 static bool link_requires_uuid(Link
*link
) {
2841 assert(link
->manager
);
2842 assert(link
->network
);
2844 duid
= link_get_duid(link
);
2845 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2848 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2851 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2857 static int link_configure_duid(Link
*link
) {
2863 assert(link
->manager
);
2864 assert(link
->network
);
2867 duid
= link_get_duid(link
);
2869 if (!link_requires_uuid(link
))
2872 if (m
->has_product_uuid
) {
2873 (void) duid_set_uuid(duid
, m
->product_uuid
);
2877 if (!m
->links_requesting_uuid
) {
2878 r
= manager_request_product_uuid(m
, link
);
2883 log_link_warning_errno(link
, r
,
2884 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2888 r
= set_put(m
->links_requesting_uuid
, link
);
2892 r
= set_put(m
->duids_requesting_uuid
, duid
);
2900 static int link_initialized_and_synced(Link
*link
) {
2905 assert(link
->ifname
);
2906 assert(link
->manager
);
2908 /* We may get called either from the asynchronous netlink callback,
2909 * or directly for link_add() if running in a container. See link_add(). */
2910 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2913 log_link_debug(link
, "Link state is up-to-date");
2914 link_set_state(link
, LINK_STATE_INITIALIZED
);
2916 r
= link_new_bound_by_list(link
);
2920 r
= link_handle_bound_by_list(link
);
2924 if (!link
->network
) {
2925 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2926 &link
->mac
, &network
);
2928 link_enter_unmanaged(link
);
2930 } else if (r
== 0 && network
->unmanaged
) {
2931 link_enter_unmanaged(link
);
2936 if (link
->flags
& IFF_LOOPBACK
) {
2937 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2938 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2940 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2941 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2943 if (network
->dhcp_server
)
2944 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2947 r
= network_apply(network
, link
);
2952 r
= link_new_bound_to_list(link
);
2956 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2957 * link_configure() is called later asynchronously. */
2958 r
= link_configure_duid(link
);
2962 r
= link_configure(link
);
2969 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2970 (void) link_initialized_and_synced(link
);
2974 int link_initialized(Link
*link
, sd_device
*device
) {
2975 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2979 assert(link
->manager
);
2980 assert(link
->manager
->rtnl
);
2983 if (link
->state
!= LINK_STATE_PENDING
)
2986 if (link
->sd_device
)
2989 log_link_debug(link
, "udev initialized link");
2990 link_set_state(link
, LINK_STATE_INITIALIZED
);
2992 link
->sd_device
= sd_device_ref(device
);
2994 /* udev has initialized the link, but we don't know if we have yet
2995 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2996 * when it returns we know that the pending NEWLINKs have already been
2997 * processed and that we are up-to-date */
2999 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3004 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3005 link_netlink_destroy_callback
, link
);
3014 static int link_load(Link
*link
) {
3015 _cleanup_free_
char *network_file
= NULL
,
3018 *dhcp4_address
= NULL
,
3019 *ipv4ll_address
= NULL
;
3020 union in_addr_union address
;
3021 union in_addr_union route_dst
;
3027 r
= parse_env_file(NULL
, link
->state_file
,
3028 "NETWORK_FILE", &network_file
,
3029 "ADDRESSES", &addresses
,
3031 "DHCP4_ADDRESS", &dhcp4_address
,
3032 "IPV4LL_ADDRESS", &ipv4ll_address
);
3033 if (r
< 0 && r
!= -ENOENT
)
3034 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3041 suffix
= strrchr(network_file
, '.');
3043 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3044 goto network_file_fail
;
3048 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3050 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3051 goto network_file_fail
;
3054 r
= network_apply(network
, link
);
3056 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3065 _cleanup_free_
char *address_str
= NULL
;
3066 char *prefixlen_str
;
3068 unsigned char prefixlen
;
3070 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3072 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3078 prefixlen_str
= strchr(address_str
, '/');
3079 if (!prefixlen_str
) {
3080 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3084 *prefixlen_str
++ = '\0';
3086 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3088 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3092 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3094 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3098 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3100 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3109 _cleanup_free_
char *route_str
= NULL
;
3110 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3112 char *prefixlen_str
;
3114 unsigned char prefixlen
, tos
, table
;
3117 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3119 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3125 prefixlen_str
= strchr(route_str
, '/');
3126 if (!prefixlen_str
) {
3127 log_link_debug(link
, "Failed to parse route %s", route_str
);
3131 *prefixlen_str
++ = '\0';
3133 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3135 log_link_debug(link
,
3136 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3141 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3143 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3147 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3149 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3151 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3152 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3153 0, route_expire_handler
, route
);
3155 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3158 route
->lifetime
= lifetime
;
3159 sd_event_source_unref(route
->expire
);
3160 route
->expire
= TAKE_PTR(expire
);
3164 if (dhcp4_address
) {
3165 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3167 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3168 goto dhcp4_address_fail
;
3171 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3173 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3175 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3177 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3182 if (ipv4ll_address
) {
3183 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3185 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3186 goto ipv4ll_address_fail
;
3189 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3191 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3193 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3195 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3198 ipv4ll_address_fail
:
3203 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3204 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3205 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3214 r
= link_new(m
, message
, ret
);
3220 log_link_debug(link
, "Link %d added", link
->ifindex
);
3222 r
= link_load(link
);
3226 if (detect_container() <= 0) {
3227 /* not in a container, udev will be around */
3228 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3229 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3231 log_link_warning_errno(link
, r
, "Could not find device: %m");
3235 r
= sd_device_get_is_initialized(device
);
3237 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3242 log_link_debug(link
, "link pending udev initialization...");
3246 r
= device_is_renaming(device
);
3248 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3252 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3256 r
= link_initialized(link
, device
);
3260 r
= link_initialized_and_synced(link
);
3267 link_enter_failed(link
);
3271 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3276 log_link_info(link
, "Gained IPv6LL");
3278 link
->ipv6ll_address
= *address
;
3279 link_check_ready(link
);
3281 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3282 r
= link_acquire_ipv6_conf(link
);
3284 link_enter_failed(link
);
3292 static int link_carrier_gained(Link
*link
) {
3297 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3298 r
= link_acquire_conf(link
);
3300 link_enter_failed(link
);
3304 r
= link_request_set_addresses(link
);
3309 r
= link_handle_bound_by_list(link
);
3316 static int link_carrier_lost(Link
*link
) {
3321 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3324 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3325 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3326 * configuration and stop the clients as well. */
3327 if (link
->setting_mtu
)
3330 r
= link_stop_clients(link
, false);
3332 link_enter_failed(link
);
3336 if (link_dhcp4_server_enabled(link
))
3337 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3339 r
= link_drop_config(link
);
3343 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3344 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3345 r
= link_drop_foreign_config(link
);
3350 r
= link_handle_bound_by_list(link
);
3357 int link_carrier_reset(Link
*link
) {
3362 if (link_has_carrier(link
)) {
3363 r
= link_carrier_lost(link
);
3367 r
= link_carrier_gained(link
);
3371 log_link_info(link
, "Reset carrier");
3377 int link_update(Link
*link
, sd_netlink_message
*m
) {
3378 struct ether_addr mac
;
3381 bool had_carrier
, carrier_gained
, carrier_lost
;
3385 assert(link
->ifname
);
3388 if (link
->state
== LINK_STATE_LINGER
) {
3389 log_link_info(link
, "Link re-added");
3390 link_set_state(link
, LINK_STATE_CONFIGURING
);
3392 r
= link_new_carrier_maps(link
);
3397 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3398 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3399 Manager
*manager
= link
->manager
;
3401 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3404 r
= link_add(manager
, m
, &link
);
3409 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3410 if (r
>= 0 && mtu
> 0) {
3412 if (link
->original_mtu
== 0) {
3413 link
->original_mtu
= mtu
;
3414 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3417 if (link
->dhcp_client
) {
3418 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3421 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3425 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3427 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3431 /* The kernel may broadcast NEWLINK messages without the MAC address
3432 set, simply ignore them. */
3433 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3435 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3438 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3441 log_link_debug(link
, "MAC address: "
3442 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3443 mac
.ether_addr_octet
[0],
3444 mac
.ether_addr_octet
[1],
3445 mac
.ether_addr_octet
[2],
3446 mac
.ether_addr_octet
[3],
3447 mac
.ether_addr_octet
[4],
3448 mac
.ether_addr_octet
[5]);
3451 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3453 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3456 if (link
->dhcp_client
) {
3457 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3458 (const uint8_t *) &link
->mac
,
3462 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3464 r
= dhcp4_set_client_identifier(link
);
3469 if (link
->dhcp6_client
) {
3470 const DUID
* duid
= link_get_duid(link
);
3472 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3473 (const uint8_t *) &link
->mac
,
3477 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3479 if (link
->network
->iaid_set
) {
3480 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3481 link
->network
->iaid
);
3483 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3486 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3488 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3489 duid
->raw_data_len
);
3491 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3495 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3497 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3501 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3503 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3508 old_master
= link
->master_ifindex
;
3509 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3511 had_carrier
= link_has_carrier(link
);
3513 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3517 r
= link_update_lldp(link
);
3521 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3522 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3524 if (carrier_gained
) {
3525 log_link_info(link
, "Gained carrier");
3527 r
= link_carrier_gained(link
);
3530 } else if (carrier_lost
) {
3531 log_link_info(link
, "Lost carrier");
3533 r
= link_carrier_lost(link
);
3541 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3549 if (hashmap_isempty(h
))
3553 HASHMAP_FOREACH(link
, h
, i
) {
3557 fprintf(f
, "%i", link
->ifindex
);
3564 int link_save(Link
*link
) {
3565 _cleanup_free_
char *temp_path
= NULL
;
3566 _cleanup_fclose_
FILE *f
= NULL
;
3567 const char *admin_state
, *oper_state
;
3574 assert(link
->state_file
);
3575 assert(link
->lease_file
);
3576 assert(link
->manager
);
3578 if (link
->state
== LINK_STATE_LINGER
) {
3579 (void) unlink(link
->state_file
);
3583 link_lldp_save(link
);
3585 admin_state
= link_state_to_string(link
->state
);
3586 assert(admin_state
);
3588 oper_state
= link_operstate_to_string(link
->operstate
);
3591 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3595 (void) fchmod(fileno(f
), 0644);
3598 "# This is private data. Do not parse.\n"
3601 admin_state
, oper_state
);
3603 if (link
->network
) {
3604 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3605 const char *dhcp_domainname
= NULL
, *p
;
3606 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3610 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3611 yes_no(link
->network
->required_for_online
));
3613 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3614 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3616 if (link
->dhcp6_client
) {
3617 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3618 if (r
< 0 && r
!= -ENOMSG
)
3619 log_link_debug(link
, "No DHCPv6 lease");
3622 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3627 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3628 _cleanup_free_
char *b
= NULL
;
3630 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3631 &link
->network
->dns
[j
].address
, &b
);
3633 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3643 if (link
->network
->dhcp_use_dns
&&
3645 const struct in_addr
*addresses
;
3647 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3649 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3653 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3654 struct in6_addr
*in6_addrs
;
3656 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3660 serialize_in6_addrs(f
, in6_addrs
, r
);
3665 /* Make sure to flush out old entries before we use the NDISC data */
3668 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3671 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3675 serialize_in6_addrs(f
, &dd
->address
, 1);
3684 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3686 if (link
->network
->dhcp_use_ntp
&&
3688 const struct in_addr
*addresses
;
3690 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3692 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3696 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3697 struct in6_addr
*in6_addrs
;
3700 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3705 serialize_in6_addrs(f
, in6_addrs
, r
);
3709 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3711 fputstrv(f
, hosts
, NULL
, &space
);
3716 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3717 if (link
->dhcp_lease
) {
3718 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3719 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3722 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3725 fputs("DOMAINS=", f
);
3727 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3728 fputs_with_space(f
, p
, NULL
, &space
);
3730 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3733 if (dhcp_domainname
)
3734 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3736 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3738 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3740 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3741 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3746 fputs("ROUTE_DOMAINS=", f
);
3748 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3749 fputs_with_space(f
, p
, NULL
, &space
);
3751 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3754 if (dhcp_domainname
)
3755 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3757 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3759 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3761 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3762 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3767 fprintf(f
, "LLMNR=%s\n",
3768 resolve_support_to_string(link
->network
->llmnr
));
3769 fprintf(f
, "MDNS=%s\n",
3770 resolve_support_to_string(link
->network
->mdns
));
3771 if (link
->network
->dns_default_route
>= 0)
3772 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3774 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3775 fprintf(f
, "DNS_OVER_TLS=%s\n",
3776 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3778 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3779 fprintf(f
, "DNSSEC=%s\n",
3780 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3782 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3785 fputs("DNSSEC_NTA=", f
);
3787 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3788 fputs_with_space(f
, n
, NULL
, &space
);
3792 fputs("ADDRESSES=", f
);
3794 SET_FOREACH(a
, link
->addresses
, i
) {
3795 _cleanup_free_
char *address_str
= NULL
;
3797 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3801 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3806 fputs("ROUTES=", f
);
3808 SET_FOREACH(route
, link
->routes
, i
) {
3809 _cleanup_free_
char *route_str
= NULL
;
3811 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3815 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3816 space
? " " : "", route_str
,
3817 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3824 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3825 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3827 if (link
->dhcp_lease
) {
3828 struct in_addr address
;
3829 const char *tz
= NULL
;
3831 assert(link
->network
);
3833 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3835 fprintf(f
, "TIMEZONE=%s\n", tz
);
3837 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3839 fputs("DHCP4_ADDRESS=", f
);
3840 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3844 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3852 (void) unlink(link
->lease_file
);
3855 struct in_addr address
;
3857 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3859 fputs("IPV4LL_ADDRESS=", f
);
3860 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3865 r
= fflush_and_check(f
);
3869 if (rename(temp_path
, link
->state_file
) < 0) {
3877 (void) unlink(link
->state_file
);
3879 (void) unlink(temp_path
);
3881 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3884 /* The serialized state in /run is no longer up-to-date. */
3885 void link_dirty(Link
*link
) {
3890 /* mark manager dirty as link is dirty */
3891 manager_dirty(link
->manager
);
3893 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3895 /* allocation errors are ignored */
3898 r
= set_put(link
->manager
->dirty_links
, link
);
3900 /* don't take another ref if the link was already dirty */
3906 /* The serialized state in /run is up-to-date */
3907 void link_clean(Link
*link
) {
3909 assert(link
->manager
);
3911 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3914 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3915 [LINK_STATE_PENDING
] = "pending",
3916 [LINK_STATE_INITIALIZED
] = "initialized",
3917 [LINK_STATE_CONFIGURING
] = "configuring",
3918 [LINK_STATE_CONFIGURED
] = "configured",
3919 [LINK_STATE_UNMANAGED
] = "unmanaged",
3920 [LINK_STATE_FAILED
] = "failed",
3921 [LINK_STATE_LINGER
] = "linger",
3924 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);