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 for interface %s: %m",
342 enable_disable(!disabled
), link
->ifname
);
344 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
349 static bool link_is_enslaved(Link
*link
) {
350 if (link
->flags
& IFF_SLAVE
)
351 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
354 if (!link
->enslaved_raw
)
360 if (link
->network
->bridge
)
361 /* TODO: support the case when link is not managed by networkd. */
367 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
373 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
376 link_update_operstate(master
, true);
379 void link_update_operstate(Link
*link
, bool also_update_master
) {
380 LinkOperationalState operstate
;
385 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
386 operstate
= LINK_OPERSTATE_DORMANT
;
387 else if (link_has_carrier(link
)) {
389 uint8_t scope
= RT_SCOPE_NOWHERE
;
391 /* if we have carrier, check what addresses we have */
392 SET_FOREACH(address
, link
->addresses
, i
) {
393 if (!address_is_ready(address
))
396 if (address
->scope
< scope
)
397 scope
= address
->scope
;
400 /* for operstate we also take foreign addresses into account */
401 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
402 if (!address_is_ready(address
))
405 if (address
->scope
< scope
)
406 scope
= address
->scope
;
409 if (scope
< RT_SCOPE_SITE
)
410 /* universally accessible addresses found */
411 operstate
= LINK_OPERSTATE_ROUTABLE
;
412 else if (scope
< RT_SCOPE_HOST
)
413 /* only link or site local addresses found */
414 operstate
= LINK_OPERSTATE_DEGRADED
;
416 /* no useful addresses found */
417 operstate
= LINK_OPERSTATE_CARRIER
;
418 } else if (link
->flags
& IFF_UP
)
419 operstate
= LINK_OPERSTATE_NO_CARRIER
;
421 operstate
= LINK_OPERSTATE_OFF
;
423 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
424 link_is_enslaved(link
))
425 operstate
= LINK_OPERSTATE_ENSLAVED
;
427 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
430 SET_FOREACH(slave
, link
->slaves
, i
) {
431 link_update_operstate(slave
, false);
433 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
434 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
438 if (link
->operstate
!= operstate
) {
439 link
->operstate
= operstate
;
440 link_send_changed(link
, "OperationalState", NULL
);
444 if (also_update_master
&& link
->network
) {
445 link_update_master_operstate(link
, link
->network
->bond
);
446 link_update_master_operstate(link
, link
->network
->bridge
);
450 #define FLAG_STRING(string, flag, old, new) \
451 (((old ^ new) & flag) \
452 ? ((old & flag) ? (" -" string) : (" +" string)) \
455 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
456 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
462 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
464 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
466 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
468 /* if we got a message without operstate, take it to mean
469 the state was unchanged */
470 operstate
= link
->kernel_operstate
;
472 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
475 if (link
->flags
!= flags
) {
476 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",
477 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
478 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
479 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
480 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
481 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
482 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
483 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
484 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
485 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
486 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
487 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
488 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
489 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
490 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
491 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
492 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
493 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
494 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
495 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
497 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
498 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
499 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
500 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
501 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
502 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
503 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
504 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
506 /* link flags are currently at most 18 bits, let's align to
508 if (unknown_flags_added
)
510 "Unknown link flags gained: %#.5x (ignoring)",
511 unknown_flags_added
);
513 if (unknown_flags_removed
)
515 "Unknown link flags lost: %#.5x (ignoring)",
516 unknown_flags_removed
);
520 link
->kernel_operstate
= operstate
;
522 link_update_operstate(link
, true);
527 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
528 _cleanup_(link_unrefp
) Link
*link
= NULL
;
530 const char *ifname
, *kind
= NULL
;
532 unsigned short iftype
;
538 /* check for link kind */
539 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
541 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
542 r
= sd_netlink_message_exit_container(message
);
547 r
= sd_netlink_message_get_type(message
, &type
);
550 else if (type
!= RTM_NEWLINK
)
553 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
556 else if (ifindex
<= 0)
559 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
563 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
574 .state
= LINK_STATE_PENDING
,
575 .rtnl_extended_attrs
= true,
580 link
->ifname
= strdup(ifname
);
585 link
->kind
= strdup(kind
);
590 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
592 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
594 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
596 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
598 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
601 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
604 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
607 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
611 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
615 r
= link_update_flags(link
, message
);
619 *ret
= TAKE_PTR(link
);
624 static Link
*link_free(Link
*link
) {
629 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
630 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
632 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
633 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
635 while ((address
= link
->pool_addresses
)) {
636 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
637 address_free(address
);
640 sd_dhcp_server_unref(link
->dhcp_server
);
641 sd_dhcp_client_unref(link
->dhcp_client
);
642 sd_dhcp_lease_unref(link
->dhcp_lease
);
644 link_lldp_emit_stop(link
);
646 free(link
->lease_file
);
648 sd_lldp_unref(link
->lldp
);
649 free(link
->lldp_file
);
653 sd_ipv4ll_unref(link
->ipv4ll
);
654 sd_dhcp6_client_unref(link
->dhcp6_client
);
655 sd_ndisc_unref(link
->ndisc
);
656 sd_radv_unref(link
->radv
);
661 (void) unlink(link
->state_file
);
662 free(link
->state_file
);
664 sd_device_unref(link
->sd_device
);
666 hashmap_free(link
->bound_to_links
);
667 hashmap_free(link
->bound_by_links
);
669 set_free_with_destructor(link
->slaves
, link_unref
);
671 network_unref(link
->network
);
676 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
678 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
685 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
694 static void link_set_state(Link
*link
, LinkState state
) {
697 if (link
->state
== state
)
700 log_link_debug(link
, "State changed: %s -> %s",
701 link_state_to_string(link
->state
),
702 link_state_to_string(state
));
706 link_send_changed(link
, "AdministrativeState", NULL
);
709 static void link_enter_unmanaged(Link
*link
) {
712 log_link_debug(link
, "Unmanaged");
714 link_set_state(link
, LINK_STATE_UNMANAGED
);
719 int link_stop_clients(Link
*link
) {
723 assert(link
->manager
);
724 assert(link
->manager
->event
);
726 if (link
->dhcp_client
) {
727 k
= sd_dhcp_client_stop(link
->dhcp_client
);
729 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
733 k
= sd_ipv4ll_stop(link
->ipv4ll
);
735 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
738 if (link
->dhcp6_client
) {
739 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
741 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
745 k
= sd_ndisc_stop(link
->ndisc
);
747 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
751 k
= sd_radv_stop(link
->radv
);
753 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
756 link_lldp_emit_stop(link
);
760 void link_enter_failed(Link
*link
) {
763 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
766 log_link_warning(link
, "Failed");
768 link_set_state(link
, LINK_STATE_FAILED
);
770 link_stop_clients(link
);
775 static Address
* link_find_dhcp_server_address(Link
*link
) {
779 assert(link
->network
);
781 /* The first statically configured address if there is any */
782 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
784 if (address
->family
!= AF_INET
)
787 if (in_addr_is_null(address
->family
, &address
->in_addr
))
793 /* If that didn't work, find a suitable address we got from the pool */
794 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
795 if (address
->family
!= AF_INET
)
804 static int link_join_netdevs_after_configured(Link
*link
) {
809 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
810 if (netdev
->ifindex
> 0)
811 /* Assume already enslaved. */
814 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
817 log_struct(LOG_DEBUG
,
818 LOG_LINK_INTERFACE(link
),
819 LOG_NETDEV_INTERFACE(netdev
),
820 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
822 r
= netdev_join(netdev
, link
, NULL
);
824 return log_struct_errno(LOG_WARNING
, r
,
825 LOG_LINK_INTERFACE(link
),
826 LOG_NETDEV_INTERFACE(netdev
),
827 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
833 static void link_enter_configured(Link
*link
) {
835 assert(link
->network
);
837 if (link
->state
!= LINK_STATE_CONFIGURING
)
840 log_link_info(link
, "Configured");
842 link_set_state(link
, LINK_STATE_CONFIGURED
);
844 (void) link_join_netdevs_after_configured(link
);
849 static int link_request_set_routing_policy_rule(Link
*link
) {
850 RoutingPolicyRule
*rule
, *rrule
= NULL
;
854 assert(link
->network
);
856 link_set_state(link
, LINK_STATE_CONFIGURING
);
857 link
->routing_policy_rules_configured
= false;
859 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
860 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
861 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
862 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
864 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
868 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
870 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
871 link_enter_failed(link
);
875 link
->routing_policy_rule_messages
++;
878 routing_policy_rule_purge(link
->manager
, link
);
879 if (link
->routing_policy_rule_messages
== 0) {
880 link
->routing_policy_rules_configured
= true;
881 link_check_ready(link
);
883 log_link_debug(link
, "Setting routing policy rules");
888 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
892 assert(link
->route_messages
> 0);
893 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
894 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
896 link
->route_messages
--;
898 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
901 r
= sd_netlink_message_get_errno(m
);
902 if (r
< 0 && r
!= -EEXIST
)
903 log_link_warning_errno(link
, r
, "Could not set route: %m");
905 if (link
->route_messages
== 0) {
906 log_link_debug(link
, "Routes set");
907 link
->static_routes_configured
= true;
908 link_check_ready(link
);
914 int link_request_set_routes(Link
*link
) {
916 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
917 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
924 assert(link
->network
);
925 assert(link
->addresses_configured
);
926 assert(link
->address_messages
== 0);
927 assert(link
->state
!= _LINK_STATE_INVALID
);
929 link_set_state(link
, LINK_STATE_CONFIGURING
);
930 link
->static_routes_configured
= false;
932 r
= link_request_set_routing_policy_rule(link
);
936 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
937 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
938 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
940 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
943 r
= route_configure(rt
, link
, route_handler
);
945 log_link_warning_errno(link
, r
, "Could not set routes: %m");
946 link_enter_failed(link
);
950 link
->route_messages
++;
953 if (link
->route_messages
== 0) {
954 link
->static_routes_configured
= true;
955 link_check_ready(link
);
957 log_link_debug(link
, "Setting routes");
962 void link_check_ready(Link
*link
) {
968 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
974 if (!link
->addresses_configured
)
977 if (!link
->neighbors_configured
)
980 SET_FOREACH(a
, link
->addresses
, i
)
981 if (!address_is_ready(a
))
984 if (!link
->addresses_ready
) {
985 link
->addresses_ready
= true;
986 link_request_set_routes(link
);
989 if (!link
->static_routes_configured
)
992 if (!link
->routing_policy_rules_configured
)
995 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
998 if (link_ipv6ll_enabled(link
) &&
999 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1002 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1003 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
1004 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
1005 /* When DHCP is enabled, at least one protocol must provide an address, or
1006 * an IPv4ll fallback address must be configured. */
1009 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1012 if (link
->state
!= LINK_STATE_CONFIGURED
)
1013 link_enter_configured(link
);
1018 static int link_request_set_neighbors(Link
*link
) {
1023 assert(link
->network
);
1024 assert(link
->state
!= _LINK_STATE_INVALID
);
1026 link_set_state(link
, LINK_STATE_CONFIGURING
);
1027 link
->neighbors_configured
= false;
1029 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1030 r
= neighbor_configure(neighbor
, link
, NULL
);
1032 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1033 link_enter_failed(link
);
1038 if (link
->neighbor_messages
== 0) {
1039 link
->neighbors_configured
= true;
1040 link_check_ready(link
);
1042 log_link_debug(link
, "Setting neighbors");
1047 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1053 assert(link
->ifname
);
1054 assert(link
->address_messages
> 0);
1055 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1056 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1058 link
->address_messages
--;
1060 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1063 r
= sd_netlink_message_get_errno(m
);
1064 if (r
< 0 && r
!= -EEXIST
)
1065 log_link_warning_errno(link
, r
, "could not set address: %m");
1067 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1069 if (link
->address_messages
== 0) {
1070 log_link_debug(link
, "Addresses set");
1071 link
->addresses_configured
= true;
1072 link_check_ready(link
);
1078 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1079 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1080 size_t n_addresses
= 0, n_allocated
= 0;
1083 log_debug("Copying DNS server information from %s", link
->ifname
);
1088 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1091 /* Only look for IPv4 addresses */
1092 if (link
->network
->dns
[i
].family
!= AF_INET
)
1095 ia
= link
->network
->dns
[i
].address
.in
;
1097 /* Never propagate obviously borked data */
1098 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1101 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1104 addresses
[n_addresses
++] = ia
;
1107 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1108 const struct in_addr
*da
= NULL
;
1111 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1114 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1117 for (j
= 0; j
< n
; j
++)
1118 if (in4_addr_is_non_local(&da
[j
]))
1119 addresses
[n_addresses
++] = da
[j
];
1123 if (n_addresses
<= 0)
1126 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1129 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1130 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1131 size_t n_addresses
= 0, n_allocated
= 0;
1137 log_debug("Copying NTP server information from %s", link
->ifname
);
1139 STRV_FOREACH(a
, link
->network
->ntp
) {
1142 /* Only look for IPv4 addresses */
1143 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1146 /* Never propagate obviously borked data */
1147 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1150 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1153 addresses
[n_addresses
++] = ia
;
1156 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1157 const struct in_addr
*da
= NULL
;
1160 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1163 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1166 for (j
= 0; j
< n
; j
++)
1167 if (in4_addr_is_non_local(&da
[j
]))
1168 addresses
[n_addresses
++] = da
[j
];
1172 if (n_addresses
<= 0)
1175 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1178 static int link_set_bridge_fdb(Link
*link
) {
1179 FdbEntry
*fdb_entry
;
1182 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1183 r
= fdb_entry_configure(link
, fdb_entry
);
1185 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1191 static int link_request_set_addresses(Link
*link
) {
1192 AddressLabel
*label
;
1197 assert(link
->network
);
1198 assert(link
->state
!= _LINK_STATE_INVALID
);
1200 link_set_state(link
, LINK_STATE_CONFIGURING
);
1202 /* Reset all *_configured flags we are configuring. */
1203 link
->addresses_configured
= false;
1204 link
->addresses_ready
= false;
1205 link
->neighbors_configured
= false;
1206 link
->static_routes_configured
= false;
1207 link
->routing_policy_rules_configured
= false;
1209 r
= link_set_bridge_fdb(link
);
1213 r
= link_request_set_neighbors(link
);
1217 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1220 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1222 r
= address_configure(ad
, link
, address_handler
, update
);
1224 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1225 link_enter_failed(link
);
1229 link
->address_messages
++;
1232 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1233 r
= address_label_configure(label
, link
, NULL
, false);
1235 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1236 link_enter_failed(link
);
1240 link
->address_label_messages
++;
1243 /* now that we can figure out a default address for the dhcp server,
1245 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1247 Link
*uplink
= NULL
;
1248 bool acquired_uplink
= false;
1250 address
= link_find_dhcp_server_address(link
);
1252 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1253 link_enter_failed(link
);
1257 /* use the server address' subnet as the pool */
1258 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1259 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1264 r = sd_dhcp_server_set_router(link->dhcp_server,
1265 &main_address->in_addr.in);
1270 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1271 r
= sd_dhcp_server_set_max_lease_time(
1273 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1278 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1279 r
= sd_dhcp_server_set_default_lease_time(
1281 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1286 if (link
->network
->dhcp_server_emit_dns
) {
1288 if (link
->network
->n_dhcp_server_dns
> 0)
1289 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1291 uplink
= manager_find_uplink(link
->manager
, link
);
1292 acquired_uplink
= true;
1295 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1298 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1301 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1304 if (link
->network
->dhcp_server_emit_ntp
) {
1306 if (link
->network
->n_dhcp_server_ntp
> 0)
1307 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1309 if (!acquired_uplink
)
1310 uplink
= manager_find_uplink(link
->manager
, link
);
1313 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1316 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1320 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1323 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1325 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1327 if (link
->network
->dhcp_server_emit_timezone
) {
1328 _cleanup_free_
char *buffer
= NULL
;
1329 const char *tz
= NULL
;
1331 if (link
->network
->dhcp_server_timezone
)
1332 tz
= link
->network
->dhcp_server_timezone
;
1334 r
= get_timezone(&buffer
);
1336 log_warning_errno(r
, "Failed to determine timezone: %m");
1342 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1347 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1348 r
= sd_dhcp_server_start(link
->dhcp_server
);
1350 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1352 link_enter_failed(link
);
1358 log_link_debug(link
, "Offering DHCPv4 leases");
1361 if (link
->address_messages
== 0) {
1362 link
->addresses_configured
= true;
1363 link_check_ready(link
);
1365 log_link_debug(link
, "Setting addresses");
1370 static int link_set_bridge_vlan(Link
*link
) {
1373 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1375 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1380 static int link_set_proxy_arp(Link
*link
) {
1383 if (!link_proxy_arp_enabled(link
))
1386 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1388 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1393 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1398 log_link_debug(link
, "Set link");
1400 r
= sd_netlink_message_get_errno(m
);
1401 if (r
< 0 && r
!= -EEXIST
) {
1402 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1403 link_enter_failed(link
);
1409 static int link_configure_after_setting_mtu(Link
*link
);
1411 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1416 assert(link
->ifname
);
1418 link
->setting_mtu
= false;
1420 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1423 r
= sd_netlink_message_get_errno(m
);
1425 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1429 log_link_debug(link
, "Setting MTU done.");
1431 if (link
->state
== LINK_STATE_INITIALIZED
)
1432 (void) link_configure_after_setting_mtu(link
);
1437 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1438 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1442 assert(link
->manager
);
1443 assert(link
->manager
->rtnl
);
1445 if (mtu
== 0 || link
->setting_mtu
)
1448 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1451 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1453 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1455 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1457 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1458 * for this interface, then disable IPv6 else enable it. */
1459 (void) link_enable_ipv6(link
);
1461 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1462 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1463 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1465 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1466 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1471 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1473 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1475 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1476 link_netlink_destroy_callback
, link
);
1478 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1481 link
->setting_mtu
= true;
1486 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1491 assert(link
->ifname
);
1493 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1496 r
= sd_netlink_message_get_errno(m
);
1498 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1503 static int link_set_flags(Link
*link
) {
1504 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1505 unsigned ifi_change
= 0;
1506 unsigned ifi_flags
= 0;
1510 assert(link
->manager
);
1511 assert(link
->manager
->rtnl
);
1513 if (link
->flags
& IFF_LOOPBACK
)
1519 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1522 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1524 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1526 if (link
->network
->arp
>= 0) {
1527 ifi_change
|= IFF_NOARP
;
1528 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1531 if (link
->network
->multicast
>= 0) {
1532 ifi_change
|= IFF_MULTICAST
;
1533 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1536 if (link
->network
->allmulticast
>= 0) {
1537 ifi_change
|= IFF_ALLMULTI
;
1538 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1541 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1543 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1545 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1546 link_netlink_destroy_callback
, link
);
1548 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1555 static int link_set_bridge(Link
*link
) {
1556 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1560 assert(link
->network
);
1562 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1564 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1566 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1568 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1570 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1572 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1574 if (link
->network
->use_bpdu
>= 0) {
1575 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1577 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1580 if (link
->network
->hairpin
>= 0) {
1581 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1583 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1586 if (link
->network
->fast_leave
>= 0) {
1587 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1589 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1592 if (link
->network
->allow_port_to_be_root
>= 0) {
1593 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1595 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1599 if (link
->network
->unicast_flood
>= 0) {
1600 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1602 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1605 if (link
->network
->multicast_flood
>= 0) {
1606 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
1608 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
1611 if (link
->network
->multicast_to_unicast
>= 0) {
1612 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1614 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1617 if (link
->network
->neighbor_suppression
>= 0) {
1618 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
1620 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
1623 if (link
->network
->learning
>= 0) {
1624 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
1626 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
1629 if (link
->network
->cost
!= 0) {
1630 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1632 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1635 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1636 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1638 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1641 r
= sd_netlink_message_close_container(req
);
1643 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1645 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1646 link_netlink_destroy_callback
, link
);
1648 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1655 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1660 assert(link
->ifname
);
1662 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1665 r
= sd_netlink_message_get_errno(m
);
1667 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1674 static int link_set_bond(Link
*link
) {
1675 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1679 assert(link
->network
);
1681 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1683 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1685 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1687 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1689 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1691 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1693 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1695 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1697 if (link
->network
->active_slave
) {
1698 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1700 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1703 if (link
->network
->primary_slave
) {
1704 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1706 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1709 r
= sd_netlink_message_close_container(req
);
1711 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1713 r
= sd_netlink_message_close_container(req
);
1715 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1717 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1718 link_netlink_destroy_callback
, link
);
1720 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1727 static int link_lldp_save(Link
*link
) {
1728 _cleanup_free_
char *temp_path
= NULL
;
1729 _cleanup_fclose_
FILE *f
= NULL
;
1730 sd_lldp_neighbor
**l
= NULL
;
1734 assert(link
->lldp_file
);
1737 (void) unlink(link
->lldp_file
);
1741 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1745 (void) unlink(link
->lldp_file
);
1751 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1755 fchmod(fileno(f
), 0644);
1757 for (i
= 0; i
< n
; i
++) {
1762 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1767 (void) fwrite(&u
, 1, sizeof(u
), f
);
1768 (void) fwrite(p
, 1, sz
, f
);
1771 r
= fflush_and_check(f
);
1775 if (rename(temp_path
, link
->lldp_file
) < 0) {
1782 (void) unlink(link
->lldp_file
);
1784 (void) unlink(temp_path
);
1786 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1790 for (i
= 0; i
< n
; i
++)
1791 sd_lldp_neighbor_unref(l
[i
]);
1798 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1799 Link
*link
= userdata
;
1804 (void) link_lldp_save(link
);
1806 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1807 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1809 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1811 r
= link_lldp_emit_start(link
);
1813 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1817 static int link_acquire_ipv6_conf(Link
*link
) {
1822 if (link_ipv6_accept_ra_enabled(link
)) {
1823 assert(link
->ndisc
);
1825 log_link_debug(link
, "Discovering IPv6 routers");
1827 r
= sd_ndisc_start(link
->ndisc
);
1828 if (r
< 0 && r
!= -EBUSY
)
1829 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1832 if (link_radv_enabled(link
)) {
1834 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1836 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1838 r
= sd_radv_start(link
->radv
);
1839 if (r
< 0 && r
!= -EBUSY
)
1840 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1843 (void) dhcp6_request_prefix_delegation(link
);
1848 static int link_acquire_ipv4_conf(Link
*link
) {
1852 assert(link
->manager
);
1853 assert(link
->manager
->event
);
1855 if (link_ipv4ll_enabled(link
)) {
1856 assert(link
->ipv4ll
);
1858 log_link_debug(link
, "Acquiring IPv4 link-local address");
1860 r
= sd_ipv4ll_start(link
->ipv4ll
);
1862 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1865 if (link_dhcp4_enabled(link
)) {
1866 assert(link
->dhcp_client
);
1868 log_link_debug(link
, "Acquiring DHCPv4 lease");
1870 r
= sd_dhcp_client_start(link
->dhcp_client
);
1872 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1878 static int link_acquire_conf(Link
*link
) {
1883 r
= link_acquire_ipv4_conf(link
);
1887 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1888 r
= link_acquire_ipv6_conf(link
);
1893 if (link_lldp_emit_enabled(link
)) {
1894 r
= link_lldp_emit_start(link
);
1896 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1902 bool link_has_carrier(Link
*link
) {
1903 /* see Documentation/networking/operstates.txt in the kernel sources */
1905 if (link
->kernel_operstate
== IF_OPER_UP
)
1908 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1909 /* operstate may not be implemented, so fall back to flags */
1910 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1916 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1921 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1924 r
= sd_netlink_message_get_errno(m
);
1926 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1931 static int link_configure_addrgen_mode(Link
*link
) {
1932 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1933 uint8_t ipv6ll_mode
;
1937 assert(link
->network
);
1938 assert(link
->manager
);
1939 assert(link
->manager
->rtnl
);
1941 log_link_debug(link
, "Setting address genmode for link");
1943 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1945 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1947 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1949 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1951 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1953 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1955 if (!link_ipv6ll_enabled(link
))
1956 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1957 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1958 /* The file may not exist. And event if it exists, when stable_secret is unset,
1959 * reading the file fails with EIO. */
1960 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1962 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1964 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1966 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1968 r
= sd_netlink_message_close_container(req
);
1970 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1972 r
= sd_netlink_message_close_container(req
);
1974 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1976 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1977 link_netlink_destroy_callback
, link
);
1979 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1986 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1991 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1994 r
= sd_netlink_message_get_errno(m
);
1996 /* we warn but don't fail the link, as it may be brought up later */
1997 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
2002 static int link_up(Link
*link
) {
2003 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2007 assert(link
->network
);
2008 assert(link
->manager
);
2009 assert(link
->manager
->rtnl
);
2011 log_link_debug(link
, "Bringing link up");
2013 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2015 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2017 /* set it free if not enslaved with networkd */
2018 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
2019 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
2021 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
2024 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2026 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2028 if (link
->network
->mac
) {
2029 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
2031 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2034 if (link_ipv6_enabled(link
)) {
2035 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2037 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2039 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2040 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2042 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2044 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2045 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2047 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2050 r
= sd_netlink_message_close_container(req
);
2052 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2054 r
= sd_netlink_message_close_container(req
);
2056 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2059 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2060 link_netlink_destroy_callback
, link
);
2062 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2069 static int link_up_can(Link
*link
) {
2070 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2075 log_link_debug(link
, "Bringing CAN link up");
2077 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2079 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2081 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2083 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2085 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2086 link_netlink_destroy_callback
, link
);
2088 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2095 static int link_set_can(Link
*link
) {
2096 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2100 assert(link
->network
);
2101 assert(link
->manager
);
2102 assert(link
->manager
->rtnl
);
2104 log_link_debug(link
, "link_set_can");
2106 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2108 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2110 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2112 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2114 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2116 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2118 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2120 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2122 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2123 struct can_bittiming bt
= {
2124 .bitrate
= link
->network
->can_bitrate
,
2125 .sample_point
= link
->network
->can_sample_point
,
2128 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2129 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2133 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2134 if (link
->network
->can_sample_point
> 0)
2135 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2137 log_link_debug(link
, "Using default sample point");
2139 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2141 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2144 if (link
->network
->can_restart_us
> 0) {
2145 char time_string
[FORMAT_TIMESPAN_MAX
];
2146 uint64_t restart_ms
;
2148 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2151 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2153 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2155 if (restart_ms
> UINT32_MAX
) {
2156 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2160 log_link_debug(link
, "Setting restart = %s", time_string
);
2162 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2164 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2167 if (link
->network
->can_triple_sampling
>= 0) {
2168 struct can_ctrlmode cm
= {
2169 .mask
= CAN_CTRLMODE_3_SAMPLES
,
2170 .flags
= link
->network
->can_triple_sampling
? CAN_CTRLMODE_3_SAMPLES
: 0,
2173 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
2175 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
2177 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
2180 r
= sd_netlink_message_close_container(m
);
2182 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2184 r
= sd_netlink_message_close_container(m
);
2186 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2188 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2189 link_netlink_destroy_callback
, link
);
2191 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2195 if (!(link
->flags
& IFF_UP
)) {
2196 r
= link_up_can(link
);
2198 link_enter_failed(link
);
2203 log_link_debug(link
, "link_set_can done");
2208 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2213 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2216 r
= sd_netlink_message_get_errno(m
);
2218 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2220 if (streq_ptr(link
->kind
, "can"))
2226 int link_down(Link
*link
) {
2227 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2231 assert(link
->manager
);
2232 assert(link
->manager
->rtnl
);
2234 log_link_debug(link
, "Bringing link down");
2236 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2237 RTM_SETLINK
, link
->ifindex
);
2239 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2241 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2243 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2245 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2246 link_netlink_destroy_callback
, link
);
2248 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2255 static int link_handle_bound_to_list(Link
*link
) {
2259 bool required_up
= false;
2260 bool link_is_up
= false;
2264 if (hashmap_isempty(link
->bound_to_links
))
2267 if (link
->flags
& IFF_UP
)
2270 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2271 if (link_has_carrier(l
)) {
2276 if (!required_up
&& link_is_up
) {
2277 r
= link_down(link
);
2280 } else if (required_up
&& !link_is_up
) {
2289 static int link_handle_bound_by_list(Link
*link
) {
2296 if (hashmap_isempty(link
->bound_by_links
))
2299 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2300 r
= link_handle_bound_to_list(l
);
2308 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2314 if (link
== carrier
)
2317 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2320 r
= hashmap_ensure_allocated(h
, NULL
);
2324 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2331 static int link_new_bound_by_list(Link
*link
) {
2336 bool list_updated
= false;
2339 assert(link
->manager
);
2343 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2344 if (!carrier
->network
)
2347 if (strv_isempty(carrier
->network
->bind_carrier
))
2350 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2351 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2355 list_updated
= true;
2362 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2363 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2367 link_dirty(carrier
);
2373 static int link_new_bound_to_list(Link
*link
) {
2378 bool list_updated
= false;
2381 assert(link
->manager
);
2386 if (strv_isempty(link
->network
->bind_carrier
))
2391 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2392 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2393 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2397 list_updated
= true;
2404 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2405 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2409 link_dirty(carrier
);
2415 static int link_new_carrier_maps(Link
*link
) {
2418 r
= link_new_bound_by_list(link
);
2422 r
= link_handle_bound_by_list(link
);
2426 r
= link_new_bound_to_list(link
);
2430 r
= link_handle_bound_to_list(link
);
2437 static void link_free_bound_to_list(Link
*link
) {
2441 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2442 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2444 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2445 link_dirty(bound_to
);
2451 static void link_free_bound_by_list(Link
*link
) {
2455 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2456 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2458 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2459 link_dirty(bound_by
);
2460 link_handle_bound_to_list(bound_by
);
2467 static void link_free_carrier_maps(Link
*link
) {
2468 bool list_updated
= false;
2472 if (!hashmap_isempty(link
->bound_to_links
)) {
2473 link_free_bound_to_list(link
);
2474 list_updated
= true;
2477 if (!hashmap_isempty(link
->bound_by_links
)) {
2478 link_free_bound_by_list(link
);
2479 list_updated
= true;
2488 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2495 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2499 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2503 r
= set_put(master
->slaves
, link
);
2511 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2516 if (!link
->manager
|| !netdev
)
2519 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2522 link_unref(set_remove(master
->slaves
, link
));
2525 static void link_detach_from_manager(Link
*link
) {
2526 if (!link
|| !link
->manager
)
2529 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2532 /* The following must be called at last. */
2533 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2537 void link_drop(Link
*link
) {
2538 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2541 link_set_state(link
, LINK_STATE_LINGER
);
2543 link_free_carrier_maps(link
);
2545 if (link
->network
) {
2546 link_drop_from_master(link
, link
->network
->bridge
);
2547 link_drop_from_master(link
, link
->network
->bond
);
2550 log_link_debug(link
, "Link removed");
2552 (void) unlink(link
->state_file
);
2553 link_detach_from_manager(link
);
2556 static int link_joined(Link
*link
) {
2560 assert(link
->network
);
2562 if (!hashmap_isempty(link
->bound_to_links
)) {
2563 r
= link_handle_bound_to_list(link
);
2566 } else if (!(link
->flags
& IFF_UP
)) {
2569 link_enter_failed(link
);
2574 if (link
->network
->bridge
) {
2575 r
= link_set_bridge(link
);
2577 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2579 r
= link_append_to_master(link
, link
->network
->bridge
);
2581 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2584 if (link
->network
->bond
) {
2585 r
= link_set_bond(link
);
2587 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2589 r
= link_append_to_master(link
, link
->network
->bond
);
2591 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2594 if (link
->network
->use_br_vlan
&&
2595 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2596 r
= link_set_bridge_vlan(link
);
2598 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2601 /* Skip setting up addresses until it gets carrier,
2602 or it would try to set addresses twice,
2603 which is bad for non-idempotent steps. */
2604 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2607 return link_request_set_addresses(link
);
2610 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2614 assert(link
->network
);
2615 assert(link
->enslaving
> 0);
2616 assert(!link
->enslaved_raw
);
2620 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2623 r
= sd_netlink_message_get_errno(m
);
2624 if (r
< 0 && r
!= -EEXIST
) {
2625 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2626 link_enter_failed(link
);
2629 log_link_debug(link
, "Joined netdev");
2631 if (link
->enslaving
== 0) {
2632 link
->enslaved_raw
= true;
2639 static int link_enter_join_netdev(Link
*link
) {
2645 assert(link
->network
);
2646 assert(link
->state
== LINK_STATE_INITIALIZED
);
2648 link_set_state(link
, LINK_STATE_CONFIGURING
);
2651 link
->enslaving
= 0;
2652 link
->enslaved_raw
= false;
2654 if (link
->network
->bond
) {
2655 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2656 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2657 return link_joined(link
);
2659 log_struct(LOG_DEBUG
,
2660 LOG_LINK_INTERFACE(link
),
2661 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2662 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2666 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2668 log_struct_errno(LOG_WARNING
, r
,
2669 LOG_LINK_INTERFACE(link
),
2670 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2671 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2672 link_enter_failed(link
);
2677 if (link
->network
->bridge
) {
2678 log_struct(LOG_DEBUG
,
2679 LOG_LINK_INTERFACE(link
),
2680 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2681 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2685 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2687 log_struct_errno(LOG_WARNING
, r
,
2688 LOG_LINK_INTERFACE(link
),
2689 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2690 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2691 link_enter_failed(link
);
2696 if (link
->network
->vrf
) {
2697 log_struct(LOG_DEBUG
,
2698 LOG_LINK_INTERFACE(link
),
2699 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2700 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2704 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2706 log_struct_errno(LOG_WARNING
, r
,
2707 LOG_LINK_INTERFACE(link
),
2708 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2709 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2710 link_enter_failed(link
);
2715 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2717 if (netdev
->ifindex
> 0)
2718 /* Assume already enslaved. */
2721 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2724 log_struct(LOG_DEBUG
,
2725 LOG_LINK_INTERFACE(link
),
2726 LOG_NETDEV_INTERFACE(netdev
),
2727 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2731 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2733 log_struct_errno(LOG_WARNING
, r
,
2734 LOG_LINK_INTERFACE(link
),
2735 LOG_NETDEV_INTERFACE(netdev
),
2736 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2737 link_enter_failed(link
);
2742 if (link
->enslaving
== 0)
2743 return link_joined(link
);
2748 static int link_set_ipv4_forward(Link
*link
) {
2751 if (!link_ipv4_forward_enabled(link
))
2754 /* We propagate the forwarding flag from one interface to the
2755 * global setting one way. This means: as long as at least one
2756 * interface was configured at any time that had IP forwarding
2757 * enabled the setting will stay on for good. We do this
2758 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2759 * somewhat in sync (see below). */
2761 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2763 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2768 static int link_set_ipv6_forward(Link
*link
) {
2771 if (!link_ipv6_forward_enabled(link
))
2774 /* On Linux, the IPv6 stack does not know a per-interface
2775 * packet forwarding setting: either packet forwarding is on
2776 * for all, or off for all. We hence don't bother with a
2777 * per-interface setting, but simply propagate the interface
2778 * flag, if it is set, to the global flag, one-way. Note that
2779 * while IPv4 would allow a per-interface flag, we expose the
2780 * same behaviour there and also propagate the setting from
2781 * one to all, to keep things simple (see above). */
2783 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2785 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2790 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2791 IPv6PrivacyExtensions s
;
2794 s
= link_ipv6_privacy_extensions(link
);
2798 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2800 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2805 static int link_set_ipv6_accept_ra(Link
*link
) {
2808 /* Make this a NOP if IPv6 is not available */
2809 if (!socket_ipv6_is_supported())
2812 if (link
->flags
& IFF_LOOPBACK
)
2818 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2820 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2825 static int link_set_ipv6_dad_transmits(Link
*link
) {
2828 /* Make this a NOP if IPv6 is not available */
2829 if (!socket_ipv6_is_supported())
2832 if (link
->flags
& IFF_LOOPBACK
)
2838 if (link
->network
->ipv6_dad_transmits
< 0)
2841 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2843 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2848 static int link_set_ipv6_hop_limit(Link
*link
) {
2851 /* Make this a NOP if IPv6 is not available */
2852 if (!socket_ipv6_is_supported())
2855 if (link
->flags
& IFF_LOOPBACK
)
2861 if (link
->network
->ipv6_hop_limit
< 0)
2864 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2866 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2871 static int link_set_ipv6_mtu(Link
*link
) {
2874 /* Make this a NOP if IPv6 is not available */
2875 if (!socket_ipv6_is_supported())
2878 if (link
->flags
& IFF_LOOPBACK
)
2881 if (link
->network
->ipv6_mtu
== 0)
2884 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2886 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2891 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2892 Address
*net_address
;
2900 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2901 if (address_equal(net_address
, address
))
2907 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2916 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2917 if (route_equal(net_route
, route
))
2923 static int link_drop_foreign_config(Link
*link
) {
2929 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2930 /* we consider IPv6LL addresses to be managed by the kernel */
2931 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2934 if (link_is_static_address_configured(link
, address
)) {
2935 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2937 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2939 r
= address_remove(address
, link
, NULL
);
2945 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2946 /* do not touch routes managed by the kernel */
2947 if (route
->protocol
== RTPROT_KERNEL
)
2950 if (link_is_static_route_configured(link
, route
)) {
2951 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2955 r
= route_remove(route
, link
, NULL
);
2964 static int link_drop_config(Link
*link
) {
2965 Address
*address
, *pool_address
;
2970 SET_FOREACH(address
, link
->addresses
, i
) {
2971 /* we consider IPv6LL addresses to be managed by the kernel */
2972 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2975 r
= address_remove(address
, link
, NULL
);
2979 /* If this address came from an address pool, clean up the pool */
2980 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2981 if (address_equal(address
, pool_address
)) {
2982 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2983 address_free(pool_address
);
2989 SET_FOREACH(route
, link
->routes
, i
) {
2990 /* do not touch routes managed by the kernel */
2991 if (route
->protocol
== RTPROT_KERNEL
)
2994 r
= route_remove(route
, link
, NULL
);
3004 static int link_update_lldp(Link
*link
) {
3012 if (link
->flags
& IFF_UP
) {
3013 r
= sd_lldp_start(link
->lldp
);
3015 log_link_debug(link
, "Started LLDP.");
3017 r
= sd_lldp_stop(link
->lldp
);
3019 log_link_debug(link
, "Stopped LLDP.");
3025 static int link_configure_can(Link
*link
) {
3028 if (streq_ptr(link
->kind
, "can")) {
3029 /* The CAN interface must be down to configure bitrate, etc... */
3030 if ((link
->flags
& IFF_UP
)) {
3031 r
= link_down(link
);
3033 link_enter_failed(link
);
3040 return link_set_can(link
);
3043 if (!(link
->flags
& IFF_UP
)) {
3044 r
= link_up_can(link
);
3046 link_enter_failed(link
);
3054 static int link_configure(Link
*link
) {
3058 assert(link
->network
);
3059 assert(link
->state
== LINK_STATE_INITIALIZED
);
3061 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
3062 return link_configure_can(link
);
3064 /* Drop foreign config, but ignore loopback or critical devices.
3065 * We do not want to remove loopback address or addresses used for root NFS. */
3066 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
3067 r
= link_drop_foreign_config(link
);
3072 r
= link_set_proxy_arp(link
);
3076 r
= ipv6_proxy_ndp_addresses_configure(link
);
3080 r
= link_set_ipv4_forward(link
);
3084 r
= link_set_ipv6_forward(link
);
3088 r
= link_set_ipv6_privacy_extensions(link
);
3092 r
= link_set_ipv6_accept_ra(link
);
3096 r
= link_set_ipv6_dad_transmits(link
);
3100 r
= link_set_ipv6_hop_limit(link
);
3104 r
= link_set_flags(link
);
3108 r
= link_set_ipv6_mtu(link
);
3112 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
3113 r
= ipv4ll_configure(link
);
3118 if (link_dhcp4_enabled(link
)) {
3119 r
= dhcp4_set_promote_secondaries(link
);
3123 r
= dhcp4_configure(link
);
3128 if (link_dhcp4_server_enabled(link
)) {
3129 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3133 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3138 if (link_dhcp6_enabled(link
) ||
3139 link_ipv6_accept_ra_enabled(link
)) {
3140 r
= dhcp6_configure(link
);
3145 if (link_ipv6_accept_ra_enabled(link
)) {
3146 r
= ndisc_configure(link
);
3151 if (link_radv_enabled(link
)) {
3152 r
= radv_configure(link
);
3157 if (link_lldp_rx_enabled(link
)) {
3158 r
= sd_lldp_new(&link
->lldp
);
3162 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3166 r
= sd_lldp_match_capabilities(link
->lldp
,
3167 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3168 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3169 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3173 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3177 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3181 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3185 r
= link_update_lldp(link
);
3190 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
3194 if (socket_ipv6_is_supported()) {
3195 r
= link_configure_addrgen_mode(link
);
3200 return link_configure_after_setting_mtu(link
);
3203 static int link_configure_after_setting_mtu(Link
*link
) {
3207 assert(link
->network
);
3208 assert(link
->state
== LINK_STATE_INITIALIZED
);
3210 if (link
->setting_mtu
)
3213 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3214 r
= link_acquire_conf(link
);
3219 return link_enter_join_netdev(link
);
3222 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3225 if (duid
->raw_data_len
> 0)
3228 if (duid
->type
!= DUID_TYPE_UUID
)
3231 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3232 duid
->raw_data_len
= sizeof(sd_id128_t
);
3237 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3238 Manager
*manager
= userdata
;
3239 const sd_bus_error
*e
;
3249 e
= sd_bus_message_get_error(m
);
3251 log_error_errno(sd_bus_error_get_errno(e
),
3252 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3257 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3261 if (sz
!= sizeof(sd_id128_t
)) {
3262 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3266 memcpy(&manager
->product_uuid
, a
, sz
);
3267 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3268 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3270 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3273 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3274 r
= link_configure(link
);
3276 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3279 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3281 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3282 * even if the method fails. */
3283 manager
->has_product_uuid
= true;
3288 static bool link_requires_uuid(Link
*link
) {
3292 assert(link
->manager
);
3293 assert(link
->network
);
3295 duid
= link_get_duid(link
);
3296 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3299 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3302 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3308 static int link_configure_duid(Link
*link
) {
3314 assert(link
->manager
);
3315 assert(link
->network
);
3318 duid
= link_get_duid(link
);
3320 if (!link_requires_uuid(link
))
3323 if (m
->has_product_uuid
) {
3324 (void) duid_set_uuid(duid
, m
->product_uuid
);
3328 if (!m
->links_requesting_uuid
) {
3329 r
= manager_request_product_uuid(m
, link
);
3334 log_link_warning_errno(link
, r
,
3335 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3339 r
= set_put(m
->links_requesting_uuid
, link
);
3343 r
= set_put(m
->duids_requesting_uuid
, duid
);
3351 static int link_initialized_and_synced(Link
*link
) {
3356 assert(link
->ifname
);
3357 assert(link
->manager
);
3359 /* We may get called either from the asynchronous netlink callback,
3360 * or directly for link_add() if running in a container. See link_add(). */
3361 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3364 log_link_debug(link
, "Link state is up-to-date");
3365 link_set_state(link
, LINK_STATE_INITIALIZED
);
3367 r
= link_new_bound_by_list(link
);
3371 r
= link_handle_bound_by_list(link
);
3375 if (!link
->network
) {
3376 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3377 &link
->mac
, &network
);
3379 link_enter_unmanaged(link
);
3381 } else if (r
== 0 && network
->unmanaged
) {
3382 link_enter_unmanaged(link
);
3387 if (link
->flags
& IFF_LOOPBACK
) {
3388 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3389 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3391 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3392 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3394 if (network
->dhcp_server
)
3395 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3398 r
= network_apply(network
, link
);
3403 r
= link_new_bound_to_list(link
);
3407 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3408 * link_configure() is called later asynchronously. */
3409 r
= link_configure_duid(link
);
3413 r
= link_configure(link
);
3420 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3421 (void) link_initialized_and_synced(link
);
3425 int link_initialized(Link
*link
, sd_device
*device
) {
3426 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3430 assert(link
->manager
);
3431 assert(link
->manager
->rtnl
);
3434 if (link
->state
!= LINK_STATE_PENDING
)
3437 if (link
->sd_device
)
3440 log_link_debug(link
, "udev initialized link");
3441 link_set_state(link
, LINK_STATE_INITIALIZED
);
3443 link
->sd_device
= sd_device_ref(device
);
3445 /* udev has initialized the link, but we don't know if we have yet
3446 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3447 * when it returns we know that the pending NEWLINKs have already been
3448 * processed and that we are up-to-date */
3450 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3455 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3456 link_netlink_destroy_callback
, link
);
3465 static int link_load(Link
*link
) {
3466 _cleanup_free_
char *network_file
= NULL
,
3469 *dhcp4_address
= NULL
,
3470 *ipv4ll_address
= NULL
;
3471 union in_addr_union address
;
3472 union in_addr_union route_dst
;
3478 r
= parse_env_file(NULL
, link
->state_file
,
3479 "NETWORK_FILE", &network_file
,
3480 "ADDRESSES", &addresses
,
3482 "DHCP4_ADDRESS", &dhcp4_address
,
3483 "IPV4LL_ADDRESS", &ipv4ll_address
);
3484 if (r
< 0 && r
!= -ENOENT
)
3485 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3492 suffix
= strrchr(network_file
, '.');
3494 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3495 goto network_file_fail
;
3499 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3501 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3502 goto network_file_fail
;
3505 r
= network_apply(network
, link
);
3507 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3516 _cleanup_free_
char *address_str
= NULL
;
3517 char *prefixlen_str
;
3519 unsigned char prefixlen
;
3521 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3523 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3529 prefixlen_str
= strchr(address_str
, '/');
3530 if (!prefixlen_str
) {
3531 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3535 *prefixlen_str
++ = '\0';
3537 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3539 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3543 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3545 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3549 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3551 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3560 _cleanup_free_
char *route_str
= NULL
;
3561 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3563 char *prefixlen_str
;
3565 unsigned char prefixlen
, tos
, table
;
3568 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3570 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3576 prefixlen_str
= strchr(route_str
, '/');
3577 if (!prefixlen_str
) {
3578 log_link_debug(link
, "Failed to parse route %s", route_str
);
3582 *prefixlen_str
++ = '\0';
3584 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3586 log_link_debug(link
,
3587 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3592 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3594 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3598 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3600 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3602 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3603 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3604 0, route_expire_handler
, route
);
3606 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3609 route
->lifetime
= lifetime
;
3610 sd_event_source_unref(route
->expire
);
3611 route
->expire
= TAKE_PTR(expire
);
3615 if (dhcp4_address
) {
3616 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3618 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3619 goto dhcp4_address_fail
;
3622 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3624 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3626 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3628 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3633 if (ipv4ll_address
) {
3634 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3636 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3637 goto ipv4ll_address_fail
;
3640 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3642 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3644 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3646 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3649 ipv4ll_address_fail
:
3654 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3655 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3656 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3665 r
= link_new(m
, message
, ret
);
3671 log_link_debug(link
, "Link %d added", link
->ifindex
);
3673 r
= link_load(link
);
3677 if (detect_container() <= 0) {
3678 /* not in a container, udev will be around */
3679 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3680 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3682 log_link_warning_errno(link
, r
, "Could not find device: %m");
3686 r
= sd_device_get_is_initialized(device
);
3688 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3693 log_link_debug(link
, "link pending udev initialization...");
3697 r
= device_is_renaming(device
);
3699 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3703 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3707 r
= link_initialized(link
, device
);
3711 r
= link_initialized_and_synced(link
);
3718 link_enter_failed(link
);
3722 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3727 log_link_info(link
, "Gained IPv6LL");
3729 link
->ipv6ll_address
= *address
;
3730 link_check_ready(link
);
3732 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3733 r
= link_acquire_ipv6_conf(link
);
3735 link_enter_failed(link
);
3743 static int link_carrier_gained(Link
*link
) {
3748 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3749 r
= link_acquire_conf(link
);
3751 link_enter_failed(link
);
3755 r
= link_request_set_addresses(link
);
3760 r
= link_handle_bound_by_list(link
);
3767 static int link_carrier_lost(Link
*link
) {
3772 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3775 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3776 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3777 * configuration and stop the clients as well. */
3778 if (link
->setting_mtu
)
3781 r
= link_stop_clients(link
);
3783 link_enter_failed(link
);
3787 if (link_dhcp4_server_enabled(link
))
3788 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3790 r
= link_drop_config(link
);
3794 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3795 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3796 r
= link_drop_foreign_config(link
);
3801 r
= link_handle_bound_by_list(link
);
3808 int link_carrier_reset(Link
*link
) {
3813 if (link_has_carrier(link
)) {
3814 r
= link_carrier_lost(link
);
3818 r
= link_carrier_gained(link
);
3822 log_link_info(link
, "Reset carrier");
3828 int link_update(Link
*link
, sd_netlink_message
*m
) {
3829 struct ether_addr mac
;
3832 bool had_carrier
, carrier_gained
, carrier_lost
;
3836 assert(link
->ifname
);
3839 if (link
->state
== LINK_STATE_LINGER
) {
3840 log_link_info(link
, "Link re-added");
3841 link_set_state(link
, LINK_STATE_CONFIGURING
);
3843 r
= link_new_carrier_maps(link
);
3848 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3849 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3850 Manager
*manager
= link
->manager
;
3852 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3855 r
= link_add(manager
, m
, &link
);
3860 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3861 if (r
>= 0 && mtu
> 0) {
3863 if (link
->original_mtu
== 0) {
3864 link
->original_mtu
= mtu
;
3865 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3868 if (link
->dhcp_client
) {
3869 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3872 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3876 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3878 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3882 /* The kernel may broadcast NEWLINK messages without the MAC address
3883 set, simply ignore them. */
3884 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3886 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3889 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3892 log_link_debug(link
, "MAC address: "
3893 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3894 mac
.ether_addr_octet
[0],
3895 mac
.ether_addr_octet
[1],
3896 mac
.ether_addr_octet
[2],
3897 mac
.ether_addr_octet
[3],
3898 mac
.ether_addr_octet
[4],
3899 mac
.ether_addr_octet
[5]);
3902 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3904 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3907 if (link
->dhcp_client
) {
3908 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3909 (const uint8_t *) &link
->mac
,
3913 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3915 r
= dhcp4_set_client_identifier(link
);
3920 if (link
->dhcp6_client
) {
3921 const DUID
* duid
= link_get_duid(link
);
3923 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3924 (const uint8_t *) &link
->mac
,
3928 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3930 if (link
->network
->iaid_set
) {
3931 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3932 link
->network
->iaid
);
3934 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3937 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3939 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3940 duid
->raw_data_len
);
3942 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3946 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3948 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3952 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3954 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3959 had_carrier
= link_has_carrier(link
);
3961 r
= link_update_flags(link
, m
);
3965 r
= link_update_lldp(link
);
3969 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3970 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3972 if (carrier_gained
) {
3973 log_link_info(link
, "Gained carrier");
3975 r
= link_carrier_gained(link
);
3978 } else if (carrier_lost
) {
3979 log_link_info(link
, "Lost carrier");
3981 r
= link_carrier_lost(link
);
3989 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3997 if (hashmap_isempty(h
))
4001 HASHMAP_FOREACH(link
, h
, i
) {
4005 fprintf(f
, "%i", link
->ifindex
);
4012 int link_save(Link
*link
) {
4013 _cleanup_free_
char *temp_path
= NULL
;
4014 _cleanup_fclose_
FILE *f
= NULL
;
4015 const char *admin_state
, *oper_state
;
4022 assert(link
->state_file
);
4023 assert(link
->lease_file
);
4024 assert(link
->manager
);
4026 if (link
->state
== LINK_STATE_LINGER
) {
4027 (void) unlink(link
->state_file
);
4031 link_lldp_save(link
);
4033 admin_state
= link_state_to_string(link
->state
);
4034 assert(admin_state
);
4036 oper_state
= link_operstate_to_string(link
->operstate
);
4039 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4043 (void) fchmod(fileno(f
), 0644);
4046 "# This is private data. Do not parse.\n"
4049 admin_state
, oper_state
);
4051 if (link
->network
) {
4053 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
4054 const char *dhcp_domainname
= NULL
;
4055 char **dhcp6_domains
= NULL
;
4056 char **dhcp_domains
= NULL
;
4059 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4060 yes_no(link
->network
->required_for_online
));
4062 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
4063 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
4065 if (link
->dhcp6_client
) {
4066 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4067 if (r
< 0 && r
!= -ENOMSG
)
4068 log_link_debug(link
, "No DHCPv6 lease");
4071 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4076 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
4077 _cleanup_free_
char *b
= NULL
;
4079 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
4080 &link
->network
->dns
[j
].address
, &b
);
4082 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4092 if (link
->network
->dhcp_use_dns
&&
4094 const struct in_addr
*addresses
;
4096 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4098 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4102 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4103 struct in6_addr
*in6_addrs
;
4105 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4109 serialize_in6_addrs(f
, in6_addrs
, r
);
4114 /* Make sure to flush out old entries before we use the NDISC data */
4117 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4120 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4124 serialize_in6_addrs(f
, &dd
->address
, 1);
4133 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4135 if (link
->network
->dhcp_use_ntp
&&
4137 const struct in_addr
*addresses
;
4139 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4141 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4145 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4146 struct in6_addr
*in6_addrs
;
4149 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4154 serialize_in6_addrs(f
, in6_addrs
, r
);
4158 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4160 fputstrv(f
, hosts
, NULL
, &space
);
4165 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4166 if (link
->dhcp_lease
) {
4167 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4168 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4171 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4174 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4176 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4179 if (dhcp_domainname
)
4180 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4182 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4184 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4186 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4187 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4192 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4194 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4197 if (dhcp_domainname
)
4198 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4200 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4202 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4204 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4205 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4210 fprintf(f
, "LLMNR=%s\n",
4211 resolve_support_to_string(link
->network
->llmnr
));
4212 fprintf(f
, "MDNS=%s\n",
4213 resolve_support_to_string(link
->network
->mdns
));
4214 if (link
->network
->dns_default_route
>= 0)
4215 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4217 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4218 fprintf(f
, "DNS_OVER_TLS=%s\n",
4219 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4221 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4222 fprintf(f
, "DNSSEC=%s\n",
4223 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4225 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4228 fputs("DNSSEC_NTA=", f
);
4230 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4231 fputs_with_space(f
, n
, NULL
, &space
);
4235 fputs("ADDRESSES=", f
);
4237 SET_FOREACH(a
, link
->addresses
, i
) {
4238 _cleanup_free_
char *address_str
= NULL
;
4240 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4244 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4249 fputs("ROUTES=", f
);
4251 SET_FOREACH(route
, link
->routes
, i
) {
4252 _cleanup_free_
char *route_str
= NULL
;
4254 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4258 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4259 space
? " " : "", route_str
,
4260 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4267 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4268 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4270 if (link
->dhcp_lease
) {
4271 struct in_addr address
;
4272 const char *tz
= NULL
;
4274 assert(link
->network
);
4276 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4278 fprintf(f
, "TIMEZONE=%s\n", tz
);
4280 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4282 fputs("DHCP4_ADDRESS=", f
);
4283 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4287 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4295 (void) unlink(link
->lease_file
);
4298 struct in_addr address
;
4300 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4302 fputs("IPV4LL_ADDRESS=", f
);
4303 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4308 r
= fflush_and_check(f
);
4312 if (rename(temp_path
, link
->state_file
) < 0) {
4320 (void) unlink(link
->state_file
);
4322 (void) unlink(temp_path
);
4324 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4327 /* The serialized state in /run is no longer up-to-date. */
4328 void link_dirty(Link
*link
) {
4333 /* mark manager dirty as link is dirty */
4334 manager_dirty(link
->manager
);
4336 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4338 /* allocation errors are ignored */
4341 r
= set_put(link
->manager
->dirty_links
, link
);
4343 /* don't take another ref if the link was already dirty */
4349 /* The serialized state in /run is up-to-date */
4350 void link_clean(Link
*link
) {
4352 assert(link
->manager
);
4354 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4357 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4358 [LINK_STATE_PENDING
] = "pending",
4359 [LINK_STATE_INITIALIZED
] = "initialized",
4360 [LINK_STATE_CONFIGURING
] = "configuring",
4361 [LINK_STATE_CONFIGURED
] = "configured",
4362 [LINK_STATE_UNMANAGED
] = "unmanaged",
4363 [LINK_STATE_FAILED
] = "failed",
4364 [LINK_STATE_LINGER
] = "linger",
4367 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);