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 (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
83 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
86 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
89 static bool link_dhcp4_enabled(Link
*link
) {
92 if (link
->flags
& IFF_LOOPBACK
)
98 if (link
->network
->bond
)
101 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
104 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
107 static bool link_dhcp4_server_enabled(Link
*link
) {
110 if (link
->flags
& IFF_LOOPBACK
)
116 if (link
->network
->bond
)
119 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
122 return link
->network
->dhcp_server
;
125 bool link_ipv4ll_enabled(Link
*link
, AddressFamilyBoolean mask
) {
127 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
129 if (link
->flags
& IFF_LOOPBACK
)
135 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6", "can", "vcan", "vxcan", "nlmon"))
138 /* L3 or L3S mode do not support ARP. */
139 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
142 if (link
->network
->bond
)
145 if (link
->network
->bond
)
148 return link
->network
->link_local
& mask
;
151 static bool link_ipv6ll_enabled(Link
*link
) {
154 if (!socket_ipv6_is_supported())
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "can", "vcan", "vxcan", "nlmon"))
166 if (link
->network
->bond
)
169 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
172 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
175 static bool link_ipv6_enabled(Link
*link
) {
178 if (!socket_ipv6_is_supported())
181 if (link
->network
->bond
)
184 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
187 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
190 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
191 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
194 static bool link_radv_enabled(Link
*link
) {
197 if (!link_ipv6ll_enabled(link
))
200 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
203 static bool link_ipv4_forward_enabled(Link
*link
) {
206 if (link
->flags
& IFF_LOOPBACK
)
212 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
215 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
218 static bool link_ipv6_forward_enabled(Link
*link
) {
221 if (!socket_ipv6_is_supported())
224 if (link
->flags
& IFF_LOOPBACK
)
230 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
233 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
236 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
239 static bool link_proxy_arp_enabled(Link
*link
) {
242 if (link
->flags
& IFF_LOOPBACK
)
248 if (link
->network
->proxy_arp
< 0)
254 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
257 if (!socket_ipv6_is_supported())
260 if (link
->flags
& IFF_LOOPBACK
)
266 if (!link_ipv6ll_enabled(link
))
269 /* If unset use system default (enabled if local forwarding is disabled.
270 * disabled if local forwarding is enabled).
271 * If set, ignore or enforce RA independent of local forwarding state.
273 if (link
->network
->ipv6_accept_ra
< 0)
274 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
275 return !link_ipv6_forward_enabled(link
);
276 else if (link
->network
->ipv6_accept_ra
> 0)
277 /* accept RA even if ip_forward is enabled */
284 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
287 if (!socket_ipv6_is_supported())
288 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
290 if (link
->flags
& IFF_LOOPBACK
)
291 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
294 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
296 return link
->network
->ipv6_privacy_extensions
;
299 static int link_enable_ipv6(Link
*link
) {
303 if (link
->flags
& IFF_LOOPBACK
)
306 disabled
= !link_ipv6_enabled(link
);
308 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
310 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
312 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
317 static bool link_is_enslaved(Link
*link
) {
318 if (link
->flags
& IFF_SLAVE
)
319 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
325 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
328 /* TODO: add conditions for other netdevs. */
333 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
339 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
342 link_update_operstate(master
, true);
345 void link_update_operstate(Link
*link
, bool also_update_master
) {
346 LinkOperationalState operstate
;
351 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
352 operstate
= LINK_OPERSTATE_DORMANT
;
353 else if (link_has_carrier(link
)) {
355 uint8_t scope
= RT_SCOPE_NOWHERE
;
357 /* if we have carrier, check what addresses we have */
358 SET_FOREACH(address
, link
->addresses
, i
) {
359 if (!address_is_ready(address
))
362 if (address
->scope
< scope
)
363 scope
= address
->scope
;
366 /* for operstate we also take foreign addresses into account */
367 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
368 if (!address_is_ready(address
))
371 if (address
->scope
< scope
)
372 scope
= address
->scope
;
375 if (scope
< RT_SCOPE_SITE
)
376 /* universally accessible addresses found */
377 operstate
= LINK_OPERSTATE_ROUTABLE
;
378 else if (scope
< RT_SCOPE_HOST
)
379 /* only link or site local addresses found */
380 operstate
= LINK_OPERSTATE_DEGRADED
;
382 /* no useful addresses found */
383 operstate
= LINK_OPERSTATE_CARRIER
;
384 } else if (link
->flags
& IFF_UP
)
385 operstate
= LINK_OPERSTATE_NO_CARRIER
;
387 operstate
= LINK_OPERSTATE_OFF
;
389 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
390 link_is_enslaved(link
))
391 operstate
= LINK_OPERSTATE_ENSLAVED
;
393 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
396 SET_FOREACH(slave
, link
->slaves
, i
) {
397 link_update_operstate(slave
, false);
399 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
400 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
404 if (link
->operstate
!= operstate
) {
405 link
->operstate
= operstate
;
406 link_send_changed(link
, "OperationalState", NULL
);
410 if (also_update_master
&& link
->network
) {
411 link_update_master_operstate(link
, link
->network
->bond
);
412 link_update_master_operstate(link
, link
->network
->bridge
);
416 #define FLAG_STRING(string, flag, old, new) \
417 (((old ^ new) & flag) \
418 ? ((old & flag) ? (" -" string) : (" +" string)) \
421 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
422 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
428 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
430 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
432 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
434 /* if we got a message without operstate, take it to mean
435 the state was unchanged */
436 operstate
= link
->kernel_operstate
;
438 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
441 if (link
->flags
!= flags
) {
442 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",
443 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
444 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
445 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
446 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
447 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
448 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
449 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
450 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
451 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
452 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
453 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
454 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
455 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
456 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
457 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
458 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
459 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
460 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
461 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
463 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
464 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
465 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
466 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
467 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
468 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
469 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
470 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
472 /* link flags are currently at most 18 bits, let's align to
474 if (unknown_flags_added
)
476 "Unknown link flags gained: %#.5x (ignoring)",
477 unknown_flags_added
);
479 if (unknown_flags_removed
)
481 "Unknown link flags lost: %#.5x (ignoring)",
482 unknown_flags_removed
);
486 link
->kernel_operstate
= operstate
;
488 link_update_operstate(link
, true);
493 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
494 _cleanup_(link_unrefp
) Link
*link
= NULL
;
496 const char *ifname
, *kind
= NULL
;
498 unsigned short iftype
;
504 /* check for link kind */
505 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
507 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
508 r
= sd_netlink_message_exit_container(message
);
513 r
= sd_netlink_message_get_type(message
, &type
);
516 else if (type
!= RTM_NEWLINK
)
519 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
522 else if (ifindex
<= 0)
525 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
529 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
540 .state
= LINK_STATE_PENDING
,
541 .rtnl_extended_attrs
= true,
546 link
->ifname
= strdup(ifname
);
551 link
->kind
= strdup(kind
);
556 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
558 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
560 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
562 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
564 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
567 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
570 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
573 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
577 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
581 r
= link_update_flags(link
, message
, false);
585 *ret
= TAKE_PTR(link
);
590 static Link
*link_free(Link
*link
) {
595 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
596 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
598 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
599 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
601 while ((address
= link
->pool_addresses
)) {
602 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
603 address_free(address
);
606 sd_dhcp_server_unref(link
->dhcp_server
);
607 sd_dhcp_client_unref(link
->dhcp_client
);
608 sd_dhcp_lease_unref(link
->dhcp_lease
);
610 link_lldp_emit_stop(link
);
612 free(link
->lease_file
);
614 sd_lldp_unref(link
->lldp
);
615 free(link
->lldp_file
);
619 sd_ipv4ll_unref(link
->ipv4ll
);
620 sd_dhcp6_client_unref(link
->dhcp6_client
);
621 sd_ndisc_unref(link
->ndisc
);
622 sd_radv_unref(link
->radv
);
627 (void) unlink(link
->state_file
);
628 free(link
->state_file
);
630 sd_device_unref(link
->sd_device
);
632 hashmap_free(link
->bound_to_links
);
633 hashmap_free(link
->bound_by_links
);
635 set_free_with_destructor(link
->slaves
, link_unref
);
637 network_unref(link
->network
);
642 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
644 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
651 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
660 void link_set_state(Link
*link
, LinkState state
) {
663 if (link
->state
== state
)
666 log_link_debug(link
, "State changed: %s -> %s",
667 link_state_to_string(link
->state
),
668 link_state_to_string(state
));
672 link_send_changed(link
, "AdministrativeState", NULL
);
675 static void link_enter_unmanaged(Link
*link
) {
678 link_set_state(link
, LINK_STATE_UNMANAGED
);
683 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
687 assert(link
->manager
);
688 assert(link
->manager
->event
);
690 dhcp4_release_old_lease(link
);
692 if (link
->dhcp_client
&& (!may_keep_dhcp
|| !link
->network
||
693 !FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
))) {
694 k
= sd_dhcp_client_stop(link
->dhcp_client
);
696 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
700 k
= sd_ipv4ll_stop(link
->ipv4ll
);
702 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
705 if (link
->dhcp6_client
) {
706 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
708 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
712 k
= sd_ndisc_stop(link
->ndisc
);
714 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
718 k
= sd_radv_stop(link
->radv
);
720 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
723 link_lldp_emit_stop(link
);
727 void link_enter_failed(Link
*link
) {
730 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
733 log_link_warning(link
, "Failed");
735 link_set_state(link
, LINK_STATE_FAILED
);
737 link_stop_clients(link
, false);
742 static Address
* link_find_dhcp_server_address(Link
*link
) {
746 assert(link
->network
);
748 /* The first statically configured address if there is any */
749 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
751 if (address
->family
!= AF_INET
)
754 if (in_addr_is_null(address
->family
, &address
->in_addr
))
760 /* If that didn't work, find a suitable address we got from the pool */
761 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
762 if (address
->family
!= AF_INET
)
771 static int link_join_netdevs_after_configured(Link
*link
) {
776 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
777 if (netdev
->ifindex
> 0)
778 /* Assume already enslaved. */
781 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
784 log_struct(LOG_DEBUG
,
785 LOG_LINK_INTERFACE(link
),
786 LOG_NETDEV_INTERFACE(netdev
),
787 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
789 r
= netdev_join(netdev
, link
, NULL
);
791 return log_struct_errno(LOG_WARNING
, r
,
792 LOG_LINK_INTERFACE(link
),
793 LOG_NETDEV_INTERFACE(netdev
),
794 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
800 static void link_enter_configured(Link
*link
) {
802 assert(link
->network
);
804 if (link
->state
!= LINK_STATE_CONFIGURING
)
807 log_link_info(link
, "Configured");
809 link_set_state(link
, LINK_STATE_CONFIGURED
);
811 (void) link_join_netdevs_after_configured(link
);
816 static int link_request_set_routing_policy_rule(Link
*link
) {
817 RoutingPolicyRule
*rule
, *rrule
= NULL
;
821 assert(link
->network
);
823 link_set_state(link
, LINK_STATE_CONFIGURING
);
824 link
->routing_policy_rules_configured
= false;
826 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
827 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
828 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
829 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
831 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
835 r
= routing_policy_rule_configure(rule
, link
, NULL
);
837 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
838 link_enter_failed(link
);
842 link
->routing_policy_rule_messages
++;
845 routing_policy_rule_purge(link
->manager
, link
);
846 if (link
->routing_policy_rule_messages
== 0) {
847 link
->routing_policy_rules_configured
= true;
848 link_check_ready(link
);
850 log_link_debug(link
, "Setting routing policy rules");
855 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
859 assert(link
->route_messages
> 0);
860 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
861 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
863 link
->route_messages
--;
865 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
868 r
= sd_netlink_message_get_errno(m
);
869 if (r
< 0 && r
!= -EEXIST
)
870 log_link_warning_errno(link
, r
, "Could not set route: %m");
872 if (link
->route_messages
== 0) {
873 log_link_debug(link
, "Routes set");
874 link
->static_routes_configured
= true;
875 link_check_ready(link
);
881 int link_request_set_routes(Link
*link
) {
883 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
884 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
891 assert(link
->network
);
892 assert(link
->addresses_configured
);
893 assert(link
->address_messages
== 0);
894 assert(link
->state
!= _LINK_STATE_INVALID
);
896 link_set_state(link
, LINK_STATE_CONFIGURING
);
897 link
->static_routes_configured
= false;
899 r
= link_request_set_routing_policy_rule(link
);
903 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
904 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
905 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
907 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
910 r
= route_configure(rt
, link
, route_handler
);
912 log_link_warning_errno(link
, r
, "Could not set routes: %m");
913 link_enter_failed(link
);
917 link
->route_messages
++;
920 if (link
->route_messages
== 0) {
921 link
->static_routes_configured
= true;
922 link_check_ready(link
);
924 log_link_debug(link
, "Setting routes");
929 void link_check_ready(Link
*link
) {
935 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
941 if (!link
->addresses_configured
)
944 if (!link
->neighbors_configured
)
947 SET_FOREACH(a
, link
->addresses
, i
)
948 if (!address_is_ready(a
))
951 if (!link
->addresses_ready
) {
952 link
->addresses_ready
= true;
953 link_request_set_routes(link
);
957 if (!link
->static_routes_configured
)
960 if (!link
->routing_policy_rules_configured
)
963 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
966 if (link_ipv6ll_enabled(link
) &&
967 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
970 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
971 !link
->dhcp4_configured
&&
972 !link
->dhcp6_configured
&&
973 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
974 /* When DHCP is enabled, at least one protocol must provide an address, or
975 * an IPv4ll fallback address must be configured. */
978 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
981 if (link
->state
!= LINK_STATE_CONFIGURED
)
982 link_enter_configured(link
);
987 static int link_request_set_neighbors(Link
*link
) {
992 assert(link
->network
);
993 assert(link
->state
!= _LINK_STATE_INVALID
);
995 link_set_state(link
, LINK_STATE_CONFIGURING
);
996 link
->neighbors_configured
= false;
998 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
999 r
= neighbor_configure(neighbor
, link
, NULL
);
1001 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1002 link_enter_failed(link
);
1007 if (link
->neighbor_messages
== 0) {
1008 link
->neighbors_configured
= true;
1009 link_check_ready(link
);
1011 log_link_debug(link
, "Setting neighbors");
1016 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1022 assert(link
->ifname
);
1023 assert(link
->address_messages
> 0);
1024 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1025 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1027 link
->address_messages
--;
1029 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1032 r
= sd_netlink_message_get_errno(m
);
1033 if (r
< 0 && r
!= -EEXIST
)
1034 log_link_warning_errno(link
, r
, "could not set address: %m");
1036 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1038 if (link
->address_messages
== 0) {
1039 log_link_debug(link
, "Addresses set");
1040 link
->addresses_configured
= true;
1041 link_check_ready(link
);
1047 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1048 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1049 size_t n_addresses
= 0, n_allocated
= 0;
1052 log_debug("Copying DNS server information from %s", link
->ifname
);
1057 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1060 /* Only look for IPv4 addresses */
1061 if (link
->network
->dns
[i
].family
!= AF_INET
)
1064 ia
= link
->network
->dns
[i
].address
.in
;
1066 /* Never propagate obviously borked data */
1067 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1070 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1073 addresses
[n_addresses
++] = ia
;
1076 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1077 const struct in_addr
*da
= NULL
;
1080 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1083 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1086 for (j
= 0; j
< n
; j
++)
1087 if (in4_addr_is_non_local(&da
[j
]))
1088 addresses
[n_addresses
++] = da
[j
];
1092 if (n_addresses
<= 0)
1095 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1098 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1099 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1100 size_t n_addresses
= 0, n_allocated
= 0;
1106 log_debug("Copying NTP server information from %s", link
->ifname
);
1108 STRV_FOREACH(a
, link
->network
->ntp
) {
1109 union in_addr_union ia
;
1111 /* Only look for IPv4 addresses */
1112 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1115 /* Never propagate obviously borked data */
1116 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1119 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1122 addresses
[n_addresses
++] = ia
.in
;
1125 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1126 const struct in_addr
*da
= NULL
;
1129 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1132 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1135 for (j
= 0; j
< n
; j
++)
1136 if (in4_addr_is_non_local(&da
[j
]))
1137 addresses
[n_addresses
++] = da
[j
];
1141 if (n_addresses
<= 0)
1144 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1147 static int link_set_bridge_fdb(Link
*link
) {
1148 FdbEntry
*fdb_entry
;
1151 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1152 r
= fdb_entry_configure(link
, fdb_entry
);
1154 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1160 static int link_request_set_addresses(Link
*link
) {
1161 AddressLabel
*label
;
1166 assert(link
->network
);
1167 assert(link
->state
!= _LINK_STATE_INVALID
);
1169 link_set_state(link
, LINK_STATE_CONFIGURING
);
1171 /* Reset all *_configured flags we are configuring. */
1172 link
->addresses_configured
= false;
1173 link
->addresses_ready
= false;
1174 link
->neighbors_configured
= false;
1175 link
->static_routes_configured
= false;
1176 link
->routing_policy_rules_configured
= false;
1178 r
= link_set_bridge_fdb(link
);
1182 r
= link_request_set_neighbors(link
);
1186 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1189 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1191 r
= address_configure(ad
, link
, address_handler
, update
);
1193 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1194 link_enter_failed(link
);
1198 link
->address_messages
++;
1201 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1202 r
= address_label_configure(label
, link
, NULL
, false);
1204 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1205 link_enter_failed(link
);
1209 link
->address_label_messages
++;
1212 /* now that we can figure out a default address for the dhcp server,
1214 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1216 Link
*uplink
= NULL
;
1217 bool acquired_uplink
= false;
1219 address
= link_find_dhcp_server_address(link
);
1221 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1222 link_enter_failed(link
);
1226 /* use the server address' subnet as the pool */
1227 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1228 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1233 r = sd_dhcp_server_set_router(link->dhcp_server,
1234 &main_address->in_addr.in);
1239 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1240 r
= sd_dhcp_server_set_max_lease_time(
1242 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1247 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1248 r
= sd_dhcp_server_set_default_lease_time(
1250 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1255 if (link
->network
->dhcp_server_emit_dns
) {
1257 if (link
->network
->n_dhcp_server_dns
> 0)
1258 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1260 uplink
= manager_find_uplink(link
->manager
, link
);
1261 acquired_uplink
= true;
1264 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1267 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1270 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1273 if (link
->network
->dhcp_server_emit_ntp
) {
1275 if (link
->network
->n_dhcp_server_ntp
> 0)
1276 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1278 if (!acquired_uplink
)
1279 uplink
= manager_find_uplink(link
->manager
, link
);
1282 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1285 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1289 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1292 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1294 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1296 if (link
->network
->dhcp_server_emit_timezone
) {
1297 _cleanup_free_
char *buffer
= NULL
;
1298 const char *tz
= NULL
;
1300 if (link
->network
->dhcp_server_timezone
)
1301 tz
= link
->network
->dhcp_server_timezone
;
1303 r
= get_timezone(&buffer
);
1305 log_warning_errno(r
, "Failed to determine timezone: %m");
1311 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1316 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1317 r
= sd_dhcp_server_start(link
->dhcp_server
);
1319 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1321 link_enter_failed(link
);
1327 log_link_debug(link
, "Offering DHCPv4 leases");
1330 if (link
->address_messages
== 0) {
1331 link
->addresses_configured
= true;
1332 link_check_ready(link
);
1334 log_link_debug(link
, "Setting addresses");
1339 static int link_set_bridge_vlan(Link
*link
) {
1342 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1344 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1349 static int link_set_proxy_arp(Link
*link
) {
1352 if (!link_proxy_arp_enabled(link
))
1355 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1357 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1362 static int link_configure_after_setting_mtu(Link
*link
);
1364 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1369 assert(link
->ifname
);
1371 link
->setting_mtu
= false;
1373 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1376 r
= sd_netlink_message_get_errno(m
);
1378 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1382 log_link_debug(link
, "Setting MTU done.");
1384 if (link
->state
== LINK_STATE_INITIALIZED
)
1385 (void) link_configure_after_setting_mtu(link
);
1390 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1391 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1395 assert(link
->manager
);
1396 assert(link
->manager
->rtnl
);
1398 if (mtu
== 0 || link
->setting_mtu
)
1401 if (link
->mtu
== mtu
)
1404 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1406 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1408 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1410 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1411 * for this interface, then disable IPv6 else enable it. */
1412 (void) link_enable_ipv6(link
);
1414 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1415 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1416 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1418 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1419 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1424 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1426 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1428 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1429 link_netlink_destroy_callback
, link
);
1431 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1434 link
->setting_mtu
= true;
1439 static bool link_reduces_vlan_mtu(Link
*link
) {
1440 /* See netif_reduces_vlan_mtu() in kernel. */
1441 return streq_ptr(link
->kind
, "macsec");
1444 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1449 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1450 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1451 /* See vlan_dev_change_mtu() in kernel. */
1452 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1454 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1455 /* See macvlan_change_mtu() in kernel. */
1461 static int link_configure_mtu(Link
*link
) {
1465 assert(link
->network
);
1467 if (link
->network
->mtu
> 0)
1468 return link_set_mtu(link
, link
->network
->mtu
);
1470 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1471 if (link
->mtu
>= mtu
)
1474 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1475 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1478 return link_set_mtu(link
, mtu
);
1481 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1486 assert(link
->ifname
);
1488 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1491 r
= sd_netlink_message_get_errno(m
);
1493 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1498 static int link_set_flags(Link
*link
) {
1499 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1500 unsigned ifi_change
= 0;
1501 unsigned ifi_flags
= 0;
1505 assert(link
->manager
);
1506 assert(link
->manager
->rtnl
);
1508 if (link
->flags
& IFF_LOOPBACK
)
1514 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1517 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1519 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1521 if (link
->network
->arp
>= 0) {
1522 ifi_change
|= IFF_NOARP
;
1523 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1526 if (link
->network
->multicast
>= 0) {
1527 ifi_change
|= IFF_MULTICAST
;
1528 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1531 if (link
->network
->allmulticast
>= 0) {
1532 ifi_change
|= IFF_ALLMULTI
;
1533 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1536 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1538 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1540 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1541 link_netlink_destroy_callback
, link
);
1543 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1550 static int link_acquire_ipv6_conf(Link
*link
) {
1555 if (link_ipv6_accept_ra_enabled(link
)) {
1556 assert(link
->ndisc
);
1558 log_link_debug(link
, "Discovering IPv6 routers");
1560 r
= sd_ndisc_start(link
->ndisc
);
1561 if (r
< 0 && r
!= -EBUSY
)
1562 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1565 if (link_radv_enabled(link
)) {
1567 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1569 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1571 r
= sd_radv_start(link
->radv
);
1572 if (r
< 0 && r
!= -EBUSY
)
1573 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1576 (void) dhcp6_request_prefix_delegation(link
);
1581 static int link_acquire_ipv4_conf(Link
*link
) {
1585 assert(link
->manager
);
1586 assert(link
->manager
->event
);
1588 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1589 assert(link
->ipv4ll
);
1591 log_link_debug(link
, "Acquiring IPv4 link-local address");
1593 r
= sd_ipv4ll_start(link
->ipv4ll
);
1595 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1598 if (link_dhcp4_enabled(link
)) {
1599 assert(link
->dhcp_client
);
1601 log_link_debug(link
, "Acquiring DHCPv4 lease");
1603 r
= sd_dhcp_client_start(link
->dhcp_client
);
1605 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1611 static int link_acquire_conf(Link
*link
) {
1616 r
= link_acquire_ipv4_conf(link
);
1620 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1621 r
= link_acquire_ipv6_conf(link
);
1626 if (link_lldp_emit_enabled(link
)) {
1627 r
= link_lldp_emit_start(link
);
1629 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1635 bool link_has_carrier(Link
*link
) {
1636 /* see Documentation/networking/operstates.txt in the kernel sources */
1638 if (link
->kernel_operstate
== IF_OPER_UP
)
1641 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1642 /* operstate may not be implemented, so fall back to flags */
1643 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1644 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1650 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1655 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1658 r
= sd_netlink_message_get_errno(m
);
1660 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1665 static int link_configure_addrgen_mode(Link
*link
) {
1666 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1667 uint8_t ipv6ll_mode
;
1671 assert(link
->network
);
1672 assert(link
->manager
);
1673 assert(link
->manager
->rtnl
);
1675 if (!socket_ipv6_is_supported())
1678 log_link_debug(link
, "Setting address genmode for link");
1680 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1682 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1684 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1686 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1688 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1690 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1692 if (!link_ipv6ll_enabled(link
))
1693 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1694 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1695 /* The file may not exist. And event if it exists, when stable_secret is unset,
1696 * reading the file fails with EIO. */
1697 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1699 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1701 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1703 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1705 r
= sd_netlink_message_close_container(req
);
1707 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1709 r
= sd_netlink_message_close_container(req
);
1711 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1713 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1714 link_netlink_destroy_callback
, link
);
1716 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1723 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1728 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1731 r
= sd_netlink_message_get_errno(m
);
1733 /* we warn but don't fail the link, as it may be brought up later */
1734 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1739 static int link_up(Link
*link
) {
1740 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1744 assert(link
->network
);
1745 assert(link
->manager
);
1746 assert(link
->manager
->rtnl
);
1748 log_link_debug(link
, "Bringing link up");
1750 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1752 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1754 /* set it free if not enslaved with networkd */
1755 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1756 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1758 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1761 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1763 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1765 if (link
->network
->mac
) {
1766 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1768 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1771 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1772 link_netlink_destroy_callback
, link
);
1774 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1781 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1786 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1789 r
= sd_netlink_message_get_errno(m
);
1791 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1796 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1797 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1801 assert(link
->manager
);
1802 assert(link
->manager
->rtnl
);
1804 log_link_debug(link
, "Bringing link down");
1806 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1807 RTM_SETLINK
, link
->ifindex
);
1809 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1811 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1813 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1815 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1816 callback
?: link_down_handler
,
1817 link_netlink_destroy_callback
, link
);
1819 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1826 static int link_handle_bound_to_list(Link
*link
) {
1830 bool required_up
= false;
1831 bool link_is_up
= false;
1835 if (hashmap_isempty(link
->bound_to_links
))
1838 if (link
->flags
& IFF_UP
)
1841 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1842 if (link_has_carrier(l
)) {
1847 if (!required_up
&& link_is_up
) {
1848 r
= link_down(link
, NULL
);
1851 } else if (required_up
&& !link_is_up
) {
1860 static int link_handle_bound_by_list(Link
*link
) {
1867 if (hashmap_isempty(link
->bound_by_links
))
1870 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1871 r
= link_handle_bound_to_list(l
);
1879 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1885 if (link
== carrier
)
1888 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1891 r
= hashmap_ensure_allocated(h
, NULL
);
1895 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1902 static int link_new_bound_by_list(Link
*link
) {
1907 bool list_updated
= false;
1910 assert(link
->manager
);
1914 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1915 if (!carrier
->network
)
1918 if (strv_isempty(carrier
->network
->bind_carrier
))
1921 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1922 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1926 list_updated
= true;
1933 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1934 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1938 link_dirty(carrier
);
1944 static int link_new_bound_to_list(Link
*link
) {
1949 bool list_updated
= false;
1952 assert(link
->manager
);
1957 if (strv_isempty(link
->network
->bind_carrier
))
1962 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1963 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1964 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1968 list_updated
= true;
1975 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1976 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1980 link_dirty(carrier
);
1986 static int link_new_carrier_maps(Link
*link
) {
1989 r
= link_new_bound_by_list(link
);
1993 r
= link_handle_bound_by_list(link
);
1997 r
= link_new_bound_to_list(link
);
2001 r
= link_handle_bound_to_list(link
);
2008 static void link_free_bound_to_list(Link
*link
) {
2012 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2013 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2015 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2016 link_dirty(bound_to
);
2022 static void link_free_bound_by_list(Link
*link
) {
2026 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2027 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2029 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2030 link_dirty(bound_by
);
2031 link_handle_bound_to_list(bound_by
);
2038 static void link_free_carrier_maps(Link
*link
) {
2039 bool list_updated
= false;
2043 if (!hashmap_isempty(link
->bound_to_links
)) {
2044 link_free_bound_to_list(link
);
2045 list_updated
= true;
2048 if (!hashmap_isempty(link
->bound_by_links
)) {
2049 link_free_bound_by_list(link
);
2050 list_updated
= true;
2059 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2066 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2070 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2074 r
= set_put(master
->slaves
, link
);
2082 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2087 if (!link
->manager
|| !netdev
)
2090 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2093 link_unref(set_remove(master
->slaves
, link
));
2096 static void link_detach_from_manager(Link
*link
) {
2097 if (!link
|| !link
->manager
)
2100 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2103 /* The following must be called at last. */
2104 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2108 void link_drop(Link
*link
) {
2109 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2112 link_set_state(link
, LINK_STATE_LINGER
);
2114 link_free_carrier_maps(link
);
2116 if (link
->network
) {
2117 link_drop_from_master(link
, link
->network
->bridge
);
2118 link_drop_from_master(link
, link
->network
->bond
);
2121 log_link_debug(link
, "Link removed");
2123 (void) unlink(link
->state_file
);
2124 link_detach_from_manager(link
);
2127 static int link_joined(Link
*link
) {
2131 assert(link
->network
);
2133 if (!hashmap_isempty(link
->bound_to_links
)) {
2134 r
= link_handle_bound_to_list(link
);
2137 } else if (!(link
->flags
& IFF_UP
)) {
2140 link_enter_failed(link
);
2145 if (link
->network
->bridge
) {
2146 r
= link_set_bridge(link
);
2148 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2150 r
= link_append_to_master(link
, link
->network
->bridge
);
2152 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2155 if (link
->network
->bond
) {
2156 r
= link_set_bond(link
);
2158 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2160 r
= link_append_to_master(link
, link
->network
->bond
);
2162 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2165 if (link
->network
->use_br_vlan
&&
2166 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2167 r
= link_set_bridge_vlan(link
);
2169 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2172 /* Skip setting up addresses until it gets carrier,
2173 or it would try to set addresses twice,
2174 which is bad for non-idempotent steps. */
2175 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2178 return link_request_set_addresses(link
);
2181 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2185 assert(link
->network
);
2186 assert(link
->enslaving
> 0);
2190 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2193 r
= sd_netlink_message_get_errno(m
);
2194 if (r
< 0 && r
!= -EEXIST
) {
2195 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2196 link_enter_failed(link
);
2199 log_link_debug(link
, "Joined netdev");
2201 if (link
->enslaving
== 0) {
2208 static int link_enter_join_netdev(Link
*link
) {
2214 assert(link
->network
);
2215 assert(link
->state
== LINK_STATE_INITIALIZED
);
2217 link_set_state(link
, LINK_STATE_CONFIGURING
);
2220 link
->enslaving
= 0;
2222 if (link
->network
->bond
) {
2223 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2224 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2225 return link_joined(link
);
2227 log_struct(LOG_DEBUG
,
2228 LOG_LINK_INTERFACE(link
),
2229 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2230 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2234 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2236 log_struct_errno(LOG_WARNING
, r
,
2237 LOG_LINK_INTERFACE(link
),
2238 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2239 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2240 link_enter_failed(link
);
2245 if (link
->network
->bridge
) {
2246 log_struct(LOG_DEBUG
,
2247 LOG_LINK_INTERFACE(link
),
2248 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2249 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2253 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2255 log_struct_errno(LOG_WARNING
, r
,
2256 LOG_LINK_INTERFACE(link
),
2257 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2258 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2259 link_enter_failed(link
);
2264 if (link
->network
->vrf
) {
2265 log_struct(LOG_DEBUG
,
2266 LOG_LINK_INTERFACE(link
),
2267 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2268 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2272 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2274 log_struct_errno(LOG_WARNING
, r
,
2275 LOG_LINK_INTERFACE(link
),
2276 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2277 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2278 link_enter_failed(link
);
2283 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2285 if (netdev
->ifindex
> 0)
2286 /* Assume already enslaved. */
2289 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2292 log_struct(LOG_DEBUG
,
2293 LOG_LINK_INTERFACE(link
),
2294 LOG_NETDEV_INTERFACE(netdev
),
2295 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2299 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2301 log_struct_errno(LOG_WARNING
, r
,
2302 LOG_LINK_INTERFACE(link
),
2303 LOG_NETDEV_INTERFACE(netdev
),
2304 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2305 link_enter_failed(link
);
2310 if (link
->enslaving
== 0)
2311 return link_joined(link
);
2316 static int link_set_ipv4_forward(Link
*link
) {
2319 if (!link_ipv4_forward_enabled(link
))
2322 /* We propagate the forwarding flag from one interface to the
2323 * global setting one way. This means: as long as at least one
2324 * interface was configured at any time that had IP forwarding
2325 * enabled the setting will stay on for good. We do this
2326 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2327 * somewhat in sync (see below). */
2329 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2331 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2336 static int link_set_ipv6_forward(Link
*link
) {
2339 if (!link_ipv6_forward_enabled(link
))
2342 /* On Linux, the IPv6 stack does not know a per-interface
2343 * packet forwarding setting: either packet forwarding is on
2344 * for all, or off for all. We hence don't bother with a
2345 * per-interface setting, but simply propagate the interface
2346 * flag, if it is set, to the global flag, one-way. Note that
2347 * while IPv4 would allow a per-interface flag, we expose the
2348 * same behaviour there and also propagate the setting from
2349 * one to all, to keep things simple (see above). */
2351 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2353 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2358 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2359 IPv6PrivacyExtensions s
;
2362 s
= link_ipv6_privacy_extensions(link
);
2366 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2368 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2373 static int link_set_ipv6_accept_ra(Link
*link
) {
2376 /* Make this a NOP if IPv6 is not available */
2377 if (!socket_ipv6_is_supported())
2380 if (link
->flags
& IFF_LOOPBACK
)
2386 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2388 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2393 static int link_set_ipv6_dad_transmits(Link
*link
) {
2396 /* Make this a NOP if IPv6 is not available */
2397 if (!socket_ipv6_is_supported())
2400 if (link
->flags
& IFF_LOOPBACK
)
2406 if (link
->network
->ipv6_dad_transmits
< 0)
2409 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2411 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2416 static int link_set_ipv6_hop_limit(Link
*link
) {
2419 /* Make this a NOP if IPv6 is not available */
2420 if (!socket_ipv6_is_supported())
2423 if (link
->flags
& IFF_LOOPBACK
)
2429 if (link
->network
->ipv6_hop_limit
< 0)
2432 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2434 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2439 static int link_set_ipv6_mtu(Link
*link
) {
2442 /* Make this a NOP if IPv6 is not available */
2443 if (!socket_ipv6_is_supported())
2446 if (link
->flags
& IFF_LOOPBACK
)
2449 if (link
->network
->ipv6_mtu
== 0)
2452 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2454 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2459 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2460 Address
*net_address
;
2468 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2469 if (address_equal(net_address
, address
))
2475 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2484 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2485 if (route_equal(net_route
, route
))
2491 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2498 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2501 /* Even when the address is leased from a DHCP server, networkd assign the address
2502 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2503 * corresponding routes with RTPROT_DHCP. */
2504 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2505 if (route
->protocol
!= RTPROT_DHCP
)
2508 if (address
->family
!= route
->family
)
2511 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2518 static int link_drop_foreign_config(Link
*link
) {
2524 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2525 /* we consider IPv6LL addresses to be managed by the kernel */
2526 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2529 if (link_address_is_dynamic(link
, address
)) {
2530 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2532 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2535 if (link_is_static_address_configured(link
, address
)) {
2536 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2538 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2540 r
= address_remove(address
, link
, NULL
);
2546 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2547 /* do not touch routes managed by the kernel */
2548 if (route
->protocol
== RTPROT_KERNEL
)
2551 if (route
->protocol
== RTPROT_STATIC
&&
2552 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2555 if (route
->protocol
== RTPROT_DHCP
&&
2556 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2559 if (link_is_static_route_configured(link
, route
)) {
2560 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2564 r
= route_remove(route
, link
, NULL
);
2573 static int link_drop_config(Link
*link
) {
2574 Address
*address
, *pool_address
;
2579 SET_FOREACH(address
, link
->addresses
, i
) {
2580 /* we consider IPv6LL addresses to be managed by the kernel */
2581 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2584 r
= address_remove(address
, link
, NULL
);
2588 /* If this address came from an address pool, clean up the pool */
2589 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2590 if (address_equal(address
, pool_address
)) {
2591 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2592 address_free(pool_address
);
2598 SET_FOREACH(route
, link
->routes
, i
) {
2599 /* do not touch routes managed by the kernel */
2600 if (route
->protocol
== RTPROT_KERNEL
)
2603 r
= route_remove(route
, link
, NULL
);
2613 static int link_configure(Link
*link
) {
2617 assert(link
->network
);
2618 assert(link
->state
== LINK_STATE_INITIALIZED
);
2620 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
2621 return link_configure_can(link
);
2623 /* Drop foreign config, but ignore loopback or critical devices.
2624 * We do not want to remove loopback address or addresses used for root NFS. */
2625 if (!(link
->flags
& IFF_LOOPBACK
) &&
2626 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2627 r
= link_drop_foreign_config(link
);
2632 r
= link_set_proxy_arp(link
);
2636 r
= ipv6_proxy_ndp_addresses_configure(link
);
2640 r
= link_set_ipv4_forward(link
);
2644 r
= link_set_ipv6_forward(link
);
2648 r
= link_set_ipv6_privacy_extensions(link
);
2652 r
= link_set_ipv6_accept_ra(link
);
2656 r
= link_set_ipv6_dad_transmits(link
);
2660 r
= link_set_ipv6_hop_limit(link
);
2664 r
= link_set_flags(link
);
2668 r
= link_set_ipv6_mtu(link
);
2672 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2673 r
= ipv4ll_configure(link
);
2678 if (link_dhcp4_enabled(link
)) {
2679 r
= dhcp4_set_promote_secondaries(link
);
2683 r
= dhcp4_configure(link
);
2688 if (link_dhcp4_server_enabled(link
)) {
2689 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2693 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2698 if (link_dhcp6_enabled(link
) ||
2699 link_ipv6_accept_ra_enabled(link
)) {
2700 r
= dhcp6_configure(link
);
2705 if (link_ipv6_accept_ra_enabled(link
)) {
2706 r
= ndisc_configure(link
);
2711 if (link_radv_enabled(link
)) {
2712 r
= radv_configure(link
);
2717 if (link_lldp_rx_enabled(link
)) {
2718 r
= link_lldp_rx_configure(link
);
2723 r
= link_configure_mtu(link
);
2727 r
= link_configure_addrgen_mode(link
);
2731 return link_configure_after_setting_mtu(link
);
2734 static int link_configure_after_setting_mtu(Link
*link
) {
2738 assert(link
->network
);
2739 assert(link
->state
== LINK_STATE_INITIALIZED
);
2741 if (link
->setting_mtu
)
2744 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2745 r
= link_acquire_conf(link
);
2750 return link_enter_join_netdev(link
);
2753 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2756 if (duid
->raw_data_len
> 0)
2759 if (duid
->type
!= DUID_TYPE_UUID
)
2762 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2763 duid
->raw_data_len
= sizeof(sd_id128_t
);
2768 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2769 Manager
*manager
= userdata
;
2770 const sd_bus_error
*e
;
2780 e
= sd_bus_message_get_error(m
);
2782 log_error_errno(sd_bus_error_get_errno(e
),
2783 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2788 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2792 if (sz
!= sizeof(sd_id128_t
)) {
2793 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2797 memcpy(&manager
->product_uuid
, a
, sz
);
2798 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2799 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2801 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2804 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2805 r
= link_configure(link
);
2807 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2810 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2812 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2813 * even if the method fails. */
2814 manager
->has_product_uuid
= true;
2819 static bool link_requires_uuid(Link
*link
) {
2823 assert(link
->manager
);
2824 assert(link
->network
);
2826 duid
= link_get_duid(link
);
2827 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2830 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2833 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2839 static int link_configure_duid(Link
*link
) {
2845 assert(link
->manager
);
2846 assert(link
->network
);
2849 duid
= link_get_duid(link
);
2851 if (!link_requires_uuid(link
))
2854 if (m
->has_product_uuid
) {
2855 (void) duid_set_uuid(duid
, m
->product_uuid
);
2859 if (!m
->links_requesting_uuid
) {
2860 r
= manager_request_product_uuid(m
, link
);
2865 log_link_warning_errno(link
, r
,
2866 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2870 r
= set_put(m
->links_requesting_uuid
, link
);
2874 r
= set_put(m
->duids_requesting_uuid
, duid
);
2882 static int link_initialized_and_synced(Link
*link
) {
2887 assert(link
->ifname
);
2888 assert(link
->manager
);
2890 /* We may get called either from the asynchronous netlink callback,
2891 * or directly for link_add() if running in a container. See link_add(). */
2892 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2895 log_link_debug(link
, "Link state is up-to-date");
2896 link_set_state(link
, LINK_STATE_INITIALIZED
);
2898 r
= link_new_bound_by_list(link
);
2902 r
= link_handle_bound_by_list(link
);
2906 if (!link
->network
) {
2907 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2908 &link
->mac
, &network
);
2910 link_enter_unmanaged(link
);
2912 } else if (r
== 0 && network
->unmanaged
) {
2913 link_enter_unmanaged(link
);
2918 if (link
->flags
& IFF_LOOPBACK
) {
2919 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2920 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2922 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2923 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2925 if (network
->dhcp_server
)
2926 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2929 r
= network_apply(network
, link
);
2934 r
= link_new_bound_to_list(link
);
2938 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2939 * link_configure() is called later asynchronously. */
2940 r
= link_configure_duid(link
);
2944 r
= link_configure(link
);
2951 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2952 (void) link_initialized_and_synced(link
);
2956 int link_initialized(Link
*link
, sd_device
*device
) {
2957 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2961 assert(link
->manager
);
2962 assert(link
->manager
->rtnl
);
2965 if (link
->state
!= LINK_STATE_PENDING
)
2968 if (link
->sd_device
)
2971 log_link_debug(link
, "udev initialized link");
2972 link_set_state(link
, LINK_STATE_INITIALIZED
);
2974 link
->sd_device
= sd_device_ref(device
);
2976 /* udev has initialized the link, but we don't know if we have yet
2977 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2978 * when it returns we know that the pending NEWLINKs have already been
2979 * processed and that we are up-to-date */
2981 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2986 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2987 link_netlink_destroy_callback
, link
);
2996 static int link_load(Link
*link
) {
2997 _cleanup_free_
char *network_file
= NULL
,
3000 *dhcp4_address
= NULL
,
3001 *ipv4ll_address
= NULL
;
3002 union in_addr_union address
;
3003 union in_addr_union route_dst
;
3009 r
= parse_env_file(NULL
, link
->state_file
,
3010 "NETWORK_FILE", &network_file
,
3011 "ADDRESSES", &addresses
,
3013 "DHCP4_ADDRESS", &dhcp4_address
,
3014 "IPV4LL_ADDRESS", &ipv4ll_address
);
3015 if (r
< 0 && r
!= -ENOENT
)
3016 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3023 suffix
= strrchr(network_file
, '.');
3025 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3026 goto network_file_fail
;
3030 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3032 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3033 goto network_file_fail
;
3036 r
= network_apply(network
, link
);
3038 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3047 _cleanup_free_
char *address_str
= NULL
;
3048 char *prefixlen_str
;
3050 unsigned char prefixlen
;
3052 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3054 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3060 prefixlen_str
= strchr(address_str
, '/');
3061 if (!prefixlen_str
) {
3062 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3066 *prefixlen_str
++ = '\0';
3068 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3070 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3074 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3076 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3080 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3082 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3091 _cleanup_free_
char *route_str
= NULL
;
3092 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3094 char *prefixlen_str
;
3096 unsigned char prefixlen
, tos
, table
;
3099 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3101 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3107 prefixlen_str
= strchr(route_str
, '/');
3108 if (!prefixlen_str
) {
3109 log_link_debug(link
, "Failed to parse route %s", route_str
);
3113 *prefixlen_str
++ = '\0';
3115 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3117 log_link_debug(link
,
3118 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3123 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3125 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3129 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3131 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3133 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3134 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3135 0, route_expire_handler
, route
);
3137 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3140 route
->lifetime
= lifetime
;
3141 sd_event_source_unref(route
->expire
);
3142 route
->expire
= TAKE_PTR(expire
);
3146 if (dhcp4_address
) {
3147 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3149 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3150 goto dhcp4_address_fail
;
3153 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3155 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3157 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3159 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3164 if (ipv4ll_address
) {
3165 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3167 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3168 goto ipv4ll_address_fail
;
3171 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3173 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3175 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3177 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3180 ipv4ll_address_fail
:
3185 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3186 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3187 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3196 r
= link_new(m
, message
, ret
);
3202 log_link_debug(link
, "Link %d added", link
->ifindex
);
3204 r
= link_load(link
);
3208 if (detect_container() <= 0) {
3209 /* not in a container, udev will be around */
3210 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3211 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3213 log_link_warning_errno(link
, r
, "Could not find device: %m");
3217 r
= sd_device_get_is_initialized(device
);
3219 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3224 log_link_debug(link
, "link pending udev initialization...");
3228 r
= device_is_renaming(device
);
3230 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3234 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3238 r
= link_initialized(link
, device
);
3242 r
= link_initialized_and_synced(link
);
3249 link_enter_failed(link
);
3253 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3258 log_link_info(link
, "Gained IPv6LL");
3260 link
->ipv6ll_address
= *address
;
3261 link_check_ready(link
);
3263 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3264 r
= link_acquire_ipv6_conf(link
);
3266 link_enter_failed(link
);
3274 static int link_carrier_gained(Link
*link
) {
3279 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3280 r
= link_acquire_conf(link
);
3282 link_enter_failed(link
);
3286 r
= link_request_set_addresses(link
);
3291 r
= link_handle_bound_by_list(link
);
3298 static int link_carrier_lost(Link
*link
) {
3303 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3306 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3307 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3308 * configuration and stop the clients as well. */
3309 if (link
->setting_mtu
)
3312 r
= link_stop_clients(link
, false);
3314 link_enter_failed(link
);
3318 if (link_dhcp4_server_enabled(link
))
3319 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3321 r
= link_drop_config(link
);
3325 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3326 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3327 r
= link_drop_foreign_config(link
);
3332 r
= link_handle_bound_by_list(link
);
3339 int link_carrier_reset(Link
*link
) {
3344 if (link_has_carrier(link
)) {
3345 r
= link_carrier_lost(link
);
3349 r
= link_carrier_gained(link
);
3353 log_link_info(link
, "Reset carrier");
3359 int link_update(Link
*link
, sd_netlink_message
*m
) {
3360 struct ether_addr mac
;
3363 bool had_carrier
, carrier_gained
, carrier_lost
;
3367 assert(link
->ifname
);
3370 if (link
->state
== LINK_STATE_LINGER
) {
3371 log_link_info(link
, "Link re-added");
3372 link_set_state(link
, LINK_STATE_CONFIGURING
);
3374 r
= link_new_carrier_maps(link
);
3379 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3380 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3381 Manager
*manager
= link
->manager
;
3383 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3386 r
= link_add(manager
, m
, &link
);
3391 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3392 if (r
>= 0 && mtu
> 0) {
3394 if (link
->original_mtu
== 0) {
3395 link
->original_mtu
= mtu
;
3396 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3399 if (link
->dhcp_client
) {
3400 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3403 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3407 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3409 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3413 /* The kernel may broadcast NEWLINK messages without the MAC address
3414 set, simply ignore them. */
3415 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3417 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3420 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3423 log_link_debug(link
, "MAC address: "
3424 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3425 mac
.ether_addr_octet
[0],
3426 mac
.ether_addr_octet
[1],
3427 mac
.ether_addr_octet
[2],
3428 mac
.ether_addr_octet
[3],
3429 mac
.ether_addr_octet
[4],
3430 mac
.ether_addr_octet
[5]);
3433 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3435 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3438 if (link
->dhcp_client
) {
3439 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3440 (const uint8_t *) &link
->mac
,
3444 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3446 r
= dhcp4_set_client_identifier(link
);
3451 if (link
->dhcp6_client
) {
3452 const DUID
* duid
= link_get_duid(link
);
3454 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3455 (const uint8_t *) &link
->mac
,
3459 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3461 if (link
->network
->iaid_set
) {
3462 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3463 link
->network
->iaid
);
3465 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3468 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3470 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3471 duid
->raw_data_len
);
3473 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3477 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3479 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3483 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3485 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3490 old_master
= link
->master_ifindex
;
3491 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3493 had_carrier
= link_has_carrier(link
);
3495 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3499 r
= link_update_lldp(link
);
3503 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3504 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3506 if (carrier_gained
) {
3507 log_link_info(link
, "Gained carrier");
3509 r
= link_carrier_gained(link
);
3512 } else if (carrier_lost
) {
3513 log_link_info(link
, "Lost carrier");
3515 r
= link_carrier_lost(link
);
3523 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3531 if (hashmap_isempty(h
))
3535 HASHMAP_FOREACH(link
, h
, i
) {
3539 fprintf(f
, "%i", link
->ifindex
);
3546 int link_save(Link
*link
) {
3547 _cleanup_free_
char *temp_path
= NULL
;
3548 _cleanup_fclose_
FILE *f
= NULL
;
3549 const char *admin_state
, *oper_state
;
3556 assert(link
->state_file
);
3557 assert(link
->lease_file
);
3558 assert(link
->manager
);
3560 if (link
->state
== LINK_STATE_LINGER
) {
3561 (void) unlink(link
->state_file
);
3565 link_lldp_save(link
);
3567 admin_state
= link_state_to_string(link
->state
);
3568 assert(admin_state
);
3570 oper_state
= link_operstate_to_string(link
->operstate
);
3573 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3577 (void) fchmod(fileno(f
), 0644);
3580 "# This is private data. Do not parse.\n"
3583 admin_state
, oper_state
);
3585 if (link
->network
) {
3586 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3587 const char *dhcp_domainname
= NULL
, *p
;
3588 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3592 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3593 yes_no(link
->network
->required_for_online
));
3595 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3596 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3598 if (link
->dhcp6_client
) {
3599 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3600 if (r
< 0 && r
!= -ENOMSG
)
3601 log_link_debug(link
, "No DHCPv6 lease");
3604 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3609 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3610 _cleanup_free_
char *b
= NULL
;
3612 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3613 &link
->network
->dns
[j
].address
, &b
);
3615 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3625 if (link
->network
->dhcp_use_dns
&&
3627 const struct in_addr
*addresses
;
3629 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3631 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3635 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3636 struct in6_addr
*in6_addrs
;
3638 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3642 serialize_in6_addrs(f
, in6_addrs
, r
);
3647 /* Make sure to flush out old entries before we use the NDISC data */
3650 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3653 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3657 serialize_in6_addrs(f
, &dd
->address
, 1);
3666 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3668 if (link
->network
->dhcp_use_ntp
&&
3670 const struct in_addr
*addresses
;
3672 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3674 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3678 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3679 struct in6_addr
*in6_addrs
;
3682 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3687 serialize_in6_addrs(f
, in6_addrs
, r
);
3691 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3693 fputstrv(f
, hosts
, NULL
, &space
);
3698 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3699 if (link
->dhcp_lease
) {
3700 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3701 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3704 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3707 fputs("DOMAINS=", f
);
3709 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3710 fputs_with_space(f
, p
, NULL
, &space
);
3712 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3715 if (dhcp_domainname
)
3716 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3718 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3720 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3722 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3723 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3728 fputs("ROUTE_DOMAINS=", f
);
3730 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3731 fputs_with_space(f
, p
, NULL
, &space
);
3733 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3736 if (dhcp_domainname
)
3737 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3739 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3741 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3743 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3744 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3749 fprintf(f
, "LLMNR=%s\n",
3750 resolve_support_to_string(link
->network
->llmnr
));
3751 fprintf(f
, "MDNS=%s\n",
3752 resolve_support_to_string(link
->network
->mdns
));
3753 if (link
->network
->dns_default_route
>= 0)
3754 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3756 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3757 fprintf(f
, "DNS_OVER_TLS=%s\n",
3758 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3760 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3761 fprintf(f
, "DNSSEC=%s\n",
3762 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3764 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3767 fputs("DNSSEC_NTA=", f
);
3769 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3770 fputs_with_space(f
, n
, NULL
, &space
);
3774 fputs("ADDRESSES=", f
);
3776 SET_FOREACH(a
, link
->addresses
, i
) {
3777 _cleanup_free_
char *address_str
= NULL
;
3779 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3783 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3788 fputs("ROUTES=", f
);
3790 SET_FOREACH(route
, link
->routes
, i
) {
3791 _cleanup_free_
char *route_str
= NULL
;
3793 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3797 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3798 space
? " " : "", route_str
,
3799 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3806 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3807 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3809 if (link
->dhcp_lease
) {
3810 struct in_addr address
;
3811 const char *tz
= NULL
;
3813 assert(link
->network
);
3815 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3817 fprintf(f
, "TIMEZONE=%s\n", tz
);
3819 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3821 fputs("DHCP4_ADDRESS=", f
);
3822 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3826 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3834 (void) unlink(link
->lease_file
);
3837 struct in_addr address
;
3839 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3841 fputs("IPV4LL_ADDRESS=", f
);
3842 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3847 r
= fflush_and_check(f
);
3851 if (rename(temp_path
, link
->state_file
) < 0) {
3859 (void) unlink(link
->state_file
);
3861 (void) unlink(temp_path
);
3863 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3866 /* The serialized state in /run is no longer up-to-date. */
3867 void link_dirty(Link
*link
) {
3872 /* mark manager dirty as link is dirty */
3873 manager_dirty(link
->manager
);
3875 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3877 /* allocation errors are ignored */
3880 r
= set_put(link
->manager
->dirty_links
, link
);
3882 /* don't take another ref if the link was already dirty */
3888 /* The serialized state in /run is up-to-date */
3889 void link_clean(Link
*link
) {
3891 assert(link
->manager
);
3893 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3896 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3897 [LINK_STATE_PENDING
] = "pending",
3898 [LINK_STATE_INITIALIZED
] = "initialized",
3899 [LINK_STATE_CONFIGURING
] = "configuring",
3900 [LINK_STATE_CONFIGURED
] = "configured",
3901 [LINK_STATE_UNMANAGED
] = "unmanaged",
3902 [LINK_STATE_FAILED
] = "failed",
3903 [LINK_STATE_LINGER
] = "linger",
3906 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);