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. */
343 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
346 if (!link
->enslaved_raw
)
349 /* TODO: add conditions for other netdevs. */
354 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
360 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
363 link_update_operstate(master
, true);
366 void link_update_operstate(Link
*link
, bool also_update_master
) {
367 LinkOperationalState operstate
;
372 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
373 operstate
= LINK_OPERSTATE_DORMANT
;
374 else if (link_has_carrier(link
)) {
376 uint8_t scope
= RT_SCOPE_NOWHERE
;
378 /* if we have carrier, check what addresses we have */
379 SET_FOREACH(address
, link
->addresses
, i
) {
380 if (!address_is_ready(address
))
383 if (address
->scope
< scope
)
384 scope
= address
->scope
;
387 /* for operstate we also take foreign addresses into account */
388 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
389 if (!address_is_ready(address
))
392 if (address
->scope
< scope
)
393 scope
= address
->scope
;
396 if (scope
< RT_SCOPE_SITE
)
397 /* universally accessible addresses found */
398 operstate
= LINK_OPERSTATE_ROUTABLE
;
399 else if (scope
< RT_SCOPE_HOST
)
400 /* only link or site local addresses found */
401 operstate
= LINK_OPERSTATE_DEGRADED
;
403 /* no useful addresses found */
404 operstate
= LINK_OPERSTATE_CARRIER
;
405 } else if (link
->flags
& IFF_UP
)
406 operstate
= LINK_OPERSTATE_NO_CARRIER
;
408 operstate
= LINK_OPERSTATE_OFF
;
410 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
411 link_is_enslaved(link
))
412 operstate
= LINK_OPERSTATE_ENSLAVED
;
414 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
417 SET_FOREACH(slave
, link
->slaves
, i
) {
418 link_update_operstate(slave
, false);
420 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
421 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
425 if (link
->operstate
!= operstate
) {
426 link
->operstate
= operstate
;
427 link_send_changed(link
, "OperationalState", NULL
);
431 if (also_update_master
&& link
->network
) {
432 link_update_master_operstate(link
, link
->network
->bond
);
433 link_update_master_operstate(link
, link
->network
->bridge
);
437 #define FLAG_STRING(string, flag, old, new) \
438 (((old ^ new) & flag) \
439 ? ((old & flag) ? (" -" string) : (" +" string)) \
442 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
443 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
449 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
451 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
453 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
455 /* if we got a message without operstate, take it to mean
456 the state was unchanged */
457 operstate
= link
->kernel_operstate
;
459 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
462 if (link
->flags
!= flags
) {
463 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",
464 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
465 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
466 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
467 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
468 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
469 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
470 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
471 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
472 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
473 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
474 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
475 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
476 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
477 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
478 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
479 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
480 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
481 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
482 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
484 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
485 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
486 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
487 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
488 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
489 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
490 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
491 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
493 /* link flags are currently at most 18 bits, let's align to
495 if (unknown_flags_added
)
497 "Unknown link flags gained: %#.5x (ignoring)",
498 unknown_flags_added
);
500 if (unknown_flags_removed
)
502 "Unknown link flags lost: %#.5x (ignoring)",
503 unknown_flags_removed
);
507 link
->kernel_operstate
= operstate
;
509 link_update_operstate(link
, true);
514 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
515 _cleanup_(link_unrefp
) Link
*link
= NULL
;
517 const char *ifname
, *kind
= NULL
;
519 unsigned short iftype
;
525 /* check for link kind */
526 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
528 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
529 r
= sd_netlink_message_exit_container(message
);
534 r
= sd_netlink_message_get_type(message
, &type
);
537 else if (type
!= RTM_NEWLINK
)
540 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
543 else if (ifindex
<= 0)
546 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
550 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
561 .state
= LINK_STATE_PENDING
,
562 .rtnl_extended_attrs
= true,
567 link
->ifname
= strdup(ifname
);
572 link
->kind
= strdup(kind
);
577 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
579 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
581 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
583 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
585 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
588 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
591 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
594 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
598 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
602 r
= link_update_flags(link
, message
, false);
606 *ret
= TAKE_PTR(link
);
611 static Link
*link_free(Link
*link
) {
616 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
617 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
619 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
620 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
622 while ((address
= link
->pool_addresses
)) {
623 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
624 address_free(address
);
627 sd_dhcp_server_unref(link
->dhcp_server
);
628 sd_dhcp_client_unref(link
->dhcp_client
);
629 sd_dhcp_lease_unref(link
->dhcp_lease
);
631 link_lldp_emit_stop(link
);
633 free(link
->lease_file
);
635 sd_lldp_unref(link
->lldp
);
636 free(link
->lldp_file
);
640 sd_ipv4ll_unref(link
->ipv4ll
);
641 sd_dhcp6_client_unref(link
->dhcp6_client
);
642 sd_ndisc_unref(link
->ndisc
);
643 sd_radv_unref(link
->radv
);
648 (void) unlink(link
->state_file
);
649 free(link
->state_file
);
651 sd_device_unref(link
->sd_device
);
653 hashmap_free(link
->bound_to_links
);
654 hashmap_free(link
->bound_by_links
);
656 set_free_with_destructor(link
->slaves
, link_unref
);
658 network_unref(link
->network
);
663 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
665 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
672 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
681 void link_set_state(Link
*link
, LinkState state
) {
684 if (link
->state
== state
)
687 log_link_debug(link
, "State changed: %s -> %s",
688 link_state_to_string(link
->state
),
689 link_state_to_string(state
));
693 link_send_changed(link
, "AdministrativeState", NULL
);
696 static void link_enter_unmanaged(Link
*link
) {
699 link_set_state(link
, LINK_STATE_UNMANAGED
);
704 int link_stop_clients(Link
*link
) {
708 assert(link
->manager
);
709 assert(link
->manager
->event
);
711 if (link
->dhcp_client
) {
712 k
= sd_dhcp_client_stop(link
->dhcp_client
);
714 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
718 k
= sd_ipv4ll_stop(link
->ipv4ll
);
720 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
723 if (link
->dhcp6_client
) {
724 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
726 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
730 k
= sd_ndisc_stop(link
->ndisc
);
732 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
736 k
= sd_radv_stop(link
->radv
);
738 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
741 link_lldp_emit_stop(link
);
745 void link_enter_failed(Link
*link
) {
748 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
751 log_link_warning(link
, "Failed");
753 link_set_state(link
, LINK_STATE_FAILED
);
755 link_stop_clients(link
);
760 static Address
* link_find_dhcp_server_address(Link
*link
) {
764 assert(link
->network
);
766 /* The first statically configured address if there is any */
767 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
769 if (address
->family
!= AF_INET
)
772 if (in_addr_is_null(address
->family
, &address
->in_addr
))
778 /* If that didn't work, find a suitable address we got from the pool */
779 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
780 if (address
->family
!= AF_INET
)
789 static int link_join_netdevs_after_configured(Link
*link
) {
794 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
795 if (netdev
->ifindex
> 0)
796 /* Assume already enslaved. */
799 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
802 log_struct(LOG_DEBUG
,
803 LOG_LINK_INTERFACE(link
),
804 LOG_NETDEV_INTERFACE(netdev
),
805 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
807 r
= netdev_join(netdev
, link
, NULL
);
809 return log_struct_errno(LOG_WARNING
, r
,
810 LOG_LINK_INTERFACE(link
),
811 LOG_NETDEV_INTERFACE(netdev
),
812 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
818 static void link_enter_configured(Link
*link
) {
820 assert(link
->network
);
822 if (link
->state
!= LINK_STATE_CONFIGURING
)
825 log_link_info(link
, "Configured");
827 link_set_state(link
, LINK_STATE_CONFIGURED
);
829 (void) link_join_netdevs_after_configured(link
);
834 static int link_request_set_routing_policy_rule(Link
*link
) {
835 RoutingPolicyRule
*rule
, *rrule
= NULL
;
839 assert(link
->network
);
841 link_set_state(link
, LINK_STATE_CONFIGURING
);
842 link
->routing_policy_rules_configured
= false;
844 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
845 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
846 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
847 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
849 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
853 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
855 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
856 link_enter_failed(link
);
860 link
->routing_policy_rule_messages
++;
863 routing_policy_rule_purge(link
->manager
, link
);
864 if (link
->routing_policy_rule_messages
== 0) {
865 link
->routing_policy_rules_configured
= true;
866 link_check_ready(link
);
868 log_link_debug(link
, "Setting routing policy rules");
873 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
877 assert(link
->route_messages
> 0);
878 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
879 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
881 link
->route_messages
--;
883 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
886 r
= sd_netlink_message_get_errno(m
);
887 if (r
< 0 && r
!= -EEXIST
)
888 log_link_warning_errno(link
, r
, "Could not set route: %m");
890 if (link
->route_messages
== 0) {
891 log_link_debug(link
, "Routes set");
892 link
->static_routes_configured
= true;
893 link_check_ready(link
);
899 int link_request_set_routes(Link
*link
) {
901 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
902 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
909 assert(link
->network
);
910 assert(link
->addresses_configured
);
911 assert(link
->address_messages
== 0);
912 assert(link
->state
!= _LINK_STATE_INVALID
);
914 link_set_state(link
, LINK_STATE_CONFIGURING
);
915 link
->static_routes_configured
= false;
917 r
= link_request_set_routing_policy_rule(link
);
921 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
922 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
923 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
925 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
928 r
= route_configure(rt
, link
, route_handler
);
930 log_link_warning_errno(link
, r
, "Could not set routes: %m");
931 link_enter_failed(link
);
935 link
->route_messages
++;
938 if (link
->route_messages
== 0) {
939 link
->static_routes_configured
= true;
940 link_check_ready(link
);
942 log_link_debug(link
, "Setting routes");
947 void link_check_ready(Link
*link
) {
953 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
959 if (!link
->addresses_configured
)
962 if (!link
->neighbors_configured
)
965 SET_FOREACH(a
, link
->addresses
, i
)
966 if (!address_is_ready(a
))
969 if (!link
->addresses_ready
) {
970 link
->addresses_ready
= true;
971 link_request_set_routes(link
);
975 if (!link
->static_routes_configured
)
978 if (!link
->routing_policy_rules_configured
)
981 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
984 if (link_ipv6ll_enabled(link
) &&
985 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
988 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
989 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
990 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
991 /* When DHCP is enabled, at least one protocol must provide an address, or
992 * an IPv4ll fallback address must be configured. */
995 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
998 if (link
->state
!= LINK_STATE_CONFIGURED
)
999 link_enter_configured(link
);
1004 static int link_request_set_neighbors(Link
*link
) {
1009 assert(link
->network
);
1010 assert(link
->state
!= _LINK_STATE_INVALID
);
1012 link_set_state(link
, LINK_STATE_CONFIGURING
);
1013 link
->neighbors_configured
= false;
1015 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1016 r
= neighbor_configure(neighbor
, link
, NULL
);
1018 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1019 link_enter_failed(link
);
1024 if (link
->neighbor_messages
== 0) {
1025 link
->neighbors_configured
= true;
1026 link_check_ready(link
);
1028 log_link_debug(link
, "Setting neighbors");
1033 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1039 assert(link
->ifname
);
1040 assert(link
->address_messages
> 0);
1041 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1042 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1044 link
->address_messages
--;
1046 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1049 r
= sd_netlink_message_get_errno(m
);
1050 if (r
< 0 && r
!= -EEXIST
)
1051 log_link_warning_errno(link
, r
, "could not set address: %m");
1053 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1055 if (link
->address_messages
== 0) {
1056 log_link_debug(link
, "Addresses set");
1057 link
->addresses_configured
= true;
1058 link_check_ready(link
);
1064 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1065 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1066 size_t n_addresses
= 0, n_allocated
= 0;
1069 log_debug("Copying DNS server information from %s", link
->ifname
);
1074 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1077 /* Only look for IPv4 addresses */
1078 if (link
->network
->dns
[i
].family
!= AF_INET
)
1081 ia
= link
->network
->dns
[i
].address
.in
;
1083 /* Never propagate obviously borked data */
1084 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1087 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1090 addresses
[n_addresses
++] = ia
;
1093 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1094 const struct in_addr
*da
= NULL
;
1097 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1100 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1103 for (j
= 0; j
< n
; j
++)
1104 if (in4_addr_is_non_local(&da
[j
]))
1105 addresses
[n_addresses
++] = da
[j
];
1109 if (n_addresses
<= 0)
1112 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1115 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1116 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1117 size_t n_addresses
= 0, n_allocated
= 0;
1123 log_debug("Copying NTP server information from %s", link
->ifname
);
1125 STRV_FOREACH(a
, link
->network
->ntp
) {
1126 union in_addr_union ia
;
1128 /* Only look for IPv4 addresses */
1129 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1132 /* Never propagate obviously borked data */
1133 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1136 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1139 addresses
[n_addresses
++] = ia
.in
;
1142 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1143 const struct in_addr
*da
= NULL
;
1146 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1149 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1152 for (j
= 0; j
< n
; j
++)
1153 if (in4_addr_is_non_local(&da
[j
]))
1154 addresses
[n_addresses
++] = da
[j
];
1158 if (n_addresses
<= 0)
1161 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1164 static int link_set_bridge_fdb(Link
*link
) {
1165 FdbEntry
*fdb_entry
;
1168 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1169 r
= fdb_entry_configure(link
, fdb_entry
);
1171 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1177 static int link_request_set_addresses(Link
*link
) {
1178 AddressLabel
*label
;
1183 assert(link
->network
);
1184 assert(link
->state
!= _LINK_STATE_INVALID
);
1186 link_set_state(link
, LINK_STATE_CONFIGURING
);
1188 /* Reset all *_configured flags we are configuring. */
1189 link
->addresses_configured
= false;
1190 link
->addresses_ready
= false;
1191 link
->neighbors_configured
= false;
1192 link
->static_routes_configured
= false;
1193 link
->routing_policy_rules_configured
= false;
1195 r
= link_set_bridge_fdb(link
);
1199 r
= link_request_set_neighbors(link
);
1203 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1206 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1208 r
= address_configure(ad
, link
, address_handler
, update
);
1210 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1211 link_enter_failed(link
);
1215 link
->address_messages
++;
1218 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1219 r
= address_label_configure(label
, link
, NULL
, false);
1221 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1222 link_enter_failed(link
);
1226 link
->address_label_messages
++;
1229 /* now that we can figure out a default address for the dhcp server,
1231 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1233 Link
*uplink
= NULL
;
1234 bool acquired_uplink
= false;
1236 address
= link_find_dhcp_server_address(link
);
1238 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1239 link_enter_failed(link
);
1243 /* use the server address' subnet as the pool */
1244 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1245 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1250 r = sd_dhcp_server_set_router(link->dhcp_server,
1251 &main_address->in_addr.in);
1256 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1257 r
= sd_dhcp_server_set_max_lease_time(
1259 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1264 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1265 r
= sd_dhcp_server_set_default_lease_time(
1267 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1272 if (link
->network
->dhcp_server_emit_dns
) {
1274 if (link
->network
->n_dhcp_server_dns
> 0)
1275 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1277 uplink
= manager_find_uplink(link
->manager
, link
);
1278 acquired_uplink
= true;
1281 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1284 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1287 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1290 if (link
->network
->dhcp_server_emit_ntp
) {
1292 if (link
->network
->n_dhcp_server_ntp
> 0)
1293 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1295 if (!acquired_uplink
)
1296 uplink
= manager_find_uplink(link
->manager
, link
);
1299 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1302 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1306 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1309 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1311 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1313 if (link
->network
->dhcp_server_emit_timezone
) {
1314 _cleanup_free_
char *buffer
= NULL
;
1315 const char *tz
= NULL
;
1317 if (link
->network
->dhcp_server_timezone
)
1318 tz
= link
->network
->dhcp_server_timezone
;
1320 r
= get_timezone(&buffer
);
1322 log_warning_errno(r
, "Failed to determine timezone: %m");
1328 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1333 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1334 r
= sd_dhcp_server_start(link
->dhcp_server
);
1336 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1338 link_enter_failed(link
);
1344 log_link_debug(link
, "Offering DHCPv4 leases");
1347 if (link
->address_messages
== 0) {
1348 link
->addresses_configured
= true;
1349 link_check_ready(link
);
1351 log_link_debug(link
, "Setting addresses");
1356 static int link_set_bridge_vlan(Link
*link
) {
1359 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1361 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1366 static int link_set_proxy_arp(Link
*link
) {
1369 if (!link_proxy_arp_enabled(link
))
1372 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1374 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1379 static int link_configure_after_setting_mtu(Link
*link
);
1381 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1386 assert(link
->ifname
);
1388 link
->setting_mtu
= false;
1390 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1393 r
= sd_netlink_message_get_errno(m
);
1395 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1399 log_link_debug(link
, "Setting MTU done.");
1401 if (link
->state
== LINK_STATE_INITIALIZED
)
1402 (void) link_configure_after_setting_mtu(link
);
1407 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1408 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1412 assert(link
->manager
);
1413 assert(link
->manager
->rtnl
);
1415 if (mtu
== 0 || link
->setting_mtu
)
1418 if (link
->mtu
== mtu
)
1421 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1423 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1425 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1427 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1428 * for this interface, then disable IPv6 else enable it. */
1429 (void) link_enable_ipv6(link
);
1431 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1432 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1433 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1435 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1436 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1441 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1443 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1445 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1446 link_netlink_destroy_callback
, link
);
1448 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1451 link
->setting_mtu
= true;
1456 static bool link_reduces_vlan_mtu(Link
*link
) {
1457 /* See netif_reduces_vlan_mtu() in kernel. */
1458 return streq_ptr(link
->kind
, "macsec");
1461 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1466 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1467 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1468 /* See vlan_dev_change_mtu() in kernel. */
1469 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1471 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1472 /* See macvlan_change_mtu() in kernel. */
1478 static int link_configure_mtu(Link
*link
) {
1482 assert(link
->network
);
1484 if (link
->network
->mtu
> 0)
1485 return link_set_mtu(link
, link
->network
->mtu
);
1487 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1488 if (link
->mtu
>= mtu
)
1491 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1492 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1495 return link_set_mtu(link
, mtu
);
1498 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1503 assert(link
->ifname
);
1505 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1508 r
= sd_netlink_message_get_errno(m
);
1510 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1515 static int link_set_flags(Link
*link
) {
1516 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1517 unsigned ifi_change
= 0;
1518 unsigned ifi_flags
= 0;
1522 assert(link
->manager
);
1523 assert(link
->manager
->rtnl
);
1525 if (link
->flags
& IFF_LOOPBACK
)
1531 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1534 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1536 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1538 if (link
->network
->arp
>= 0) {
1539 ifi_change
|= IFF_NOARP
;
1540 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1543 if (link
->network
->multicast
>= 0) {
1544 ifi_change
|= IFF_MULTICAST
;
1545 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1548 if (link
->network
->allmulticast
>= 0) {
1549 ifi_change
|= IFF_ALLMULTI
;
1550 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1553 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1555 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1557 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1558 link_netlink_destroy_callback
, link
);
1560 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1567 static int link_acquire_ipv6_conf(Link
*link
) {
1572 if (link_ipv6_accept_ra_enabled(link
)) {
1573 assert(link
->ndisc
);
1575 log_link_debug(link
, "Discovering IPv6 routers");
1577 r
= sd_ndisc_start(link
->ndisc
);
1578 if (r
< 0 && r
!= -EBUSY
)
1579 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1582 if (link_radv_enabled(link
)) {
1584 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1586 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1588 r
= sd_radv_start(link
->radv
);
1589 if (r
< 0 && r
!= -EBUSY
)
1590 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1593 (void) dhcp6_request_prefix_delegation(link
);
1598 static int link_acquire_ipv4_conf(Link
*link
) {
1602 assert(link
->manager
);
1603 assert(link
->manager
->event
);
1605 if (link_ipv4ll_enabled(link
)) {
1606 assert(link
->ipv4ll
);
1608 log_link_debug(link
, "Acquiring IPv4 link-local address");
1610 r
= sd_ipv4ll_start(link
->ipv4ll
);
1612 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1615 if (link_dhcp4_enabled(link
)) {
1616 assert(link
->dhcp_client
);
1618 log_link_debug(link
, "Acquiring DHCPv4 lease");
1620 r
= sd_dhcp_client_start(link
->dhcp_client
);
1622 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1628 static int link_acquire_conf(Link
*link
) {
1633 r
= link_acquire_ipv4_conf(link
);
1637 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1638 r
= link_acquire_ipv6_conf(link
);
1643 if (link_lldp_emit_enabled(link
)) {
1644 r
= link_lldp_emit_start(link
);
1646 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1652 bool link_has_carrier(Link
*link
) {
1653 /* see Documentation/networking/operstates.txt in the kernel sources */
1655 if (link
->kernel_operstate
== IF_OPER_UP
)
1658 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1659 /* operstate may not be implemented, so fall back to flags */
1660 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1661 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1667 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1672 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1675 r
= sd_netlink_message_get_errno(m
);
1677 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1682 static int link_configure_addrgen_mode(Link
*link
) {
1683 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1684 uint8_t ipv6ll_mode
;
1688 assert(link
->network
);
1689 assert(link
->manager
);
1690 assert(link
->manager
->rtnl
);
1692 if (!socket_ipv6_is_supported())
1695 log_link_debug(link
, "Setting address genmode for link");
1697 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1699 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1701 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1703 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1705 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1707 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1709 if (!link_ipv6ll_enabled(link
))
1710 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1711 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1712 /* The file may not exist. And event if it exists, when stable_secret is unset,
1713 * reading the file fails with EIO. */
1714 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1716 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1718 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1720 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1722 r
= sd_netlink_message_close_container(req
);
1724 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1726 r
= sd_netlink_message_close_container(req
);
1728 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1730 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1731 link_netlink_destroy_callback
, link
);
1733 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1740 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1745 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1748 r
= sd_netlink_message_get_errno(m
);
1750 /* we warn but don't fail the link, as it may be brought up later */
1751 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1756 static int link_up(Link
*link
) {
1757 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1761 assert(link
->network
);
1762 assert(link
->manager
);
1763 assert(link
->manager
->rtnl
);
1765 log_link_debug(link
, "Bringing link up");
1767 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1769 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1771 /* set it free if not enslaved with networkd */
1772 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1773 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1775 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1778 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1780 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1782 if (link
->network
->mac
) {
1783 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1785 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1788 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1789 link_netlink_destroy_callback
, link
);
1791 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1798 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1803 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1806 r
= sd_netlink_message_get_errno(m
);
1808 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1813 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1814 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1818 assert(link
->manager
);
1819 assert(link
->manager
->rtnl
);
1821 log_link_debug(link
, "Bringing link down");
1823 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1824 RTM_SETLINK
, link
->ifindex
);
1826 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1828 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1830 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1832 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1833 callback
?: link_down_handler
,
1834 link_netlink_destroy_callback
, link
);
1836 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1843 static int link_handle_bound_to_list(Link
*link
) {
1847 bool required_up
= false;
1848 bool link_is_up
= false;
1852 if (hashmap_isempty(link
->bound_to_links
))
1855 if (link
->flags
& IFF_UP
)
1858 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1859 if (link_has_carrier(l
)) {
1864 if (!required_up
&& link_is_up
) {
1865 r
= link_down(link
, NULL
);
1868 } else if (required_up
&& !link_is_up
) {
1877 static int link_handle_bound_by_list(Link
*link
) {
1884 if (hashmap_isempty(link
->bound_by_links
))
1887 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1888 r
= link_handle_bound_to_list(l
);
1896 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1902 if (link
== carrier
)
1905 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1908 r
= hashmap_ensure_allocated(h
, NULL
);
1912 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1919 static int link_new_bound_by_list(Link
*link
) {
1924 bool list_updated
= false;
1927 assert(link
->manager
);
1931 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1932 if (!carrier
->network
)
1935 if (strv_isempty(carrier
->network
->bind_carrier
))
1938 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1939 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1943 list_updated
= true;
1950 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1951 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1955 link_dirty(carrier
);
1961 static int link_new_bound_to_list(Link
*link
) {
1966 bool list_updated
= false;
1969 assert(link
->manager
);
1974 if (strv_isempty(link
->network
->bind_carrier
))
1979 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1980 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1981 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1985 list_updated
= true;
1992 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1993 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1997 link_dirty(carrier
);
2003 static int link_new_carrier_maps(Link
*link
) {
2006 r
= link_new_bound_by_list(link
);
2010 r
= link_handle_bound_by_list(link
);
2014 r
= link_new_bound_to_list(link
);
2018 r
= link_handle_bound_to_list(link
);
2025 static void link_free_bound_to_list(Link
*link
) {
2029 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2030 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2032 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2033 link_dirty(bound_to
);
2039 static void link_free_bound_by_list(Link
*link
) {
2043 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2044 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2046 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2047 link_dirty(bound_by
);
2048 link_handle_bound_to_list(bound_by
);
2055 static void link_free_carrier_maps(Link
*link
) {
2056 bool list_updated
= false;
2060 if (!hashmap_isempty(link
->bound_to_links
)) {
2061 link_free_bound_to_list(link
);
2062 list_updated
= true;
2065 if (!hashmap_isempty(link
->bound_by_links
)) {
2066 link_free_bound_by_list(link
);
2067 list_updated
= true;
2076 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2083 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2087 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2091 r
= set_put(master
->slaves
, link
);
2099 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2104 if (!link
->manager
|| !netdev
)
2107 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2110 link_unref(set_remove(master
->slaves
, link
));
2113 static void link_detach_from_manager(Link
*link
) {
2114 if (!link
|| !link
->manager
)
2117 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2120 /* The following must be called at last. */
2121 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2125 void link_drop(Link
*link
) {
2126 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2129 link_set_state(link
, LINK_STATE_LINGER
);
2131 link_free_carrier_maps(link
);
2133 if (link
->network
) {
2134 link_drop_from_master(link
, link
->network
->bridge
);
2135 link_drop_from_master(link
, link
->network
->bond
);
2138 log_link_debug(link
, "Link removed");
2140 (void) unlink(link
->state_file
);
2141 link_detach_from_manager(link
);
2144 static int link_joined(Link
*link
) {
2148 assert(link
->network
);
2150 if (!hashmap_isempty(link
->bound_to_links
)) {
2151 r
= link_handle_bound_to_list(link
);
2154 } else if (!(link
->flags
& IFF_UP
)) {
2157 link_enter_failed(link
);
2162 if (link
->network
->bridge
) {
2163 r
= link_set_bridge(link
);
2165 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2167 r
= link_append_to_master(link
, link
->network
->bridge
);
2169 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2172 if (link
->network
->bond
) {
2173 r
= link_set_bond(link
);
2175 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2177 r
= link_append_to_master(link
, link
->network
->bond
);
2179 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2182 if (link
->network
->use_br_vlan
&&
2183 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2184 r
= link_set_bridge_vlan(link
);
2186 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2189 /* Skip setting up addresses until it gets carrier,
2190 or it would try to set addresses twice,
2191 which is bad for non-idempotent steps. */
2192 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2195 return link_request_set_addresses(link
);
2198 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2202 assert(link
->network
);
2203 assert(link
->enslaving
> 0);
2204 assert(!link
->enslaved_raw
);
2208 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2211 r
= sd_netlink_message_get_errno(m
);
2212 if (r
< 0 && r
!= -EEXIST
) {
2213 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2214 link_enter_failed(link
);
2217 log_link_debug(link
, "Joined netdev");
2219 if (link
->enslaving
== 0) {
2220 link
->enslaved_raw
= true;
2227 static int link_enter_join_netdev(Link
*link
) {
2233 assert(link
->network
);
2234 assert(link
->state
== LINK_STATE_INITIALIZED
);
2236 link_set_state(link
, LINK_STATE_CONFIGURING
);
2239 link
->enslaving
= 0;
2240 link
->enslaved_raw
= false;
2242 if (link
->network
->bond
) {
2243 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2244 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2245 return link_joined(link
);
2247 log_struct(LOG_DEBUG
,
2248 LOG_LINK_INTERFACE(link
),
2249 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2250 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2254 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2256 log_struct_errno(LOG_WARNING
, r
,
2257 LOG_LINK_INTERFACE(link
),
2258 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2259 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2260 link_enter_failed(link
);
2265 if (link
->network
->bridge
) {
2266 log_struct(LOG_DEBUG
,
2267 LOG_LINK_INTERFACE(link
),
2268 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2269 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2273 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2275 log_struct_errno(LOG_WARNING
, r
,
2276 LOG_LINK_INTERFACE(link
),
2277 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2278 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2279 link_enter_failed(link
);
2284 if (link
->network
->vrf
) {
2285 log_struct(LOG_DEBUG
,
2286 LOG_LINK_INTERFACE(link
),
2287 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2288 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2292 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2294 log_struct_errno(LOG_WARNING
, r
,
2295 LOG_LINK_INTERFACE(link
),
2296 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2297 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2298 link_enter_failed(link
);
2303 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2305 if (netdev
->ifindex
> 0)
2306 /* Assume already enslaved. */
2309 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2312 log_struct(LOG_DEBUG
,
2313 LOG_LINK_INTERFACE(link
),
2314 LOG_NETDEV_INTERFACE(netdev
),
2315 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2319 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2321 log_struct_errno(LOG_WARNING
, r
,
2322 LOG_LINK_INTERFACE(link
),
2323 LOG_NETDEV_INTERFACE(netdev
),
2324 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2325 link_enter_failed(link
);
2330 if (link
->enslaving
== 0)
2331 return link_joined(link
);
2336 static int link_set_ipv4_forward(Link
*link
) {
2339 if (!link_ipv4_forward_enabled(link
))
2342 /* We propagate the forwarding flag from one interface to the
2343 * global setting one way. This means: as long as at least one
2344 * interface was configured at any time that had IP forwarding
2345 * enabled the setting will stay on for good. We do this
2346 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2347 * somewhat in sync (see below). */
2349 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2351 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2356 static int link_set_ipv6_forward(Link
*link
) {
2359 if (!link_ipv6_forward_enabled(link
))
2362 /* On Linux, the IPv6 stack does not know a per-interface
2363 * packet forwarding setting: either packet forwarding is on
2364 * for all, or off for all. We hence don't bother with a
2365 * per-interface setting, but simply propagate the interface
2366 * flag, if it is set, to the global flag, one-way. Note that
2367 * while IPv4 would allow a per-interface flag, we expose the
2368 * same behaviour there and also propagate the setting from
2369 * one to all, to keep things simple (see above). */
2371 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2373 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2378 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2379 IPv6PrivacyExtensions s
;
2382 s
= link_ipv6_privacy_extensions(link
);
2386 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2388 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2393 static int link_set_ipv6_accept_ra(Link
*link
) {
2396 /* Make this a NOP if IPv6 is not available */
2397 if (!socket_ipv6_is_supported())
2400 if (link
->flags
& IFF_LOOPBACK
)
2406 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2408 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2413 static int link_set_ipv6_dad_transmits(Link
*link
) {
2416 /* Make this a NOP if IPv6 is not available */
2417 if (!socket_ipv6_is_supported())
2420 if (link
->flags
& IFF_LOOPBACK
)
2426 if (link
->network
->ipv6_dad_transmits
< 0)
2429 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2431 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2436 static int link_set_ipv6_hop_limit(Link
*link
) {
2439 /* Make this a NOP if IPv6 is not available */
2440 if (!socket_ipv6_is_supported())
2443 if (link
->flags
& IFF_LOOPBACK
)
2449 if (link
->network
->ipv6_hop_limit
< 0)
2452 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2454 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2459 static int link_set_ipv6_mtu(Link
*link
) {
2462 /* Make this a NOP if IPv6 is not available */
2463 if (!socket_ipv6_is_supported())
2466 if (link
->flags
& IFF_LOOPBACK
)
2469 if (link
->network
->ipv6_mtu
== 0)
2472 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2474 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2479 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2480 Address
*net_address
;
2488 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2489 if (address_equal(net_address
, address
))
2495 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2504 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2505 if (route_equal(net_route
, route
))
2511 static int link_drop_foreign_config(Link
*link
) {
2517 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2518 /* we consider IPv6LL addresses to be managed by the kernel */
2519 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2522 if (link_is_static_address_configured(link
, address
)) {
2523 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2525 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2527 r
= address_remove(address
, link
, NULL
);
2533 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2534 /* do not touch routes managed by the kernel */
2535 if (route
->protocol
== RTPROT_KERNEL
)
2538 if (link_is_static_route_configured(link
, route
)) {
2539 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2543 r
= route_remove(route
, link
, NULL
);
2552 static int link_drop_config(Link
*link
) {
2553 Address
*address
, *pool_address
;
2558 SET_FOREACH(address
, link
->addresses
, i
) {
2559 /* we consider IPv6LL addresses to be managed by the kernel */
2560 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2563 r
= address_remove(address
, link
, NULL
);
2567 /* If this address came from an address pool, clean up the pool */
2568 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2569 if (address_equal(address
, pool_address
)) {
2570 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2571 address_free(pool_address
);
2577 SET_FOREACH(route
, link
->routes
, i
) {
2578 /* do not touch routes managed by the kernel */
2579 if (route
->protocol
== RTPROT_KERNEL
)
2582 r
= route_remove(route
, link
, NULL
);
2592 static int link_configure(Link
*link
) {
2596 assert(link
->network
);
2597 assert(link
->state
== LINK_STATE_INITIALIZED
);
2599 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2600 return link_configure_can(link
);
2602 /* Drop foreign config, but ignore loopback or critical devices.
2603 * We do not want to remove loopback address or addresses used for root NFS. */
2604 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2605 r
= link_drop_foreign_config(link
);
2610 r
= link_set_proxy_arp(link
);
2614 r
= ipv6_proxy_ndp_addresses_configure(link
);
2618 r
= link_set_ipv4_forward(link
);
2622 r
= link_set_ipv6_forward(link
);
2626 r
= link_set_ipv6_privacy_extensions(link
);
2630 r
= link_set_ipv6_accept_ra(link
);
2634 r
= link_set_ipv6_dad_transmits(link
);
2638 r
= link_set_ipv6_hop_limit(link
);
2642 r
= link_set_flags(link
);
2646 r
= link_set_ipv6_mtu(link
);
2650 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2651 r
= ipv4ll_configure(link
);
2656 if (link_dhcp4_enabled(link
)) {
2657 r
= dhcp4_set_promote_secondaries(link
);
2661 r
= dhcp4_configure(link
);
2666 if (link_dhcp4_server_enabled(link
)) {
2667 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2671 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2676 if (link_dhcp6_enabled(link
) ||
2677 link_ipv6_accept_ra_enabled(link
)) {
2678 r
= dhcp6_configure(link
);
2683 if (link_ipv6_accept_ra_enabled(link
)) {
2684 r
= ndisc_configure(link
);
2689 if (link_radv_enabled(link
)) {
2690 r
= radv_configure(link
);
2695 if (link_lldp_rx_enabled(link
)) {
2696 r
= link_lldp_rx_configure(link
);
2701 r
= link_configure_mtu(link
);
2705 r
= link_configure_addrgen_mode(link
);
2709 return link_configure_after_setting_mtu(link
);
2712 static int link_configure_after_setting_mtu(Link
*link
) {
2716 assert(link
->network
);
2717 assert(link
->state
== LINK_STATE_INITIALIZED
);
2719 if (link
->setting_mtu
)
2722 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2723 r
= link_acquire_conf(link
);
2728 return link_enter_join_netdev(link
);
2731 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2734 if (duid
->raw_data_len
> 0)
2737 if (duid
->type
!= DUID_TYPE_UUID
)
2740 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2741 duid
->raw_data_len
= sizeof(sd_id128_t
);
2746 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2747 Manager
*manager
= userdata
;
2748 const sd_bus_error
*e
;
2758 e
= sd_bus_message_get_error(m
);
2760 log_error_errno(sd_bus_error_get_errno(e
),
2761 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2766 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2770 if (sz
!= sizeof(sd_id128_t
)) {
2771 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2775 memcpy(&manager
->product_uuid
, a
, sz
);
2776 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2777 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2779 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2782 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2783 r
= link_configure(link
);
2785 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2788 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2790 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2791 * even if the method fails. */
2792 manager
->has_product_uuid
= true;
2797 static bool link_requires_uuid(Link
*link
) {
2801 assert(link
->manager
);
2802 assert(link
->network
);
2804 duid
= link_get_duid(link
);
2805 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2808 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2811 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2817 static int link_configure_duid(Link
*link
) {
2823 assert(link
->manager
);
2824 assert(link
->network
);
2827 duid
= link_get_duid(link
);
2829 if (!link_requires_uuid(link
))
2832 if (m
->has_product_uuid
) {
2833 (void) duid_set_uuid(duid
, m
->product_uuid
);
2837 if (!m
->links_requesting_uuid
) {
2838 r
= manager_request_product_uuid(m
, link
);
2843 log_link_warning_errno(link
, r
,
2844 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2848 r
= set_put(m
->links_requesting_uuid
, link
);
2852 r
= set_put(m
->duids_requesting_uuid
, duid
);
2860 static int link_initialized_and_synced(Link
*link
) {
2865 assert(link
->ifname
);
2866 assert(link
->manager
);
2868 /* We may get called either from the asynchronous netlink callback,
2869 * or directly for link_add() if running in a container. See link_add(). */
2870 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2873 log_link_debug(link
, "Link state is up-to-date");
2874 link_set_state(link
, LINK_STATE_INITIALIZED
);
2876 r
= link_new_bound_by_list(link
);
2880 r
= link_handle_bound_by_list(link
);
2884 if (!link
->network
) {
2885 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2886 &link
->mac
, &network
);
2888 link_enter_unmanaged(link
);
2890 } else if (r
== 0 && network
->unmanaged
) {
2891 link_enter_unmanaged(link
);
2896 if (link
->flags
& IFF_LOOPBACK
) {
2897 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2898 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2900 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2901 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2903 if (network
->dhcp_server
)
2904 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2907 r
= network_apply(network
, link
);
2912 r
= link_new_bound_to_list(link
);
2916 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2917 * link_configure() is called later asynchronously. */
2918 r
= link_configure_duid(link
);
2922 r
= link_configure(link
);
2929 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2930 (void) link_initialized_and_synced(link
);
2934 int link_initialized(Link
*link
, sd_device
*device
) {
2935 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2939 assert(link
->manager
);
2940 assert(link
->manager
->rtnl
);
2943 if (link
->state
!= LINK_STATE_PENDING
)
2946 if (link
->sd_device
)
2949 log_link_debug(link
, "udev initialized link");
2950 link_set_state(link
, LINK_STATE_INITIALIZED
);
2952 link
->sd_device
= sd_device_ref(device
);
2954 /* udev has initialized the link, but we don't know if we have yet
2955 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2956 * when it returns we know that the pending NEWLINKs have already been
2957 * processed and that we are up-to-date */
2959 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2964 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2965 link_netlink_destroy_callback
, link
);
2974 static int link_load(Link
*link
) {
2975 _cleanup_free_
char *network_file
= NULL
,
2978 *dhcp4_address
= NULL
,
2979 *ipv4ll_address
= NULL
;
2980 union in_addr_union address
;
2981 union in_addr_union route_dst
;
2987 r
= parse_env_file(NULL
, link
->state_file
,
2988 "NETWORK_FILE", &network_file
,
2989 "ADDRESSES", &addresses
,
2991 "DHCP4_ADDRESS", &dhcp4_address
,
2992 "IPV4LL_ADDRESS", &ipv4ll_address
);
2993 if (r
< 0 && r
!= -ENOENT
)
2994 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3001 suffix
= strrchr(network_file
, '.');
3003 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3004 goto network_file_fail
;
3008 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3010 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3011 goto network_file_fail
;
3014 r
= network_apply(network
, link
);
3016 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3025 _cleanup_free_
char *address_str
= NULL
;
3026 char *prefixlen_str
;
3028 unsigned char prefixlen
;
3030 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3032 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3038 prefixlen_str
= strchr(address_str
, '/');
3039 if (!prefixlen_str
) {
3040 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3044 *prefixlen_str
++ = '\0';
3046 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3048 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3052 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3054 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3058 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3060 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3069 _cleanup_free_
char *route_str
= NULL
;
3070 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3072 char *prefixlen_str
;
3074 unsigned char prefixlen
, tos
, table
;
3077 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3079 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3085 prefixlen_str
= strchr(route_str
, '/');
3086 if (!prefixlen_str
) {
3087 log_link_debug(link
, "Failed to parse route %s", route_str
);
3091 *prefixlen_str
++ = '\0';
3093 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3095 log_link_debug(link
,
3096 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3101 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3103 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3107 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3109 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3111 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3112 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3113 0, route_expire_handler
, route
);
3115 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3118 route
->lifetime
= lifetime
;
3119 sd_event_source_unref(route
->expire
);
3120 route
->expire
= TAKE_PTR(expire
);
3124 if (dhcp4_address
) {
3125 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3127 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3128 goto dhcp4_address_fail
;
3131 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3133 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3135 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3137 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3142 if (ipv4ll_address
) {
3143 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3145 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3146 goto ipv4ll_address_fail
;
3149 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3151 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3153 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3155 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3158 ipv4ll_address_fail
:
3163 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3164 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3165 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3174 r
= link_new(m
, message
, ret
);
3180 log_link_debug(link
, "Link %d added", link
->ifindex
);
3182 r
= link_load(link
);
3186 if (detect_container() <= 0) {
3187 /* not in a container, udev will be around */
3188 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3189 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3191 log_link_warning_errno(link
, r
, "Could not find device: %m");
3195 r
= sd_device_get_is_initialized(device
);
3197 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3202 log_link_debug(link
, "link pending udev initialization...");
3206 r
= device_is_renaming(device
);
3208 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3212 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3216 r
= link_initialized(link
, device
);
3220 r
= link_initialized_and_synced(link
);
3227 link_enter_failed(link
);
3231 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3236 log_link_info(link
, "Gained IPv6LL");
3238 link
->ipv6ll_address
= *address
;
3239 link_check_ready(link
);
3241 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3242 r
= link_acquire_ipv6_conf(link
);
3244 link_enter_failed(link
);
3252 static int link_carrier_gained(Link
*link
) {
3257 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3258 r
= link_acquire_conf(link
);
3260 link_enter_failed(link
);
3264 r
= link_request_set_addresses(link
);
3269 r
= link_handle_bound_by_list(link
);
3276 static int link_carrier_lost(Link
*link
) {
3281 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3284 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3285 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3286 * configuration and stop the clients as well. */
3287 if (link
->setting_mtu
)
3290 r
= link_stop_clients(link
);
3292 link_enter_failed(link
);
3296 if (link_dhcp4_server_enabled(link
))
3297 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3299 r
= link_drop_config(link
);
3303 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3304 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3305 r
= link_drop_foreign_config(link
);
3310 r
= link_handle_bound_by_list(link
);
3317 int link_carrier_reset(Link
*link
) {
3322 if (link_has_carrier(link
)) {
3323 r
= link_carrier_lost(link
);
3327 r
= link_carrier_gained(link
);
3331 log_link_info(link
, "Reset carrier");
3337 int link_update(Link
*link
, sd_netlink_message
*m
) {
3338 struct ether_addr mac
;
3341 bool had_carrier
, carrier_gained
, carrier_lost
;
3345 assert(link
->ifname
);
3348 if (link
->state
== LINK_STATE_LINGER
) {
3349 log_link_info(link
, "Link re-added");
3350 link_set_state(link
, LINK_STATE_CONFIGURING
);
3352 r
= link_new_carrier_maps(link
);
3357 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3358 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3359 Manager
*manager
= link
->manager
;
3361 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3364 r
= link_add(manager
, m
, &link
);
3369 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3370 if (r
>= 0 && mtu
> 0) {
3372 if (link
->original_mtu
== 0) {
3373 link
->original_mtu
= mtu
;
3374 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3377 if (link
->dhcp_client
) {
3378 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3381 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3385 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3387 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3391 /* The kernel may broadcast NEWLINK messages without the MAC address
3392 set, simply ignore them. */
3393 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3395 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3398 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3401 log_link_debug(link
, "MAC address: "
3402 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3403 mac
.ether_addr_octet
[0],
3404 mac
.ether_addr_octet
[1],
3405 mac
.ether_addr_octet
[2],
3406 mac
.ether_addr_octet
[3],
3407 mac
.ether_addr_octet
[4],
3408 mac
.ether_addr_octet
[5]);
3411 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3413 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3416 if (link
->dhcp_client
) {
3417 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3418 (const uint8_t *) &link
->mac
,
3422 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3424 r
= dhcp4_set_client_identifier(link
);
3429 if (link
->dhcp6_client
) {
3430 const DUID
* duid
= link_get_duid(link
);
3432 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3433 (const uint8_t *) &link
->mac
,
3437 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3439 if (link
->network
->iaid_set
) {
3440 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3441 link
->network
->iaid
);
3443 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3446 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3448 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3449 duid
->raw_data_len
);
3451 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3455 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3457 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3461 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3463 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3468 old_master
= link
->master_ifindex
;
3469 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3471 had_carrier
= link_has_carrier(link
);
3473 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3477 r
= link_update_lldp(link
);
3481 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3482 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3484 if (carrier_gained
) {
3485 log_link_info(link
, "Gained carrier");
3487 r
= link_carrier_gained(link
);
3490 } else if (carrier_lost
) {
3491 log_link_info(link
, "Lost carrier");
3493 r
= link_carrier_lost(link
);
3501 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3509 if (hashmap_isempty(h
))
3513 HASHMAP_FOREACH(link
, h
, i
) {
3517 fprintf(f
, "%i", link
->ifindex
);
3524 int link_save(Link
*link
) {
3525 _cleanup_free_
char *temp_path
= NULL
;
3526 _cleanup_fclose_
FILE *f
= NULL
;
3527 const char *admin_state
, *oper_state
;
3534 assert(link
->state_file
);
3535 assert(link
->lease_file
);
3536 assert(link
->manager
);
3538 if (link
->state
== LINK_STATE_LINGER
) {
3539 (void) unlink(link
->state_file
);
3543 link_lldp_save(link
);
3545 admin_state
= link_state_to_string(link
->state
);
3546 assert(admin_state
);
3548 oper_state
= link_operstate_to_string(link
->operstate
);
3551 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3555 (void) fchmod(fileno(f
), 0644);
3558 "# This is private data. Do not parse.\n"
3561 admin_state
, oper_state
);
3563 if (link
->network
) {
3564 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3565 const char *dhcp_domainname
= NULL
, *p
;
3566 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3570 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3571 yes_no(link
->network
->required_for_online
));
3573 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3574 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3576 if (link
->dhcp6_client
) {
3577 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3578 if (r
< 0 && r
!= -ENOMSG
)
3579 log_link_debug(link
, "No DHCPv6 lease");
3582 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3587 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3588 _cleanup_free_
char *b
= NULL
;
3590 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3591 &link
->network
->dns
[j
].address
, &b
);
3593 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3603 if (link
->network
->dhcp_use_dns
&&
3605 const struct in_addr
*addresses
;
3607 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3609 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3613 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3614 struct in6_addr
*in6_addrs
;
3616 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3620 serialize_in6_addrs(f
, in6_addrs
, r
);
3625 /* Make sure to flush out old entries before we use the NDISC data */
3628 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3631 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3635 serialize_in6_addrs(f
, &dd
->address
, 1);
3644 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3646 if (link
->network
->dhcp_use_ntp
&&
3648 const struct in_addr
*addresses
;
3650 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3652 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3656 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3657 struct in6_addr
*in6_addrs
;
3660 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3665 serialize_in6_addrs(f
, in6_addrs
, r
);
3669 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3671 fputstrv(f
, hosts
, NULL
, &space
);
3676 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3677 if (link
->dhcp_lease
) {
3678 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3679 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3682 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3685 fputs("DOMAINS=", f
);
3687 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3688 fputs_with_space(f
, p
, NULL
, &space
);
3690 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3693 if (dhcp_domainname
)
3694 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3696 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3698 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3700 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3701 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3706 fputs("ROUTE_DOMAINS=", f
);
3708 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3709 fputs_with_space(f
, p
, NULL
, &space
);
3711 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3714 if (dhcp_domainname
)
3715 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3717 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3719 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3721 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3722 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3727 fprintf(f
, "LLMNR=%s\n",
3728 resolve_support_to_string(link
->network
->llmnr
));
3729 fprintf(f
, "MDNS=%s\n",
3730 resolve_support_to_string(link
->network
->mdns
));
3731 if (link
->network
->dns_default_route
>= 0)
3732 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3734 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3735 fprintf(f
, "DNS_OVER_TLS=%s\n",
3736 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3738 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3739 fprintf(f
, "DNSSEC=%s\n",
3740 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3742 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3745 fputs("DNSSEC_NTA=", f
);
3747 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3748 fputs_with_space(f
, n
, NULL
, &space
);
3752 fputs("ADDRESSES=", f
);
3754 SET_FOREACH(a
, link
->addresses
, i
) {
3755 _cleanup_free_
char *address_str
= NULL
;
3757 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3761 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3766 fputs("ROUTES=", f
);
3768 SET_FOREACH(route
, link
->routes
, i
) {
3769 _cleanup_free_
char *route_str
= NULL
;
3771 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3775 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3776 space
? " " : "", route_str
,
3777 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3784 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3785 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3787 if (link
->dhcp_lease
) {
3788 struct in_addr address
;
3789 const char *tz
= NULL
;
3791 assert(link
->network
);
3793 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3795 fprintf(f
, "TIMEZONE=%s\n", tz
);
3797 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3799 fputs("DHCP4_ADDRESS=", f
);
3800 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3804 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3812 (void) unlink(link
->lease_file
);
3815 struct in_addr address
;
3817 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3819 fputs("IPV4LL_ADDRESS=", f
);
3820 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3825 r
= fflush_and_check(f
);
3829 if (rename(temp_path
, link
->state_file
) < 0) {
3837 (void) unlink(link
->state_file
);
3839 (void) unlink(temp_path
);
3841 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3844 /* The serialized state in /run is no longer up-to-date. */
3845 void link_dirty(Link
*link
) {
3850 /* mark manager dirty as link is dirty */
3851 manager_dirty(link
->manager
);
3853 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3855 /* allocation errors are ignored */
3858 r
= set_put(link
->manager
->dirty_links
, link
);
3860 /* don't take another ref if the link was already dirty */
3866 /* The serialized state in /run is up-to-date */
3867 void link_clean(Link
*link
) {
3869 assert(link
->manager
);
3871 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3874 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3875 [LINK_STATE_PENDING
] = "pending",
3876 [LINK_STATE_INITIALIZED
] = "initialized",
3877 [LINK_STATE_CONFIGURING
] = "configuring",
3878 [LINK_STATE_CONFIGURED
] = "configured",
3879 [LINK_STATE_UNMANAGED
] = "unmanaged",
3880 [LINK_STATE_FAILED
] = "failed",
3881 [LINK_STATE_LINGER
] = "linger",
3884 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);