1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.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_ipv4_forward_enabled(Link
*link
) {
201 if (link
->flags
& IFF_LOOPBACK
)
207 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
210 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
213 static bool link_ipv6_forward_enabled(Link
*link
) {
216 if (!socket_ipv6_is_supported())
219 if (link
->flags
& IFF_LOOPBACK
)
225 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
228 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
231 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
234 static bool link_proxy_arp_enabled(Link
*link
) {
237 if (link
->flags
& IFF_LOOPBACK
)
243 if (link
->network
->proxy_arp
< 0)
249 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
252 if (!socket_ipv6_is_supported())
255 if (link
->flags
& IFF_LOOPBACK
)
261 if (!link_ipv6ll_enabled(link
))
264 /* If unset use system default (enabled if local forwarding is disabled.
265 * disabled if local forwarding is enabled).
266 * If set, ignore or enforce RA independent of local forwarding state.
268 if (link
->network
->ipv6_accept_ra
< 0)
269 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
270 return !link_ipv6_forward_enabled(link
);
271 else if (link
->network
->ipv6_accept_ra
> 0)
272 /* accept RA even if ip_forward is enabled */
279 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
282 if (!socket_ipv6_is_supported())
283 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
285 if (link
->flags
& IFF_LOOPBACK
)
286 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
289 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
291 return link
->network
->ipv6_privacy_extensions
;
294 static int link_enable_ipv6(Link
*link
) {
298 if (link
->flags
& IFF_LOOPBACK
)
301 disabled
= !link_ipv6_enabled(link
);
303 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
305 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
307 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
312 static bool link_is_enslaved(Link
*link
) {
313 if (link
->flags
& IFF_SLAVE
)
314 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
317 if (!link
->enslaved_raw
)
323 if (link
->network
->bridge
)
324 /* TODO: support the case when link is not managed by networkd. */
330 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
336 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
339 link_update_operstate(master
, true);
342 void link_update_operstate(Link
*link
, bool also_update_master
) {
343 LinkOperationalState operstate
;
348 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
349 operstate
= LINK_OPERSTATE_DORMANT
;
350 else if (link_has_carrier(link
)) {
352 uint8_t scope
= RT_SCOPE_NOWHERE
;
354 /* if we have carrier, check what addresses we have */
355 SET_FOREACH(address
, link
->addresses
, i
) {
356 if (!address_is_ready(address
))
359 if (address
->scope
< scope
)
360 scope
= address
->scope
;
363 /* for operstate we also take foreign addresses into account */
364 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
365 if (!address_is_ready(address
))
368 if (address
->scope
< scope
)
369 scope
= address
->scope
;
372 if (scope
< RT_SCOPE_SITE
)
373 /* universally accessible addresses found */
374 operstate
= LINK_OPERSTATE_ROUTABLE
;
375 else if (scope
< RT_SCOPE_HOST
)
376 /* only link or site local addresses found */
377 operstate
= LINK_OPERSTATE_DEGRADED
;
379 /* no useful addresses found */
380 operstate
= LINK_OPERSTATE_CARRIER
;
381 } else if (link
->flags
& IFF_UP
)
382 operstate
= LINK_OPERSTATE_NO_CARRIER
;
384 operstate
= LINK_OPERSTATE_OFF
;
386 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
387 link_is_enslaved(link
))
388 operstate
= LINK_OPERSTATE_ENSLAVED
;
390 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
393 SET_FOREACH(slave
, link
->slaves
, i
) {
394 link_update_operstate(slave
, false);
396 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
397 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
401 if (link
->operstate
!= operstate
) {
402 link
->operstate
= operstate
;
403 link_send_changed(link
, "OperationalState", NULL
);
407 if (also_update_master
&& link
->network
) {
408 link_update_master_operstate(link
, link
->network
->bond
);
409 link_update_master_operstate(link
, link
->network
->bridge
);
413 #define FLAG_STRING(string, flag, old, new) \
414 (((old ^ new) & flag) \
415 ? ((old & flag) ? (" -" string) : (" +" string)) \
418 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
419 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
425 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
427 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
429 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
431 /* if we got a message without operstate, take it to mean
432 the state was unchanged */
433 operstate
= link
->kernel_operstate
;
435 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
438 if (link
->flags
!= flags
) {
439 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",
440 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
441 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
442 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
443 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
444 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
445 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
446 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
447 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
448 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
449 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
450 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
451 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
452 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
453 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
454 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
455 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
456 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
457 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
458 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
460 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
461 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
462 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
463 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
464 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
465 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
466 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
467 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
469 /* link flags are currently at most 18 bits, let's align to
471 if (unknown_flags_added
)
473 "Unknown link flags gained: %#.5x (ignoring)",
474 unknown_flags_added
);
476 if (unknown_flags_removed
)
478 "Unknown link flags lost: %#.5x (ignoring)",
479 unknown_flags_removed
);
483 link
->kernel_operstate
= operstate
;
485 link_update_operstate(link
, true);
490 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
491 _cleanup_(link_unrefp
) Link
*link
= NULL
;
493 const char *ifname
, *kind
= NULL
;
495 unsigned short iftype
;
501 /* check for link kind */
502 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
504 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
505 r
= sd_netlink_message_exit_container(message
);
510 r
= sd_netlink_message_get_type(message
, &type
);
513 else if (type
!= RTM_NEWLINK
)
516 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
519 else if (ifindex
<= 0)
522 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
526 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
537 .state
= LINK_STATE_PENDING
,
538 .rtnl_extended_attrs
= true,
543 link
->ifname
= strdup(ifname
);
548 link
->kind
= strdup(kind
);
553 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
555 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
557 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
559 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
561 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
564 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
567 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
570 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
574 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
578 r
= link_update_flags(link
, message
);
582 *ret
= TAKE_PTR(link
);
587 static Link
*link_free(Link
*link
) {
592 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
593 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
595 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
596 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
598 while ((address
= link
->pool_addresses
)) {
599 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
600 address_free(address
);
603 sd_dhcp_server_unref(link
->dhcp_server
);
604 sd_dhcp_client_unref(link
->dhcp_client
);
605 sd_dhcp_lease_unref(link
->dhcp_lease
);
607 link_lldp_emit_stop(link
);
609 free(link
->lease_file
);
611 sd_lldp_unref(link
->lldp
);
612 free(link
->lldp_file
);
616 sd_ipv4ll_unref(link
->ipv4ll
);
617 sd_dhcp6_client_unref(link
->dhcp6_client
);
618 sd_ndisc_unref(link
->ndisc
);
619 sd_radv_unref(link
->radv
);
624 (void) unlink(link
->state_file
);
625 free(link
->state_file
);
627 sd_device_unref(link
->sd_device
);
629 hashmap_free(link
->bound_to_links
);
630 hashmap_free(link
->bound_by_links
);
632 set_free_with_destructor(link
->slaves
, link_unref
);
634 network_unref(link
->network
);
639 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
641 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
648 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
657 static void link_set_state(Link
*link
, LinkState state
) {
660 if (link
->state
== state
)
663 log_link_debug(link
, "State changed: %s -> %s",
664 link_state_to_string(link
->state
),
665 link_state_to_string(state
));
669 link_send_changed(link
, "AdministrativeState", NULL
);
672 static void link_enter_unmanaged(Link
*link
) {
675 log_link_debug(link
, "Unmanaged");
677 link_set_state(link
, LINK_STATE_UNMANAGED
);
682 int link_stop_clients(Link
*link
) {
686 assert(link
->manager
);
687 assert(link
->manager
->event
);
689 if (link
->dhcp_client
) {
690 k
= sd_dhcp_client_stop(link
->dhcp_client
);
692 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
696 k
= sd_ipv4ll_stop(link
->ipv4ll
);
698 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
701 if (link
->dhcp6_client
) {
702 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
704 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
708 k
= sd_ndisc_stop(link
->ndisc
);
710 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
714 k
= sd_radv_stop(link
->radv
);
716 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
719 link_lldp_emit_stop(link
);
723 void link_enter_failed(Link
*link
) {
726 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
729 log_link_warning(link
, "Failed");
731 link_set_state(link
, LINK_STATE_FAILED
);
733 link_stop_clients(link
);
738 static Address
* link_find_dhcp_server_address(Link
*link
) {
742 assert(link
->network
);
744 /* The first statically configured address if there is any */
745 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
747 if (address
->family
!= AF_INET
)
750 if (in_addr_is_null(address
->family
, &address
->in_addr
))
756 /* If that didn't work, find a suitable address we got from the pool */
757 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
758 if (address
->family
!= AF_INET
)
767 static int link_join_netdevs_after_configured(Link
*link
) {
772 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
773 if (netdev
->ifindex
> 0)
774 /* Assume already enslaved. */
777 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
780 log_struct(LOG_DEBUG
,
781 LOG_LINK_INTERFACE(link
),
782 LOG_NETDEV_INTERFACE(netdev
),
783 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
785 r
= netdev_join(netdev
, link
, NULL
);
787 return log_struct_errno(LOG_WARNING
, r
,
788 LOG_LINK_INTERFACE(link
),
789 LOG_NETDEV_INTERFACE(netdev
),
790 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
796 static void link_enter_configured(Link
*link
) {
798 assert(link
->network
);
800 if (link
->state
!= LINK_STATE_CONFIGURING
)
803 log_link_info(link
, "Configured");
805 link_set_state(link
, LINK_STATE_CONFIGURED
);
807 (void) link_join_netdevs_after_configured(link
);
812 static int link_request_set_routing_policy_rule(Link
*link
) {
813 RoutingPolicyRule
*rule
, *rrule
= NULL
;
817 assert(link
->network
);
819 link_set_state(link
, LINK_STATE_CONFIGURING
);
820 link
->routing_policy_rules_configured
= false;
822 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
823 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
824 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
825 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
827 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
831 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
833 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
834 link_enter_failed(link
);
838 link
->routing_policy_rule_messages
++;
841 routing_policy_rule_purge(link
->manager
, link
);
842 if (link
->routing_policy_rule_messages
== 0) {
843 link
->routing_policy_rules_configured
= true;
844 link_check_ready(link
);
846 log_link_debug(link
, "Setting routing policy rules");
851 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
855 assert(link
->route_messages
> 0);
856 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
857 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
859 link
->route_messages
--;
861 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
864 r
= sd_netlink_message_get_errno(m
);
865 if (r
< 0 && r
!= -EEXIST
)
866 log_link_warning_errno(link
, r
, "Could not set route: %m");
868 if (link
->route_messages
== 0) {
869 log_link_debug(link
, "Routes set");
870 link
->static_routes_configured
= true;
871 link_check_ready(link
);
877 int link_request_set_routes(Link
*link
) {
879 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
880 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
887 assert(link
->network
);
888 assert(link
->addresses_configured
);
889 assert(link
->address_messages
== 0);
890 assert(link
->state
!= _LINK_STATE_INVALID
);
892 link_set_state(link
, LINK_STATE_CONFIGURING
);
893 link
->static_routes_configured
= false;
895 r
= link_request_set_routing_policy_rule(link
);
899 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
900 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
901 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
903 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
906 r
= route_configure(rt
, link
, route_handler
);
908 log_link_warning_errno(link
, r
, "Could not set routes: %m");
909 link_enter_failed(link
);
913 link
->route_messages
++;
916 if (link
->route_messages
== 0) {
917 link
->static_routes_configured
= true;
918 link_check_ready(link
);
920 log_link_debug(link
, "Setting routes");
925 void link_check_ready(Link
*link
) {
931 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
937 if (!link
->addresses_configured
)
940 if (!link
->neighbors_configured
)
943 SET_FOREACH(a
, link
->addresses
, i
)
944 if (!address_is_ready(a
))
947 if (!link
->addresses_ready
) {
948 link
->addresses_ready
= true;
949 link_request_set_routes(link
);
952 if (!link
->static_routes_configured
)
955 if (!link
->routing_policy_rules_configured
)
958 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
961 if (link_ipv6ll_enabled(link
) &&
962 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
965 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
966 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
967 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
968 /* When DHCP is enabled, at least one protocol must provide an address, or
969 * an IPv4ll fallback address must be configured. */
972 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
975 if (link
->state
!= LINK_STATE_CONFIGURED
)
976 link_enter_configured(link
);
981 static int link_request_set_neighbors(Link
*link
) {
986 assert(link
->network
);
987 assert(link
->state
!= _LINK_STATE_INVALID
);
989 link_set_state(link
, LINK_STATE_CONFIGURING
);
990 link
->neighbors_configured
= false;
992 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
993 r
= neighbor_configure(neighbor
, link
, NULL
);
995 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
996 link_enter_failed(link
);
1001 if (link
->neighbor_messages
== 0) {
1002 link
->neighbors_configured
= true;
1003 link_check_ready(link
);
1005 log_link_debug(link
, "Setting neighbors");
1010 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1016 assert(link
->ifname
);
1017 assert(link
->address_messages
> 0);
1018 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1019 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1021 link
->address_messages
--;
1023 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1026 r
= sd_netlink_message_get_errno(m
);
1027 if (r
< 0 && r
!= -EEXIST
)
1028 log_link_warning_errno(link
, r
, "could not set address: %m");
1030 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1032 if (link
->address_messages
== 0) {
1033 log_link_debug(link
, "Addresses set");
1034 link
->addresses_configured
= true;
1035 link_check_ready(link
);
1041 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1042 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1043 size_t n_addresses
= 0, n_allocated
= 0;
1046 log_debug("Copying DNS server information from %s", link
->ifname
);
1051 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1054 /* Only look for IPv4 addresses */
1055 if (link
->network
->dns
[i
].family
!= AF_INET
)
1058 ia
= link
->network
->dns
[i
].address
.in
;
1060 /* Never propagate obviously borked data */
1061 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1064 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1067 addresses
[n_addresses
++] = ia
;
1070 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1071 const struct in_addr
*da
= NULL
;
1074 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1077 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1080 for (j
= 0; j
< n
; j
++)
1081 if (in4_addr_is_non_local(&da
[j
]))
1082 addresses
[n_addresses
++] = da
[j
];
1086 if (n_addresses
<= 0)
1089 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1092 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1093 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1094 size_t n_addresses
= 0, n_allocated
= 0;
1100 log_debug("Copying NTP server information from %s", link
->ifname
);
1102 STRV_FOREACH(a
, link
->network
->ntp
) {
1103 union in_addr_union ia
;
1105 /* Only look for IPv4 addresses */
1106 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1109 /* Never propagate obviously borked data */
1110 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1113 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1116 addresses
[n_addresses
++] = ia
.in
;
1119 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1120 const struct in_addr
*da
= NULL
;
1123 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1126 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1129 for (j
= 0; j
< n
; j
++)
1130 if (in4_addr_is_non_local(&da
[j
]))
1131 addresses
[n_addresses
++] = da
[j
];
1135 if (n_addresses
<= 0)
1138 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1141 static int link_set_bridge_fdb(Link
*link
) {
1142 FdbEntry
*fdb_entry
;
1145 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1146 r
= fdb_entry_configure(link
, fdb_entry
);
1148 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1154 static int link_request_set_addresses(Link
*link
) {
1155 AddressLabel
*label
;
1160 assert(link
->network
);
1161 assert(link
->state
!= _LINK_STATE_INVALID
);
1163 link_set_state(link
, LINK_STATE_CONFIGURING
);
1165 /* Reset all *_configured flags we are configuring. */
1166 link
->addresses_configured
= false;
1167 link
->addresses_ready
= false;
1168 link
->neighbors_configured
= false;
1169 link
->static_routes_configured
= false;
1170 link
->routing_policy_rules_configured
= false;
1172 r
= link_set_bridge_fdb(link
);
1176 r
= link_request_set_neighbors(link
);
1180 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1183 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1185 r
= address_configure(ad
, link
, address_handler
, update
);
1187 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1188 link_enter_failed(link
);
1192 link
->address_messages
++;
1195 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1196 r
= address_label_configure(label
, link
, NULL
, false);
1198 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1199 link_enter_failed(link
);
1203 link
->address_label_messages
++;
1206 /* now that we can figure out a default address for the dhcp server,
1208 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1210 Link
*uplink
= NULL
;
1211 bool acquired_uplink
= false;
1213 address
= link_find_dhcp_server_address(link
);
1215 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1216 link_enter_failed(link
);
1220 /* use the server address' subnet as the pool */
1221 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1222 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1227 r = sd_dhcp_server_set_router(link->dhcp_server,
1228 &main_address->in_addr.in);
1233 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1234 r
= sd_dhcp_server_set_max_lease_time(
1236 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1241 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1242 r
= sd_dhcp_server_set_default_lease_time(
1244 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1249 if (link
->network
->dhcp_server_emit_dns
) {
1251 if (link
->network
->n_dhcp_server_dns
> 0)
1252 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1254 uplink
= manager_find_uplink(link
->manager
, link
);
1255 acquired_uplink
= true;
1258 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1261 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1264 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1267 if (link
->network
->dhcp_server_emit_ntp
) {
1269 if (link
->network
->n_dhcp_server_ntp
> 0)
1270 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1272 if (!acquired_uplink
)
1273 uplink
= manager_find_uplink(link
->manager
, link
);
1276 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1279 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1283 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1286 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1288 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1290 if (link
->network
->dhcp_server_emit_timezone
) {
1291 _cleanup_free_
char *buffer
= NULL
;
1292 const char *tz
= NULL
;
1294 if (link
->network
->dhcp_server_timezone
)
1295 tz
= link
->network
->dhcp_server_timezone
;
1297 r
= get_timezone(&buffer
);
1299 log_warning_errno(r
, "Failed to determine timezone: %m");
1305 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1310 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1311 r
= sd_dhcp_server_start(link
->dhcp_server
);
1313 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1315 link_enter_failed(link
);
1321 log_link_debug(link
, "Offering DHCPv4 leases");
1324 if (link
->address_messages
== 0) {
1325 link
->addresses_configured
= true;
1326 link_check_ready(link
);
1328 log_link_debug(link
, "Setting addresses");
1333 static int link_set_bridge_vlan(Link
*link
) {
1336 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1338 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1343 static int link_set_proxy_arp(Link
*link
) {
1346 if (!link_proxy_arp_enabled(link
))
1349 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1351 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1356 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1361 log_link_debug(link
, "Set link");
1363 r
= sd_netlink_message_get_errno(m
);
1364 if (r
< 0 && r
!= -EEXIST
) {
1365 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1366 link_enter_failed(link
);
1372 static int link_configure_after_setting_mtu(Link
*link
);
1374 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1379 assert(link
->ifname
);
1381 link
->setting_mtu
= false;
1383 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1386 r
= sd_netlink_message_get_errno(m
);
1388 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1392 log_link_debug(link
, "Setting MTU done.");
1394 if (link
->state
== LINK_STATE_INITIALIZED
)
1395 (void) link_configure_after_setting_mtu(link
);
1400 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1401 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1405 assert(link
->manager
);
1406 assert(link
->manager
->rtnl
);
1408 if (mtu
== 0 || link
->setting_mtu
)
1411 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1414 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1416 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1418 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1420 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1421 * for this interface, then disable IPv6 else enable it. */
1422 (void) link_enable_ipv6(link
);
1424 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1425 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1426 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1428 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1429 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1434 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1436 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1438 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1439 link_netlink_destroy_callback
, link
);
1441 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1444 link
->setting_mtu
= true;
1449 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1454 assert(link
->ifname
);
1456 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1459 r
= sd_netlink_message_get_errno(m
);
1461 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1466 static int link_set_flags(Link
*link
) {
1467 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1468 unsigned ifi_change
= 0;
1469 unsigned ifi_flags
= 0;
1473 assert(link
->manager
);
1474 assert(link
->manager
->rtnl
);
1476 if (link
->flags
& IFF_LOOPBACK
)
1482 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1485 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1487 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1489 if (link
->network
->arp
>= 0) {
1490 ifi_change
|= IFF_NOARP
;
1491 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1494 if (link
->network
->multicast
>= 0) {
1495 ifi_change
|= IFF_MULTICAST
;
1496 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1499 if (link
->network
->allmulticast
>= 0) {
1500 ifi_change
|= IFF_ALLMULTI
;
1501 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1504 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1506 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1508 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1509 link_netlink_destroy_callback
, link
);
1511 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1518 static int link_set_bridge(Link
*link
) {
1519 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1523 assert(link
->network
);
1525 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1527 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1529 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1531 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1533 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1535 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1537 if (link
->network
->use_bpdu
>= 0) {
1538 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1540 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1543 if (link
->network
->hairpin
>= 0) {
1544 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1546 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1549 if (link
->network
->fast_leave
>= 0) {
1550 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1552 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1555 if (link
->network
->allow_port_to_be_root
>= 0) {
1556 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1558 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1561 if (link
->network
->unicast_flood
>= 0) {
1562 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1564 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1567 if (link
->network
->multicast_flood
>= 0) {
1568 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
1570 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
1573 if (link
->network
->multicast_to_unicast
>= 0) {
1574 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1576 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1579 if (link
->network
->neighbor_suppression
>= 0) {
1580 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
1582 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
1585 if (link
->network
->learning
>= 0) {
1586 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
1588 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
1591 if (link
->network
->bridge_proxy_arp
>= 0) {
1592 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP
, link
->network
->bridge_proxy_arp
);
1594 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP attribute: %m");
1597 if (link
->network
->bridge_proxy_arp_wifi
>= 0) {
1598 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP_WIFI
, link
->network
->bridge_proxy_arp_wifi
);
1600 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP_WIFI attribute: %m");
1603 if (link
->network
->cost
!= 0) {
1604 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1606 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1609 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1610 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1612 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1615 if (link
->network
->multicast_router
!= _MULTICAST_ROUTER_INVALID
) {
1616 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MULTICAST_ROUTER
, link
->network
->multicast_router
);
1618 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MULTICAST_ROUTER attribute: %m");
1621 r
= sd_netlink_message_close_container(req
);
1623 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1625 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1626 link_netlink_destroy_callback
, link
);
1628 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1635 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1640 assert(link
->ifname
);
1642 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1645 r
= sd_netlink_message_get_errno(m
);
1647 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1654 static int link_set_bond(Link
*link
) {
1655 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1659 assert(link
->network
);
1661 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1663 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1665 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1667 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1669 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1671 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1673 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1675 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1677 if (link
->network
->active_slave
) {
1678 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1680 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1683 if (link
->network
->primary_slave
) {
1684 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1686 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1689 r
= sd_netlink_message_close_container(req
);
1691 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1693 r
= sd_netlink_message_close_container(req
);
1695 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1697 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1698 link_netlink_destroy_callback
, link
);
1700 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1707 static int link_acquire_ipv6_conf(Link
*link
) {
1712 if (link_ipv6_accept_ra_enabled(link
)) {
1713 assert(link
->ndisc
);
1715 log_link_debug(link
, "Discovering IPv6 routers");
1717 r
= sd_ndisc_start(link
->ndisc
);
1718 if (r
< 0 && r
!= -EBUSY
)
1719 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1722 if (link_radv_enabled(link
)) {
1724 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1726 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1728 r
= sd_radv_start(link
->radv
);
1729 if (r
< 0 && r
!= -EBUSY
)
1730 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1733 (void) dhcp6_request_prefix_delegation(link
);
1738 static int link_acquire_ipv4_conf(Link
*link
) {
1742 assert(link
->manager
);
1743 assert(link
->manager
->event
);
1745 if (link_ipv4ll_enabled(link
)) {
1746 assert(link
->ipv4ll
);
1748 log_link_debug(link
, "Acquiring IPv4 link-local address");
1750 r
= sd_ipv4ll_start(link
->ipv4ll
);
1752 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1755 if (link_dhcp4_enabled(link
)) {
1756 assert(link
->dhcp_client
);
1758 log_link_debug(link
, "Acquiring DHCPv4 lease");
1760 r
= sd_dhcp_client_start(link
->dhcp_client
);
1762 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1768 static int link_acquire_conf(Link
*link
) {
1773 r
= link_acquire_ipv4_conf(link
);
1777 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1778 r
= link_acquire_ipv6_conf(link
);
1783 if (link_lldp_emit_enabled(link
)) {
1784 r
= link_lldp_emit_start(link
);
1786 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1792 bool link_has_carrier(Link
*link
) {
1793 /* see Documentation/networking/operstates.txt in the kernel sources */
1795 if (link
->kernel_operstate
== IF_OPER_UP
)
1798 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1799 /* operstate may not be implemented, so fall back to flags */
1800 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1806 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1811 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1814 r
= sd_netlink_message_get_errno(m
);
1816 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1821 static int link_configure_addrgen_mode(Link
*link
) {
1822 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1823 uint8_t ipv6ll_mode
;
1827 assert(link
->network
);
1828 assert(link
->manager
);
1829 assert(link
->manager
->rtnl
);
1831 log_link_debug(link
, "Setting address genmode for link");
1833 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1835 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1837 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1839 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1841 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1843 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1845 if (!link_ipv6ll_enabled(link
))
1846 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1847 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1848 /* The file may not exist. And event if it exists, when stable_secret is unset,
1849 * reading the file fails with EIO. */
1850 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1852 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1854 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1856 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1858 r
= sd_netlink_message_close_container(req
);
1860 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1862 r
= sd_netlink_message_close_container(req
);
1864 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1866 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1867 link_netlink_destroy_callback
, link
);
1869 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1876 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1881 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1884 r
= sd_netlink_message_get_errno(m
);
1886 /* we warn but don't fail the link, as it may be brought up later */
1887 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1892 static int link_up(Link
*link
) {
1893 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1897 assert(link
->network
);
1898 assert(link
->manager
);
1899 assert(link
->manager
->rtnl
);
1901 log_link_debug(link
, "Bringing link up");
1903 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1905 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1907 /* set it free if not enslaved with networkd */
1908 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1909 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1911 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1914 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1916 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1918 if (link
->network
->mac
) {
1919 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1921 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1924 if (link_ipv6_enabled(link
)) {
1925 uint8_t ipv6ll_mode
;
1927 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1929 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1931 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1932 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1934 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1936 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1937 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1939 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1942 if (!link_ipv6ll_enabled(link
))
1943 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1944 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1945 /* The file may not exist. And event if it exists, when stable_secret is unset,
1946 * reading the file fails with EIO. */
1947 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1949 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1951 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1953 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1955 r
= sd_netlink_message_close_container(req
);
1957 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1959 r
= sd_netlink_message_close_container(req
);
1961 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1964 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1965 link_netlink_destroy_callback
, link
);
1967 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1974 static int link_up_can(Link
*link
) {
1975 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1980 log_link_debug(link
, "Bringing CAN link up");
1982 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1984 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1986 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1988 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1990 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1991 link_netlink_destroy_callback
, link
);
1993 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2000 static int link_set_can(Link
*link
) {
2001 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2005 assert(link
->network
);
2006 assert(link
->manager
);
2007 assert(link
->manager
->rtnl
);
2009 log_link_debug(link
, "link_set_can");
2011 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2013 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2015 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2017 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2019 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2021 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2023 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2025 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2027 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2028 struct can_bittiming bt
= {
2029 .bitrate
= link
->network
->can_bitrate
,
2030 .sample_point
= link
->network
->can_sample_point
,
2033 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2034 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2038 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2039 if (link
->network
->can_sample_point
> 0)
2040 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2042 log_link_debug(link
, "Using default sample point");
2044 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2046 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2049 if (link
->network
->can_restart_us
> 0) {
2050 char time_string
[FORMAT_TIMESPAN_MAX
];
2051 uint64_t restart_ms
;
2053 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2056 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2058 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2060 if (restart_ms
> UINT32_MAX
) {
2061 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2065 log_link_debug(link
, "Setting restart = %s", time_string
);
2067 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2069 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2072 if (link
->network
->can_triple_sampling
>= 0) {
2073 struct can_ctrlmode cm
= {
2074 .mask
= CAN_CTRLMODE_3_SAMPLES
,
2075 .flags
= link
->network
->can_triple_sampling
? CAN_CTRLMODE_3_SAMPLES
: 0,
2078 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
2080 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
2082 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
2085 r
= sd_netlink_message_close_container(m
);
2087 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2089 r
= sd_netlink_message_close_container(m
);
2091 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2093 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2094 link_netlink_destroy_callback
, link
);
2096 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2100 if (!(link
->flags
& IFF_UP
)) {
2101 r
= link_up_can(link
);
2103 link_enter_failed(link
);
2108 log_link_debug(link
, "link_set_can done");
2113 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2118 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2121 r
= sd_netlink_message_get_errno(m
);
2123 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2125 if (streq_ptr(link
->kind
, "can"))
2131 int link_down(Link
*link
) {
2132 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2136 assert(link
->manager
);
2137 assert(link
->manager
->rtnl
);
2139 log_link_debug(link
, "Bringing link down");
2141 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2142 RTM_SETLINK
, link
->ifindex
);
2144 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2146 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2148 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2150 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2151 link_netlink_destroy_callback
, link
);
2153 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2160 static int link_handle_bound_to_list(Link
*link
) {
2164 bool required_up
= false;
2165 bool link_is_up
= false;
2169 if (hashmap_isempty(link
->bound_to_links
))
2172 if (link
->flags
& IFF_UP
)
2175 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2176 if (link_has_carrier(l
)) {
2181 if (!required_up
&& link_is_up
) {
2182 r
= link_down(link
);
2185 } else if (required_up
&& !link_is_up
) {
2194 static int link_handle_bound_by_list(Link
*link
) {
2201 if (hashmap_isempty(link
->bound_by_links
))
2204 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2205 r
= link_handle_bound_to_list(l
);
2213 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2219 if (link
== carrier
)
2222 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2225 r
= hashmap_ensure_allocated(h
, NULL
);
2229 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2236 static int link_new_bound_by_list(Link
*link
) {
2241 bool list_updated
= false;
2244 assert(link
->manager
);
2248 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2249 if (!carrier
->network
)
2252 if (strv_isempty(carrier
->network
->bind_carrier
))
2255 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2256 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2260 list_updated
= true;
2267 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2268 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2272 link_dirty(carrier
);
2278 static int link_new_bound_to_list(Link
*link
) {
2283 bool list_updated
= false;
2286 assert(link
->manager
);
2291 if (strv_isempty(link
->network
->bind_carrier
))
2296 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2297 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2298 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2302 list_updated
= true;
2309 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2310 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2314 link_dirty(carrier
);
2320 static int link_new_carrier_maps(Link
*link
) {
2323 r
= link_new_bound_by_list(link
);
2327 r
= link_handle_bound_by_list(link
);
2331 r
= link_new_bound_to_list(link
);
2335 r
= link_handle_bound_to_list(link
);
2342 static void link_free_bound_to_list(Link
*link
) {
2346 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2347 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2349 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2350 link_dirty(bound_to
);
2356 static void link_free_bound_by_list(Link
*link
) {
2360 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2361 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2363 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2364 link_dirty(bound_by
);
2365 link_handle_bound_to_list(bound_by
);
2372 static void link_free_carrier_maps(Link
*link
) {
2373 bool list_updated
= false;
2377 if (!hashmap_isempty(link
->bound_to_links
)) {
2378 link_free_bound_to_list(link
);
2379 list_updated
= true;
2382 if (!hashmap_isempty(link
->bound_by_links
)) {
2383 link_free_bound_by_list(link
);
2384 list_updated
= true;
2393 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2400 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2404 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2408 r
= set_put(master
->slaves
, link
);
2416 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2421 if (!link
->manager
|| !netdev
)
2424 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2427 link_unref(set_remove(master
->slaves
, link
));
2430 static void link_detach_from_manager(Link
*link
) {
2431 if (!link
|| !link
->manager
)
2434 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2437 /* The following must be called at last. */
2438 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2442 void link_drop(Link
*link
) {
2443 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2446 link_set_state(link
, LINK_STATE_LINGER
);
2448 link_free_carrier_maps(link
);
2450 if (link
->network
) {
2451 link_drop_from_master(link
, link
->network
->bridge
);
2452 link_drop_from_master(link
, link
->network
->bond
);
2455 log_link_debug(link
, "Link removed");
2457 (void) unlink(link
->state_file
);
2458 link_detach_from_manager(link
);
2461 static int link_joined(Link
*link
) {
2465 assert(link
->network
);
2467 if (!hashmap_isempty(link
->bound_to_links
)) {
2468 r
= link_handle_bound_to_list(link
);
2471 } else if (!(link
->flags
& IFF_UP
)) {
2474 link_enter_failed(link
);
2479 if (link
->network
->bridge
) {
2480 r
= link_set_bridge(link
);
2482 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2484 r
= link_append_to_master(link
, link
->network
->bridge
);
2486 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2489 if (link
->network
->bond
) {
2490 r
= link_set_bond(link
);
2492 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2494 r
= link_append_to_master(link
, link
->network
->bond
);
2496 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2499 if (link
->network
->use_br_vlan
&&
2500 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2501 r
= link_set_bridge_vlan(link
);
2503 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2506 /* Skip setting up addresses until it gets carrier,
2507 or it would try to set addresses twice,
2508 which is bad for non-idempotent steps. */
2509 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2512 return link_request_set_addresses(link
);
2515 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2519 assert(link
->network
);
2520 assert(link
->enslaving
> 0);
2521 assert(!link
->enslaved_raw
);
2525 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2528 r
= sd_netlink_message_get_errno(m
);
2529 if (r
< 0 && r
!= -EEXIST
) {
2530 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2531 link_enter_failed(link
);
2534 log_link_debug(link
, "Joined netdev");
2536 if (link
->enslaving
== 0) {
2537 link
->enslaved_raw
= true;
2544 static int link_enter_join_netdev(Link
*link
) {
2550 assert(link
->network
);
2551 assert(link
->state
== LINK_STATE_INITIALIZED
);
2553 link_set_state(link
, LINK_STATE_CONFIGURING
);
2556 link
->enslaving
= 0;
2557 link
->enslaved_raw
= false;
2559 if (link
->network
->bond
) {
2560 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2561 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2562 return link_joined(link
);
2564 log_struct(LOG_DEBUG
,
2565 LOG_LINK_INTERFACE(link
),
2566 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2567 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2571 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2573 log_struct_errno(LOG_WARNING
, r
,
2574 LOG_LINK_INTERFACE(link
),
2575 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2576 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2577 link_enter_failed(link
);
2582 if (link
->network
->bridge
) {
2583 log_struct(LOG_DEBUG
,
2584 LOG_LINK_INTERFACE(link
),
2585 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2586 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2590 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2592 log_struct_errno(LOG_WARNING
, r
,
2593 LOG_LINK_INTERFACE(link
),
2594 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2595 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2596 link_enter_failed(link
);
2601 if (link
->network
->vrf
) {
2602 log_struct(LOG_DEBUG
,
2603 LOG_LINK_INTERFACE(link
),
2604 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2605 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2609 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2611 log_struct_errno(LOG_WARNING
, r
,
2612 LOG_LINK_INTERFACE(link
),
2613 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2614 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2615 link_enter_failed(link
);
2620 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2622 if (netdev
->ifindex
> 0)
2623 /* Assume already enslaved. */
2626 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2629 log_struct(LOG_DEBUG
,
2630 LOG_LINK_INTERFACE(link
),
2631 LOG_NETDEV_INTERFACE(netdev
),
2632 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2636 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2638 log_struct_errno(LOG_WARNING
, r
,
2639 LOG_LINK_INTERFACE(link
),
2640 LOG_NETDEV_INTERFACE(netdev
),
2641 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2642 link_enter_failed(link
);
2647 if (link
->enslaving
== 0)
2648 return link_joined(link
);
2653 static int link_set_ipv4_forward(Link
*link
) {
2656 if (!link_ipv4_forward_enabled(link
))
2659 /* We propagate the forwarding flag from one interface to the
2660 * global setting one way. This means: as long as at least one
2661 * interface was configured at any time that had IP forwarding
2662 * enabled the setting will stay on for good. We do this
2663 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2664 * somewhat in sync (see below). */
2666 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2668 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2673 static int link_set_ipv6_forward(Link
*link
) {
2676 if (!link_ipv6_forward_enabled(link
))
2679 /* On Linux, the IPv6 stack does not know a per-interface
2680 * packet forwarding setting: either packet forwarding is on
2681 * for all, or off for all. We hence don't bother with a
2682 * per-interface setting, but simply propagate the interface
2683 * flag, if it is set, to the global flag, one-way. Note that
2684 * while IPv4 would allow a per-interface flag, we expose the
2685 * same behaviour there and also propagate the setting from
2686 * one to all, to keep things simple (see above). */
2688 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2690 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2695 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2696 IPv6PrivacyExtensions s
;
2699 s
= link_ipv6_privacy_extensions(link
);
2703 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2705 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2710 static int link_set_ipv6_accept_ra(Link
*link
) {
2713 /* Make this a NOP if IPv6 is not available */
2714 if (!socket_ipv6_is_supported())
2717 if (link
->flags
& IFF_LOOPBACK
)
2723 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2725 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2730 static int link_set_ipv6_dad_transmits(Link
*link
) {
2733 /* Make this a NOP if IPv6 is not available */
2734 if (!socket_ipv6_is_supported())
2737 if (link
->flags
& IFF_LOOPBACK
)
2743 if (link
->network
->ipv6_dad_transmits
< 0)
2746 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2748 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2753 static int link_set_ipv6_hop_limit(Link
*link
) {
2756 /* Make this a NOP if IPv6 is not available */
2757 if (!socket_ipv6_is_supported())
2760 if (link
->flags
& IFF_LOOPBACK
)
2766 if (link
->network
->ipv6_hop_limit
< 0)
2769 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2771 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2776 static int link_set_ipv6_mtu(Link
*link
) {
2779 /* Make this a NOP if IPv6 is not available */
2780 if (!socket_ipv6_is_supported())
2783 if (link
->flags
& IFF_LOOPBACK
)
2786 if (link
->network
->ipv6_mtu
== 0)
2789 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2791 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2796 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2797 Address
*net_address
;
2805 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2806 if (address_equal(net_address
, address
))
2812 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2821 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2822 if (route_equal(net_route
, route
))
2828 static int link_drop_foreign_config(Link
*link
) {
2834 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2835 /* we consider IPv6LL addresses to be managed by the kernel */
2836 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2839 if (link_is_static_address_configured(link
, address
)) {
2840 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2842 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2844 r
= address_remove(address
, link
, NULL
);
2850 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2851 /* do not touch routes managed by the kernel */
2852 if (route
->protocol
== RTPROT_KERNEL
)
2855 if (link_is_static_route_configured(link
, route
)) {
2856 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2860 r
= route_remove(route
, link
, NULL
);
2869 static int link_drop_config(Link
*link
) {
2870 Address
*address
, *pool_address
;
2875 SET_FOREACH(address
, link
->addresses
, i
) {
2876 /* we consider IPv6LL addresses to be managed by the kernel */
2877 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2880 r
= address_remove(address
, link
, NULL
);
2884 /* If this address came from an address pool, clean up the pool */
2885 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2886 if (address_equal(address
, pool_address
)) {
2887 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2888 address_free(pool_address
);
2894 SET_FOREACH(route
, link
->routes
, i
) {
2895 /* do not touch routes managed by the kernel */
2896 if (route
->protocol
== RTPROT_KERNEL
)
2899 r
= route_remove(route
, link
, NULL
);
2909 static int link_configure_can(Link
*link
) {
2912 if (streq_ptr(link
->kind
, "can")) {
2913 /* The CAN interface must be down to configure bitrate, etc... */
2914 if ((link
->flags
& IFF_UP
)) {
2915 r
= link_down(link
);
2917 link_enter_failed(link
);
2924 return link_set_can(link
);
2927 if (!(link
->flags
& IFF_UP
)) {
2928 r
= link_up_can(link
);
2930 link_enter_failed(link
);
2938 static int link_configure(Link
*link
) {
2942 assert(link
->network
);
2943 assert(link
->state
== LINK_STATE_INITIALIZED
);
2945 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2946 return link_configure_can(link
);
2948 /* Drop foreign config, but ignore loopback or critical devices.
2949 * We do not want to remove loopback address or addresses used for root NFS. */
2950 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2951 r
= link_drop_foreign_config(link
);
2956 r
= link_set_proxy_arp(link
);
2960 r
= ipv6_proxy_ndp_addresses_configure(link
);
2964 r
= link_set_ipv4_forward(link
);
2968 r
= link_set_ipv6_forward(link
);
2972 r
= link_set_ipv6_privacy_extensions(link
);
2976 r
= link_set_ipv6_accept_ra(link
);
2980 r
= link_set_ipv6_dad_transmits(link
);
2984 r
= link_set_ipv6_hop_limit(link
);
2988 r
= link_set_flags(link
);
2992 r
= link_set_ipv6_mtu(link
);
2996 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2997 r
= ipv4ll_configure(link
);
3002 if (link_dhcp4_enabled(link
)) {
3003 r
= dhcp4_set_promote_secondaries(link
);
3007 r
= dhcp4_configure(link
);
3012 if (link_dhcp4_server_enabled(link
)) {
3013 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3017 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3022 if (link_dhcp6_enabled(link
) ||
3023 link_ipv6_accept_ra_enabled(link
)) {
3024 r
= dhcp6_configure(link
);
3029 if (link_ipv6_accept_ra_enabled(link
)) {
3030 r
= ndisc_configure(link
);
3035 if (link_radv_enabled(link
)) {
3036 r
= radv_configure(link
);
3041 if (link_lldp_rx_enabled(link
)) {
3042 r
= link_lldp_rx_configure(link
);
3047 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
3051 if (socket_ipv6_is_supported()) {
3052 r
= link_configure_addrgen_mode(link
);
3057 return link_configure_after_setting_mtu(link
);
3060 static int link_configure_after_setting_mtu(Link
*link
) {
3064 assert(link
->network
);
3065 assert(link
->state
== LINK_STATE_INITIALIZED
);
3067 if (link
->setting_mtu
)
3070 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3071 r
= link_acquire_conf(link
);
3076 return link_enter_join_netdev(link
);
3079 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3082 if (duid
->raw_data_len
> 0)
3085 if (duid
->type
!= DUID_TYPE_UUID
)
3088 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3089 duid
->raw_data_len
= sizeof(sd_id128_t
);
3094 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3095 Manager
*manager
= userdata
;
3096 const sd_bus_error
*e
;
3106 e
= sd_bus_message_get_error(m
);
3108 log_error_errno(sd_bus_error_get_errno(e
),
3109 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3114 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3118 if (sz
!= sizeof(sd_id128_t
)) {
3119 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3123 memcpy(&manager
->product_uuid
, a
, sz
);
3124 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3125 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3127 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3130 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3131 r
= link_configure(link
);
3133 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3136 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3138 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3139 * even if the method fails. */
3140 manager
->has_product_uuid
= true;
3145 static bool link_requires_uuid(Link
*link
) {
3149 assert(link
->manager
);
3150 assert(link
->network
);
3152 duid
= link_get_duid(link
);
3153 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3156 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3159 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3165 static int link_configure_duid(Link
*link
) {
3171 assert(link
->manager
);
3172 assert(link
->network
);
3175 duid
= link_get_duid(link
);
3177 if (!link_requires_uuid(link
))
3180 if (m
->has_product_uuid
) {
3181 (void) duid_set_uuid(duid
, m
->product_uuid
);
3185 if (!m
->links_requesting_uuid
) {
3186 r
= manager_request_product_uuid(m
, link
);
3191 log_link_warning_errno(link
, r
,
3192 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3196 r
= set_put(m
->links_requesting_uuid
, link
);
3200 r
= set_put(m
->duids_requesting_uuid
, duid
);
3208 static int link_initialized_and_synced(Link
*link
) {
3213 assert(link
->ifname
);
3214 assert(link
->manager
);
3216 /* We may get called either from the asynchronous netlink callback,
3217 * or directly for link_add() if running in a container. See link_add(). */
3218 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3221 log_link_debug(link
, "Link state is up-to-date");
3222 link_set_state(link
, LINK_STATE_INITIALIZED
);
3224 r
= link_new_bound_by_list(link
);
3228 r
= link_handle_bound_by_list(link
);
3232 if (!link
->network
) {
3233 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3234 &link
->mac
, &network
);
3236 link_enter_unmanaged(link
);
3238 } else if (r
== 0 && network
->unmanaged
) {
3239 link_enter_unmanaged(link
);
3244 if (link
->flags
& IFF_LOOPBACK
) {
3245 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3246 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3248 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3249 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3251 if (network
->dhcp_server
)
3252 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3255 r
= network_apply(network
, link
);
3260 r
= link_new_bound_to_list(link
);
3264 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3265 * link_configure() is called later asynchronously. */
3266 r
= link_configure_duid(link
);
3270 r
= link_configure(link
);
3277 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3278 (void) link_initialized_and_synced(link
);
3282 int link_initialized(Link
*link
, sd_device
*device
) {
3283 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3287 assert(link
->manager
);
3288 assert(link
->manager
->rtnl
);
3291 if (link
->state
!= LINK_STATE_PENDING
)
3294 if (link
->sd_device
)
3297 log_link_debug(link
, "udev initialized link");
3298 link_set_state(link
, LINK_STATE_INITIALIZED
);
3300 link
->sd_device
= sd_device_ref(device
);
3302 /* udev has initialized the link, but we don't know if we have yet
3303 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3304 * when it returns we know that the pending NEWLINKs have already been
3305 * processed and that we are up-to-date */
3307 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3312 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3313 link_netlink_destroy_callback
, link
);
3322 static int link_load(Link
*link
) {
3323 _cleanup_free_
char *network_file
= NULL
,
3326 *dhcp4_address
= NULL
,
3327 *ipv4ll_address
= NULL
;
3328 union in_addr_union address
;
3329 union in_addr_union route_dst
;
3335 r
= parse_env_file(NULL
, link
->state_file
,
3336 "NETWORK_FILE", &network_file
,
3337 "ADDRESSES", &addresses
,
3339 "DHCP4_ADDRESS", &dhcp4_address
,
3340 "IPV4LL_ADDRESS", &ipv4ll_address
);
3341 if (r
< 0 && r
!= -ENOENT
)
3342 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3349 suffix
= strrchr(network_file
, '.');
3351 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3352 goto network_file_fail
;
3356 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3358 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3359 goto network_file_fail
;
3362 r
= network_apply(network
, link
);
3364 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3373 _cleanup_free_
char *address_str
= NULL
;
3374 char *prefixlen_str
;
3376 unsigned char prefixlen
;
3378 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3380 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3386 prefixlen_str
= strchr(address_str
, '/');
3387 if (!prefixlen_str
) {
3388 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3392 *prefixlen_str
++ = '\0';
3394 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3396 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3400 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3402 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3406 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3408 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3417 _cleanup_free_
char *route_str
= NULL
;
3418 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3420 char *prefixlen_str
;
3422 unsigned char prefixlen
, tos
, table
;
3425 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3427 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3433 prefixlen_str
= strchr(route_str
, '/');
3434 if (!prefixlen_str
) {
3435 log_link_debug(link
, "Failed to parse route %s", route_str
);
3439 *prefixlen_str
++ = '\0';
3441 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3443 log_link_debug(link
,
3444 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3449 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3451 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3455 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3457 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3459 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3460 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3461 0, route_expire_handler
, route
);
3463 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3466 route
->lifetime
= lifetime
;
3467 sd_event_source_unref(route
->expire
);
3468 route
->expire
= TAKE_PTR(expire
);
3472 if (dhcp4_address
) {
3473 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3475 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3476 goto dhcp4_address_fail
;
3479 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3481 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3483 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3485 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3490 if (ipv4ll_address
) {
3491 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3493 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3494 goto ipv4ll_address_fail
;
3497 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3499 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3501 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3503 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3506 ipv4ll_address_fail
:
3511 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3512 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3513 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3522 r
= link_new(m
, message
, ret
);
3528 log_link_debug(link
, "Link %d added", link
->ifindex
);
3530 r
= link_load(link
);
3534 if (detect_container() <= 0) {
3535 /* not in a container, udev will be around */
3536 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3537 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3539 log_link_warning_errno(link
, r
, "Could not find device: %m");
3543 r
= sd_device_get_is_initialized(device
);
3545 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3550 log_link_debug(link
, "link pending udev initialization...");
3554 r
= device_is_renaming(device
);
3556 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3560 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3564 r
= link_initialized(link
, device
);
3568 r
= link_initialized_and_synced(link
);
3575 link_enter_failed(link
);
3579 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3584 log_link_info(link
, "Gained IPv6LL");
3586 link
->ipv6ll_address
= *address
;
3587 link_check_ready(link
);
3589 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3590 r
= link_acquire_ipv6_conf(link
);
3592 link_enter_failed(link
);
3600 static int link_carrier_gained(Link
*link
) {
3605 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3606 r
= link_acquire_conf(link
);
3608 link_enter_failed(link
);
3612 r
= link_request_set_addresses(link
);
3617 r
= link_handle_bound_by_list(link
);
3624 static int link_carrier_lost(Link
*link
) {
3629 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3632 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3633 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3634 * configuration and stop the clients as well. */
3635 if (link
->setting_mtu
)
3638 r
= link_stop_clients(link
);
3640 link_enter_failed(link
);
3644 if (link_dhcp4_server_enabled(link
))
3645 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3647 r
= link_drop_config(link
);
3651 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3652 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3653 r
= link_drop_foreign_config(link
);
3658 r
= link_handle_bound_by_list(link
);
3665 int link_carrier_reset(Link
*link
) {
3670 if (link_has_carrier(link
)) {
3671 r
= link_carrier_lost(link
);
3675 r
= link_carrier_gained(link
);
3679 log_link_info(link
, "Reset carrier");
3685 int link_update(Link
*link
, sd_netlink_message
*m
) {
3686 struct ether_addr mac
;
3689 bool had_carrier
, carrier_gained
, carrier_lost
;
3693 assert(link
->ifname
);
3696 if (link
->state
== LINK_STATE_LINGER
) {
3697 log_link_info(link
, "Link re-added");
3698 link_set_state(link
, LINK_STATE_CONFIGURING
);
3700 r
= link_new_carrier_maps(link
);
3705 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3706 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3707 Manager
*manager
= link
->manager
;
3709 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3712 r
= link_add(manager
, m
, &link
);
3717 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3718 if (r
>= 0 && mtu
> 0) {
3720 if (link
->original_mtu
== 0) {
3721 link
->original_mtu
= mtu
;
3722 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3725 if (link
->dhcp_client
) {
3726 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3729 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3733 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3735 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3739 /* The kernel may broadcast NEWLINK messages without the MAC address
3740 set, simply ignore them. */
3741 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3743 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3746 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3749 log_link_debug(link
, "MAC address: "
3750 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3751 mac
.ether_addr_octet
[0],
3752 mac
.ether_addr_octet
[1],
3753 mac
.ether_addr_octet
[2],
3754 mac
.ether_addr_octet
[3],
3755 mac
.ether_addr_octet
[4],
3756 mac
.ether_addr_octet
[5]);
3759 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3761 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3764 if (link
->dhcp_client
) {
3765 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3766 (const uint8_t *) &link
->mac
,
3770 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3772 r
= dhcp4_set_client_identifier(link
);
3777 if (link
->dhcp6_client
) {
3778 const DUID
* duid
= link_get_duid(link
);
3780 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3781 (const uint8_t *) &link
->mac
,
3785 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3787 if (link
->network
->iaid_set
) {
3788 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3789 link
->network
->iaid
);
3791 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3794 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3796 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3797 duid
->raw_data_len
);
3799 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3803 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3805 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3809 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3811 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3816 had_carrier
= link_has_carrier(link
);
3818 r
= link_update_flags(link
, m
);
3822 r
= link_update_lldp(link
);
3826 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3827 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3829 if (carrier_gained
) {
3830 log_link_info(link
, "Gained carrier");
3832 r
= link_carrier_gained(link
);
3835 } else if (carrier_lost
) {
3836 log_link_info(link
, "Lost carrier");
3838 r
= link_carrier_lost(link
);
3846 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3854 if (hashmap_isempty(h
))
3858 HASHMAP_FOREACH(link
, h
, i
) {
3862 fprintf(f
, "%i", link
->ifindex
);
3869 int link_save(Link
*link
) {
3870 _cleanup_free_
char *temp_path
= NULL
;
3871 _cleanup_fclose_
FILE *f
= NULL
;
3872 const char *admin_state
, *oper_state
;
3879 assert(link
->state_file
);
3880 assert(link
->lease_file
);
3881 assert(link
->manager
);
3883 if (link
->state
== LINK_STATE_LINGER
) {
3884 (void) unlink(link
->state_file
);
3888 link_lldp_save(link
);
3890 admin_state
= link_state_to_string(link
->state
);
3891 assert(admin_state
);
3893 oper_state
= link_operstate_to_string(link
->operstate
);
3896 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3900 (void) fchmod(fileno(f
), 0644);
3903 "# This is private data. Do not parse.\n"
3906 admin_state
, oper_state
);
3908 if (link
->network
) {
3910 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3911 const char *dhcp_domainname
= NULL
;
3912 char **dhcp6_domains
= NULL
;
3913 char **dhcp_domains
= NULL
;
3916 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3917 yes_no(link
->network
->required_for_online
));
3919 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3920 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3922 if (link
->dhcp6_client
) {
3923 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3924 if (r
< 0 && r
!= -ENOMSG
)
3925 log_link_debug(link
, "No DHCPv6 lease");
3928 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3933 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3934 _cleanup_free_
char *b
= NULL
;
3936 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3937 &link
->network
->dns
[j
].address
, &b
);
3939 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3949 if (link
->network
->dhcp_use_dns
&&
3951 const struct in_addr
*addresses
;
3953 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3955 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3959 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3960 struct in6_addr
*in6_addrs
;
3962 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3966 serialize_in6_addrs(f
, in6_addrs
, r
);
3971 /* Make sure to flush out old entries before we use the NDISC data */
3974 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3977 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3981 serialize_in6_addrs(f
, &dd
->address
, 1);
3990 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3992 if (link
->network
->dhcp_use_ntp
&&
3994 const struct in_addr
*addresses
;
3996 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3998 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4002 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4003 struct in6_addr
*in6_addrs
;
4006 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4011 serialize_in6_addrs(f
, in6_addrs
, r
);
4015 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4017 fputstrv(f
, hosts
, NULL
, &space
);
4022 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4023 if (link
->dhcp_lease
) {
4024 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4025 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4028 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4031 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4033 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4036 if (dhcp_domainname
)
4037 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4039 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4041 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4043 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4044 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4049 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4051 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4054 if (dhcp_domainname
)
4055 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4057 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4059 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4061 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4062 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4067 fprintf(f
, "LLMNR=%s\n",
4068 resolve_support_to_string(link
->network
->llmnr
));
4069 fprintf(f
, "MDNS=%s\n",
4070 resolve_support_to_string(link
->network
->mdns
));
4071 if (link
->network
->dns_default_route
>= 0)
4072 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4074 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4075 fprintf(f
, "DNS_OVER_TLS=%s\n",
4076 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4078 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4079 fprintf(f
, "DNSSEC=%s\n",
4080 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4082 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4085 fputs("DNSSEC_NTA=", f
);
4087 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4088 fputs_with_space(f
, n
, NULL
, &space
);
4092 fputs("ADDRESSES=", f
);
4094 SET_FOREACH(a
, link
->addresses
, i
) {
4095 _cleanup_free_
char *address_str
= NULL
;
4097 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4101 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4106 fputs("ROUTES=", f
);
4108 SET_FOREACH(route
, link
->routes
, i
) {
4109 _cleanup_free_
char *route_str
= NULL
;
4111 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4115 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4116 space
? " " : "", route_str
,
4117 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4124 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4125 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4127 if (link
->dhcp_lease
) {
4128 struct in_addr address
;
4129 const char *tz
= NULL
;
4131 assert(link
->network
);
4133 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4135 fprintf(f
, "TIMEZONE=%s\n", tz
);
4137 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4139 fputs("DHCP4_ADDRESS=", f
);
4140 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4144 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4152 (void) unlink(link
->lease_file
);
4155 struct in_addr address
;
4157 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4159 fputs("IPV4LL_ADDRESS=", f
);
4160 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4165 r
= fflush_and_check(f
);
4169 if (rename(temp_path
, link
->state_file
) < 0) {
4177 (void) unlink(link
->state_file
);
4179 (void) unlink(temp_path
);
4181 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4184 /* The serialized state in /run is no longer up-to-date. */
4185 void link_dirty(Link
*link
) {
4190 /* mark manager dirty as link is dirty */
4191 manager_dirty(link
->manager
);
4193 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4195 /* allocation errors are ignored */
4198 r
= set_put(link
->manager
->dirty_links
, link
);
4200 /* don't take another ref if the link was already dirty */
4206 /* The serialized state in /run is up-to-date */
4207 void link_clean(Link
*link
) {
4209 assert(link
->manager
);
4211 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4214 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4215 [LINK_STATE_PENDING
] = "pending",
4216 [LINK_STATE_INITIALIZED
] = "initialized",
4217 [LINK_STATE_CONFIGURING
] = "configuring",
4218 [LINK_STATE_CONFIGURED
] = "configured",
4219 [LINK_STATE_UNMANAGED
] = "unmanaged",
4220 [LINK_STATE_FAILED
] = "failed",
4221 [LINK_STATE_LINGER
] = "linger",
4224 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);