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 log_link_debug(link
, "Unmanaged");
700 link_set_state(link
, LINK_STATE_UNMANAGED
);
705 int link_stop_clients(Link
*link
) {
709 assert(link
->manager
);
710 assert(link
->manager
->event
);
712 if (link
->dhcp_client
) {
713 k
= sd_dhcp_client_stop(link
->dhcp_client
);
715 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
719 k
= sd_ipv4ll_stop(link
->ipv4ll
);
721 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
724 if (link
->dhcp6_client
) {
725 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
727 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
731 k
= sd_ndisc_stop(link
->ndisc
);
733 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
737 k
= sd_radv_stop(link
->radv
);
739 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
742 link_lldp_emit_stop(link
);
746 void link_enter_failed(Link
*link
) {
749 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
752 log_link_warning(link
, "Failed");
754 link_set_state(link
, LINK_STATE_FAILED
);
756 link_stop_clients(link
);
761 static Address
* link_find_dhcp_server_address(Link
*link
) {
765 assert(link
->network
);
767 /* The first statically configured address if there is any */
768 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
770 if (address
->family
!= AF_INET
)
773 if (in_addr_is_null(address
->family
, &address
->in_addr
))
779 /* If that didn't work, find a suitable address we got from the pool */
780 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
781 if (address
->family
!= AF_INET
)
790 static int link_join_netdevs_after_configured(Link
*link
) {
795 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
796 if (netdev
->ifindex
> 0)
797 /* Assume already enslaved. */
800 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
803 log_struct(LOG_DEBUG
,
804 LOG_LINK_INTERFACE(link
),
805 LOG_NETDEV_INTERFACE(netdev
),
806 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
808 r
= netdev_join(netdev
, link
, NULL
);
810 return log_struct_errno(LOG_WARNING
, r
,
811 LOG_LINK_INTERFACE(link
),
812 LOG_NETDEV_INTERFACE(netdev
),
813 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
819 static void link_enter_configured(Link
*link
) {
821 assert(link
->network
);
823 if (link
->state
!= LINK_STATE_CONFIGURING
)
826 log_link_info(link
, "Configured");
828 link_set_state(link
, LINK_STATE_CONFIGURED
);
830 (void) link_join_netdevs_after_configured(link
);
835 static int link_request_set_routing_policy_rule(Link
*link
) {
836 RoutingPolicyRule
*rule
, *rrule
= NULL
;
840 assert(link
->network
);
842 link_set_state(link
, LINK_STATE_CONFIGURING
);
843 link
->routing_policy_rules_configured
= false;
845 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
846 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
847 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
848 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
850 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
854 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
856 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
857 link_enter_failed(link
);
861 link
->routing_policy_rule_messages
++;
864 routing_policy_rule_purge(link
->manager
, link
);
865 if (link
->routing_policy_rule_messages
== 0) {
866 link
->routing_policy_rules_configured
= true;
867 link_check_ready(link
);
869 log_link_debug(link
, "Setting routing policy rules");
874 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
878 assert(link
->route_messages
> 0);
879 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
880 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
882 link
->route_messages
--;
884 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
887 r
= sd_netlink_message_get_errno(m
);
888 if (r
< 0 && r
!= -EEXIST
)
889 log_link_warning_errno(link
, r
, "Could not set route: %m");
891 if (link
->route_messages
== 0) {
892 log_link_debug(link
, "Routes set");
893 link
->static_routes_configured
= true;
894 link_check_ready(link
);
900 int link_request_set_routes(Link
*link
) {
902 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
903 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
910 assert(link
->network
);
911 assert(link
->addresses_configured
);
912 assert(link
->address_messages
== 0);
913 assert(link
->state
!= _LINK_STATE_INVALID
);
915 link_set_state(link
, LINK_STATE_CONFIGURING
);
916 link
->static_routes_configured
= false;
918 r
= link_request_set_routing_policy_rule(link
);
922 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
923 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
924 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
926 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
929 r
= route_configure(rt
, link
, route_handler
);
931 log_link_warning_errno(link
, r
, "Could not set routes: %m");
932 link_enter_failed(link
);
936 link
->route_messages
++;
939 if (link
->route_messages
== 0) {
940 link
->static_routes_configured
= true;
941 link_check_ready(link
);
943 log_link_debug(link
, "Setting routes");
948 void link_check_ready(Link
*link
) {
954 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
960 if (!link
->addresses_configured
)
963 if (!link
->neighbors_configured
)
966 SET_FOREACH(a
, link
->addresses
, i
)
967 if (!address_is_ready(a
))
970 if (!link
->addresses_ready
) {
971 link
->addresses_ready
= true;
972 link_request_set_routes(link
);
976 if (!link
->static_routes_configured
)
979 if (!link
->routing_policy_rules_configured
)
982 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
985 if (link_ipv6ll_enabled(link
) &&
986 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
989 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
990 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
991 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
992 /* When DHCP is enabled, at least one protocol must provide an address, or
993 * an IPv4ll fallback address must be configured. */
996 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
999 if (link
->state
!= LINK_STATE_CONFIGURED
)
1000 link_enter_configured(link
);
1005 static int link_request_set_neighbors(Link
*link
) {
1010 assert(link
->network
);
1011 assert(link
->state
!= _LINK_STATE_INVALID
);
1013 link_set_state(link
, LINK_STATE_CONFIGURING
);
1014 link
->neighbors_configured
= false;
1016 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1017 r
= neighbor_configure(neighbor
, link
, NULL
);
1019 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1020 link_enter_failed(link
);
1025 if (link
->neighbor_messages
== 0) {
1026 link
->neighbors_configured
= true;
1027 link_check_ready(link
);
1029 log_link_debug(link
, "Setting neighbors");
1034 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1040 assert(link
->ifname
);
1041 assert(link
->address_messages
> 0);
1042 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1043 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1045 link
->address_messages
--;
1047 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1050 r
= sd_netlink_message_get_errno(m
);
1051 if (r
< 0 && r
!= -EEXIST
)
1052 log_link_warning_errno(link
, r
, "could not set address: %m");
1054 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1056 if (link
->address_messages
== 0) {
1057 log_link_debug(link
, "Addresses set");
1058 link
->addresses_configured
= true;
1059 link_check_ready(link
);
1065 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1066 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1067 size_t n_addresses
= 0, n_allocated
= 0;
1070 log_debug("Copying DNS server information from %s", link
->ifname
);
1075 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1078 /* Only look for IPv4 addresses */
1079 if (link
->network
->dns
[i
].family
!= AF_INET
)
1082 ia
= link
->network
->dns
[i
].address
.in
;
1084 /* Never propagate obviously borked data */
1085 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1088 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1091 addresses
[n_addresses
++] = ia
;
1094 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1095 const struct in_addr
*da
= NULL
;
1098 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1101 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1104 for (j
= 0; j
< n
; j
++)
1105 if (in4_addr_is_non_local(&da
[j
]))
1106 addresses
[n_addresses
++] = da
[j
];
1110 if (n_addresses
<= 0)
1113 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1116 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1117 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1118 size_t n_addresses
= 0, n_allocated
= 0;
1124 log_debug("Copying NTP server information from %s", link
->ifname
);
1126 STRV_FOREACH(a
, link
->network
->ntp
) {
1127 union in_addr_union ia
;
1129 /* Only look for IPv4 addresses */
1130 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1133 /* Never propagate obviously borked data */
1134 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1137 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1140 addresses
[n_addresses
++] = ia
.in
;
1143 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1144 const struct in_addr
*da
= NULL
;
1147 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1150 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1153 for (j
= 0; j
< n
; j
++)
1154 if (in4_addr_is_non_local(&da
[j
]))
1155 addresses
[n_addresses
++] = da
[j
];
1159 if (n_addresses
<= 0)
1162 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1165 static int link_set_bridge_fdb(Link
*link
) {
1166 FdbEntry
*fdb_entry
;
1169 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1170 r
= fdb_entry_configure(link
, fdb_entry
);
1172 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1178 static int link_request_set_addresses(Link
*link
) {
1179 AddressLabel
*label
;
1184 assert(link
->network
);
1185 assert(link
->state
!= _LINK_STATE_INVALID
);
1187 link_set_state(link
, LINK_STATE_CONFIGURING
);
1189 /* Reset all *_configured flags we are configuring. */
1190 link
->addresses_configured
= false;
1191 link
->addresses_ready
= false;
1192 link
->neighbors_configured
= false;
1193 link
->static_routes_configured
= false;
1194 link
->routing_policy_rules_configured
= false;
1196 r
= link_set_bridge_fdb(link
);
1200 r
= link_request_set_neighbors(link
);
1204 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1207 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1209 r
= address_configure(ad
, link
, address_handler
, update
);
1211 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1212 link_enter_failed(link
);
1216 link
->address_messages
++;
1219 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1220 r
= address_label_configure(label
, link
, NULL
, false);
1222 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1223 link_enter_failed(link
);
1227 link
->address_label_messages
++;
1230 /* now that we can figure out a default address for the dhcp server,
1232 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1234 Link
*uplink
= NULL
;
1235 bool acquired_uplink
= false;
1237 address
= link_find_dhcp_server_address(link
);
1239 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1240 link_enter_failed(link
);
1244 /* use the server address' subnet as the pool */
1245 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1246 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1251 r = sd_dhcp_server_set_router(link->dhcp_server,
1252 &main_address->in_addr.in);
1257 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1258 r
= sd_dhcp_server_set_max_lease_time(
1260 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1265 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1266 r
= sd_dhcp_server_set_default_lease_time(
1268 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1273 if (link
->network
->dhcp_server_emit_dns
) {
1275 if (link
->network
->n_dhcp_server_dns
> 0)
1276 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1278 uplink
= manager_find_uplink(link
->manager
, link
);
1279 acquired_uplink
= true;
1282 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1285 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1288 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1291 if (link
->network
->dhcp_server_emit_ntp
) {
1293 if (link
->network
->n_dhcp_server_ntp
> 0)
1294 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1296 if (!acquired_uplink
)
1297 uplink
= manager_find_uplink(link
->manager
, link
);
1300 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1303 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1307 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1310 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1312 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1314 if (link
->network
->dhcp_server_emit_timezone
) {
1315 _cleanup_free_
char *buffer
= NULL
;
1316 const char *tz
= NULL
;
1318 if (link
->network
->dhcp_server_timezone
)
1319 tz
= link
->network
->dhcp_server_timezone
;
1321 r
= get_timezone(&buffer
);
1323 log_warning_errno(r
, "Failed to determine timezone: %m");
1329 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1334 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1335 r
= sd_dhcp_server_start(link
->dhcp_server
);
1337 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1339 link_enter_failed(link
);
1345 log_link_debug(link
, "Offering DHCPv4 leases");
1348 if (link
->address_messages
== 0) {
1349 link
->addresses_configured
= true;
1350 link_check_ready(link
);
1352 log_link_debug(link
, "Setting addresses");
1357 static int link_set_bridge_vlan(Link
*link
) {
1360 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1362 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1367 static int link_set_proxy_arp(Link
*link
) {
1370 if (!link_proxy_arp_enabled(link
))
1373 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1375 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1380 static int link_configure_after_setting_mtu(Link
*link
);
1382 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1387 assert(link
->ifname
);
1389 link
->setting_mtu
= false;
1391 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1394 r
= sd_netlink_message_get_errno(m
);
1396 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1400 log_link_debug(link
, "Setting MTU done.");
1402 if (link
->state
== LINK_STATE_INITIALIZED
)
1403 (void) link_configure_after_setting_mtu(link
);
1408 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1409 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1413 assert(link
->manager
);
1414 assert(link
->manager
->rtnl
);
1416 if (mtu
== 0 || link
->setting_mtu
)
1419 if (link
->mtu
== mtu
)
1422 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1424 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1426 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1428 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1429 * for this interface, then disable IPv6 else enable it. */
1430 (void) link_enable_ipv6(link
);
1432 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1433 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1434 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1436 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1437 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1442 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1444 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1446 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1447 link_netlink_destroy_callback
, link
);
1449 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1452 link
->setting_mtu
= true;
1457 static bool link_reduces_vlan_mtu(Link
*link
) {
1458 /* See netif_reduces_vlan_mtu() in kernel. */
1459 return streq_ptr(link
->kind
, "macsec");
1462 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1467 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1468 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1469 /* See vlan_dev_change_mtu() in kernel. */
1470 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1472 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1473 /* See macvlan_change_mtu() in kernel. */
1479 static int link_configure_mtu(Link
*link
) {
1483 assert(link
->network
);
1485 if (link
->network
->mtu
> 0)
1486 return link_set_mtu(link
, link
->network
->mtu
);
1488 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1489 if (link
->mtu
>= mtu
)
1492 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1493 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1496 return link_set_mtu(link
, mtu
);
1499 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1504 assert(link
->ifname
);
1506 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1509 r
= sd_netlink_message_get_errno(m
);
1511 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1516 static int link_set_flags(Link
*link
) {
1517 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1518 unsigned ifi_change
= 0;
1519 unsigned ifi_flags
= 0;
1523 assert(link
->manager
);
1524 assert(link
->manager
->rtnl
);
1526 if (link
->flags
& IFF_LOOPBACK
)
1532 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1535 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1537 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1539 if (link
->network
->arp
>= 0) {
1540 ifi_change
|= IFF_NOARP
;
1541 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1544 if (link
->network
->multicast
>= 0) {
1545 ifi_change
|= IFF_MULTICAST
;
1546 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1549 if (link
->network
->allmulticast
>= 0) {
1550 ifi_change
|= IFF_ALLMULTI
;
1551 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1554 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1556 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1558 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1559 link_netlink_destroy_callback
, link
);
1561 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1568 static int link_acquire_ipv6_conf(Link
*link
) {
1573 if (link_ipv6_accept_ra_enabled(link
)) {
1574 assert(link
->ndisc
);
1576 log_link_debug(link
, "Discovering IPv6 routers");
1578 r
= sd_ndisc_start(link
->ndisc
);
1579 if (r
< 0 && r
!= -EBUSY
)
1580 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1583 if (link_radv_enabled(link
)) {
1585 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1587 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1589 r
= sd_radv_start(link
->radv
);
1590 if (r
< 0 && r
!= -EBUSY
)
1591 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1594 (void) dhcp6_request_prefix_delegation(link
);
1599 static int link_acquire_ipv4_conf(Link
*link
) {
1603 assert(link
->manager
);
1604 assert(link
->manager
->event
);
1606 if (link_ipv4ll_enabled(link
)) {
1607 assert(link
->ipv4ll
);
1609 log_link_debug(link
, "Acquiring IPv4 link-local address");
1611 r
= sd_ipv4ll_start(link
->ipv4ll
);
1613 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1616 if (link_dhcp4_enabled(link
)) {
1617 assert(link
->dhcp_client
);
1619 log_link_debug(link
, "Acquiring DHCPv4 lease");
1621 r
= sd_dhcp_client_start(link
->dhcp_client
);
1623 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1629 static int link_acquire_conf(Link
*link
) {
1634 r
= link_acquire_ipv4_conf(link
);
1638 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1639 r
= link_acquire_ipv6_conf(link
);
1644 if (link_lldp_emit_enabled(link
)) {
1645 r
= link_lldp_emit_start(link
);
1647 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1653 bool link_has_carrier(Link
*link
) {
1654 /* see Documentation/networking/operstates.txt in the kernel sources */
1656 if (link
->kernel_operstate
== IF_OPER_UP
)
1659 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1660 /* operstate may not be implemented, so fall back to flags */
1661 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1662 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1668 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1673 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1676 r
= sd_netlink_message_get_errno(m
);
1678 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1683 static int link_configure_addrgen_mode(Link
*link
) {
1684 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1685 uint8_t ipv6ll_mode
;
1689 assert(link
->network
);
1690 assert(link
->manager
);
1691 assert(link
->manager
->rtnl
);
1693 if (!socket_ipv6_is_supported())
1696 log_link_debug(link
, "Setting address genmode for link");
1698 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1700 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1702 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1704 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1706 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1708 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1710 if (!link_ipv6ll_enabled(link
))
1711 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1712 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1713 /* The file may not exist. And event if it exists, when stable_secret is unset,
1714 * reading the file fails with EIO. */
1715 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1717 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1719 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1721 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1723 r
= sd_netlink_message_close_container(req
);
1725 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1727 r
= sd_netlink_message_close_container(req
);
1729 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1731 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1732 link_netlink_destroy_callback
, link
);
1734 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1741 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1746 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1749 r
= sd_netlink_message_get_errno(m
);
1751 /* we warn but don't fail the link, as it may be brought up later */
1752 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1757 static int link_up(Link
*link
) {
1758 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1762 assert(link
->network
);
1763 assert(link
->manager
);
1764 assert(link
->manager
->rtnl
);
1766 log_link_debug(link
, "Bringing link up");
1768 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1770 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1772 /* set it free if not enslaved with networkd */
1773 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1774 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1776 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1779 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1781 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1783 if (link
->network
->mac
) {
1784 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1786 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1789 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1790 link_netlink_destroy_callback
, link
);
1792 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1799 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1804 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1807 r
= sd_netlink_message_get_errno(m
);
1809 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1814 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1815 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1819 assert(link
->manager
);
1820 assert(link
->manager
->rtnl
);
1822 log_link_debug(link
, "Bringing link down");
1824 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1825 RTM_SETLINK
, link
->ifindex
);
1827 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1829 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1831 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1833 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1834 callback
?: link_down_handler
,
1835 link_netlink_destroy_callback
, link
);
1837 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1844 static int link_handle_bound_to_list(Link
*link
) {
1848 bool required_up
= false;
1849 bool link_is_up
= false;
1853 if (hashmap_isempty(link
->bound_to_links
))
1856 if (link
->flags
& IFF_UP
)
1859 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1860 if (link_has_carrier(l
)) {
1865 if (!required_up
&& link_is_up
) {
1866 r
= link_down(link
, NULL
);
1869 } else if (required_up
&& !link_is_up
) {
1878 static int link_handle_bound_by_list(Link
*link
) {
1885 if (hashmap_isempty(link
->bound_by_links
))
1888 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1889 r
= link_handle_bound_to_list(l
);
1897 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1903 if (link
== carrier
)
1906 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1909 r
= hashmap_ensure_allocated(h
, NULL
);
1913 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1920 static int link_new_bound_by_list(Link
*link
) {
1925 bool list_updated
= false;
1928 assert(link
->manager
);
1932 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1933 if (!carrier
->network
)
1936 if (strv_isempty(carrier
->network
->bind_carrier
))
1939 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1940 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1944 list_updated
= true;
1951 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1952 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1956 link_dirty(carrier
);
1962 static int link_new_bound_to_list(Link
*link
) {
1967 bool list_updated
= false;
1970 assert(link
->manager
);
1975 if (strv_isempty(link
->network
->bind_carrier
))
1980 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1981 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1982 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1986 list_updated
= true;
1993 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1994 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1998 link_dirty(carrier
);
2004 static int link_new_carrier_maps(Link
*link
) {
2007 r
= link_new_bound_by_list(link
);
2011 r
= link_handle_bound_by_list(link
);
2015 r
= link_new_bound_to_list(link
);
2019 r
= link_handle_bound_to_list(link
);
2026 static void link_free_bound_to_list(Link
*link
) {
2030 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2031 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2033 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2034 link_dirty(bound_to
);
2040 static void link_free_bound_by_list(Link
*link
) {
2044 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2045 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2047 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2048 link_dirty(bound_by
);
2049 link_handle_bound_to_list(bound_by
);
2056 static void link_free_carrier_maps(Link
*link
) {
2057 bool list_updated
= false;
2061 if (!hashmap_isempty(link
->bound_to_links
)) {
2062 link_free_bound_to_list(link
);
2063 list_updated
= true;
2066 if (!hashmap_isempty(link
->bound_by_links
)) {
2067 link_free_bound_by_list(link
);
2068 list_updated
= true;
2077 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2084 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2088 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2092 r
= set_put(master
->slaves
, link
);
2100 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2105 if (!link
->manager
|| !netdev
)
2108 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2111 link_unref(set_remove(master
->slaves
, link
));
2114 static void link_detach_from_manager(Link
*link
) {
2115 if (!link
|| !link
->manager
)
2118 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2121 /* The following must be called at last. */
2122 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2126 void link_drop(Link
*link
) {
2127 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2130 link_set_state(link
, LINK_STATE_LINGER
);
2132 link_free_carrier_maps(link
);
2134 if (link
->network
) {
2135 link_drop_from_master(link
, link
->network
->bridge
);
2136 link_drop_from_master(link
, link
->network
->bond
);
2139 log_link_debug(link
, "Link removed");
2141 (void) unlink(link
->state_file
);
2142 link_detach_from_manager(link
);
2145 static int link_joined(Link
*link
) {
2149 assert(link
->network
);
2151 if (!hashmap_isempty(link
->bound_to_links
)) {
2152 r
= link_handle_bound_to_list(link
);
2155 } else if (!(link
->flags
& IFF_UP
)) {
2158 link_enter_failed(link
);
2163 if (link
->network
->bridge
) {
2164 r
= link_set_bridge(link
);
2166 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2168 r
= link_append_to_master(link
, link
->network
->bridge
);
2170 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2173 if (link
->network
->bond
) {
2174 r
= link_set_bond(link
);
2176 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2178 r
= link_append_to_master(link
, link
->network
->bond
);
2180 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2183 if (link
->network
->use_br_vlan
&&
2184 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2185 r
= link_set_bridge_vlan(link
);
2187 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2190 /* Skip setting up addresses until it gets carrier,
2191 or it would try to set addresses twice,
2192 which is bad for non-idempotent steps. */
2193 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2196 return link_request_set_addresses(link
);
2199 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2203 assert(link
->network
);
2204 assert(link
->enslaving
> 0);
2205 assert(!link
->enslaved_raw
);
2209 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2212 r
= sd_netlink_message_get_errno(m
);
2213 if (r
< 0 && r
!= -EEXIST
) {
2214 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2215 link_enter_failed(link
);
2218 log_link_debug(link
, "Joined netdev");
2220 if (link
->enslaving
== 0) {
2221 link
->enslaved_raw
= true;
2228 static int link_enter_join_netdev(Link
*link
) {
2234 assert(link
->network
);
2235 assert(link
->state
== LINK_STATE_INITIALIZED
);
2237 link_set_state(link
, LINK_STATE_CONFIGURING
);
2240 link
->enslaving
= 0;
2241 link
->enslaved_raw
= false;
2243 if (link
->network
->bond
) {
2244 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2245 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2246 return link_joined(link
);
2248 log_struct(LOG_DEBUG
,
2249 LOG_LINK_INTERFACE(link
),
2250 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2251 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2255 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2257 log_struct_errno(LOG_WARNING
, r
,
2258 LOG_LINK_INTERFACE(link
),
2259 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2260 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2261 link_enter_failed(link
);
2266 if (link
->network
->bridge
) {
2267 log_struct(LOG_DEBUG
,
2268 LOG_LINK_INTERFACE(link
),
2269 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2270 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2274 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2276 log_struct_errno(LOG_WARNING
, r
,
2277 LOG_LINK_INTERFACE(link
),
2278 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2279 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2280 link_enter_failed(link
);
2285 if (link
->network
->vrf
) {
2286 log_struct(LOG_DEBUG
,
2287 LOG_LINK_INTERFACE(link
),
2288 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2289 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2293 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2295 log_struct_errno(LOG_WARNING
, r
,
2296 LOG_LINK_INTERFACE(link
),
2297 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2298 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2299 link_enter_failed(link
);
2304 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2306 if (netdev
->ifindex
> 0)
2307 /* Assume already enslaved. */
2310 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2313 log_struct(LOG_DEBUG
,
2314 LOG_LINK_INTERFACE(link
),
2315 LOG_NETDEV_INTERFACE(netdev
),
2316 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2320 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2322 log_struct_errno(LOG_WARNING
, r
,
2323 LOG_LINK_INTERFACE(link
),
2324 LOG_NETDEV_INTERFACE(netdev
),
2325 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2326 link_enter_failed(link
);
2331 if (link
->enslaving
== 0)
2332 return link_joined(link
);
2337 static int link_set_ipv4_forward(Link
*link
) {
2340 if (!link_ipv4_forward_enabled(link
))
2343 /* We propagate the forwarding flag from one interface to the
2344 * global setting one way. This means: as long as at least one
2345 * interface was configured at any time that had IP forwarding
2346 * enabled the setting will stay on for good. We do this
2347 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2348 * somewhat in sync (see below). */
2350 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2352 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2357 static int link_set_ipv6_forward(Link
*link
) {
2360 if (!link_ipv6_forward_enabled(link
))
2363 /* On Linux, the IPv6 stack does not know a per-interface
2364 * packet forwarding setting: either packet forwarding is on
2365 * for all, or off for all. We hence don't bother with a
2366 * per-interface setting, but simply propagate the interface
2367 * flag, if it is set, to the global flag, one-way. Note that
2368 * while IPv4 would allow a per-interface flag, we expose the
2369 * same behaviour there and also propagate the setting from
2370 * one to all, to keep things simple (see above). */
2372 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2374 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2379 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2380 IPv6PrivacyExtensions s
;
2383 s
= link_ipv6_privacy_extensions(link
);
2387 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2389 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2394 static int link_set_ipv6_accept_ra(Link
*link
) {
2397 /* Make this a NOP if IPv6 is not available */
2398 if (!socket_ipv6_is_supported())
2401 if (link
->flags
& IFF_LOOPBACK
)
2407 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2409 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2414 static int link_set_ipv6_dad_transmits(Link
*link
) {
2417 /* Make this a NOP if IPv6 is not available */
2418 if (!socket_ipv6_is_supported())
2421 if (link
->flags
& IFF_LOOPBACK
)
2427 if (link
->network
->ipv6_dad_transmits
< 0)
2430 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2432 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2437 static int link_set_ipv6_hop_limit(Link
*link
) {
2440 /* Make this a NOP if IPv6 is not available */
2441 if (!socket_ipv6_is_supported())
2444 if (link
->flags
& IFF_LOOPBACK
)
2450 if (link
->network
->ipv6_hop_limit
< 0)
2453 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2455 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2460 static int link_set_ipv6_mtu(Link
*link
) {
2463 /* Make this a NOP if IPv6 is not available */
2464 if (!socket_ipv6_is_supported())
2467 if (link
->flags
& IFF_LOOPBACK
)
2470 if (link
->network
->ipv6_mtu
== 0)
2473 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2475 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2480 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2481 Address
*net_address
;
2489 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2490 if (address_equal(net_address
, address
))
2496 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2505 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2506 if (route_equal(net_route
, route
))
2512 static int link_drop_foreign_config(Link
*link
) {
2518 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2519 /* we consider IPv6LL addresses to be managed by the kernel */
2520 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2523 if (link_is_static_address_configured(link
, address
)) {
2524 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2526 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2528 r
= address_remove(address
, link
, NULL
);
2534 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2535 /* do not touch routes managed by the kernel */
2536 if (route
->protocol
== RTPROT_KERNEL
)
2539 if (link_is_static_route_configured(link
, route
)) {
2540 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2544 r
= route_remove(route
, link
, NULL
);
2553 static int link_drop_config(Link
*link
) {
2554 Address
*address
, *pool_address
;
2559 SET_FOREACH(address
, link
->addresses
, i
) {
2560 /* we consider IPv6LL addresses to be managed by the kernel */
2561 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2564 r
= address_remove(address
, link
, NULL
);
2568 /* If this address came from an address pool, clean up the pool */
2569 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2570 if (address_equal(address
, pool_address
)) {
2571 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2572 address_free(pool_address
);
2578 SET_FOREACH(route
, link
->routes
, i
) {
2579 /* do not touch routes managed by the kernel */
2580 if (route
->protocol
== RTPROT_KERNEL
)
2583 r
= route_remove(route
, link
, NULL
);
2593 static int link_configure(Link
*link
) {
2597 assert(link
->network
);
2598 assert(link
->state
== LINK_STATE_INITIALIZED
);
2600 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2601 return link_configure_can(link
);
2603 /* Drop foreign config, but ignore loopback or critical devices.
2604 * We do not want to remove loopback address or addresses used for root NFS. */
2605 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2606 r
= link_drop_foreign_config(link
);
2611 r
= link_set_proxy_arp(link
);
2615 r
= ipv6_proxy_ndp_addresses_configure(link
);
2619 r
= link_set_ipv4_forward(link
);
2623 r
= link_set_ipv6_forward(link
);
2627 r
= link_set_ipv6_privacy_extensions(link
);
2631 r
= link_set_ipv6_accept_ra(link
);
2635 r
= link_set_ipv6_dad_transmits(link
);
2639 r
= link_set_ipv6_hop_limit(link
);
2643 r
= link_set_flags(link
);
2647 r
= link_set_ipv6_mtu(link
);
2651 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2652 r
= ipv4ll_configure(link
);
2657 if (link_dhcp4_enabled(link
)) {
2658 r
= dhcp4_set_promote_secondaries(link
);
2662 r
= dhcp4_configure(link
);
2667 if (link_dhcp4_server_enabled(link
)) {
2668 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2672 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2677 if (link_dhcp6_enabled(link
) ||
2678 link_ipv6_accept_ra_enabled(link
)) {
2679 r
= dhcp6_configure(link
);
2684 if (link_ipv6_accept_ra_enabled(link
)) {
2685 r
= ndisc_configure(link
);
2690 if (link_radv_enabled(link
)) {
2691 r
= radv_configure(link
);
2696 if (link_lldp_rx_enabled(link
)) {
2697 r
= link_lldp_rx_configure(link
);
2702 r
= link_configure_mtu(link
);
2706 r
= link_configure_addrgen_mode(link
);
2710 return link_configure_after_setting_mtu(link
);
2713 static int link_configure_after_setting_mtu(Link
*link
) {
2717 assert(link
->network
);
2718 assert(link
->state
== LINK_STATE_INITIALIZED
);
2720 if (link
->setting_mtu
)
2723 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2724 r
= link_acquire_conf(link
);
2729 return link_enter_join_netdev(link
);
2732 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2735 if (duid
->raw_data_len
> 0)
2738 if (duid
->type
!= DUID_TYPE_UUID
)
2741 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2742 duid
->raw_data_len
= sizeof(sd_id128_t
);
2747 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2748 Manager
*manager
= userdata
;
2749 const sd_bus_error
*e
;
2759 e
= sd_bus_message_get_error(m
);
2761 log_error_errno(sd_bus_error_get_errno(e
),
2762 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2767 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2771 if (sz
!= sizeof(sd_id128_t
)) {
2772 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2776 memcpy(&manager
->product_uuid
, a
, sz
);
2777 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2778 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2780 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2783 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2784 r
= link_configure(link
);
2786 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2789 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2791 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2792 * even if the method fails. */
2793 manager
->has_product_uuid
= true;
2798 static bool link_requires_uuid(Link
*link
) {
2802 assert(link
->manager
);
2803 assert(link
->network
);
2805 duid
= link_get_duid(link
);
2806 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2809 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2812 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2818 static int link_configure_duid(Link
*link
) {
2824 assert(link
->manager
);
2825 assert(link
->network
);
2828 duid
= link_get_duid(link
);
2830 if (!link_requires_uuid(link
))
2833 if (m
->has_product_uuid
) {
2834 (void) duid_set_uuid(duid
, m
->product_uuid
);
2838 if (!m
->links_requesting_uuid
) {
2839 r
= manager_request_product_uuid(m
, link
);
2844 log_link_warning_errno(link
, r
,
2845 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2849 r
= set_put(m
->links_requesting_uuid
, link
);
2853 r
= set_put(m
->duids_requesting_uuid
, duid
);
2861 static int link_initialized_and_synced(Link
*link
) {
2866 assert(link
->ifname
);
2867 assert(link
->manager
);
2869 /* We may get called either from the asynchronous netlink callback,
2870 * or directly for link_add() if running in a container. See link_add(). */
2871 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2874 log_link_debug(link
, "Link state is up-to-date");
2875 link_set_state(link
, LINK_STATE_INITIALIZED
);
2877 r
= link_new_bound_by_list(link
);
2881 r
= link_handle_bound_by_list(link
);
2885 if (!link
->network
) {
2886 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2887 &link
->mac
, &network
);
2889 link_enter_unmanaged(link
);
2891 } else if (r
== 0 && network
->unmanaged
) {
2892 link_enter_unmanaged(link
);
2897 if (link
->flags
& IFF_LOOPBACK
) {
2898 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2899 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2901 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2902 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2904 if (network
->dhcp_server
)
2905 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2908 r
= network_apply(network
, link
);
2913 r
= link_new_bound_to_list(link
);
2917 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2918 * link_configure() is called later asynchronously. */
2919 r
= link_configure_duid(link
);
2923 r
= link_configure(link
);
2930 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2931 (void) link_initialized_and_synced(link
);
2935 int link_initialized(Link
*link
, sd_device
*device
) {
2936 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2940 assert(link
->manager
);
2941 assert(link
->manager
->rtnl
);
2944 if (link
->state
!= LINK_STATE_PENDING
)
2947 if (link
->sd_device
)
2950 log_link_debug(link
, "udev initialized link");
2951 link_set_state(link
, LINK_STATE_INITIALIZED
);
2953 link
->sd_device
= sd_device_ref(device
);
2955 /* udev has initialized the link, but we don't know if we have yet
2956 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2957 * when it returns we know that the pending NEWLINKs have already been
2958 * processed and that we are up-to-date */
2960 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2965 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2966 link_netlink_destroy_callback
, link
);
2975 static int link_load(Link
*link
) {
2976 _cleanup_free_
char *network_file
= NULL
,
2979 *dhcp4_address
= NULL
,
2980 *ipv4ll_address
= NULL
;
2981 union in_addr_union address
;
2982 union in_addr_union route_dst
;
2988 r
= parse_env_file(NULL
, link
->state_file
,
2989 "NETWORK_FILE", &network_file
,
2990 "ADDRESSES", &addresses
,
2992 "DHCP4_ADDRESS", &dhcp4_address
,
2993 "IPV4LL_ADDRESS", &ipv4ll_address
);
2994 if (r
< 0 && r
!= -ENOENT
)
2995 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3002 suffix
= strrchr(network_file
, '.');
3004 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3005 goto network_file_fail
;
3009 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3011 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3012 goto network_file_fail
;
3015 r
= network_apply(network
, link
);
3017 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3026 _cleanup_free_
char *address_str
= NULL
;
3027 char *prefixlen_str
;
3029 unsigned char prefixlen
;
3031 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3033 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3039 prefixlen_str
= strchr(address_str
, '/');
3040 if (!prefixlen_str
) {
3041 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3045 *prefixlen_str
++ = '\0';
3047 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3049 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3053 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3055 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3059 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3061 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3070 _cleanup_free_
char *route_str
= NULL
;
3071 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3073 char *prefixlen_str
;
3075 unsigned char prefixlen
, tos
, table
;
3078 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3080 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3086 prefixlen_str
= strchr(route_str
, '/');
3087 if (!prefixlen_str
) {
3088 log_link_debug(link
, "Failed to parse route %s", route_str
);
3092 *prefixlen_str
++ = '\0';
3094 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3096 log_link_debug(link
,
3097 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3102 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3104 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3108 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3110 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3112 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3113 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3114 0, route_expire_handler
, route
);
3116 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3119 route
->lifetime
= lifetime
;
3120 sd_event_source_unref(route
->expire
);
3121 route
->expire
= TAKE_PTR(expire
);
3125 if (dhcp4_address
) {
3126 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3128 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3129 goto dhcp4_address_fail
;
3132 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3134 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3136 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3138 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3143 if (ipv4ll_address
) {
3144 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3146 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3147 goto ipv4ll_address_fail
;
3150 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3152 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3154 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3156 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3159 ipv4ll_address_fail
:
3164 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3165 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3166 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3175 r
= link_new(m
, message
, ret
);
3181 log_link_debug(link
, "Link %d added", link
->ifindex
);
3183 r
= link_load(link
);
3187 if (detect_container() <= 0) {
3188 /* not in a container, udev will be around */
3189 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3190 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3192 log_link_warning_errno(link
, r
, "Could not find device: %m");
3196 r
= sd_device_get_is_initialized(device
);
3198 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3203 log_link_debug(link
, "link pending udev initialization...");
3207 r
= device_is_renaming(device
);
3209 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3213 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3217 r
= link_initialized(link
, device
);
3221 r
= link_initialized_and_synced(link
);
3228 link_enter_failed(link
);
3232 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3237 log_link_info(link
, "Gained IPv6LL");
3239 link
->ipv6ll_address
= *address
;
3240 link_check_ready(link
);
3242 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3243 r
= link_acquire_ipv6_conf(link
);
3245 link_enter_failed(link
);
3253 static int link_carrier_gained(Link
*link
) {
3258 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3259 r
= link_acquire_conf(link
);
3261 link_enter_failed(link
);
3265 r
= link_request_set_addresses(link
);
3270 r
= link_handle_bound_by_list(link
);
3277 static int link_carrier_lost(Link
*link
) {
3282 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3285 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3286 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3287 * configuration and stop the clients as well. */
3288 if (link
->setting_mtu
)
3291 r
= link_stop_clients(link
);
3293 link_enter_failed(link
);
3297 if (link_dhcp4_server_enabled(link
))
3298 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3300 r
= link_drop_config(link
);
3304 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3305 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3306 r
= link_drop_foreign_config(link
);
3311 r
= link_handle_bound_by_list(link
);
3318 int link_carrier_reset(Link
*link
) {
3323 if (link_has_carrier(link
)) {
3324 r
= link_carrier_lost(link
);
3328 r
= link_carrier_gained(link
);
3332 log_link_info(link
, "Reset carrier");
3338 int link_update(Link
*link
, sd_netlink_message
*m
) {
3339 struct ether_addr mac
;
3342 bool had_carrier
, carrier_gained
, carrier_lost
;
3346 assert(link
->ifname
);
3349 if (link
->state
== LINK_STATE_LINGER
) {
3350 log_link_info(link
, "Link re-added");
3351 link_set_state(link
, LINK_STATE_CONFIGURING
);
3353 r
= link_new_carrier_maps(link
);
3358 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3359 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3360 Manager
*manager
= link
->manager
;
3362 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3365 r
= link_add(manager
, m
, &link
);
3370 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3371 if (r
>= 0 && mtu
> 0) {
3373 if (link
->original_mtu
== 0) {
3374 link
->original_mtu
= mtu
;
3375 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3378 if (link
->dhcp_client
) {
3379 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3382 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3386 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3388 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3392 /* The kernel may broadcast NEWLINK messages without the MAC address
3393 set, simply ignore them. */
3394 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3396 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3399 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3402 log_link_debug(link
, "MAC address: "
3403 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3404 mac
.ether_addr_octet
[0],
3405 mac
.ether_addr_octet
[1],
3406 mac
.ether_addr_octet
[2],
3407 mac
.ether_addr_octet
[3],
3408 mac
.ether_addr_octet
[4],
3409 mac
.ether_addr_octet
[5]);
3412 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3414 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3417 if (link
->dhcp_client
) {
3418 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3419 (const uint8_t *) &link
->mac
,
3423 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3425 r
= dhcp4_set_client_identifier(link
);
3430 if (link
->dhcp6_client
) {
3431 const DUID
* duid
= link_get_duid(link
);
3433 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3434 (const uint8_t *) &link
->mac
,
3438 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3440 if (link
->network
->iaid_set
) {
3441 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3442 link
->network
->iaid
);
3444 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3447 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3449 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3450 duid
->raw_data_len
);
3452 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3456 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3458 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3462 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3464 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3469 had_carrier
= link_has_carrier(link
);
3471 r
= link_update_flags(link
, m
);
3475 r
= link_update_lldp(link
);
3479 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3480 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3482 if (carrier_gained
) {
3483 log_link_info(link
, "Gained carrier");
3485 r
= link_carrier_gained(link
);
3488 } else if (carrier_lost
) {
3489 log_link_info(link
, "Lost carrier");
3491 r
= link_carrier_lost(link
);
3499 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3507 if (hashmap_isempty(h
))
3511 HASHMAP_FOREACH(link
, h
, i
) {
3515 fprintf(f
, "%i", link
->ifindex
);
3522 int link_save(Link
*link
) {
3523 _cleanup_free_
char *temp_path
= NULL
;
3524 _cleanup_fclose_
FILE *f
= NULL
;
3525 const char *admin_state
, *oper_state
;
3532 assert(link
->state_file
);
3533 assert(link
->lease_file
);
3534 assert(link
->manager
);
3536 if (link
->state
== LINK_STATE_LINGER
) {
3537 (void) unlink(link
->state_file
);
3541 link_lldp_save(link
);
3543 admin_state
= link_state_to_string(link
->state
);
3544 assert(admin_state
);
3546 oper_state
= link_operstate_to_string(link
->operstate
);
3549 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3553 (void) fchmod(fileno(f
), 0644);
3556 "# This is private data. Do not parse.\n"
3559 admin_state
, oper_state
);
3561 if (link
->network
) {
3562 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3563 const char *dhcp_domainname
= NULL
, *p
;
3564 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3568 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3569 yes_no(link
->network
->required_for_online
));
3571 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3572 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3574 if (link
->dhcp6_client
) {
3575 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3576 if (r
< 0 && r
!= -ENOMSG
)
3577 log_link_debug(link
, "No DHCPv6 lease");
3580 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3585 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3586 _cleanup_free_
char *b
= NULL
;
3588 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3589 &link
->network
->dns
[j
].address
, &b
);
3591 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3601 if (link
->network
->dhcp_use_dns
&&
3603 const struct in_addr
*addresses
;
3605 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3607 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3611 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3612 struct in6_addr
*in6_addrs
;
3614 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3618 serialize_in6_addrs(f
, in6_addrs
, r
);
3623 /* Make sure to flush out old entries before we use the NDISC data */
3626 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3629 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3633 serialize_in6_addrs(f
, &dd
->address
, 1);
3642 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3644 if (link
->network
->dhcp_use_ntp
&&
3646 const struct in_addr
*addresses
;
3648 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3650 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3654 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3655 struct in6_addr
*in6_addrs
;
3658 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3663 serialize_in6_addrs(f
, in6_addrs
, r
);
3667 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3669 fputstrv(f
, hosts
, NULL
, &space
);
3674 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3675 if (link
->dhcp_lease
) {
3676 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3677 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3680 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3683 fputs("DOMAINS=", f
);
3685 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3686 fputs_with_space(f
, p
, NULL
, &space
);
3688 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3691 if (dhcp_domainname
)
3692 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3694 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3696 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3698 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3699 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3704 fputs("ROUTE_DOMAINS=", f
);
3706 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3707 fputs_with_space(f
, p
, NULL
, &space
);
3709 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3712 if (dhcp_domainname
)
3713 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3715 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3717 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3719 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3720 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3725 fprintf(f
, "LLMNR=%s\n",
3726 resolve_support_to_string(link
->network
->llmnr
));
3727 fprintf(f
, "MDNS=%s\n",
3728 resolve_support_to_string(link
->network
->mdns
));
3729 if (link
->network
->dns_default_route
>= 0)
3730 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3732 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3733 fprintf(f
, "DNS_OVER_TLS=%s\n",
3734 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3736 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3737 fprintf(f
, "DNSSEC=%s\n",
3738 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3740 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3743 fputs("DNSSEC_NTA=", f
);
3745 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3746 fputs_with_space(f
, n
, NULL
, &space
);
3750 fputs("ADDRESSES=", f
);
3752 SET_FOREACH(a
, link
->addresses
, i
) {
3753 _cleanup_free_
char *address_str
= NULL
;
3755 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3759 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3764 fputs("ROUTES=", f
);
3766 SET_FOREACH(route
, link
->routes
, i
) {
3767 _cleanup_free_
char *route_str
= NULL
;
3769 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3773 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3774 space
? " " : "", route_str
,
3775 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3782 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3783 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3785 if (link
->dhcp_lease
) {
3786 struct in_addr address
;
3787 const char *tz
= NULL
;
3789 assert(link
->network
);
3791 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3793 fprintf(f
, "TIMEZONE=%s\n", tz
);
3795 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3797 fputs("DHCP4_ADDRESS=", f
);
3798 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3802 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3810 (void) unlink(link
->lease_file
);
3813 struct in_addr address
;
3815 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3817 fputs("IPV4LL_ADDRESS=", f
);
3818 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3823 r
= fflush_and_check(f
);
3827 if (rename(temp_path
, link
->state_file
) < 0) {
3835 (void) unlink(link
->state_file
);
3837 (void) unlink(temp_path
);
3839 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3842 /* The serialized state in /run is no longer up-to-date. */
3843 void link_dirty(Link
*link
) {
3848 /* mark manager dirty as link is dirty */
3849 manager_dirty(link
->manager
);
3851 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3853 /* allocation errors are ignored */
3856 r
= set_put(link
->manager
->dirty_links
, link
);
3858 /* don't take another ref if the link was already dirty */
3864 /* The serialized state in /run is up-to-date */
3865 void link_clean(Link
*link
) {
3867 assert(link
->manager
);
3869 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3872 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3873 [LINK_STATE_PENDING
] = "pending",
3874 [LINK_STATE_INITIALIZED
] = "initialized",
3875 [LINK_STATE_CONFIGURING
] = "configuring",
3876 [LINK_STATE_CONFIGURED
] = "configured",
3877 [LINK_STATE_UNMANAGED
] = "unmanaged",
3878 [LINK_STATE_FAILED
] = "failed",
3879 [LINK_STATE_LINGER
] = "linger",
3882 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);