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 /* TODO: add conditions for other netdevs. */
351 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
357 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
360 link_update_operstate(master
, true);
363 void link_update_operstate(Link
*link
, bool also_update_master
) {
364 LinkOperationalState operstate
;
369 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
370 operstate
= LINK_OPERSTATE_DORMANT
;
371 else if (link_has_carrier(link
)) {
373 uint8_t scope
= RT_SCOPE_NOWHERE
;
375 /* if we have carrier, check what addresses we have */
376 SET_FOREACH(address
, link
->addresses
, i
) {
377 if (!address_is_ready(address
))
380 if (address
->scope
< scope
)
381 scope
= address
->scope
;
384 /* for operstate we also take foreign addresses into account */
385 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
386 if (!address_is_ready(address
))
389 if (address
->scope
< scope
)
390 scope
= address
->scope
;
393 if (scope
< RT_SCOPE_SITE
)
394 /* universally accessible addresses found */
395 operstate
= LINK_OPERSTATE_ROUTABLE
;
396 else if (scope
< RT_SCOPE_HOST
)
397 /* only link or site local addresses found */
398 operstate
= LINK_OPERSTATE_DEGRADED
;
400 /* no useful addresses found */
401 operstate
= LINK_OPERSTATE_CARRIER
;
402 } else if (link
->flags
& IFF_UP
)
403 operstate
= LINK_OPERSTATE_NO_CARRIER
;
405 operstate
= LINK_OPERSTATE_OFF
;
407 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
408 link_is_enslaved(link
))
409 operstate
= LINK_OPERSTATE_ENSLAVED
;
411 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
414 SET_FOREACH(slave
, link
->slaves
, i
) {
415 link_update_operstate(slave
, false);
417 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
418 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
422 if (link
->operstate
!= operstate
) {
423 link
->operstate
= operstate
;
424 link_send_changed(link
, "OperationalState", NULL
);
428 if (also_update_master
&& link
->network
) {
429 link_update_master_operstate(link
, link
->network
->bond
);
430 link_update_master_operstate(link
, link
->network
->bridge
);
434 #define FLAG_STRING(string, flag, old, new) \
435 (((old ^ new) & flag) \
436 ? ((old & flag) ? (" -" string) : (" +" string)) \
439 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
440 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
446 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
448 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
450 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
452 /* if we got a message without operstate, take it to mean
453 the state was unchanged */
454 operstate
= link
->kernel_operstate
;
456 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
459 if (link
->flags
!= flags
) {
460 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",
461 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
462 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
463 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
464 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
465 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
466 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
467 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
468 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
469 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
470 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
471 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
472 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
473 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
474 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
475 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
476 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
477 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
478 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
479 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
481 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
482 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
483 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
484 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
485 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
486 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
487 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
488 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
490 /* link flags are currently at most 18 bits, let's align to
492 if (unknown_flags_added
)
494 "Unknown link flags gained: %#.5x (ignoring)",
495 unknown_flags_added
);
497 if (unknown_flags_removed
)
499 "Unknown link flags lost: %#.5x (ignoring)",
500 unknown_flags_removed
);
504 link
->kernel_operstate
= operstate
;
506 link_update_operstate(link
, true);
511 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
512 _cleanup_(link_unrefp
) Link
*link
= NULL
;
514 const char *ifname
, *kind
= NULL
;
516 unsigned short iftype
;
522 /* check for link kind */
523 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
525 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
526 r
= sd_netlink_message_exit_container(message
);
531 r
= sd_netlink_message_get_type(message
, &type
);
534 else if (type
!= RTM_NEWLINK
)
537 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
540 else if (ifindex
<= 0)
543 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
547 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
558 .state
= LINK_STATE_PENDING
,
559 .rtnl_extended_attrs
= true,
564 link
->ifname
= strdup(ifname
);
569 link
->kind
= strdup(kind
);
574 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
576 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
578 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
580 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
582 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
585 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
588 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
591 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
595 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
599 r
= link_update_flags(link
, message
, false);
603 *ret
= TAKE_PTR(link
);
608 static Link
*link_free(Link
*link
) {
613 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
614 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
616 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
617 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
619 while ((address
= link
->pool_addresses
)) {
620 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
621 address_free(address
);
624 sd_dhcp_server_unref(link
->dhcp_server
);
625 sd_dhcp_client_unref(link
->dhcp_client
);
626 sd_dhcp_lease_unref(link
->dhcp_lease
);
628 link_lldp_emit_stop(link
);
630 free(link
->lease_file
);
632 sd_lldp_unref(link
->lldp
);
633 free(link
->lldp_file
);
637 sd_ipv4ll_unref(link
->ipv4ll
);
638 sd_dhcp6_client_unref(link
->dhcp6_client
);
639 sd_ndisc_unref(link
->ndisc
);
640 sd_radv_unref(link
->radv
);
645 (void) unlink(link
->state_file
);
646 free(link
->state_file
);
648 sd_device_unref(link
->sd_device
);
650 hashmap_free(link
->bound_to_links
);
651 hashmap_free(link
->bound_by_links
);
653 set_free_with_destructor(link
->slaves
, link_unref
);
655 network_unref(link
->network
);
660 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
662 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
669 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
678 void link_set_state(Link
*link
, LinkState state
) {
681 if (link
->state
== state
)
684 log_link_debug(link
, "State changed: %s -> %s",
685 link_state_to_string(link
->state
),
686 link_state_to_string(state
));
690 link_send_changed(link
, "AdministrativeState", NULL
);
693 static void link_enter_unmanaged(Link
*link
) {
696 link_set_state(link
, LINK_STATE_UNMANAGED
);
701 int link_stop_clients(Link
*link
) {
705 assert(link
->manager
);
706 assert(link
->manager
->event
);
708 if (link
->dhcp_client
) {
709 k
= sd_dhcp_client_stop(link
->dhcp_client
);
711 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
715 k
= sd_ipv4ll_stop(link
->ipv4ll
);
717 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
720 if (link
->dhcp6_client
) {
721 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
723 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
727 k
= sd_ndisc_stop(link
->ndisc
);
729 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
733 k
= sd_radv_stop(link
->radv
);
735 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
738 link_lldp_emit_stop(link
);
742 void link_enter_failed(Link
*link
) {
745 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
748 log_link_warning(link
, "Failed");
750 link_set_state(link
, LINK_STATE_FAILED
);
752 link_stop_clients(link
);
757 static Address
* link_find_dhcp_server_address(Link
*link
) {
761 assert(link
->network
);
763 /* The first statically configured address if there is any */
764 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
766 if (address
->family
!= AF_INET
)
769 if (in_addr_is_null(address
->family
, &address
->in_addr
))
775 /* If that didn't work, find a suitable address we got from the pool */
776 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
777 if (address
->family
!= AF_INET
)
786 static int link_join_netdevs_after_configured(Link
*link
) {
791 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
792 if (netdev
->ifindex
> 0)
793 /* Assume already enslaved. */
796 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
799 log_struct(LOG_DEBUG
,
800 LOG_LINK_INTERFACE(link
),
801 LOG_NETDEV_INTERFACE(netdev
),
802 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
804 r
= netdev_join(netdev
, link
, NULL
);
806 return log_struct_errno(LOG_WARNING
, r
,
807 LOG_LINK_INTERFACE(link
),
808 LOG_NETDEV_INTERFACE(netdev
),
809 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
815 static void link_enter_configured(Link
*link
) {
817 assert(link
->network
);
819 if (link
->state
!= LINK_STATE_CONFIGURING
)
822 log_link_info(link
, "Configured");
824 link_set_state(link
, LINK_STATE_CONFIGURED
);
826 (void) link_join_netdevs_after_configured(link
);
831 static int link_request_set_routing_policy_rule(Link
*link
) {
832 RoutingPolicyRule
*rule
, *rrule
= NULL
;
836 assert(link
->network
);
838 link_set_state(link
, LINK_STATE_CONFIGURING
);
839 link
->routing_policy_rules_configured
= false;
841 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
842 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
843 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
844 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
846 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
850 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
852 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
853 link_enter_failed(link
);
857 link
->routing_policy_rule_messages
++;
860 routing_policy_rule_purge(link
->manager
, link
);
861 if (link
->routing_policy_rule_messages
== 0) {
862 link
->routing_policy_rules_configured
= true;
863 link_check_ready(link
);
865 log_link_debug(link
, "Setting routing policy rules");
870 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
874 assert(link
->route_messages
> 0);
875 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
876 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
878 link
->route_messages
--;
880 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
883 r
= sd_netlink_message_get_errno(m
);
884 if (r
< 0 && r
!= -EEXIST
)
885 log_link_warning_errno(link
, r
, "Could not set route: %m");
887 if (link
->route_messages
== 0) {
888 log_link_debug(link
, "Routes set");
889 link
->static_routes_configured
= true;
890 link_check_ready(link
);
896 int link_request_set_routes(Link
*link
) {
898 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
899 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
906 assert(link
->network
);
907 assert(link
->addresses_configured
);
908 assert(link
->address_messages
== 0);
909 assert(link
->state
!= _LINK_STATE_INVALID
);
911 link_set_state(link
, LINK_STATE_CONFIGURING
);
912 link
->static_routes_configured
= false;
914 r
= link_request_set_routing_policy_rule(link
);
918 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
919 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
920 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
922 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
925 r
= route_configure(rt
, link
, route_handler
);
927 log_link_warning_errno(link
, r
, "Could not set routes: %m");
928 link_enter_failed(link
);
932 link
->route_messages
++;
935 if (link
->route_messages
== 0) {
936 link
->static_routes_configured
= true;
937 link_check_ready(link
);
939 log_link_debug(link
, "Setting routes");
944 void link_check_ready(Link
*link
) {
950 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
956 if (!link
->addresses_configured
)
959 if (!link
->neighbors_configured
)
962 SET_FOREACH(a
, link
->addresses
, i
)
963 if (!address_is_ready(a
))
966 if (!link
->addresses_ready
) {
967 link
->addresses_ready
= true;
968 link_request_set_routes(link
);
972 if (!link
->static_routes_configured
)
975 if (!link
->routing_policy_rules_configured
)
978 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
981 if (link_ipv6ll_enabled(link
) &&
982 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
985 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
986 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
987 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
988 /* When DHCP is enabled, at least one protocol must provide an address, or
989 * an IPv4ll fallback address must be configured. */
992 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
995 if (link
->state
!= LINK_STATE_CONFIGURED
)
996 link_enter_configured(link
);
1001 static int link_request_set_neighbors(Link
*link
) {
1006 assert(link
->network
);
1007 assert(link
->state
!= _LINK_STATE_INVALID
);
1009 link_set_state(link
, LINK_STATE_CONFIGURING
);
1010 link
->neighbors_configured
= false;
1012 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1013 r
= neighbor_configure(neighbor
, link
, NULL
);
1015 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1016 link_enter_failed(link
);
1021 if (link
->neighbor_messages
== 0) {
1022 link
->neighbors_configured
= true;
1023 link_check_ready(link
);
1025 log_link_debug(link
, "Setting neighbors");
1030 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1036 assert(link
->ifname
);
1037 assert(link
->address_messages
> 0);
1038 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1039 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1041 link
->address_messages
--;
1043 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1046 r
= sd_netlink_message_get_errno(m
);
1047 if (r
< 0 && r
!= -EEXIST
)
1048 log_link_warning_errno(link
, r
, "could not set address: %m");
1050 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1052 if (link
->address_messages
== 0) {
1053 log_link_debug(link
, "Addresses set");
1054 link
->addresses_configured
= true;
1055 link_check_ready(link
);
1061 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1062 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1063 size_t n_addresses
= 0, n_allocated
= 0;
1066 log_debug("Copying DNS server information from %s", link
->ifname
);
1071 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1074 /* Only look for IPv4 addresses */
1075 if (link
->network
->dns
[i
].family
!= AF_INET
)
1078 ia
= link
->network
->dns
[i
].address
.in
;
1080 /* Never propagate obviously borked data */
1081 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1084 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1087 addresses
[n_addresses
++] = ia
;
1090 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1091 const struct in_addr
*da
= NULL
;
1094 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1097 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1100 for (j
= 0; j
< n
; j
++)
1101 if (in4_addr_is_non_local(&da
[j
]))
1102 addresses
[n_addresses
++] = da
[j
];
1106 if (n_addresses
<= 0)
1109 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1112 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1113 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1114 size_t n_addresses
= 0, n_allocated
= 0;
1120 log_debug("Copying NTP server information from %s", link
->ifname
);
1122 STRV_FOREACH(a
, link
->network
->ntp
) {
1123 union in_addr_union ia
;
1125 /* Only look for IPv4 addresses */
1126 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1129 /* Never propagate obviously borked data */
1130 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1133 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1136 addresses
[n_addresses
++] = ia
.in
;
1139 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1140 const struct in_addr
*da
= NULL
;
1143 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1146 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1149 for (j
= 0; j
< n
; j
++)
1150 if (in4_addr_is_non_local(&da
[j
]))
1151 addresses
[n_addresses
++] = da
[j
];
1155 if (n_addresses
<= 0)
1158 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1161 static int link_set_bridge_fdb(Link
*link
) {
1162 FdbEntry
*fdb_entry
;
1165 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1166 r
= fdb_entry_configure(link
, fdb_entry
);
1168 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1174 static int link_request_set_addresses(Link
*link
) {
1175 AddressLabel
*label
;
1180 assert(link
->network
);
1181 assert(link
->state
!= _LINK_STATE_INVALID
);
1183 link_set_state(link
, LINK_STATE_CONFIGURING
);
1185 /* Reset all *_configured flags we are configuring. */
1186 link
->addresses_configured
= false;
1187 link
->addresses_ready
= false;
1188 link
->neighbors_configured
= false;
1189 link
->static_routes_configured
= false;
1190 link
->routing_policy_rules_configured
= false;
1192 r
= link_set_bridge_fdb(link
);
1196 r
= link_request_set_neighbors(link
);
1200 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1203 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1205 r
= address_configure(ad
, link
, address_handler
, update
);
1207 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1208 link_enter_failed(link
);
1212 link
->address_messages
++;
1215 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1216 r
= address_label_configure(label
, link
, NULL
, false);
1218 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1219 link_enter_failed(link
);
1223 link
->address_label_messages
++;
1226 /* now that we can figure out a default address for the dhcp server,
1228 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1230 Link
*uplink
= NULL
;
1231 bool acquired_uplink
= false;
1233 address
= link_find_dhcp_server_address(link
);
1235 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1236 link_enter_failed(link
);
1240 /* use the server address' subnet as the pool */
1241 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1242 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1247 r = sd_dhcp_server_set_router(link->dhcp_server,
1248 &main_address->in_addr.in);
1253 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1254 r
= sd_dhcp_server_set_max_lease_time(
1256 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1261 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1262 r
= sd_dhcp_server_set_default_lease_time(
1264 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1269 if (link
->network
->dhcp_server_emit_dns
) {
1271 if (link
->network
->n_dhcp_server_dns
> 0)
1272 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1274 uplink
= manager_find_uplink(link
->manager
, link
);
1275 acquired_uplink
= true;
1278 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1281 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1284 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1287 if (link
->network
->dhcp_server_emit_ntp
) {
1289 if (link
->network
->n_dhcp_server_ntp
> 0)
1290 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1292 if (!acquired_uplink
)
1293 uplink
= manager_find_uplink(link
->manager
, link
);
1296 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1299 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1303 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1306 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1308 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1310 if (link
->network
->dhcp_server_emit_timezone
) {
1311 _cleanup_free_
char *buffer
= NULL
;
1312 const char *tz
= NULL
;
1314 if (link
->network
->dhcp_server_timezone
)
1315 tz
= link
->network
->dhcp_server_timezone
;
1317 r
= get_timezone(&buffer
);
1319 log_warning_errno(r
, "Failed to determine timezone: %m");
1325 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1330 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1331 r
= sd_dhcp_server_start(link
->dhcp_server
);
1333 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1335 link_enter_failed(link
);
1341 log_link_debug(link
, "Offering DHCPv4 leases");
1344 if (link
->address_messages
== 0) {
1345 link
->addresses_configured
= true;
1346 link_check_ready(link
);
1348 log_link_debug(link
, "Setting addresses");
1353 static int link_set_bridge_vlan(Link
*link
) {
1356 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1358 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1363 static int link_set_proxy_arp(Link
*link
) {
1366 if (!link_proxy_arp_enabled(link
))
1369 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1371 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1376 static int link_configure_after_setting_mtu(Link
*link
);
1378 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1383 assert(link
->ifname
);
1385 link
->setting_mtu
= false;
1387 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1390 r
= sd_netlink_message_get_errno(m
);
1392 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1396 log_link_debug(link
, "Setting MTU done.");
1398 if (link
->state
== LINK_STATE_INITIALIZED
)
1399 (void) link_configure_after_setting_mtu(link
);
1404 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1405 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1409 assert(link
->manager
);
1410 assert(link
->manager
->rtnl
);
1412 if (mtu
== 0 || link
->setting_mtu
)
1415 if (link
->mtu
== mtu
)
1418 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1420 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1422 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1424 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1425 * for this interface, then disable IPv6 else enable it. */
1426 (void) link_enable_ipv6(link
);
1428 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1429 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1430 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1432 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1433 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1438 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1440 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1442 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1443 link_netlink_destroy_callback
, link
);
1445 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1448 link
->setting_mtu
= true;
1453 static bool link_reduces_vlan_mtu(Link
*link
) {
1454 /* See netif_reduces_vlan_mtu() in kernel. */
1455 return streq_ptr(link
->kind
, "macsec");
1458 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1463 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1464 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1465 /* See vlan_dev_change_mtu() in kernel. */
1466 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1468 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1469 /* See macvlan_change_mtu() in kernel. */
1475 static int link_configure_mtu(Link
*link
) {
1479 assert(link
->network
);
1481 if (link
->network
->mtu
> 0)
1482 return link_set_mtu(link
, link
->network
->mtu
);
1484 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1485 if (link
->mtu
>= mtu
)
1488 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1489 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1492 return link_set_mtu(link
, mtu
);
1495 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1500 assert(link
->ifname
);
1502 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1505 r
= sd_netlink_message_get_errno(m
);
1507 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1512 static int link_set_flags(Link
*link
) {
1513 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1514 unsigned ifi_change
= 0;
1515 unsigned ifi_flags
= 0;
1519 assert(link
->manager
);
1520 assert(link
->manager
->rtnl
);
1522 if (link
->flags
& IFF_LOOPBACK
)
1528 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1531 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1533 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1535 if (link
->network
->arp
>= 0) {
1536 ifi_change
|= IFF_NOARP
;
1537 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1540 if (link
->network
->multicast
>= 0) {
1541 ifi_change
|= IFF_MULTICAST
;
1542 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1545 if (link
->network
->allmulticast
>= 0) {
1546 ifi_change
|= IFF_ALLMULTI
;
1547 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1550 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1552 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1554 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1555 link_netlink_destroy_callback
, link
);
1557 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1564 static int link_acquire_ipv6_conf(Link
*link
) {
1569 if (link_ipv6_accept_ra_enabled(link
)) {
1570 assert(link
->ndisc
);
1572 log_link_debug(link
, "Discovering IPv6 routers");
1574 r
= sd_ndisc_start(link
->ndisc
);
1575 if (r
< 0 && r
!= -EBUSY
)
1576 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1579 if (link_radv_enabled(link
)) {
1581 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1583 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1585 r
= sd_radv_start(link
->radv
);
1586 if (r
< 0 && r
!= -EBUSY
)
1587 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1590 (void) dhcp6_request_prefix_delegation(link
);
1595 static int link_acquire_ipv4_conf(Link
*link
) {
1599 assert(link
->manager
);
1600 assert(link
->manager
->event
);
1602 if (link_ipv4ll_enabled(link
)) {
1603 assert(link
->ipv4ll
);
1605 log_link_debug(link
, "Acquiring IPv4 link-local address");
1607 r
= sd_ipv4ll_start(link
->ipv4ll
);
1609 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1612 if (link_dhcp4_enabled(link
)) {
1613 assert(link
->dhcp_client
);
1615 log_link_debug(link
, "Acquiring DHCPv4 lease");
1617 r
= sd_dhcp_client_start(link
->dhcp_client
);
1619 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1625 static int link_acquire_conf(Link
*link
) {
1630 r
= link_acquire_ipv4_conf(link
);
1634 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1635 r
= link_acquire_ipv6_conf(link
);
1640 if (link_lldp_emit_enabled(link
)) {
1641 r
= link_lldp_emit_start(link
);
1643 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1649 bool link_has_carrier(Link
*link
) {
1650 /* see Documentation/networking/operstates.txt in the kernel sources */
1652 if (link
->kernel_operstate
== IF_OPER_UP
)
1655 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1656 /* operstate may not be implemented, so fall back to flags */
1657 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1658 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1664 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1669 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1672 r
= sd_netlink_message_get_errno(m
);
1674 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1679 static int link_configure_addrgen_mode(Link
*link
) {
1680 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1681 uint8_t ipv6ll_mode
;
1685 assert(link
->network
);
1686 assert(link
->manager
);
1687 assert(link
->manager
->rtnl
);
1689 if (!socket_ipv6_is_supported())
1692 log_link_debug(link
, "Setting address genmode for link");
1694 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1696 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1698 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1700 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1702 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1704 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1706 if (!link_ipv6ll_enabled(link
))
1707 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1708 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1709 /* The file may not exist. And event if it exists, when stable_secret is unset,
1710 * reading the file fails with EIO. */
1711 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1713 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1715 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1717 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1719 r
= sd_netlink_message_close_container(req
);
1721 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1723 r
= sd_netlink_message_close_container(req
);
1725 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1727 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1728 link_netlink_destroy_callback
, link
);
1730 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1737 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1742 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1745 r
= sd_netlink_message_get_errno(m
);
1747 /* we warn but don't fail the link, as it may be brought up later */
1748 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1753 static int link_up(Link
*link
) {
1754 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1758 assert(link
->network
);
1759 assert(link
->manager
);
1760 assert(link
->manager
->rtnl
);
1762 log_link_debug(link
, "Bringing link up");
1764 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1766 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1768 /* set it free if not enslaved with networkd */
1769 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1770 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1772 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1775 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1777 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1779 if (link
->network
->mac
) {
1780 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1782 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1785 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1786 link_netlink_destroy_callback
, link
);
1788 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1795 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1800 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1803 r
= sd_netlink_message_get_errno(m
);
1805 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1810 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1811 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1815 assert(link
->manager
);
1816 assert(link
->manager
->rtnl
);
1818 log_link_debug(link
, "Bringing link down");
1820 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1821 RTM_SETLINK
, link
->ifindex
);
1823 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1825 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1827 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1829 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1830 callback
?: link_down_handler
,
1831 link_netlink_destroy_callback
, link
);
1833 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1840 static int link_handle_bound_to_list(Link
*link
) {
1844 bool required_up
= false;
1845 bool link_is_up
= false;
1849 if (hashmap_isempty(link
->bound_to_links
))
1852 if (link
->flags
& IFF_UP
)
1855 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1856 if (link_has_carrier(l
)) {
1861 if (!required_up
&& link_is_up
) {
1862 r
= link_down(link
, NULL
);
1865 } else if (required_up
&& !link_is_up
) {
1874 static int link_handle_bound_by_list(Link
*link
) {
1881 if (hashmap_isempty(link
->bound_by_links
))
1884 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1885 r
= link_handle_bound_to_list(l
);
1893 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1899 if (link
== carrier
)
1902 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1905 r
= hashmap_ensure_allocated(h
, NULL
);
1909 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1916 static int link_new_bound_by_list(Link
*link
) {
1921 bool list_updated
= false;
1924 assert(link
->manager
);
1928 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1929 if (!carrier
->network
)
1932 if (strv_isempty(carrier
->network
->bind_carrier
))
1935 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1936 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1940 list_updated
= true;
1947 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1948 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1952 link_dirty(carrier
);
1958 static int link_new_bound_to_list(Link
*link
) {
1963 bool list_updated
= false;
1966 assert(link
->manager
);
1971 if (strv_isempty(link
->network
->bind_carrier
))
1976 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1977 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1978 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1982 list_updated
= true;
1989 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1990 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1994 link_dirty(carrier
);
2000 static int link_new_carrier_maps(Link
*link
) {
2003 r
= link_new_bound_by_list(link
);
2007 r
= link_handle_bound_by_list(link
);
2011 r
= link_new_bound_to_list(link
);
2015 r
= link_handle_bound_to_list(link
);
2022 static void link_free_bound_to_list(Link
*link
) {
2026 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2027 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2029 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2030 link_dirty(bound_to
);
2036 static void link_free_bound_by_list(Link
*link
) {
2040 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2041 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2043 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2044 link_dirty(bound_by
);
2045 link_handle_bound_to_list(bound_by
);
2052 static void link_free_carrier_maps(Link
*link
) {
2053 bool list_updated
= false;
2057 if (!hashmap_isempty(link
->bound_to_links
)) {
2058 link_free_bound_to_list(link
);
2059 list_updated
= true;
2062 if (!hashmap_isempty(link
->bound_by_links
)) {
2063 link_free_bound_by_list(link
);
2064 list_updated
= true;
2073 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2080 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2084 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2088 r
= set_put(master
->slaves
, link
);
2096 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2101 if (!link
->manager
|| !netdev
)
2104 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2107 link_unref(set_remove(master
->slaves
, link
));
2110 static void link_detach_from_manager(Link
*link
) {
2111 if (!link
|| !link
->manager
)
2114 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2117 /* The following must be called at last. */
2118 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2122 void link_drop(Link
*link
) {
2123 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2126 link_set_state(link
, LINK_STATE_LINGER
);
2128 link_free_carrier_maps(link
);
2130 if (link
->network
) {
2131 link_drop_from_master(link
, link
->network
->bridge
);
2132 link_drop_from_master(link
, link
->network
->bond
);
2135 log_link_debug(link
, "Link removed");
2137 (void) unlink(link
->state_file
);
2138 link_detach_from_manager(link
);
2141 static int link_joined(Link
*link
) {
2145 assert(link
->network
);
2147 if (!hashmap_isempty(link
->bound_to_links
)) {
2148 r
= link_handle_bound_to_list(link
);
2151 } else if (!(link
->flags
& IFF_UP
)) {
2154 link_enter_failed(link
);
2159 if (link
->network
->bridge
) {
2160 r
= link_set_bridge(link
);
2162 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2164 r
= link_append_to_master(link
, link
->network
->bridge
);
2166 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2169 if (link
->network
->bond
) {
2170 r
= link_set_bond(link
);
2172 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2174 r
= link_append_to_master(link
, link
->network
->bond
);
2176 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2179 if (link
->network
->use_br_vlan
&&
2180 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2181 r
= link_set_bridge_vlan(link
);
2183 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2186 /* Skip setting up addresses until it gets carrier,
2187 or it would try to set addresses twice,
2188 which is bad for non-idempotent steps. */
2189 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2192 return link_request_set_addresses(link
);
2195 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2199 assert(link
->network
);
2200 assert(link
->enslaving
> 0);
2204 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2207 r
= sd_netlink_message_get_errno(m
);
2208 if (r
< 0 && r
!= -EEXIST
) {
2209 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2210 link_enter_failed(link
);
2213 log_link_debug(link
, "Joined netdev");
2215 if (link
->enslaving
== 0) {
2222 static int link_enter_join_netdev(Link
*link
) {
2228 assert(link
->network
);
2229 assert(link
->state
== LINK_STATE_INITIALIZED
);
2231 link_set_state(link
, LINK_STATE_CONFIGURING
);
2234 link
->enslaving
= 0;
2236 if (link
->network
->bond
) {
2237 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2238 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2239 return link_joined(link
);
2241 log_struct(LOG_DEBUG
,
2242 LOG_LINK_INTERFACE(link
),
2243 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2244 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2248 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2250 log_struct_errno(LOG_WARNING
, r
,
2251 LOG_LINK_INTERFACE(link
),
2252 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2253 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2254 link_enter_failed(link
);
2259 if (link
->network
->bridge
) {
2260 log_struct(LOG_DEBUG
,
2261 LOG_LINK_INTERFACE(link
),
2262 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2263 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2267 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2269 log_struct_errno(LOG_WARNING
, r
,
2270 LOG_LINK_INTERFACE(link
),
2271 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2272 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2273 link_enter_failed(link
);
2278 if (link
->network
->vrf
) {
2279 log_struct(LOG_DEBUG
,
2280 LOG_LINK_INTERFACE(link
),
2281 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2282 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2286 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2288 log_struct_errno(LOG_WARNING
, r
,
2289 LOG_LINK_INTERFACE(link
),
2290 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2291 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2292 link_enter_failed(link
);
2297 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2299 if (netdev
->ifindex
> 0)
2300 /* Assume already enslaved. */
2303 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2306 log_struct(LOG_DEBUG
,
2307 LOG_LINK_INTERFACE(link
),
2308 LOG_NETDEV_INTERFACE(netdev
),
2309 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2313 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2315 log_struct_errno(LOG_WARNING
, r
,
2316 LOG_LINK_INTERFACE(link
),
2317 LOG_NETDEV_INTERFACE(netdev
),
2318 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2319 link_enter_failed(link
);
2324 if (link
->enslaving
== 0)
2325 return link_joined(link
);
2330 static int link_set_ipv4_forward(Link
*link
) {
2333 if (!link_ipv4_forward_enabled(link
))
2336 /* We propagate the forwarding flag from one interface to the
2337 * global setting one way. This means: as long as at least one
2338 * interface was configured at any time that had IP forwarding
2339 * enabled the setting will stay on for good. We do this
2340 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2341 * somewhat in sync (see below). */
2343 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2345 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2350 static int link_set_ipv6_forward(Link
*link
) {
2353 if (!link_ipv6_forward_enabled(link
))
2356 /* On Linux, the IPv6 stack does not know a per-interface
2357 * packet forwarding setting: either packet forwarding is on
2358 * for all, or off for all. We hence don't bother with a
2359 * per-interface setting, but simply propagate the interface
2360 * flag, if it is set, to the global flag, one-way. Note that
2361 * while IPv4 would allow a per-interface flag, we expose the
2362 * same behaviour there and also propagate the setting from
2363 * one to all, to keep things simple (see above). */
2365 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2367 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2372 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2373 IPv6PrivacyExtensions s
;
2376 s
= link_ipv6_privacy_extensions(link
);
2380 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2382 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2387 static int link_set_ipv6_accept_ra(Link
*link
) {
2390 /* Make this a NOP if IPv6 is not available */
2391 if (!socket_ipv6_is_supported())
2394 if (link
->flags
& IFF_LOOPBACK
)
2400 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2402 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2407 static int link_set_ipv6_dad_transmits(Link
*link
) {
2410 /* Make this a NOP if IPv6 is not available */
2411 if (!socket_ipv6_is_supported())
2414 if (link
->flags
& IFF_LOOPBACK
)
2420 if (link
->network
->ipv6_dad_transmits
< 0)
2423 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2425 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2430 static int link_set_ipv6_hop_limit(Link
*link
) {
2433 /* Make this a NOP if IPv6 is not available */
2434 if (!socket_ipv6_is_supported())
2437 if (link
->flags
& IFF_LOOPBACK
)
2443 if (link
->network
->ipv6_hop_limit
< 0)
2446 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2448 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2453 static int link_set_ipv6_mtu(Link
*link
) {
2456 /* Make this a NOP if IPv6 is not available */
2457 if (!socket_ipv6_is_supported())
2460 if (link
->flags
& IFF_LOOPBACK
)
2463 if (link
->network
->ipv6_mtu
== 0)
2466 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2468 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2473 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2474 Address
*net_address
;
2482 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2483 if (address_equal(net_address
, address
))
2489 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2498 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2499 if (route_equal(net_route
, route
))
2505 static int link_drop_foreign_config(Link
*link
) {
2511 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2512 /* we consider IPv6LL addresses to be managed by the kernel */
2513 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2516 if (link_is_static_address_configured(link
, address
)) {
2517 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2519 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2521 r
= address_remove(address
, link
, NULL
);
2527 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2528 /* do not touch routes managed by the kernel */
2529 if (route
->protocol
== RTPROT_KERNEL
)
2532 if (link_is_static_route_configured(link
, route
)) {
2533 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2537 r
= route_remove(route
, link
, NULL
);
2546 static int link_drop_config(Link
*link
) {
2547 Address
*address
, *pool_address
;
2552 SET_FOREACH(address
, link
->addresses
, i
) {
2553 /* we consider IPv6LL addresses to be managed by the kernel */
2554 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2557 r
= address_remove(address
, link
, NULL
);
2561 /* If this address came from an address pool, clean up the pool */
2562 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2563 if (address_equal(address
, pool_address
)) {
2564 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2565 address_free(pool_address
);
2571 SET_FOREACH(route
, link
->routes
, i
) {
2572 /* do not touch routes managed by the kernel */
2573 if (route
->protocol
== RTPROT_KERNEL
)
2576 r
= route_remove(route
, link
, NULL
);
2586 static int link_configure(Link
*link
) {
2590 assert(link
->network
);
2591 assert(link
->state
== LINK_STATE_INITIALIZED
);
2593 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2594 return link_configure_can(link
);
2596 /* Drop foreign config, but ignore loopback or critical devices.
2597 * We do not want to remove loopback address or addresses used for root NFS. */
2598 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2599 r
= link_drop_foreign_config(link
);
2604 r
= link_set_proxy_arp(link
);
2608 r
= ipv6_proxy_ndp_addresses_configure(link
);
2612 r
= link_set_ipv4_forward(link
);
2616 r
= link_set_ipv6_forward(link
);
2620 r
= link_set_ipv6_privacy_extensions(link
);
2624 r
= link_set_ipv6_accept_ra(link
);
2628 r
= link_set_ipv6_dad_transmits(link
);
2632 r
= link_set_ipv6_hop_limit(link
);
2636 r
= link_set_flags(link
);
2640 r
= link_set_ipv6_mtu(link
);
2644 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2645 r
= ipv4ll_configure(link
);
2650 if (link_dhcp4_enabled(link
)) {
2651 r
= dhcp4_set_promote_secondaries(link
);
2655 r
= dhcp4_configure(link
);
2660 if (link_dhcp4_server_enabled(link
)) {
2661 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2665 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2670 if (link_dhcp6_enabled(link
) ||
2671 link_ipv6_accept_ra_enabled(link
)) {
2672 r
= dhcp6_configure(link
);
2677 if (link_ipv6_accept_ra_enabled(link
)) {
2678 r
= ndisc_configure(link
);
2683 if (link_radv_enabled(link
)) {
2684 r
= radv_configure(link
);
2689 if (link_lldp_rx_enabled(link
)) {
2690 r
= link_lldp_rx_configure(link
);
2695 r
= link_configure_mtu(link
);
2699 r
= link_configure_addrgen_mode(link
);
2703 return link_configure_after_setting_mtu(link
);
2706 static int link_configure_after_setting_mtu(Link
*link
) {
2710 assert(link
->network
);
2711 assert(link
->state
== LINK_STATE_INITIALIZED
);
2713 if (link
->setting_mtu
)
2716 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2717 r
= link_acquire_conf(link
);
2722 return link_enter_join_netdev(link
);
2725 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2728 if (duid
->raw_data_len
> 0)
2731 if (duid
->type
!= DUID_TYPE_UUID
)
2734 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2735 duid
->raw_data_len
= sizeof(sd_id128_t
);
2740 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2741 Manager
*manager
= userdata
;
2742 const sd_bus_error
*e
;
2752 e
= sd_bus_message_get_error(m
);
2754 log_error_errno(sd_bus_error_get_errno(e
),
2755 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2760 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2764 if (sz
!= sizeof(sd_id128_t
)) {
2765 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2769 memcpy(&manager
->product_uuid
, a
, sz
);
2770 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2771 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2773 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2776 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2777 r
= link_configure(link
);
2779 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2782 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2784 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2785 * even if the method fails. */
2786 manager
->has_product_uuid
= true;
2791 static bool link_requires_uuid(Link
*link
) {
2795 assert(link
->manager
);
2796 assert(link
->network
);
2798 duid
= link_get_duid(link
);
2799 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2802 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2805 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2811 static int link_configure_duid(Link
*link
) {
2817 assert(link
->manager
);
2818 assert(link
->network
);
2821 duid
= link_get_duid(link
);
2823 if (!link_requires_uuid(link
))
2826 if (m
->has_product_uuid
) {
2827 (void) duid_set_uuid(duid
, m
->product_uuid
);
2831 if (!m
->links_requesting_uuid
) {
2832 r
= manager_request_product_uuid(m
, link
);
2837 log_link_warning_errno(link
, r
,
2838 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
2842 r
= set_put(m
->links_requesting_uuid
, link
);
2846 r
= set_put(m
->duids_requesting_uuid
, duid
);
2854 static int link_initialized_and_synced(Link
*link
) {
2859 assert(link
->ifname
);
2860 assert(link
->manager
);
2862 /* We may get called either from the asynchronous netlink callback,
2863 * or directly for link_add() if running in a container. See link_add(). */
2864 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2867 log_link_debug(link
, "Link state is up-to-date");
2868 link_set_state(link
, LINK_STATE_INITIALIZED
);
2870 r
= link_new_bound_by_list(link
);
2874 r
= link_handle_bound_by_list(link
);
2878 if (!link
->network
) {
2879 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2880 &link
->mac
, &network
);
2882 link_enter_unmanaged(link
);
2884 } else if (r
== 0 && network
->unmanaged
) {
2885 link_enter_unmanaged(link
);
2890 if (link
->flags
& IFF_LOOPBACK
) {
2891 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2892 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2894 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2895 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2897 if (network
->dhcp_server
)
2898 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2901 r
= network_apply(network
, link
);
2906 r
= link_new_bound_to_list(link
);
2910 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2911 * link_configure() is called later asynchronously. */
2912 r
= link_configure_duid(link
);
2916 r
= link_configure(link
);
2923 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2924 (void) link_initialized_and_synced(link
);
2928 int link_initialized(Link
*link
, sd_device
*device
) {
2929 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2933 assert(link
->manager
);
2934 assert(link
->manager
->rtnl
);
2937 if (link
->state
!= LINK_STATE_PENDING
)
2940 if (link
->sd_device
)
2943 log_link_debug(link
, "udev initialized link");
2944 link_set_state(link
, LINK_STATE_INITIALIZED
);
2946 link
->sd_device
= sd_device_ref(device
);
2948 /* udev has initialized the link, but we don't know if we have yet
2949 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2950 * when it returns we know that the pending NEWLINKs have already been
2951 * processed and that we are up-to-date */
2953 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2958 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2959 link_netlink_destroy_callback
, link
);
2968 static int link_load(Link
*link
) {
2969 _cleanup_free_
char *network_file
= NULL
,
2972 *dhcp4_address
= NULL
,
2973 *ipv4ll_address
= NULL
;
2974 union in_addr_union address
;
2975 union in_addr_union route_dst
;
2981 r
= parse_env_file(NULL
, link
->state_file
,
2982 "NETWORK_FILE", &network_file
,
2983 "ADDRESSES", &addresses
,
2985 "DHCP4_ADDRESS", &dhcp4_address
,
2986 "IPV4LL_ADDRESS", &ipv4ll_address
);
2987 if (r
< 0 && r
!= -ENOENT
)
2988 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2995 suffix
= strrchr(network_file
, '.');
2997 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2998 goto network_file_fail
;
3002 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3004 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3005 goto network_file_fail
;
3008 r
= network_apply(network
, link
);
3010 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3019 _cleanup_free_
char *address_str
= NULL
;
3020 char *prefixlen_str
;
3022 unsigned char prefixlen
;
3024 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3026 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3032 prefixlen_str
= strchr(address_str
, '/');
3033 if (!prefixlen_str
) {
3034 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3038 *prefixlen_str
++ = '\0';
3040 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3042 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3046 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3048 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3052 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3054 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3063 _cleanup_free_
char *route_str
= NULL
;
3064 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3066 char *prefixlen_str
;
3068 unsigned char prefixlen
, tos
, table
;
3071 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3073 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3079 prefixlen_str
= strchr(route_str
, '/');
3080 if (!prefixlen_str
) {
3081 log_link_debug(link
, "Failed to parse route %s", route_str
);
3085 *prefixlen_str
++ = '\0';
3087 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3089 log_link_debug(link
,
3090 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3095 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3097 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3101 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3103 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3105 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3106 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3107 0, route_expire_handler
, route
);
3109 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3112 route
->lifetime
= lifetime
;
3113 sd_event_source_unref(route
->expire
);
3114 route
->expire
= TAKE_PTR(expire
);
3118 if (dhcp4_address
) {
3119 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3121 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3122 goto dhcp4_address_fail
;
3125 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3127 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3129 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3131 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3136 if (ipv4ll_address
) {
3137 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3139 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3140 goto ipv4ll_address_fail
;
3143 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3145 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3147 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3149 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3152 ipv4ll_address_fail
:
3157 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3158 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3159 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3168 r
= link_new(m
, message
, ret
);
3174 log_link_debug(link
, "Link %d added", link
->ifindex
);
3176 r
= link_load(link
);
3180 if (detect_container() <= 0) {
3181 /* not in a container, udev will be around */
3182 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3183 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3185 log_link_warning_errno(link
, r
, "Could not find device: %m");
3189 r
= sd_device_get_is_initialized(device
);
3191 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3196 log_link_debug(link
, "link pending udev initialization...");
3200 r
= device_is_renaming(device
);
3202 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3206 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3210 r
= link_initialized(link
, device
);
3214 r
= link_initialized_and_synced(link
);
3221 link_enter_failed(link
);
3225 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3230 log_link_info(link
, "Gained IPv6LL");
3232 link
->ipv6ll_address
= *address
;
3233 link_check_ready(link
);
3235 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3236 r
= link_acquire_ipv6_conf(link
);
3238 link_enter_failed(link
);
3246 static int link_carrier_gained(Link
*link
) {
3251 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3252 r
= link_acquire_conf(link
);
3254 link_enter_failed(link
);
3258 r
= link_request_set_addresses(link
);
3263 r
= link_handle_bound_by_list(link
);
3270 static int link_carrier_lost(Link
*link
) {
3275 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3278 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3279 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3280 * configuration and stop the clients as well. */
3281 if (link
->setting_mtu
)
3284 r
= link_stop_clients(link
);
3286 link_enter_failed(link
);
3290 if (link_dhcp4_server_enabled(link
))
3291 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3293 r
= link_drop_config(link
);
3297 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3298 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3299 r
= link_drop_foreign_config(link
);
3304 r
= link_handle_bound_by_list(link
);
3311 int link_carrier_reset(Link
*link
) {
3316 if (link_has_carrier(link
)) {
3317 r
= link_carrier_lost(link
);
3321 r
= link_carrier_gained(link
);
3325 log_link_info(link
, "Reset carrier");
3331 int link_update(Link
*link
, sd_netlink_message
*m
) {
3332 struct ether_addr mac
;
3335 bool had_carrier
, carrier_gained
, carrier_lost
;
3339 assert(link
->ifname
);
3342 if (link
->state
== LINK_STATE_LINGER
) {
3343 log_link_info(link
, "Link re-added");
3344 link_set_state(link
, LINK_STATE_CONFIGURING
);
3346 r
= link_new_carrier_maps(link
);
3351 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3352 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3353 Manager
*manager
= link
->manager
;
3355 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3358 r
= link_add(manager
, m
, &link
);
3363 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3364 if (r
>= 0 && mtu
> 0) {
3366 if (link
->original_mtu
== 0) {
3367 link
->original_mtu
= mtu
;
3368 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3371 if (link
->dhcp_client
) {
3372 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3375 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3379 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3381 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3385 /* The kernel may broadcast NEWLINK messages without the MAC address
3386 set, simply ignore them. */
3387 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3389 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3392 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3395 log_link_debug(link
, "MAC address: "
3396 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3397 mac
.ether_addr_octet
[0],
3398 mac
.ether_addr_octet
[1],
3399 mac
.ether_addr_octet
[2],
3400 mac
.ether_addr_octet
[3],
3401 mac
.ether_addr_octet
[4],
3402 mac
.ether_addr_octet
[5]);
3405 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3407 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3410 if (link
->dhcp_client
) {
3411 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3412 (const uint8_t *) &link
->mac
,
3416 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3418 r
= dhcp4_set_client_identifier(link
);
3423 if (link
->dhcp6_client
) {
3424 const DUID
* duid
= link_get_duid(link
);
3426 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3427 (const uint8_t *) &link
->mac
,
3431 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3433 if (link
->network
->iaid_set
) {
3434 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3435 link
->network
->iaid
);
3437 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3440 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3442 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3443 duid
->raw_data_len
);
3445 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3449 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3451 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3455 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3457 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3462 old_master
= link
->master_ifindex
;
3463 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3465 had_carrier
= link_has_carrier(link
);
3467 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3471 r
= link_update_lldp(link
);
3475 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3476 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3478 if (carrier_gained
) {
3479 log_link_info(link
, "Gained carrier");
3481 r
= link_carrier_gained(link
);
3484 } else if (carrier_lost
) {
3485 log_link_info(link
, "Lost carrier");
3487 r
= link_carrier_lost(link
);
3495 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3503 if (hashmap_isempty(h
))
3507 HASHMAP_FOREACH(link
, h
, i
) {
3511 fprintf(f
, "%i", link
->ifindex
);
3518 int link_save(Link
*link
) {
3519 _cleanup_free_
char *temp_path
= NULL
;
3520 _cleanup_fclose_
FILE *f
= NULL
;
3521 const char *admin_state
, *oper_state
;
3528 assert(link
->state_file
);
3529 assert(link
->lease_file
);
3530 assert(link
->manager
);
3532 if (link
->state
== LINK_STATE_LINGER
) {
3533 (void) unlink(link
->state_file
);
3537 link_lldp_save(link
);
3539 admin_state
= link_state_to_string(link
->state
);
3540 assert(admin_state
);
3542 oper_state
= link_operstate_to_string(link
->operstate
);
3545 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3549 (void) fchmod(fileno(f
), 0644);
3552 "# This is private data. Do not parse.\n"
3555 admin_state
, oper_state
);
3557 if (link
->network
) {
3558 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
3559 const char *dhcp_domainname
= NULL
, *p
;
3560 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3564 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3565 yes_no(link
->network
->required_for_online
));
3567 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3568 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3570 if (link
->dhcp6_client
) {
3571 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3572 if (r
< 0 && r
!= -ENOMSG
)
3573 log_link_debug(link
, "No DHCPv6 lease");
3576 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3581 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3582 _cleanup_free_
char *b
= NULL
;
3584 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3585 &link
->network
->dns
[j
].address
, &b
);
3587 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3597 if (link
->network
->dhcp_use_dns
&&
3599 const struct in_addr
*addresses
;
3601 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3603 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3607 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3608 struct in6_addr
*in6_addrs
;
3610 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3614 serialize_in6_addrs(f
, in6_addrs
, r
);
3619 /* Make sure to flush out old entries before we use the NDISC data */
3622 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3625 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3629 serialize_in6_addrs(f
, &dd
->address
, 1);
3638 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3640 if (link
->network
->dhcp_use_ntp
&&
3642 const struct in_addr
*addresses
;
3644 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3646 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3650 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3651 struct in6_addr
*in6_addrs
;
3654 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3659 serialize_in6_addrs(f
, in6_addrs
, r
);
3663 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3665 fputstrv(f
, hosts
, NULL
, &space
);
3670 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3671 if (link
->dhcp_lease
) {
3672 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3673 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3676 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3679 fputs("DOMAINS=", f
);
3681 ORDERED_SET_FOREACH(p
, link
->network
->search_domains
, i
)
3682 fputs_with_space(f
, p
, NULL
, &space
);
3684 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3687 if (dhcp_domainname
)
3688 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3690 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3692 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3694 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3695 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3700 fputs("ROUTE_DOMAINS=", f
);
3702 ORDERED_SET_FOREACH(p
, link
->network
->route_domains
, i
)
3703 fputs_with_space(f
, p
, NULL
, &space
);
3705 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3708 if (dhcp_domainname
)
3709 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3711 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3713 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3715 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3716 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3721 fprintf(f
, "LLMNR=%s\n",
3722 resolve_support_to_string(link
->network
->llmnr
));
3723 fprintf(f
, "MDNS=%s\n",
3724 resolve_support_to_string(link
->network
->mdns
));
3725 if (link
->network
->dns_default_route
>= 0)
3726 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3728 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3729 fprintf(f
, "DNS_OVER_TLS=%s\n",
3730 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3732 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3733 fprintf(f
, "DNSSEC=%s\n",
3734 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3736 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3739 fputs("DNSSEC_NTA=", f
);
3741 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3742 fputs_with_space(f
, n
, NULL
, &space
);
3746 fputs("ADDRESSES=", f
);
3748 SET_FOREACH(a
, link
->addresses
, i
) {
3749 _cleanup_free_
char *address_str
= NULL
;
3751 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3755 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3760 fputs("ROUTES=", f
);
3762 SET_FOREACH(route
, link
->routes
, i
) {
3763 _cleanup_free_
char *route_str
= NULL
;
3765 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3769 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3770 space
? " " : "", route_str
,
3771 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3778 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3779 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3781 if (link
->dhcp_lease
) {
3782 struct in_addr address
;
3783 const char *tz
= NULL
;
3785 assert(link
->network
);
3787 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3789 fprintf(f
, "TIMEZONE=%s\n", tz
);
3791 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3793 fputs("DHCP4_ADDRESS=", f
);
3794 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3798 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3806 (void) unlink(link
->lease_file
);
3809 struct in_addr address
;
3811 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3813 fputs("IPV4LL_ADDRESS=", f
);
3814 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3819 r
= fflush_and_check(f
);
3823 if (rename(temp_path
, link
->state_file
) < 0) {
3831 (void) unlink(link
->state_file
);
3833 (void) unlink(temp_path
);
3835 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3838 /* The serialized state in /run is no longer up-to-date. */
3839 void link_dirty(Link
*link
) {
3844 /* mark manager dirty as link is dirty */
3845 manager_dirty(link
->manager
);
3847 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3849 /* allocation errors are ignored */
3852 r
= set_put(link
->manager
->dirty_links
, link
);
3854 /* don't take another ref if the link was already dirty */
3860 /* The serialized state in /run is up-to-date */
3861 void link_clean(Link
*link
) {
3863 assert(link
->manager
);
3865 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3868 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3869 [LINK_STATE_PENDING
] = "pending",
3870 [LINK_STATE_INITIALIZED
] = "initialized",
3871 [LINK_STATE_CONFIGURING
] = "configuring",
3872 [LINK_STATE_CONFIGURED
] = "configured",
3873 [LINK_STATE_UNMANAGED
] = "unmanaged",
3874 [LINK_STATE_FAILED
] = "failed",
3875 [LINK_STATE_LINGER
] = "linger",
3878 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);