1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
8 #include "alloc-util.h"
10 #include "dhcp-identifier.h"
11 #include "dhcp-lease-internal.h"
15 #include "missing_network.h"
16 #include "netdev/vrf.h"
17 #include "netlink-util.h"
18 #include "network-internal.h"
19 #include "networkd-ipv6-proxy-ndp.h"
20 #include "networkd-lldp-tx.h"
21 #include "networkd-manager.h"
22 #include "networkd-ndisc.h"
23 #include "networkd-neighbor.h"
24 #include "networkd-radv.h"
25 #include "networkd-routing-policy-rule.h"
27 #include "socket-util.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
31 #include "sysctl-util.h"
32 #include "tmpfile-util.h"
33 #include "udev-util.h"
37 uint32_t link_get_vrf_table(Link
*link
) {
38 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
41 uint32_t link_get_dhcp_route_table(Link
*link
) {
42 /* When the interface is part of an VRF use the VRFs routing table, unless
43 * another table is explicitly specified. */
44 if (link
->network
->dhcp_route_table_set
)
45 return link
->network
->dhcp_route_table
;
46 return link_get_vrf_table(link
);
49 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
50 if (link
->network
->ipv6_accept_ra_route_table_set
)
51 return link
->network
->ipv6_accept_ra_route_table
;
52 return link_get_vrf_table(link
);
55 DUID
* link_get_duid(Link
*link
) {
56 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
57 return &link
->network
->duid
;
59 return &link
->manager
->duid
;
62 static bool link_dhcp6_enabled(Link
*link
) {
65 if (!socket_ipv6_is_supported())
68 if (link
->flags
& IFF_LOOPBACK
)
74 if (link
->network
->bond
)
77 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
80 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
83 static bool link_dhcp4_enabled(Link
*link
) {
86 if (link
->flags
& IFF_LOOPBACK
)
92 if (link
->network
->bond
)
95 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
98 static bool link_dhcp4_server_enabled(Link
*link
) {
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (link
->network
->bond
)
110 return link
->network
->dhcp_server
;
113 bool link_ipv4ll_enabled(Link
*link
) {
116 if (link
->flags
& IFF_LOOPBACK
)
122 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
125 if (link
->network
->bond
)
128 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
131 bool link_ipv4ll_fallback_enabled(Link
*link
) {
134 if (link
->flags
& IFF_LOOPBACK
)
140 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
143 if (link
->network
->bond
)
146 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
149 static bool link_ipv6ll_enabled(Link
*link
) {
152 if (!socket_ipv6_is_supported())
155 if (link
->flags
& IFF_LOOPBACK
)
161 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
164 if (link
->network
->bond
)
167 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
170 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
173 static bool link_ipv6_enabled(Link
*link
) {
176 if (!socket_ipv6_is_supported())
179 if (link
->network
->bond
)
182 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
185 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
186 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
189 static bool link_radv_enabled(Link
*link
) {
192 if (!link_ipv6ll_enabled(link
))
195 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
198 static bool link_lldp_rx_enabled(Link
*link
) {
201 if (link
->flags
& IFF_LOOPBACK
)
204 if (link
->iftype
!= ARPHRD_ETHER
)
210 /* LLDP should be handled on bridge slaves as those have a direct
211 * connection to their peers not on the bridge master. Linux doesn't
212 * even (by default) forward lldp packets to the bridge master.*/
213 if (streq_ptr("bridge", link
->kind
))
216 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
219 static bool link_lldp_emit_enabled(Link
*link
) {
222 if (link
->flags
& IFF_LOOPBACK
)
225 if (link
->iftype
!= ARPHRD_ETHER
)
231 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
234 static bool link_ipv4_forward_enabled(Link
*link
) {
237 if (link
->flags
& IFF_LOOPBACK
)
243 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
246 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
249 static bool link_ipv6_forward_enabled(Link
*link
) {
252 if (!socket_ipv6_is_supported())
255 if (link
->flags
& IFF_LOOPBACK
)
261 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
264 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
267 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
270 static bool link_proxy_arp_enabled(Link
*link
) {
273 if (link
->flags
& IFF_LOOPBACK
)
279 if (link
->network
->proxy_arp
< 0)
285 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
288 if (!socket_ipv6_is_supported())
291 if (link
->flags
& IFF_LOOPBACK
)
297 if (!link_ipv6ll_enabled(link
))
300 /* If unset use system default (enabled if local forwarding is disabled.
301 * disabled if local forwarding is enabled).
302 * If set, ignore or enforce RA independent of local forwarding state.
304 if (link
->network
->ipv6_accept_ra
< 0)
305 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
306 return !link_ipv6_forward_enabled(link
);
307 else if (link
->network
->ipv6_accept_ra
> 0)
308 /* accept RA even if ip_forward is enabled */
315 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
318 if (!socket_ipv6_is_supported())
319 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
321 if (link
->flags
& IFF_LOOPBACK
)
322 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
325 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
327 return link
->network
->ipv6_privacy_extensions
;
330 static int link_enable_ipv6(Link
*link
) {
334 if (link
->flags
& IFF_LOOPBACK
)
337 disabled
= !link_ipv6_enabled(link
);
339 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
341 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
343 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
348 static bool link_is_enslaved(Link
*link
) {
349 if (link
->flags
& IFF_SLAVE
)
350 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
353 if (!link
->enslaved_raw
)
359 if (link
->network
->bridge
)
360 /* TODO: support the case when link is not managed by networkd. */
366 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
372 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
375 link_update_operstate(master
, true);
378 void link_update_operstate(Link
*link
, bool also_update_master
) {
379 LinkOperationalState operstate
;
384 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
385 operstate
= LINK_OPERSTATE_DORMANT
;
386 else if (link_has_carrier(link
)) {
388 uint8_t scope
= RT_SCOPE_NOWHERE
;
390 /* if we have carrier, check what addresses we have */
391 SET_FOREACH(address
, link
->addresses
, i
) {
392 if (!address_is_ready(address
))
395 if (address
->scope
< scope
)
396 scope
= address
->scope
;
399 /* for operstate we also take foreign addresses into account */
400 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
401 if (!address_is_ready(address
))
404 if (address
->scope
< scope
)
405 scope
= address
->scope
;
408 if (scope
< RT_SCOPE_SITE
)
409 /* universally accessible addresses found */
410 operstate
= LINK_OPERSTATE_ROUTABLE
;
411 else if (scope
< RT_SCOPE_HOST
)
412 /* only link or site local addresses found */
413 operstate
= LINK_OPERSTATE_DEGRADED
;
415 /* no useful addresses found */
416 operstate
= LINK_OPERSTATE_CARRIER
;
417 } else if (link
->flags
& IFF_UP
)
418 operstate
= LINK_OPERSTATE_NO_CARRIER
;
420 operstate
= LINK_OPERSTATE_OFF
;
422 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
423 link_is_enslaved(link
))
424 operstate
= LINK_OPERSTATE_ENSLAVED
;
426 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
429 SET_FOREACH(slave
, link
->slaves
, i
) {
430 link_update_operstate(slave
, false);
432 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
433 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
437 if (link
->operstate
!= operstate
) {
438 link
->operstate
= operstate
;
439 link_send_changed(link
, "OperationalState", NULL
);
443 if (also_update_master
&& link
->network
) {
444 link_update_master_operstate(link
, link
->network
->bond
);
445 link_update_master_operstate(link
, link
->network
->bridge
);
449 #define FLAG_STRING(string, flag, old, new) \
450 (((old ^ new) & flag) \
451 ? ((old & flag) ? (" -" string) : (" +" string)) \
454 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
455 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
461 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
463 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
465 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
467 /* if we got a message without operstate, take it to mean
468 the state was unchanged */
469 operstate
= link
->kernel_operstate
;
471 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
474 if (link
->flags
!= flags
) {
475 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",
476 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
477 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
478 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
479 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
480 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
481 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
482 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
483 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
484 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
485 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
486 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
487 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
488 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
489 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
490 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
491 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
492 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
493 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
494 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
496 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
497 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
498 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
499 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
500 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
501 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
502 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
503 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
505 /* link flags are currently at most 18 bits, let's align to
507 if (unknown_flags_added
)
509 "Unknown link flags gained: %#.5x (ignoring)",
510 unknown_flags_added
);
512 if (unknown_flags_removed
)
514 "Unknown link flags lost: %#.5x (ignoring)",
515 unknown_flags_removed
);
519 link
->kernel_operstate
= operstate
;
521 link_update_operstate(link
, true);
526 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
527 _cleanup_(link_unrefp
) Link
*link
= NULL
;
529 const char *ifname
, *kind
= NULL
;
531 unsigned short iftype
;
537 /* check for link kind */
538 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
540 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
541 r
= sd_netlink_message_exit_container(message
);
546 r
= sd_netlink_message_get_type(message
, &type
);
549 else if (type
!= RTM_NEWLINK
)
552 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
555 else if (ifindex
<= 0)
558 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
562 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
573 .state
= LINK_STATE_PENDING
,
574 .rtnl_extended_attrs
= true,
579 link
->ifname
= strdup(ifname
);
584 link
->kind
= strdup(kind
);
589 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
591 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
593 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
595 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
597 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
600 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
603 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
606 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
610 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
614 r
= link_update_flags(link
, message
);
618 *ret
= TAKE_PTR(link
);
623 static Link
*link_free(Link
*link
) {
628 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
629 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
631 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
632 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
634 while ((address
= link
->pool_addresses
)) {
635 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
636 address_free(address
);
639 sd_dhcp_server_unref(link
->dhcp_server
);
640 sd_dhcp_client_unref(link
->dhcp_client
);
641 sd_dhcp_lease_unref(link
->dhcp_lease
);
643 link_lldp_emit_stop(link
);
645 free(link
->lease_file
);
647 sd_lldp_unref(link
->lldp
);
648 free(link
->lldp_file
);
652 sd_ipv4ll_unref(link
->ipv4ll
);
653 sd_dhcp6_client_unref(link
->dhcp6_client
);
654 sd_ndisc_unref(link
->ndisc
);
655 sd_radv_unref(link
->radv
);
660 (void) unlink(link
->state_file
);
661 free(link
->state_file
);
663 sd_device_unref(link
->sd_device
);
665 hashmap_free(link
->bound_to_links
);
666 hashmap_free(link
->bound_by_links
);
668 set_free_with_destructor(link
->slaves
, link_unref
);
670 network_unref(link
->network
);
675 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
677 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
684 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
693 static void link_set_state(Link
*link
, LinkState state
) {
696 if (link
->state
== state
)
699 log_link_debug(link
, "State changed: %s -> %s",
700 link_state_to_string(link
->state
),
701 link_state_to_string(state
));
705 link_send_changed(link
, "AdministrativeState", NULL
);
708 static void link_enter_unmanaged(Link
*link
) {
711 log_link_debug(link
, "Unmanaged");
713 link_set_state(link
, LINK_STATE_UNMANAGED
);
718 int link_stop_clients(Link
*link
) {
722 assert(link
->manager
);
723 assert(link
->manager
->event
);
725 if (link
->dhcp_client
) {
726 k
= sd_dhcp_client_stop(link
->dhcp_client
);
728 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
732 k
= sd_ipv4ll_stop(link
->ipv4ll
);
734 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
737 if (link
->dhcp6_client
) {
738 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
740 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
744 k
= sd_ndisc_stop(link
->ndisc
);
746 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
750 k
= sd_radv_stop(link
->radv
);
752 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
755 link_lldp_emit_stop(link
);
759 void link_enter_failed(Link
*link
) {
762 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
765 log_link_warning(link
, "Failed");
767 link_set_state(link
, LINK_STATE_FAILED
);
769 link_stop_clients(link
);
774 static Address
* link_find_dhcp_server_address(Link
*link
) {
778 assert(link
->network
);
780 /* The first statically configured address if there is any */
781 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
783 if (address
->family
!= AF_INET
)
786 if (in_addr_is_null(address
->family
, &address
->in_addr
))
792 /* If that didn't work, find a suitable address we got from the pool */
793 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
794 if (address
->family
!= AF_INET
)
803 static int link_join_netdevs_after_configured(Link
*link
) {
808 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
809 if (netdev
->ifindex
> 0)
810 /* Assume already enslaved. */
813 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
816 log_struct(LOG_DEBUG
,
817 LOG_LINK_INTERFACE(link
),
818 LOG_NETDEV_INTERFACE(netdev
),
819 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
821 r
= netdev_join(netdev
, link
, NULL
);
823 return log_struct_errno(LOG_WARNING
, r
,
824 LOG_LINK_INTERFACE(link
),
825 LOG_NETDEV_INTERFACE(netdev
),
826 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
832 static void link_enter_configured(Link
*link
) {
834 assert(link
->network
);
836 if (link
->state
!= LINK_STATE_CONFIGURING
)
839 log_link_info(link
, "Configured");
841 link_set_state(link
, LINK_STATE_CONFIGURED
);
843 (void) link_join_netdevs_after_configured(link
);
848 static int link_request_set_routing_policy_rule(Link
*link
) {
849 RoutingPolicyRule
*rule
, *rrule
= NULL
;
853 assert(link
->network
);
855 link_set_state(link
, LINK_STATE_CONFIGURING
);
856 link
->routing_policy_rules_configured
= false;
858 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
859 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
860 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
861 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
863 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
867 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
869 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
870 link_enter_failed(link
);
874 link
->routing_policy_rule_messages
++;
877 routing_policy_rule_purge(link
->manager
, link
);
878 if (link
->routing_policy_rule_messages
== 0) {
879 link
->routing_policy_rules_configured
= true;
880 link_check_ready(link
);
882 log_link_debug(link
, "Setting routing policy rules");
887 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
891 assert(link
->route_messages
> 0);
892 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
893 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
895 link
->route_messages
--;
897 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
900 r
= sd_netlink_message_get_errno(m
);
901 if (r
< 0 && r
!= -EEXIST
)
902 log_link_warning_errno(link
, r
, "Could not set route: %m");
904 if (link
->route_messages
== 0) {
905 log_link_debug(link
, "Routes set");
906 link
->static_routes_configured
= true;
907 link_check_ready(link
);
913 int link_request_set_routes(Link
*link
) {
915 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
916 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
923 assert(link
->network
);
924 assert(link
->addresses_configured
);
925 assert(link
->address_messages
== 0);
926 assert(link
->state
!= _LINK_STATE_INVALID
);
928 link_set_state(link
, LINK_STATE_CONFIGURING
);
929 link
->static_routes_configured
= false;
931 r
= link_request_set_routing_policy_rule(link
);
935 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
936 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
937 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
939 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
942 r
= route_configure(rt
, link
, route_handler
);
944 log_link_warning_errno(link
, r
, "Could not set routes: %m");
945 link_enter_failed(link
);
949 link
->route_messages
++;
952 if (link
->route_messages
== 0) {
953 link
->static_routes_configured
= true;
954 link_check_ready(link
);
956 log_link_debug(link
, "Setting routes");
961 void link_check_ready(Link
*link
) {
967 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
973 if (!link
->addresses_configured
)
976 if (!link
->neighbors_configured
)
979 SET_FOREACH(a
, link
->addresses
, i
)
980 if (!address_is_ready(a
))
983 if (!link
->addresses_ready
) {
984 link
->addresses_ready
= true;
985 link_request_set_routes(link
);
988 if (!link
->static_routes_configured
)
991 if (!link
->routing_policy_rules_configured
)
994 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
997 if (link_ipv6ll_enabled(link
) &&
998 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1001 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1002 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
1003 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1004 /* When DHCP is enabled, at least one protocol must provide an address, or
1005 * an IPv4ll fallback address must be configured. */
1008 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1011 if (link
->state
!= LINK_STATE_CONFIGURED
)
1012 link_enter_configured(link
);
1017 static int link_request_set_neighbors(Link
*link
) {
1022 assert(link
->network
);
1023 assert(link
->state
!= _LINK_STATE_INVALID
);
1025 link_set_state(link
, LINK_STATE_CONFIGURING
);
1026 link
->neighbors_configured
= false;
1028 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1029 r
= neighbor_configure(neighbor
, link
, NULL
);
1031 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1032 link_enter_failed(link
);
1037 if (link
->neighbor_messages
== 0) {
1038 link
->neighbors_configured
= true;
1039 link_check_ready(link
);
1041 log_link_debug(link
, "Setting neighbors");
1046 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1052 assert(link
->ifname
);
1053 assert(link
->address_messages
> 0);
1054 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1055 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1057 link
->address_messages
--;
1059 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1062 r
= sd_netlink_message_get_errno(m
);
1063 if (r
< 0 && r
!= -EEXIST
)
1064 log_link_warning_errno(link
, r
, "could not set address: %m");
1066 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1068 if (link
->address_messages
== 0) {
1069 log_link_debug(link
, "Addresses set");
1070 link
->addresses_configured
= true;
1071 link_check_ready(link
);
1077 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1078 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1079 size_t n_addresses
= 0, n_allocated
= 0;
1082 log_debug("Copying DNS server information from %s", link
->ifname
);
1087 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1090 /* Only look for IPv4 addresses */
1091 if (link
->network
->dns
[i
].family
!= AF_INET
)
1094 ia
= link
->network
->dns
[i
].address
.in
;
1096 /* Never propagate obviously borked data */
1097 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1100 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1103 addresses
[n_addresses
++] = ia
;
1106 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1107 const struct in_addr
*da
= NULL
;
1110 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1113 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1116 for (j
= 0; j
< n
; j
++)
1117 if (in4_addr_is_non_local(&da
[j
]))
1118 addresses
[n_addresses
++] = da
[j
];
1122 if (n_addresses
<= 0)
1125 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1128 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1129 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1130 size_t n_addresses
= 0, n_allocated
= 0;
1136 log_debug("Copying NTP server information from %s", link
->ifname
);
1138 STRV_FOREACH(a
, link
->network
->ntp
) {
1141 /* Only look for IPv4 addresses */
1142 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1145 /* Never propagate obviously borked data */
1146 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1149 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1152 addresses
[n_addresses
++] = ia
;
1155 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1156 const struct in_addr
*da
= NULL
;
1159 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1162 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1165 for (j
= 0; j
< n
; j
++)
1166 if (in4_addr_is_non_local(&da
[j
]))
1167 addresses
[n_addresses
++] = da
[j
];
1171 if (n_addresses
<= 0)
1174 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1177 static int link_set_bridge_fdb(Link
*link
) {
1178 FdbEntry
*fdb_entry
;
1181 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1182 r
= fdb_entry_configure(link
, fdb_entry
);
1184 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1190 static int link_request_set_addresses(Link
*link
) {
1191 AddressLabel
*label
;
1196 assert(link
->network
);
1197 assert(link
->state
!= _LINK_STATE_INVALID
);
1199 link_set_state(link
, LINK_STATE_CONFIGURING
);
1201 /* Reset all *_configured flags we are configuring. */
1202 link
->addresses_configured
= false;
1203 link
->addresses_ready
= false;
1204 link
->neighbors_configured
= false;
1205 link
->static_routes_configured
= false;
1206 link
->routing_policy_rules_configured
= false;
1208 r
= link_set_bridge_fdb(link
);
1212 r
= link_request_set_neighbors(link
);
1216 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1219 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1221 r
= address_configure(ad
, link
, address_handler
, update
);
1223 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1224 link_enter_failed(link
);
1228 link
->address_messages
++;
1231 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1232 r
= address_label_configure(label
, link
, NULL
, false);
1234 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1235 link_enter_failed(link
);
1239 link
->address_label_messages
++;
1242 /* now that we can figure out a default address for the dhcp server,
1244 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1246 Link
*uplink
= NULL
;
1247 bool acquired_uplink
= false;
1249 address
= link_find_dhcp_server_address(link
);
1251 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1252 link_enter_failed(link
);
1256 /* use the server address' subnet as the pool */
1257 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1258 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1263 r = sd_dhcp_server_set_router(link->dhcp_server,
1264 &main_address->in_addr.in);
1269 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1270 r
= sd_dhcp_server_set_max_lease_time(
1272 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1277 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1278 r
= sd_dhcp_server_set_default_lease_time(
1280 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1285 if (link
->network
->dhcp_server_emit_dns
) {
1287 if (link
->network
->n_dhcp_server_dns
> 0)
1288 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1290 uplink
= manager_find_uplink(link
->manager
, link
);
1291 acquired_uplink
= true;
1294 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1297 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1300 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1303 if (link
->network
->dhcp_server_emit_ntp
) {
1305 if (link
->network
->n_dhcp_server_ntp
> 0)
1306 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1308 if (!acquired_uplink
)
1309 uplink
= manager_find_uplink(link
->manager
, link
);
1312 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1315 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1319 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1322 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1324 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1326 if (link
->network
->dhcp_server_emit_timezone
) {
1327 _cleanup_free_
char *buffer
= NULL
;
1328 const char *tz
= NULL
;
1330 if (link
->network
->dhcp_server_timezone
)
1331 tz
= link
->network
->dhcp_server_timezone
;
1333 r
= get_timezone(&buffer
);
1335 log_warning_errno(r
, "Failed to determine timezone: %m");
1341 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1346 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1347 r
= sd_dhcp_server_start(link
->dhcp_server
);
1349 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1351 link_enter_failed(link
);
1357 log_link_debug(link
, "Offering DHCPv4 leases");
1360 if (link
->address_messages
== 0) {
1361 link
->addresses_configured
= true;
1362 link_check_ready(link
);
1364 log_link_debug(link
, "Setting addresses");
1369 static int link_set_bridge_vlan(Link
*link
) {
1372 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1374 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1379 static int link_set_proxy_arp(Link
*link
) {
1382 if (!link_proxy_arp_enabled(link
))
1385 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1387 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1392 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1397 log_link_debug(link
, "Set link");
1399 r
= sd_netlink_message_get_errno(m
);
1400 if (r
< 0 && r
!= -EEXIST
) {
1401 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1402 link_enter_failed(link
);
1408 static int link_configure_after_setting_mtu(Link
*link
);
1410 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1415 assert(link
->ifname
);
1417 link
->setting_mtu
= false;
1419 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1422 r
= sd_netlink_message_get_errno(m
);
1424 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1428 log_link_debug(link
, "Setting MTU done.");
1430 if (link
->state
== LINK_STATE_INITIALIZED
)
1431 (void) link_configure_after_setting_mtu(link
);
1436 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1437 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1441 assert(link
->manager
);
1442 assert(link
->manager
->rtnl
);
1444 if (mtu
== 0 || link
->setting_mtu
)
1447 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1450 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1452 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1454 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1456 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1457 * for this interface, then disable IPv6 else enable it. */
1458 (void) link_enable_ipv6(link
);
1460 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1461 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1462 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1464 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1465 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1470 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1472 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1474 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1475 link_netlink_destroy_callback
, link
);
1477 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1480 link
->setting_mtu
= true;
1485 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1490 assert(link
->ifname
);
1492 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1495 r
= sd_netlink_message_get_errno(m
);
1497 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1502 static int link_set_flags(Link
*link
) {
1503 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1504 unsigned ifi_change
= 0;
1505 unsigned ifi_flags
= 0;
1509 assert(link
->manager
);
1510 assert(link
->manager
->rtnl
);
1512 if (link
->flags
& IFF_LOOPBACK
)
1518 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1521 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1523 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1525 if (link
->network
->arp
>= 0) {
1526 ifi_change
|= IFF_NOARP
;
1527 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1530 if (link
->network
->multicast
>= 0) {
1531 ifi_change
|= IFF_MULTICAST
;
1532 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1535 if (link
->network
->allmulticast
>= 0) {
1536 ifi_change
|= IFF_ALLMULTI
;
1537 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1540 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1542 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1544 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1545 link_netlink_destroy_callback
, link
);
1547 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1554 static int link_set_bridge(Link
*link
) {
1555 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1559 assert(link
->network
);
1561 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1563 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1565 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1567 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1569 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1571 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1573 if (link
->network
->use_bpdu
>= 0) {
1574 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1576 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1579 if (link
->network
->hairpin
>= 0) {
1580 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1582 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1585 if (link
->network
->fast_leave
>= 0) {
1586 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1588 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1591 if (link
->network
->allow_port_to_be_root
>= 0) {
1592 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1594 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1598 if (link
->network
->unicast_flood
>= 0) {
1599 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1601 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1604 if (link
->network
->multicast_flood
>= 0) {
1605 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
1607 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
1610 if (link
->network
->multicast_to_unicast
>= 0) {
1611 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1613 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1616 if (link
->network
->neighbor_suppression
>= 0) {
1617 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
1619 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
1622 if (link
->network
->learning
>= 0) {
1623 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
1625 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
1628 if (link
->network
->cost
!= 0) {
1629 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1631 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1634 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1635 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1637 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1640 r
= sd_netlink_message_close_container(req
);
1642 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1644 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1645 link_netlink_destroy_callback
, link
);
1647 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1654 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1659 assert(link
->ifname
);
1661 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1664 r
= sd_netlink_message_get_errno(m
);
1666 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1673 static int link_set_bond(Link
*link
) {
1674 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1678 assert(link
->network
);
1680 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1682 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1684 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1686 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1688 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1690 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1692 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1694 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1696 if (link
->network
->active_slave
) {
1697 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1699 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1702 if (link
->network
->primary_slave
) {
1703 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1705 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1708 r
= sd_netlink_message_close_container(req
);
1710 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1712 r
= sd_netlink_message_close_container(req
);
1714 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1716 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1717 link_netlink_destroy_callback
, link
);
1719 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1726 static int link_lldp_save(Link
*link
) {
1727 _cleanup_free_
char *temp_path
= NULL
;
1728 _cleanup_fclose_
FILE *f
= NULL
;
1729 sd_lldp_neighbor
**l
= NULL
;
1733 assert(link
->lldp_file
);
1736 (void) unlink(link
->lldp_file
);
1740 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1744 (void) unlink(link
->lldp_file
);
1750 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1754 fchmod(fileno(f
), 0644);
1756 for (i
= 0; i
< n
; i
++) {
1761 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1766 (void) fwrite(&u
, 1, sizeof(u
), f
);
1767 (void) fwrite(p
, 1, sz
, f
);
1770 r
= fflush_and_check(f
);
1774 if (rename(temp_path
, link
->lldp_file
) < 0) {
1781 (void) unlink(link
->lldp_file
);
1783 (void) unlink(temp_path
);
1785 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1789 for (i
= 0; i
< n
; i
++)
1790 sd_lldp_neighbor_unref(l
[i
]);
1797 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1798 Link
*link
= userdata
;
1803 (void) link_lldp_save(link
);
1805 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1806 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1808 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1810 r
= link_lldp_emit_start(link
);
1812 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1816 static int link_acquire_ipv6_conf(Link
*link
) {
1821 if (link_ipv6_accept_ra_enabled(link
)) {
1822 assert(link
->ndisc
);
1824 log_link_debug(link
, "Discovering IPv6 routers");
1826 r
= sd_ndisc_start(link
->ndisc
);
1827 if (r
< 0 && r
!= -EBUSY
)
1828 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1831 if (link_radv_enabled(link
)) {
1833 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1835 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1837 r
= sd_radv_start(link
->radv
);
1838 if (r
< 0 && r
!= -EBUSY
)
1839 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1842 (void) dhcp6_request_prefix_delegation(link
);
1847 static int link_acquire_ipv4_conf(Link
*link
) {
1851 assert(link
->manager
);
1852 assert(link
->manager
->event
);
1854 if (link_ipv4ll_enabled(link
)) {
1855 assert(link
->ipv4ll
);
1857 log_link_debug(link
, "Acquiring IPv4 link-local address");
1859 r
= sd_ipv4ll_start(link
->ipv4ll
);
1861 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1864 if (link_dhcp4_enabled(link
)) {
1865 assert(link
->dhcp_client
);
1867 log_link_debug(link
, "Acquiring DHCPv4 lease");
1869 r
= sd_dhcp_client_start(link
->dhcp_client
);
1871 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1877 static int link_acquire_conf(Link
*link
) {
1882 r
= link_acquire_ipv4_conf(link
);
1886 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1887 r
= link_acquire_ipv6_conf(link
);
1892 if (link_lldp_emit_enabled(link
)) {
1893 r
= link_lldp_emit_start(link
);
1895 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1901 bool link_has_carrier(Link
*link
) {
1902 /* see Documentation/networking/operstates.txt in the kernel sources */
1904 if (link
->kernel_operstate
== IF_OPER_UP
)
1907 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1908 /* operstate may not be implemented, so fall back to flags */
1909 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1915 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1920 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1923 r
= sd_netlink_message_get_errno(m
);
1925 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1930 static int link_configure_addrgen_mode(Link
*link
) {
1931 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1932 uint8_t ipv6ll_mode
;
1936 assert(link
->network
);
1937 assert(link
->manager
);
1938 assert(link
->manager
->rtnl
);
1940 log_link_debug(link
, "Setting address genmode for link");
1942 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1944 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1946 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1948 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1950 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1952 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1954 if (!link_ipv6ll_enabled(link
))
1955 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1956 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1957 /* The file may not exist. And event if it exists, when stable_secret is unset,
1958 * reading the file fails with EIO. */
1959 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1961 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1963 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1965 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1967 r
= sd_netlink_message_close_container(req
);
1969 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1971 r
= sd_netlink_message_close_container(req
);
1973 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1975 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1976 link_netlink_destroy_callback
, link
);
1978 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1985 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1990 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1993 r
= sd_netlink_message_get_errno(m
);
1995 /* we warn but don't fail the link, as it may be brought up later */
1996 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
2001 static int link_up(Link
*link
) {
2002 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2006 assert(link
->network
);
2007 assert(link
->manager
);
2008 assert(link
->manager
->rtnl
);
2010 log_link_debug(link
, "Bringing link up");
2012 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2014 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2016 /* set it free if not enslaved with networkd */
2017 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
2018 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
2020 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
2023 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2025 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2027 if (link
->network
->mac
) {
2028 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
2030 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2033 if (link_ipv6_enabled(link
)) {
2034 uint8_t ipv6ll_mode
;
2036 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2038 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2040 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2041 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2043 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2045 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2046 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2048 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2051 if (!link_ipv6ll_enabled(link
))
2052 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
2053 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
2054 /* The file may not exist. And event if it exists, when stable_secret is unset,
2055 * reading the file fails with EIO. */
2056 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
2058 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
2060 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
2062 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
2064 r
= sd_netlink_message_close_container(req
);
2066 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2068 r
= sd_netlink_message_close_container(req
);
2070 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2073 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2074 link_netlink_destroy_callback
, link
);
2076 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2083 static int link_up_can(Link
*link
) {
2084 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2089 log_link_debug(link
, "Bringing CAN link up");
2091 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2093 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2095 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2097 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2099 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2100 link_netlink_destroy_callback
, link
);
2102 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2109 static int link_set_can(Link
*link
) {
2110 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2114 assert(link
->network
);
2115 assert(link
->manager
);
2116 assert(link
->manager
->rtnl
);
2118 log_link_debug(link
, "link_set_can");
2120 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2122 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2124 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2126 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2128 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2130 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2132 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2134 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2136 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2137 struct can_bittiming bt
= {
2138 .bitrate
= link
->network
->can_bitrate
,
2139 .sample_point
= link
->network
->can_sample_point
,
2142 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2143 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2147 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2148 if (link
->network
->can_sample_point
> 0)
2149 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2151 log_link_debug(link
, "Using default sample point");
2153 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2155 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2158 if (link
->network
->can_restart_us
> 0) {
2159 char time_string
[FORMAT_TIMESPAN_MAX
];
2160 uint64_t restart_ms
;
2162 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2165 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2167 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2169 if (restart_ms
> UINT32_MAX
) {
2170 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2174 log_link_debug(link
, "Setting restart = %s", time_string
);
2176 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2178 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2181 if (link
->network
->can_triple_sampling
>= 0) {
2182 struct can_ctrlmode cm
= {
2183 .mask
= CAN_CTRLMODE_3_SAMPLES
,
2184 .flags
= link
->network
->can_triple_sampling
? CAN_CTRLMODE_3_SAMPLES
: 0,
2187 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
2189 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
2191 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
2194 r
= sd_netlink_message_close_container(m
);
2196 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2198 r
= sd_netlink_message_close_container(m
);
2200 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2202 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2203 link_netlink_destroy_callback
, link
);
2205 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2209 if (!(link
->flags
& IFF_UP
)) {
2210 r
= link_up_can(link
);
2212 link_enter_failed(link
);
2217 log_link_debug(link
, "link_set_can done");
2222 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2227 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2230 r
= sd_netlink_message_get_errno(m
);
2232 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2234 if (streq_ptr(link
->kind
, "can"))
2240 int link_down(Link
*link
) {
2241 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2245 assert(link
->manager
);
2246 assert(link
->manager
->rtnl
);
2248 log_link_debug(link
, "Bringing link down");
2250 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2251 RTM_SETLINK
, link
->ifindex
);
2253 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2255 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2257 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2259 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2260 link_netlink_destroy_callback
, link
);
2262 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2269 static int link_handle_bound_to_list(Link
*link
) {
2273 bool required_up
= false;
2274 bool link_is_up
= false;
2278 if (hashmap_isempty(link
->bound_to_links
))
2281 if (link
->flags
& IFF_UP
)
2284 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2285 if (link_has_carrier(l
)) {
2290 if (!required_up
&& link_is_up
) {
2291 r
= link_down(link
);
2294 } else if (required_up
&& !link_is_up
) {
2303 static int link_handle_bound_by_list(Link
*link
) {
2310 if (hashmap_isempty(link
->bound_by_links
))
2313 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2314 r
= link_handle_bound_to_list(l
);
2322 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2328 if (link
== carrier
)
2331 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2334 r
= hashmap_ensure_allocated(h
, NULL
);
2338 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2345 static int link_new_bound_by_list(Link
*link
) {
2350 bool list_updated
= false;
2353 assert(link
->manager
);
2357 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2358 if (!carrier
->network
)
2361 if (strv_isempty(carrier
->network
->bind_carrier
))
2364 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2365 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2369 list_updated
= true;
2376 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2377 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2381 link_dirty(carrier
);
2387 static int link_new_bound_to_list(Link
*link
) {
2392 bool list_updated
= false;
2395 assert(link
->manager
);
2400 if (strv_isempty(link
->network
->bind_carrier
))
2405 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2406 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2407 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2411 list_updated
= true;
2418 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2419 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2423 link_dirty(carrier
);
2429 static int link_new_carrier_maps(Link
*link
) {
2432 r
= link_new_bound_by_list(link
);
2436 r
= link_handle_bound_by_list(link
);
2440 r
= link_new_bound_to_list(link
);
2444 r
= link_handle_bound_to_list(link
);
2451 static void link_free_bound_to_list(Link
*link
) {
2455 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2456 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2458 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2459 link_dirty(bound_to
);
2465 static void link_free_bound_by_list(Link
*link
) {
2469 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2470 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2472 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2473 link_dirty(bound_by
);
2474 link_handle_bound_to_list(bound_by
);
2481 static void link_free_carrier_maps(Link
*link
) {
2482 bool list_updated
= false;
2486 if (!hashmap_isempty(link
->bound_to_links
)) {
2487 link_free_bound_to_list(link
);
2488 list_updated
= true;
2491 if (!hashmap_isempty(link
->bound_by_links
)) {
2492 link_free_bound_by_list(link
);
2493 list_updated
= true;
2502 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2509 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2513 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2517 r
= set_put(master
->slaves
, link
);
2525 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2530 if (!link
->manager
|| !netdev
)
2533 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2536 link_unref(set_remove(master
->slaves
, link
));
2539 static void link_detach_from_manager(Link
*link
) {
2540 if (!link
|| !link
->manager
)
2543 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2546 /* The following must be called at last. */
2547 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2551 void link_drop(Link
*link
) {
2552 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2555 link_set_state(link
, LINK_STATE_LINGER
);
2557 link_free_carrier_maps(link
);
2559 if (link
->network
) {
2560 link_drop_from_master(link
, link
->network
->bridge
);
2561 link_drop_from_master(link
, link
->network
->bond
);
2564 log_link_debug(link
, "Link removed");
2566 (void) unlink(link
->state_file
);
2567 link_detach_from_manager(link
);
2570 static int link_joined(Link
*link
) {
2574 assert(link
->network
);
2576 if (!hashmap_isempty(link
->bound_to_links
)) {
2577 r
= link_handle_bound_to_list(link
);
2580 } else if (!(link
->flags
& IFF_UP
)) {
2583 link_enter_failed(link
);
2588 if (link
->network
->bridge
) {
2589 r
= link_set_bridge(link
);
2591 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2593 r
= link_append_to_master(link
, link
->network
->bridge
);
2595 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2598 if (link
->network
->bond
) {
2599 r
= link_set_bond(link
);
2601 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2603 r
= link_append_to_master(link
, link
->network
->bond
);
2605 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2608 if (link
->network
->use_br_vlan
&&
2609 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2610 r
= link_set_bridge_vlan(link
);
2612 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2615 /* Skip setting up addresses until it gets carrier,
2616 or it would try to set addresses twice,
2617 which is bad for non-idempotent steps. */
2618 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2621 return link_request_set_addresses(link
);
2624 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2628 assert(link
->network
);
2629 assert(link
->enslaving
> 0);
2630 assert(!link
->enslaved_raw
);
2634 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2637 r
= sd_netlink_message_get_errno(m
);
2638 if (r
< 0 && r
!= -EEXIST
) {
2639 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2640 link_enter_failed(link
);
2643 log_link_debug(link
, "Joined netdev");
2645 if (link
->enslaving
== 0) {
2646 link
->enslaved_raw
= true;
2653 static int link_enter_join_netdev(Link
*link
) {
2659 assert(link
->network
);
2660 assert(link
->state
== LINK_STATE_INITIALIZED
);
2662 link_set_state(link
, LINK_STATE_CONFIGURING
);
2665 link
->enslaving
= 0;
2666 link
->enslaved_raw
= false;
2668 if (link
->network
->bond
) {
2669 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2670 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2671 return link_joined(link
);
2673 log_struct(LOG_DEBUG
,
2674 LOG_LINK_INTERFACE(link
),
2675 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2676 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2680 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2682 log_struct_errno(LOG_WARNING
, r
,
2683 LOG_LINK_INTERFACE(link
),
2684 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2685 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2686 link_enter_failed(link
);
2691 if (link
->network
->bridge
) {
2692 log_struct(LOG_DEBUG
,
2693 LOG_LINK_INTERFACE(link
),
2694 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2695 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2699 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2701 log_struct_errno(LOG_WARNING
, r
,
2702 LOG_LINK_INTERFACE(link
),
2703 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2704 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2705 link_enter_failed(link
);
2710 if (link
->network
->vrf
) {
2711 log_struct(LOG_DEBUG
,
2712 LOG_LINK_INTERFACE(link
),
2713 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2714 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2718 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2720 log_struct_errno(LOG_WARNING
, r
,
2721 LOG_LINK_INTERFACE(link
),
2722 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2723 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2724 link_enter_failed(link
);
2729 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2731 if (netdev
->ifindex
> 0)
2732 /* Assume already enslaved. */
2735 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2738 log_struct(LOG_DEBUG
,
2739 LOG_LINK_INTERFACE(link
),
2740 LOG_NETDEV_INTERFACE(netdev
),
2741 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2745 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2747 log_struct_errno(LOG_WARNING
, r
,
2748 LOG_LINK_INTERFACE(link
),
2749 LOG_NETDEV_INTERFACE(netdev
),
2750 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2751 link_enter_failed(link
);
2756 if (link
->enslaving
== 0)
2757 return link_joined(link
);
2762 static int link_set_ipv4_forward(Link
*link
) {
2765 if (!link_ipv4_forward_enabled(link
))
2768 /* We propagate the forwarding flag from one interface to the
2769 * global setting one way. This means: as long as at least one
2770 * interface was configured at any time that had IP forwarding
2771 * enabled the setting will stay on for good. We do this
2772 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2773 * somewhat in sync (see below). */
2775 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2777 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2782 static int link_set_ipv6_forward(Link
*link
) {
2785 if (!link_ipv6_forward_enabled(link
))
2788 /* On Linux, the IPv6 stack does not know a per-interface
2789 * packet forwarding setting: either packet forwarding is on
2790 * for all, or off for all. We hence don't bother with a
2791 * per-interface setting, but simply propagate the interface
2792 * flag, if it is set, to the global flag, one-way. Note that
2793 * while IPv4 would allow a per-interface flag, we expose the
2794 * same behaviour there and also propagate the setting from
2795 * one to all, to keep things simple (see above). */
2797 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2799 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2804 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2805 IPv6PrivacyExtensions s
;
2808 s
= link_ipv6_privacy_extensions(link
);
2812 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2814 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2819 static int link_set_ipv6_accept_ra(Link
*link
) {
2822 /* Make this a NOP if IPv6 is not available */
2823 if (!socket_ipv6_is_supported())
2826 if (link
->flags
& IFF_LOOPBACK
)
2832 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2834 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2839 static int link_set_ipv6_dad_transmits(Link
*link
) {
2842 /* Make this a NOP if IPv6 is not available */
2843 if (!socket_ipv6_is_supported())
2846 if (link
->flags
& IFF_LOOPBACK
)
2852 if (link
->network
->ipv6_dad_transmits
< 0)
2855 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2857 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2862 static int link_set_ipv6_hop_limit(Link
*link
) {
2865 /* Make this a NOP if IPv6 is not available */
2866 if (!socket_ipv6_is_supported())
2869 if (link
->flags
& IFF_LOOPBACK
)
2875 if (link
->network
->ipv6_hop_limit
< 0)
2878 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2880 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2885 static int link_set_ipv6_mtu(Link
*link
) {
2888 /* Make this a NOP if IPv6 is not available */
2889 if (!socket_ipv6_is_supported())
2892 if (link
->flags
& IFF_LOOPBACK
)
2895 if (link
->network
->ipv6_mtu
== 0)
2898 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2900 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2905 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2906 Address
*net_address
;
2914 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2915 if (address_equal(net_address
, address
))
2921 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2930 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2931 if (route_equal(net_route
, route
))
2937 static int link_drop_foreign_config(Link
*link
) {
2943 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2944 /* we consider IPv6LL addresses to be managed by the kernel */
2945 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2948 if (link_is_static_address_configured(link
, address
)) {
2949 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2951 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2953 r
= address_remove(address
, link
, NULL
);
2959 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2960 /* do not touch routes managed by the kernel */
2961 if (route
->protocol
== RTPROT_KERNEL
)
2964 if (link_is_static_route_configured(link
, route
)) {
2965 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2969 r
= route_remove(route
, link
, NULL
);
2978 static int link_drop_config(Link
*link
) {
2979 Address
*address
, *pool_address
;
2984 SET_FOREACH(address
, link
->addresses
, i
) {
2985 /* we consider IPv6LL addresses to be managed by the kernel */
2986 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2989 r
= address_remove(address
, link
, NULL
);
2993 /* If this address came from an address pool, clean up the pool */
2994 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2995 if (address_equal(address
, pool_address
)) {
2996 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2997 address_free(pool_address
);
3003 SET_FOREACH(route
, link
->routes
, i
) {
3004 /* do not touch routes managed by the kernel */
3005 if (route
->protocol
== RTPROT_KERNEL
)
3008 r
= route_remove(route
, link
, NULL
);
3018 static int link_update_lldp(Link
*link
) {
3026 if (link
->flags
& IFF_UP
) {
3027 r
= sd_lldp_start(link
->lldp
);
3029 log_link_debug(link
, "Started LLDP.");
3031 r
= sd_lldp_stop(link
->lldp
);
3033 log_link_debug(link
, "Stopped LLDP.");
3039 static int link_configure_can(Link
*link
) {
3042 if (streq_ptr(link
->kind
, "can")) {
3043 /* The CAN interface must be down to configure bitrate, etc... */
3044 if ((link
->flags
& IFF_UP
)) {
3045 r
= link_down(link
);
3047 link_enter_failed(link
);
3054 return link_set_can(link
);
3057 if (!(link
->flags
& IFF_UP
)) {
3058 r
= link_up_can(link
);
3060 link_enter_failed(link
);
3068 static int link_configure(Link
*link
) {
3072 assert(link
->network
);
3073 assert(link
->state
== LINK_STATE_INITIALIZED
);
3075 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
3076 return link_configure_can(link
);
3078 /* Drop foreign config, but ignore loopback or critical devices.
3079 * We do not want to remove loopback address or addresses used for root NFS. */
3080 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
3081 r
= link_drop_foreign_config(link
);
3086 r
= link_set_proxy_arp(link
);
3090 r
= ipv6_proxy_ndp_addresses_configure(link
);
3094 r
= link_set_ipv4_forward(link
);
3098 r
= link_set_ipv6_forward(link
);
3102 r
= link_set_ipv6_privacy_extensions(link
);
3106 r
= link_set_ipv6_accept_ra(link
);
3110 r
= link_set_ipv6_dad_transmits(link
);
3114 r
= link_set_ipv6_hop_limit(link
);
3118 r
= link_set_flags(link
);
3122 r
= link_set_ipv6_mtu(link
);
3126 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
3127 r
= ipv4ll_configure(link
);
3132 if (link_dhcp4_enabled(link
)) {
3133 r
= dhcp4_set_promote_secondaries(link
);
3137 r
= dhcp4_configure(link
);
3142 if (link_dhcp4_server_enabled(link
)) {
3143 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3147 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3152 if (link_dhcp6_enabled(link
) ||
3153 link_ipv6_accept_ra_enabled(link
)) {
3154 r
= dhcp6_configure(link
);
3159 if (link_ipv6_accept_ra_enabled(link
)) {
3160 r
= ndisc_configure(link
);
3165 if (link_radv_enabled(link
)) {
3166 r
= radv_configure(link
);
3171 if (link_lldp_rx_enabled(link
)) {
3172 r
= sd_lldp_new(&link
->lldp
);
3176 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3180 r
= sd_lldp_match_capabilities(link
->lldp
,
3181 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3182 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3183 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3187 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3191 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3195 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3199 r
= link_update_lldp(link
);
3204 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
3208 if (socket_ipv6_is_supported()) {
3209 r
= link_configure_addrgen_mode(link
);
3214 return link_configure_after_setting_mtu(link
);
3217 static int link_configure_after_setting_mtu(Link
*link
) {
3221 assert(link
->network
);
3222 assert(link
->state
== LINK_STATE_INITIALIZED
);
3224 if (link
->setting_mtu
)
3227 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3228 r
= link_acquire_conf(link
);
3233 return link_enter_join_netdev(link
);
3236 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3239 if (duid
->raw_data_len
> 0)
3242 if (duid
->type
!= DUID_TYPE_UUID
)
3245 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3246 duid
->raw_data_len
= sizeof(sd_id128_t
);
3251 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3252 Manager
*manager
= userdata
;
3253 const sd_bus_error
*e
;
3263 e
= sd_bus_message_get_error(m
);
3265 log_error_errno(sd_bus_error_get_errno(e
),
3266 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3271 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3275 if (sz
!= sizeof(sd_id128_t
)) {
3276 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3280 memcpy(&manager
->product_uuid
, a
, sz
);
3281 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3282 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3284 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3287 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3288 r
= link_configure(link
);
3290 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3293 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3295 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3296 * even if the method fails. */
3297 manager
->has_product_uuid
= true;
3302 static bool link_requires_uuid(Link
*link
) {
3306 assert(link
->manager
);
3307 assert(link
->network
);
3309 duid
= link_get_duid(link
);
3310 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3313 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3316 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3322 static int link_configure_duid(Link
*link
) {
3328 assert(link
->manager
);
3329 assert(link
->network
);
3332 duid
= link_get_duid(link
);
3334 if (!link_requires_uuid(link
))
3337 if (m
->has_product_uuid
) {
3338 (void) duid_set_uuid(duid
, m
->product_uuid
);
3342 if (!m
->links_requesting_uuid
) {
3343 r
= manager_request_product_uuid(m
, link
);
3348 log_link_warning_errno(link
, r
,
3349 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3353 r
= set_put(m
->links_requesting_uuid
, link
);
3357 r
= set_put(m
->duids_requesting_uuid
, duid
);
3365 static int link_initialized_and_synced(Link
*link
) {
3370 assert(link
->ifname
);
3371 assert(link
->manager
);
3373 /* We may get called either from the asynchronous netlink callback,
3374 * or directly for link_add() if running in a container. See link_add(). */
3375 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3378 log_link_debug(link
, "Link state is up-to-date");
3379 link_set_state(link
, LINK_STATE_INITIALIZED
);
3381 r
= link_new_bound_by_list(link
);
3385 r
= link_handle_bound_by_list(link
);
3389 if (!link
->network
) {
3390 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3391 &link
->mac
, &network
);
3393 link_enter_unmanaged(link
);
3395 } else if (r
== 0 && network
->unmanaged
) {
3396 link_enter_unmanaged(link
);
3401 if (link
->flags
& IFF_LOOPBACK
) {
3402 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3403 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3405 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3406 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3408 if (network
->dhcp_server
)
3409 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3412 r
= network_apply(network
, link
);
3417 r
= link_new_bound_to_list(link
);
3421 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3422 * link_configure() is called later asynchronously. */
3423 r
= link_configure_duid(link
);
3427 r
= link_configure(link
);
3434 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3435 (void) link_initialized_and_synced(link
);
3439 int link_initialized(Link
*link
, sd_device
*device
) {
3440 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3444 assert(link
->manager
);
3445 assert(link
->manager
->rtnl
);
3448 if (link
->state
!= LINK_STATE_PENDING
)
3451 if (link
->sd_device
)
3454 log_link_debug(link
, "udev initialized link");
3455 link_set_state(link
, LINK_STATE_INITIALIZED
);
3457 link
->sd_device
= sd_device_ref(device
);
3459 /* udev has initialized the link, but we don't know if we have yet
3460 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3461 * when it returns we know that the pending NEWLINKs have already been
3462 * processed and that we are up-to-date */
3464 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3469 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3470 link_netlink_destroy_callback
, link
);
3479 static int link_load(Link
*link
) {
3480 _cleanup_free_
char *network_file
= NULL
,
3483 *dhcp4_address
= NULL
,
3484 *ipv4ll_address
= NULL
;
3485 union in_addr_union address
;
3486 union in_addr_union route_dst
;
3492 r
= parse_env_file(NULL
, link
->state_file
,
3493 "NETWORK_FILE", &network_file
,
3494 "ADDRESSES", &addresses
,
3496 "DHCP4_ADDRESS", &dhcp4_address
,
3497 "IPV4LL_ADDRESS", &ipv4ll_address
);
3498 if (r
< 0 && r
!= -ENOENT
)
3499 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3506 suffix
= strrchr(network_file
, '.');
3508 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3509 goto network_file_fail
;
3513 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3515 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3516 goto network_file_fail
;
3519 r
= network_apply(network
, link
);
3521 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3530 _cleanup_free_
char *address_str
= NULL
;
3531 char *prefixlen_str
;
3533 unsigned char prefixlen
;
3535 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3537 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3543 prefixlen_str
= strchr(address_str
, '/');
3544 if (!prefixlen_str
) {
3545 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3549 *prefixlen_str
++ = '\0';
3551 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3553 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3557 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3559 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3563 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3565 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3574 _cleanup_free_
char *route_str
= NULL
;
3575 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3577 char *prefixlen_str
;
3579 unsigned char prefixlen
, tos
, table
;
3582 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3584 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3590 prefixlen_str
= strchr(route_str
, '/');
3591 if (!prefixlen_str
) {
3592 log_link_debug(link
, "Failed to parse route %s", route_str
);
3596 *prefixlen_str
++ = '\0';
3598 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3600 log_link_debug(link
,
3601 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3606 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3608 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3612 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3614 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3616 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3617 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3618 0, route_expire_handler
, route
);
3620 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3623 route
->lifetime
= lifetime
;
3624 sd_event_source_unref(route
->expire
);
3625 route
->expire
= TAKE_PTR(expire
);
3629 if (dhcp4_address
) {
3630 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3632 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3633 goto dhcp4_address_fail
;
3636 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3638 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3640 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3642 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3647 if (ipv4ll_address
) {
3648 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3650 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3651 goto ipv4ll_address_fail
;
3654 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3656 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3658 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3660 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3663 ipv4ll_address_fail
:
3668 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3669 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3670 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3679 r
= link_new(m
, message
, ret
);
3685 log_link_debug(link
, "Link %d added", link
->ifindex
);
3687 r
= link_load(link
);
3691 if (detect_container() <= 0) {
3692 /* not in a container, udev will be around */
3693 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3694 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3696 log_link_warning_errno(link
, r
, "Could not find device: %m");
3700 r
= sd_device_get_is_initialized(device
);
3702 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3707 log_link_debug(link
, "link pending udev initialization...");
3711 r
= device_is_renaming(device
);
3713 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3717 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3721 r
= link_initialized(link
, device
);
3725 r
= link_initialized_and_synced(link
);
3732 link_enter_failed(link
);
3736 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3741 log_link_info(link
, "Gained IPv6LL");
3743 link
->ipv6ll_address
= *address
;
3744 link_check_ready(link
);
3746 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3747 r
= link_acquire_ipv6_conf(link
);
3749 link_enter_failed(link
);
3757 static int link_carrier_gained(Link
*link
) {
3762 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3763 r
= link_acquire_conf(link
);
3765 link_enter_failed(link
);
3769 r
= link_request_set_addresses(link
);
3774 r
= link_handle_bound_by_list(link
);
3781 static int link_carrier_lost(Link
*link
) {
3786 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3789 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3790 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3791 * configuration and stop the clients as well. */
3792 if (link
->setting_mtu
)
3795 r
= link_stop_clients(link
);
3797 link_enter_failed(link
);
3801 if (link_dhcp4_server_enabled(link
))
3802 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3804 r
= link_drop_config(link
);
3808 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3809 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3810 r
= link_drop_foreign_config(link
);
3815 r
= link_handle_bound_by_list(link
);
3822 int link_carrier_reset(Link
*link
) {
3827 if (link_has_carrier(link
)) {
3828 r
= link_carrier_lost(link
);
3832 r
= link_carrier_gained(link
);
3836 log_link_info(link
, "Reset carrier");
3842 int link_update(Link
*link
, sd_netlink_message
*m
) {
3843 struct ether_addr mac
;
3846 bool had_carrier
, carrier_gained
, carrier_lost
;
3850 assert(link
->ifname
);
3853 if (link
->state
== LINK_STATE_LINGER
) {
3854 log_link_info(link
, "Link re-added");
3855 link_set_state(link
, LINK_STATE_CONFIGURING
);
3857 r
= link_new_carrier_maps(link
);
3862 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3863 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3864 Manager
*manager
= link
->manager
;
3866 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3869 r
= link_add(manager
, m
, &link
);
3874 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3875 if (r
>= 0 && mtu
> 0) {
3877 if (link
->original_mtu
== 0) {
3878 link
->original_mtu
= mtu
;
3879 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3882 if (link
->dhcp_client
) {
3883 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3886 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3890 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3892 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3896 /* The kernel may broadcast NEWLINK messages without the MAC address
3897 set, simply ignore them. */
3898 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3900 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3903 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3906 log_link_debug(link
, "MAC address: "
3907 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3908 mac
.ether_addr_octet
[0],
3909 mac
.ether_addr_octet
[1],
3910 mac
.ether_addr_octet
[2],
3911 mac
.ether_addr_octet
[3],
3912 mac
.ether_addr_octet
[4],
3913 mac
.ether_addr_octet
[5]);
3916 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3918 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3921 if (link
->dhcp_client
) {
3922 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3923 (const uint8_t *) &link
->mac
,
3927 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3929 r
= dhcp4_set_client_identifier(link
);
3934 if (link
->dhcp6_client
) {
3935 const DUID
* duid
= link_get_duid(link
);
3937 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3938 (const uint8_t *) &link
->mac
,
3942 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3944 if (link
->network
->iaid_set
) {
3945 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3946 link
->network
->iaid
);
3948 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3951 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3953 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3954 duid
->raw_data_len
);
3956 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3960 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3962 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3966 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3968 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3973 had_carrier
= link_has_carrier(link
);
3975 r
= link_update_flags(link
, m
);
3979 r
= link_update_lldp(link
);
3983 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3984 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3986 if (carrier_gained
) {
3987 log_link_info(link
, "Gained carrier");
3989 r
= link_carrier_gained(link
);
3992 } else if (carrier_lost
) {
3993 log_link_info(link
, "Lost carrier");
3995 r
= link_carrier_lost(link
);
4003 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
4011 if (hashmap_isempty(h
))
4015 HASHMAP_FOREACH(link
, h
, i
) {
4019 fprintf(f
, "%i", link
->ifindex
);
4026 int link_save(Link
*link
) {
4027 _cleanup_free_
char *temp_path
= NULL
;
4028 _cleanup_fclose_
FILE *f
= NULL
;
4029 const char *admin_state
, *oper_state
;
4036 assert(link
->state_file
);
4037 assert(link
->lease_file
);
4038 assert(link
->manager
);
4040 if (link
->state
== LINK_STATE_LINGER
) {
4041 (void) unlink(link
->state_file
);
4045 link_lldp_save(link
);
4047 admin_state
= link_state_to_string(link
->state
);
4048 assert(admin_state
);
4050 oper_state
= link_operstate_to_string(link
->operstate
);
4053 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4057 (void) fchmod(fileno(f
), 0644);
4060 "# This is private data. Do not parse.\n"
4063 admin_state
, oper_state
);
4065 if (link
->network
) {
4067 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
4068 const char *dhcp_domainname
= NULL
;
4069 char **dhcp6_domains
= NULL
;
4070 char **dhcp_domains
= NULL
;
4073 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4074 yes_no(link
->network
->required_for_online
));
4076 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
4077 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
4079 if (link
->dhcp6_client
) {
4080 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4081 if (r
< 0 && r
!= -ENOMSG
)
4082 log_link_debug(link
, "No DHCPv6 lease");
4085 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4090 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
4091 _cleanup_free_
char *b
= NULL
;
4093 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
4094 &link
->network
->dns
[j
].address
, &b
);
4096 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4106 if (link
->network
->dhcp_use_dns
&&
4108 const struct in_addr
*addresses
;
4110 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4112 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4116 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4117 struct in6_addr
*in6_addrs
;
4119 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4123 serialize_in6_addrs(f
, in6_addrs
, r
);
4128 /* Make sure to flush out old entries before we use the NDISC data */
4131 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4134 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4138 serialize_in6_addrs(f
, &dd
->address
, 1);
4147 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4149 if (link
->network
->dhcp_use_ntp
&&
4151 const struct in_addr
*addresses
;
4153 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4155 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4159 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4160 struct in6_addr
*in6_addrs
;
4163 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4168 serialize_in6_addrs(f
, in6_addrs
, r
);
4172 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4174 fputstrv(f
, hosts
, NULL
, &space
);
4179 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4180 if (link
->dhcp_lease
) {
4181 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4182 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4185 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4188 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4190 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4193 if (dhcp_domainname
)
4194 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4196 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4198 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4200 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4201 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4206 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4208 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4211 if (dhcp_domainname
)
4212 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4214 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4216 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4218 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4219 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4224 fprintf(f
, "LLMNR=%s\n",
4225 resolve_support_to_string(link
->network
->llmnr
));
4226 fprintf(f
, "MDNS=%s\n",
4227 resolve_support_to_string(link
->network
->mdns
));
4228 if (link
->network
->dns_default_route
>= 0)
4229 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4231 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4232 fprintf(f
, "DNS_OVER_TLS=%s\n",
4233 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4235 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4236 fprintf(f
, "DNSSEC=%s\n",
4237 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4239 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4242 fputs("DNSSEC_NTA=", f
);
4244 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4245 fputs_with_space(f
, n
, NULL
, &space
);
4249 fputs("ADDRESSES=", f
);
4251 SET_FOREACH(a
, link
->addresses
, i
) {
4252 _cleanup_free_
char *address_str
= NULL
;
4254 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4258 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4263 fputs("ROUTES=", f
);
4265 SET_FOREACH(route
, link
->routes
, i
) {
4266 _cleanup_free_
char *route_str
= NULL
;
4268 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4272 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4273 space
? " " : "", route_str
,
4274 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4281 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4282 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4284 if (link
->dhcp_lease
) {
4285 struct in_addr address
;
4286 const char *tz
= NULL
;
4288 assert(link
->network
);
4290 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4292 fprintf(f
, "TIMEZONE=%s\n", tz
);
4294 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4296 fputs("DHCP4_ADDRESS=", f
);
4297 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4301 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4309 (void) unlink(link
->lease_file
);
4312 struct in_addr address
;
4314 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4316 fputs("IPV4LL_ADDRESS=", f
);
4317 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4322 r
= fflush_and_check(f
);
4326 if (rename(temp_path
, link
->state_file
) < 0) {
4334 (void) unlink(link
->state_file
);
4336 (void) unlink(temp_path
);
4338 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4341 /* The serialized state in /run is no longer up-to-date. */
4342 void link_dirty(Link
*link
) {
4347 /* mark manager dirty as link is dirty */
4348 manager_dirty(link
->manager
);
4350 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4352 /* allocation errors are ignored */
4355 r
= set_put(link
->manager
->dirty_links
, link
);
4357 /* don't take another ref if the link was already dirty */
4363 /* The serialized state in /run is up-to-date */
4364 void link_clean(Link
*link
) {
4366 assert(link
->manager
);
4368 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4371 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4372 [LINK_STATE_PENDING
] = "pending",
4373 [LINK_STATE_INITIALIZED
] = "initialized",
4374 [LINK_STATE_CONFIGURING
] = "configuring",
4375 [LINK_STATE_CONFIGURED
] = "configured",
4376 [LINK_STATE_UNMANAGED
] = "unmanaged",
4377 [LINK_STATE_FAILED
] = "failed",
4378 [LINK_STATE_LINGER
] = "linger",
4381 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);