1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
16 #include "missing_network.h"
17 #include "netdev/vrf.h"
18 #include "netlink-util.h"
19 #include "network-internal.h"
20 #include "networkd-ipv6-proxy-ndp.h"
21 #include "networkd-lldp-tx.h"
22 #include "networkd-manager.h"
23 #include "networkd-ndisc.h"
24 #include "networkd-neighbor.h"
25 #include "networkd-radv.h"
26 #include "networkd-routing-policy-rule.h"
28 #include "socket-util.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
32 #include "sysctl-util.h"
33 #include "tmpfile-util.h"
34 #include "udev-util.h"
38 uint32_t link_get_vrf_table(Link
*link
) {
39 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
42 uint32_t link_get_dhcp_route_table(Link
*link
) {
43 /* When the interface is part of an VRF use the VRFs routing table, unless
44 * another table is explicitly specified. */
45 if (link
->network
->dhcp_route_table_set
)
46 return link
->network
->dhcp_route_table
;
47 return link_get_vrf_table(link
);
50 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
51 if (link
->network
->ipv6_accept_ra_route_table_set
)
52 return link
->network
->ipv6_accept_ra_route_table
;
53 return link_get_vrf_table(link
);
56 DUID
* link_get_duid(Link
*link
) {
57 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
58 return &link
->network
->duid
;
60 return &link
->manager
->duid
;
63 static bool link_dhcp6_enabled(Link
*link
) {
66 if (!socket_ipv6_is_supported())
69 if (link
->flags
& IFF_LOOPBACK
)
75 if (link
->network
->bond
)
78 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
81 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
84 static bool link_dhcp4_enabled(Link
*link
) {
87 if (link
->flags
& IFF_LOOPBACK
)
93 if (link
->network
->bond
)
96 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
99 static bool link_dhcp4_server_enabled(Link
*link
) {
102 if (link
->flags
& IFF_LOOPBACK
)
108 if (link
->network
->bond
)
111 return link
->network
->dhcp_server
;
114 static bool link_ipv4ll_enabled(Link
*link
) {
117 if (link
->flags
& IFF_LOOPBACK
)
123 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
126 if (link
->network
->bond
)
129 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
132 static bool link_ipv6ll_enabled(Link
*link
) {
135 if (!socket_ipv6_is_supported())
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
147 if (link
->network
->bond
)
150 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
153 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
156 static bool link_ipv6_enabled(Link
*link
) {
159 if (!socket_ipv6_is_supported())
162 if (link
->network
->bond
)
165 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
168 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
169 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
172 static bool link_radv_enabled(Link
*link
) {
175 if (!link_ipv6ll_enabled(link
))
178 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
181 static bool link_lldp_rx_enabled(Link
*link
) {
184 if (link
->flags
& IFF_LOOPBACK
)
187 if (link
->iftype
!= ARPHRD_ETHER
)
193 /* LLDP should be handled on bridge slaves as those have a direct
194 * connection to their peers not on the bridge master. Linux doesn't
195 * even (by default) forward lldp packets to the bridge master.*/
196 if (streq_ptr("bridge", link
->kind
))
199 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
202 static bool link_lldp_emit_enabled(Link
*link
) {
205 if (link
->flags
& IFF_LOOPBACK
)
208 if (link
->iftype
!= ARPHRD_ETHER
)
214 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
217 static bool link_ipv4_forward_enabled(Link
*link
) {
220 if (link
->flags
& IFF_LOOPBACK
)
226 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
229 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
232 static bool link_ipv6_forward_enabled(Link
*link
) {
235 if (!socket_ipv6_is_supported())
238 if (link
->flags
& IFF_LOOPBACK
)
244 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
247 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
250 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
253 static bool link_proxy_arp_enabled(Link
*link
) {
256 if (link
->flags
& IFF_LOOPBACK
)
262 if (link
->network
->proxy_arp
< 0)
268 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
271 if (!socket_ipv6_is_supported())
274 if (link
->flags
& IFF_LOOPBACK
)
280 if (!link_ipv6ll_enabled(link
))
283 /* If unset use system default (enabled if local forwarding is disabled.
284 * disabled if local forwarding is enabled).
285 * If set, ignore or enforce RA independent of local forwarding state.
287 if (link
->network
->ipv6_accept_ra
< 0)
288 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
289 return !link_ipv6_forward_enabled(link
);
290 else if (link
->network
->ipv6_accept_ra
> 0)
291 /* accept RA even if ip_forward is enabled */
298 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
301 if (!socket_ipv6_is_supported())
302 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
304 if (link
->flags
& IFF_LOOPBACK
)
305 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
308 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
310 return link
->network
->ipv6_privacy_extensions
;
313 static int link_enable_ipv6(Link
*link
) {
317 if (link
->flags
& IFF_LOOPBACK
)
320 disabled
= !link_ipv6_enabled(link
);
322 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
324 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
325 enable_disable(!disabled
), link
->ifname
);
327 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
332 static bool link_is_enslaved(Link
*link
) {
333 if (link
->flags
& IFF_SLAVE
)
334 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
337 if (!link
->enslaved_raw
)
343 if (link
->network
->bridge
)
344 /* TODO: support the case when link is not managed by networkd. */
350 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
356 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
359 link_update_operstate(master
, true);
362 void link_update_operstate(Link
*link
, bool also_update_master
) {
363 LinkOperationalState operstate
;
368 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
369 operstate
= LINK_OPERSTATE_DORMANT
;
370 else if (link_has_carrier(link
)) {
372 uint8_t scope
= RT_SCOPE_NOWHERE
;
374 /* if we have carrier, check what addresses we have */
375 SET_FOREACH(address
, link
->addresses
, i
) {
376 if (!address_is_ready(address
))
379 if (address
->scope
< scope
)
380 scope
= address
->scope
;
383 /* for operstate we also take foreign addresses into account */
384 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
385 if (!address_is_ready(address
))
388 if (address
->scope
< scope
)
389 scope
= address
->scope
;
392 if (scope
< RT_SCOPE_SITE
)
393 /* universally accessible addresses found */
394 operstate
= LINK_OPERSTATE_ROUTABLE
;
395 else if (scope
< RT_SCOPE_HOST
)
396 /* only link or site local addresses found */
397 operstate
= LINK_OPERSTATE_DEGRADED
;
399 /* no useful addresses found */
400 operstate
= LINK_OPERSTATE_CARRIER
;
401 } else if (link
->flags
& IFF_UP
)
402 operstate
= LINK_OPERSTATE_NO_CARRIER
;
404 operstate
= LINK_OPERSTATE_OFF
;
406 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
407 link_is_enslaved(link
))
408 operstate
= LINK_OPERSTATE_ENSLAVED
;
410 if (IN_SET(operstate
, LINK_OPERSTATE_CARRIER
, LINK_OPERSTATE_ENSLAVED
, LINK_OPERSTATE_ROUTABLE
)) {
413 HASHMAP_FOREACH(slave
, link
->slaves
, i
) {
414 link_update_operstate(slave
, false);
416 if (IN_SET(slave
->operstate
,
417 LINK_OPERSTATE_OFF
, LINK_OPERSTATE_NO_CARRIER
, LINK_OPERSTATE_DORMANT
))
418 operstate
= LINK_OPERSTATE_DEGRADED
;
422 if (link
->operstate
!= operstate
) {
423 link
->operstate
= operstate
;
424 link_send_changed(link
, "OperationalState", NULL
);
428 if (also_update_master
&& link
->network
) {
429 link_update_master_operstate(link
, link
->network
->bond
);
430 link_update_master_operstate(link
, link
->network
->bridge
);
434 #define FLAG_STRING(string, flag, old, new) \
435 (((old ^ new) & flag) \
436 ? ((old & flag) ? (" -" string) : (" +" string)) \
439 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
440 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
446 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
448 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
450 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
452 /* if we got a message without operstate, take it to mean
453 the state was unchanged */
454 operstate
= link
->kernel_operstate
;
456 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
459 if (link
->flags
!= flags
) {
460 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
461 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
462 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
463 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
464 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
465 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
466 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
467 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
468 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
469 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
470 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
471 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
472 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
473 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
474 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
475 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
476 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
477 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
478 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
479 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
481 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
482 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
483 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
484 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
485 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
486 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
487 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
488 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
490 /* link flags are currently at most 18 bits, let's align to
492 if (unknown_flags_added
)
494 "Unknown link flags gained: %#.5x (ignoring)",
495 unknown_flags_added
);
497 if (unknown_flags_removed
)
499 "Unknown link flags lost: %#.5x (ignoring)",
500 unknown_flags_removed
);
504 link
->kernel_operstate
= operstate
;
506 link_update_operstate(link
, true);
511 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
513 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
514 _cleanup_(link_unrefp
) Link
*link
= NULL
;
516 const char *ifname
, *kind
= NULL
;
518 unsigned short iftype
;
524 /* check for link kind */
525 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
527 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
528 r
= sd_netlink_message_exit_container(message
);
533 r
= sd_netlink_message_get_type(message
, &type
);
536 else if (type
!= RTM_NEWLINK
)
539 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
542 else if (ifindex
<= 0)
545 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
549 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
560 .state
= LINK_STATE_PENDING
,
561 .rtnl_extended_attrs
= true,
566 link
->ifname
= strdup(ifname
);
571 link
->kind
= strdup(kind
);
576 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
578 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
580 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
582 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
584 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
587 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
590 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
593 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
597 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
601 r
= link_update_flags(link
, message
);
605 *ret
= TAKE_PTR(link
);
610 static void link_detach_from_manager(Link
*link
) {
611 if (!link
|| !link
->manager
)
614 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
615 set_remove(link
->manager
->links_requesting_uuid
, link
);
619 static Link
*link_free(Link
*link
) {
620 Link
*carrier
, *master
;
627 while ((route
= set_first(link
->routes
)))
630 while ((route
= set_first(link
->routes_foreign
)))
633 link
->routes
= set_free(link
->routes
);
634 link
->routes_foreign
= set_free(link
->routes_foreign
);
636 while ((address
= set_first(link
->addresses
)))
637 address_free(address
);
639 while ((address
= set_first(link
->addresses_foreign
)))
640 address_free(address
);
642 link
->addresses
= set_free(link
->addresses
);
643 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
645 while ((address
= link
->pool_addresses
)) {
646 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
647 address_free(address
);
650 sd_dhcp_server_unref(link
->dhcp_server
);
651 sd_dhcp_client_unref(link
->dhcp_client
);
652 sd_dhcp_lease_unref(link
->dhcp_lease
);
654 link_lldp_emit_stop(link
);
656 free(link
->lease_file
);
658 sd_lldp_unref(link
->lldp
);
659 free(link
->lldp_file
);
663 sd_ipv4ll_unref(link
->ipv4ll
);
664 sd_dhcp6_client_unref(link
->dhcp6_client
);
665 sd_ndisc_unref(link
->ndisc
);
666 sd_radv_unref(link
->radv
);
668 link_detach_from_manager(link
);
674 (void) unlink(link
->state_file
);
675 free(link
->state_file
);
677 sd_device_unref(link
->sd_device
);
679 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
680 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
681 hashmap_free(link
->bound_to_links
);
683 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
684 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
685 hashmap_free(link
->bound_by_links
);
687 hashmap_free(link
->slaves
);
690 if (link
->network
->bond
&&
691 link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
) >= 0)
692 (void) hashmap_remove(master
->slaves
, INT_TO_PTR(link
->ifindex
));
694 if (link
->network
->bridge
&&
695 link_get(link
->manager
, link
->network
->bridge
->ifindex
, &master
) >= 0)
696 (void) hashmap_remove(master
->slaves
, INT_TO_PTR(link
->ifindex
));
702 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
704 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
711 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
720 static void link_set_state(Link
*link
, LinkState state
) {
723 if (link
->state
== state
)
728 link_send_changed(link
, "AdministrativeState", NULL
);
731 static void link_enter_unmanaged(Link
*link
) {
734 log_link_debug(link
, "Unmanaged");
736 link_set_state(link
, LINK_STATE_UNMANAGED
);
741 static int link_stop_clients(Link
*link
) {
745 assert(link
->manager
);
746 assert(link
->manager
->event
);
748 if (link
->dhcp_client
) {
749 k
= sd_dhcp_client_stop(link
->dhcp_client
);
751 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
755 k
= sd_ipv4ll_stop(link
->ipv4ll
);
757 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
760 if (link
->dhcp6_client
) {
761 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
763 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
767 k
= sd_ndisc_stop(link
->ndisc
);
769 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
773 k
= sd_radv_stop(link
->radv
);
775 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
778 link_lldp_emit_stop(link
);
782 void link_enter_failed(Link
*link
) {
785 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
788 log_link_warning(link
, "Failed");
790 link_set_state(link
, LINK_STATE_FAILED
);
792 link_stop_clients(link
);
797 static Address
* link_find_dhcp_server_address(Link
*link
) {
801 assert(link
->network
);
803 /* The first statically configured address if there is any */
804 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
806 if (address
->family
!= AF_INET
)
809 if (in_addr_is_null(address
->family
, &address
->in_addr
))
815 /* If that didn't work, find a suitable address we got from the pool */
816 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
817 if (address
->family
!= AF_INET
)
826 static void link_enter_configured(Link
*link
) {
828 assert(link
->network
);
830 if (link
->state
!= LINK_STATE_CONFIGURING
)
833 log_link_info(link
, "Configured");
835 link_set_state(link
, LINK_STATE_CONFIGURED
);
840 static int link_request_set_routing_policy_rule(Link
*link
) {
841 RoutingPolicyRule
*rule
, *rrule
= NULL
;
845 assert(link
->network
);
847 link_set_state(link
, LINK_STATE_CONFIGURING
);
848 link
->routing_policy_rules_configured
= false;
850 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
851 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
852 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
853 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
855 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
859 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
861 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
862 link_enter_failed(link
);
866 link
->routing_policy_rule_messages
++;
869 routing_policy_rule_purge(link
->manager
, link
);
870 if (link
->routing_policy_rule_messages
== 0) {
871 link
->routing_policy_rules_configured
= true;
872 link_check_ready(link
);
874 log_link_debug(link
, "Setting routing policy rules");
879 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
883 assert(link
->route_messages
> 0);
884 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
885 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
887 link
->route_messages
--;
889 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
892 r
= sd_netlink_message_get_errno(m
);
893 if (r
< 0 && r
!= -EEXIST
)
894 log_link_warning_errno(link
, r
, "Could not set route: %m");
896 if (link
->route_messages
== 0) {
897 log_link_debug(link
, "Routes set");
898 link
->static_routes_configured
= true;
899 link_check_ready(link
);
905 static int link_request_set_routes(Link
*link
) {
907 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
908 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
915 assert(link
->network
);
916 assert(link
->addresses_configured
);
917 assert(link
->address_messages
== 0);
918 assert(link
->state
!= _LINK_STATE_INVALID
);
920 link_set_state(link
, LINK_STATE_CONFIGURING
);
921 link
->static_routes_configured
= false;
923 r
= link_request_set_routing_policy_rule(link
);
927 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
928 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
929 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
931 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
934 r
= route_configure(rt
, link
, route_handler
);
936 log_link_warning_errno(link
, r
, "Could not set routes: %m");
937 link_enter_failed(link
);
941 link
->route_messages
++;
944 if (link
->route_messages
== 0) {
945 link
->static_routes_configured
= true;
946 link_check_ready(link
);
948 log_link_debug(link
, "Setting routes");
953 void link_check_ready(Link
*link
) {
959 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
965 if (!link
->addresses_configured
)
968 if (!link
->neighbors_configured
)
971 SET_FOREACH(a
, link
->addresses
, i
)
972 if (!address_is_ready(a
))
975 if (!link
->addresses_ready
) {
976 link
->addresses_ready
= true;
977 link_request_set_routes(link
);
980 if (!link
->static_routes_configured
)
983 if (!link
->routing_policy_rules_configured
)
986 if (link_ipv4ll_enabled(link
))
987 if (!link
->ipv4ll_address
||
991 if (link_ipv6ll_enabled(link
) &&
992 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
995 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
996 !link
->dhcp4_configured
) ||
997 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
998 !link
->dhcp6_configured
) ||
999 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
1000 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
1003 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1006 if (link
->state
!= LINK_STATE_CONFIGURED
)
1007 link_enter_configured(link
);
1012 static int link_request_set_neighbors(Link
*link
) {
1017 assert(link
->network
);
1018 assert(link
->state
!= _LINK_STATE_INVALID
);
1020 link_set_state(link
, LINK_STATE_CONFIGURING
);
1021 link
->neighbors_configured
= false;
1023 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1024 r
= neighbor_configure(neighbor
, link
, NULL
);
1026 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1027 link_enter_failed(link
);
1032 if (link
->neighbor_messages
== 0) {
1033 link
->neighbors_configured
= true;
1034 link_check_ready(link
);
1036 log_link_debug(link
, "Setting neighbors");
1041 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1047 assert(link
->ifname
);
1048 assert(link
->address_messages
> 0);
1049 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1050 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1052 link
->address_messages
--;
1054 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1057 r
= sd_netlink_message_get_errno(m
);
1058 if (r
< 0 && r
!= -EEXIST
)
1059 log_link_warning_errno(link
, r
, "could not set address: %m");
1061 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1063 if (link
->address_messages
== 0) {
1064 log_link_debug(link
, "Addresses set");
1065 link
->addresses_configured
= true;
1066 link_check_ready(link
);
1072 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1073 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1074 size_t n_addresses
= 0, n_allocated
= 0;
1077 log_debug("Copying DNS server information from %s", link
->ifname
);
1082 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1085 /* Only look for IPv4 addresses */
1086 if (link
->network
->dns
[i
].family
!= AF_INET
)
1089 ia
= link
->network
->dns
[i
].address
.in
;
1091 /* Never propagate obviously borked data */
1092 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1095 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1098 addresses
[n_addresses
++] = ia
;
1101 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1102 const struct in_addr
*da
= NULL
;
1105 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1108 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1111 for (j
= 0; j
< n
; j
++)
1112 if (in4_addr_is_non_local(&da
[j
]))
1113 addresses
[n_addresses
++] = da
[j
];
1117 if (n_addresses
<= 0)
1120 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1123 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1124 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1125 size_t n_addresses
= 0, n_allocated
= 0;
1131 log_debug("Copying NTP server information from %s", link
->ifname
);
1133 STRV_FOREACH(a
, link
->network
->ntp
) {
1136 /* Only look for IPv4 addresses */
1137 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1140 /* Never propagate obviously borked data */
1141 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1144 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1147 addresses
[n_addresses
++] = ia
;
1150 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1151 const struct in_addr
*da
= NULL
;
1154 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1157 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1160 for (j
= 0; j
< n
; j
++)
1161 if (in4_addr_is_non_local(&da
[j
]))
1162 addresses
[n_addresses
++] = da
[j
];
1166 if (n_addresses
<= 0)
1169 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1172 static int link_set_bridge_fdb(Link
*link
) {
1173 FdbEntry
*fdb_entry
;
1176 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1177 r
= fdb_entry_configure(link
, fdb_entry
);
1179 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1185 static int link_request_set_addresses(Link
*link
) {
1186 AddressLabel
*label
;
1191 assert(link
->network
);
1192 assert(link
->state
!= _LINK_STATE_INVALID
);
1194 link_set_state(link
, LINK_STATE_CONFIGURING
);
1196 /* Reset all *_configured flags we are configuring. */
1197 link
->addresses_configured
= false;
1198 link
->addresses_ready
= false;
1199 link
->neighbors_configured
= false;
1200 link
->static_routes_configured
= false;
1201 link
->routing_policy_rules_configured
= false;
1203 r
= link_set_bridge_fdb(link
);
1207 r
= link_request_set_neighbors(link
);
1211 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1214 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1216 r
= address_configure(ad
, link
, address_handler
, update
);
1218 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1219 link_enter_failed(link
);
1223 link
->address_messages
++;
1226 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1227 r
= address_label_configure(label
, link
, NULL
, false);
1229 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1230 link_enter_failed(link
);
1234 link
->address_label_messages
++;
1237 /* now that we can figure out a default address for the dhcp server,
1239 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1241 Link
*uplink
= NULL
;
1242 bool acquired_uplink
= false;
1244 address
= link_find_dhcp_server_address(link
);
1246 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1247 link_enter_failed(link
);
1251 /* use the server address' subnet as the pool */
1252 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1253 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1258 r = sd_dhcp_server_set_router(link->dhcp_server,
1259 &main_address->in_addr.in);
1264 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1265 r
= sd_dhcp_server_set_max_lease_time(
1267 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1272 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1273 r
= sd_dhcp_server_set_default_lease_time(
1275 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1280 if (link
->network
->dhcp_server_emit_dns
) {
1282 if (link
->network
->n_dhcp_server_dns
> 0)
1283 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1285 uplink
= manager_find_uplink(link
->manager
, link
);
1286 acquired_uplink
= true;
1289 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1292 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1295 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1298 if (link
->network
->dhcp_server_emit_ntp
) {
1300 if (link
->network
->n_dhcp_server_ntp
> 0)
1301 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1303 if (!acquired_uplink
)
1304 uplink
= manager_find_uplink(link
->manager
, link
);
1307 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1310 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1314 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1317 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1319 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1321 if (link
->network
->dhcp_server_emit_timezone
) {
1322 _cleanup_free_
char *buffer
= NULL
;
1323 const char *tz
= NULL
;
1325 if (link
->network
->dhcp_server_timezone
)
1326 tz
= link
->network
->dhcp_server_timezone
;
1328 r
= get_timezone(&buffer
);
1330 log_warning_errno(r
, "Failed to determine timezone: %m");
1336 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1341 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1342 r
= sd_dhcp_server_start(link
->dhcp_server
);
1344 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1346 link_enter_failed(link
);
1352 log_link_debug(link
, "Offering DHCPv4 leases");
1355 if (link
->address_messages
== 0) {
1356 link
->addresses_configured
= true;
1357 link_check_ready(link
);
1359 log_link_debug(link
, "Setting addresses");
1364 static int link_set_bridge_vlan(Link
*link
) {
1367 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1369 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1374 static int link_set_proxy_arp(Link
*link
) {
1377 if (!link_proxy_arp_enabled(link
))
1380 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1382 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1387 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1392 log_link_debug(link
, "Set link");
1394 r
= sd_netlink_message_get_errno(m
);
1395 if (r
< 0 && r
!= -EEXIST
) {
1396 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1397 link_enter_failed(link
);
1403 static int link_configure_after_setting_mtu(Link
*link
);
1405 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1410 assert(link
->ifname
);
1412 link
->setting_mtu
= false;
1414 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1417 r
= sd_netlink_message_get_errno(m
);
1419 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1423 log_link_debug(link
, "Setting MTU done.");
1425 if (link
->state
== LINK_STATE_PENDING
)
1426 (void) link_configure_after_setting_mtu(link
);
1431 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1432 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1436 assert(link
->manager
);
1437 assert(link
->manager
->rtnl
);
1439 if (mtu
== 0 || link
->setting_mtu
)
1442 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1445 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1447 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1449 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1451 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1452 * for this interface, then disable IPv6 else enable it. */
1453 (void) link_enable_ipv6(link
);
1455 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1456 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1457 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1459 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1460 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1465 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1467 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1469 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1470 link_netlink_destroy_callback
, link
);
1472 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1475 link
->setting_mtu
= true;
1480 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1485 assert(link
->ifname
);
1487 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1490 r
= sd_netlink_message_get_errno(m
);
1492 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1497 static int link_set_flags(Link
*link
) {
1498 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1499 unsigned ifi_change
= 0;
1500 unsigned ifi_flags
= 0;
1504 assert(link
->manager
);
1505 assert(link
->manager
->rtnl
);
1507 if (link
->flags
& IFF_LOOPBACK
)
1513 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1516 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1518 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1520 if (link
->network
->arp
>= 0) {
1521 ifi_change
|= IFF_NOARP
;
1522 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1525 if (link
->network
->multicast
>= 0) {
1526 ifi_change
|= IFF_MULTICAST
;
1527 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1530 if (link
->network
->allmulticast
>= 0) {
1531 ifi_change
|= IFF_ALLMULTI
;
1532 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1535 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1537 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1539 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1540 link_netlink_destroy_callback
, link
);
1542 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1549 static int link_set_bridge(Link
*link
) {
1550 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1554 assert(link
->network
);
1556 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1558 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1560 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1562 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1564 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1566 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1568 if (link
->network
->use_bpdu
>= 0) {
1569 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1571 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1574 if (link
->network
->hairpin
>= 0) {
1575 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1577 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1580 if (link
->network
->fast_leave
>= 0) {
1581 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1583 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1586 if (link
->network
->allow_port_to_be_root
>= 0) {
1587 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1589 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1593 if (link
->network
->unicast_flood
>= 0) {
1594 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1596 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1599 if (link
->network
->multicast_to_unicast
>= 0) {
1600 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1602 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1605 if (link
->network
->cost
!= 0) {
1606 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1608 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1611 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1612 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1614 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1617 r
= sd_netlink_message_close_container(req
);
1619 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1621 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1622 link_netlink_destroy_callback
, link
);
1624 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1631 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1636 assert(link
->ifname
);
1638 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1641 r
= sd_netlink_message_get_errno(m
);
1643 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1650 static int link_set_bond(Link
*link
) {
1651 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1655 assert(link
->network
);
1657 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1659 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1661 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1663 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1665 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1667 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1669 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1671 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1673 if (link
->network
->active_slave
) {
1674 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1676 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1679 if (link
->network
->primary_slave
) {
1680 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1682 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1685 r
= sd_netlink_message_close_container(req
);
1687 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1689 r
= sd_netlink_message_close_container(req
);
1691 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1693 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1694 link_netlink_destroy_callback
, link
);
1696 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1703 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1710 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1714 r
= hashmap_ensure_allocated(&master
->slaves
, NULL
);
1718 r
= hashmap_put(master
->slaves
, INT_TO_PTR(link
->ifindex
), link
);
1725 static int link_lldp_save(Link
*link
) {
1726 _cleanup_free_
char *temp_path
= NULL
;
1727 _cleanup_fclose_
FILE *f
= NULL
;
1728 sd_lldp_neighbor
**l
= NULL
;
1732 assert(link
->lldp_file
);
1735 (void) unlink(link
->lldp_file
);
1739 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1743 (void) unlink(link
->lldp_file
);
1749 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1753 fchmod(fileno(f
), 0644);
1755 for (i
= 0; i
< n
; i
++) {
1760 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1765 (void) fwrite(&u
, 1, sizeof(u
), f
);
1766 (void) fwrite(p
, 1, sz
, f
);
1769 r
= fflush_and_check(f
);
1773 if (rename(temp_path
, link
->lldp_file
) < 0) {
1780 (void) unlink(link
->lldp_file
);
1782 (void) unlink(temp_path
);
1784 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1788 for (i
= 0; i
< n
; i
++)
1789 sd_lldp_neighbor_unref(l
[i
]);
1796 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1797 Link
*link
= userdata
;
1802 (void) link_lldp_save(link
);
1804 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1805 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1807 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1809 r
= link_lldp_emit_start(link
);
1811 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1815 static int link_acquire_ipv6_conf(Link
*link
) {
1820 if (link_ipv6_accept_ra_enabled(link
)) {
1821 assert(link
->ndisc
);
1823 log_link_debug(link
, "Discovering IPv6 routers");
1825 r
= sd_ndisc_start(link
->ndisc
);
1826 if (r
< 0 && r
!= -EBUSY
)
1827 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1830 if (link_radv_enabled(link
)) {
1832 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1834 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1836 r
= sd_radv_start(link
->radv
);
1837 if (r
< 0 && r
!= -EBUSY
)
1838 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1841 (void) dhcp6_request_prefix_delegation(link
);
1846 static int link_acquire_ipv4_conf(Link
*link
) {
1850 assert(link
->network
);
1851 assert(link
->manager
);
1852 assert(link
->manager
->event
);
1854 if (link_ipv4ll_enabled(link
)) {
1855 assert(link
->ipv4ll
);
1857 log_link_debug(link
, "Acquiring IPv4 link-local address");
1859 r
= sd_ipv4ll_start(link
->ipv4ll
);
1861 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1864 if (link_dhcp4_enabled(link
)) {
1865 assert(link
->dhcp_client
);
1867 log_link_debug(link
, "Acquiring DHCPv4 lease");
1869 r
= sd_dhcp_client_start(link
->dhcp_client
);
1871 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1877 static int link_acquire_conf(Link
*link
) {
1882 r
= link_acquire_ipv4_conf(link
);
1886 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1887 r
= link_acquire_ipv6_conf(link
);
1892 if (link_lldp_emit_enabled(link
)) {
1893 r
= link_lldp_emit_start(link
);
1895 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1901 bool link_has_carrier(Link
*link
) {
1902 /* see Documentation/networking/operstates.txt in the kernel sources */
1904 if (link
->kernel_operstate
== IF_OPER_UP
)
1907 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1908 /* operstate may not be implemented, so fall back to flags */
1909 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1915 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1920 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1923 r
= sd_netlink_message_get_errno(m
);
1925 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1930 static int link_configure_addrgen_mode(Link
*link
) {
1931 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1932 uint8_t ipv6ll_mode
;
1936 assert(link
->network
);
1937 assert(link
->manager
);
1938 assert(link
->manager
->rtnl
);
1940 log_link_debug(link
, "Setting address genmode for link");
1942 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1944 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1946 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1948 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1950 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1952 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1954 if (!link_ipv6ll_enabled(link
))
1955 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1956 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1957 /* The file may not exist. And event if it exists, when stable_secret is unset,
1958 * reading the file fails with EIO. */
1959 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1961 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1963 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1965 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1967 r
= sd_netlink_message_close_container(req
);
1969 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1971 r
= sd_netlink_message_close_container(req
);
1973 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1975 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1976 link_netlink_destroy_callback
, link
);
1978 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1985 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1990 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1993 r
= sd_netlink_message_get_errno(m
);
1995 /* we warn but don't fail the link, as it may be brought up later */
1996 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
2001 static int link_up(Link
*link
) {
2002 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2006 assert(link
->network
);
2007 assert(link
->manager
);
2008 assert(link
->manager
->rtnl
);
2010 log_link_debug(link
, "Bringing link up");
2012 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2014 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2016 /* set it free if not enslaved with networkd */
2017 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
2018 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
2020 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
2023 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2025 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2027 if (link
->network
->mac
) {
2028 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
2030 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2033 if (link_ipv6_enabled(link
)) {
2034 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2036 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2038 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2039 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2041 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2043 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2044 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2046 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2049 r
= sd_netlink_message_close_container(req
);
2051 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2053 r
= sd_netlink_message_close_container(req
);
2055 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2058 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2059 link_netlink_destroy_callback
, link
);
2061 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2068 static int link_up_can(Link
*link
) {
2069 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2074 log_link_debug(link
, "Bringing CAN link up");
2076 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2078 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2080 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2082 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2084 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2085 link_netlink_destroy_callback
, link
);
2087 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2094 static int link_set_can(Link
*link
) {
2095 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2099 assert(link
->network
);
2100 assert(link
->manager
);
2101 assert(link
->manager
->rtnl
);
2103 log_link_debug(link
, "link_set_can");
2105 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2107 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2109 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2111 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2113 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2115 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2117 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2119 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2121 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2122 struct can_bittiming bt
= {
2123 .bitrate
= link
->network
->can_bitrate
,
2124 .sample_point
= link
->network
->can_sample_point
,
2127 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2128 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2132 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2133 if (link
->network
->can_sample_point
> 0)
2134 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2136 log_link_debug(link
, "Using default sample point");
2138 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2140 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2143 if (link
->network
->can_restart_us
> 0) {
2144 char time_string
[FORMAT_TIMESPAN_MAX
];
2145 uint64_t restart_ms
;
2147 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2150 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2152 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2154 if (restart_ms
> UINT32_MAX
) {
2155 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2159 log_link_debug(link
, "Setting restart = %s", time_string
);
2161 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2163 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2166 if (link
->network
->can_triple_sampling
>= 0) {
2167 struct can_ctrlmode cm
= {
2168 .mask
= CAN_CTRLMODE_3_SAMPLES
,
2169 .flags
= link
->network
->can_triple_sampling
? CAN_CTRLMODE_3_SAMPLES
: 0,
2172 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
2174 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
2176 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
2179 r
= sd_netlink_message_close_container(m
);
2181 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2183 r
= sd_netlink_message_close_container(m
);
2185 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2187 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2188 link_netlink_destroy_callback
, link
);
2190 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2194 if (!(link
->flags
& IFF_UP
)) {
2195 r
= link_up_can(link
);
2197 link_enter_failed(link
);
2202 log_link_debug(link
, "link_set_can done");
2207 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2212 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2215 r
= sd_netlink_message_get_errno(m
);
2217 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2219 if (streq_ptr(link
->kind
, "can"))
2225 int link_down(Link
*link
) {
2226 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2230 assert(link
->manager
);
2231 assert(link
->manager
->rtnl
);
2233 log_link_debug(link
, "Bringing link down");
2235 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2236 RTM_SETLINK
, link
->ifindex
);
2238 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2240 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2242 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2244 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2245 link_netlink_destroy_callback
, link
);
2247 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2254 static int link_handle_bound_to_list(Link
*link
) {
2258 bool required_up
= false;
2259 bool link_is_up
= false;
2263 if (hashmap_isempty(link
->bound_to_links
))
2266 if (link
->flags
& IFF_UP
)
2269 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2270 if (link_has_carrier(l
)) {
2275 if (!required_up
&& link_is_up
) {
2276 r
= link_down(link
);
2279 } else if (required_up
&& !link_is_up
) {
2288 static int link_handle_bound_by_list(Link
*link
) {
2295 if (hashmap_isempty(link
->bound_by_links
))
2298 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2299 r
= link_handle_bound_to_list(l
);
2307 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2313 if (link
== carrier
)
2316 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2319 r
= hashmap_ensure_allocated(h
, NULL
);
2323 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2330 static int link_new_bound_by_list(Link
*link
) {
2335 bool list_updated
= false;
2338 assert(link
->manager
);
2342 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2343 if (!carrier
->network
)
2346 if (strv_isempty(carrier
->network
->bind_carrier
))
2349 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2350 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2354 list_updated
= true;
2361 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2362 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2366 link_dirty(carrier
);
2372 static int link_new_bound_to_list(Link
*link
) {
2377 bool list_updated
= false;
2380 assert(link
->manager
);
2385 if (strv_isempty(link
->network
->bind_carrier
))
2390 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2391 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2392 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2396 list_updated
= true;
2403 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2404 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2408 link_dirty(carrier
);
2414 static int link_new_carrier_maps(Link
*link
) {
2417 r
= link_new_bound_by_list(link
);
2421 r
= link_handle_bound_by_list(link
);
2425 r
= link_new_bound_to_list(link
);
2429 r
= link_handle_bound_to_list(link
);
2436 static void link_free_bound_to_list(Link
*link
) {
2440 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2441 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2443 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2444 link_dirty(bound_to
);
2450 static void link_free_bound_by_list(Link
*link
) {
2454 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2455 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2457 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2458 link_dirty(bound_by
);
2459 link_handle_bound_to_list(bound_by
);
2466 static void link_free_carrier_maps(Link
*link
) {
2467 bool list_updated
= false;
2471 if (!hashmap_isempty(link
->bound_to_links
)) {
2472 link_free_bound_to_list(link
);
2473 list_updated
= true;
2476 if (!hashmap_isempty(link
->bound_by_links
)) {
2477 link_free_bound_by_list(link
);
2478 list_updated
= true;
2487 void link_drop(Link
*link
) {
2488 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2491 link_set_state(link
, LINK_STATE_LINGER
);
2493 link_free_carrier_maps(link
);
2495 log_link_debug(link
, "Link removed");
2497 (void) unlink(link
->state_file
);
2499 link_detach_from_manager(link
);
2506 static int link_joined(Link
*link
) {
2510 assert(link
->network
);
2512 if (!hashmap_isempty(link
->bound_to_links
)) {
2513 r
= link_handle_bound_to_list(link
);
2516 } else if (!(link
->flags
& IFF_UP
)) {
2519 link_enter_failed(link
);
2524 if (link
->network
->bridge
) {
2525 r
= link_set_bridge(link
);
2527 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2529 r
= link_append_to_master(link
, link
->network
->bridge
);
2531 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2534 if (link
->network
->bond
) {
2535 r
= link_set_bond(link
);
2537 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2539 r
= link_append_to_master(link
, link
->network
->bond
);
2541 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2544 if (link
->network
->use_br_vlan
&&
2545 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2546 r
= link_set_bridge_vlan(link
);
2548 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2551 /* Skip setting up addresses until it gets carrier,
2552 or it would try to set addresses twice,
2553 which is bad for non-idempotent steps. */
2554 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2557 return link_request_set_addresses(link
);
2560 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2564 assert(link
->network
);
2565 assert(link
->enslaving
> 0);
2566 assert(!link
->enslaved_raw
);
2570 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2573 r
= sd_netlink_message_get_errno(m
);
2574 if (r
< 0 && r
!= -EEXIST
) {
2575 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2576 link_enter_failed(link
);
2579 log_link_debug(link
, "Joined netdev");
2581 if (link
->enslaving
== 0) {
2582 link
->enslaved_raw
= true;
2589 static int link_enter_join_netdev(Link
*link
) {
2595 assert(link
->network
);
2596 assert(link
->state
== LINK_STATE_PENDING
);
2598 link_set_state(link
, LINK_STATE_CONFIGURING
);
2601 link
->enslaving
= 0;
2602 link
->enslaved_raw
= false;
2604 if (link
->network
->bond
) {
2605 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2606 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2607 return link_joined(link
);
2609 log_struct(LOG_DEBUG
,
2610 LOG_LINK_INTERFACE(link
),
2611 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2612 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2616 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2618 log_struct_errno(LOG_WARNING
, r
,
2619 LOG_LINK_INTERFACE(link
),
2620 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2621 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2622 link_enter_failed(link
);
2627 if (link
->network
->bridge
) {
2628 log_struct(LOG_DEBUG
,
2629 LOG_LINK_INTERFACE(link
),
2630 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2631 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2635 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2637 log_struct_errno(LOG_WARNING
, r
,
2638 LOG_LINK_INTERFACE(link
),
2639 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2640 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2641 link_enter_failed(link
);
2646 if (link
->network
->vrf
) {
2647 log_struct(LOG_DEBUG
,
2648 LOG_LINK_INTERFACE(link
),
2649 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2650 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2654 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2656 log_struct_errno(LOG_WARNING
, r
,
2657 LOG_LINK_INTERFACE(link
),
2658 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2659 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2660 link_enter_failed(link
);
2665 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2667 if (netdev
->ifindex
> 0) {
2672 log_struct(LOG_DEBUG
,
2673 LOG_LINK_INTERFACE(link
),
2674 LOG_NETDEV_INTERFACE(netdev
),
2675 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2679 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2681 log_struct_errno(LOG_WARNING
, r
,
2682 LOG_LINK_INTERFACE(link
),
2683 LOG_NETDEV_INTERFACE(netdev
),
2684 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2685 link_enter_failed(link
);
2690 if (link
->enslaving
== 0)
2691 return link_joined(link
);
2696 static int link_set_ipv4_forward(Link
*link
) {
2699 if (!link_ipv4_forward_enabled(link
))
2702 /* We propagate the forwarding flag from one interface to the
2703 * global setting one way. This means: as long as at least one
2704 * interface was configured at any time that had IP forwarding
2705 * enabled the setting will stay on for good. We do this
2706 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2707 * somewhat in sync (see below). */
2709 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2711 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2716 static int link_set_ipv6_forward(Link
*link
) {
2719 if (!link_ipv6_forward_enabled(link
))
2722 /* On Linux, the IPv6 stack does not know a per-interface
2723 * packet forwarding setting: either packet forwarding is on
2724 * for all, or off for all. We hence don't bother with a
2725 * per-interface setting, but simply propagate the interface
2726 * flag, if it is set, to the global flag, one-way. Note that
2727 * while IPv4 would allow a per-interface flag, we expose the
2728 * same behaviour there and also propagate the setting from
2729 * one to all, to keep things simple (see above). */
2731 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2733 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2738 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2739 IPv6PrivacyExtensions s
;
2742 s
= link_ipv6_privacy_extensions(link
);
2746 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2748 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2753 static int link_set_ipv6_accept_ra(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 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2768 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2773 static int link_set_ipv6_dad_transmits(Link
*link
) {
2776 /* Make this a NOP if IPv6 is not available */
2777 if (!socket_ipv6_is_supported())
2780 if (link
->flags
& IFF_LOOPBACK
)
2786 if (link
->network
->ipv6_dad_transmits
< 0)
2789 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2791 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2796 static int link_set_ipv6_hop_limit(Link
*link
) {
2799 /* Make this a NOP if IPv6 is not available */
2800 if (!socket_ipv6_is_supported())
2803 if (link
->flags
& IFF_LOOPBACK
)
2809 if (link
->network
->ipv6_hop_limit
< 0)
2812 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2814 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2819 static int link_set_ipv6_mtu(Link
*link
) {
2822 /* Make this a NOP if IPv6 is not available */
2823 if (!socket_ipv6_is_supported())
2826 if (link
->flags
& IFF_LOOPBACK
)
2829 if (link
->network
->ipv6_mtu
== 0)
2832 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2834 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2839 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2840 Address
*net_address
;
2848 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2849 if (address_equal(net_address
, address
))
2855 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2864 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2865 if (route_equal(net_route
, route
))
2871 static int link_drop_foreign_config(Link
*link
) {
2877 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2878 /* we consider IPv6LL addresses to be managed by the kernel */
2879 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2882 if (link_is_static_address_configured(link
, address
)) {
2883 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2885 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2887 r
= address_remove(address
, link
, NULL
);
2893 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2894 /* do not touch routes managed by the kernel */
2895 if (route
->protocol
== RTPROT_KERNEL
)
2898 if (link_is_static_route_configured(link
, route
)) {
2899 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2903 r
= route_remove(route
, link
, NULL
);
2912 static int link_drop_config(Link
*link
) {
2913 Address
*address
, *pool_address
;
2918 SET_FOREACH(address
, link
->addresses
, i
) {
2919 /* we consider IPv6LL addresses to be managed by the kernel */
2920 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2923 r
= address_remove(address
, link
, NULL
);
2927 /* If this address came from an address pool, clean up the pool */
2928 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2929 if (address_equal(address
, pool_address
)) {
2930 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2931 address_free(pool_address
);
2937 SET_FOREACH(route
, link
->routes
, i
) {
2938 /* do not touch routes managed by the kernel */
2939 if (route
->protocol
== RTPROT_KERNEL
)
2942 r
= route_remove(route
, link
, NULL
);
2952 static int link_update_lldp(Link
*link
) {
2960 if (link
->flags
& IFF_UP
) {
2961 r
= sd_lldp_start(link
->lldp
);
2963 log_link_debug(link
, "Started LLDP.");
2965 r
= sd_lldp_stop(link
->lldp
);
2967 log_link_debug(link
, "Stopped LLDP.");
2973 static int link_configure_can(Link
*link
) {
2976 if (streq_ptr(link
->kind
, "can")) {
2977 /* The CAN interface must be down to configure bitrate, etc... */
2978 if ((link
->flags
& IFF_UP
)) {
2979 r
= link_down(link
);
2981 link_enter_failed(link
);
2988 return link_set_can(link
);
2991 if (!(link
->flags
& IFF_UP
)) {
2992 r
= link_up_can(link
);
2994 link_enter_failed(link
);
3002 static int link_configure(Link
*link
) {
3006 assert(link
->network
);
3007 assert(link
->state
== LINK_STATE_PENDING
);
3009 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
3010 return link_configure_can(link
);
3012 /* Drop foreign config, but ignore loopback or critical devices.
3013 * We do not want to remove loopback address or addresses used for root NFS. */
3014 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
3015 r
= link_drop_foreign_config(link
);
3020 r
= link_set_proxy_arp(link
);
3024 r
= ipv6_proxy_ndp_addresses_configure(link
);
3028 r
= link_set_ipv4_forward(link
);
3032 r
= link_set_ipv6_forward(link
);
3036 r
= link_set_ipv6_privacy_extensions(link
);
3040 r
= link_set_ipv6_accept_ra(link
);
3044 r
= link_set_ipv6_dad_transmits(link
);
3048 r
= link_set_ipv6_hop_limit(link
);
3052 r
= link_set_flags(link
);
3056 r
= link_set_ipv6_mtu(link
);
3060 if (link_ipv4ll_enabled(link
)) {
3061 r
= ipv4ll_configure(link
);
3066 if (link_dhcp4_enabled(link
)) {
3067 r
= dhcp4_set_promote_secondaries(link
);
3071 r
= dhcp4_configure(link
);
3076 if (link_dhcp4_server_enabled(link
)) {
3077 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3081 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3086 if (link_dhcp6_enabled(link
) ||
3087 link_ipv6_accept_ra_enabled(link
)) {
3088 r
= dhcp6_configure(link
);
3093 if (link_ipv6_accept_ra_enabled(link
)) {
3094 r
= ndisc_configure(link
);
3099 if (link_radv_enabled(link
)) {
3100 r
= radv_configure(link
);
3105 if (link_lldp_rx_enabled(link
)) {
3106 r
= sd_lldp_new(&link
->lldp
);
3110 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3114 r
= sd_lldp_match_capabilities(link
->lldp
,
3115 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3116 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3117 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3121 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3125 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3129 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3133 r
= link_update_lldp(link
);
3138 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
3142 if (socket_ipv6_is_supported()) {
3143 r
= link_configure_addrgen_mode(link
);
3148 return link_configure_after_setting_mtu(link
);
3151 static int link_configure_after_setting_mtu(Link
*link
) {
3155 assert(link
->network
);
3156 assert(link
->state
== LINK_STATE_PENDING
);
3158 if (link
->setting_mtu
)
3161 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3162 r
= link_acquire_conf(link
);
3167 return link_enter_join_netdev(link
);
3170 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3173 if (duid
->raw_data_len
> 0)
3176 if (duid
->type
!= DUID_TYPE_UUID
)
3179 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3180 duid
->raw_data_len
= sizeof(sd_id128_t
);
3185 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3186 Manager
*manager
= userdata
;
3187 const sd_bus_error
*e
;
3197 e
= sd_bus_message_get_error(m
);
3199 log_error_errno(sd_bus_error_get_errno(e
),
3200 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3205 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3209 if (sz
!= sizeof(sd_id128_t
)) {
3210 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3214 memcpy(&manager
->product_uuid
, a
, sz
);
3215 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3216 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3218 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3221 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3222 r
= link_configure(link
);
3224 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3227 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3229 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3230 * even if the method fails. */
3231 manager
->has_product_uuid
= true;
3236 static bool link_requires_uuid(Link
*link
) {
3240 assert(link
->manager
);
3241 assert(link
->network
);
3243 duid
= link_get_duid(link
);
3244 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3247 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3250 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3256 static int link_configure_duid(Link
*link
) {
3262 assert(link
->manager
);
3263 assert(link
->network
);
3266 duid
= link_get_duid(link
);
3268 if (!link_requires_uuid(link
))
3271 if (m
->has_product_uuid
) {
3272 (void) duid_set_uuid(duid
, m
->product_uuid
);
3276 if (!m
->links_requesting_uuid
) {
3277 r
= manager_request_product_uuid(m
, link
);
3282 log_link_warning_errno(link
, r
,
3283 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3287 r
= set_put(m
->links_requesting_uuid
, link
);
3291 r
= set_put(m
->duids_requesting_uuid
, duid
);
3299 static int link_initialized_and_synced(Link
*link
) {
3304 assert(link
->ifname
);
3305 assert(link
->manager
);
3307 if (link
->state
!= LINK_STATE_PENDING
)
3310 log_link_debug(link
, "Link state is up-to-date");
3312 r
= link_new_bound_by_list(link
);
3316 r
= link_handle_bound_by_list(link
);
3320 if (!link
->network
) {
3321 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3322 &link
->mac
, &network
);
3324 link_enter_unmanaged(link
);
3326 } else if (r
== 0 && network
->unmanaged
) {
3327 link_enter_unmanaged(link
);
3332 if (link
->flags
& IFF_LOOPBACK
) {
3333 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3334 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3336 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3337 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3339 if (network
->dhcp_server
)
3340 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3343 r
= network_apply(network
, link
);
3348 r
= link_new_bound_to_list(link
);
3352 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3353 * link_configure() is called later asynchronously. */
3354 r
= link_configure_duid(link
);
3358 r
= link_configure(link
);
3365 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3366 (void) link_initialized_and_synced(link
);
3370 int link_initialized(Link
*link
, sd_device
*device
) {
3371 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3375 assert(link
->manager
);
3376 assert(link
->manager
->rtnl
);
3379 if (link
->state
!= LINK_STATE_PENDING
)
3382 if (link
->sd_device
)
3385 log_link_debug(link
, "udev initialized link");
3387 link
->sd_device
= sd_device_ref(device
);
3389 /* udev has initialized the link, but we don't know if we have yet
3390 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3391 * when it returns we know that the pending NEWLINKs have already been
3392 * processed and that we are up-to-date */
3394 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3399 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3400 link_netlink_destroy_callback
, link
);
3409 static int link_load(Link
*link
) {
3410 _cleanup_free_
char *network_file
= NULL
,
3413 *dhcp4_address
= NULL
,
3414 *ipv4ll_address
= NULL
;
3415 union in_addr_union address
;
3416 union in_addr_union route_dst
;
3422 r
= parse_env_file(NULL
, link
->state_file
,
3423 "NETWORK_FILE", &network_file
,
3424 "ADDRESSES", &addresses
,
3426 "DHCP4_ADDRESS", &dhcp4_address
,
3427 "IPV4LL_ADDRESS", &ipv4ll_address
);
3428 if (r
< 0 && r
!= -ENOENT
)
3429 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3436 suffix
= strrchr(network_file
, '.');
3438 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3439 goto network_file_fail
;
3443 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3445 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3446 goto network_file_fail
;
3449 r
= network_apply(network
, link
);
3451 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3460 _cleanup_free_
char *address_str
= NULL
;
3461 char *prefixlen_str
;
3463 unsigned char prefixlen
;
3465 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3467 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3473 prefixlen_str
= strchr(address_str
, '/');
3474 if (!prefixlen_str
) {
3475 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3479 *prefixlen_str
++ = '\0';
3481 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3483 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3487 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3489 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3493 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3495 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3504 _cleanup_free_
char *route_str
= NULL
;
3505 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3507 char *prefixlen_str
;
3509 unsigned char prefixlen
, tos
, table
;
3512 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3514 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3520 prefixlen_str
= strchr(route_str
, '/');
3521 if (!prefixlen_str
) {
3522 log_link_debug(link
, "Failed to parse route %s", route_str
);
3526 *prefixlen_str
++ = '\0';
3528 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3530 log_link_debug(link
,
3531 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3536 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3538 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3542 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3544 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3546 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3547 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3548 0, route_expire_handler
, route
);
3550 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3553 route
->lifetime
= lifetime
;
3554 sd_event_source_unref(route
->expire
);
3555 route
->expire
= TAKE_PTR(expire
);
3559 if (dhcp4_address
) {
3560 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3562 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3563 goto dhcp4_address_fail
;
3566 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3568 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3570 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3572 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3577 if (ipv4ll_address
) {
3578 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3580 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3581 goto ipv4ll_address_fail
;
3584 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3586 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3588 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3590 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3593 ipv4ll_address_fail
:
3598 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3599 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3600 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3609 r
= link_new(m
, message
, ret
);
3615 log_link_debug(link
, "Link %d added", link
->ifindex
);
3617 r
= link_load(link
);
3621 if (detect_container() <= 0) {
3622 /* not in a container, udev will be around */
3623 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3624 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3626 log_link_warning_errno(link
, r
, "Could not find device: %m");
3630 r
= sd_device_get_is_initialized(device
);
3632 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3637 log_link_debug(link
, "link pending udev initialization...");
3641 r
= device_is_renaming(device
);
3643 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3647 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3651 r
= link_initialized(link
, device
);
3655 r
= link_initialized_and_synced(link
);
3662 link_enter_failed(link
);
3666 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3671 log_link_info(link
, "Gained IPv6LL");
3673 link
->ipv6ll_address
= *address
;
3674 link_check_ready(link
);
3676 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3677 r
= link_acquire_ipv6_conf(link
);
3679 link_enter_failed(link
);
3687 static int link_carrier_gained(Link
*link
) {
3692 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3693 r
= link_acquire_conf(link
);
3695 link_enter_failed(link
);
3699 r
= link_request_set_addresses(link
);
3704 r
= link_handle_bound_by_list(link
);
3711 static int link_carrier_lost(Link
*link
) {
3716 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3719 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3720 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3721 * configuration and stop the clients as well. */
3722 if (link
->setting_mtu
)
3725 r
= link_stop_clients(link
);
3727 link_enter_failed(link
);
3731 if (link_dhcp4_server_enabled(link
))
3732 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3734 r
= link_drop_config(link
);
3738 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3739 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3740 r
= link_drop_foreign_config(link
);
3745 r
= link_handle_bound_by_list(link
);
3752 int link_carrier_reset(Link
*link
) {
3757 if (link_has_carrier(link
)) {
3758 r
= link_carrier_lost(link
);
3762 r
= link_carrier_gained(link
);
3766 log_link_info(link
, "Reset carrier");
3772 int link_update(Link
*link
, sd_netlink_message
*m
) {
3773 struct ether_addr mac
;
3776 bool had_carrier
, carrier_gained
, carrier_lost
;
3780 assert(link
->ifname
);
3783 if (link
->state
== LINK_STATE_LINGER
) {
3784 log_link_info(link
, "Link readded");
3785 link_set_state(link
, LINK_STATE_CONFIGURING
);
3787 r
= link_new_carrier_maps(link
);
3792 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3793 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3794 Manager
*manager
= link
->manager
;
3796 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3799 r
= link_add(manager
, m
, &link
);
3804 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3805 if (r
>= 0 && mtu
> 0) {
3807 if (link
->original_mtu
== 0) {
3808 link
->original_mtu
= mtu
;
3809 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3812 if (link
->dhcp_client
) {
3813 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3816 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3820 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3822 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3826 /* The kernel may broadcast NEWLINK messages without the MAC address
3827 set, simply ignore them. */
3828 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3830 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3833 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3836 log_link_debug(link
, "MAC address: "
3837 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3838 mac
.ether_addr_octet
[0],
3839 mac
.ether_addr_octet
[1],
3840 mac
.ether_addr_octet
[2],
3841 mac
.ether_addr_octet
[3],
3842 mac
.ether_addr_octet
[4],
3843 mac
.ether_addr_octet
[5]);
3846 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3848 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3851 if (link
->dhcp_client
) {
3852 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3853 (const uint8_t *) &link
->mac
,
3857 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3859 r
= dhcp4_set_client_identifier(link
);
3864 if (link
->dhcp6_client
) {
3865 const DUID
* duid
= link_get_duid(link
);
3867 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3868 (const uint8_t *) &link
->mac
,
3872 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3874 if (link
->network
->iaid_set
) {
3875 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3876 link
->network
->iaid
);
3878 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3881 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3883 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3884 duid
->raw_data_len
);
3886 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3890 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3892 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3896 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3898 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3903 had_carrier
= link_has_carrier(link
);
3905 r
= link_update_flags(link
, m
);
3909 r
= link_update_lldp(link
);
3913 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3914 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3916 if (carrier_gained
) {
3917 log_link_info(link
, "Gained carrier");
3919 r
= link_carrier_gained(link
);
3922 } else if (carrier_lost
) {
3923 log_link_info(link
, "Lost carrier");
3925 r
= link_carrier_lost(link
);
3933 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3941 if (hashmap_isempty(h
))
3945 HASHMAP_FOREACH(link
, h
, i
) {
3949 fprintf(f
, "%i", link
->ifindex
);
3956 int link_save(Link
*link
) {
3957 _cleanup_free_
char *temp_path
= NULL
;
3958 _cleanup_fclose_
FILE *f
= NULL
;
3959 const char *admin_state
, *oper_state
;
3966 assert(link
->state_file
);
3967 assert(link
->lease_file
);
3968 assert(link
->manager
);
3970 if (link
->state
== LINK_STATE_LINGER
) {
3971 unlink(link
->state_file
);
3975 link_lldp_save(link
);
3977 admin_state
= link_state_to_string(link
->state
);
3978 assert(admin_state
);
3980 oper_state
= link_operstate_to_string(link
->operstate
);
3983 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3987 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3988 (void) fchmod(fileno(f
), 0644);
3991 "# This is private data. Do not parse.\n"
3994 admin_state
, oper_state
);
3996 if (link
->network
) {
3998 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3999 const char *dhcp_domainname
= NULL
;
4000 char **dhcp6_domains
= NULL
;
4001 char **dhcp_domains
= NULL
;
4004 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4005 yes_no(link
->network
->required_for_online
));
4007 if (link
->dhcp6_client
) {
4008 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4009 if (r
< 0 && r
!= -ENOMSG
)
4010 log_link_debug(link
, "No DHCPv6 lease");
4013 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4018 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
4019 _cleanup_free_
char *b
= NULL
;
4021 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
4022 &link
->network
->dns
[j
].address
, &b
);
4024 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4034 if (link
->network
->dhcp_use_dns
&&
4036 const struct in_addr
*addresses
;
4038 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4040 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4044 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4045 struct in6_addr
*in6_addrs
;
4047 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4051 serialize_in6_addrs(f
, in6_addrs
, r
);
4056 /* Make sure to flush out old entries before we use the NDISC data */
4059 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4062 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4066 serialize_in6_addrs(f
, &dd
->address
, 1);
4075 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4077 if (link
->network
->dhcp_use_ntp
&&
4079 const struct in_addr
*addresses
;
4081 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4083 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4087 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4088 struct in6_addr
*in6_addrs
;
4091 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4096 serialize_in6_addrs(f
, in6_addrs
, r
);
4100 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4102 fputstrv(f
, hosts
, NULL
, &space
);
4107 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4108 if (link
->dhcp_lease
) {
4109 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4110 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4113 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4116 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4118 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4121 if (dhcp_domainname
)
4122 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4124 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4126 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4128 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4129 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4134 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4136 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4139 if (dhcp_domainname
)
4140 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4142 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4144 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4146 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4147 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4152 fprintf(f
, "LLMNR=%s\n",
4153 resolve_support_to_string(link
->network
->llmnr
));
4154 fprintf(f
, "MDNS=%s\n",
4155 resolve_support_to_string(link
->network
->mdns
));
4156 if (link
->network
->dns_default_route
>= 0)
4157 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4159 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4160 fprintf(f
, "DNS_OVER_TLS=%s\n",
4161 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4163 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4164 fprintf(f
, "DNSSEC=%s\n",
4165 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4167 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4170 fputs("DNSSEC_NTA=", f
);
4172 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4173 fputs_with_space(f
, n
, NULL
, &space
);
4177 fputs("ADDRESSES=", f
);
4179 SET_FOREACH(a
, link
->addresses
, i
) {
4180 _cleanup_free_
char *address_str
= NULL
;
4182 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4186 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4191 fputs("ROUTES=", f
);
4193 SET_FOREACH(route
, link
->routes
, i
) {
4194 _cleanup_free_
char *route_str
= NULL
;
4196 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4200 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4201 space
? " " : "", route_str
,
4202 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4209 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4210 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4212 if (link
->dhcp_lease
) {
4213 struct in_addr address
;
4214 const char *tz
= NULL
;
4216 assert(link
->network
);
4218 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4220 fprintf(f
, "TIMEZONE=%s\n", tz
);
4222 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4224 fputs("DHCP4_ADDRESS=", f
);
4225 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4229 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4237 unlink(link
->lease_file
);
4240 struct in_addr address
;
4242 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4244 fputs("IPV4LL_ADDRESS=", f
);
4245 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4250 r
= fflush_and_check(f
);
4254 if (rename(temp_path
, link
->state_file
) < 0) {
4262 (void) unlink(link
->state_file
);
4264 (void) unlink(temp_path
);
4266 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4269 /* The serialized state in /run is no longer up-to-date. */
4270 void link_dirty(Link
*link
) {
4275 /* mark manager dirty as link is dirty */
4276 manager_dirty(link
->manager
);
4278 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4280 /* allocation errors are ignored */
4283 r
= set_put(link
->manager
->dirty_links
, link
);
4285 /* don't take another ref if the link was already dirty */
4291 /* The serialized state in /run is up-to-date */
4292 void link_clean(Link
*link
) {
4294 assert(link
->manager
);
4296 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4299 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4300 [LINK_STATE_PENDING
] = "pending",
4301 [LINK_STATE_CONFIGURING
] = "configuring",
4302 [LINK_STATE_CONFIGURED
] = "configured",
4303 [LINK_STATE_UNMANAGED
] = "unmanaged",
4304 [LINK_STATE_FAILED
] = "failed",
4305 [LINK_STATE_LINGER
] = "linger",
4308 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4310 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4311 [LINK_OPERSTATE_OFF
] = "off",
4312 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4313 [LINK_OPERSTATE_DORMANT
] = "dormant",
4314 [LINK_OPERSTATE_CARRIER
] = "carrier",
4315 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4316 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
4317 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4320 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);