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/vrf.h"
18 #include "netlink-util.h"
19 #include "network-internal.h"
20 #include "networkd-can.h"
21 #include "networkd-ipv6-proxy-ndp.h"
22 #include "networkd-lldp-tx.h"
23 #include "networkd-manager.h"
24 #include "networkd-ndisc.h"
25 #include "networkd-neighbor.h"
26 #include "networkd-radv.h"
27 #include "networkd-routing-policy-rule.h"
29 #include "socket-util.h"
30 #include "stdio-util.h"
31 #include "string-table.h"
33 #include "sysctl-util.h"
34 #include "tmpfile-util.h"
35 #include "udev-util.h"
39 uint32_t link_get_vrf_table(Link
*link
) {
40 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
43 uint32_t link_get_dhcp_route_table(Link
*link
) {
44 /* When the interface is part of an VRF use the VRFs routing table, unless
45 * another table is explicitly specified. */
46 if (link
->network
->dhcp_route_table_set
)
47 return link
->network
->dhcp_route_table
;
48 return link_get_vrf_table(link
);
51 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
52 if (link
->network
->ipv6_accept_ra_route_table_set
)
53 return link
->network
->ipv6_accept_ra_route_table
;
54 return link_get_vrf_table(link
);
57 DUID
* link_get_duid(Link
*link
) {
58 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
59 return &link
->network
->duid
;
61 return &link
->manager
->duid
;
64 static bool link_dhcp6_enabled(Link
*link
) {
67 if (!socket_ipv6_is_supported())
70 if (link
->flags
& IFF_LOOPBACK
)
76 if (link
->network
->bond
)
79 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
82 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
85 static bool link_dhcp4_enabled(Link
*link
) {
88 if (link
->flags
& IFF_LOOPBACK
)
94 if (link
->network
->bond
)
97 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
100 static bool link_dhcp4_server_enabled(Link
*link
) {
103 if (link
->flags
& IFF_LOOPBACK
)
109 if (link
->network
->bond
)
112 return link
->network
->dhcp_server
;
115 bool link_ipv4ll_enabled(Link
*link
) {
118 if (link
->flags
& IFF_LOOPBACK
)
124 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
127 if (link
->network
->bond
)
130 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
133 bool link_ipv4ll_fallback_enabled(Link
*link
) {
136 if (link
->flags
& IFF_LOOPBACK
)
142 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
145 if (link
->network
->bond
)
148 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
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"))
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 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
188 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
191 static bool link_radv_enabled(Link
*link
) {
194 if (!link_ipv6ll_enabled(link
))
197 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
200 static bool link_ipv4_forward_enabled(Link
*link
) {
203 if (link
->flags
& IFF_LOOPBACK
)
209 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
212 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
215 static bool link_ipv6_forward_enabled(Link
*link
) {
218 if (!socket_ipv6_is_supported())
221 if (link
->flags
& IFF_LOOPBACK
)
227 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
230 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
233 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
236 static bool link_proxy_arp_enabled(Link
*link
) {
239 if (link
->flags
& IFF_LOOPBACK
)
245 if (link
->network
->proxy_arp
< 0)
251 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
254 if (!socket_ipv6_is_supported())
257 if (link
->flags
& IFF_LOOPBACK
)
263 if (!link_ipv6ll_enabled(link
))
266 /* If unset use system default (enabled if local forwarding is disabled.
267 * disabled if local forwarding is enabled).
268 * If set, ignore or enforce RA independent of local forwarding state.
270 if (link
->network
->ipv6_accept_ra
< 0)
271 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
272 return !link_ipv6_forward_enabled(link
);
273 else if (link
->network
->ipv6_accept_ra
> 0)
274 /* accept RA even if ip_forward is enabled */
281 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
284 if (!socket_ipv6_is_supported())
285 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
287 if (link
->flags
& IFF_LOOPBACK
)
288 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
291 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
293 return link
->network
->ipv6_privacy_extensions
;
296 static int link_enable_ipv6(Link
*link
) {
300 if (link
->flags
& IFF_LOOPBACK
)
303 disabled
= !link_ipv6_enabled(link
);
305 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
307 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
309 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
314 static bool link_is_enslaved(Link
*link
) {
315 if (link
->flags
& IFF_SLAVE
)
316 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
319 if (!link
->enslaved_raw
)
325 if (link
->network
->bridge
)
326 /* TODO: support the case when link is not managed by networkd. */
332 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
338 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
341 link_update_operstate(master
, true);
344 void link_update_operstate(Link
*link
, bool also_update_master
) {
345 LinkOperationalState operstate
;
350 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
351 operstate
= LINK_OPERSTATE_DORMANT
;
352 else if (link_has_carrier(link
)) {
354 uint8_t scope
= RT_SCOPE_NOWHERE
;
356 /* if we have carrier, check what addresses we have */
357 SET_FOREACH(address
, link
->addresses
, i
) {
358 if (!address_is_ready(address
))
361 if (address
->scope
< scope
)
362 scope
= address
->scope
;
365 /* for operstate we also take foreign addresses into account */
366 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
367 if (!address_is_ready(address
))
370 if (address
->scope
< scope
)
371 scope
= address
->scope
;
374 if (scope
< RT_SCOPE_SITE
)
375 /* universally accessible addresses found */
376 operstate
= LINK_OPERSTATE_ROUTABLE
;
377 else if (scope
< RT_SCOPE_HOST
)
378 /* only link or site local addresses found */
379 operstate
= LINK_OPERSTATE_DEGRADED
;
381 /* no useful addresses found */
382 operstate
= LINK_OPERSTATE_CARRIER
;
383 } else if (link
->flags
& IFF_UP
)
384 operstate
= LINK_OPERSTATE_NO_CARRIER
;
386 operstate
= LINK_OPERSTATE_OFF
;
388 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
389 link_is_enslaved(link
))
390 operstate
= LINK_OPERSTATE_ENSLAVED
;
392 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
395 SET_FOREACH(slave
, link
->slaves
, i
) {
396 link_update_operstate(slave
, false);
398 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
399 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
403 if (link
->operstate
!= operstate
) {
404 link
->operstate
= operstate
;
405 link_send_changed(link
, "OperationalState", NULL
);
409 if (also_update_master
&& link
->network
) {
410 link_update_master_operstate(link
, link
->network
->bond
);
411 link_update_master_operstate(link
, link
->network
->bridge
);
415 #define FLAG_STRING(string, flag, old, new) \
416 (((old ^ new) & flag) \
417 ? ((old & flag) ? (" -" string) : (" +" string)) \
420 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
421 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
427 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
429 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
431 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
433 /* if we got a message without operstate, take it to mean
434 the state was unchanged */
435 operstate
= link
->kernel_operstate
;
437 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
440 if (link
->flags
!= flags
) {
441 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",
442 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
443 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
444 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
445 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
446 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
447 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
448 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
449 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
450 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
451 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
452 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
453 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
454 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
455 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
456 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
457 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
458 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
459 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
460 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
462 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
463 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
464 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
465 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
466 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
467 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
468 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
469 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
471 /* link flags are currently at most 18 bits, let's align to
473 if (unknown_flags_added
)
475 "Unknown link flags gained: %#.5x (ignoring)",
476 unknown_flags_added
);
478 if (unknown_flags_removed
)
480 "Unknown link flags lost: %#.5x (ignoring)",
481 unknown_flags_removed
);
485 link
->kernel_operstate
= operstate
;
487 link_update_operstate(link
, true);
492 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
493 _cleanup_(link_unrefp
) Link
*link
= NULL
;
495 const char *ifname
, *kind
= NULL
;
497 unsigned short iftype
;
503 /* check for link kind */
504 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
506 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
507 r
= sd_netlink_message_exit_container(message
);
512 r
= sd_netlink_message_get_type(message
, &type
);
515 else if (type
!= RTM_NEWLINK
)
518 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
521 else if (ifindex
<= 0)
524 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
528 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
539 .state
= LINK_STATE_PENDING
,
540 .rtnl_extended_attrs
= true,
545 link
->ifname
= strdup(ifname
);
550 link
->kind
= strdup(kind
);
555 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
557 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
559 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
561 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
563 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
566 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
569 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
572 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
576 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
580 r
= link_update_flags(link
, message
);
584 *ret
= TAKE_PTR(link
);
589 static Link
*link_free(Link
*link
) {
594 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
595 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
597 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
598 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
600 while ((address
= link
->pool_addresses
)) {
601 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
602 address_free(address
);
605 sd_dhcp_server_unref(link
->dhcp_server
);
606 sd_dhcp_client_unref(link
->dhcp_client
);
607 sd_dhcp_lease_unref(link
->dhcp_lease
);
609 link_lldp_emit_stop(link
);
611 free(link
->lease_file
);
613 sd_lldp_unref(link
->lldp
);
614 free(link
->lldp_file
);
618 sd_ipv4ll_unref(link
->ipv4ll
);
619 sd_dhcp6_client_unref(link
->dhcp6_client
);
620 sd_ndisc_unref(link
->ndisc
);
621 sd_radv_unref(link
->radv
);
626 (void) unlink(link
->state_file
);
627 free(link
->state_file
);
629 sd_device_unref(link
->sd_device
);
631 hashmap_free(link
->bound_to_links
);
632 hashmap_free(link
->bound_by_links
);
634 set_free_with_destructor(link
->slaves
, link_unref
);
636 network_unref(link
->network
);
641 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
643 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
650 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
659 static void link_set_state(Link
*link
, LinkState state
) {
662 if (link
->state
== state
)
665 log_link_debug(link
, "State changed: %s -> %s",
666 link_state_to_string(link
->state
),
667 link_state_to_string(state
));
671 link_send_changed(link
, "AdministrativeState", NULL
);
674 static void link_enter_unmanaged(Link
*link
) {
677 log_link_debug(link
, "Unmanaged");
679 link_set_state(link
, LINK_STATE_UNMANAGED
);
684 int link_stop_clients(Link
*link
) {
688 assert(link
->manager
);
689 assert(link
->manager
->event
);
691 if (link
->dhcp_client
) {
692 k
= sd_dhcp_client_stop(link
->dhcp_client
);
694 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
698 k
= sd_ipv4ll_stop(link
->ipv4ll
);
700 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
703 if (link
->dhcp6_client
) {
704 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
706 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
710 k
= sd_ndisc_stop(link
->ndisc
);
712 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
716 k
= sd_radv_stop(link
->radv
);
718 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
721 link_lldp_emit_stop(link
);
725 void link_enter_failed(Link
*link
) {
728 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
731 log_link_warning(link
, "Failed");
733 link_set_state(link
, LINK_STATE_FAILED
);
735 link_stop_clients(link
);
740 static Address
* link_find_dhcp_server_address(Link
*link
) {
744 assert(link
->network
);
746 /* The first statically configured address if there is any */
747 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
749 if (address
->family
!= AF_INET
)
752 if (in_addr_is_null(address
->family
, &address
->in_addr
))
758 /* If that didn't work, find a suitable address we got from the pool */
759 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
760 if (address
->family
!= AF_INET
)
769 static int link_join_netdevs_after_configured(Link
*link
) {
774 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
775 if (netdev
->ifindex
> 0)
776 /* Assume already enslaved. */
779 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
782 log_struct(LOG_DEBUG
,
783 LOG_LINK_INTERFACE(link
),
784 LOG_NETDEV_INTERFACE(netdev
),
785 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
787 r
= netdev_join(netdev
, link
, NULL
);
789 return log_struct_errno(LOG_WARNING
, r
,
790 LOG_LINK_INTERFACE(link
),
791 LOG_NETDEV_INTERFACE(netdev
),
792 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
798 static void link_enter_configured(Link
*link
) {
800 assert(link
->network
);
802 if (link
->state
!= LINK_STATE_CONFIGURING
)
805 log_link_info(link
, "Configured");
807 link_set_state(link
, LINK_STATE_CONFIGURED
);
809 (void) link_join_netdevs_after_configured(link
);
814 static int link_request_set_routing_policy_rule(Link
*link
) {
815 RoutingPolicyRule
*rule
, *rrule
= NULL
;
819 assert(link
->network
);
821 link_set_state(link
, LINK_STATE_CONFIGURING
);
822 link
->routing_policy_rules_configured
= false;
824 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
825 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
826 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
827 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
829 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
833 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
835 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
836 link_enter_failed(link
);
840 link
->routing_policy_rule_messages
++;
843 routing_policy_rule_purge(link
->manager
, link
);
844 if (link
->routing_policy_rule_messages
== 0) {
845 link
->routing_policy_rules_configured
= true;
846 link_check_ready(link
);
848 log_link_debug(link
, "Setting routing policy rules");
853 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
857 assert(link
->route_messages
> 0);
858 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
859 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
861 link
->route_messages
--;
863 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
866 r
= sd_netlink_message_get_errno(m
);
867 if (r
< 0 && r
!= -EEXIST
)
868 log_link_warning_errno(link
, r
, "Could not set route: %m");
870 if (link
->route_messages
== 0) {
871 log_link_debug(link
, "Routes set");
872 link
->static_routes_configured
= true;
873 link_check_ready(link
);
879 int link_request_set_routes(Link
*link
) {
881 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
882 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
889 assert(link
->network
);
890 assert(link
->addresses_configured
);
891 assert(link
->address_messages
== 0);
892 assert(link
->state
!= _LINK_STATE_INVALID
);
894 link_set_state(link
, LINK_STATE_CONFIGURING
);
895 link
->static_routes_configured
= false;
897 r
= link_request_set_routing_policy_rule(link
);
901 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
902 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
903 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
905 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
908 r
= route_configure(rt
, link
, route_handler
);
910 log_link_warning_errno(link
, r
, "Could not set routes: %m");
911 link_enter_failed(link
);
915 link
->route_messages
++;
918 if (link
->route_messages
== 0) {
919 link
->static_routes_configured
= true;
920 link_check_ready(link
);
922 log_link_debug(link
, "Setting routes");
927 void link_check_ready(Link
*link
) {
933 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
939 if (!link
->addresses_configured
)
942 if (!link
->neighbors_configured
)
945 SET_FOREACH(a
, link
->addresses
, i
)
946 if (!address_is_ready(a
))
949 if (!link
->addresses_ready
) {
950 link
->addresses_ready
= true;
951 link_request_set_routes(link
);
954 if (!link
->static_routes_configured
)
957 if (!link
->routing_policy_rules_configured
)
960 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
963 if (link_ipv6ll_enabled(link
) &&
964 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
967 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
968 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
969 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
970 /* When DHCP is enabled, at least one protocol must provide an address, or
971 * an IPv4ll fallback address must be configured. */
974 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
977 if (link
->state
!= LINK_STATE_CONFIGURED
)
978 link_enter_configured(link
);
983 static int link_request_set_neighbors(Link
*link
) {
988 assert(link
->network
);
989 assert(link
->state
!= _LINK_STATE_INVALID
);
991 link_set_state(link
, LINK_STATE_CONFIGURING
);
992 link
->neighbors_configured
= false;
994 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
995 r
= neighbor_configure(neighbor
, link
, NULL
);
997 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
998 link_enter_failed(link
);
1003 if (link
->neighbor_messages
== 0) {
1004 link
->neighbors_configured
= true;
1005 link_check_ready(link
);
1007 log_link_debug(link
, "Setting neighbors");
1012 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1018 assert(link
->ifname
);
1019 assert(link
->address_messages
> 0);
1020 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1021 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1023 link
->address_messages
--;
1025 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1028 r
= sd_netlink_message_get_errno(m
);
1029 if (r
< 0 && r
!= -EEXIST
)
1030 log_link_warning_errno(link
, r
, "could not set address: %m");
1032 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1034 if (link
->address_messages
== 0) {
1035 log_link_debug(link
, "Addresses set");
1036 link
->addresses_configured
= true;
1037 link_check_ready(link
);
1043 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1044 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1045 size_t n_addresses
= 0, n_allocated
= 0;
1048 log_debug("Copying DNS server information from %s", link
->ifname
);
1053 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1056 /* Only look for IPv4 addresses */
1057 if (link
->network
->dns
[i
].family
!= AF_INET
)
1060 ia
= link
->network
->dns
[i
].address
.in
;
1062 /* Never propagate obviously borked data */
1063 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1066 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1069 addresses
[n_addresses
++] = ia
;
1072 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1073 const struct in_addr
*da
= NULL
;
1076 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1079 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1082 for (j
= 0; j
< n
; j
++)
1083 if (in4_addr_is_non_local(&da
[j
]))
1084 addresses
[n_addresses
++] = da
[j
];
1088 if (n_addresses
<= 0)
1091 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1094 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1095 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1096 size_t n_addresses
= 0, n_allocated
= 0;
1102 log_debug("Copying NTP server information from %s", link
->ifname
);
1104 STRV_FOREACH(a
, link
->network
->ntp
) {
1105 union in_addr_union ia
;
1107 /* Only look for IPv4 addresses */
1108 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1111 /* Never propagate obviously borked data */
1112 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1115 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1118 addresses
[n_addresses
++] = ia
.in
;
1121 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1122 const struct in_addr
*da
= NULL
;
1125 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1128 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1131 for (j
= 0; j
< n
; j
++)
1132 if (in4_addr_is_non_local(&da
[j
]))
1133 addresses
[n_addresses
++] = da
[j
];
1137 if (n_addresses
<= 0)
1140 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1143 static int link_set_bridge_fdb(Link
*link
) {
1144 FdbEntry
*fdb_entry
;
1147 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1148 r
= fdb_entry_configure(link
, fdb_entry
);
1150 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1156 static int link_request_set_addresses(Link
*link
) {
1157 AddressLabel
*label
;
1162 assert(link
->network
);
1163 assert(link
->state
!= _LINK_STATE_INVALID
);
1165 link_set_state(link
, LINK_STATE_CONFIGURING
);
1167 /* Reset all *_configured flags we are configuring. */
1168 link
->addresses_configured
= false;
1169 link
->addresses_ready
= false;
1170 link
->neighbors_configured
= false;
1171 link
->static_routes_configured
= false;
1172 link
->routing_policy_rules_configured
= false;
1174 r
= link_set_bridge_fdb(link
);
1178 r
= link_request_set_neighbors(link
);
1182 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1185 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1187 r
= address_configure(ad
, link
, address_handler
, update
);
1189 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1190 link_enter_failed(link
);
1194 link
->address_messages
++;
1197 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1198 r
= address_label_configure(label
, link
, NULL
, false);
1200 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1201 link_enter_failed(link
);
1205 link
->address_label_messages
++;
1208 /* now that we can figure out a default address for the dhcp server,
1210 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1212 Link
*uplink
= NULL
;
1213 bool acquired_uplink
= false;
1215 address
= link_find_dhcp_server_address(link
);
1217 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1218 link_enter_failed(link
);
1222 /* use the server address' subnet as the pool */
1223 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1224 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1229 r = sd_dhcp_server_set_router(link->dhcp_server,
1230 &main_address->in_addr.in);
1235 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1236 r
= sd_dhcp_server_set_max_lease_time(
1238 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1243 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1244 r
= sd_dhcp_server_set_default_lease_time(
1246 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1251 if (link
->network
->dhcp_server_emit_dns
) {
1253 if (link
->network
->n_dhcp_server_dns
> 0)
1254 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1256 uplink
= manager_find_uplink(link
->manager
, link
);
1257 acquired_uplink
= true;
1260 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1263 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1266 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1269 if (link
->network
->dhcp_server_emit_ntp
) {
1271 if (link
->network
->n_dhcp_server_ntp
> 0)
1272 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1274 if (!acquired_uplink
)
1275 uplink
= manager_find_uplink(link
->manager
, link
);
1278 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1281 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1285 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1288 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1290 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1292 if (link
->network
->dhcp_server_emit_timezone
) {
1293 _cleanup_free_
char *buffer
= NULL
;
1294 const char *tz
= NULL
;
1296 if (link
->network
->dhcp_server_timezone
)
1297 tz
= link
->network
->dhcp_server_timezone
;
1299 r
= get_timezone(&buffer
);
1301 log_warning_errno(r
, "Failed to determine timezone: %m");
1307 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1312 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1313 r
= sd_dhcp_server_start(link
->dhcp_server
);
1315 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1317 link_enter_failed(link
);
1323 log_link_debug(link
, "Offering DHCPv4 leases");
1326 if (link
->address_messages
== 0) {
1327 link
->addresses_configured
= true;
1328 link_check_ready(link
);
1330 log_link_debug(link
, "Setting addresses");
1335 static int link_set_bridge_vlan(Link
*link
) {
1338 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1340 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1345 static int link_set_proxy_arp(Link
*link
) {
1348 if (!link_proxy_arp_enabled(link
))
1351 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1353 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1358 static int link_configure_after_setting_mtu(Link
*link
);
1360 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1365 assert(link
->ifname
);
1367 link
->setting_mtu
= false;
1369 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1372 r
= sd_netlink_message_get_errno(m
);
1374 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1378 log_link_debug(link
, "Setting MTU done.");
1380 if (link
->state
== LINK_STATE_INITIALIZED
)
1381 (void) link_configure_after_setting_mtu(link
);
1386 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1387 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1391 assert(link
->manager
);
1392 assert(link
->manager
->rtnl
);
1394 if (mtu
== 0 || link
->setting_mtu
)
1397 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1400 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1402 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1404 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1406 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1407 * for this interface, then disable IPv6 else enable it. */
1408 (void) link_enable_ipv6(link
);
1410 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1411 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1412 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1414 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1415 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1420 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1422 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1424 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1425 link_netlink_destroy_callback
, link
);
1427 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1430 link
->setting_mtu
= true;
1435 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1440 assert(link
->ifname
);
1442 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1445 r
= sd_netlink_message_get_errno(m
);
1447 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1452 static int link_set_flags(Link
*link
) {
1453 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1454 unsigned ifi_change
= 0;
1455 unsigned ifi_flags
= 0;
1459 assert(link
->manager
);
1460 assert(link
->manager
->rtnl
);
1462 if (link
->flags
& IFF_LOOPBACK
)
1468 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1471 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1473 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1475 if (link
->network
->arp
>= 0) {
1476 ifi_change
|= IFF_NOARP
;
1477 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1480 if (link
->network
->multicast
>= 0) {
1481 ifi_change
|= IFF_MULTICAST
;
1482 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1485 if (link
->network
->allmulticast
>= 0) {
1486 ifi_change
|= IFF_ALLMULTI
;
1487 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1490 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1492 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1494 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1495 link_netlink_destroy_callback
, link
);
1497 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1504 static int link_acquire_ipv6_conf(Link
*link
) {
1509 if (link_ipv6_accept_ra_enabled(link
)) {
1510 assert(link
->ndisc
);
1512 log_link_debug(link
, "Discovering IPv6 routers");
1514 r
= sd_ndisc_start(link
->ndisc
);
1515 if (r
< 0 && r
!= -EBUSY
)
1516 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1519 if (link_radv_enabled(link
)) {
1521 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1523 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1525 r
= sd_radv_start(link
->radv
);
1526 if (r
< 0 && r
!= -EBUSY
)
1527 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1530 (void) dhcp6_request_prefix_delegation(link
);
1535 static int link_acquire_ipv4_conf(Link
*link
) {
1539 assert(link
->manager
);
1540 assert(link
->manager
->event
);
1542 if (link_ipv4ll_enabled(link
)) {
1543 assert(link
->ipv4ll
);
1545 log_link_debug(link
, "Acquiring IPv4 link-local address");
1547 r
= sd_ipv4ll_start(link
->ipv4ll
);
1549 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1552 if (link_dhcp4_enabled(link
)) {
1553 assert(link
->dhcp_client
);
1555 log_link_debug(link
, "Acquiring DHCPv4 lease");
1557 r
= sd_dhcp_client_start(link
->dhcp_client
);
1559 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1565 static int link_acquire_conf(Link
*link
) {
1570 r
= link_acquire_ipv4_conf(link
);
1574 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1575 r
= link_acquire_ipv6_conf(link
);
1580 if (link_lldp_emit_enabled(link
)) {
1581 r
= link_lldp_emit_start(link
);
1583 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1589 bool link_has_carrier(Link
*link
) {
1590 /* see Documentation/networking/operstates.txt in the kernel sources */
1592 if (link
->kernel_operstate
== IF_OPER_UP
)
1595 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1596 /* operstate may not be implemented, so fall back to flags */
1597 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1603 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1608 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1611 r
= sd_netlink_message_get_errno(m
);
1613 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1618 static int link_configure_addrgen_mode(Link
*link
) {
1619 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1620 uint8_t ipv6ll_mode
;
1624 assert(link
->network
);
1625 assert(link
->manager
);
1626 assert(link
->manager
->rtnl
);
1628 log_link_debug(link
, "Setting address genmode for link");
1630 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1632 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1634 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1636 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1638 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1640 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1642 if (!link_ipv6ll_enabled(link
))
1643 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1644 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1645 /* The file may not exist. And event if it exists, when stable_secret is unset,
1646 * reading the file fails with EIO. */
1647 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1649 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1651 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1653 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1655 r
= sd_netlink_message_close_container(req
);
1657 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1659 r
= sd_netlink_message_close_container(req
);
1661 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1663 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1664 link_netlink_destroy_callback
, link
);
1666 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1673 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1678 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1681 r
= sd_netlink_message_get_errno(m
);
1683 /* we warn but don't fail the link, as it may be brought up later */
1684 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1689 static int link_up(Link
*link
) {
1690 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1694 assert(link
->network
);
1695 assert(link
->manager
);
1696 assert(link
->manager
->rtnl
);
1698 log_link_debug(link
, "Bringing link up");
1700 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1702 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1704 /* set it free if not enslaved with networkd */
1705 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1706 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1708 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1711 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1713 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1715 if (link
->network
->mac
) {
1716 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1718 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1721 if (link_ipv6_enabled(link
)) {
1722 uint8_t ipv6ll_mode
;
1724 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1726 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1728 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1729 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1731 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1733 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1734 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1736 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1739 if (!link_ipv6ll_enabled(link
))
1740 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1741 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1742 /* The file may not exist. And event if it exists, when stable_secret is unset,
1743 * reading the file fails with EIO. */
1744 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1746 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1748 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1750 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1752 r
= sd_netlink_message_close_container(req
);
1754 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1756 r
= sd_netlink_message_close_container(req
);
1758 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1761 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1762 link_netlink_destroy_callback
, link
);
1764 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1771 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1776 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1779 r
= sd_netlink_message_get_errno(m
);
1781 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1786 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1787 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1791 assert(link
->manager
);
1792 assert(link
->manager
->rtnl
);
1794 log_link_debug(link
, "Bringing link down");
1796 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1797 RTM_SETLINK
, link
->ifindex
);
1799 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1801 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1803 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1805 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1806 callback
?: link_down_handler
,
1807 link_netlink_destroy_callback
, link
);
1809 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1816 static int link_handle_bound_to_list(Link
*link
) {
1820 bool required_up
= false;
1821 bool link_is_up
= false;
1825 if (hashmap_isempty(link
->bound_to_links
))
1828 if (link
->flags
& IFF_UP
)
1831 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1832 if (link_has_carrier(l
)) {
1837 if (!required_up
&& link_is_up
) {
1838 r
= link_down(link
, NULL
);
1841 } else if (required_up
&& !link_is_up
) {
1850 static int link_handle_bound_by_list(Link
*link
) {
1857 if (hashmap_isempty(link
->bound_by_links
))
1860 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1861 r
= link_handle_bound_to_list(l
);
1869 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1875 if (link
== carrier
)
1878 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1881 r
= hashmap_ensure_allocated(h
, NULL
);
1885 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1892 static int link_new_bound_by_list(Link
*link
) {
1897 bool list_updated
= false;
1900 assert(link
->manager
);
1904 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1905 if (!carrier
->network
)
1908 if (strv_isempty(carrier
->network
->bind_carrier
))
1911 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1912 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1916 list_updated
= true;
1923 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1924 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1928 link_dirty(carrier
);
1934 static int link_new_bound_to_list(Link
*link
) {
1939 bool list_updated
= false;
1942 assert(link
->manager
);
1947 if (strv_isempty(link
->network
->bind_carrier
))
1952 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1953 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1954 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1958 list_updated
= true;
1965 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1966 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1970 link_dirty(carrier
);
1976 static int link_new_carrier_maps(Link
*link
) {
1979 r
= link_new_bound_by_list(link
);
1983 r
= link_handle_bound_by_list(link
);
1987 r
= link_new_bound_to_list(link
);
1991 r
= link_handle_bound_to_list(link
);
1998 static void link_free_bound_to_list(Link
*link
) {
2002 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2003 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2005 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2006 link_dirty(bound_to
);
2012 static void link_free_bound_by_list(Link
*link
) {
2016 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2017 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2019 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2020 link_dirty(bound_by
);
2021 link_handle_bound_to_list(bound_by
);
2028 static void link_free_carrier_maps(Link
*link
) {
2029 bool list_updated
= false;
2033 if (!hashmap_isempty(link
->bound_to_links
)) {
2034 link_free_bound_to_list(link
);
2035 list_updated
= true;
2038 if (!hashmap_isempty(link
->bound_by_links
)) {
2039 link_free_bound_by_list(link
);
2040 list_updated
= true;
2049 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2056 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2060 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2064 r
= set_put(master
->slaves
, link
);
2072 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2077 if (!link
->manager
|| !netdev
)
2080 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2083 link_unref(set_remove(master
->slaves
, link
));
2086 static void link_detach_from_manager(Link
*link
) {
2087 if (!link
|| !link
->manager
)
2090 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2093 /* The following must be called at last. */
2094 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2098 void link_drop(Link
*link
) {
2099 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2102 link_set_state(link
, LINK_STATE_LINGER
);
2104 link_free_carrier_maps(link
);
2106 if (link
->network
) {
2107 link_drop_from_master(link
, link
->network
->bridge
);
2108 link_drop_from_master(link
, link
->network
->bond
);
2111 log_link_debug(link
, "Link removed");
2113 (void) unlink(link
->state_file
);
2114 link_detach_from_manager(link
);
2117 static int link_joined(Link
*link
) {
2121 assert(link
->network
);
2123 if (!hashmap_isempty(link
->bound_to_links
)) {
2124 r
= link_handle_bound_to_list(link
);
2127 } else if (!(link
->flags
& IFF_UP
)) {
2130 link_enter_failed(link
);
2135 if (link
->network
->bridge
) {
2136 r
= link_set_bridge(link
);
2138 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2140 r
= link_append_to_master(link
, link
->network
->bridge
);
2142 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2145 if (link
->network
->bond
) {
2146 r
= link_set_bond(link
);
2148 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2150 r
= link_append_to_master(link
, link
->network
->bond
);
2152 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2155 if (link
->network
->use_br_vlan
&&
2156 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2157 r
= link_set_bridge_vlan(link
);
2159 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2162 /* Skip setting up addresses until it gets carrier,
2163 or it would try to set addresses twice,
2164 which is bad for non-idempotent steps. */
2165 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2168 return link_request_set_addresses(link
);
2171 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2175 assert(link
->network
);
2176 assert(link
->enslaving
> 0);
2177 assert(!link
->enslaved_raw
);
2181 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2184 r
= sd_netlink_message_get_errno(m
);
2185 if (r
< 0 && r
!= -EEXIST
) {
2186 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2187 link_enter_failed(link
);
2190 log_link_debug(link
, "Joined netdev");
2192 if (link
->enslaving
== 0) {
2193 link
->enslaved_raw
= true;
2200 static int link_enter_join_netdev(Link
*link
) {
2206 assert(link
->network
);
2207 assert(link
->state
== LINK_STATE_INITIALIZED
);
2209 link_set_state(link
, LINK_STATE_CONFIGURING
);
2212 link
->enslaving
= 0;
2213 link
->enslaved_raw
= false;
2215 if (link
->network
->bond
) {
2216 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2217 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2218 return link_joined(link
);
2220 log_struct(LOG_DEBUG
,
2221 LOG_LINK_INTERFACE(link
),
2222 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2223 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2227 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2229 log_struct_errno(LOG_WARNING
, r
,
2230 LOG_LINK_INTERFACE(link
),
2231 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2232 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2233 link_enter_failed(link
);
2238 if (link
->network
->bridge
) {
2239 log_struct(LOG_DEBUG
,
2240 LOG_LINK_INTERFACE(link
),
2241 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2242 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2246 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2248 log_struct_errno(LOG_WARNING
, r
,
2249 LOG_LINK_INTERFACE(link
),
2250 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2251 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2252 link_enter_failed(link
);
2257 if (link
->network
->vrf
) {
2258 log_struct(LOG_DEBUG
,
2259 LOG_LINK_INTERFACE(link
),
2260 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2261 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2265 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2267 log_struct_errno(LOG_WARNING
, r
,
2268 LOG_LINK_INTERFACE(link
),
2269 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2270 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2271 link_enter_failed(link
);
2276 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2278 if (netdev
->ifindex
> 0)
2279 /* Assume already enslaved. */
2282 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2285 log_struct(LOG_DEBUG
,
2286 LOG_LINK_INTERFACE(link
),
2287 LOG_NETDEV_INTERFACE(netdev
),
2288 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2292 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2294 log_struct_errno(LOG_WARNING
, r
,
2295 LOG_LINK_INTERFACE(link
),
2296 LOG_NETDEV_INTERFACE(netdev
),
2297 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2298 link_enter_failed(link
);
2303 if (link
->enslaving
== 0)
2304 return link_joined(link
);
2309 static int link_set_ipv4_forward(Link
*link
) {
2312 if (!link_ipv4_forward_enabled(link
))
2315 /* We propagate the forwarding flag from one interface to the
2316 * global setting one way. This means: as long as at least one
2317 * interface was configured at any time that had IP forwarding
2318 * enabled the setting will stay on for good. We do this
2319 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2320 * somewhat in sync (see below). */
2322 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2324 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2329 static int link_set_ipv6_forward(Link
*link
) {
2332 if (!link_ipv6_forward_enabled(link
))
2335 /* On Linux, the IPv6 stack does not know a per-interface
2336 * packet forwarding setting: either packet forwarding is on
2337 * for all, or off for all. We hence don't bother with a
2338 * per-interface setting, but simply propagate the interface
2339 * flag, if it is set, to the global flag, one-way. Note that
2340 * while IPv4 would allow a per-interface flag, we expose the
2341 * same behaviour there and also propagate the setting from
2342 * one to all, to keep things simple (see above). */
2344 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2346 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2351 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2352 IPv6PrivacyExtensions s
;
2355 s
= link_ipv6_privacy_extensions(link
);
2359 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2361 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2366 static int link_set_ipv6_accept_ra(Link
*link
) {
2369 /* Make this a NOP if IPv6 is not available */
2370 if (!socket_ipv6_is_supported())
2373 if (link
->flags
& IFF_LOOPBACK
)
2379 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2381 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2386 static int link_set_ipv6_dad_transmits(Link
*link
) {
2389 /* Make this a NOP if IPv6 is not available */
2390 if (!socket_ipv6_is_supported())
2393 if (link
->flags
& IFF_LOOPBACK
)
2399 if (link
->network
->ipv6_dad_transmits
< 0)
2402 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2404 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2409 static int link_set_ipv6_hop_limit(Link
*link
) {
2412 /* Make this a NOP if IPv6 is not available */
2413 if (!socket_ipv6_is_supported())
2416 if (link
->flags
& IFF_LOOPBACK
)
2422 if (link
->network
->ipv6_hop_limit
< 0)
2425 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2427 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2432 static int link_set_ipv6_mtu(Link
*link
) {
2435 /* Make this a NOP if IPv6 is not available */
2436 if (!socket_ipv6_is_supported())
2439 if (link
->flags
& IFF_LOOPBACK
)
2442 if (link
->network
->ipv6_mtu
== 0)
2445 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2447 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2452 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2453 Address
*net_address
;
2461 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2462 if (address_equal(net_address
, address
))
2468 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2477 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2478 if (route_equal(net_route
, route
))
2484 static int link_drop_foreign_config(Link
*link
) {
2490 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2491 /* we consider IPv6LL addresses to be managed by the kernel */
2492 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2495 if (link_is_static_address_configured(link
, address
)) {
2496 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2498 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2500 r
= address_remove(address
, link
, NULL
);
2506 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2507 /* do not touch routes managed by the kernel */
2508 if (route
->protocol
== RTPROT_KERNEL
)
2511 if (link_is_static_route_configured(link
, route
)) {
2512 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2516 r
= route_remove(route
, link
, NULL
);
2525 static int link_drop_config(Link
*link
) {
2526 Address
*address
, *pool_address
;
2531 SET_FOREACH(address
, link
->addresses
, i
) {
2532 /* we consider IPv6LL addresses to be managed by the kernel */
2533 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2536 r
= address_remove(address
, link
, NULL
);
2540 /* If this address came from an address pool, clean up the pool */
2541 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2542 if (address_equal(address
, pool_address
)) {
2543 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2544 address_free(pool_address
);
2550 SET_FOREACH(route
, link
->routes
, i
) {
2551 /* do not touch routes managed by the kernel */
2552 if (route
->protocol
== RTPROT_KERNEL
)
2555 r
= route_remove(route
, link
, NULL
);
2565 static int link_configure(Link
*link
) {
2569 assert(link
->network
);
2570 assert(link
->state
== LINK_STATE_INITIALIZED
);
2572 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2573 return link_configure_can(link
);
2575 /* Drop foreign config, but ignore loopback or critical devices.
2576 * We do not want to remove loopback address or addresses used for root NFS. */
2577 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2578 r
= link_drop_foreign_config(link
);
2583 r
= link_set_proxy_arp(link
);
2587 r
= ipv6_proxy_ndp_addresses_configure(link
);
2591 r
= link_set_ipv4_forward(link
);
2595 r
= link_set_ipv6_forward(link
);
2599 r
= link_set_ipv6_privacy_extensions(link
);
2603 r
= link_set_ipv6_accept_ra(link
);
2607 r
= link_set_ipv6_dad_transmits(link
);
2611 r
= link_set_ipv6_hop_limit(link
);
2615 r
= link_set_flags(link
);
2619 r
= link_set_ipv6_mtu(link
);
2623 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2624 r
= ipv4ll_configure(link
);
2629 if (link_dhcp4_enabled(link
)) {
2630 r
= dhcp4_set_promote_secondaries(link
);
2634 r
= dhcp4_configure(link
);
2639 if (link_dhcp4_server_enabled(link
)) {
2640 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2644 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2649 if (link_dhcp6_enabled(link
) ||
2650 link_ipv6_accept_ra_enabled(link
)) {
2651 r
= dhcp6_configure(link
);
2656 if (link_ipv6_accept_ra_enabled(link
)) {
2657 r
= ndisc_configure(link
);
2662 if (link_radv_enabled(link
)) {
2663 r
= radv_configure(link
);
2668 if (link_lldp_rx_enabled(link
)) {
2669 r
= link_lldp_rx_configure(link
);
2674 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
2678 if (socket_ipv6_is_supported()) {
2679 r
= link_configure_addrgen_mode(link
);
2684 return link_configure_after_setting_mtu(link
);
2687 static int link_configure_after_setting_mtu(Link
*link
) {
2691 assert(link
->network
);
2692 assert(link
->state
== LINK_STATE_INITIALIZED
);
2694 if (link
->setting_mtu
)
2697 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2698 r
= link_acquire_conf(link
);
2703 return link_enter_join_netdev(link
);
2706 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2709 if (duid
->raw_data_len
> 0)
2712 if (duid
->type
!= DUID_TYPE_UUID
)
2715 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2716 duid
->raw_data_len
= sizeof(sd_id128_t
);
2721 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2722 Manager
*manager
= userdata
;
2723 const sd_bus_error
*e
;
2733 e
= sd_bus_message_get_error(m
);
2735 log_error_errno(sd_bus_error_get_errno(e
),
2736 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2741 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2745 if (sz
!= sizeof(sd_id128_t
)) {
2746 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2750 memcpy(&manager
->product_uuid
, a
, sz
);
2751 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2752 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2754 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2757 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2758 r
= link_configure(link
);
2760 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2763 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2765 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2766 * even if the method fails. */
2767 manager
->has_product_uuid
= true;
2772 static bool link_requires_uuid(Link
*link
) {
2776 assert(link
->manager
);
2777 assert(link
->network
);
2779 duid
= link_get_duid(link
);
2780 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2783 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2786 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2792 static int link_configure_duid(Link
*link
) {
2798 assert(link
->manager
);
2799 assert(link
->network
);
2802 duid
= link_get_duid(link
);
2804 if (!link_requires_uuid(link
))
2807 if (m
->has_product_uuid
) {
2808 (void) duid_set_uuid(duid
, m
->product_uuid
);
2812 if (!m
->links_requesting_uuid
) {
2813 r
= manager_request_product_uuid(m
, link
);
2818 log_link_warning_errno(link
, r
,
2819 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2823 r
= set_put(m
->links_requesting_uuid
, link
);
2827 r
= set_put(m
->duids_requesting_uuid
, duid
);
2835 static int link_initialized_and_synced(Link
*link
) {
2840 assert(link
->ifname
);
2841 assert(link
->manager
);
2843 /* We may get called either from the asynchronous netlink callback,
2844 * or directly for link_add() if running in a container. See link_add(). */
2845 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2848 log_link_debug(link
, "Link state is up-to-date");
2849 link_set_state(link
, LINK_STATE_INITIALIZED
);
2851 r
= link_new_bound_by_list(link
);
2855 r
= link_handle_bound_by_list(link
);
2859 if (!link
->network
) {
2860 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2861 &link
->mac
, &network
);
2863 link_enter_unmanaged(link
);
2865 } else if (r
== 0 && network
->unmanaged
) {
2866 link_enter_unmanaged(link
);
2871 if (link
->flags
& IFF_LOOPBACK
) {
2872 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2873 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2875 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2876 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2878 if (network
->dhcp_server
)
2879 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2882 r
= network_apply(network
, link
);
2887 r
= link_new_bound_to_list(link
);
2891 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2892 * link_configure() is called later asynchronously. */
2893 r
= link_configure_duid(link
);
2897 r
= link_configure(link
);
2904 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2905 (void) link_initialized_and_synced(link
);
2909 int link_initialized(Link
*link
, sd_device
*device
) {
2910 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2914 assert(link
->manager
);
2915 assert(link
->manager
->rtnl
);
2918 if (link
->state
!= LINK_STATE_PENDING
)
2921 if (link
->sd_device
)
2924 log_link_debug(link
, "udev initialized link");
2925 link_set_state(link
, LINK_STATE_INITIALIZED
);
2927 link
->sd_device
= sd_device_ref(device
);
2929 /* udev has initialized the link, but we don't know if we have yet
2930 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2931 * when it returns we know that the pending NEWLINKs have already been
2932 * processed and that we are up-to-date */
2934 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2939 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2940 link_netlink_destroy_callback
, link
);
2949 static int link_load(Link
*link
) {
2950 _cleanup_free_
char *network_file
= NULL
,
2953 *dhcp4_address
= NULL
,
2954 *ipv4ll_address
= NULL
;
2955 union in_addr_union address
;
2956 union in_addr_union route_dst
;
2962 r
= parse_env_file(NULL
, link
->state_file
,
2963 "NETWORK_FILE", &network_file
,
2964 "ADDRESSES", &addresses
,
2966 "DHCP4_ADDRESS", &dhcp4_address
,
2967 "IPV4LL_ADDRESS", &ipv4ll_address
);
2968 if (r
< 0 && r
!= -ENOENT
)
2969 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2976 suffix
= strrchr(network_file
, '.');
2978 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2979 goto network_file_fail
;
2983 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2985 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2986 goto network_file_fail
;
2989 r
= network_apply(network
, link
);
2991 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3000 _cleanup_free_
char *address_str
= NULL
;
3001 char *prefixlen_str
;
3003 unsigned char prefixlen
;
3005 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3007 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3013 prefixlen_str
= strchr(address_str
, '/');
3014 if (!prefixlen_str
) {
3015 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3019 *prefixlen_str
++ = '\0';
3021 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3023 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3027 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3029 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3033 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3035 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3044 _cleanup_free_
char *route_str
= NULL
;
3045 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3047 char *prefixlen_str
;
3049 unsigned char prefixlen
, tos
, table
;
3052 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3054 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3060 prefixlen_str
= strchr(route_str
, '/');
3061 if (!prefixlen_str
) {
3062 log_link_debug(link
, "Failed to parse route %s", route_str
);
3066 *prefixlen_str
++ = '\0';
3068 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3070 log_link_debug(link
,
3071 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3076 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3078 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3082 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3084 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3086 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3087 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3088 0, route_expire_handler
, route
);
3090 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3093 route
->lifetime
= lifetime
;
3094 sd_event_source_unref(route
->expire
);
3095 route
->expire
= TAKE_PTR(expire
);
3099 if (dhcp4_address
) {
3100 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3102 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3103 goto dhcp4_address_fail
;
3106 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3108 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3110 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3112 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3117 if (ipv4ll_address
) {
3118 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3120 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3121 goto ipv4ll_address_fail
;
3124 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3126 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3128 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3130 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3133 ipv4ll_address_fail
:
3138 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3139 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3140 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3149 r
= link_new(m
, message
, ret
);
3155 log_link_debug(link
, "Link %d added", link
->ifindex
);
3157 r
= link_load(link
);
3161 if (detect_container() <= 0) {
3162 /* not in a container, udev will be around */
3163 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3164 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3166 log_link_warning_errno(link
, r
, "Could not find device: %m");
3170 r
= sd_device_get_is_initialized(device
);
3172 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3177 log_link_debug(link
, "link pending udev initialization...");
3181 r
= device_is_renaming(device
);
3183 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3187 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3191 r
= link_initialized(link
, device
);
3195 r
= link_initialized_and_synced(link
);
3202 link_enter_failed(link
);
3206 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3211 log_link_info(link
, "Gained IPv6LL");
3213 link
->ipv6ll_address
= *address
;
3214 link_check_ready(link
);
3216 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3217 r
= link_acquire_ipv6_conf(link
);
3219 link_enter_failed(link
);
3227 static int link_carrier_gained(Link
*link
) {
3232 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3233 r
= link_acquire_conf(link
);
3235 link_enter_failed(link
);
3239 r
= link_request_set_addresses(link
);
3244 r
= link_handle_bound_by_list(link
);
3251 static int link_carrier_lost(Link
*link
) {
3256 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3259 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3260 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3261 * configuration and stop the clients as well. */
3262 if (link
->setting_mtu
)
3265 r
= link_stop_clients(link
);
3267 link_enter_failed(link
);
3271 if (link_dhcp4_server_enabled(link
))
3272 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3274 r
= link_drop_config(link
);
3278 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3279 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3280 r
= link_drop_foreign_config(link
);
3285 r
= link_handle_bound_by_list(link
);
3292 int link_carrier_reset(Link
*link
) {
3297 if (link_has_carrier(link
)) {
3298 r
= link_carrier_lost(link
);
3302 r
= link_carrier_gained(link
);
3306 log_link_info(link
, "Reset carrier");
3312 int link_update(Link
*link
, sd_netlink_message
*m
) {
3313 struct ether_addr mac
;
3316 bool had_carrier
, carrier_gained
, carrier_lost
;
3320 assert(link
->ifname
);
3323 if (link
->state
== LINK_STATE_LINGER
) {
3324 log_link_info(link
, "Link re-added");
3325 link_set_state(link
, LINK_STATE_CONFIGURING
);
3327 r
= link_new_carrier_maps(link
);
3332 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3333 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3334 Manager
*manager
= link
->manager
;
3336 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3339 r
= link_add(manager
, m
, &link
);
3344 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3345 if (r
>= 0 && mtu
> 0) {
3347 if (link
->original_mtu
== 0) {
3348 link
->original_mtu
= mtu
;
3349 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3352 if (link
->dhcp_client
) {
3353 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3356 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3360 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3362 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3366 /* The kernel may broadcast NEWLINK messages without the MAC address
3367 set, simply ignore them. */
3368 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3370 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3373 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3376 log_link_debug(link
, "MAC address: "
3377 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3378 mac
.ether_addr_octet
[0],
3379 mac
.ether_addr_octet
[1],
3380 mac
.ether_addr_octet
[2],
3381 mac
.ether_addr_octet
[3],
3382 mac
.ether_addr_octet
[4],
3383 mac
.ether_addr_octet
[5]);
3386 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3388 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3391 if (link
->dhcp_client
) {
3392 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3393 (const uint8_t *) &link
->mac
,
3397 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3399 r
= dhcp4_set_client_identifier(link
);
3404 if (link
->dhcp6_client
) {
3405 const DUID
* duid
= link_get_duid(link
);
3407 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3408 (const uint8_t *) &link
->mac
,
3412 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3414 if (link
->network
->iaid_set
) {
3415 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3416 link
->network
->iaid
);
3418 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3421 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3423 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3424 duid
->raw_data_len
);
3426 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3430 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3432 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3436 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3438 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3443 had_carrier
= link_has_carrier(link
);
3445 r
= link_update_flags(link
, m
);
3449 r
= link_update_lldp(link
);
3453 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3454 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3456 if (carrier_gained
) {
3457 log_link_info(link
, "Gained carrier");
3459 r
= link_carrier_gained(link
);
3462 } else if (carrier_lost
) {
3463 log_link_info(link
, "Lost carrier");
3465 r
= link_carrier_lost(link
);
3473 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3481 if (hashmap_isempty(h
))
3485 HASHMAP_FOREACH(link
, h
, i
) {
3489 fprintf(f
, "%i", link
->ifindex
);
3496 int link_save(Link
*link
) {
3497 _cleanup_free_
char *temp_path
= NULL
;
3498 _cleanup_fclose_
FILE *f
= NULL
;
3499 const char *admin_state
, *oper_state
;
3506 assert(link
->state_file
);
3507 assert(link
->lease_file
);
3508 assert(link
->manager
);
3510 if (link
->state
== LINK_STATE_LINGER
) {
3511 (void) unlink(link
->state_file
);
3515 link_lldp_save(link
);
3517 admin_state
= link_state_to_string(link
->state
);
3518 assert(admin_state
);
3520 oper_state
= link_operstate_to_string(link
->operstate
);
3523 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3527 (void) fchmod(fileno(f
), 0644);
3530 "# This is private data. Do not parse.\n"
3533 admin_state
, oper_state
);
3535 if (link
->network
) {
3537 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3538 const char *dhcp_domainname
= NULL
;
3539 char **dhcp6_domains
= NULL
;
3540 char **dhcp_domains
= NULL
;
3543 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3544 yes_no(link
->network
->required_for_online
));
3546 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3547 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3549 if (link
->dhcp6_client
) {
3550 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3551 if (r
< 0 && r
!= -ENOMSG
)
3552 log_link_debug(link
, "No DHCPv6 lease");
3555 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3560 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3561 _cleanup_free_
char *b
= NULL
;
3563 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3564 &link
->network
->dns
[j
].address
, &b
);
3566 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3576 if (link
->network
->dhcp_use_dns
&&
3578 const struct in_addr
*addresses
;
3580 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3582 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3586 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3587 struct in6_addr
*in6_addrs
;
3589 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3593 serialize_in6_addrs(f
, in6_addrs
, r
);
3598 /* Make sure to flush out old entries before we use the NDISC data */
3601 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3604 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3608 serialize_in6_addrs(f
, &dd
->address
, 1);
3617 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3619 if (link
->network
->dhcp_use_ntp
&&
3621 const struct in_addr
*addresses
;
3623 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3625 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3629 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3630 struct in6_addr
*in6_addrs
;
3633 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3638 serialize_in6_addrs(f
, in6_addrs
, r
);
3642 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3644 fputstrv(f
, hosts
, NULL
, &space
);
3649 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3650 if (link
->dhcp_lease
) {
3651 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3652 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3655 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3658 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
3660 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3663 if (dhcp_domainname
)
3664 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3666 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3668 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3670 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3671 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3676 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
3678 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3681 if (dhcp_domainname
)
3682 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3684 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3686 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3688 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3689 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3694 fprintf(f
, "LLMNR=%s\n",
3695 resolve_support_to_string(link
->network
->llmnr
));
3696 fprintf(f
, "MDNS=%s\n",
3697 resolve_support_to_string(link
->network
->mdns
));
3698 if (link
->network
->dns_default_route
>= 0)
3699 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3701 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3702 fprintf(f
, "DNS_OVER_TLS=%s\n",
3703 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3705 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3706 fprintf(f
, "DNSSEC=%s\n",
3707 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3709 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3712 fputs("DNSSEC_NTA=", f
);
3714 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3715 fputs_with_space(f
, n
, NULL
, &space
);
3719 fputs("ADDRESSES=", f
);
3721 SET_FOREACH(a
, link
->addresses
, i
) {
3722 _cleanup_free_
char *address_str
= NULL
;
3724 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3728 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3733 fputs("ROUTES=", f
);
3735 SET_FOREACH(route
, link
->routes
, i
) {
3736 _cleanup_free_
char *route_str
= NULL
;
3738 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3742 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3743 space
? " " : "", route_str
,
3744 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3751 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3752 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3754 if (link
->dhcp_lease
) {
3755 struct in_addr address
;
3756 const char *tz
= NULL
;
3758 assert(link
->network
);
3760 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3762 fprintf(f
, "TIMEZONE=%s\n", tz
);
3764 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3766 fputs("DHCP4_ADDRESS=", f
);
3767 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3771 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3779 (void) unlink(link
->lease_file
);
3782 struct in_addr address
;
3784 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3786 fputs("IPV4LL_ADDRESS=", f
);
3787 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3792 r
= fflush_and_check(f
);
3796 if (rename(temp_path
, link
->state_file
) < 0) {
3804 (void) unlink(link
->state_file
);
3806 (void) unlink(temp_path
);
3808 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3811 /* The serialized state in /run is no longer up-to-date. */
3812 void link_dirty(Link
*link
) {
3817 /* mark manager dirty as link is dirty */
3818 manager_dirty(link
->manager
);
3820 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3822 /* allocation errors are ignored */
3825 r
= set_put(link
->manager
->dirty_links
, link
);
3827 /* don't take another ref if the link was already dirty */
3833 /* The serialized state in /run is up-to-date */
3834 void link_clean(Link
*link
) {
3836 assert(link
->manager
);
3838 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3841 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3842 [LINK_STATE_PENDING
] = "pending",
3843 [LINK_STATE_INITIALIZED
] = "initialized",
3844 [LINK_STATE_CONFIGURING
] = "configuring",
3845 [LINK_STATE_CONFIGURED
] = "configured",
3846 [LINK_STATE_UNMANAGED
] = "unmanaged",
3847 [LINK_STATE_FAILED
] = "failed",
3848 [LINK_STATE_LINGER
] = "linger",
3851 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);