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_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
965 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
968 if (link_ipv6ll_enabled(link
) &&
969 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
972 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
973 !link
->dhcp4_configured
&&
974 !link
->dhcp6_configured
&&
975 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
976 /* When DHCP is enabled, at least one protocol must provide an address, or
977 * an IPv4ll fallback address must be configured. */
980 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
984 if (link
->state
!= LINK_STATE_CONFIGURED
)
985 link_enter_configured(link
);
990 static int link_request_set_neighbors(Link
*link
) {
995 assert(link
->network
);
996 assert(link
->state
!= _LINK_STATE_INVALID
);
998 link_set_state(link
, LINK_STATE_CONFIGURING
);
999 link
->neighbors_configured
= false;
1001 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1002 r
= neighbor_configure(neighbor
, link
, NULL
);
1004 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1005 link_enter_failed(link
);
1010 if (link
->neighbor_messages
== 0) {
1011 link
->neighbors_configured
= true;
1012 link_check_ready(link
);
1014 log_link_debug(link
, "Setting neighbors");
1019 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1025 assert(link
->ifname
);
1026 assert(link
->address_messages
> 0);
1027 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1028 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1030 link
->address_messages
--;
1032 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1035 r
= sd_netlink_message_get_errno(m
);
1036 if (r
< 0 && r
!= -EEXIST
)
1037 log_link_warning_errno(link
, r
, "could not set address: %m");
1039 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1041 if (link
->address_messages
== 0) {
1042 log_link_debug(link
, "Addresses set");
1043 link
->addresses_configured
= true;
1044 link_check_ready(link
);
1050 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1051 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1052 size_t n_addresses
= 0, n_allocated
= 0;
1055 log_debug("Copying DNS server information from %s", link
->ifname
);
1060 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1063 /* Only look for IPv4 addresses */
1064 if (link
->network
->dns
[i
].family
!= AF_INET
)
1067 ia
= link
->network
->dns
[i
].address
.in
;
1069 /* Never propagate obviously borked data */
1070 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1073 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1076 addresses
[n_addresses
++] = ia
;
1079 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1080 const struct in_addr
*da
= NULL
;
1083 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1086 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1089 for (j
= 0; j
< n
; j
++)
1090 if (in4_addr_is_non_local(&da
[j
]))
1091 addresses
[n_addresses
++] = da
[j
];
1095 if (n_addresses
<= 0)
1098 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1101 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1102 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1103 size_t n_addresses
= 0, n_allocated
= 0;
1109 log_debug("Copying NTP server information from %s", link
->ifname
);
1111 STRV_FOREACH(a
, link
->network
->ntp
) {
1112 union in_addr_union ia
;
1114 /* Only look for IPv4 addresses */
1115 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1118 /* Never propagate obviously borked data */
1119 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1122 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1125 addresses
[n_addresses
++] = ia
.in
;
1128 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1129 const struct in_addr
*da
= NULL
;
1132 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1135 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1138 for (j
= 0; j
< n
; j
++)
1139 if (in4_addr_is_non_local(&da
[j
]))
1140 addresses
[n_addresses
++] = da
[j
];
1144 if (n_addresses
<= 0)
1147 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1150 static int link_set_bridge_fdb(Link
*link
) {
1151 FdbEntry
*fdb_entry
;
1154 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1155 r
= fdb_entry_configure(link
, fdb_entry
);
1157 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1163 static int link_request_set_addresses(Link
*link
) {
1164 AddressLabel
*label
;
1169 assert(link
->network
);
1170 assert(link
->state
!= _LINK_STATE_INVALID
);
1172 link_set_state(link
, LINK_STATE_CONFIGURING
);
1174 /* Reset all *_configured flags we are configuring. */
1175 link
->addresses_configured
= false;
1176 link
->addresses_ready
= false;
1177 link
->neighbors_configured
= false;
1178 link
->static_routes_configured
= false;
1179 link
->routing_policy_rules_configured
= false;
1181 r
= link_set_bridge_fdb(link
);
1185 r
= link_request_set_neighbors(link
);
1189 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1192 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1194 r
= address_configure(ad
, link
, address_handler
, update
);
1196 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1197 link_enter_failed(link
);
1201 link
->address_messages
++;
1204 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1205 r
= address_label_configure(label
, link
, NULL
, false);
1207 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1208 link_enter_failed(link
);
1212 link
->address_label_messages
++;
1215 /* now that we can figure out a default address for the dhcp server,
1217 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1219 Link
*uplink
= NULL
;
1220 bool acquired_uplink
= false;
1222 address
= link_find_dhcp_server_address(link
);
1224 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1225 link_enter_failed(link
);
1229 /* use the server address' subnet as the pool */
1230 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1231 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1236 r = sd_dhcp_server_set_router(link->dhcp_server,
1237 &main_address->in_addr.in);
1242 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1243 r
= sd_dhcp_server_set_max_lease_time(
1245 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1250 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1251 r
= sd_dhcp_server_set_default_lease_time(
1253 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1258 if (link
->network
->dhcp_server_emit_dns
) {
1260 if (link
->network
->n_dhcp_server_dns
> 0)
1261 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1263 uplink
= manager_find_uplink(link
->manager
, link
);
1264 acquired_uplink
= true;
1267 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1270 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1273 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1276 if (link
->network
->dhcp_server_emit_ntp
) {
1278 if (link
->network
->n_dhcp_server_ntp
> 0)
1279 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1281 if (!acquired_uplink
)
1282 uplink
= manager_find_uplink(link
->manager
, link
);
1285 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1288 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1292 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1295 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1297 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1299 if (link
->network
->dhcp_server_emit_timezone
) {
1300 _cleanup_free_
char *buffer
= NULL
;
1301 const char *tz
= NULL
;
1303 if (link
->network
->dhcp_server_timezone
)
1304 tz
= link
->network
->dhcp_server_timezone
;
1306 r
= get_timezone(&buffer
);
1308 log_warning_errno(r
, "Failed to determine timezone: %m");
1314 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1319 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1320 r
= sd_dhcp_server_start(link
->dhcp_server
);
1322 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1324 link_enter_failed(link
);
1330 log_link_debug(link
, "Offering DHCPv4 leases");
1333 if (link
->address_messages
== 0) {
1334 link
->addresses_configured
= true;
1335 link_check_ready(link
);
1337 log_link_debug(link
, "Setting addresses");
1342 static int link_set_bridge_vlan(Link
*link
) {
1345 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1347 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1352 static int link_set_proxy_arp(Link
*link
) {
1355 if (!link_proxy_arp_enabled(link
))
1358 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1360 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1365 static int link_configure_after_setting_mtu(Link
*link
);
1367 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1372 assert(link
->ifname
);
1374 link
->setting_mtu
= false;
1376 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1379 r
= sd_netlink_message_get_errno(m
);
1381 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1385 log_link_debug(link
, "Setting MTU done.");
1387 if (link
->state
== LINK_STATE_INITIALIZED
)
1388 (void) link_configure_after_setting_mtu(link
);
1393 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1394 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1398 assert(link
->manager
);
1399 assert(link
->manager
->rtnl
);
1401 if (mtu
== 0 || link
->setting_mtu
)
1404 if (link
->mtu
== mtu
)
1407 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1409 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1411 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1413 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1414 * for this interface, then disable IPv6 else enable it. */
1415 (void) link_enable_ipv6(link
);
1417 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1418 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1419 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1421 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1422 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1427 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1429 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1431 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1432 link_netlink_destroy_callback
, link
);
1434 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1437 link
->setting_mtu
= true;
1442 static bool link_reduces_vlan_mtu(Link
*link
) {
1443 /* See netif_reduces_vlan_mtu() in kernel. */
1444 return streq_ptr(link
->kind
, "macsec");
1447 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1452 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1453 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1454 /* See vlan_dev_change_mtu() in kernel. */
1455 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1457 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1458 /* See macvlan_change_mtu() in kernel. */
1464 static int link_configure_mtu(Link
*link
) {
1468 assert(link
->network
);
1470 if (link
->network
->mtu
> 0)
1471 return link_set_mtu(link
, link
->network
->mtu
);
1473 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1474 if (link
->mtu
>= mtu
)
1477 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1478 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1481 return link_set_mtu(link
, mtu
);
1484 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1489 assert(link
->ifname
);
1491 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1494 r
= sd_netlink_message_get_errno(m
);
1496 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1501 static int link_set_flags(Link
*link
) {
1502 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1503 unsigned ifi_change
= 0;
1504 unsigned ifi_flags
= 0;
1508 assert(link
->manager
);
1509 assert(link
->manager
->rtnl
);
1511 if (link
->flags
& IFF_LOOPBACK
)
1517 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1520 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1522 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1524 if (link
->network
->arp
>= 0) {
1525 ifi_change
|= IFF_NOARP
;
1526 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1529 if (link
->network
->multicast
>= 0) {
1530 ifi_change
|= IFF_MULTICAST
;
1531 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1534 if (link
->network
->allmulticast
>= 0) {
1535 ifi_change
|= IFF_ALLMULTI
;
1536 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1539 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1541 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1543 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1544 link_netlink_destroy_callback
, link
);
1546 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1553 static int link_acquire_ipv6_conf(Link
*link
) {
1558 if (link_ipv6_accept_ra_enabled(link
)) {
1559 assert(link
->ndisc
);
1561 log_link_debug(link
, "Discovering IPv6 routers");
1563 r
= sd_ndisc_start(link
->ndisc
);
1564 if (r
< 0 && r
!= -EBUSY
)
1565 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1568 if (link_radv_enabled(link
)) {
1570 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1572 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1574 r
= sd_radv_start(link
->radv
);
1575 if (r
< 0 && r
!= -EBUSY
)
1576 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1579 (void) dhcp6_request_prefix_delegation(link
);
1584 static int link_acquire_ipv4_conf(Link
*link
) {
1588 assert(link
->manager
);
1589 assert(link
->manager
->event
);
1591 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1592 assert(link
->ipv4ll
);
1594 log_link_debug(link
, "Acquiring IPv4 link-local address");
1596 r
= sd_ipv4ll_start(link
->ipv4ll
);
1598 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1601 if (link_dhcp4_enabled(link
)) {
1602 assert(link
->dhcp_client
);
1604 log_link_debug(link
, "Acquiring DHCPv4 lease");
1606 r
= sd_dhcp_client_start(link
->dhcp_client
);
1608 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1614 static int link_acquire_conf(Link
*link
) {
1619 r
= link_acquire_ipv4_conf(link
);
1623 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1624 r
= link_acquire_ipv6_conf(link
);
1629 if (link_lldp_emit_enabled(link
)) {
1630 r
= link_lldp_emit_start(link
);
1632 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1638 bool link_has_carrier(Link
*link
) {
1639 /* see Documentation/networking/operstates.txt in the kernel sources */
1641 if (link
->kernel_operstate
== IF_OPER_UP
)
1644 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1645 /* operstate may not be implemented, so fall back to flags */
1646 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1647 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1653 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1658 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1661 r
= sd_netlink_message_get_errno(m
);
1663 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1668 static int link_configure_addrgen_mode(Link
*link
) {
1669 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1670 uint8_t ipv6ll_mode
;
1674 assert(link
->network
);
1675 assert(link
->manager
);
1676 assert(link
->manager
->rtnl
);
1678 if (!socket_ipv6_is_supported())
1681 log_link_debug(link
, "Setting address genmode for link");
1683 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1685 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1687 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1689 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1691 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1693 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1695 if (!link_ipv6ll_enabled(link
))
1696 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1697 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1698 /* The file may not exist. And event if it exists, when stable_secret is unset,
1699 * reading the file fails with EIO. */
1700 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1702 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1704 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1706 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1708 r
= sd_netlink_message_close_container(req
);
1710 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1712 r
= sd_netlink_message_close_container(req
);
1714 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1716 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1717 link_netlink_destroy_callback
, link
);
1719 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1726 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1731 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1734 r
= sd_netlink_message_get_errno(m
);
1736 /* we warn but don't fail the link, as it may be brought up later */
1737 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1742 static int link_up(Link
*link
) {
1743 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1747 assert(link
->network
);
1748 assert(link
->manager
);
1749 assert(link
->manager
->rtnl
);
1751 log_link_debug(link
, "Bringing link up");
1753 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1755 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1757 /* set it free if not enslaved with networkd */
1758 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1759 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1761 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1764 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1766 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1768 if (link
->network
->mac
) {
1769 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1771 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1774 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1775 link_netlink_destroy_callback
, link
);
1777 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1784 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1789 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1792 r
= sd_netlink_message_get_errno(m
);
1794 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1799 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1800 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1804 assert(link
->manager
);
1805 assert(link
->manager
->rtnl
);
1807 log_link_debug(link
, "Bringing link down");
1809 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1810 RTM_SETLINK
, link
->ifindex
);
1812 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1814 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1816 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1818 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1819 callback
?: link_down_handler
,
1820 link_netlink_destroy_callback
, link
);
1822 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1829 static int link_handle_bound_to_list(Link
*link
) {
1833 bool required_up
= false;
1834 bool link_is_up
= false;
1838 if (hashmap_isempty(link
->bound_to_links
))
1841 if (link
->flags
& IFF_UP
)
1844 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1845 if (link_has_carrier(l
)) {
1850 if (!required_up
&& link_is_up
) {
1851 r
= link_down(link
, NULL
);
1854 } else if (required_up
&& !link_is_up
) {
1863 static int link_handle_bound_by_list(Link
*link
) {
1870 if (hashmap_isempty(link
->bound_by_links
))
1873 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1874 r
= link_handle_bound_to_list(l
);
1882 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1888 if (link
== carrier
)
1891 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1894 r
= hashmap_ensure_allocated(h
, NULL
);
1898 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1905 static int link_new_bound_by_list(Link
*link
) {
1910 bool list_updated
= false;
1913 assert(link
->manager
);
1917 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1918 if (!carrier
->network
)
1921 if (strv_isempty(carrier
->network
->bind_carrier
))
1924 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1925 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1929 list_updated
= true;
1936 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1937 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1941 link_dirty(carrier
);
1947 static int link_new_bound_to_list(Link
*link
) {
1952 bool list_updated
= false;
1955 assert(link
->manager
);
1960 if (strv_isempty(link
->network
->bind_carrier
))
1965 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1966 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1967 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1971 list_updated
= true;
1978 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1979 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1983 link_dirty(carrier
);
1989 static int link_new_carrier_maps(Link
*link
) {
1992 r
= link_new_bound_by_list(link
);
1996 r
= link_handle_bound_by_list(link
);
2000 r
= link_new_bound_to_list(link
);
2004 r
= link_handle_bound_to_list(link
);
2011 static void link_free_bound_to_list(Link
*link
) {
2015 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2016 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2018 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2019 link_dirty(bound_to
);
2025 static void link_free_bound_by_list(Link
*link
) {
2029 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2030 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2032 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2033 link_dirty(bound_by
);
2034 link_handle_bound_to_list(bound_by
);
2041 static void link_free_carrier_maps(Link
*link
) {
2042 bool list_updated
= false;
2046 if (!hashmap_isempty(link
->bound_to_links
)) {
2047 link_free_bound_to_list(link
);
2048 list_updated
= true;
2051 if (!hashmap_isempty(link
->bound_by_links
)) {
2052 link_free_bound_by_list(link
);
2053 list_updated
= true;
2062 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2069 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2073 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2077 r
= set_put(master
->slaves
, link
);
2085 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2090 if (!link
->manager
|| !netdev
)
2093 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2096 link_unref(set_remove(master
->slaves
, link
));
2099 static void link_detach_from_manager(Link
*link
) {
2100 if (!link
|| !link
->manager
)
2103 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2106 /* The following must be called at last. */
2107 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2111 void link_drop(Link
*link
) {
2112 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2115 link_set_state(link
, LINK_STATE_LINGER
);
2117 link_free_carrier_maps(link
);
2119 if (link
->network
) {
2120 link_drop_from_master(link
, link
->network
->bridge
);
2121 link_drop_from_master(link
, link
->network
->bond
);
2124 log_link_debug(link
, "Link removed");
2126 (void) unlink(link
->state_file
);
2127 link_detach_from_manager(link
);
2130 static int link_joined(Link
*link
) {
2134 assert(link
->network
);
2136 if (!hashmap_isempty(link
->bound_to_links
)) {
2137 r
= link_handle_bound_to_list(link
);
2140 } else if (!(link
->flags
& IFF_UP
)) {
2143 link_enter_failed(link
);
2148 if (link
->network
->bridge
) {
2149 r
= link_set_bridge(link
);
2151 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2153 r
= link_append_to_master(link
, link
->network
->bridge
);
2155 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2158 if (link
->network
->bond
) {
2159 r
= link_set_bond(link
);
2161 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2163 r
= link_append_to_master(link
, link
->network
->bond
);
2165 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2168 if (link
->network
->use_br_vlan
&&
2169 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2170 r
= link_set_bridge_vlan(link
);
2172 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2175 /* Skip setting up addresses until it gets carrier,
2176 or it would try to set addresses twice,
2177 which is bad for non-idempotent steps. */
2178 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2181 return link_request_set_addresses(link
);
2184 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2188 assert(link
->network
);
2189 assert(link
->enslaving
> 0);
2193 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2196 r
= sd_netlink_message_get_errno(m
);
2197 if (r
< 0 && r
!= -EEXIST
) {
2198 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2199 link_enter_failed(link
);
2202 log_link_debug(link
, "Joined netdev");
2204 if (link
->enslaving
== 0) {
2211 static int link_enter_join_netdev(Link
*link
) {
2217 assert(link
->network
);
2218 assert(link
->state
== LINK_STATE_INITIALIZED
);
2220 link_set_state(link
, LINK_STATE_CONFIGURING
);
2223 link
->enslaving
= 0;
2225 if (link
->network
->bond
) {
2226 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2227 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2228 return link_joined(link
);
2230 log_struct(LOG_DEBUG
,
2231 LOG_LINK_INTERFACE(link
),
2232 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2233 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2237 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2239 log_struct_errno(LOG_WARNING
, r
,
2240 LOG_LINK_INTERFACE(link
),
2241 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2242 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2243 link_enter_failed(link
);
2248 if (link
->network
->bridge
) {
2249 log_struct(LOG_DEBUG
,
2250 LOG_LINK_INTERFACE(link
),
2251 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2252 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2256 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2258 log_struct_errno(LOG_WARNING
, r
,
2259 LOG_LINK_INTERFACE(link
),
2260 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2261 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2262 link_enter_failed(link
);
2267 if (link
->network
->vrf
) {
2268 log_struct(LOG_DEBUG
,
2269 LOG_LINK_INTERFACE(link
),
2270 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2271 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2275 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2277 log_struct_errno(LOG_WARNING
, r
,
2278 LOG_LINK_INTERFACE(link
),
2279 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2280 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2281 link_enter_failed(link
);
2286 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2288 if (netdev
->ifindex
> 0)
2289 /* Assume already enslaved. */
2292 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2295 log_struct(LOG_DEBUG
,
2296 LOG_LINK_INTERFACE(link
),
2297 LOG_NETDEV_INTERFACE(netdev
),
2298 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2302 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2304 log_struct_errno(LOG_WARNING
, r
,
2305 LOG_LINK_INTERFACE(link
),
2306 LOG_NETDEV_INTERFACE(netdev
),
2307 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2308 link_enter_failed(link
);
2313 if (link
->enslaving
== 0)
2314 return link_joined(link
);
2319 static int link_set_ipv4_forward(Link
*link
) {
2322 if (!link_ipv4_forward_enabled(link
))
2325 /* We propagate the forwarding flag from one interface to the
2326 * global setting one way. This means: as long as at least one
2327 * interface was configured at any time that had IP forwarding
2328 * enabled the setting will stay on for good. We do this
2329 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2330 * somewhat in sync (see below). */
2332 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2334 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2339 static int link_set_ipv6_forward(Link
*link
) {
2342 if (!link_ipv6_forward_enabled(link
))
2345 /* On Linux, the IPv6 stack does not know a per-interface
2346 * packet forwarding setting: either packet forwarding is on
2347 * for all, or off for all. We hence don't bother with a
2348 * per-interface setting, but simply propagate the interface
2349 * flag, if it is set, to the global flag, one-way. Note that
2350 * while IPv4 would allow a per-interface flag, we expose the
2351 * same behaviour there and also propagate the setting from
2352 * one to all, to keep things simple (see above). */
2354 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2356 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2361 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2362 IPv6PrivacyExtensions s
;
2365 s
= link_ipv6_privacy_extensions(link
);
2369 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2371 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2376 static int link_set_ipv6_accept_ra(Link
*link
) {
2379 /* Make this a NOP if IPv6 is not available */
2380 if (!socket_ipv6_is_supported())
2383 if (link
->flags
& IFF_LOOPBACK
)
2389 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2391 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2396 static int link_set_ipv6_dad_transmits(Link
*link
) {
2399 /* Make this a NOP if IPv6 is not available */
2400 if (!socket_ipv6_is_supported())
2403 if (link
->flags
& IFF_LOOPBACK
)
2409 if (link
->network
->ipv6_dad_transmits
< 0)
2412 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2414 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2419 static int link_set_ipv6_hop_limit(Link
*link
) {
2422 /* Make this a NOP if IPv6 is not available */
2423 if (!socket_ipv6_is_supported())
2426 if (link
->flags
& IFF_LOOPBACK
)
2432 if (link
->network
->ipv6_hop_limit
< 0)
2435 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2437 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2442 static int link_set_ipv6_mtu(Link
*link
) {
2445 /* Make this a NOP if IPv6 is not available */
2446 if (!socket_ipv6_is_supported())
2449 if (link
->flags
& IFF_LOOPBACK
)
2452 if (link
->network
->ipv6_mtu
== 0)
2455 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2457 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2462 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2463 Address
*net_address
;
2471 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2472 if (address_equal(net_address
, address
))
2478 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2487 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2488 if (route_equal(net_route
, route
))
2494 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2501 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2504 /* Even when the address is leased from a DHCP server, networkd assign the address
2505 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2506 * corresponding routes with RTPROT_DHCP. */
2507 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2508 if (route
->protocol
!= RTPROT_DHCP
)
2511 if (address
->family
!= route
->family
)
2514 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2521 static int link_drop_foreign_config(Link
*link
) {
2527 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2528 /* we consider IPv6LL addresses to be managed by the kernel */
2529 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2532 if (link_address_is_dynamic(link
, address
)) {
2533 if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2535 } else if (FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2538 if (link_is_static_address_configured(link
, address
)) {
2539 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2541 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2543 r
= address_remove(address
, link
, NULL
);
2549 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2550 /* do not touch routes managed by the kernel */
2551 if (route
->protocol
== RTPROT_KERNEL
)
2554 if (route
->protocol
== RTPROT_STATIC
&&
2555 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2558 if (route
->protocol
== RTPROT_DHCP
&&
2559 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2562 if (link_is_static_route_configured(link
, route
)) {
2563 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2567 r
= route_remove(route
, link
, NULL
);
2576 static int link_drop_config(Link
*link
) {
2577 Address
*address
, *pool_address
;
2582 SET_FOREACH(address
, link
->addresses
, i
) {
2583 /* we consider IPv6LL addresses to be managed by the kernel */
2584 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2587 r
= address_remove(address
, link
, NULL
);
2591 /* If this address came from an address pool, clean up the pool */
2592 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2593 if (address_equal(address
, pool_address
)) {
2594 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2595 address_free(pool_address
);
2601 SET_FOREACH(route
, link
->routes
, i
) {
2602 /* do not touch routes managed by the kernel */
2603 if (route
->protocol
== RTPROT_KERNEL
)
2606 r
= route_remove(route
, link
, NULL
);
2616 static int link_configure(Link
*link
) {
2620 assert(link
->network
);
2621 assert(link
->state
== LINK_STATE_INITIALIZED
);
2623 if (STRPTR_IN_SET(link
->kind
, "can", "vcan", "vxcan"))
2624 return link_configure_can(link
);
2626 /* Drop foreign config, but ignore loopback or critical devices.
2627 * We do not want to remove loopback address or addresses used for root NFS. */
2628 if (!(link
->flags
& IFF_LOOPBACK
) &&
2629 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2630 r
= link_drop_foreign_config(link
);
2635 r
= link_set_proxy_arp(link
);
2639 r
= ipv6_proxy_ndp_addresses_configure(link
);
2643 r
= link_set_ipv4_forward(link
);
2647 r
= link_set_ipv6_forward(link
);
2651 r
= link_set_ipv6_privacy_extensions(link
);
2655 r
= link_set_ipv6_accept_ra(link
);
2659 r
= link_set_ipv6_dad_transmits(link
);
2663 r
= link_set_ipv6_hop_limit(link
);
2667 r
= link_set_flags(link
);
2671 r
= link_set_ipv6_mtu(link
);
2675 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2676 r
= ipv4ll_configure(link
);
2681 if (link_dhcp4_enabled(link
)) {
2682 r
= dhcp4_set_promote_secondaries(link
);
2686 r
= dhcp4_configure(link
);
2691 if (link_dhcp4_server_enabled(link
)) {
2692 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2696 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2701 if (link_dhcp6_enabled(link
) ||
2702 link_ipv6_accept_ra_enabled(link
)) {
2703 r
= dhcp6_configure(link
);
2708 if (link_ipv6_accept_ra_enabled(link
)) {
2709 r
= ndisc_configure(link
);
2714 if (link_radv_enabled(link
)) {
2715 r
= radv_configure(link
);
2720 if (link_lldp_rx_enabled(link
)) {
2721 r
= link_lldp_rx_configure(link
);
2726 r
= link_configure_mtu(link
);
2730 r
= link_configure_addrgen_mode(link
);
2734 return link_configure_after_setting_mtu(link
);
2737 static int link_configure_after_setting_mtu(Link
*link
) {
2741 assert(link
->network
);
2742 assert(link
->state
== LINK_STATE_INITIALIZED
);
2744 if (link
->setting_mtu
)
2747 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2748 r
= link_acquire_conf(link
);
2753 return link_enter_join_netdev(link
);
2756 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2759 if (duid
->raw_data_len
> 0)
2762 if (duid
->type
!= DUID_TYPE_UUID
)
2765 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2766 duid
->raw_data_len
= sizeof(sd_id128_t
);
2771 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2772 Manager
*manager
= userdata
;
2773 const sd_bus_error
*e
;
2783 e
= sd_bus_message_get_error(m
);
2785 log_error_errno(sd_bus_error_get_errno(e
),
2786 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2791 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2795 if (sz
!= sizeof(sd_id128_t
)) {
2796 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2800 memcpy(&manager
->product_uuid
, a
, sz
);
2801 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2802 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2804 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2807 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2808 r
= link_configure(link
);
2810 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2813 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2815 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2816 * even if the method fails. */
2817 manager
->has_product_uuid
= true;
2822 static bool link_requires_uuid(Link
*link
) {
2826 assert(link
->manager
);
2827 assert(link
->network
);
2829 duid
= link_get_duid(link
);
2830 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2833 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2836 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2842 static int link_configure_duid(Link
*link
) {
2848 assert(link
->manager
);
2849 assert(link
->network
);
2852 duid
= link_get_duid(link
);
2854 if (!link_requires_uuid(link
))
2857 if (m
->has_product_uuid
) {
2858 (void) duid_set_uuid(duid
, m
->product_uuid
);
2862 if (!m
->links_requesting_uuid
) {
2863 r
= manager_request_product_uuid(m
, link
);
2868 log_link_warning_errno(link
, r
,
2869 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2873 r
= set_put(m
->links_requesting_uuid
, link
);
2877 r
= set_put(m
->duids_requesting_uuid
, duid
);
2885 static int link_initialized_and_synced(Link
*link
) {
2890 assert(link
->ifname
);
2891 assert(link
->manager
);
2893 /* We may get called either from the asynchronous netlink callback,
2894 * or directly for link_add() if running in a container. See link_add(). */
2895 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2898 log_link_debug(link
, "Link state is up-to-date");
2899 link_set_state(link
, LINK_STATE_INITIALIZED
);
2901 r
= link_new_bound_by_list(link
);
2905 r
= link_handle_bound_by_list(link
);
2909 if (!link
->network
) {
2910 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2911 &link
->mac
, &network
);
2913 link_enter_unmanaged(link
);
2915 } else if (r
== 0 && network
->unmanaged
) {
2916 link_enter_unmanaged(link
);
2921 if (link
->flags
& IFF_LOOPBACK
) {
2922 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2923 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2925 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2926 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2928 if (network
->dhcp_server
)
2929 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2932 r
= network_apply(network
, link
);
2937 r
= link_new_bound_to_list(link
);
2941 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2942 * link_configure() is called later asynchronously. */
2943 r
= link_configure_duid(link
);
2947 r
= link_configure(link
);
2954 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2955 (void) link_initialized_and_synced(link
);
2959 int link_initialized(Link
*link
, sd_device
*device
) {
2960 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2964 assert(link
->manager
);
2965 assert(link
->manager
->rtnl
);
2968 if (link
->state
!= LINK_STATE_PENDING
)
2971 if (link
->sd_device
)
2974 log_link_debug(link
, "udev initialized link");
2975 link_set_state(link
, LINK_STATE_INITIALIZED
);
2977 link
->sd_device
= sd_device_ref(device
);
2979 /* udev has initialized the link, but we don't know if we have yet
2980 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2981 * when it returns we know that the pending NEWLINKs have already been
2982 * processed and that we are up-to-date */
2984 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2989 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2990 link_netlink_destroy_callback
, link
);
2999 static int link_load(Link
*link
) {
3000 _cleanup_free_
char *network_file
= NULL
,
3003 *dhcp4_address
= NULL
,
3004 *ipv4ll_address
= NULL
;
3005 union in_addr_union address
;
3006 union in_addr_union route_dst
;
3012 r
= parse_env_file(NULL
, link
->state_file
,
3013 "NETWORK_FILE", &network_file
,
3014 "ADDRESSES", &addresses
,
3016 "DHCP4_ADDRESS", &dhcp4_address
,
3017 "IPV4LL_ADDRESS", &ipv4ll_address
);
3018 if (r
< 0 && r
!= -ENOENT
)
3019 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3026 suffix
= strrchr(network_file
, '.');
3028 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3029 goto network_file_fail
;
3033 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3035 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3036 goto network_file_fail
;
3039 r
= network_apply(network
, link
);
3041 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3050 _cleanup_free_
char *address_str
= NULL
;
3051 char *prefixlen_str
;
3053 unsigned char prefixlen
;
3055 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3057 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3063 prefixlen_str
= strchr(address_str
, '/');
3064 if (!prefixlen_str
) {
3065 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3069 *prefixlen_str
++ = '\0';
3071 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3073 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3077 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3079 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3083 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3085 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3094 _cleanup_free_
char *route_str
= NULL
;
3095 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3097 char *prefixlen_str
;
3099 unsigned char prefixlen
, tos
, table
;
3102 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3104 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3110 prefixlen_str
= strchr(route_str
, '/');
3111 if (!prefixlen_str
) {
3112 log_link_debug(link
, "Failed to parse route %s", route_str
);
3116 *prefixlen_str
++ = '\0';
3118 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3120 log_link_debug(link
,
3121 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3126 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3128 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3132 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3134 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3136 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3137 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3138 0, route_expire_handler
, route
);
3140 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3143 route
->lifetime
= lifetime
;
3144 sd_event_source_unref(route
->expire
);
3145 route
->expire
= TAKE_PTR(expire
);
3149 if (dhcp4_address
) {
3150 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3152 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3153 goto dhcp4_address_fail
;
3156 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3158 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3160 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3162 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3167 if (ipv4ll_address
) {
3168 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3170 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3171 goto ipv4ll_address_fail
;
3174 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3176 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3178 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3180 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3183 ipv4ll_address_fail
:
3188 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3189 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3190 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3199 r
= link_new(m
, message
, ret
);
3205 log_link_debug(link
, "Link %d added", link
->ifindex
);
3207 r
= link_load(link
);
3211 if (detect_container() <= 0) {
3212 /* not in a container, udev will be around */
3213 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3214 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3216 log_link_warning_errno(link
, r
, "Could not find device: %m");
3220 r
= sd_device_get_is_initialized(device
);
3222 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3227 log_link_debug(link
, "link pending udev initialization...");
3231 r
= device_is_renaming(device
);
3233 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3237 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3241 r
= link_initialized(link
, device
);
3245 r
= link_initialized_and_synced(link
);
3252 link_enter_failed(link
);
3256 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3261 log_link_info(link
, "Gained IPv6LL");
3263 link
->ipv6ll_address
= *address
;
3264 link_check_ready(link
);
3266 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3267 r
= link_acquire_ipv6_conf(link
);
3269 link_enter_failed(link
);
3277 static int link_carrier_gained(Link
*link
) {
3282 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3283 r
= link_acquire_conf(link
);
3285 link_enter_failed(link
);
3289 r
= link_request_set_addresses(link
);
3294 r
= link_handle_bound_by_list(link
);
3301 static int link_carrier_lost(Link
*link
) {
3306 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3309 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3310 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3311 * configuration and stop the clients as well. */
3312 if (link
->setting_mtu
)
3315 r
= link_stop_clients(link
, false);
3317 link_enter_failed(link
);
3321 if (link_dhcp4_server_enabled(link
))
3322 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3324 r
= link_drop_config(link
);
3328 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3329 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3330 r
= link_drop_foreign_config(link
);
3335 r
= link_handle_bound_by_list(link
);
3342 int link_carrier_reset(Link
*link
) {
3347 if (link_has_carrier(link
)) {
3348 r
= link_carrier_lost(link
);
3352 r
= link_carrier_gained(link
);
3356 log_link_info(link
, "Reset carrier");
3362 int link_update(Link
*link
, sd_netlink_message
*m
) {
3363 struct ether_addr mac
;
3366 bool had_carrier
, carrier_gained
, carrier_lost
;
3370 assert(link
->ifname
);
3373 if (link
->state
== LINK_STATE_LINGER
) {
3374 log_link_info(link
, "Link re-added");
3375 link_set_state(link
, LINK_STATE_CONFIGURING
);
3377 r
= link_new_carrier_maps(link
);
3382 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3383 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3384 Manager
*manager
= link
->manager
;
3386 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3389 r
= link_add(manager
, m
, &link
);
3394 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3395 if (r
>= 0 && mtu
> 0) {
3397 if (link
->original_mtu
== 0) {
3398 link
->original_mtu
= mtu
;
3399 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3402 if (link
->dhcp_client
) {
3403 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3406 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3410 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3412 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3416 /* The kernel may broadcast NEWLINK messages without the MAC address
3417 set, simply ignore them. */
3418 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3420 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3423 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3426 log_link_debug(link
, "MAC address: "
3427 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3428 mac
.ether_addr_octet
[0],
3429 mac
.ether_addr_octet
[1],
3430 mac
.ether_addr_octet
[2],
3431 mac
.ether_addr_octet
[3],
3432 mac
.ether_addr_octet
[4],
3433 mac
.ether_addr_octet
[5]);
3436 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3438 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3441 if (link
->dhcp_client
) {
3442 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3443 (const uint8_t *) &link
->mac
,
3447 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3449 r
= dhcp4_set_client_identifier(link
);
3454 if (link
->dhcp6_client
) {
3455 const DUID
* duid
= link_get_duid(link
);
3457 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3458 (const uint8_t *) &link
->mac
,
3462 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3464 if (link
->network
->iaid_set
) {
3465 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3466 link
->network
->iaid
);
3468 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3471 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3473 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3474 duid
->raw_data_len
);
3476 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3480 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3482 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3486 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3488 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3493 old_master
= link
->master_ifindex
;
3494 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3496 had_carrier
= link_has_carrier(link
);
3498 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3502 r
= link_update_lldp(link
);
3506 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3507 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3509 if (carrier_gained
) {
3510 log_link_info(link
, "Gained carrier");
3512 r
= link_carrier_gained(link
);
3515 } else if (carrier_lost
) {
3516 log_link_info(link
, "Lost carrier");
3518 r
= link_carrier_lost(link
);
3526 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3534 if (hashmap_isempty(h
))
3538 HASHMAP_FOREACH(link
, h
, i
) {
3542 fprintf(f
, "%i", link
->ifindex
);
3549 int link_save(Link
*link
) {
3550 _cleanup_free_
char *temp_path
= NULL
;
3551 _cleanup_fclose_
FILE *f
= NULL
;
3552 const char *admin_state
, *oper_state
;
3559 assert(link
->state_file
);
3560 assert(link
->lease_file
);
3561 assert(link
->manager
);
3563 if (link
->state
== LINK_STATE_LINGER
) {
3564 (void) unlink(link
->state_file
);
3568 link_lldp_save(link
);
3570 admin_state
= link_state_to_string(link
->state
);
3571 assert(admin_state
);
3573 oper_state
= link_operstate_to_string(link
->operstate
);
3576 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3580 (void) fchmod(fileno(f
), 0644);
3583 "# This is private data. Do not parse.\n"
3586 admin_state
, oper_state
);
3588 if (link
->network
) {
3589 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3590 const char *dhcp_domainname
= NULL
, *p
;
3591 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3595 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3596 yes_no(link
->network
->required_for_online
));
3598 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3599 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3601 if (link
->dhcp6_client
) {
3602 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3603 if (r
< 0 && r
!= -ENOMSG
)
3604 log_link_debug(link
, "No DHCPv6 lease");
3607 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3612 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3613 _cleanup_free_
char *b
= NULL
;
3615 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3616 &link
->network
->dns
[j
].address
, &b
);
3618 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3628 if (link
->network
->dhcp_use_dns
&&
3630 const struct in_addr
*addresses
;
3632 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3634 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3638 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3639 struct in6_addr
*in6_addrs
;
3641 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3645 serialize_in6_addrs(f
, in6_addrs
, r
);
3650 /* Make sure to flush out old entries before we use the NDISC data */
3653 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3656 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3660 serialize_in6_addrs(f
, &dd
->address
, 1);
3669 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3671 if (link
->network
->dhcp_use_ntp
&&
3673 const struct in_addr
*addresses
;
3675 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3677 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3681 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3682 struct in6_addr
*in6_addrs
;
3685 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3690 serialize_in6_addrs(f
, in6_addrs
, r
);
3694 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3696 fputstrv(f
, hosts
, NULL
, &space
);
3701 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3702 if (link
->dhcp_lease
) {
3703 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3704 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3707 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3710 fputs("DOMAINS=", f
);
3712 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3713 fputs_with_space(f
, p
, NULL
, &space
);
3715 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3718 if (dhcp_domainname
)
3719 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3721 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3723 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3725 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3726 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3731 fputs("ROUTE_DOMAINS=", f
);
3733 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3734 fputs_with_space(f
, p
, NULL
, &space
);
3736 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3739 if (dhcp_domainname
)
3740 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3742 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3744 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3746 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3747 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3752 fprintf(f
, "LLMNR=%s\n",
3753 resolve_support_to_string(link
->network
->llmnr
));
3754 fprintf(f
, "MDNS=%s\n",
3755 resolve_support_to_string(link
->network
->mdns
));
3756 if (link
->network
->dns_default_route
>= 0)
3757 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3759 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3760 fprintf(f
, "DNS_OVER_TLS=%s\n",
3761 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3763 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3764 fprintf(f
, "DNSSEC=%s\n",
3765 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3767 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3770 fputs("DNSSEC_NTA=", f
);
3772 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3773 fputs_with_space(f
, n
, NULL
, &space
);
3777 fputs("ADDRESSES=", f
);
3779 SET_FOREACH(a
, link
->addresses
, i
) {
3780 _cleanup_free_
char *address_str
= NULL
;
3782 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3786 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3791 fputs("ROUTES=", f
);
3793 SET_FOREACH(route
, link
->routes
, i
) {
3794 _cleanup_free_
char *route_str
= NULL
;
3796 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3800 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3801 space
? " " : "", route_str
,
3802 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3809 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3810 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3812 if (link
->dhcp_lease
) {
3813 struct in_addr address
;
3814 const char *tz
= NULL
;
3816 assert(link
->network
);
3818 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3820 fprintf(f
, "TIMEZONE=%s\n", tz
);
3822 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3824 fputs("DHCP4_ADDRESS=", f
);
3825 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3829 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3837 (void) unlink(link
->lease_file
);
3840 struct in_addr address
;
3842 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3844 fputs("IPV4LL_ADDRESS=", f
);
3845 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3850 r
= fflush_and_check(f
);
3854 if (rename(temp_path
, link
->state_file
) < 0) {
3862 (void) unlink(link
->state_file
);
3864 (void) unlink(temp_path
);
3866 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3869 /* The serialized state in /run is no longer up-to-date. */
3870 void link_dirty(Link
*link
) {
3875 /* mark manager dirty as link is dirty */
3876 manager_dirty(link
->manager
);
3878 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3880 /* allocation errors are ignored */
3883 r
= set_put(link
->manager
->dirty_links
, link
);
3885 /* don't take another ref if the link was already dirty */
3891 /* The serialized state in /run is up-to-date */
3892 void link_clean(Link
*link
) {
3894 assert(link
->manager
);
3896 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3899 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3900 [LINK_STATE_PENDING
] = "pending",
3901 [LINK_STATE_INITIALIZED
] = "initialized",
3902 [LINK_STATE_CONFIGURING
] = "configuring",
3903 [LINK_STATE_CONFIGURED
] = "configured",
3904 [LINK_STATE_UNMANAGED
] = "unmanaged",
3905 [LINK_STATE_FAILED
] = "failed",
3906 [LINK_STATE_LINGER
] = "linger",
3909 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);