1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
7 #include "alloc-util.h"
9 #include "dhcp-identifier.h"
10 #include "dhcp-lease-internal.h"
14 #include "missing_network.h"
15 #include "netdev/bond.h"
16 #include "netdev/bridge.h"
17 #include "netdev/ipvlan.h"
18 #include "netdev/vrf.h"
19 #include "netlink-util.h"
20 #include "network-internal.h"
21 #include "networkd-can.h"
22 #include "networkd-ipv6-proxy-ndp.h"
23 #include "networkd-lldp-tx.h"
24 #include "networkd-manager.h"
25 #include "networkd-ndisc.h"
26 #include "networkd-neighbor.h"
27 #include "networkd-radv.h"
28 #include "networkd-routing-policy-rule.h"
30 #include "socket-util.h"
31 #include "stdio-util.h"
32 #include "string-table.h"
34 #include "sysctl-util.h"
35 #include "tmpfile-util.h"
36 #include "udev-util.h"
40 uint32_t link_get_vrf_table(Link
*link
) {
41 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
44 uint32_t link_get_dhcp_route_table(Link
*link
) {
45 /* When the interface is part of an VRF use the VRFs routing table, unless
46 * another table is explicitly specified. */
47 if (link
->network
->dhcp_route_table_set
)
48 return link
->network
->dhcp_route_table
;
49 return link_get_vrf_table(link
);
52 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
53 if (link
->network
->ipv6_accept_ra_route_table_set
)
54 return link
->network
->ipv6_accept_ra_route_table
;
55 return link_get_vrf_table(link
);
58 DUID
* link_get_duid(Link
*link
) {
59 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
60 return &link
->network
->duid
;
62 return &link
->manager
->duid
;
65 static bool link_dhcp6_enabled(Link
*link
) {
68 if (!socket_ipv6_is_supported())
71 if (link
->flags
& IFF_LOOPBACK
)
77 if (link
->network
->bond
)
80 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
83 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
86 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
89 static bool link_dhcp4_enabled(Link
*link
) {
92 if (link
->flags
& IFF_LOOPBACK
)
98 if (link
->network
->bond
)
101 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
104 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
107 static bool link_dhcp4_server_enabled(Link
*link
) {
110 if (link
->flags
& IFF_LOOPBACK
)
116 if (link
->network
->bond
)
119 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
122 return link
->network
->dhcp_server
;
125 bool link_ipv4ll_enabled(Link
*link
) {
128 if (link
->flags
& IFF_LOOPBACK
)
134 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6", "can", "vcan"))
137 /* L3 or L3S mode do not support ARP. */
138 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
141 if (link
->network
->bond
)
144 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
147 bool link_ipv4ll_fallback_enabled(Link
*link
) {
150 if (link
->flags
& IFF_LOOPBACK
)
156 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "ip6gre", "ip6tnl", "sit", "vti", "vti6", "can", "vcan"))
159 /* L3 or L3S mode do not support ARP. */
160 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
163 if (link
->network
->bond
)
166 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
169 static bool link_ipv6ll_enabled(Link
*link
) {
172 if (!socket_ipv6_is_supported())
175 if (link
->flags
& IFF_LOOPBACK
)
181 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "can", "vcan"))
184 if (link
->network
->bond
)
187 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
190 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
193 static bool link_ipv6_enabled(Link
*link
) {
196 if (!socket_ipv6_is_supported())
199 if (link
->network
->bond
)
202 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
205 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
208 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
209 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
212 static bool link_radv_enabled(Link
*link
) {
215 if (!link_ipv6ll_enabled(link
))
218 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
221 static bool link_ipv4_forward_enabled(Link
*link
) {
224 if (link
->flags
& IFF_LOOPBACK
)
230 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
233 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
236 static bool link_ipv6_forward_enabled(Link
*link
) {
239 if (!socket_ipv6_is_supported())
242 if (link
->flags
& IFF_LOOPBACK
)
248 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
251 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
254 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
257 static bool link_proxy_arp_enabled(Link
*link
) {
260 if (link
->flags
& IFF_LOOPBACK
)
266 if (link
->network
->proxy_arp
< 0)
272 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
275 if (!socket_ipv6_is_supported())
278 if (link
->flags
& IFF_LOOPBACK
)
284 if (!link_ipv6ll_enabled(link
))
287 /* If unset use system default (enabled if local forwarding is disabled.
288 * disabled if local forwarding is enabled).
289 * If set, ignore or enforce RA independent of local forwarding state.
291 if (link
->network
->ipv6_accept_ra
< 0)
292 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
293 return !link_ipv6_forward_enabled(link
);
294 else if (link
->network
->ipv6_accept_ra
> 0)
295 /* accept RA even if ip_forward is enabled */
302 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
305 if (!socket_ipv6_is_supported())
306 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
308 if (link
->flags
& IFF_LOOPBACK
)
309 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
312 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
314 return link
->network
->ipv6_privacy_extensions
;
317 static int link_enable_ipv6(Link
*link
) {
321 if (link
->flags
& IFF_LOOPBACK
)
324 disabled
= !link_ipv6_enabled(link
);
326 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
328 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
330 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
335 static bool link_is_enslaved(Link
*link
) {
336 if (link
->flags
& IFF_SLAVE
)
337 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
340 if (!link
->enslaved_raw
)
346 if (link
->network
->bridge
)
347 /* TODO: support the case when link is not managed by networkd. */
353 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
359 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
362 link_update_operstate(master
, true);
365 void link_update_operstate(Link
*link
, bool also_update_master
) {
366 LinkOperationalState operstate
;
371 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
372 operstate
= LINK_OPERSTATE_DORMANT
;
373 else if (link_has_carrier(link
)) {
375 uint8_t scope
= RT_SCOPE_NOWHERE
;
377 /* if we have carrier, check what addresses we have */
378 SET_FOREACH(address
, link
->addresses
, i
) {
379 if (!address_is_ready(address
))
382 if (address
->scope
< scope
)
383 scope
= address
->scope
;
386 /* for operstate we also take foreign addresses into account */
387 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
388 if (!address_is_ready(address
))
391 if (address
->scope
< scope
)
392 scope
= address
->scope
;
395 if (scope
< RT_SCOPE_SITE
)
396 /* universally accessible addresses found */
397 operstate
= LINK_OPERSTATE_ROUTABLE
;
398 else if (scope
< RT_SCOPE_HOST
)
399 /* only link or site local addresses found */
400 operstate
= LINK_OPERSTATE_DEGRADED
;
402 /* no useful addresses found */
403 operstate
= LINK_OPERSTATE_CARRIER
;
404 } else if (link
->flags
& IFF_UP
)
405 operstate
= LINK_OPERSTATE_NO_CARRIER
;
407 operstate
= LINK_OPERSTATE_OFF
;
409 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
410 link_is_enslaved(link
))
411 operstate
= LINK_OPERSTATE_ENSLAVED
;
413 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
416 SET_FOREACH(slave
, link
->slaves
, i
) {
417 link_update_operstate(slave
, false);
419 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
420 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
424 if (link
->operstate
!= operstate
) {
425 link
->operstate
= operstate
;
426 link_send_changed(link
, "OperationalState", NULL
);
430 if (also_update_master
&& link
->network
) {
431 link_update_master_operstate(link
, link
->network
->bond
);
432 link_update_master_operstate(link
, link
->network
->bridge
);
436 #define FLAG_STRING(string, flag, old, new) \
437 (((old ^ new) & flag) \
438 ? ((old & flag) ? (" -" string) : (" +" string)) \
441 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
442 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
448 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
450 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
452 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
454 /* if we got a message without operstate, take it to mean
455 the state was unchanged */
456 operstate
= link
->kernel_operstate
;
458 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
461 if (link
->flags
!= flags
) {
462 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",
463 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
464 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
465 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
466 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
467 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
468 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
469 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
470 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
471 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
472 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
473 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
474 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
475 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
476 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
477 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
478 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
479 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
480 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
481 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
483 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
484 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
485 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
486 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
487 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
488 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
489 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
490 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
492 /* link flags are currently at most 18 bits, let's align to
494 if (unknown_flags_added
)
496 "Unknown link flags gained: %#.5x (ignoring)",
497 unknown_flags_added
);
499 if (unknown_flags_removed
)
501 "Unknown link flags lost: %#.5x (ignoring)",
502 unknown_flags_removed
);
506 link
->kernel_operstate
= operstate
;
508 link_update_operstate(link
, true);
513 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
514 _cleanup_(link_unrefp
) Link
*link
= NULL
;
516 const char *ifname
, *kind
= NULL
;
518 unsigned short iftype
;
524 /* check for link kind */
525 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
527 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
528 r
= sd_netlink_message_exit_container(message
);
533 r
= sd_netlink_message_get_type(message
, &type
);
536 else if (type
!= RTM_NEWLINK
)
539 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
542 else if (ifindex
<= 0)
545 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
549 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
560 .state
= LINK_STATE_PENDING
,
561 .rtnl_extended_attrs
= true,
566 link
->ifname
= strdup(ifname
);
571 link
->kind
= strdup(kind
);
576 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
578 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
580 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
582 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
584 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
587 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
590 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
593 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
597 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
601 r
= link_update_flags(link
, message
);
605 *ret
= TAKE_PTR(link
);
610 static Link
*link_free(Link
*link
) {
615 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
616 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
618 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
619 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
621 while ((address
= link
->pool_addresses
)) {
622 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
623 address_free(address
);
626 sd_dhcp_server_unref(link
->dhcp_server
);
627 sd_dhcp_client_unref(link
->dhcp_client
);
628 sd_dhcp_lease_unref(link
->dhcp_lease
);
630 link_lldp_emit_stop(link
);
632 free(link
->lease_file
);
634 sd_lldp_unref(link
->lldp
);
635 free(link
->lldp_file
);
639 sd_ipv4ll_unref(link
->ipv4ll
);
640 sd_dhcp6_client_unref(link
->dhcp6_client
);
641 sd_ndisc_unref(link
->ndisc
);
642 sd_radv_unref(link
->radv
);
647 (void) unlink(link
->state_file
);
648 free(link
->state_file
);
650 sd_device_unref(link
->sd_device
);
652 hashmap_free(link
->bound_to_links
);
653 hashmap_free(link
->bound_by_links
);
655 set_free_with_destructor(link
->slaves
, link_unref
);
657 network_unref(link
->network
);
662 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
664 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
671 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
680 void link_set_state(Link
*link
, LinkState state
) {
683 if (link
->state
== state
)
686 log_link_debug(link
, "State changed: %s -> %s",
687 link_state_to_string(link
->state
),
688 link_state_to_string(state
));
692 link_send_changed(link
, "AdministrativeState", NULL
);
695 static void link_enter_unmanaged(Link
*link
) {
698 link_set_state(link
, LINK_STATE_UNMANAGED
);
703 int link_stop_clients(Link
*link
) {
707 assert(link
->manager
);
708 assert(link
->manager
->event
);
710 if (link
->dhcp_client
) {
711 k
= sd_dhcp_client_stop(link
->dhcp_client
);
713 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
717 k
= sd_ipv4ll_stop(link
->ipv4ll
);
719 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
722 if (link
->dhcp6_client
) {
723 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
725 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
729 k
= sd_ndisc_stop(link
->ndisc
);
731 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
735 k
= sd_radv_stop(link
->radv
);
737 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
740 link_lldp_emit_stop(link
);
744 void link_enter_failed(Link
*link
) {
747 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
750 log_link_warning(link
, "Failed");
752 link_set_state(link
, LINK_STATE_FAILED
);
754 link_stop_clients(link
);
759 static Address
* link_find_dhcp_server_address(Link
*link
) {
763 assert(link
->network
);
765 /* The first statically configured address if there is any */
766 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
768 if (address
->family
!= AF_INET
)
771 if (in_addr_is_null(address
->family
, &address
->in_addr
))
777 /* If that didn't work, find a suitable address we got from the pool */
778 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
779 if (address
->family
!= AF_INET
)
788 static int link_join_netdevs_after_configured(Link
*link
) {
793 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
794 if (netdev
->ifindex
> 0)
795 /* Assume already enslaved. */
798 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
801 log_struct(LOG_DEBUG
,
802 LOG_LINK_INTERFACE(link
),
803 LOG_NETDEV_INTERFACE(netdev
),
804 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
806 r
= netdev_join(netdev
, link
, NULL
);
808 return log_struct_errno(LOG_WARNING
, r
,
809 LOG_LINK_INTERFACE(link
),
810 LOG_NETDEV_INTERFACE(netdev
),
811 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
817 static void link_enter_configured(Link
*link
) {
819 assert(link
->network
);
821 if (link
->state
!= LINK_STATE_CONFIGURING
)
824 log_link_info(link
, "Configured");
826 link_set_state(link
, LINK_STATE_CONFIGURED
);
828 (void) link_join_netdevs_after_configured(link
);
833 static int link_request_set_routing_policy_rule(Link
*link
) {
834 RoutingPolicyRule
*rule
, *rrule
= NULL
;
838 assert(link
->network
);
840 link_set_state(link
, LINK_STATE_CONFIGURING
);
841 link
->routing_policy_rules_configured
= false;
843 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
844 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
845 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
846 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
848 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
852 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
854 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
855 link_enter_failed(link
);
859 link
->routing_policy_rule_messages
++;
862 routing_policy_rule_purge(link
->manager
, link
);
863 if (link
->routing_policy_rule_messages
== 0) {
864 link
->routing_policy_rules_configured
= true;
865 link_check_ready(link
);
867 log_link_debug(link
, "Setting routing policy rules");
872 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
876 assert(link
->route_messages
> 0);
877 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
878 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
880 link
->route_messages
--;
882 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
885 r
= sd_netlink_message_get_errno(m
);
886 if (r
< 0 && r
!= -EEXIST
)
887 log_link_warning_errno(link
, r
, "Could not set route: %m");
889 if (link
->route_messages
== 0) {
890 log_link_debug(link
, "Routes set");
891 link
->static_routes_configured
= true;
892 link_check_ready(link
);
898 int link_request_set_routes(Link
*link
) {
900 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
901 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
908 assert(link
->network
);
909 assert(link
->addresses_configured
);
910 assert(link
->address_messages
== 0);
911 assert(link
->state
!= _LINK_STATE_INVALID
);
913 link_set_state(link
, LINK_STATE_CONFIGURING
);
914 link
->static_routes_configured
= false;
916 r
= link_request_set_routing_policy_rule(link
);
920 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
921 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
922 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
924 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
927 r
= route_configure(rt
, link
, route_handler
);
929 log_link_warning_errno(link
, r
, "Could not set routes: %m");
930 link_enter_failed(link
);
934 link
->route_messages
++;
937 if (link
->route_messages
== 0) {
938 link
->static_routes_configured
= true;
939 link_check_ready(link
);
941 log_link_debug(link
, "Setting routes");
946 void link_check_ready(Link
*link
) {
952 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
958 if (!link
->addresses_configured
)
961 if (!link
->neighbors_configured
)
964 SET_FOREACH(a
, link
->addresses
, i
)
965 if (!address_is_ready(a
))
968 if (!link
->addresses_ready
) {
969 link
->addresses_ready
= true;
970 link_request_set_routes(link
);
974 if (!link
->static_routes_configured
)
977 if (!link
->routing_policy_rules_configured
)
980 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
983 if (link_ipv6ll_enabled(link
) &&
984 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
987 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
988 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
989 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
990 /* When DHCP is enabled, at least one protocol must provide an address, or
991 * an IPv4ll fallback address must be configured. */
994 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
997 if (link
->state
!= LINK_STATE_CONFIGURED
)
998 link_enter_configured(link
);
1003 static int link_request_set_neighbors(Link
*link
) {
1008 assert(link
->network
);
1009 assert(link
->state
!= _LINK_STATE_INVALID
);
1011 link_set_state(link
, LINK_STATE_CONFIGURING
);
1012 link
->neighbors_configured
= false;
1014 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1015 r
= neighbor_configure(neighbor
, link
, NULL
);
1017 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1018 link_enter_failed(link
);
1023 if (link
->neighbor_messages
== 0) {
1024 link
->neighbors_configured
= true;
1025 link_check_ready(link
);
1027 log_link_debug(link
, "Setting neighbors");
1032 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1038 assert(link
->ifname
);
1039 assert(link
->address_messages
> 0);
1040 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1041 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1043 link
->address_messages
--;
1045 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1048 r
= sd_netlink_message_get_errno(m
);
1049 if (r
< 0 && r
!= -EEXIST
)
1050 log_link_warning_errno(link
, r
, "could not set address: %m");
1052 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1054 if (link
->address_messages
== 0) {
1055 log_link_debug(link
, "Addresses set");
1056 link
->addresses_configured
= true;
1057 link_check_ready(link
);
1063 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1064 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1065 size_t n_addresses
= 0, n_allocated
= 0;
1068 log_debug("Copying DNS server information from %s", link
->ifname
);
1073 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1076 /* Only look for IPv4 addresses */
1077 if (link
->network
->dns
[i
].family
!= AF_INET
)
1080 ia
= link
->network
->dns
[i
].address
.in
;
1082 /* Never propagate obviously borked data */
1083 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1086 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1089 addresses
[n_addresses
++] = ia
;
1092 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1093 const struct in_addr
*da
= NULL
;
1096 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1099 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1102 for (j
= 0; j
< n
; j
++)
1103 if (in4_addr_is_non_local(&da
[j
]))
1104 addresses
[n_addresses
++] = da
[j
];
1108 if (n_addresses
<= 0)
1111 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1114 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1115 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1116 size_t n_addresses
= 0, n_allocated
= 0;
1122 log_debug("Copying NTP server information from %s", link
->ifname
);
1124 STRV_FOREACH(a
, link
->network
->ntp
) {
1125 union in_addr_union ia
;
1127 /* Only look for IPv4 addresses */
1128 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1131 /* Never propagate obviously borked data */
1132 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1135 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1138 addresses
[n_addresses
++] = ia
.in
;
1141 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1142 const struct in_addr
*da
= NULL
;
1145 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1148 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1151 for (j
= 0; j
< n
; j
++)
1152 if (in4_addr_is_non_local(&da
[j
]))
1153 addresses
[n_addresses
++] = da
[j
];
1157 if (n_addresses
<= 0)
1160 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1163 static int link_set_bridge_fdb(Link
*link
) {
1164 FdbEntry
*fdb_entry
;
1167 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1168 r
= fdb_entry_configure(link
, fdb_entry
);
1170 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1176 static int link_request_set_addresses(Link
*link
) {
1177 AddressLabel
*label
;
1182 assert(link
->network
);
1183 assert(link
->state
!= _LINK_STATE_INVALID
);
1185 link_set_state(link
, LINK_STATE_CONFIGURING
);
1187 /* Reset all *_configured flags we are configuring. */
1188 link
->addresses_configured
= false;
1189 link
->addresses_ready
= false;
1190 link
->neighbors_configured
= false;
1191 link
->static_routes_configured
= false;
1192 link
->routing_policy_rules_configured
= false;
1194 r
= link_set_bridge_fdb(link
);
1198 r
= link_request_set_neighbors(link
);
1202 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1205 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1207 r
= address_configure(ad
, link
, address_handler
, update
);
1209 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1210 link_enter_failed(link
);
1214 link
->address_messages
++;
1217 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1218 r
= address_label_configure(label
, link
, NULL
, false);
1220 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1221 link_enter_failed(link
);
1225 link
->address_label_messages
++;
1228 /* now that we can figure out a default address for the dhcp server,
1230 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1232 Link
*uplink
= NULL
;
1233 bool acquired_uplink
= false;
1235 address
= link_find_dhcp_server_address(link
);
1237 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1238 link_enter_failed(link
);
1242 /* use the server address' subnet as the pool */
1243 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1244 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1249 r = sd_dhcp_server_set_router(link->dhcp_server,
1250 &main_address->in_addr.in);
1255 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1256 r
= sd_dhcp_server_set_max_lease_time(
1258 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1263 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1264 r
= sd_dhcp_server_set_default_lease_time(
1266 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1271 if (link
->network
->dhcp_server_emit_dns
) {
1273 if (link
->network
->n_dhcp_server_dns
> 0)
1274 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1276 uplink
= manager_find_uplink(link
->manager
, link
);
1277 acquired_uplink
= true;
1280 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1283 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1286 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1289 if (link
->network
->dhcp_server_emit_ntp
) {
1291 if (link
->network
->n_dhcp_server_ntp
> 0)
1292 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1294 if (!acquired_uplink
)
1295 uplink
= manager_find_uplink(link
->manager
, link
);
1298 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1301 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1305 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1308 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1310 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1312 if (link
->network
->dhcp_server_emit_timezone
) {
1313 _cleanup_free_
char *buffer
= NULL
;
1314 const char *tz
= NULL
;
1316 if (link
->network
->dhcp_server_timezone
)
1317 tz
= link
->network
->dhcp_server_timezone
;
1319 r
= get_timezone(&buffer
);
1321 log_warning_errno(r
, "Failed to determine timezone: %m");
1327 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1332 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1333 r
= sd_dhcp_server_start(link
->dhcp_server
);
1335 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1337 link_enter_failed(link
);
1343 log_link_debug(link
, "Offering DHCPv4 leases");
1346 if (link
->address_messages
== 0) {
1347 link
->addresses_configured
= true;
1348 link_check_ready(link
);
1350 log_link_debug(link
, "Setting addresses");
1355 static int link_set_bridge_vlan(Link
*link
) {
1358 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1360 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1365 static int link_set_proxy_arp(Link
*link
) {
1368 if (!link_proxy_arp_enabled(link
))
1371 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1373 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1378 static int link_configure_after_setting_mtu(Link
*link
);
1380 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1385 assert(link
->ifname
);
1387 link
->setting_mtu
= false;
1389 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1392 r
= sd_netlink_message_get_errno(m
);
1394 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1398 log_link_debug(link
, "Setting MTU done.");
1400 if (link
->state
== LINK_STATE_INITIALIZED
)
1401 (void) link_configure_after_setting_mtu(link
);
1406 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1407 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1411 assert(link
->manager
);
1412 assert(link
->manager
->rtnl
);
1414 if (mtu
== 0 || link
->setting_mtu
)
1417 if (link
->mtu
== mtu
)
1420 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1422 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1424 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1426 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1427 * for this interface, then disable IPv6 else enable it. */
1428 (void) link_enable_ipv6(link
);
1430 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1431 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1432 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1434 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1435 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1440 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1442 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1444 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1445 link_netlink_destroy_callback
, link
);
1447 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1450 link
->setting_mtu
= true;
1455 static bool link_reduces_vlan_mtu(Link
*link
) {
1456 /* See netif_reduces_vlan_mtu() in kernel. */
1457 return streq_ptr(link
->kind
, "macsec");
1460 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1465 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1466 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1467 /* See vlan_dev_change_mtu() in kernel. */
1468 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1470 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1471 /* See macvlan_change_mtu() in kernel. */
1477 static int link_configure_mtu(Link
*link
) {
1481 assert(link
->network
);
1483 if (link
->network
->mtu
> 0)
1484 return link_set_mtu(link
, link
->network
->mtu
);
1486 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1487 if (link
->mtu
>= mtu
)
1490 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1491 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1494 return link_set_mtu(link
, mtu
);
1497 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1502 assert(link
->ifname
);
1504 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1507 r
= sd_netlink_message_get_errno(m
);
1509 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1514 static int link_set_flags(Link
*link
) {
1515 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1516 unsigned ifi_change
= 0;
1517 unsigned ifi_flags
= 0;
1521 assert(link
->manager
);
1522 assert(link
->manager
->rtnl
);
1524 if (link
->flags
& IFF_LOOPBACK
)
1530 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1533 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1535 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1537 if (link
->network
->arp
>= 0) {
1538 ifi_change
|= IFF_NOARP
;
1539 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1542 if (link
->network
->multicast
>= 0) {
1543 ifi_change
|= IFF_MULTICAST
;
1544 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1547 if (link
->network
->allmulticast
>= 0) {
1548 ifi_change
|= IFF_ALLMULTI
;
1549 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1552 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1554 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1556 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1557 link_netlink_destroy_callback
, link
);
1559 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1566 static int link_acquire_ipv6_conf(Link
*link
) {
1571 if (link_ipv6_accept_ra_enabled(link
)) {
1572 assert(link
->ndisc
);
1574 log_link_debug(link
, "Discovering IPv6 routers");
1576 r
= sd_ndisc_start(link
->ndisc
);
1577 if (r
< 0 && r
!= -EBUSY
)
1578 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1581 if (link_radv_enabled(link
)) {
1583 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1585 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1587 r
= sd_radv_start(link
->radv
);
1588 if (r
< 0 && r
!= -EBUSY
)
1589 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1592 (void) dhcp6_request_prefix_delegation(link
);
1597 static int link_acquire_ipv4_conf(Link
*link
) {
1601 assert(link
->manager
);
1602 assert(link
->manager
->event
);
1604 if (link_ipv4ll_enabled(link
)) {
1605 assert(link
->ipv4ll
);
1607 log_link_debug(link
, "Acquiring IPv4 link-local address");
1609 r
= sd_ipv4ll_start(link
->ipv4ll
);
1611 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1614 if (link_dhcp4_enabled(link
)) {
1615 assert(link
->dhcp_client
);
1617 log_link_debug(link
, "Acquiring DHCPv4 lease");
1619 r
= sd_dhcp_client_start(link
->dhcp_client
);
1621 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1627 static int link_acquire_conf(Link
*link
) {
1632 r
= link_acquire_ipv4_conf(link
);
1636 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1637 r
= link_acquire_ipv6_conf(link
);
1642 if (link_lldp_emit_enabled(link
)) {
1643 r
= link_lldp_emit_start(link
);
1645 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1651 bool link_has_carrier(Link
*link
) {
1652 /* see Documentation/networking/operstates.txt in the kernel sources */
1654 if (link
->kernel_operstate
== IF_OPER_UP
)
1657 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1658 /* operstate may not be implemented, so fall back to flags */
1659 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1660 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1666 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1671 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1674 r
= sd_netlink_message_get_errno(m
);
1676 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1681 static int link_configure_addrgen_mode(Link
*link
) {
1682 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1683 uint8_t ipv6ll_mode
;
1687 assert(link
->network
);
1688 assert(link
->manager
);
1689 assert(link
->manager
->rtnl
);
1691 if (!socket_ipv6_is_supported())
1694 log_link_debug(link
, "Setting address genmode for link");
1696 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1698 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1700 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1702 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1704 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1706 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1708 if (!link_ipv6ll_enabled(link
))
1709 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1710 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1711 /* The file may not exist. And event if it exists, when stable_secret is unset,
1712 * reading the file fails with EIO. */
1713 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1715 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1717 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1719 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1721 r
= sd_netlink_message_close_container(req
);
1723 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1725 r
= sd_netlink_message_close_container(req
);
1727 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1729 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1730 link_netlink_destroy_callback
, link
);
1732 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1739 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1744 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1747 r
= sd_netlink_message_get_errno(m
);
1749 /* we warn but don't fail the link, as it may be brought up later */
1750 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1755 static int link_up(Link
*link
) {
1756 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1760 assert(link
->network
);
1761 assert(link
->manager
);
1762 assert(link
->manager
->rtnl
);
1764 log_link_debug(link
, "Bringing link up");
1766 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1768 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1770 /* set it free if not enslaved with networkd */
1771 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1772 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1774 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1777 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1779 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1781 if (link
->network
->mac
) {
1782 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1784 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1787 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1788 link_netlink_destroy_callback
, link
);
1790 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1797 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1802 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1805 r
= sd_netlink_message_get_errno(m
);
1807 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1812 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1813 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1817 assert(link
->manager
);
1818 assert(link
->manager
->rtnl
);
1820 log_link_debug(link
, "Bringing link down");
1822 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1823 RTM_SETLINK
, link
->ifindex
);
1825 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1827 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1829 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1831 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1832 callback
?: link_down_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_handle_bound_to_list(Link
*link
) {
1846 bool required_up
= false;
1847 bool link_is_up
= false;
1851 if (hashmap_isempty(link
->bound_to_links
))
1854 if (link
->flags
& IFF_UP
)
1857 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1858 if (link_has_carrier(l
)) {
1863 if (!required_up
&& link_is_up
) {
1864 r
= link_down(link
, NULL
);
1867 } else if (required_up
&& !link_is_up
) {
1876 static int link_handle_bound_by_list(Link
*link
) {
1883 if (hashmap_isempty(link
->bound_by_links
))
1886 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1887 r
= link_handle_bound_to_list(l
);
1895 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1901 if (link
== carrier
)
1904 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1907 r
= hashmap_ensure_allocated(h
, NULL
);
1911 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1918 static int link_new_bound_by_list(Link
*link
) {
1923 bool list_updated
= false;
1926 assert(link
->manager
);
1930 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1931 if (!carrier
->network
)
1934 if (strv_isempty(carrier
->network
->bind_carrier
))
1937 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1938 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1942 list_updated
= true;
1949 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1950 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1954 link_dirty(carrier
);
1960 static int link_new_bound_to_list(Link
*link
) {
1965 bool list_updated
= false;
1968 assert(link
->manager
);
1973 if (strv_isempty(link
->network
->bind_carrier
))
1978 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1979 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1980 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1984 list_updated
= true;
1991 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1992 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1996 link_dirty(carrier
);
2002 static int link_new_carrier_maps(Link
*link
) {
2005 r
= link_new_bound_by_list(link
);
2009 r
= link_handle_bound_by_list(link
);
2013 r
= link_new_bound_to_list(link
);
2017 r
= link_handle_bound_to_list(link
);
2024 static void link_free_bound_to_list(Link
*link
) {
2028 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2029 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2031 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2032 link_dirty(bound_to
);
2038 static void link_free_bound_by_list(Link
*link
) {
2042 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2043 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2045 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2046 link_dirty(bound_by
);
2047 link_handle_bound_to_list(bound_by
);
2054 static void link_free_carrier_maps(Link
*link
) {
2055 bool list_updated
= false;
2059 if (!hashmap_isempty(link
->bound_to_links
)) {
2060 link_free_bound_to_list(link
);
2061 list_updated
= true;
2064 if (!hashmap_isempty(link
->bound_by_links
)) {
2065 link_free_bound_by_list(link
);
2066 list_updated
= true;
2075 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2082 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2086 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2090 r
= set_put(master
->slaves
, link
);
2098 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2103 if (!link
->manager
|| !netdev
)
2106 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2109 link_unref(set_remove(master
->slaves
, link
));
2112 static void link_detach_from_manager(Link
*link
) {
2113 if (!link
|| !link
->manager
)
2116 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2119 /* The following must be called at last. */
2120 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2124 void link_drop(Link
*link
) {
2125 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2128 link_set_state(link
, LINK_STATE_LINGER
);
2130 link_free_carrier_maps(link
);
2132 if (link
->network
) {
2133 link_drop_from_master(link
, link
->network
->bridge
);
2134 link_drop_from_master(link
, link
->network
->bond
);
2137 log_link_debug(link
, "Link removed");
2139 (void) unlink(link
->state_file
);
2140 link_detach_from_manager(link
);
2143 static int link_joined(Link
*link
) {
2147 assert(link
->network
);
2149 if (!hashmap_isempty(link
->bound_to_links
)) {
2150 r
= link_handle_bound_to_list(link
);
2153 } else if (!(link
->flags
& IFF_UP
)) {
2156 link_enter_failed(link
);
2161 if (link
->network
->bridge
) {
2162 r
= link_set_bridge(link
);
2164 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2166 r
= link_append_to_master(link
, link
->network
->bridge
);
2168 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2171 if (link
->network
->bond
) {
2172 r
= link_set_bond(link
);
2174 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2176 r
= link_append_to_master(link
, link
->network
->bond
);
2178 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2181 if (link
->network
->use_br_vlan
&&
2182 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2183 r
= link_set_bridge_vlan(link
);
2185 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2188 /* Skip setting up addresses until it gets carrier,
2189 or it would try to set addresses twice,
2190 which is bad for non-idempotent steps. */
2191 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2194 return link_request_set_addresses(link
);
2197 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2201 assert(link
->network
);
2202 assert(link
->enslaving
> 0);
2203 assert(!link
->enslaved_raw
);
2207 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2210 r
= sd_netlink_message_get_errno(m
);
2211 if (r
< 0 && r
!= -EEXIST
) {
2212 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2213 link_enter_failed(link
);
2216 log_link_debug(link
, "Joined netdev");
2218 if (link
->enslaving
== 0) {
2219 link
->enslaved_raw
= true;
2226 static int link_enter_join_netdev(Link
*link
) {
2232 assert(link
->network
);
2233 assert(link
->state
== LINK_STATE_INITIALIZED
);
2235 link_set_state(link
, LINK_STATE_CONFIGURING
);
2238 link
->enslaving
= 0;
2239 link
->enslaved_raw
= false;
2241 if (link
->network
->bond
) {
2242 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2243 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2244 return link_joined(link
);
2246 log_struct(LOG_DEBUG
,
2247 LOG_LINK_INTERFACE(link
),
2248 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2249 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2253 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2255 log_struct_errno(LOG_WARNING
, r
,
2256 LOG_LINK_INTERFACE(link
),
2257 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2258 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2259 link_enter_failed(link
);
2264 if (link
->network
->bridge
) {
2265 log_struct(LOG_DEBUG
,
2266 LOG_LINK_INTERFACE(link
),
2267 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2268 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2272 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2274 log_struct_errno(LOG_WARNING
, r
,
2275 LOG_LINK_INTERFACE(link
),
2276 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2277 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2278 link_enter_failed(link
);
2283 if (link
->network
->vrf
) {
2284 log_struct(LOG_DEBUG
,
2285 LOG_LINK_INTERFACE(link
),
2286 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2287 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2291 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2293 log_struct_errno(LOG_WARNING
, r
,
2294 LOG_LINK_INTERFACE(link
),
2295 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2296 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2297 link_enter_failed(link
);
2302 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2304 if (netdev
->ifindex
> 0)
2305 /* Assume already enslaved. */
2308 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2311 log_struct(LOG_DEBUG
,
2312 LOG_LINK_INTERFACE(link
),
2313 LOG_NETDEV_INTERFACE(netdev
),
2314 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2318 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2320 log_struct_errno(LOG_WARNING
, r
,
2321 LOG_LINK_INTERFACE(link
),
2322 LOG_NETDEV_INTERFACE(netdev
),
2323 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2324 link_enter_failed(link
);
2329 if (link
->enslaving
== 0)
2330 return link_joined(link
);
2335 static int link_set_ipv4_forward(Link
*link
) {
2338 if (!link_ipv4_forward_enabled(link
))
2341 /* We propagate the forwarding flag from one interface to the
2342 * global setting one way. This means: as long as at least one
2343 * interface was configured at any time that had IP forwarding
2344 * enabled the setting will stay on for good. We do this
2345 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2346 * somewhat in sync (see below). */
2348 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2350 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2355 static int link_set_ipv6_forward(Link
*link
) {
2358 if (!link_ipv6_forward_enabled(link
))
2361 /* On Linux, the IPv6 stack does not know a per-interface
2362 * packet forwarding setting: either packet forwarding is on
2363 * for all, or off for all. We hence don't bother with a
2364 * per-interface setting, but simply propagate the interface
2365 * flag, if it is set, to the global flag, one-way. Note that
2366 * while IPv4 would allow a per-interface flag, we expose the
2367 * same behaviour there and also propagate the setting from
2368 * one to all, to keep things simple (see above). */
2370 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2372 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2377 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2378 IPv6PrivacyExtensions s
;
2381 s
= link_ipv6_privacy_extensions(link
);
2385 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2387 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2392 static int link_set_ipv6_accept_ra(Link
*link
) {
2395 /* Make this a NOP if IPv6 is not available */
2396 if (!socket_ipv6_is_supported())
2399 if (link
->flags
& IFF_LOOPBACK
)
2405 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2407 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2412 static int link_set_ipv6_dad_transmits(Link
*link
) {
2415 /* Make this a NOP if IPv6 is not available */
2416 if (!socket_ipv6_is_supported())
2419 if (link
->flags
& IFF_LOOPBACK
)
2425 if (link
->network
->ipv6_dad_transmits
< 0)
2428 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2430 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2435 static int link_set_ipv6_hop_limit(Link
*link
) {
2438 /* Make this a NOP if IPv6 is not available */
2439 if (!socket_ipv6_is_supported())
2442 if (link
->flags
& IFF_LOOPBACK
)
2448 if (link
->network
->ipv6_hop_limit
< 0)
2451 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2453 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2458 static int link_set_ipv6_mtu(Link
*link
) {
2461 /* Make this a NOP if IPv6 is not available */
2462 if (!socket_ipv6_is_supported())
2465 if (link
->flags
& IFF_LOOPBACK
)
2468 if (link
->network
->ipv6_mtu
== 0)
2471 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2473 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2478 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2479 Address
*net_address
;
2487 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2488 if (address_equal(net_address
, address
))
2494 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2503 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2504 if (route_equal(net_route
, route
))
2510 static int link_drop_foreign_config(Link
*link
) {
2516 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2517 /* we consider IPv6LL addresses to be managed by the kernel */
2518 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2521 if (link_is_static_address_configured(link
, address
)) {
2522 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2524 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2526 r
= address_remove(address
, link
, NULL
);
2532 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2533 /* do not touch routes managed by the kernel */
2534 if (route
->protocol
== RTPROT_KERNEL
)
2537 if (link_is_static_route_configured(link
, route
)) {
2538 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2542 r
= route_remove(route
, link
, NULL
);
2551 static int link_drop_config(Link
*link
) {
2552 Address
*address
, *pool_address
;
2557 SET_FOREACH(address
, link
->addresses
, i
) {
2558 /* we consider IPv6LL addresses to be managed by the kernel */
2559 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2562 r
= address_remove(address
, link
, NULL
);
2566 /* If this address came from an address pool, clean up the pool */
2567 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2568 if (address_equal(address
, pool_address
)) {
2569 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2570 address_free(pool_address
);
2576 SET_FOREACH(route
, link
->routes
, i
) {
2577 /* do not touch routes managed by the kernel */
2578 if (route
->protocol
== RTPROT_KERNEL
)
2581 r
= route_remove(route
, link
, NULL
);
2591 static int link_configure(Link
*link
) {
2595 assert(link
->network
);
2596 assert(link
->state
== LINK_STATE_INITIALIZED
);
2598 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2599 return link_configure_can(link
);
2601 /* Drop foreign config, but ignore loopback or critical devices.
2602 * We do not want to remove loopback address or addresses used for root NFS. */
2603 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2604 r
= link_drop_foreign_config(link
);
2609 r
= link_set_proxy_arp(link
);
2613 r
= ipv6_proxy_ndp_addresses_configure(link
);
2617 r
= link_set_ipv4_forward(link
);
2621 r
= link_set_ipv6_forward(link
);
2625 r
= link_set_ipv6_privacy_extensions(link
);
2629 r
= link_set_ipv6_accept_ra(link
);
2633 r
= link_set_ipv6_dad_transmits(link
);
2637 r
= link_set_ipv6_hop_limit(link
);
2641 r
= link_set_flags(link
);
2645 r
= link_set_ipv6_mtu(link
);
2649 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2650 r
= ipv4ll_configure(link
);
2655 if (link_dhcp4_enabled(link
)) {
2656 r
= dhcp4_set_promote_secondaries(link
);
2660 r
= dhcp4_configure(link
);
2665 if (link_dhcp4_server_enabled(link
)) {
2666 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2670 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2675 if (link_dhcp6_enabled(link
) ||
2676 link_ipv6_accept_ra_enabled(link
)) {
2677 r
= dhcp6_configure(link
);
2682 if (link_ipv6_accept_ra_enabled(link
)) {
2683 r
= ndisc_configure(link
);
2688 if (link_radv_enabled(link
)) {
2689 r
= radv_configure(link
);
2694 if (link_lldp_rx_enabled(link
)) {
2695 r
= link_lldp_rx_configure(link
);
2700 r
= link_configure_mtu(link
);
2704 r
= link_configure_addrgen_mode(link
);
2708 return link_configure_after_setting_mtu(link
);
2711 static int link_configure_after_setting_mtu(Link
*link
) {
2715 assert(link
->network
);
2716 assert(link
->state
== LINK_STATE_INITIALIZED
);
2718 if (link
->setting_mtu
)
2721 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2722 r
= link_acquire_conf(link
);
2727 return link_enter_join_netdev(link
);
2730 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2733 if (duid
->raw_data_len
> 0)
2736 if (duid
->type
!= DUID_TYPE_UUID
)
2739 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2740 duid
->raw_data_len
= sizeof(sd_id128_t
);
2745 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2746 Manager
*manager
= userdata
;
2747 const sd_bus_error
*e
;
2757 e
= sd_bus_message_get_error(m
);
2759 log_error_errno(sd_bus_error_get_errno(e
),
2760 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2765 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2769 if (sz
!= sizeof(sd_id128_t
)) {
2770 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2774 memcpy(&manager
->product_uuid
, a
, sz
);
2775 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2776 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2778 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2781 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2782 r
= link_configure(link
);
2784 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2787 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2789 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2790 * even if the method fails. */
2791 manager
->has_product_uuid
= true;
2796 static bool link_requires_uuid(Link
*link
) {
2800 assert(link
->manager
);
2801 assert(link
->network
);
2803 duid
= link_get_duid(link
);
2804 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2807 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2810 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2816 static int link_configure_duid(Link
*link
) {
2822 assert(link
->manager
);
2823 assert(link
->network
);
2826 duid
= link_get_duid(link
);
2828 if (!link_requires_uuid(link
))
2831 if (m
->has_product_uuid
) {
2832 (void) duid_set_uuid(duid
, m
->product_uuid
);
2836 if (!m
->links_requesting_uuid
) {
2837 r
= manager_request_product_uuid(m
, link
);
2842 log_link_warning_errno(link
, r
,
2843 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2847 r
= set_put(m
->links_requesting_uuid
, link
);
2851 r
= set_put(m
->duids_requesting_uuid
, duid
);
2859 static int link_initialized_and_synced(Link
*link
) {
2864 assert(link
->ifname
);
2865 assert(link
->manager
);
2867 /* We may get called either from the asynchronous netlink callback,
2868 * or directly for link_add() if running in a container. See link_add(). */
2869 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2872 log_link_debug(link
, "Link state is up-to-date");
2873 link_set_state(link
, LINK_STATE_INITIALIZED
);
2875 r
= link_new_bound_by_list(link
);
2879 r
= link_handle_bound_by_list(link
);
2883 if (!link
->network
) {
2884 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2885 &link
->mac
, &network
);
2887 link_enter_unmanaged(link
);
2889 } else if (r
== 0 && network
->unmanaged
) {
2890 link_enter_unmanaged(link
);
2895 if (link
->flags
& IFF_LOOPBACK
) {
2896 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2897 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2899 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2900 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2902 if (network
->dhcp_server
)
2903 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2906 r
= network_apply(network
, link
);
2911 r
= link_new_bound_to_list(link
);
2915 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2916 * link_configure() is called later asynchronously. */
2917 r
= link_configure_duid(link
);
2921 r
= link_configure(link
);
2928 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2929 (void) link_initialized_and_synced(link
);
2933 int link_initialized(Link
*link
, sd_device
*device
) {
2934 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2938 assert(link
->manager
);
2939 assert(link
->manager
->rtnl
);
2942 if (link
->state
!= LINK_STATE_PENDING
)
2945 if (link
->sd_device
)
2948 log_link_debug(link
, "udev initialized link");
2949 link_set_state(link
, LINK_STATE_INITIALIZED
);
2951 link
->sd_device
= sd_device_ref(device
);
2953 /* udev has initialized the link, but we don't know if we have yet
2954 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2955 * when it returns we know that the pending NEWLINKs have already been
2956 * processed and that we are up-to-date */
2958 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2963 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2964 link_netlink_destroy_callback
, link
);
2973 static int link_load(Link
*link
) {
2974 _cleanup_free_
char *network_file
= NULL
,
2977 *dhcp4_address
= NULL
,
2978 *ipv4ll_address
= NULL
;
2979 union in_addr_union address
;
2980 union in_addr_union route_dst
;
2986 r
= parse_env_file(NULL
, link
->state_file
,
2987 "NETWORK_FILE", &network_file
,
2988 "ADDRESSES", &addresses
,
2990 "DHCP4_ADDRESS", &dhcp4_address
,
2991 "IPV4LL_ADDRESS", &ipv4ll_address
);
2992 if (r
< 0 && r
!= -ENOENT
)
2993 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3000 suffix
= strrchr(network_file
, '.');
3002 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3003 goto network_file_fail
;
3007 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3009 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3010 goto network_file_fail
;
3013 r
= network_apply(network
, link
);
3015 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3024 _cleanup_free_
char *address_str
= NULL
;
3025 char *prefixlen_str
;
3027 unsigned char prefixlen
;
3029 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3031 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3037 prefixlen_str
= strchr(address_str
, '/');
3038 if (!prefixlen_str
) {
3039 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3043 *prefixlen_str
++ = '\0';
3045 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3047 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3051 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3053 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3057 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3059 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3068 _cleanup_free_
char *route_str
= NULL
;
3069 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3071 char *prefixlen_str
;
3073 unsigned char prefixlen
, tos
, table
;
3076 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3078 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3084 prefixlen_str
= strchr(route_str
, '/');
3085 if (!prefixlen_str
) {
3086 log_link_debug(link
, "Failed to parse route %s", route_str
);
3090 *prefixlen_str
++ = '\0';
3092 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3094 log_link_debug(link
,
3095 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3100 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3102 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3106 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3108 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3110 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3111 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3112 0, route_expire_handler
, route
);
3114 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3117 route
->lifetime
= lifetime
;
3118 sd_event_source_unref(route
->expire
);
3119 route
->expire
= TAKE_PTR(expire
);
3123 if (dhcp4_address
) {
3124 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3126 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3127 goto dhcp4_address_fail
;
3130 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3132 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3134 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3136 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3141 if (ipv4ll_address
) {
3142 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3144 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3145 goto ipv4ll_address_fail
;
3148 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3150 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3152 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3154 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3157 ipv4ll_address_fail
:
3162 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3163 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3164 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3173 r
= link_new(m
, message
, ret
);
3179 log_link_debug(link
, "Link %d added", link
->ifindex
);
3181 r
= link_load(link
);
3185 if (detect_container() <= 0) {
3186 /* not in a container, udev will be around */
3187 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3188 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3190 log_link_warning_errno(link
, r
, "Could not find device: %m");
3194 r
= sd_device_get_is_initialized(device
);
3196 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3201 log_link_debug(link
, "link pending udev initialization...");
3205 r
= device_is_renaming(device
);
3207 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3211 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3215 r
= link_initialized(link
, device
);
3219 r
= link_initialized_and_synced(link
);
3226 link_enter_failed(link
);
3230 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3235 log_link_info(link
, "Gained IPv6LL");
3237 link
->ipv6ll_address
= *address
;
3238 link_check_ready(link
);
3240 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3241 r
= link_acquire_ipv6_conf(link
);
3243 link_enter_failed(link
);
3251 static int link_carrier_gained(Link
*link
) {
3256 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3257 r
= link_acquire_conf(link
);
3259 link_enter_failed(link
);
3263 r
= link_request_set_addresses(link
);
3268 r
= link_handle_bound_by_list(link
);
3275 static int link_carrier_lost(Link
*link
) {
3280 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3283 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3284 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3285 * configuration and stop the clients as well. */
3286 if (link
->setting_mtu
)
3289 r
= link_stop_clients(link
);
3291 link_enter_failed(link
);
3295 if (link_dhcp4_server_enabled(link
))
3296 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3298 r
= link_drop_config(link
);
3302 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3303 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3304 r
= link_drop_foreign_config(link
);
3309 r
= link_handle_bound_by_list(link
);
3316 int link_carrier_reset(Link
*link
) {
3321 if (link_has_carrier(link
)) {
3322 r
= link_carrier_lost(link
);
3326 r
= link_carrier_gained(link
);
3330 log_link_info(link
, "Reset carrier");
3336 int link_update(Link
*link
, sd_netlink_message
*m
) {
3337 struct ether_addr mac
;
3340 bool had_carrier
, carrier_gained
, carrier_lost
;
3344 assert(link
->ifname
);
3347 if (link
->state
== LINK_STATE_LINGER
) {
3348 log_link_info(link
, "Link re-added");
3349 link_set_state(link
, LINK_STATE_CONFIGURING
);
3351 r
= link_new_carrier_maps(link
);
3356 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3357 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3358 Manager
*manager
= link
->manager
;
3360 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3363 r
= link_add(manager
, m
, &link
);
3368 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3369 if (r
>= 0 && mtu
> 0) {
3371 if (link
->original_mtu
== 0) {
3372 link
->original_mtu
= mtu
;
3373 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3376 if (link
->dhcp_client
) {
3377 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3380 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3384 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3386 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3390 /* The kernel may broadcast NEWLINK messages without the MAC address
3391 set, simply ignore them. */
3392 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3394 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3397 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3400 log_link_debug(link
, "MAC address: "
3401 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3402 mac
.ether_addr_octet
[0],
3403 mac
.ether_addr_octet
[1],
3404 mac
.ether_addr_octet
[2],
3405 mac
.ether_addr_octet
[3],
3406 mac
.ether_addr_octet
[4],
3407 mac
.ether_addr_octet
[5]);
3410 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3412 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3415 if (link
->dhcp_client
) {
3416 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3417 (const uint8_t *) &link
->mac
,
3421 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3423 r
= dhcp4_set_client_identifier(link
);
3428 if (link
->dhcp6_client
) {
3429 const DUID
* duid
= link_get_duid(link
);
3431 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3432 (const uint8_t *) &link
->mac
,
3436 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3438 if (link
->network
->iaid_set
) {
3439 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3440 link
->network
->iaid
);
3442 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3445 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3447 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3448 duid
->raw_data_len
);
3450 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3454 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3456 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3460 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3462 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3467 had_carrier
= link_has_carrier(link
);
3469 r
= link_update_flags(link
, m
);
3473 r
= link_update_lldp(link
);
3477 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3478 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3480 if (carrier_gained
) {
3481 log_link_info(link
, "Gained carrier");
3483 r
= link_carrier_gained(link
);
3486 } else if (carrier_lost
) {
3487 log_link_info(link
, "Lost carrier");
3489 r
= link_carrier_lost(link
);
3497 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3505 if (hashmap_isempty(h
))
3509 HASHMAP_FOREACH(link
, h
, i
) {
3513 fprintf(f
, "%i", link
->ifindex
);
3520 int link_save(Link
*link
) {
3521 _cleanup_free_
char *temp_path
= NULL
;
3522 _cleanup_fclose_
FILE *f
= NULL
;
3523 const char *admin_state
, *oper_state
;
3530 assert(link
->state_file
);
3531 assert(link
->lease_file
);
3532 assert(link
->manager
);
3534 if (link
->state
== LINK_STATE_LINGER
) {
3535 (void) unlink(link
->state_file
);
3539 link_lldp_save(link
);
3541 admin_state
= link_state_to_string(link
->state
);
3542 assert(admin_state
);
3544 oper_state
= link_operstate_to_string(link
->operstate
);
3547 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3551 (void) fchmod(fileno(f
), 0644);
3554 "# This is private data. Do not parse.\n"
3557 admin_state
, oper_state
);
3559 if (link
->network
) {
3560 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3561 const char *dhcp_domainname
= NULL
, *p
;
3562 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3566 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3567 yes_no(link
->network
->required_for_online
));
3569 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3570 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3572 if (link
->dhcp6_client
) {
3573 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3574 if (r
< 0 && r
!= -ENOMSG
)
3575 log_link_debug(link
, "No DHCPv6 lease");
3578 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3583 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3584 _cleanup_free_
char *b
= NULL
;
3586 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3587 &link
->network
->dns
[j
].address
, &b
);
3589 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3599 if (link
->network
->dhcp_use_dns
&&
3601 const struct in_addr
*addresses
;
3603 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3605 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3609 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3610 struct in6_addr
*in6_addrs
;
3612 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3616 serialize_in6_addrs(f
, in6_addrs
, r
);
3621 /* Make sure to flush out old entries before we use the NDISC data */
3624 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3627 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3631 serialize_in6_addrs(f
, &dd
->address
, 1);
3640 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3642 if (link
->network
->dhcp_use_ntp
&&
3644 const struct in_addr
*addresses
;
3646 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3648 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3652 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3653 struct in6_addr
*in6_addrs
;
3656 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3661 serialize_in6_addrs(f
, in6_addrs
, r
);
3665 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3667 fputstrv(f
, hosts
, NULL
, &space
);
3672 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3673 if (link
->dhcp_lease
) {
3674 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3675 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3678 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3681 fputs("DOMAINS=", f
);
3683 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3684 fputs_with_space(f
, p
, NULL
, &space
);
3686 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3689 if (dhcp_domainname
)
3690 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3692 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3694 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3696 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3697 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3702 fputs("ROUTE_DOMAINS=", f
);
3704 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3705 fputs_with_space(f
, p
, NULL
, &space
);
3707 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3710 if (dhcp_domainname
)
3711 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3713 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3715 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3717 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3718 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3723 fprintf(f
, "LLMNR=%s\n",
3724 resolve_support_to_string(link
->network
->llmnr
));
3725 fprintf(f
, "MDNS=%s\n",
3726 resolve_support_to_string(link
->network
->mdns
));
3727 if (link
->network
->dns_default_route
>= 0)
3728 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3730 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3731 fprintf(f
, "DNS_OVER_TLS=%s\n",
3732 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3734 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3735 fprintf(f
, "DNSSEC=%s\n",
3736 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3738 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3741 fputs("DNSSEC_NTA=", f
);
3743 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3744 fputs_with_space(f
, n
, NULL
, &space
);
3748 fputs("ADDRESSES=", f
);
3750 SET_FOREACH(a
, link
->addresses
, i
) {
3751 _cleanup_free_
char *address_str
= NULL
;
3753 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3757 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3762 fputs("ROUTES=", f
);
3764 SET_FOREACH(route
, link
->routes
, i
) {
3765 _cleanup_free_
char *route_str
= NULL
;
3767 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3771 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3772 space
? " " : "", route_str
,
3773 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3780 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3781 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3783 if (link
->dhcp_lease
) {
3784 struct in_addr address
;
3785 const char *tz
= NULL
;
3787 assert(link
->network
);
3789 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3791 fprintf(f
, "TIMEZONE=%s\n", tz
);
3793 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3795 fputs("DHCP4_ADDRESS=", f
);
3796 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3800 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3808 (void) unlink(link
->lease_file
);
3811 struct in_addr address
;
3813 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3815 fputs("IPV4LL_ADDRESS=", f
);
3816 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3821 r
= fflush_and_check(f
);
3825 if (rename(temp_path
, link
->state_file
) < 0) {
3833 (void) unlink(link
->state_file
);
3835 (void) unlink(temp_path
);
3837 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3840 /* The serialized state in /run is no longer up-to-date. */
3841 void link_dirty(Link
*link
) {
3846 /* mark manager dirty as link is dirty */
3847 manager_dirty(link
->manager
);
3849 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3851 /* allocation errors are ignored */
3854 r
= set_put(link
->manager
->dirty_links
, link
);
3856 /* don't take another ref if the link was already dirty */
3862 /* The serialized state in /run is up-to-date */
3863 void link_clean(Link
*link
) {
3865 assert(link
->manager
);
3867 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3870 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3871 [LINK_STATE_PENDING
] = "pending",
3872 [LINK_STATE_INITIALIZED
] = "initialized",
3873 [LINK_STATE_CONFIGURING
] = "configuring",
3874 [LINK_STATE_CONFIGURED
] = "configured",
3875 [LINK_STATE_UNMANAGED
] = "unmanaged",
3876 [LINK_STATE_FAILED
] = "failed",
3877 [LINK_STATE_LINGER
] = "linger",
3880 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);