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/bridge.h"
16 #include "netdev/vrf.h"
17 #include "netlink-util.h"
18 #include "network-internal.h"
19 #include "networkd-can.h"
20 #include "networkd-ipv6-proxy-ndp.h"
21 #include "networkd-lldp-tx.h"
22 #include "networkd-manager.h"
23 #include "networkd-ndisc.h"
24 #include "networkd-neighbor.h"
25 #include "networkd-radv.h"
26 #include "networkd-routing-policy-rule.h"
28 #include "socket-util.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
32 #include "sysctl-util.h"
33 #include "tmpfile-util.h"
34 #include "udev-util.h"
38 uint32_t link_get_vrf_table(Link
*link
) {
39 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
42 uint32_t link_get_dhcp_route_table(Link
*link
) {
43 /* When the interface is part of an VRF use the VRFs routing table, unless
44 * another table is explicitly specified. */
45 if (link
->network
->dhcp_route_table_set
)
46 return link
->network
->dhcp_route_table
;
47 return link_get_vrf_table(link
);
50 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
51 if (link
->network
->ipv6_accept_ra_route_table_set
)
52 return link
->network
->ipv6_accept_ra_route_table
;
53 return link_get_vrf_table(link
);
56 DUID
* link_get_duid(Link
*link
) {
57 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
58 return &link
->network
->duid
;
60 return &link
->manager
->duid
;
63 static bool link_dhcp6_enabled(Link
*link
) {
66 if (!socket_ipv6_is_supported())
69 if (link
->flags
& IFF_LOOPBACK
)
75 if (link
->network
->bond
)
78 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
81 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
84 static bool link_dhcp4_enabled(Link
*link
) {
87 if (link
->flags
& IFF_LOOPBACK
)
93 if (link
->network
->bond
)
96 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
99 static bool link_dhcp4_server_enabled(Link
*link
) {
102 if (link
->flags
& IFF_LOOPBACK
)
108 if (link
->network
->bond
)
111 return link
->network
->dhcp_server
;
114 bool link_ipv4ll_enabled(Link
*link
) {
117 if (link
->flags
& IFF_LOOPBACK
)
123 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
126 if (link
->network
->bond
)
129 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
132 bool link_ipv4ll_fallback_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
141 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
144 if (link
->network
->bond
)
147 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
150 static bool link_ipv6ll_enabled(Link
*link
) {
153 if (!socket_ipv6_is_supported())
156 if (link
->flags
& IFF_LOOPBACK
)
162 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
165 if (link
->network
->bond
)
168 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
171 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
174 static bool link_ipv6_enabled(Link
*link
) {
177 if (!socket_ipv6_is_supported())
180 if (link
->network
->bond
)
183 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
186 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
187 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
190 static bool link_radv_enabled(Link
*link
) {
193 if (!link_ipv6ll_enabled(link
))
196 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
199 static bool link_ipv4_forward_enabled(Link
*link
) {
202 if (link
->flags
& IFF_LOOPBACK
)
208 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
211 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
214 static bool link_ipv6_forward_enabled(Link
*link
) {
217 if (!socket_ipv6_is_supported())
220 if (link
->flags
& IFF_LOOPBACK
)
226 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
229 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
232 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
235 static bool link_proxy_arp_enabled(Link
*link
) {
238 if (link
->flags
& IFF_LOOPBACK
)
244 if (link
->network
->proxy_arp
< 0)
250 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
253 if (!socket_ipv6_is_supported())
256 if (link
->flags
& IFF_LOOPBACK
)
262 if (!link_ipv6ll_enabled(link
))
265 /* If unset use system default (enabled if local forwarding is disabled.
266 * disabled if local forwarding is enabled).
267 * If set, ignore or enforce RA independent of local forwarding state.
269 if (link
->network
->ipv6_accept_ra
< 0)
270 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
271 return !link_ipv6_forward_enabled(link
);
272 else if (link
->network
->ipv6_accept_ra
> 0)
273 /* accept RA even if ip_forward is enabled */
280 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
283 if (!socket_ipv6_is_supported())
284 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
286 if (link
->flags
& IFF_LOOPBACK
)
287 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
290 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
292 return link
->network
->ipv6_privacy_extensions
;
295 static int link_enable_ipv6(Link
*link
) {
299 if (link
->flags
& IFF_LOOPBACK
)
302 disabled
= !link_ipv6_enabled(link
);
304 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
306 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
308 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
313 static bool link_is_enslaved(Link
*link
) {
314 if (link
->flags
& IFF_SLAVE
)
315 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
318 if (!link
->enslaved_raw
)
324 if (link
->network
->bridge
)
325 /* TODO: support the case when link is not managed by networkd. */
331 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
337 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
340 link_update_operstate(master
, true);
343 void link_update_operstate(Link
*link
, bool also_update_master
) {
344 LinkOperationalState operstate
;
349 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
350 operstate
= LINK_OPERSTATE_DORMANT
;
351 else if (link_has_carrier(link
)) {
353 uint8_t scope
= RT_SCOPE_NOWHERE
;
355 /* if we have carrier, check what addresses we have */
356 SET_FOREACH(address
, link
->addresses
, i
) {
357 if (!address_is_ready(address
))
360 if (address
->scope
< scope
)
361 scope
= address
->scope
;
364 /* for operstate we also take foreign addresses into account */
365 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
366 if (!address_is_ready(address
))
369 if (address
->scope
< scope
)
370 scope
= address
->scope
;
373 if (scope
< RT_SCOPE_SITE
)
374 /* universally accessible addresses found */
375 operstate
= LINK_OPERSTATE_ROUTABLE
;
376 else if (scope
< RT_SCOPE_HOST
)
377 /* only link or site local addresses found */
378 operstate
= LINK_OPERSTATE_DEGRADED
;
380 /* no useful addresses found */
381 operstate
= LINK_OPERSTATE_CARRIER
;
382 } else if (link
->flags
& IFF_UP
)
383 operstate
= LINK_OPERSTATE_NO_CARRIER
;
385 operstate
= LINK_OPERSTATE_OFF
;
387 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
388 link_is_enslaved(link
))
389 operstate
= LINK_OPERSTATE_ENSLAVED
;
391 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
394 SET_FOREACH(slave
, link
->slaves
, i
) {
395 link_update_operstate(slave
, false);
397 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
398 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
402 if (link
->operstate
!= operstate
) {
403 link
->operstate
= operstate
;
404 link_send_changed(link
, "OperationalState", NULL
);
408 if (also_update_master
&& link
->network
) {
409 link_update_master_operstate(link
, link
->network
->bond
);
410 link_update_master_operstate(link
, link
->network
->bridge
);
414 #define FLAG_STRING(string, flag, old, new) \
415 (((old ^ new) & flag) \
416 ? ((old & flag) ? (" -" string) : (" +" string)) \
419 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
420 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
426 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
428 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
430 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
432 /* if we got a message without operstate, take it to mean
433 the state was unchanged */
434 operstate
= link
->kernel_operstate
;
436 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
439 if (link
->flags
!= flags
) {
440 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",
441 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
442 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
443 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
444 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
445 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
446 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
447 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
448 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
449 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
450 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
451 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
452 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
453 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
454 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
455 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
456 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
457 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
458 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
459 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
461 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
462 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
463 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
464 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
465 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
466 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
467 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
468 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
470 /* link flags are currently at most 18 bits, let's align to
472 if (unknown_flags_added
)
474 "Unknown link flags gained: %#.5x (ignoring)",
475 unknown_flags_added
);
477 if (unknown_flags_removed
)
479 "Unknown link flags lost: %#.5x (ignoring)",
480 unknown_flags_removed
);
484 link
->kernel_operstate
= operstate
;
486 link_update_operstate(link
, true);
491 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
492 _cleanup_(link_unrefp
) Link
*link
= NULL
;
494 const char *ifname
, *kind
= NULL
;
496 unsigned short iftype
;
502 /* check for link kind */
503 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
505 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
506 r
= sd_netlink_message_exit_container(message
);
511 r
= sd_netlink_message_get_type(message
, &type
);
514 else if (type
!= RTM_NEWLINK
)
517 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
520 else if (ifindex
<= 0)
523 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
527 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
538 .state
= LINK_STATE_PENDING
,
539 .rtnl_extended_attrs
= true,
544 link
->ifname
= strdup(ifname
);
549 link
->kind
= strdup(kind
);
554 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
556 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
558 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
560 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
562 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
565 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
568 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
571 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
575 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
579 r
= link_update_flags(link
, message
);
583 *ret
= TAKE_PTR(link
);
588 static Link
*link_free(Link
*link
) {
593 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
594 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
596 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
597 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
599 while ((address
= link
->pool_addresses
)) {
600 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
601 address_free(address
);
604 sd_dhcp_server_unref(link
->dhcp_server
);
605 sd_dhcp_client_unref(link
->dhcp_client
);
606 sd_dhcp_lease_unref(link
->dhcp_lease
);
608 link_lldp_emit_stop(link
);
610 free(link
->lease_file
);
612 sd_lldp_unref(link
->lldp
);
613 free(link
->lldp_file
);
617 sd_ipv4ll_unref(link
->ipv4ll
);
618 sd_dhcp6_client_unref(link
->dhcp6_client
);
619 sd_ndisc_unref(link
->ndisc
);
620 sd_radv_unref(link
->radv
);
625 (void) unlink(link
->state_file
);
626 free(link
->state_file
);
628 sd_device_unref(link
->sd_device
);
630 hashmap_free(link
->bound_to_links
);
631 hashmap_free(link
->bound_by_links
);
633 set_free_with_destructor(link
->slaves
, link_unref
);
635 network_unref(link
->network
);
640 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
642 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
649 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
658 static void link_set_state(Link
*link
, LinkState state
) {
661 if (link
->state
== state
)
664 log_link_debug(link
, "State changed: %s -> %s",
665 link_state_to_string(link
->state
),
666 link_state_to_string(state
));
670 link_send_changed(link
, "AdministrativeState", NULL
);
673 static void link_enter_unmanaged(Link
*link
) {
676 log_link_debug(link
, "Unmanaged");
678 link_set_state(link
, LINK_STATE_UNMANAGED
);
683 int link_stop_clients(Link
*link
) {
687 assert(link
->manager
);
688 assert(link
->manager
->event
);
690 if (link
->dhcp_client
) {
691 k
= sd_dhcp_client_stop(link
->dhcp_client
);
693 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
697 k
= sd_ipv4ll_stop(link
->ipv4ll
);
699 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
702 if (link
->dhcp6_client
) {
703 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
705 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
709 k
= sd_ndisc_stop(link
->ndisc
);
711 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
715 k
= sd_radv_stop(link
->radv
);
717 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
720 link_lldp_emit_stop(link
);
724 void link_enter_failed(Link
*link
) {
727 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
730 log_link_warning(link
, "Failed");
732 link_set_state(link
, LINK_STATE_FAILED
);
734 link_stop_clients(link
);
739 static Address
* link_find_dhcp_server_address(Link
*link
) {
743 assert(link
->network
);
745 /* The first statically configured address if there is any */
746 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
748 if (address
->family
!= AF_INET
)
751 if (in_addr_is_null(address
->family
, &address
->in_addr
))
757 /* If that didn't work, find a suitable address we got from the pool */
758 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
759 if (address
->family
!= AF_INET
)
768 static int link_join_netdevs_after_configured(Link
*link
) {
773 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
774 if (netdev
->ifindex
> 0)
775 /* Assume already enslaved. */
778 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
781 log_struct(LOG_DEBUG
,
782 LOG_LINK_INTERFACE(link
),
783 LOG_NETDEV_INTERFACE(netdev
),
784 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
786 r
= netdev_join(netdev
, link
, NULL
);
788 return log_struct_errno(LOG_WARNING
, r
,
789 LOG_LINK_INTERFACE(link
),
790 LOG_NETDEV_INTERFACE(netdev
),
791 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
797 static void link_enter_configured(Link
*link
) {
799 assert(link
->network
);
801 if (link
->state
!= LINK_STATE_CONFIGURING
)
804 log_link_info(link
, "Configured");
806 link_set_state(link
, LINK_STATE_CONFIGURED
);
808 (void) link_join_netdevs_after_configured(link
);
813 static int link_request_set_routing_policy_rule(Link
*link
) {
814 RoutingPolicyRule
*rule
, *rrule
= NULL
;
818 assert(link
->network
);
820 link_set_state(link
, LINK_STATE_CONFIGURING
);
821 link
->routing_policy_rules_configured
= false;
823 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
824 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
825 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
826 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
828 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
832 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
834 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
835 link_enter_failed(link
);
839 link
->routing_policy_rule_messages
++;
842 routing_policy_rule_purge(link
->manager
, link
);
843 if (link
->routing_policy_rule_messages
== 0) {
844 link
->routing_policy_rules_configured
= true;
845 link_check_ready(link
);
847 log_link_debug(link
, "Setting routing policy rules");
852 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
856 assert(link
->route_messages
> 0);
857 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
858 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
860 link
->route_messages
--;
862 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
865 r
= sd_netlink_message_get_errno(m
);
866 if (r
< 0 && r
!= -EEXIST
)
867 log_link_warning_errno(link
, r
, "Could not set route: %m");
869 if (link
->route_messages
== 0) {
870 log_link_debug(link
, "Routes set");
871 link
->static_routes_configured
= true;
872 link_check_ready(link
);
878 int link_request_set_routes(Link
*link
) {
880 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
881 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
888 assert(link
->network
);
889 assert(link
->addresses_configured
);
890 assert(link
->address_messages
== 0);
891 assert(link
->state
!= _LINK_STATE_INVALID
);
893 link_set_state(link
, LINK_STATE_CONFIGURING
);
894 link
->static_routes_configured
= false;
896 r
= link_request_set_routing_policy_rule(link
);
900 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
901 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
902 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
904 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
907 r
= route_configure(rt
, link
, route_handler
);
909 log_link_warning_errno(link
, r
, "Could not set routes: %m");
910 link_enter_failed(link
);
914 link
->route_messages
++;
917 if (link
->route_messages
== 0) {
918 link
->static_routes_configured
= true;
919 link_check_ready(link
);
921 log_link_debug(link
, "Setting routes");
926 void link_check_ready(Link
*link
) {
932 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
938 if (!link
->addresses_configured
)
941 if (!link
->neighbors_configured
)
944 SET_FOREACH(a
, link
->addresses
, i
)
945 if (!address_is_ready(a
))
948 if (!link
->addresses_ready
) {
949 link
->addresses_ready
= true;
950 link_request_set_routes(link
);
953 if (!link
->static_routes_configured
)
956 if (!link
->routing_policy_rules_configured
)
959 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
962 if (link_ipv6ll_enabled(link
) &&
963 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
966 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
967 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
968 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
969 /* When DHCP is enabled, at least one protocol must provide an address, or
970 * an IPv4ll fallback address must be configured. */
973 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
976 if (link
->state
!= LINK_STATE_CONFIGURED
)
977 link_enter_configured(link
);
982 static int link_request_set_neighbors(Link
*link
) {
987 assert(link
->network
);
988 assert(link
->state
!= _LINK_STATE_INVALID
);
990 link_set_state(link
, LINK_STATE_CONFIGURING
);
991 link
->neighbors_configured
= false;
993 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
994 r
= neighbor_configure(neighbor
, link
, NULL
);
996 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
997 link_enter_failed(link
);
1002 if (link
->neighbor_messages
== 0) {
1003 link
->neighbors_configured
= true;
1004 link_check_ready(link
);
1006 log_link_debug(link
, "Setting neighbors");
1011 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1017 assert(link
->ifname
);
1018 assert(link
->address_messages
> 0);
1019 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1020 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1022 link
->address_messages
--;
1024 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1027 r
= sd_netlink_message_get_errno(m
);
1028 if (r
< 0 && r
!= -EEXIST
)
1029 log_link_warning_errno(link
, r
, "could not set address: %m");
1031 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1033 if (link
->address_messages
== 0) {
1034 log_link_debug(link
, "Addresses set");
1035 link
->addresses_configured
= true;
1036 link_check_ready(link
);
1042 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1043 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1044 size_t n_addresses
= 0, n_allocated
= 0;
1047 log_debug("Copying DNS server information from %s", link
->ifname
);
1052 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1055 /* Only look for IPv4 addresses */
1056 if (link
->network
->dns
[i
].family
!= AF_INET
)
1059 ia
= link
->network
->dns
[i
].address
.in
;
1061 /* Never propagate obviously borked data */
1062 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1065 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1068 addresses
[n_addresses
++] = ia
;
1071 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1072 const struct in_addr
*da
= NULL
;
1075 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1078 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1081 for (j
= 0; j
< n
; j
++)
1082 if (in4_addr_is_non_local(&da
[j
]))
1083 addresses
[n_addresses
++] = da
[j
];
1087 if (n_addresses
<= 0)
1090 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1093 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1094 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1095 size_t n_addresses
= 0, n_allocated
= 0;
1101 log_debug("Copying NTP server information from %s", link
->ifname
);
1103 STRV_FOREACH(a
, link
->network
->ntp
) {
1104 union in_addr_union ia
;
1106 /* Only look for IPv4 addresses */
1107 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1110 /* Never propagate obviously borked data */
1111 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1114 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1117 addresses
[n_addresses
++] = ia
.in
;
1120 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1121 const struct in_addr
*da
= NULL
;
1124 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1127 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1130 for (j
= 0; j
< n
; j
++)
1131 if (in4_addr_is_non_local(&da
[j
]))
1132 addresses
[n_addresses
++] = da
[j
];
1136 if (n_addresses
<= 0)
1139 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1142 static int link_set_bridge_fdb(Link
*link
) {
1143 FdbEntry
*fdb_entry
;
1146 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1147 r
= fdb_entry_configure(link
, fdb_entry
);
1149 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1155 static int link_request_set_addresses(Link
*link
) {
1156 AddressLabel
*label
;
1161 assert(link
->network
);
1162 assert(link
->state
!= _LINK_STATE_INVALID
);
1164 link_set_state(link
, LINK_STATE_CONFIGURING
);
1166 /* Reset all *_configured flags we are configuring. */
1167 link
->addresses_configured
= false;
1168 link
->addresses_ready
= false;
1169 link
->neighbors_configured
= false;
1170 link
->static_routes_configured
= false;
1171 link
->routing_policy_rules_configured
= false;
1173 r
= link_set_bridge_fdb(link
);
1177 r
= link_request_set_neighbors(link
);
1181 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1184 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1186 r
= address_configure(ad
, link
, address_handler
, update
);
1188 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1189 link_enter_failed(link
);
1193 link
->address_messages
++;
1196 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1197 r
= address_label_configure(label
, link
, NULL
, false);
1199 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1200 link_enter_failed(link
);
1204 link
->address_label_messages
++;
1207 /* now that we can figure out a default address for the dhcp server,
1209 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1211 Link
*uplink
= NULL
;
1212 bool acquired_uplink
= false;
1214 address
= link_find_dhcp_server_address(link
);
1216 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1217 link_enter_failed(link
);
1221 /* use the server address' subnet as the pool */
1222 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1223 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1228 r = sd_dhcp_server_set_router(link->dhcp_server,
1229 &main_address->in_addr.in);
1234 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1235 r
= sd_dhcp_server_set_max_lease_time(
1237 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1242 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1243 r
= sd_dhcp_server_set_default_lease_time(
1245 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1250 if (link
->network
->dhcp_server_emit_dns
) {
1252 if (link
->network
->n_dhcp_server_dns
> 0)
1253 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1255 uplink
= manager_find_uplink(link
->manager
, link
);
1256 acquired_uplink
= true;
1259 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1262 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1265 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1268 if (link
->network
->dhcp_server_emit_ntp
) {
1270 if (link
->network
->n_dhcp_server_ntp
> 0)
1271 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1273 if (!acquired_uplink
)
1274 uplink
= manager_find_uplink(link
->manager
, link
);
1277 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1280 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1284 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1287 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1289 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1291 if (link
->network
->dhcp_server_emit_timezone
) {
1292 _cleanup_free_
char *buffer
= NULL
;
1293 const char *tz
= NULL
;
1295 if (link
->network
->dhcp_server_timezone
)
1296 tz
= link
->network
->dhcp_server_timezone
;
1298 r
= get_timezone(&buffer
);
1300 log_warning_errno(r
, "Failed to determine timezone: %m");
1306 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1311 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1312 r
= sd_dhcp_server_start(link
->dhcp_server
);
1314 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1316 link_enter_failed(link
);
1322 log_link_debug(link
, "Offering DHCPv4 leases");
1325 if (link
->address_messages
== 0) {
1326 link
->addresses_configured
= true;
1327 link_check_ready(link
);
1329 log_link_debug(link
, "Setting addresses");
1334 static int link_set_bridge_vlan(Link
*link
) {
1337 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1339 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1344 static int link_set_proxy_arp(Link
*link
) {
1347 if (!link_proxy_arp_enabled(link
))
1350 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1352 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1357 static int link_configure_after_setting_mtu(Link
*link
);
1359 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1364 assert(link
->ifname
);
1366 link
->setting_mtu
= false;
1368 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1371 r
= sd_netlink_message_get_errno(m
);
1373 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1377 log_link_debug(link
, "Setting MTU done.");
1379 if (link
->state
== LINK_STATE_INITIALIZED
)
1380 (void) link_configure_after_setting_mtu(link
);
1385 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1386 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1390 assert(link
->manager
);
1391 assert(link
->manager
->rtnl
);
1393 if (mtu
== 0 || link
->setting_mtu
)
1396 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1399 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1401 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1403 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1405 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1406 * for this interface, then disable IPv6 else enable it. */
1407 (void) link_enable_ipv6(link
);
1409 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1410 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1411 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1413 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1414 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1419 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1421 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1423 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1424 link_netlink_destroy_callback
, link
);
1426 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1429 link
->setting_mtu
= true;
1434 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1439 assert(link
->ifname
);
1441 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1444 r
= sd_netlink_message_get_errno(m
);
1446 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1451 static int link_set_flags(Link
*link
) {
1452 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1453 unsigned ifi_change
= 0;
1454 unsigned ifi_flags
= 0;
1458 assert(link
->manager
);
1459 assert(link
->manager
->rtnl
);
1461 if (link
->flags
& IFF_LOOPBACK
)
1467 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1470 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1472 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1474 if (link
->network
->arp
>= 0) {
1475 ifi_change
|= IFF_NOARP
;
1476 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1479 if (link
->network
->multicast
>= 0) {
1480 ifi_change
|= IFF_MULTICAST
;
1481 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1484 if (link
->network
->allmulticast
>= 0) {
1485 ifi_change
|= IFF_ALLMULTI
;
1486 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1489 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1491 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1493 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1494 link_netlink_destroy_callback
, link
);
1496 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1503 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1508 assert(link
->ifname
);
1510 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1513 r
= sd_netlink_message_get_errno(m
);
1515 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1522 static int link_set_bond(Link
*link
) {
1523 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1527 assert(link
->network
);
1529 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1531 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1533 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1535 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1537 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1539 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1541 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1543 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1545 if (link
->network
->active_slave
) {
1546 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1548 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1551 if (link
->network
->primary_slave
) {
1552 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1554 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1557 r
= sd_netlink_message_close_container(req
);
1559 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1561 r
= sd_netlink_message_close_container(req
);
1563 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1565 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1566 link_netlink_destroy_callback
, link
);
1568 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1575 static int link_acquire_ipv6_conf(Link
*link
) {
1580 if (link_ipv6_accept_ra_enabled(link
)) {
1581 assert(link
->ndisc
);
1583 log_link_debug(link
, "Discovering IPv6 routers");
1585 r
= sd_ndisc_start(link
->ndisc
);
1586 if (r
< 0 && r
!= -EBUSY
)
1587 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1590 if (link_radv_enabled(link
)) {
1592 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1594 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1596 r
= sd_radv_start(link
->radv
);
1597 if (r
< 0 && r
!= -EBUSY
)
1598 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1601 (void) dhcp6_request_prefix_delegation(link
);
1606 static int link_acquire_ipv4_conf(Link
*link
) {
1610 assert(link
->manager
);
1611 assert(link
->manager
->event
);
1613 if (link_ipv4ll_enabled(link
)) {
1614 assert(link
->ipv4ll
);
1616 log_link_debug(link
, "Acquiring IPv4 link-local address");
1618 r
= sd_ipv4ll_start(link
->ipv4ll
);
1620 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1623 if (link_dhcp4_enabled(link
)) {
1624 assert(link
->dhcp_client
);
1626 log_link_debug(link
, "Acquiring DHCPv4 lease");
1628 r
= sd_dhcp_client_start(link
->dhcp_client
);
1630 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1636 static int link_acquire_conf(Link
*link
) {
1641 r
= link_acquire_ipv4_conf(link
);
1645 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1646 r
= link_acquire_ipv6_conf(link
);
1651 if (link_lldp_emit_enabled(link
)) {
1652 r
= link_lldp_emit_start(link
);
1654 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1660 bool link_has_carrier(Link
*link
) {
1661 /* see Documentation/networking/operstates.txt in the kernel sources */
1663 if (link
->kernel_operstate
== IF_OPER_UP
)
1666 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1667 /* operstate may not be implemented, so fall back to flags */
1668 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1674 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1679 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1682 r
= sd_netlink_message_get_errno(m
);
1684 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1689 static int link_configure_addrgen_mode(Link
*link
) {
1690 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1691 uint8_t ipv6ll_mode
;
1695 assert(link
->network
);
1696 assert(link
->manager
);
1697 assert(link
->manager
->rtnl
);
1699 log_link_debug(link
, "Setting address genmode for link");
1701 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1703 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1705 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1707 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1709 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1711 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1713 if (!link_ipv6ll_enabled(link
))
1714 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1715 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1716 /* The file may not exist. And event if it exists, when stable_secret is unset,
1717 * reading the file fails with EIO. */
1718 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1720 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1722 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1724 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1726 r
= sd_netlink_message_close_container(req
);
1728 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1730 r
= sd_netlink_message_close_container(req
);
1732 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1734 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1735 link_netlink_destroy_callback
, link
);
1737 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1744 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1749 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1752 r
= sd_netlink_message_get_errno(m
);
1754 /* we warn but don't fail the link, as it may be brought up later */
1755 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1760 static int link_up(Link
*link
) {
1761 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1765 assert(link
->network
);
1766 assert(link
->manager
);
1767 assert(link
->manager
->rtnl
);
1769 log_link_debug(link
, "Bringing link up");
1771 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1773 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1775 /* set it free if not enslaved with networkd */
1776 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1777 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1779 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1782 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1784 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1786 if (link
->network
->mac
) {
1787 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1789 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1792 if (link_ipv6_enabled(link
)) {
1793 uint8_t ipv6ll_mode
;
1795 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1797 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1799 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1800 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1802 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1804 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1805 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1807 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1810 if (!link_ipv6ll_enabled(link
))
1811 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1812 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1813 /* The file may not exist. And event if it exists, when stable_secret is unset,
1814 * reading the file fails with EIO. */
1815 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1817 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1819 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1821 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1823 r
= sd_netlink_message_close_container(req
);
1825 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1827 r
= sd_netlink_message_close_container(req
);
1829 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1832 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1833 link_netlink_destroy_callback
, link
);
1835 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1842 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1847 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1850 r
= sd_netlink_message_get_errno(m
);
1852 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1857 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1858 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1862 assert(link
->manager
);
1863 assert(link
->manager
->rtnl
);
1865 log_link_debug(link
, "Bringing link down");
1867 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1868 RTM_SETLINK
, link
->ifindex
);
1870 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1872 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1874 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1876 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1877 callback
?: link_down_handler
,
1878 link_netlink_destroy_callback
, link
);
1880 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1887 static int link_handle_bound_to_list(Link
*link
) {
1891 bool required_up
= false;
1892 bool link_is_up
= false;
1896 if (hashmap_isempty(link
->bound_to_links
))
1899 if (link
->flags
& IFF_UP
)
1902 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1903 if (link_has_carrier(l
)) {
1908 if (!required_up
&& link_is_up
) {
1909 r
= link_down(link
, NULL
);
1912 } else if (required_up
&& !link_is_up
) {
1921 static int link_handle_bound_by_list(Link
*link
) {
1928 if (hashmap_isempty(link
->bound_by_links
))
1931 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1932 r
= link_handle_bound_to_list(l
);
1940 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1946 if (link
== carrier
)
1949 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1952 r
= hashmap_ensure_allocated(h
, NULL
);
1956 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1963 static int link_new_bound_by_list(Link
*link
) {
1968 bool list_updated
= false;
1971 assert(link
->manager
);
1975 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1976 if (!carrier
->network
)
1979 if (strv_isempty(carrier
->network
->bind_carrier
))
1982 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1983 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1987 list_updated
= true;
1994 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1995 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1999 link_dirty(carrier
);
2005 static int link_new_bound_to_list(Link
*link
) {
2010 bool list_updated
= false;
2013 assert(link
->manager
);
2018 if (strv_isempty(link
->network
->bind_carrier
))
2023 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2024 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2025 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2029 list_updated
= true;
2036 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2037 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2041 link_dirty(carrier
);
2047 static int link_new_carrier_maps(Link
*link
) {
2050 r
= link_new_bound_by_list(link
);
2054 r
= link_handle_bound_by_list(link
);
2058 r
= link_new_bound_to_list(link
);
2062 r
= link_handle_bound_to_list(link
);
2069 static void link_free_bound_to_list(Link
*link
) {
2073 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2074 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2076 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2077 link_dirty(bound_to
);
2083 static void link_free_bound_by_list(Link
*link
) {
2087 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2088 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2090 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2091 link_dirty(bound_by
);
2092 link_handle_bound_to_list(bound_by
);
2099 static void link_free_carrier_maps(Link
*link
) {
2100 bool list_updated
= false;
2104 if (!hashmap_isempty(link
->bound_to_links
)) {
2105 link_free_bound_to_list(link
);
2106 list_updated
= true;
2109 if (!hashmap_isempty(link
->bound_by_links
)) {
2110 link_free_bound_by_list(link
);
2111 list_updated
= true;
2120 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2127 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2131 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2135 r
= set_put(master
->slaves
, link
);
2143 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2148 if (!link
->manager
|| !netdev
)
2151 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2154 link_unref(set_remove(master
->slaves
, link
));
2157 static void link_detach_from_manager(Link
*link
) {
2158 if (!link
|| !link
->manager
)
2161 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2164 /* The following must be called at last. */
2165 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2169 void link_drop(Link
*link
) {
2170 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2173 link_set_state(link
, LINK_STATE_LINGER
);
2175 link_free_carrier_maps(link
);
2177 if (link
->network
) {
2178 link_drop_from_master(link
, link
->network
->bridge
);
2179 link_drop_from_master(link
, link
->network
->bond
);
2182 log_link_debug(link
, "Link removed");
2184 (void) unlink(link
->state_file
);
2185 link_detach_from_manager(link
);
2188 static int link_joined(Link
*link
) {
2192 assert(link
->network
);
2194 if (!hashmap_isempty(link
->bound_to_links
)) {
2195 r
= link_handle_bound_to_list(link
);
2198 } else if (!(link
->flags
& IFF_UP
)) {
2201 link_enter_failed(link
);
2206 if (link
->network
->bridge
) {
2207 r
= link_set_bridge(link
);
2209 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2211 r
= link_append_to_master(link
, link
->network
->bridge
);
2213 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2216 if (link
->network
->bond
) {
2217 r
= link_set_bond(link
);
2219 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2221 r
= link_append_to_master(link
, link
->network
->bond
);
2223 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2226 if (link
->network
->use_br_vlan
&&
2227 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2228 r
= link_set_bridge_vlan(link
);
2230 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2233 /* Skip setting up addresses until it gets carrier,
2234 or it would try to set addresses twice,
2235 which is bad for non-idempotent steps. */
2236 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2239 return link_request_set_addresses(link
);
2242 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2246 assert(link
->network
);
2247 assert(link
->enslaving
> 0);
2248 assert(!link
->enslaved_raw
);
2252 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2255 r
= sd_netlink_message_get_errno(m
);
2256 if (r
< 0 && r
!= -EEXIST
) {
2257 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2258 link_enter_failed(link
);
2261 log_link_debug(link
, "Joined netdev");
2263 if (link
->enslaving
== 0) {
2264 link
->enslaved_raw
= true;
2271 static int link_enter_join_netdev(Link
*link
) {
2277 assert(link
->network
);
2278 assert(link
->state
== LINK_STATE_INITIALIZED
);
2280 link_set_state(link
, LINK_STATE_CONFIGURING
);
2283 link
->enslaving
= 0;
2284 link
->enslaved_raw
= false;
2286 if (link
->network
->bond
) {
2287 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2288 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2289 return link_joined(link
);
2291 log_struct(LOG_DEBUG
,
2292 LOG_LINK_INTERFACE(link
),
2293 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2294 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2298 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2300 log_struct_errno(LOG_WARNING
, r
,
2301 LOG_LINK_INTERFACE(link
),
2302 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2303 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2304 link_enter_failed(link
);
2309 if (link
->network
->bridge
) {
2310 log_struct(LOG_DEBUG
,
2311 LOG_LINK_INTERFACE(link
),
2312 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2313 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2317 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2319 log_struct_errno(LOG_WARNING
, r
,
2320 LOG_LINK_INTERFACE(link
),
2321 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2322 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2323 link_enter_failed(link
);
2328 if (link
->network
->vrf
) {
2329 log_struct(LOG_DEBUG
,
2330 LOG_LINK_INTERFACE(link
),
2331 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2332 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2336 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2338 log_struct_errno(LOG_WARNING
, r
,
2339 LOG_LINK_INTERFACE(link
),
2340 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2341 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2342 link_enter_failed(link
);
2347 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2349 if (netdev
->ifindex
> 0)
2350 /* Assume already enslaved. */
2353 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2356 log_struct(LOG_DEBUG
,
2357 LOG_LINK_INTERFACE(link
),
2358 LOG_NETDEV_INTERFACE(netdev
),
2359 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2363 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2365 log_struct_errno(LOG_WARNING
, r
,
2366 LOG_LINK_INTERFACE(link
),
2367 LOG_NETDEV_INTERFACE(netdev
),
2368 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2369 link_enter_failed(link
);
2374 if (link
->enslaving
== 0)
2375 return link_joined(link
);
2380 static int link_set_ipv4_forward(Link
*link
) {
2383 if (!link_ipv4_forward_enabled(link
))
2386 /* We propagate the forwarding flag from one interface to the
2387 * global setting one way. This means: as long as at least one
2388 * interface was configured at any time that had IP forwarding
2389 * enabled the setting will stay on for good. We do this
2390 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2391 * somewhat in sync (see below). */
2393 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2395 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2400 static int link_set_ipv6_forward(Link
*link
) {
2403 if (!link_ipv6_forward_enabled(link
))
2406 /* On Linux, the IPv6 stack does not know a per-interface
2407 * packet forwarding setting: either packet forwarding is on
2408 * for all, or off for all. We hence don't bother with a
2409 * per-interface setting, but simply propagate the interface
2410 * flag, if it is set, to the global flag, one-way. Note that
2411 * while IPv4 would allow a per-interface flag, we expose the
2412 * same behaviour there and also propagate the setting from
2413 * one to all, to keep things simple (see above). */
2415 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2417 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2422 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2423 IPv6PrivacyExtensions s
;
2426 s
= link_ipv6_privacy_extensions(link
);
2430 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2432 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2437 static int link_set_ipv6_accept_ra(Link
*link
) {
2440 /* Make this a NOP if IPv6 is not available */
2441 if (!socket_ipv6_is_supported())
2444 if (link
->flags
& IFF_LOOPBACK
)
2450 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2452 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2457 static int link_set_ipv6_dad_transmits(Link
*link
) {
2460 /* Make this a NOP if IPv6 is not available */
2461 if (!socket_ipv6_is_supported())
2464 if (link
->flags
& IFF_LOOPBACK
)
2470 if (link
->network
->ipv6_dad_transmits
< 0)
2473 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2475 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2480 static int link_set_ipv6_hop_limit(Link
*link
) {
2483 /* Make this a NOP if IPv6 is not available */
2484 if (!socket_ipv6_is_supported())
2487 if (link
->flags
& IFF_LOOPBACK
)
2493 if (link
->network
->ipv6_hop_limit
< 0)
2496 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2498 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2503 static int link_set_ipv6_mtu(Link
*link
) {
2506 /* Make this a NOP if IPv6 is not available */
2507 if (!socket_ipv6_is_supported())
2510 if (link
->flags
& IFF_LOOPBACK
)
2513 if (link
->network
->ipv6_mtu
== 0)
2516 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2518 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2523 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2524 Address
*net_address
;
2532 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2533 if (address_equal(net_address
, address
))
2539 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2548 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2549 if (route_equal(net_route
, route
))
2555 static int link_drop_foreign_config(Link
*link
) {
2561 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2562 /* we consider IPv6LL addresses to be managed by the kernel */
2563 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2566 if (link_is_static_address_configured(link
, address
)) {
2567 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2569 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2571 r
= address_remove(address
, link
, NULL
);
2577 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2578 /* do not touch routes managed by the kernel */
2579 if (route
->protocol
== RTPROT_KERNEL
)
2582 if (link_is_static_route_configured(link
, route
)) {
2583 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2587 r
= route_remove(route
, link
, NULL
);
2596 static int link_drop_config(Link
*link
) {
2597 Address
*address
, *pool_address
;
2602 SET_FOREACH(address
, link
->addresses
, i
) {
2603 /* we consider IPv6LL addresses to be managed by the kernel */
2604 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2607 r
= address_remove(address
, link
, NULL
);
2611 /* If this address came from an address pool, clean up the pool */
2612 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2613 if (address_equal(address
, pool_address
)) {
2614 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2615 address_free(pool_address
);
2621 SET_FOREACH(route
, link
->routes
, i
) {
2622 /* do not touch routes managed by the kernel */
2623 if (route
->protocol
== RTPROT_KERNEL
)
2626 r
= route_remove(route
, link
, NULL
);
2636 static int link_configure(Link
*link
) {
2640 assert(link
->network
);
2641 assert(link
->state
== LINK_STATE_INITIALIZED
);
2643 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2644 return link_configure_can(link
);
2646 /* Drop foreign config, but ignore loopback or critical devices.
2647 * We do not want to remove loopback address or addresses used for root NFS. */
2648 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2649 r
= link_drop_foreign_config(link
);
2654 r
= link_set_proxy_arp(link
);
2658 r
= ipv6_proxy_ndp_addresses_configure(link
);
2662 r
= link_set_ipv4_forward(link
);
2666 r
= link_set_ipv6_forward(link
);
2670 r
= link_set_ipv6_privacy_extensions(link
);
2674 r
= link_set_ipv6_accept_ra(link
);
2678 r
= link_set_ipv6_dad_transmits(link
);
2682 r
= link_set_ipv6_hop_limit(link
);
2686 r
= link_set_flags(link
);
2690 r
= link_set_ipv6_mtu(link
);
2694 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2695 r
= ipv4ll_configure(link
);
2700 if (link_dhcp4_enabled(link
)) {
2701 r
= dhcp4_set_promote_secondaries(link
);
2705 r
= dhcp4_configure(link
);
2710 if (link_dhcp4_server_enabled(link
)) {
2711 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2715 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2720 if (link_dhcp6_enabled(link
) ||
2721 link_ipv6_accept_ra_enabled(link
)) {
2722 r
= dhcp6_configure(link
);
2727 if (link_ipv6_accept_ra_enabled(link
)) {
2728 r
= ndisc_configure(link
);
2733 if (link_radv_enabled(link
)) {
2734 r
= radv_configure(link
);
2739 if (link_lldp_rx_enabled(link
)) {
2740 r
= link_lldp_rx_configure(link
);
2745 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
2749 if (socket_ipv6_is_supported()) {
2750 r
= link_configure_addrgen_mode(link
);
2755 return link_configure_after_setting_mtu(link
);
2758 static int link_configure_after_setting_mtu(Link
*link
) {
2762 assert(link
->network
);
2763 assert(link
->state
== LINK_STATE_INITIALIZED
);
2765 if (link
->setting_mtu
)
2768 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2769 r
= link_acquire_conf(link
);
2774 return link_enter_join_netdev(link
);
2777 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2780 if (duid
->raw_data_len
> 0)
2783 if (duid
->type
!= DUID_TYPE_UUID
)
2786 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2787 duid
->raw_data_len
= sizeof(sd_id128_t
);
2792 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2793 Manager
*manager
= userdata
;
2794 const sd_bus_error
*e
;
2804 e
= sd_bus_message_get_error(m
);
2806 log_error_errno(sd_bus_error_get_errno(e
),
2807 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2812 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2816 if (sz
!= sizeof(sd_id128_t
)) {
2817 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2821 memcpy(&manager
->product_uuid
, a
, sz
);
2822 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2823 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2825 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2828 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2829 r
= link_configure(link
);
2831 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2834 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2836 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2837 * even if the method fails. */
2838 manager
->has_product_uuid
= true;
2843 static bool link_requires_uuid(Link
*link
) {
2847 assert(link
->manager
);
2848 assert(link
->network
);
2850 duid
= link_get_duid(link
);
2851 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2854 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2857 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2863 static int link_configure_duid(Link
*link
) {
2869 assert(link
->manager
);
2870 assert(link
->network
);
2873 duid
= link_get_duid(link
);
2875 if (!link_requires_uuid(link
))
2878 if (m
->has_product_uuid
) {
2879 (void) duid_set_uuid(duid
, m
->product_uuid
);
2883 if (!m
->links_requesting_uuid
) {
2884 r
= manager_request_product_uuid(m
, link
);
2889 log_link_warning_errno(link
, r
,
2890 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2894 r
= set_put(m
->links_requesting_uuid
, link
);
2898 r
= set_put(m
->duids_requesting_uuid
, duid
);
2906 static int link_initialized_and_synced(Link
*link
) {
2911 assert(link
->ifname
);
2912 assert(link
->manager
);
2914 /* We may get called either from the asynchronous netlink callback,
2915 * or directly for link_add() if running in a container. See link_add(). */
2916 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2919 log_link_debug(link
, "Link state is up-to-date");
2920 link_set_state(link
, LINK_STATE_INITIALIZED
);
2922 r
= link_new_bound_by_list(link
);
2926 r
= link_handle_bound_by_list(link
);
2930 if (!link
->network
) {
2931 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2932 &link
->mac
, &network
);
2934 link_enter_unmanaged(link
);
2936 } else if (r
== 0 && network
->unmanaged
) {
2937 link_enter_unmanaged(link
);
2942 if (link
->flags
& IFF_LOOPBACK
) {
2943 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2944 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2946 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2947 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2949 if (network
->dhcp_server
)
2950 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2953 r
= network_apply(network
, link
);
2958 r
= link_new_bound_to_list(link
);
2962 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2963 * link_configure() is called later asynchronously. */
2964 r
= link_configure_duid(link
);
2968 r
= link_configure(link
);
2975 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2976 (void) link_initialized_and_synced(link
);
2980 int link_initialized(Link
*link
, sd_device
*device
) {
2981 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2985 assert(link
->manager
);
2986 assert(link
->manager
->rtnl
);
2989 if (link
->state
!= LINK_STATE_PENDING
)
2992 if (link
->sd_device
)
2995 log_link_debug(link
, "udev initialized link");
2996 link_set_state(link
, LINK_STATE_INITIALIZED
);
2998 link
->sd_device
= sd_device_ref(device
);
3000 /* udev has initialized the link, but we don't know if we have yet
3001 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3002 * when it returns we know that the pending NEWLINKs have already been
3003 * processed and that we are up-to-date */
3005 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3010 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3011 link_netlink_destroy_callback
, link
);
3020 static int link_load(Link
*link
) {
3021 _cleanup_free_
char *network_file
= NULL
,
3024 *dhcp4_address
= NULL
,
3025 *ipv4ll_address
= NULL
;
3026 union in_addr_union address
;
3027 union in_addr_union route_dst
;
3033 r
= parse_env_file(NULL
, link
->state_file
,
3034 "NETWORK_FILE", &network_file
,
3035 "ADDRESSES", &addresses
,
3037 "DHCP4_ADDRESS", &dhcp4_address
,
3038 "IPV4LL_ADDRESS", &ipv4ll_address
);
3039 if (r
< 0 && r
!= -ENOENT
)
3040 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3047 suffix
= strrchr(network_file
, '.');
3049 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3050 goto network_file_fail
;
3054 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3056 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3057 goto network_file_fail
;
3060 r
= network_apply(network
, link
);
3062 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3071 _cleanup_free_
char *address_str
= NULL
;
3072 char *prefixlen_str
;
3074 unsigned char prefixlen
;
3076 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3078 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3084 prefixlen_str
= strchr(address_str
, '/');
3085 if (!prefixlen_str
) {
3086 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3090 *prefixlen_str
++ = '\0';
3092 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3094 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3098 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3100 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3104 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3106 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3115 _cleanup_free_
char *route_str
= NULL
;
3116 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3118 char *prefixlen_str
;
3120 unsigned char prefixlen
, tos
, table
;
3123 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3125 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3131 prefixlen_str
= strchr(route_str
, '/');
3132 if (!prefixlen_str
) {
3133 log_link_debug(link
, "Failed to parse route %s", route_str
);
3137 *prefixlen_str
++ = '\0';
3139 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3141 log_link_debug(link
,
3142 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3147 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3149 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3153 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3155 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3157 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3158 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3159 0, route_expire_handler
, route
);
3161 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3164 route
->lifetime
= lifetime
;
3165 sd_event_source_unref(route
->expire
);
3166 route
->expire
= TAKE_PTR(expire
);
3170 if (dhcp4_address
) {
3171 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3173 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3174 goto dhcp4_address_fail
;
3177 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3179 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3181 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3183 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3188 if (ipv4ll_address
) {
3189 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3191 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3192 goto ipv4ll_address_fail
;
3195 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3197 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3199 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3201 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3204 ipv4ll_address_fail
:
3209 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3210 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3211 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3220 r
= link_new(m
, message
, ret
);
3226 log_link_debug(link
, "Link %d added", link
->ifindex
);
3228 r
= link_load(link
);
3232 if (detect_container() <= 0) {
3233 /* not in a container, udev will be around */
3234 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3235 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3237 log_link_warning_errno(link
, r
, "Could not find device: %m");
3241 r
= sd_device_get_is_initialized(device
);
3243 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3248 log_link_debug(link
, "link pending udev initialization...");
3252 r
= device_is_renaming(device
);
3254 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3258 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3262 r
= link_initialized(link
, device
);
3266 r
= link_initialized_and_synced(link
);
3273 link_enter_failed(link
);
3277 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3282 log_link_info(link
, "Gained IPv6LL");
3284 link
->ipv6ll_address
= *address
;
3285 link_check_ready(link
);
3287 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3288 r
= link_acquire_ipv6_conf(link
);
3290 link_enter_failed(link
);
3298 static int link_carrier_gained(Link
*link
) {
3303 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3304 r
= link_acquire_conf(link
);
3306 link_enter_failed(link
);
3310 r
= link_request_set_addresses(link
);
3315 r
= link_handle_bound_by_list(link
);
3322 static int link_carrier_lost(Link
*link
) {
3327 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3330 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3331 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3332 * configuration and stop the clients as well. */
3333 if (link
->setting_mtu
)
3336 r
= link_stop_clients(link
);
3338 link_enter_failed(link
);
3342 if (link_dhcp4_server_enabled(link
))
3343 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3345 r
= link_drop_config(link
);
3349 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3350 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3351 r
= link_drop_foreign_config(link
);
3356 r
= link_handle_bound_by_list(link
);
3363 int link_carrier_reset(Link
*link
) {
3368 if (link_has_carrier(link
)) {
3369 r
= link_carrier_lost(link
);
3373 r
= link_carrier_gained(link
);
3377 log_link_info(link
, "Reset carrier");
3383 int link_update(Link
*link
, sd_netlink_message
*m
) {
3384 struct ether_addr mac
;
3387 bool had_carrier
, carrier_gained
, carrier_lost
;
3391 assert(link
->ifname
);
3394 if (link
->state
== LINK_STATE_LINGER
) {
3395 log_link_info(link
, "Link re-added");
3396 link_set_state(link
, LINK_STATE_CONFIGURING
);
3398 r
= link_new_carrier_maps(link
);
3403 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3404 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3405 Manager
*manager
= link
->manager
;
3407 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3410 r
= link_add(manager
, m
, &link
);
3415 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3416 if (r
>= 0 && mtu
> 0) {
3418 if (link
->original_mtu
== 0) {
3419 link
->original_mtu
= mtu
;
3420 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3423 if (link
->dhcp_client
) {
3424 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3427 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3431 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3433 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3437 /* The kernel may broadcast NEWLINK messages without the MAC address
3438 set, simply ignore them. */
3439 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3441 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3444 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3447 log_link_debug(link
, "MAC address: "
3448 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3449 mac
.ether_addr_octet
[0],
3450 mac
.ether_addr_octet
[1],
3451 mac
.ether_addr_octet
[2],
3452 mac
.ether_addr_octet
[3],
3453 mac
.ether_addr_octet
[4],
3454 mac
.ether_addr_octet
[5]);
3457 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3459 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3462 if (link
->dhcp_client
) {
3463 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3464 (const uint8_t *) &link
->mac
,
3468 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3470 r
= dhcp4_set_client_identifier(link
);
3475 if (link
->dhcp6_client
) {
3476 const DUID
* duid
= link_get_duid(link
);
3478 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3479 (const uint8_t *) &link
->mac
,
3483 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3485 if (link
->network
->iaid_set
) {
3486 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3487 link
->network
->iaid
);
3489 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3492 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3494 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3495 duid
->raw_data_len
);
3497 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3501 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3503 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3507 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3509 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3514 had_carrier
= link_has_carrier(link
);
3516 r
= link_update_flags(link
, m
);
3520 r
= link_update_lldp(link
);
3524 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3525 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3527 if (carrier_gained
) {
3528 log_link_info(link
, "Gained carrier");
3530 r
= link_carrier_gained(link
);
3533 } else if (carrier_lost
) {
3534 log_link_info(link
, "Lost carrier");
3536 r
= link_carrier_lost(link
);
3544 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3552 if (hashmap_isempty(h
))
3556 HASHMAP_FOREACH(link
, h
, i
) {
3560 fprintf(f
, "%i", link
->ifindex
);
3567 int link_save(Link
*link
) {
3568 _cleanup_free_
char *temp_path
= NULL
;
3569 _cleanup_fclose_
FILE *f
= NULL
;
3570 const char *admin_state
, *oper_state
;
3577 assert(link
->state_file
);
3578 assert(link
->lease_file
);
3579 assert(link
->manager
);
3581 if (link
->state
== LINK_STATE_LINGER
) {
3582 (void) unlink(link
->state_file
);
3586 link_lldp_save(link
);
3588 admin_state
= link_state_to_string(link
->state
);
3589 assert(admin_state
);
3591 oper_state
= link_operstate_to_string(link
->operstate
);
3594 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3598 (void) fchmod(fileno(f
), 0644);
3601 "# This is private data. Do not parse.\n"
3604 admin_state
, oper_state
);
3606 if (link
->network
) {
3608 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3609 const char *dhcp_domainname
= NULL
;
3610 char **dhcp6_domains
= NULL
;
3611 char **dhcp_domains
= NULL
;
3614 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3615 yes_no(link
->network
->required_for_online
));
3617 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3618 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3620 if (link
->dhcp6_client
) {
3621 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3622 if (r
< 0 && r
!= -ENOMSG
)
3623 log_link_debug(link
, "No DHCPv6 lease");
3626 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3631 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3632 _cleanup_free_
char *b
= NULL
;
3634 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3635 &link
->network
->dns
[j
].address
, &b
);
3637 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3647 if (link
->network
->dhcp_use_dns
&&
3649 const struct in_addr
*addresses
;
3651 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3653 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3657 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3658 struct in6_addr
*in6_addrs
;
3660 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3664 serialize_in6_addrs(f
, in6_addrs
, r
);
3669 /* Make sure to flush out old entries before we use the NDISC data */
3672 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3675 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3679 serialize_in6_addrs(f
, &dd
->address
, 1);
3688 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3690 if (link
->network
->dhcp_use_ntp
&&
3692 const struct in_addr
*addresses
;
3694 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3696 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3700 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3701 struct in6_addr
*in6_addrs
;
3704 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3709 serialize_in6_addrs(f
, in6_addrs
, r
);
3713 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3715 fputstrv(f
, hosts
, NULL
, &space
);
3720 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3721 if (link
->dhcp_lease
) {
3722 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3723 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3726 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3729 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
3731 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3734 if (dhcp_domainname
)
3735 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3737 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3739 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3741 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3742 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3747 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
3749 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3752 if (dhcp_domainname
)
3753 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3755 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3757 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3759 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3760 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3765 fprintf(f
, "LLMNR=%s\n",
3766 resolve_support_to_string(link
->network
->llmnr
));
3767 fprintf(f
, "MDNS=%s\n",
3768 resolve_support_to_string(link
->network
->mdns
));
3769 if (link
->network
->dns_default_route
>= 0)
3770 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3772 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3773 fprintf(f
, "DNS_OVER_TLS=%s\n",
3774 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3776 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3777 fprintf(f
, "DNSSEC=%s\n",
3778 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3780 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3783 fputs("DNSSEC_NTA=", f
);
3785 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3786 fputs_with_space(f
, n
, NULL
, &space
);
3790 fputs("ADDRESSES=", f
);
3792 SET_FOREACH(a
, link
->addresses
, i
) {
3793 _cleanup_free_
char *address_str
= NULL
;
3795 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3799 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3804 fputs("ROUTES=", f
);
3806 SET_FOREACH(route
, link
->routes
, i
) {
3807 _cleanup_free_
char *route_str
= NULL
;
3809 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3813 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3814 space
? " " : "", route_str
,
3815 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3822 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3823 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3825 if (link
->dhcp_lease
) {
3826 struct in_addr address
;
3827 const char *tz
= NULL
;
3829 assert(link
->network
);
3831 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3833 fprintf(f
, "TIMEZONE=%s\n", tz
);
3835 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3837 fputs("DHCP4_ADDRESS=", f
);
3838 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3842 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3850 (void) unlink(link
->lease_file
);
3853 struct in_addr address
;
3855 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3857 fputs("IPV4LL_ADDRESS=", f
);
3858 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3863 r
= fflush_and_check(f
);
3867 if (rename(temp_path
, link
->state_file
) < 0) {
3875 (void) unlink(link
->state_file
);
3877 (void) unlink(temp_path
);
3879 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3882 /* The serialized state in /run is no longer up-to-date. */
3883 void link_dirty(Link
*link
) {
3888 /* mark manager dirty as link is dirty */
3889 manager_dirty(link
->manager
);
3891 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3893 /* allocation errors are ignored */
3896 r
= set_put(link
->manager
->dirty_links
, link
);
3898 /* don't take another ref if the link was already dirty */
3904 /* The serialized state in /run is up-to-date */
3905 void link_clean(Link
*link
) {
3907 assert(link
->manager
);
3909 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3912 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3913 [LINK_STATE_PENDING
] = "pending",
3914 [LINK_STATE_INITIALIZED
] = "initialized",
3915 [LINK_STATE_CONFIGURING
] = "configuring",
3916 [LINK_STATE_CONFIGURED
] = "configured",
3917 [LINK_STATE_UNMANAGED
] = "unmanaged",
3918 [LINK_STATE_FAILED
] = "failed",
3919 [LINK_STATE_LINGER
] = "linger",
3922 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);