1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
8 #include "alloc-util.h"
10 #include "dhcp-identifier.h"
11 #include "dhcp-lease-internal.h"
15 #include "missing_network.h"
16 #include "netdev/vrf.h"
17 #include "netlink-util.h"
18 #include "network-internal.h"
19 #include "networkd-ipv6-proxy-ndp.h"
20 #include "networkd-lldp-tx.h"
21 #include "networkd-manager.h"
22 #include "networkd-ndisc.h"
23 #include "networkd-neighbor.h"
24 #include "networkd-radv.h"
25 #include "networkd-routing-policy-rule.h"
27 #include "socket-util.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
31 #include "sysctl-util.h"
32 #include "tmpfile-util.h"
33 #include "udev-util.h"
37 uint32_t link_get_vrf_table(Link
*link
) {
38 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
41 uint32_t link_get_dhcp_route_table(Link
*link
) {
42 /* When the interface is part of an VRF use the VRFs routing table, unless
43 * another table is explicitly specified. */
44 if (link
->network
->dhcp_route_table_set
)
45 return link
->network
->dhcp_route_table
;
46 return link_get_vrf_table(link
);
49 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
50 if (link
->network
->ipv6_accept_ra_route_table_set
)
51 return link
->network
->ipv6_accept_ra_route_table
;
52 return link_get_vrf_table(link
);
55 DUID
* link_get_duid(Link
*link
) {
56 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
57 return &link
->network
->duid
;
59 return &link
->manager
->duid
;
62 static bool link_dhcp6_enabled(Link
*link
) {
65 if (!socket_ipv6_is_supported())
68 if (link
->flags
& IFF_LOOPBACK
)
74 if (link
->network
->bond
)
77 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
80 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
83 static bool link_dhcp4_enabled(Link
*link
) {
86 if (link
->flags
& IFF_LOOPBACK
)
92 if (link
->network
->bond
)
95 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
98 static bool link_dhcp4_server_enabled(Link
*link
) {
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (link
->network
->bond
)
110 return link
->network
->dhcp_server
;
113 static bool link_ipv4ll_enabled(Link
*link
) {
116 if (link
->flags
& IFF_LOOPBACK
)
122 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
125 if (link
->network
->bond
)
128 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
131 static bool link_ipv6ll_enabled(Link
*link
) {
134 if (!socket_ipv6_is_supported())
137 if (link
->flags
& IFF_LOOPBACK
)
143 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
146 if (link
->network
->bond
)
149 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
152 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
155 static bool link_ipv6_enabled(Link
*link
) {
158 if (!socket_ipv6_is_supported())
161 if (link
->network
->bond
)
164 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
167 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
168 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
171 static bool link_radv_enabled(Link
*link
) {
174 if (!link_ipv6ll_enabled(link
))
177 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
180 static bool link_lldp_rx_enabled(Link
*link
) {
183 if (link
->flags
& IFF_LOOPBACK
)
186 if (link
->iftype
!= ARPHRD_ETHER
)
192 /* LLDP should be handled on bridge slaves as those have a direct
193 * connection to their peers not on the bridge master. Linux doesn't
194 * even (by default) forward lldp packets to the bridge master.*/
195 if (streq_ptr("bridge", link
->kind
))
198 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
201 static bool link_lldp_emit_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
207 if (link
->iftype
!= ARPHRD_ETHER
)
213 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
216 static bool link_ipv4_forward_enabled(Link
*link
) {
219 if (link
->flags
& IFF_LOOPBACK
)
225 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
228 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
231 static bool link_ipv6_forward_enabled(Link
*link
) {
234 if (!socket_ipv6_is_supported())
237 if (link
->flags
& IFF_LOOPBACK
)
243 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
246 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
249 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
252 static bool link_proxy_arp_enabled(Link
*link
) {
255 if (link
->flags
& IFF_LOOPBACK
)
261 if (link
->network
->proxy_arp
< 0)
267 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
270 if (!socket_ipv6_is_supported())
273 if (link
->flags
& IFF_LOOPBACK
)
279 if (!link_ipv6ll_enabled(link
))
282 /* If unset use system default (enabled if local forwarding is disabled.
283 * disabled if local forwarding is enabled).
284 * If set, ignore or enforce RA independent of local forwarding state.
286 if (link
->network
->ipv6_accept_ra
< 0)
287 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
288 return !link_ipv6_forward_enabled(link
);
289 else if (link
->network
->ipv6_accept_ra
> 0)
290 /* accept RA even if ip_forward is enabled */
297 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
300 if (!socket_ipv6_is_supported())
301 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
303 if (link
->flags
& IFF_LOOPBACK
)
304 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
307 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
309 return link
->network
->ipv6_privacy_extensions
;
312 static int link_enable_ipv6(Link
*link
) {
316 if (link
->flags
& IFF_LOOPBACK
)
319 disabled
= !link_ipv6_enabled(link
);
321 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
323 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
324 enable_disable(!disabled
), link
->ifname
);
326 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
331 static bool link_is_enslaved(Link
*link
) {
332 if (link
->flags
& IFF_SLAVE
)
333 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
336 if (!link
->enslaved_raw
)
342 if (link
->network
->bridge
)
343 /* TODO: support the case when link is not managed by networkd. */
349 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
355 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
358 link_update_operstate(master
, true);
361 void link_update_operstate(Link
*link
, bool also_update_master
) {
362 LinkOperationalState operstate
;
367 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
368 operstate
= LINK_OPERSTATE_DORMANT
;
369 else if (link_has_carrier(link
)) {
371 uint8_t scope
= RT_SCOPE_NOWHERE
;
373 /* if we have carrier, check what addresses we have */
374 SET_FOREACH(address
, link
->addresses
, i
) {
375 if (!address_is_ready(address
))
378 if (address
->scope
< scope
)
379 scope
= address
->scope
;
382 /* for operstate we also take foreign addresses into account */
383 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
384 if (!address_is_ready(address
))
387 if (address
->scope
< scope
)
388 scope
= address
->scope
;
391 if (scope
< RT_SCOPE_SITE
)
392 /* universally accessible addresses found */
393 operstate
= LINK_OPERSTATE_ROUTABLE
;
394 else if (scope
< RT_SCOPE_HOST
)
395 /* only link or site local addresses found */
396 operstate
= LINK_OPERSTATE_DEGRADED
;
398 /* no useful addresses found */
399 operstate
= LINK_OPERSTATE_CARRIER
;
400 } else if (link
->flags
& IFF_UP
)
401 operstate
= LINK_OPERSTATE_NO_CARRIER
;
403 operstate
= LINK_OPERSTATE_OFF
;
405 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
406 link_is_enslaved(link
))
407 operstate
= LINK_OPERSTATE_ENSLAVED
;
409 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
412 SET_FOREACH(slave
, link
->slaves
, i
) {
413 link_update_operstate(slave
, false);
415 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
416 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
420 if (link
->operstate
!= operstate
) {
421 link
->operstate
= operstate
;
422 link_send_changed(link
, "OperationalState", NULL
);
426 if (also_update_master
&& link
->network
) {
427 link_update_master_operstate(link
, link
->network
->bond
);
428 link_update_master_operstate(link
, link
->network
->bridge
);
432 #define FLAG_STRING(string, flag, old, new) \
433 (((old ^ new) & flag) \
434 ? ((old & flag) ? (" -" string) : (" +" string)) \
437 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
438 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
444 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
446 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
448 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
450 /* if we got a message without operstate, take it to mean
451 the state was unchanged */
452 operstate
= link
->kernel_operstate
;
454 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
457 if (link
->flags
!= flags
) {
458 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",
459 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
460 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
461 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
462 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
463 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
464 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
465 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
466 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
467 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
468 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
469 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
470 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
471 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
472 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
473 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
474 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
475 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
476 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
477 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
479 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
480 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
481 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
482 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
483 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
484 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
485 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
486 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
488 /* link flags are currently at most 18 bits, let's align to
490 if (unknown_flags_added
)
492 "Unknown link flags gained: %#.5x (ignoring)",
493 unknown_flags_added
);
495 if (unknown_flags_removed
)
497 "Unknown link flags lost: %#.5x (ignoring)",
498 unknown_flags_removed
);
502 link
->kernel_operstate
= operstate
;
504 link_update_operstate(link
, true);
509 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
511 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
512 _cleanup_(link_unrefp
) Link
*link
= NULL
;
514 const char *ifname
, *kind
= NULL
;
516 unsigned short iftype
;
522 /* check for link kind */
523 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
525 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
526 r
= sd_netlink_message_exit_container(message
);
531 r
= sd_netlink_message_get_type(message
, &type
);
534 else if (type
!= RTM_NEWLINK
)
537 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
540 else if (ifindex
<= 0)
543 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
547 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
558 .state
= LINK_STATE_PENDING
,
559 .rtnl_extended_attrs
= true,
564 link
->ifname
= strdup(ifname
);
569 link
->kind
= strdup(kind
);
574 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
576 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
578 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
580 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
582 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
585 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
588 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
591 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
595 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
599 r
= link_update_flags(link
, message
);
603 *ret
= TAKE_PTR(link
);
608 static Link
*link_free(Link
*link
) {
616 while ((route
= set_first(link
->routes
)))
619 while ((route
= set_first(link
->routes_foreign
)))
622 link
->routes
= set_free(link
->routes
);
623 link
->routes_foreign
= set_free(link
->routes_foreign
);
625 while ((address
= set_first(link
->addresses
)))
626 address_free(address
);
628 while ((address
= set_first(link
->addresses_foreign
)))
629 address_free(address
);
631 link
->addresses
= set_free(link
->addresses
);
632 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
634 while ((address
= link
->pool_addresses
)) {
635 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
636 address_free(address
);
639 sd_dhcp_server_unref(link
->dhcp_server
);
640 sd_dhcp_client_unref(link
->dhcp_client
);
641 sd_dhcp_lease_unref(link
->dhcp_lease
);
643 link_lldp_emit_stop(link
);
645 free(link
->lease_file
);
647 sd_lldp_unref(link
->lldp
);
648 free(link
->lldp_file
);
652 sd_ipv4ll_unref(link
->ipv4ll
);
653 sd_dhcp6_client_unref(link
->dhcp6_client
);
654 sd_ndisc_unref(link
->ndisc
);
655 sd_radv_unref(link
->radv
);
660 (void) unlink(link
->state_file
);
661 free(link
->state_file
);
663 sd_device_unref(link
->sd_device
);
665 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
666 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
667 hashmap_free(link
->bound_to_links
);
669 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
670 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
671 hashmap_free(link
->bound_by_links
);
673 set_free_with_destructor(link
->slaves
, link_unref
);
678 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
680 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
687 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
696 static void link_set_state(Link
*link
, LinkState state
) {
699 if (link
->state
== state
)
702 log_link_debug(link
, "State changed: %s -> %s",
703 link_state_to_string(link
->state
),
704 link_state_to_string(state
));
708 link_send_changed(link
, "AdministrativeState", NULL
);
711 static void link_enter_unmanaged(Link
*link
) {
714 log_link_debug(link
, "Unmanaged");
716 link_set_state(link
, LINK_STATE_UNMANAGED
);
721 static int link_stop_clients(Link
*link
) {
725 assert(link
->manager
);
726 assert(link
->manager
->event
);
728 if (link
->dhcp_client
) {
729 k
= sd_dhcp_client_stop(link
->dhcp_client
);
731 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
735 k
= sd_ipv4ll_stop(link
->ipv4ll
);
737 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
740 if (link
->dhcp6_client
) {
741 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
743 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
747 k
= sd_ndisc_stop(link
->ndisc
);
749 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
753 k
= sd_radv_stop(link
->radv
);
755 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
758 link_lldp_emit_stop(link
);
762 void link_enter_failed(Link
*link
) {
765 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
768 log_link_warning(link
, "Failed");
770 link_set_state(link
, LINK_STATE_FAILED
);
772 link_stop_clients(link
);
777 static Address
* link_find_dhcp_server_address(Link
*link
) {
781 assert(link
->network
);
783 /* The first statically configured address if there is any */
784 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
786 if (address
->family
!= AF_INET
)
789 if (in_addr_is_null(address
->family
, &address
->in_addr
))
795 /* If that didn't work, find a suitable address we got from the pool */
796 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
797 if (address
->family
!= AF_INET
)
806 static int link_join_netdevs_after_configured(Link
*link
) {
811 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
812 if (netdev
->ifindex
> 0)
813 /* Assume already enslaved. */
816 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
819 log_struct(LOG_DEBUG
,
820 LOG_LINK_INTERFACE(link
),
821 LOG_NETDEV_INTERFACE(netdev
),
822 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
824 r
= netdev_join(netdev
, link
, NULL
);
826 return log_struct_errno(LOG_WARNING
, r
,
827 LOG_LINK_INTERFACE(link
),
828 LOG_NETDEV_INTERFACE(netdev
),
829 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
835 static void link_enter_configured(Link
*link
) {
837 assert(link
->network
);
839 if (link
->state
!= LINK_STATE_CONFIGURING
)
842 log_link_info(link
, "Configured");
844 link_set_state(link
, LINK_STATE_CONFIGURED
);
846 (void) link_join_netdevs_after_configured(link
);
851 static int link_request_set_routing_policy_rule(Link
*link
) {
852 RoutingPolicyRule
*rule
, *rrule
= NULL
;
856 assert(link
->network
);
858 link_set_state(link
, LINK_STATE_CONFIGURING
);
859 link
->routing_policy_rules_configured
= false;
861 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
862 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
863 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
864 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
866 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
870 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
872 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
873 link_enter_failed(link
);
877 link
->routing_policy_rule_messages
++;
880 routing_policy_rule_purge(link
->manager
, link
);
881 if (link
->routing_policy_rule_messages
== 0) {
882 link
->routing_policy_rules_configured
= true;
883 link_check_ready(link
);
885 log_link_debug(link
, "Setting routing policy rules");
890 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
894 assert(link
->route_messages
> 0);
895 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
896 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
898 link
->route_messages
--;
900 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
903 r
= sd_netlink_message_get_errno(m
);
904 if (r
< 0 && r
!= -EEXIST
)
905 log_link_warning_errno(link
, r
, "Could not set route: %m");
907 if (link
->route_messages
== 0) {
908 log_link_debug(link
, "Routes set");
909 link
->static_routes_configured
= true;
910 link_check_ready(link
);
916 static int link_request_set_routes(Link
*link
) {
918 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
919 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
926 assert(link
->network
);
927 assert(link
->addresses_configured
);
928 assert(link
->address_messages
== 0);
929 assert(link
->state
!= _LINK_STATE_INVALID
);
931 link_set_state(link
, LINK_STATE_CONFIGURING
);
932 link
->static_routes_configured
= false;
934 r
= link_request_set_routing_policy_rule(link
);
938 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
939 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
940 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
942 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
945 r
= route_configure(rt
, link
, route_handler
);
947 log_link_warning_errno(link
, r
, "Could not set routes: %m");
948 link_enter_failed(link
);
952 link
->route_messages
++;
955 if (link
->route_messages
== 0) {
956 link
->static_routes_configured
= true;
957 link_check_ready(link
);
959 log_link_debug(link
, "Setting routes");
964 void link_check_ready(Link
*link
) {
970 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
976 if (!link
->addresses_configured
)
979 if (!link
->neighbors_configured
)
982 SET_FOREACH(a
, link
->addresses
, i
)
983 if (!address_is_ready(a
))
986 if (!link
->addresses_ready
) {
987 link
->addresses_ready
= true;
988 link_request_set_routes(link
);
991 if (!link
->static_routes_configured
)
994 if (!link
->routing_policy_rules_configured
)
997 if (link_ipv4ll_enabled(link
))
998 if (!link
->ipv4ll_address
||
1002 if (link_ipv6ll_enabled(link
) &&
1003 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1006 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
1007 !link
->dhcp4_configured
) ||
1008 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
1009 !link
->dhcp6_configured
) ||
1010 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
1011 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
1014 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1017 if (link
->state
!= LINK_STATE_CONFIGURED
)
1018 link_enter_configured(link
);
1023 static int link_request_set_neighbors(Link
*link
) {
1028 assert(link
->network
);
1029 assert(link
->state
!= _LINK_STATE_INVALID
);
1031 link_set_state(link
, LINK_STATE_CONFIGURING
);
1032 link
->neighbors_configured
= false;
1034 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1035 r
= neighbor_configure(neighbor
, link
, NULL
);
1037 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1038 link_enter_failed(link
);
1043 if (link
->neighbor_messages
== 0) {
1044 link
->neighbors_configured
= true;
1045 link_check_ready(link
);
1047 log_link_debug(link
, "Setting neighbors");
1052 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1058 assert(link
->ifname
);
1059 assert(link
->address_messages
> 0);
1060 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1061 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1063 link
->address_messages
--;
1065 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1068 r
= sd_netlink_message_get_errno(m
);
1069 if (r
< 0 && r
!= -EEXIST
)
1070 log_link_warning_errno(link
, r
, "could not set address: %m");
1072 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1074 if (link
->address_messages
== 0) {
1075 log_link_debug(link
, "Addresses set");
1076 link
->addresses_configured
= true;
1077 link_check_ready(link
);
1083 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1084 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1085 size_t n_addresses
= 0, n_allocated
= 0;
1088 log_debug("Copying DNS server information from %s", link
->ifname
);
1093 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1096 /* Only look for IPv4 addresses */
1097 if (link
->network
->dns
[i
].family
!= AF_INET
)
1100 ia
= link
->network
->dns
[i
].address
.in
;
1102 /* Never propagate obviously borked data */
1103 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1106 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1109 addresses
[n_addresses
++] = ia
;
1112 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1113 const struct in_addr
*da
= NULL
;
1116 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1119 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1122 for (j
= 0; j
< n
; j
++)
1123 if (in4_addr_is_non_local(&da
[j
]))
1124 addresses
[n_addresses
++] = da
[j
];
1128 if (n_addresses
<= 0)
1131 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1134 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1135 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1136 size_t n_addresses
= 0, n_allocated
= 0;
1142 log_debug("Copying NTP server information from %s", link
->ifname
);
1144 STRV_FOREACH(a
, link
->network
->ntp
) {
1147 /* Only look for IPv4 addresses */
1148 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1151 /* Never propagate obviously borked data */
1152 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1155 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1158 addresses
[n_addresses
++] = ia
;
1161 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1162 const struct in_addr
*da
= NULL
;
1165 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1168 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1171 for (j
= 0; j
< n
; j
++)
1172 if (in4_addr_is_non_local(&da
[j
]))
1173 addresses
[n_addresses
++] = da
[j
];
1177 if (n_addresses
<= 0)
1180 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1183 static int link_set_bridge_fdb(Link
*link
) {
1184 FdbEntry
*fdb_entry
;
1187 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1188 r
= fdb_entry_configure(link
, fdb_entry
);
1190 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1196 static int link_request_set_addresses(Link
*link
) {
1197 AddressLabel
*label
;
1202 assert(link
->network
);
1203 assert(link
->state
!= _LINK_STATE_INVALID
);
1205 link_set_state(link
, LINK_STATE_CONFIGURING
);
1207 /* Reset all *_configured flags we are configuring. */
1208 link
->addresses_configured
= false;
1209 link
->addresses_ready
= false;
1210 link
->neighbors_configured
= false;
1211 link
->static_routes_configured
= false;
1212 link
->routing_policy_rules_configured
= false;
1214 r
= link_set_bridge_fdb(link
);
1218 r
= link_request_set_neighbors(link
);
1222 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1225 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1227 r
= address_configure(ad
, link
, address_handler
, update
);
1229 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1230 link_enter_failed(link
);
1234 link
->address_messages
++;
1237 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1238 r
= address_label_configure(label
, link
, NULL
, false);
1240 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1241 link_enter_failed(link
);
1245 link
->address_label_messages
++;
1248 /* now that we can figure out a default address for the dhcp server,
1250 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1252 Link
*uplink
= NULL
;
1253 bool acquired_uplink
= false;
1255 address
= link_find_dhcp_server_address(link
);
1257 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1258 link_enter_failed(link
);
1262 /* use the server address' subnet as the pool */
1263 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1264 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1269 r = sd_dhcp_server_set_router(link->dhcp_server,
1270 &main_address->in_addr.in);
1275 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1276 r
= sd_dhcp_server_set_max_lease_time(
1278 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1283 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1284 r
= sd_dhcp_server_set_default_lease_time(
1286 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1291 if (link
->network
->dhcp_server_emit_dns
) {
1293 if (link
->network
->n_dhcp_server_dns
> 0)
1294 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1296 uplink
= manager_find_uplink(link
->manager
, link
);
1297 acquired_uplink
= true;
1300 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1303 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1306 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1309 if (link
->network
->dhcp_server_emit_ntp
) {
1311 if (link
->network
->n_dhcp_server_ntp
> 0)
1312 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1314 if (!acquired_uplink
)
1315 uplink
= manager_find_uplink(link
->manager
, link
);
1318 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1321 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1325 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1328 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1330 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1332 if (link
->network
->dhcp_server_emit_timezone
) {
1333 _cleanup_free_
char *buffer
= NULL
;
1334 const char *tz
= NULL
;
1336 if (link
->network
->dhcp_server_timezone
)
1337 tz
= link
->network
->dhcp_server_timezone
;
1339 r
= get_timezone(&buffer
);
1341 log_warning_errno(r
, "Failed to determine timezone: %m");
1347 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1352 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1353 r
= sd_dhcp_server_start(link
->dhcp_server
);
1355 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1357 link_enter_failed(link
);
1363 log_link_debug(link
, "Offering DHCPv4 leases");
1366 if (link
->address_messages
== 0) {
1367 link
->addresses_configured
= true;
1368 link_check_ready(link
);
1370 log_link_debug(link
, "Setting addresses");
1375 static int link_set_bridge_vlan(Link
*link
) {
1378 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1380 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1385 static int link_set_proxy_arp(Link
*link
) {
1388 if (!link_proxy_arp_enabled(link
))
1391 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1393 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1398 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1403 log_link_debug(link
, "Set link");
1405 r
= sd_netlink_message_get_errno(m
);
1406 if (r
< 0 && r
!= -EEXIST
) {
1407 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1408 link_enter_failed(link
);
1414 static int link_configure_after_setting_mtu(Link
*link
);
1416 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1421 assert(link
->ifname
);
1423 link
->setting_mtu
= false;
1425 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1428 r
= sd_netlink_message_get_errno(m
);
1430 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1434 log_link_debug(link
, "Setting MTU done.");
1436 if (link
->state
== LINK_STATE_INITIALIZED
)
1437 (void) link_configure_after_setting_mtu(link
);
1442 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1443 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1447 assert(link
->manager
);
1448 assert(link
->manager
->rtnl
);
1450 if (mtu
== 0 || link
->setting_mtu
)
1453 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1456 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1458 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1460 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1462 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1463 * for this interface, then disable IPv6 else enable it. */
1464 (void) link_enable_ipv6(link
);
1466 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1467 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1468 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1470 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1471 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1476 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1478 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1480 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1481 link_netlink_destroy_callback
, link
);
1483 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1486 link
->setting_mtu
= true;
1491 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1496 assert(link
->ifname
);
1498 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1501 r
= sd_netlink_message_get_errno(m
);
1503 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1508 static int link_set_flags(Link
*link
) {
1509 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1510 unsigned ifi_change
= 0;
1511 unsigned ifi_flags
= 0;
1515 assert(link
->manager
);
1516 assert(link
->manager
->rtnl
);
1518 if (link
->flags
& IFF_LOOPBACK
)
1524 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1527 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1529 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1531 if (link
->network
->arp
>= 0) {
1532 ifi_change
|= IFF_NOARP
;
1533 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1536 if (link
->network
->multicast
>= 0) {
1537 ifi_change
|= IFF_MULTICAST
;
1538 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1541 if (link
->network
->allmulticast
>= 0) {
1542 ifi_change
|= IFF_ALLMULTI
;
1543 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1546 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1548 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1550 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1551 link_netlink_destroy_callback
, link
);
1553 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1560 static int link_set_bridge(Link
*link
) {
1561 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1565 assert(link
->network
);
1567 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1569 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1571 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1573 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1575 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1577 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1579 if (link
->network
->use_bpdu
>= 0) {
1580 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1582 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1585 if (link
->network
->hairpin
>= 0) {
1586 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1588 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1591 if (link
->network
->fast_leave
>= 0) {
1592 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1594 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1597 if (link
->network
->allow_port_to_be_root
>= 0) {
1598 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1600 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1604 if (link
->network
->unicast_flood
>= 0) {
1605 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1607 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1610 if (link
->network
->multicast_flood
>= 0) {
1611 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
1613 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
1616 if (link
->network
->multicast_to_unicast
>= 0) {
1617 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1619 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1622 if (link
->network
->neighbor_suppression
>= 0) {
1623 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
1625 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
1628 if (link
->network
->learning
>= 0) {
1629 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
1631 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
1634 if (link
->network
->cost
!= 0) {
1635 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1637 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1640 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1641 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1643 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1646 r
= sd_netlink_message_close_container(req
);
1648 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1650 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1651 link_netlink_destroy_callback
, link
);
1653 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1660 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1665 assert(link
->ifname
);
1667 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1670 r
= sd_netlink_message_get_errno(m
);
1672 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1679 static int link_set_bond(Link
*link
) {
1680 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1684 assert(link
->network
);
1686 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1688 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1690 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1692 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1694 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1696 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1698 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1700 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1702 if (link
->network
->active_slave
) {
1703 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1705 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1708 if (link
->network
->primary_slave
) {
1709 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1711 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1714 r
= sd_netlink_message_close_container(req
);
1716 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1718 r
= sd_netlink_message_close_container(req
);
1720 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1722 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1723 link_netlink_destroy_callback
, link
);
1725 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1732 static int link_lldp_save(Link
*link
) {
1733 _cleanup_free_
char *temp_path
= NULL
;
1734 _cleanup_fclose_
FILE *f
= NULL
;
1735 sd_lldp_neighbor
**l
= NULL
;
1739 assert(link
->lldp_file
);
1742 (void) unlink(link
->lldp_file
);
1746 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1750 (void) unlink(link
->lldp_file
);
1756 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1760 fchmod(fileno(f
), 0644);
1762 for (i
= 0; i
< n
; i
++) {
1767 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1772 (void) fwrite(&u
, 1, sizeof(u
), f
);
1773 (void) fwrite(p
, 1, sz
, f
);
1776 r
= fflush_and_check(f
);
1780 if (rename(temp_path
, link
->lldp_file
) < 0) {
1787 (void) unlink(link
->lldp_file
);
1789 (void) unlink(temp_path
);
1791 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1795 for (i
= 0; i
< n
; i
++)
1796 sd_lldp_neighbor_unref(l
[i
]);
1803 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1804 Link
*link
= userdata
;
1809 (void) link_lldp_save(link
);
1811 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1812 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1814 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1816 r
= link_lldp_emit_start(link
);
1818 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1822 static int link_acquire_ipv6_conf(Link
*link
) {
1827 if (link_ipv6_accept_ra_enabled(link
)) {
1828 assert(link
->ndisc
);
1830 log_link_debug(link
, "Discovering IPv6 routers");
1832 r
= sd_ndisc_start(link
->ndisc
);
1833 if (r
< 0 && r
!= -EBUSY
)
1834 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1837 if (link_radv_enabled(link
)) {
1839 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1841 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1843 r
= sd_radv_start(link
->radv
);
1844 if (r
< 0 && r
!= -EBUSY
)
1845 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1848 (void) dhcp6_request_prefix_delegation(link
);
1853 static int link_acquire_ipv4_conf(Link
*link
) {
1857 assert(link
->network
);
1858 assert(link
->manager
);
1859 assert(link
->manager
->event
);
1861 if (link_ipv4ll_enabled(link
)) {
1862 assert(link
->ipv4ll
);
1864 log_link_debug(link
, "Acquiring IPv4 link-local address");
1866 r
= sd_ipv4ll_start(link
->ipv4ll
);
1868 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1871 if (link_dhcp4_enabled(link
)) {
1872 assert(link
->dhcp_client
);
1874 log_link_debug(link
, "Acquiring DHCPv4 lease");
1876 r
= sd_dhcp_client_start(link
->dhcp_client
);
1878 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1884 static int link_acquire_conf(Link
*link
) {
1889 r
= link_acquire_ipv4_conf(link
);
1893 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1894 r
= link_acquire_ipv6_conf(link
);
1899 if (link_lldp_emit_enabled(link
)) {
1900 r
= link_lldp_emit_start(link
);
1902 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1908 bool link_has_carrier(Link
*link
) {
1909 /* see Documentation/networking/operstates.txt in the kernel sources */
1911 if (link
->kernel_operstate
== IF_OPER_UP
)
1914 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1915 /* operstate may not be implemented, so fall back to flags */
1916 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1922 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1927 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1930 r
= sd_netlink_message_get_errno(m
);
1932 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1937 static int link_configure_addrgen_mode(Link
*link
) {
1938 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1939 uint8_t ipv6ll_mode
;
1943 assert(link
->network
);
1944 assert(link
->manager
);
1945 assert(link
->manager
->rtnl
);
1947 log_link_debug(link
, "Setting address genmode for link");
1949 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1951 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1953 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1955 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1957 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1959 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1961 if (!link_ipv6ll_enabled(link
))
1962 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1963 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1964 /* The file may not exist. And event if it exists, when stable_secret is unset,
1965 * reading the file fails with EIO. */
1966 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1968 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1970 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1972 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1974 r
= sd_netlink_message_close_container(req
);
1976 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1978 r
= sd_netlink_message_close_container(req
);
1980 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1982 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1983 link_netlink_destroy_callback
, link
);
1985 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1992 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1997 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2000 r
= sd_netlink_message_get_errno(m
);
2002 /* we warn but don't fail the link, as it may be brought up later */
2003 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
2008 static int link_up(Link
*link
) {
2009 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2013 assert(link
->network
);
2014 assert(link
->manager
);
2015 assert(link
->manager
->rtnl
);
2017 log_link_debug(link
, "Bringing link up");
2019 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2021 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2023 /* set it free if not enslaved with networkd */
2024 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
2025 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
2027 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
2030 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2032 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2034 if (link
->network
->mac
) {
2035 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
2037 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2040 if (link_ipv6_enabled(link
)) {
2041 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2043 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2045 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2046 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2048 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2050 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2051 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2053 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2056 r
= sd_netlink_message_close_container(req
);
2058 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2060 r
= sd_netlink_message_close_container(req
);
2062 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2065 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2066 link_netlink_destroy_callback
, link
);
2068 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2075 static int link_up_can(Link
*link
) {
2076 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2081 log_link_debug(link
, "Bringing CAN link up");
2083 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2085 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2087 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2089 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2091 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2092 link_netlink_destroy_callback
, link
);
2094 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2101 static int link_set_can(Link
*link
) {
2102 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2106 assert(link
->network
);
2107 assert(link
->manager
);
2108 assert(link
->manager
->rtnl
);
2110 log_link_debug(link
, "link_set_can");
2112 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2114 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2116 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2118 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2120 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2122 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2124 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2126 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2128 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2129 struct can_bittiming bt
= {
2130 .bitrate
= link
->network
->can_bitrate
,
2131 .sample_point
= link
->network
->can_sample_point
,
2134 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2135 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2139 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2140 if (link
->network
->can_sample_point
> 0)
2141 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2143 log_link_debug(link
, "Using default sample point");
2145 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2147 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2150 if (link
->network
->can_restart_us
> 0) {
2151 char time_string
[FORMAT_TIMESPAN_MAX
];
2152 uint64_t restart_ms
;
2154 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2157 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2159 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2161 if (restart_ms
> UINT32_MAX
) {
2162 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2166 log_link_debug(link
, "Setting restart = %s", time_string
);
2168 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2170 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2173 if (link
->network
->can_triple_sampling
>= 0) {
2174 struct can_ctrlmode cm
= {
2175 .mask
= CAN_CTRLMODE_3_SAMPLES
,
2176 .flags
= link
->network
->can_triple_sampling
? CAN_CTRLMODE_3_SAMPLES
: 0,
2179 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
2181 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
2183 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
2186 r
= sd_netlink_message_close_container(m
);
2188 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2190 r
= sd_netlink_message_close_container(m
);
2192 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2194 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2195 link_netlink_destroy_callback
, link
);
2197 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2201 if (!(link
->flags
& IFF_UP
)) {
2202 r
= link_up_can(link
);
2204 link_enter_failed(link
);
2209 log_link_debug(link
, "link_set_can done");
2214 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2219 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2222 r
= sd_netlink_message_get_errno(m
);
2224 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2226 if (streq_ptr(link
->kind
, "can"))
2232 int link_down(Link
*link
) {
2233 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2237 assert(link
->manager
);
2238 assert(link
->manager
->rtnl
);
2240 log_link_debug(link
, "Bringing link down");
2242 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2243 RTM_SETLINK
, link
->ifindex
);
2245 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2247 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2249 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2251 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2252 link_netlink_destroy_callback
, link
);
2254 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2261 static int link_handle_bound_to_list(Link
*link
) {
2265 bool required_up
= false;
2266 bool link_is_up
= false;
2270 if (hashmap_isempty(link
->bound_to_links
))
2273 if (link
->flags
& IFF_UP
)
2276 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2277 if (link_has_carrier(l
)) {
2282 if (!required_up
&& link_is_up
) {
2283 r
= link_down(link
);
2286 } else if (required_up
&& !link_is_up
) {
2295 static int link_handle_bound_by_list(Link
*link
) {
2302 if (hashmap_isempty(link
->bound_by_links
))
2305 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2306 r
= link_handle_bound_to_list(l
);
2314 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2320 if (link
== carrier
)
2323 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2326 r
= hashmap_ensure_allocated(h
, NULL
);
2330 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2337 static int link_new_bound_by_list(Link
*link
) {
2342 bool list_updated
= false;
2345 assert(link
->manager
);
2349 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2350 if (!carrier
->network
)
2353 if (strv_isempty(carrier
->network
->bind_carrier
))
2356 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2357 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2361 list_updated
= true;
2368 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2369 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2373 link_dirty(carrier
);
2379 static int link_new_bound_to_list(Link
*link
) {
2384 bool list_updated
= false;
2387 assert(link
->manager
);
2392 if (strv_isempty(link
->network
->bind_carrier
))
2397 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2398 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2399 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2403 list_updated
= true;
2410 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2411 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2415 link_dirty(carrier
);
2421 static int link_new_carrier_maps(Link
*link
) {
2424 r
= link_new_bound_by_list(link
);
2428 r
= link_handle_bound_by_list(link
);
2432 r
= link_new_bound_to_list(link
);
2436 r
= link_handle_bound_to_list(link
);
2443 static void link_free_bound_to_list(Link
*link
) {
2447 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2448 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2450 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2451 link_dirty(bound_to
);
2457 static void link_free_bound_by_list(Link
*link
) {
2461 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2462 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2464 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2465 link_dirty(bound_by
);
2466 link_handle_bound_to_list(bound_by
);
2473 static void link_free_carrier_maps(Link
*link
) {
2474 bool list_updated
= false;
2478 if (!hashmap_isempty(link
->bound_to_links
)) {
2479 link_free_bound_to_list(link
);
2480 list_updated
= true;
2483 if (!hashmap_isempty(link
->bound_by_links
)) {
2484 link_free_bound_by_list(link
);
2485 list_updated
= true;
2494 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2501 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2505 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2509 r
= set_put(master
->slaves
, link
);
2517 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2522 if (!link
->manager
|| !netdev
)
2525 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2528 link_unref(set_remove(master
->slaves
, link
));
2531 static void link_detach_from_manager(Link
*link
) {
2532 if (!link
|| !link
->manager
)
2535 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2538 /* The following must be called at last. */
2539 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2543 void link_drop(Link
*link
) {
2544 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2547 link_set_state(link
, LINK_STATE_LINGER
);
2549 link_free_carrier_maps(link
);
2551 if (link
->network
) {
2552 link_drop_from_master(link
, link
->network
->bridge
);
2553 link_drop_from_master(link
, link
->network
->bond
);
2556 log_link_debug(link
, "Link removed");
2558 (void) unlink(link
->state_file
);
2559 link_detach_from_manager(link
);
2562 static int link_joined(Link
*link
) {
2566 assert(link
->network
);
2568 if (!hashmap_isempty(link
->bound_to_links
)) {
2569 r
= link_handle_bound_to_list(link
);
2572 } else if (!(link
->flags
& IFF_UP
)) {
2575 link_enter_failed(link
);
2580 if (link
->network
->bridge
) {
2581 r
= link_set_bridge(link
);
2583 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2585 r
= link_append_to_master(link
, link
->network
->bridge
);
2587 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2590 if (link
->network
->bond
) {
2591 r
= link_set_bond(link
);
2593 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2595 r
= link_append_to_master(link
, link
->network
->bond
);
2597 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2600 if (link
->network
->use_br_vlan
&&
2601 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2602 r
= link_set_bridge_vlan(link
);
2604 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2607 /* Skip setting up addresses until it gets carrier,
2608 or it would try to set addresses twice,
2609 which is bad for non-idempotent steps. */
2610 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2613 return link_request_set_addresses(link
);
2616 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2620 assert(link
->network
);
2621 assert(link
->enslaving
> 0);
2622 assert(!link
->enslaved_raw
);
2626 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2629 r
= sd_netlink_message_get_errno(m
);
2630 if (r
< 0 && r
!= -EEXIST
) {
2631 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2632 link_enter_failed(link
);
2635 log_link_debug(link
, "Joined netdev");
2637 if (link
->enslaving
== 0) {
2638 link
->enslaved_raw
= true;
2645 static int link_enter_join_netdev(Link
*link
) {
2651 assert(link
->network
);
2652 assert(link
->state
== LINK_STATE_INITIALIZED
);
2654 link_set_state(link
, LINK_STATE_CONFIGURING
);
2657 link
->enslaving
= 0;
2658 link
->enslaved_raw
= false;
2660 if (link
->network
->bond
) {
2661 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2662 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2663 return link_joined(link
);
2665 log_struct(LOG_DEBUG
,
2666 LOG_LINK_INTERFACE(link
),
2667 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2668 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2672 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2674 log_struct_errno(LOG_WARNING
, r
,
2675 LOG_LINK_INTERFACE(link
),
2676 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2677 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2678 link_enter_failed(link
);
2683 if (link
->network
->bridge
) {
2684 log_struct(LOG_DEBUG
,
2685 LOG_LINK_INTERFACE(link
),
2686 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2687 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2691 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2693 log_struct_errno(LOG_WARNING
, r
,
2694 LOG_LINK_INTERFACE(link
),
2695 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2696 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2697 link_enter_failed(link
);
2702 if (link
->network
->vrf
) {
2703 log_struct(LOG_DEBUG
,
2704 LOG_LINK_INTERFACE(link
),
2705 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2706 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2710 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2712 log_struct_errno(LOG_WARNING
, r
,
2713 LOG_LINK_INTERFACE(link
),
2714 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2715 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2716 link_enter_failed(link
);
2721 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2723 if (netdev
->ifindex
> 0)
2724 /* Assume already enslaved. */
2727 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2730 log_struct(LOG_DEBUG
,
2731 LOG_LINK_INTERFACE(link
),
2732 LOG_NETDEV_INTERFACE(netdev
),
2733 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2737 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2739 log_struct_errno(LOG_WARNING
, r
,
2740 LOG_LINK_INTERFACE(link
),
2741 LOG_NETDEV_INTERFACE(netdev
),
2742 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2743 link_enter_failed(link
);
2748 if (link
->enslaving
== 0)
2749 return link_joined(link
);
2754 static int link_set_ipv4_forward(Link
*link
) {
2757 if (!link_ipv4_forward_enabled(link
))
2760 /* We propagate the forwarding flag from one interface to the
2761 * global setting one way. This means: as long as at least one
2762 * interface was configured at any time that had IP forwarding
2763 * enabled the setting will stay on for good. We do this
2764 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2765 * somewhat in sync (see below). */
2767 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2769 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2774 static int link_set_ipv6_forward(Link
*link
) {
2777 if (!link_ipv6_forward_enabled(link
))
2780 /* On Linux, the IPv6 stack does not know a per-interface
2781 * packet forwarding setting: either packet forwarding is on
2782 * for all, or off for all. We hence don't bother with a
2783 * per-interface setting, but simply propagate the interface
2784 * flag, if it is set, to the global flag, one-way. Note that
2785 * while IPv4 would allow a per-interface flag, we expose the
2786 * same behaviour there and also propagate the setting from
2787 * one to all, to keep things simple (see above). */
2789 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2791 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2796 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2797 IPv6PrivacyExtensions s
;
2800 s
= link_ipv6_privacy_extensions(link
);
2804 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2806 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2811 static int link_set_ipv6_accept_ra(Link
*link
) {
2814 /* Make this a NOP if IPv6 is not available */
2815 if (!socket_ipv6_is_supported())
2818 if (link
->flags
& IFF_LOOPBACK
)
2824 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2826 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2831 static int link_set_ipv6_dad_transmits(Link
*link
) {
2834 /* Make this a NOP if IPv6 is not available */
2835 if (!socket_ipv6_is_supported())
2838 if (link
->flags
& IFF_LOOPBACK
)
2844 if (link
->network
->ipv6_dad_transmits
< 0)
2847 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2849 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2854 static int link_set_ipv6_hop_limit(Link
*link
) {
2857 /* Make this a NOP if IPv6 is not available */
2858 if (!socket_ipv6_is_supported())
2861 if (link
->flags
& IFF_LOOPBACK
)
2867 if (link
->network
->ipv6_hop_limit
< 0)
2870 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2872 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2877 static int link_set_ipv6_mtu(Link
*link
) {
2880 /* Make this a NOP if IPv6 is not available */
2881 if (!socket_ipv6_is_supported())
2884 if (link
->flags
& IFF_LOOPBACK
)
2887 if (link
->network
->ipv6_mtu
== 0)
2890 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2892 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2897 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2898 Address
*net_address
;
2906 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2907 if (address_equal(net_address
, address
))
2913 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2922 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2923 if (route_equal(net_route
, route
))
2929 static int link_drop_foreign_config(Link
*link
) {
2935 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2936 /* we consider IPv6LL addresses to be managed by the kernel */
2937 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2940 if (link_is_static_address_configured(link
, address
)) {
2941 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2943 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2945 r
= address_remove(address
, link
, NULL
);
2951 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2952 /* do not touch routes managed by the kernel */
2953 if (route
->protocol
== RTPROT_KERNEL
)
2956 if (link_is_static_route_configured(link
, route
)) {
2957 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2961 r
= route_remove(route
, link
, NULL
);
2970 static int link_drop_config(Link
*link
) {
2971 Address
*address
, *pool_address
;
2976 SET_FOREACH(address
, link
->addresses
, i
) {
2977 /* we consider IPv6LL addresses to be managed by the kernel */
2978 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2981 r
= address_remove(address
, link
, NULL
);
2985 /* If this address came from an address pool, clean up the pool */
2986 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2987 if (address_equal(address
, pool_address
)) {
2988 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2989 address_free(pool_address
);
2995 SET_FOREACH(route
, link
->routes
, i
) {
2996 /* do not touch routes managed by the kernel */
2997 if (route
->protocol
== RTPROT_KERNEL
)
3000 r
= route_remove(route
, link
, NULL
);
3010 static int link_update_lldp(Link
*link
) {
3018 if (link
->flags
& IFF_UP
) {
3019 r
= sd_lldp_start(link
->lldp
);
3021 log_link_debug(link
, "Started LLDP.");
3023 r
= sd_lldp_stop(link
->lldp
);
3025 log_link_debug(link
, "Stopped LLDP.");
3031 static int link_configure_can(Link
*link
) {
3034 if (streq_ptr(link
->kind
, "can")) {
3035 /* The CAN interface must be down to configure bitrate, etc... */
3036 if ((link
->flags
& IFF_UP
)) {
3037 r
= link_down(link
);
3039 link_enter_failed(link
);
3046 return link_set_can(link
);
3049 if (!(link
->flags
& IFF_UP
)) {
3050 r
= link_up_can(link
);
3052 link_enter_failed(link
);
3060 static int link_configure(Link
*link
) {
3064 assert(link
->network
);
3065 assert(link
->state
== LINK_STATE_INITIALIZED
);
3067 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
3068 return link_configure_can(link
);
3070 /* Drop foreign config, but ignore loopback or critical devices.
3071 * We do not want to remove loopback address or addresses used for root NFS. */
3072 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
3073 r
= link_drop_foreign_config(link
);
3078 r
= link_set_proxy_arp(link
);
3082 r
= ipv6_proxy_ndp_addresses_configure(link
);
3086 r
= link_set_ipv4_forward(link
);
3090 r
= link_set_ipv6_forward(link
);
3094 r
= link_set_ipv6_privacy_extensions(link
);
3098 r
= link_set_ipv6_accept_ra(link
);
3102 r
= link_set_ipv6_dad_transmits(link
);
3106 r
= link_set_ipv6_hop_limit(link
);
3110 r
= link_set_flags(link
);
3114 r
= link_set_ipv6_mtu(link
);
3118 if (link_ipv4ll_enabled(link
)) {
3119 r
= ipv4ll_configure(link
);
3124 if (link_dhcp4_enabled(link
)) {
3125 r
= dhcp4_set_promote_secondaries(link
);
3129 r
= dhcp4_configure(link
);
3134 if (link_dhcp4_server_enabled(link
)) {
3135 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3139 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3144 if (link_dhcp6_enabled(link
) ||
3145 link_ipv6_accept_ra_enabled(link
)) {
3146 r
= dhcp6_configure(link
);
3151 if (link_ipv6_accept_ra_enabled(link
)) {
3152 r
= ndisc_configure(link
);
3157 if (link_radv_enabled(link
)) {
3158 r
= radv_configure(link
);
3163 if (link_lldp_rx_enabled(link
)) {
3164 r
= sd_lldp_new(&link
->lldp
);
3168 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3172 r
= sd_lldp_match_capabilities(link
->lldp
,
3173 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3174 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3175 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3179 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3183 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3187 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3191 r
= link_update_lldp(link
);
3196 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
3200 if (socket_ipv6_is_supported()) {
3201 r
= link_configure_addrgen_mode(link
);
3206 return link_configure_after_setting_mtu(link
);
3209 static int link_configure_after_setting_mtu(Link
*link
) {
3213 assert(link
->network
);
3214 assert(link
->state
== LINK_STATE_INITIALIZED
);
3216 if (link
->setting_mtu
)
3219 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3220 r
= link_acquire_conf(link
);
3225 return link_enter_join_netdev(link
);
3228 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3231 if (duid
->raw_data_len
> 0)
3234 if (duid
->type
!= DUID_TYPE_UUID
)
3237 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3238 duid
->raw_data_len
= sizeof(sd_id128_t
);
3243 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3244 Manager
*manager
= userdata
;
3245 const sd_bus_error
*e
;
3255 e
= sd_bus_message_get_error(m
);
3257 log_error_errno(sd_bus_error_get_errno(e
),
3258 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3263 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3267 if (sz
!= sizeof(sd_id128_t
)) {
3268 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3272 memcpy(&manager
->product_uuid
, a
, sz
);
3273 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3274 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3276 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3279 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3280 r
= link_configure(link
);
3282 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3285 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3287 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3288 * even if the method fails. */
3289 manager
->has_product_uuid
= true;
3294 static bool link_requires_uuid(Link
*link
) {
3298 assert(link
->manager
);
3299 assert(link
->network
);
3301 duid
= link_get_duid(link
);
3302 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3305 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3308 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3314 static int link_configure_duid(Link
*link
) {
3320 assert(link
->manager
);
3321 assert(link
->network
);
3324 duid
= link_get_duid(link
);
3326 if (!link_requires_uuid(link
))
3329 if (m
->has_product_uuid
) {
3330 (void) duid_set_uuid(duid
, m
->product_uuid
);
3334 if (!m
->links_requesting_uuid
) {
3335 r
= manager_request_product_uuid(m
, link
);
3340 log_link_warning_errno(link
, r
,
3341 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3345 r
= set_put(m
->links_requesting_uuid
, link
);
3349 r
= set_put(m
->duids_requesting_uuid
, duid
);
3357 static int link_initialized_and_synced(Link
*link
) {
3362 assert(link
->ifname
);
3363 assert(link
->manager
);
3365 /* We may get called either from the asynchronous netlink callback,
3366 * or directly for link_add() if running in a container. See link_add(). */
3367 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3370 log_link_debug(link
, "Link state is up-to-date");
3371 link_set_state(link
, LINK_STATE_INITIALIZED
);
3373 r
= link_new_bound_by_list(link
);
3377 r
= link_handle_bound_by_list(link
);
3381 if (!link
->network
) {
3382 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3383 &link
->mac
, &network
);
3385 link_enter_unmanaged(link
);
3387 } else if (r
== 0 && network
->unmanaged
) {
3388 link_enter_unmanaged(link
);
3393 if (link
->flags
& IFF_LOOPBACK
) {
3394 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3395 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3397 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3398 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3400 if (network
->dhcp_server
)
3401 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3404 r
= network_apply(network
, link
);
3409 r
= link_new_bound_to_list(link
);
3413 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3414 * link_configure() is called later asynchronously. */
3415 r
= link_configure_duid(link
);
3419 r
= link_configure(link
);
3426 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3427 (void) link_initialized_and_synced(link
);
3431 int link_initialized(Link
*link
, sd_device
*device
) {
3432 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3436 assert(link
->manager
);
3437 assert(link
->manager
->rtnl
);
3440 if (link
->state
!= LINK_STATE_PENDING
)
3443 if (link
->sd_device
)
3446 log_link_debug(link
, "udev initialized link");
3447 link_set_state(link
, LINK_STATE_INITIALIZED
);
3449 link
->sd_device
= sd_device_ref(device
);
3451 /* udev has initialized the link, but we don't know if we have yet
3452 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3453 * when it returns we know that the pending NEWLINKs have already been
3454 * processed and that we are up-to-date */
3456 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3461 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3462 link_netlink_destroy_callback
, link
);
3471 static int link_load(Link
*link
) {
3472 _cleanup_free_
char *network_file
= NULL
,
3475 *dhcp4_address
= NULL
,
3476 *ipv4ll_address
= NULL
;
3477 union in_addr_union address
;
3478 union in_addr_union route_dst
;
3484 r
= parse_env_file(NULL
, link
->state_file
,
3485 "NETWORK_FILE", &network_file
,
3486 "ADDRESSES", &addresses
,
3488 "DHCP4_ADDRESS", &dhcp4_address
,
3489 "IPV4LL_ADDRESS", &ipv4ll_address
);
3490 if (r
< 0 && r
!= -ENOENT
)
3491 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3498 suffix
= strrchr(network_file
, '.');
3500 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3501 goto network_file_fail
;
3505 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3507 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3508 goto network_file_fail
;
3511 r
= network_apply(network
, link
);
3513 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3522 _cleanup_free_
char *address_str
= NULL
;
3523 char *prefixlen_str
;
3525 unsigned char prefixlen
;
3527 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3529 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3535 prefixlen_str
= strchr(address_str
, '/');
3536 if (!prefixlen_str
) {
3537 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3541 *prefixlen_str
++ = '\0';
3543 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3545 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3549 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3551 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3555 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3557 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3566 _cleanup_free_
char *route_str
= NULL
;
3567 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3569 char *prefixlen_str
;
3571 unsigned char prefixlen
, tos
, table
;
3574 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3576 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3582 prefixlen_str
= strchr(route_str
, '/');
3583 if (!prefixlen_str
) {
3584 log_link_debug(link
, "Failed to parse route %s", route_str
);
3588 *prefixlen_str
++ = '\0';
3590 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3592 log_link_debug(link
,
3593 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3598 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3600 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3604 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3606 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3608 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3609 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3610 0, route_expire_handler
, route
);
3612 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3615 route
->lifetime
= lifetime
;
3616 sd_event_source_unref(route
->expire
);
3617 route
->expire
= TAKE_PTR(expire
);
3621 if (dhcp4_address
) {
3622 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3624 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3625 goto dhcp4_address_fail
;
3628 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3630 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3632 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3634 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3639 if (ipv4ll_address
) {
3640 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3642 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3643 goto ipv4ll_address_fail
;
3646 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3648 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3650 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3652 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3655 ipv4ll_address_fail
:
3660 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3661 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3662 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3671 r
= link_new(m
, message
, ret
);
3677 log_link_debug(link
, "Link %d added", link
->ifindex
);
3679 r
= link_load(link
);
3683 if (detect_container() <= 0) {
3684 /* not in a container, udev will be around */
3685 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3686 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3688 log_link_warning_errno(link
, r
, "Could not find device: %m");
3692 r
= sd_device_get_is_initialized(device
);
3694 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3699 log_link_debug(link
, "link pending udev initialization...");
3703 r
= device_is_renaming(device
);
3705 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3709 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3713 r
= link_initialized(link
, device
);
3717 r
= link_initialized_and_synced(link
);
3724 link_enter_failed(link
);
3728 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3733 log_link_info(link
, "Gained IPv6LL");
3735 link
->ipv6ll_address
= *address
;
3736 link_check_ready(link
);
3738 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3739 r
= link_acquire_ipv6_conf(link
);
3741 link_enter_failed(link
);
3749 static int link_carrier_gained(Link
*link
) {
3754 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3755 r
= link_acquire_conf(link
);
3757 link_enter_failed(link
);
3761 r
= link_request_set_addresses(link
);
3766 r
= link_handle_bound_by_list(link
);
3773 static int link_carrier_lost(Link
*link
) {
3778 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3781 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3782 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3783 * configuration and stop the clients as well. */
3784 if (link
->setting_mtu
)
3787 r
= link_stop_clients(link
);
3789 link_enter_failed(link
);
3793 if (link_dhcp4_server_enabled(link
))
3794 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3796 r
= link_drop_config(link
);
3800 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3801 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3802 r
= link_drop_foreign_config(link
);
3807 r
= link_handle_bound_by_list(link
);
3814 int link_carrier_reset(Link
*link
) {
3819 if (link_has_carrier(link
)) {
3820 r
= link_carrier_lost(link
);
3824 r
= link_carrier_gained(link
);
3828 log_link_info(link
, "Reset carrier");
3834 int link_update(Link
*link
, sd_netlink_message
*m
) {
3835 struct ether_addr mac
;
3838 bool had_carrier
, carrier_gained
, carrier_lost
;
3842 assert(link
->ifname
);
3845 if (link
->state
== LINK_STATE_LINGER
) {
3846 log_link_info(link
, "Link readded");
3847 link_set_state(link
, LINK_STATE_CONFIGURING
);
3849 r
= link_new_carrier_maps(link
);
3854 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3855 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3856 Manager
*manager
= link
->manager
;
3858 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3861 r
= link_add(manager
, m
, &link
);
3866 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3867 if (r
>= 0 && mtu
> 0) {
3869 if (link
->original_mtu
== 0) {
3870 link
->original_mtu
= mtu
;
3871 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3874 if (link
->dhcp_client
) {
3875 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3878 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3882 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3884 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3888 /* The kernel may broadcast NEWLINK messages without the MAC address
3889 set, simply ignore them. */
3890 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3892 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3895 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3898 log_link_debug(link
, "MAC address: "
3899 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3900 mac
.ether_addr_octet
[0],
3901 mac
.ether_addr_octet
[1],
3902 mac
.ether_addr_octet
[2],
3903 mac
.ether_addr_octet
[3],
3904 mac
.ether_addr_octet
[4],
3905 mac
.ether_addr_octet
[5]);
3908 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3910 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3913 if (link
->dhcp_client
) {
3914 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3915 (const uint8_t *) &link
->mac
,
3919 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3921 r
= dhcp4_set_client_identifier(link
);
3926 if (link
->dhcp6_client
) {
3927 const DUID
* duid
= link_get_duid(link
);
3929 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3930 (const uint8_t *) &link
->mac
,
3934 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3936 if (link
->network
->iaid_set
) {
3937 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3938 link
->network
->iaid
);
3940 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3943 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3945 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3946 duid
->raw_data_len
);
3948 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3952 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3954 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3958 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3960 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3965 had_carrier
= link_has_carrier(link
);
3967 r
= link_update_flags(link
, m
);
3971 r
= link_update_lldp(link
);
3975 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3976 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3978 if (carrier_gained
) {
3979 log_link_info(link
, "Gained carrier");
3981 r
= link_carrier_gained(link
);
3984 } else if (carrier_lost
) {
3985 log_link_info(link
, "Lost carrier");
3987 r
= link_carrier_lost(link
);
3995 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
4003 if (hashmap_isempty(h
))
4007 HASHMAP_FOREACH(link
, h
, i
) {
4011 fprintf(f
, "%i", link
->ifindex
);
4018 int link_save(Link
*link
) {
4019 _cleanup_free_
char *temp_path
= NULL
;
4020 _cleanup_fclose_
FILE *f
= NULL
;
4021 const char *admin_state
, *oper_state
;
4028 assert(link
->state_file
);
4029 assert(link
->lease_file
);
4030 assert(link
->manager
);
4032 if (link
->state
== LINK_STATE_LINGER
) {
4033 (void) unlink(link
->state_file
);
4037 link_lldp_save(link
);
4039 admin_state
= link_state_to_string(link
->state
);
4040 assert(admin_state
);
4042 oper_state
= link_operstate_to_string(link
->operstate
);
4045 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4049 (void) fchmod(fileno(f
), 0644);
4052 "# This is private data. Do not parse.\n"
4055 admin_state
, oper_state
);
4057 if (link
->network
) {
4059 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
4060 const char *dhcp_domainname
= NULL
;
4061 char **dhcp6_domains
= NULL
;
4062 char **dhcp_domains
= NULL
;
4065 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4066 yes_no(link
->network
->required_for_online
));
4068 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
4069 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
4071 if (link
->dhcp6_client
) {
4072 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4073 if (r
< 0 && r
!= -ENOMSG
)
4074 log_link_debug(link
, "No DHCPv6 lease");
4077 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4082 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
4083 _cleanup_free_
char *b
= NULL
;
4085 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
4086 &link
->network
->dns
[j
].address
, &b
);
4088 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4098 if (link
->network
->dhcp_use_dns
&&
4100 const struct in_addr
*addresses
;
4102 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4104 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4108 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4109 struct in6_addr
*in6_addrs
;
4111 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4115 serialize_in6_addrs(f
, in6_addrs
, r
);
4120 /* Make sure to flush out old entries before we use the NDISC data */
4123 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4126 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4130 serialize_in6_addrs(f
, &dd
->address
, 1);
4139 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4141 if (link
->network
->dhcp_use_ntp
&&
4143 const struct in_addr
*addresses
;
4145 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4147 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4151 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4152 struct in6_addr
*in6_addrs
;
4155 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4160 serialize_in6_addrs(f
, in6_addrs
, r
);
4164 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4166 fputstrv(f
, hosts
, NULL
, &space
);
4171 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4172 if (link
->dhcp_lease
) {
4173 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4174 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4177 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4180 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4182 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4185 if (dhcp_domainname
)
4186 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4188 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4190 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4192 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4193 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4198 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4200 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4203 if (dhcp_domainname
)
4204 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4206 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4208 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4210 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4211 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4216 fprintf(f
, "LLMNR=%s\n",
4217 resolve_support_to_string(link
->network
->llmnr
));
4218 fprintf(f
, "MDNS=%s\n",
4219 resolve_support_to_string(link
->network
->mdns
));
4220 if (link
->network
->dns_default_route
>= 0)
4221 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4223 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4224 fprintf(f
, "DNS_OVER_TLS=%s\n",
4225 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4227 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4228 fprintf(f
, "DNSSEC=%s\n",
4229 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4231 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4234 fputs("DNSSEC_NTA=", f
);
4236 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4237 fputs_with_space(f
, n
, NULL
, &space
);
4241 fputs("ADDRESSES=", f
);
4243 SET_FOREACH(a
, link
->addresses
, i
) {
4244 _cleanup_free_
char *address_str
= NULL
;
4246 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4250 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4255 fputs("ROUTES=", f
);
4257 SET_FOREACH(route
, link
->routes
, i
) {
4258 _cleanup_free_
char *route_str
= NULL
;
4260 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4264 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4265 space
? " " : "", route_str
,
4266 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4273 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4274 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4276 if (link
->dhcp_lease
) {
4277 struct in_addr address
;
4278 const char *tz
= NULL
;
4280 assert(link
->network
);
4282 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4284 fprintf(f
, "TIMEZONE=%s\n", tz
);
4286 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4288 fputs("DHCP4_ADDRESS=", f
);
4289 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4293 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4301 (void) unlink(link
->lease_file
);
4304 struct in_addr address
;
4306 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4308 fputs("IPV4LL_ADDRESS=", f
);
4309 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4314 r
= fflush_and_check(f
);
4318 if (rename(temp_path
, link
->state_file
) < 0) {
4326 (void) unlink(link
->state_file
);
4328 (void) unlink(temp_path
);
4330 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4333 /* The serialized state in /run is no longer up-to-date. */
4334 void link_dirty(Link
*link
) {
4339 /* mark manager dirty as link is dirty */
4340 manager_dirty(link
->manager
);
4342 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4344 /* allocation errors are ignored */
4347 r
= set_put(link
->manager
->dirty_links
, link
);
4349 /* don't take another ref if the link was already dirty */
4355 /* The serialized state in /run is up-to-date */
4356 void link_clean(Link
*link
) {
4358 assert(link
->manager
);
4360 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4363 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4364 [LINK_STATE_PENDING
] = "pending",
4365 [LINK_STATE_INITIALIZED
] = "initialized",
4366 [LINK_STATE_CONFIGURING
] = "configuring",
4367 [LINK_STATE_CONFIGURED
] = "configured",
4368 [LINK_STATE_UNMANAGED
] = "unmanaged",
4369 [LINK_STATE_FAILED
] = "failed",
4370 [LINK_STATE_LINGER
] = "linger",
4373 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);