1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6 #include <linux/if_link.h>
9 #include "alloc-util.h"
13 #include "dhcp-identifier.h"
14 #include "dhcp-lease-internal.h"
16 #include "ethtool-util.h"
20 #include "missing_network.h"
21 #include "netlink-util.h"
22 #include "network-internal.h"
23 #include "networkd-address-label.h"
24 #include "networkd-address.h"
25 #include "networkd-can.h"
26 #include "networkd-dhcp-server.h"
27 #include "networkd-dhcp4.h"
28 #include "networkd-dhcp6.h"
29 #include "networkd-fdb.h"
30 #include "networkd-ipv4ll.h"
31 #include "networkd-ipv6-proxy-ndp.h"
32 #include "networkd-link-bus.h"
33 #include "networkd-link.h"
34 #include "networkd-lldp-tx.h"
35 #include "networkd-manager.h"
36 #include "networkd-mdb.h"
37 #include "networkd-ndisc.h"
38 #include "networkd-neighbor.h"
39 #include "networkd-nexthop.h"
40 #include "networkd-sriov.h"
41 #include "networkd-sysctl.h"
42 #include "networkd-radv.h"
43 #include "networkd-routing-policy-rule.h"
44 #include "networkd-wifi.h"
46 #include "socket-util.h"
47 #include "stat-util.h"
48 #include "stdio-util.h"
49 #include "string-table.h"
51 #include "sysctl-util.h"
53 #include "tmpfile-util.h"
54 #include "udev-util.h"
58 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
60 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
62 if (link
->flags
& IFF_LOOPBACK
)
68 if (link
->iftype
== ARPHRD_CAN
)
71 if (STRPTR_IN_SET(link
->kind
,
72 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
73 "vti6", "nlmon", "xfrm", "bareudp"))
76 /* L3 or L3S mode do not support ARP. */
77 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
80 if (link
->network
->bond
)
83 return link
->network
->link_local
& mask
;
86 bool link_ipv6ll_enabled(Link
*link
) {
89 if (!socket_ipv6_is_supported())
92 if (link
->flags
& IFF_LOOPBACK
)
98 if (link
->iftype
== ARPHRD_CAN
)
101 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
104 if (link
->network
->bond
)
107 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
110 bool link_ipv6_enabled(Link
*link
) {
113 if (!socket_ipv6_is_supported())
116 if (link
->network
->bond
)
119 if (link
->iftype
== ARPHRD_CAN
)
122 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
123 if (link_ipv6ll_enabled(link
))
126 if (network_has_static_ipv6_configurations(link
->network
))
132 static bool link_is_enslaved(Link
*link
) {
133 if (link
->flags
& IFF_SLAVE
)
134 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
140 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
143 /* TODO: add conditions for other netdevs. */
148 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
154 if (netdev
->ifindex
<= 0)
157 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
160 link_update_operstate(master
, true);
163 void link_update_operstate(Link
*link
, bool also_update_master
) {
164 LinkOperationalState operstate
;
165 LinkCarrierState carrier_state
;
166 LinkAddressState address_state
;
167 _cleanup_strv_free_
char **p
= NULL
;
168 uint8_t scope
= RT_SCOPE_NOWHERE
;
169 bool changed
= false;
174 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
175 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
176 else if (link_has_carrier(link
)) {
177 if (link_is_enslaved(link
))
178 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
180 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
181 } else if (link
->flags
& IFF_UP
)
182 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
184 carrier_state
= LINK_CARRIER_STATE_OFF
;
186 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
189 SET_FOREACH(slave
, link
->slaves
) {
190 link_update_operstate(slave
, false);
192 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
193 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
197 SET_FOREACH(address
, link
->addresses
) {
198 if (!address_is_ready(address
))
201 if (address
->scope
< scope
)
202 scope
= address
->scope
;
205 /* for operstate we also take foreign addresses into account */
206 SET_FOREACH(address
, link
->addresses_foreign
) {
207 if (!address_is_ready(address
))
210 if (address
->scope
< scope
)
211 scope
= address
->scope
;
214 if (scope
< RT_SCOPE_SITE
)
215 /* universally accessible addresses found */
216 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
217 else if (scope
< RT_SCOPE_HOST
)
218 /* only link or site local addresses found */
219 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
221 /* no useful addresses found */
222 address_state
= LINK_ADDRESS_STATE_OFF
;
224 /* Mapping of address and carrier state vs operational state
226 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
227 * ------------------------------------------------------------------------------
228 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
229 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
230 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
233 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
234 operstate
= (LinkOperationalState
) carrier_state
;
235 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
236 operstate
= LINK_OPERSTATE_ROUTABLE
;
237 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
238 operstate
= LINK_OPERSTATE_DEGRADED
;
240 operstate
= LINK_OPERSTATE_ENSLAVED
;
242 if (link
->carrier_state
!= carrier_state
) {
243 link
->carrier_state
= carrier_state
;
245 if (strv_extend(&p
, "CarrierState") < 0)
249 if (link
->address_state
!= address_state
) {
250 link
->address_state
= address_state
;
252 if (strv_extend(&p
, "AddressState") < 0)
256 if (link
->operstate
!= operstate
) {
257 link
->operstate
= operstate
;
259 if (strv_extend(&p
, "OperationalState") < 0)
264 link_send_changed_strv(link
, p
);
268 if (also_update_master
&& link
->network
) {
269 link_update_master_operstate(link
, link
->network
->bond
);
270 link_update_master_operstate(link
, link
->network
->bridge
);
274 #define FLAG_STRING(string, flag, old, new) \
275 (((old ^ new) & flag) \
276 ? ((old & flag) ? (" -" string) : (" +" string)) \
279 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
280 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
286 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
288 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
290 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
292 /* if we got a message without operstate, take it to mean
293 the state was unchanged */
294 operstate
= link
->kernel_operstate
;
296 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
299 if (link
->flags
!= flags
) {
300 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",
301 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
302 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
303 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
304 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
305 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
306 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
307 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
308 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
309 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
310 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
311 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
312 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
313 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
314 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
315 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
316 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
317 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
318 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
319 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
321 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
322 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
323 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
324 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
325 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
326 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
327 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
328 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
330 /* link flags are currently at most 18 bits, let's align to
332 if (unknown_flags_added
)
334 "Unknown link flags gained: %#.5x (ignoring)",
335 unknown_flags_added
);
337 if (unknown_flags_removed
)
339 "Unknown link flags lost: %#.5x (ignoring)",
340 unknown_flags_removed
);
344 link
->kernel_operstate
= operstate
;
346 link_update_operstate(link
, true);
351 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
352 _cleanup_(link_unrefp
) Link
*link
= NULL
;
353 const char *ifname
, *kind
= NULL
;
354 unsigned short iftype
;
362 /* check for link kind */
363 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
365 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
366 r
= sd_netlink_message_exit_container(message
);
371 r
= sd_netlink_message_get_type(message
, &type
);
374 else if (type
!= RTM_NEWLINK
)
377 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
380 else if (ifindex
<= 0)
383 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
387 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
398 .state
= LINK_STATE_PENDING
,
402 .n_dns
= (unsigned) -1,
403 .dns_default_route
= -1,
404 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
405 .mdns
= _RESOLVE_SUPPORT_INVALID
,
406 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
407 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
410 link
->ifname
= strdup(ifname
);
415 link
->kind
= strdup(kind
);
420 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
422 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
424 r
= netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, &link
->hw_addr
);
426 log_link_debug_errno(link
, r
, "Hardware address not found for new device, continuing without");
428 r
= netlink_message_read_hw_addr(message
, IFLA_BROADCAST
, &link
->bcast_addr
);
430 log_link_debug_errno(link
, r
, "Broadcast address not found for new device, continuing without");
432 r
= ethtool_get_permanent_macaddr(&manager
->ethtool_fd
, link
->ifname
, &link
->permanent_mac
);
434 log_link_debug_errno(link
, r
, "Permanent MAC address not found for new device, continuing without: %m");
436 r
= ethtool_get_driver(&manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
438 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
440 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &link
->alternative_names
);
441 if (r
< 0 && r
!= -ENODATA
)
444 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
447 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
450 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
453 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
457 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
461 r
= link_update_flags(link
, message
, false);
465 *ret
= TAKE_PTR(link
);
470 void link_ntp_settings_clear(Link
*link
) {
471 link
->ntp
= strv_free(link
->ntp
);
474 void link_dns_settings_clear(Link
*link
) {
475 if (link
->n_dns
!= (unsigned) -1)
476 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
477 in_addr_full_free(link
->dns
[i
]);
478 link
->dns
= mfree(link
->dns
);
479 link
->n_dns
= (unsigned) -1;
481 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
482 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
484 link
->dns_default_route
= -1;
485 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
486 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
487 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
488 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
490 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
493 static void link_free_engines(Link
*link
) {
497 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
498 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
499 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
500 link
->dhcp_acd
= sd_ipv4acd_unref(link
->dhcp_acd
);
502 link
->lldp
= sd_lldp_unref(link
->lldp
);
503 link_lldp_emit_stop(link
);
507 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
508 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
509 link
->dhcp6_lease
= sd_dhcp6_lease_unref(link
->dhcp6_lease
);
510 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
511 link
->radv
= sd_radv_unref(link
->radv
);
513 ipv4_dad_unref(link
);
516 static Link
*link_free(Link
*link
) {
519 link_ntp_settings_clear(link
);
520 link_dns_settings_clear(link
);
522 link
->routes
= set_free(link
->routes
);
523 link
->routes_foreign
= set_free(link
->routes_foreign
);
524 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
525 link
->dhcp_routes_old
= set_free(link
->dhcp_routes_old
);
526 link
->dhcp6_routes
= set_free(link
->dhcp6_routes
);
527 link
->dhcp6_routes_old
= set_free(link
->dhcp6_routes_old
);
528 link
->dhcp6_pd_routes
= set_free(link
->dhcp6_pd_routes
);
529 link
->dhcp6_pd_routes_old
= set_free(link
->dhcp6_pd_routes_old
);
530 link
->ndisc_routes
= set_free(link
->ndisc_routes
);
532 link
->nexthops
= set_free(link
->nexthops
);
533 link
->nexthops_foreign
= set_free(link
->nexthops_foreign
);
535 link
->neighbors
= set_free(link
->neighbors
);
536 link
->neighbors_foreign
= set_free(link
->neighbors_foreign
);
538 link
->addresses
= set_free(link
->addresses
);
539 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
540 link
->pool_addresses
= set_free(link
->pool_addresses
);
541 link
->static_addresses
= set_free(link
->static_addresses
);
542 link
->dhcp6_addresses
= set_free(link
->dhcp6_addresses
);
543 link
->dhcp6_addresses_old
= set_free(link
->dhcp6_addresses_old
);
544 link
->dhcp6_pd_addresses
= set_free(link
->dhcp6_pd_addresses
);
545 link
->dhcp6_pd_addresses_old
= set_free(link
->dhcp6_pd_addresses_old
);
546 link
->ndisc_addresses
= set_free(link
->ndisc_addresses
);
548 link_free_engines(link
);
549 free(link
->lease_file
);
550 free(link
->lldp_file
);
553 strv_free(link
->alternative_names
);
558 (void) unlink(link
->state_file
);
559 free(link
->state_file
);
561 sd_device_unref(link
->sd_device
);
563 hashmap_free(link
->bound_to_links
);
564 hashmap_free(link
->bound_by_links
);
566 set_free_with_destructor(link
->slaves
, link_unref
);
568 network_unref(link
->network
);
573 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
575 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
582 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
591 void link_set_state(Link
*link
, LinkState state
) {
594 if (link
->state
== state
)
597 log_link_debug(link
, "State changed: %s -> %s",
598 link_state_to_string(link
->state
),
599 link_state_to_string(state
));
603 link_send_changed(link
, "AdministrativeState", NULL
);
606 static void link_enter_unmanaged(Link
*link
) {
609 link_set_state(link
, LINK_STATE_UNMANAGED
);
614 int link_stop_engines(Link
*link
, bool may_keep_dhcp
) {
618 assert(link
->manager
);
619 assert(link
->manager
->event
);
621 bool keep_dhcp
= may_keep_dhcp
&&
623 (link
->manager
->restarting
||
624 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
627 k
= sd_dhcp_client_stop(link
->dhcp_client
);
629 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
632 k
= sd_ipv4acd_stop(link
->dhcp_acd
);
634 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client for DHCPv4: %m");
636 k
= sd_dhcp_server_stop(link
->dhcp_server
);
638 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 server: %m");
640 k
= sd_lldp_stop(link
->lldp
);
642 r
= log_link_warning_errno(link
, k
, "Could not stop LLDP: %m");
644 k
= sd_ipv4ll_stop(link
->ipv4ll
);
646 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
648 k
= ipv4_dad_stop(link
);
650 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
652 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
654 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
656 k
= dhcp6_pd_remove(link
);
658 r
= log_link_warning_errno(link
, k
, "Could not remove DHCPv6 PD addresses and routes: %m");
660 k
= sd_ndisc_stop(link
->ndisc
);
662 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
664 k
= sd_radv_stop(link
->radv
);
666 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
668 link_lldp_emit_stop(link
);
672 void link_enter_failed(Link
*link
) {
675 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
678 log_link_warning(link
, "Failed");
680 link_set_state(link
, LINK_STATE_FAILED
);
682 (void) link_stop_engines(link
, false);
687 static int link_join_netdevs_after_configured(Link
*link
) {
691 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
692 if (netdev
->ifindex
> 0)
693 /* Assume already enslaved. */
696 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
699 log_struct(LOG_DEBUG
,
700 LOG_LINK_INTERFACE(link
),
701 LOG_NETDEV_INTERFACE(netdev
),
702 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
704 r
= netdev_join(netdev
, link
, NULL
);
706 return log_struct_errno(LOG_WARNING
, r
,
707 LOG_LINK_INTERFACE(link
),
708 LOG_NETDEV_INTERFACE(netdev
),
709 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
715 static void link_enter_configured(Link
*link
) {
717 assert(link
->network
);
719 if (link
->state
!= LINK_STATE_CONFIGURING
)
722 link_set_state(link
, LINK_STATE_CONFIGURED
);
724 (void) link_join_netdevs_after_configured(link
);
729 void link_check_ready(Link
*link
) {
734 if (link
->state
== LINK_STATE_CONFIGURED
)
737 if (link
->state
!= LINK_STATE_CONFIGURING
) {
738 log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
745 if (!link
->addresses_configured
) {
746 log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
750 if (!link
->neighbors_configured
) {
751 log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
755 SET_FOREACH(a
, link
->addresses
)
756 if (!address_is_ready(a
)) {
757 _cleanup_free_
char *str
= NULL
;
759 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
760 log_link_debug(link
, "%s(): an address %s/%d is not ready.", __func__
, strnull(str
), a
->prefixlen
);
764 if (!link
->static_routes_configured
) {
765 log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
769 if (!link
->static_nexthops_configured
) {
770 log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
774 if (!link
->routing_policy_rules_configured
) {
775 log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
779 if (!link
->tc_configured
) {
780 log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
784 if (!link
->sr_iov_configured
) {
785 log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
789 if (!link
->bridge_mdb_configured
) {
790 log_link_debug(link
, "%s(): Bridge MDB is not configured.", __func__
);
794 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
795 bool has_ndisc_address
= false;
798 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !link
->ipv4ll_address_configured
) {
799 log_link_debug(link
, "%s(): IPv4LL is not configured.", __func__
);
803 if (link_ipv6ll_enabled(link
) &&
804 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
805 log_link_debug(link
, "%s(): IPv6LL is not configured.", __func__
);
809 SET_FOREACH(n
, link
->ndisc_addresses
)
811 has_ndisc_address
= true;
815 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
816 !link
->dhcp_address
&& set_isempty(link
->dhcp6_addresses
) && !has_ndisc_address
&&
817 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
818 log_link_debug(link
, "%s(): DHCP4 or DHCP6 is enabled but no dynamic address is assigned yet.", __func__
);
822 if (link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
) || link_dhcp6_pd_is_enabled(link
) || link_ipv6_accept_ra_enabled(link
)) {
823 if (!link
->dhcp4_configured
&&
824 !(link
->dhcp6_address_configured
&& link
->dhcp6_route_configured
) &&
825 !(link
->dhcp6_pd_address_configured
&& link
->dhcp6_pd_route_configured
) &&
826 !(link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
) &&
827 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
828 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
829 * an IPv4ll fallback address must be configured. */
830 log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
834 log_link_debug(link
, "%s(): dhcp4:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc_addresses:%s ndisc_routes:%s",
836 yes_no(link
->dhcp4_configured
),
837 yes_no(link
->dhcp6_address_configured
),
838 yes_no(link
->dhcp6_route_configured
),
839 yes_no(link
->dhcp6_pd_address_configured
),
840 yes_no(link
->dhcp6_pd_route_configured
),
841 yes_no(link
->ndisc_addresses_configured
),
842 yes_no(link
->ndisc_routes_configured
));
846 link_enter_configured(link
);
851 static int link_set_static_configs(Link
*link
) {
855 assert(link
->network
);
856 assert(link
->state
!= _LINK_STATE_INVALID
);
858 /* Reset all *_configured flags we are configuring. */
859 link
->request_static_addresses
= false;
860 link
->addresses_configured
= false;
861 link
->addresses_ready
= false;
862 link
->neighbors_configured
= false;
863 link
->static_routes_configured
= false;
864 link
->static_nexthops_configured
= false;
865 link
->routing_policy_rules_configured
= false;
867 r
= link_set_bridge_fdb(link
);
871 r
= link_set_bridge_mdb(link
);
875 r
= link_set_neighbors(link
);
879 r
= link_set_addresses(link
);
883 r
= link_set_address_labels(link
);
887 /* now that we can figure out a default address for the dhcp server, start it */
888 r
= dhcp4_server_configure(link
);
895 static int link_configure_continue(Link
*link
);
897 static int link_mac_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
902 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
905 r
= sd_netlink_message_get_errno(m
);
907 log_link_message_warning_errno(link
, m
, r
, "Could not set MAC address, ignoring");
909 log_link_debug(link
, "Setting MAC address done.");
914 static int link_set_mac(Link
*link
) {
915 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
919 assert(link
->network
);
920 assert(link
->manager
);
921 assert(link
->manager
->rtnl
);
923 if (!link
->network
->mac
)
926 log_link_debug(link
, "Setting MAC address");
928 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
930 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
932 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
934 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
936 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_mac_handler
,
937 link_netlink_destroy_callback
, link
);
939 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
946 static int link_nomaster_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
951 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
954 r
= sd_netlink_message_get_errno(m
);
956 log_link_message_warning_errno(link
, m
, r
, "Could not set nomaster, ignoring");
958 log_link_debug(link
, "Setting nomaster done.");
963 static int link_set_nomaster(Link
*link
) {
964 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
968 assert(link
->network
);
969 assert(link
->manager
);
970 assert(link
->manager
->rtnl
);
972 /* set it free if not enslaved with networkd */
973 if (link
->network
->bridge
|| link
->network
->bond
|| link
->network
->vrf
)
976 log_link_debug(link
, "Setting nomaster");
978 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
980 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
982 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
984 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
986 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_nomaster_handler
,
987 link_netlink_destroy_callback
, link
);
989 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
996 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1001 assert(link
->ifname
);
1003 link
->setting_mtu
= false;
1005 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1008 r
= sd_netlink_message_get_errno(m
);
1010 log_link_message_warning_errno(link
, m
, r
, "Could not set MTU, ignoring");
1012 log_link_debug(link
, "Setting MTU done.");
1014 if (link
->state
== LINK_STATE_INITIALIZED
) {
1015 r
= link_configure_continue(link
);
1017 link_enter_failed(link
);
1023 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1024 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1028 assert(link
->manager
);
1029 assert(link
->manager
->rtnl
);
1031 if (mtu
== 0 || link
->setting_mtu
)
1034 if (link
->mtu
== mtu
)
1037 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1039 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1041 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1043 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1044 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1045 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1047 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1048 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes");
1053 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1055 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1057 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1058 link_netlink_destroy_callback
, link
);
1060 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1063 link
->setting_mtu
= true;
1068 static bool link_reduces_vlan_mtu(Link
*link
) {
1069 /* See netif_reduces_vlan_mtu() in kernel. */
1070 return streq_ptr(link
->kind
, "macsec");
1073 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1077 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
)
1078 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1079 /* See vlan_dev_change_mtu() in kernel. */
1080 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1082 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1083 /* See macvlan_change_mtu() in kernel. */
1089 static int link_configure_mtu(Link
*link
) {
1093 assert(link
->network
);
1095 if (link
->network
->mtu
> 0)
1096 return link_set_mtu(link
, link
->network
->mtu
);
1098 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1099 if (link
->mtu
>= mtu
)
1102 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1103 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1106 return link_set_mtu(link
, mtu
);
1109 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1114 assert(link
->ifname
);
1116 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1119 r
= sd_netlink_message_get_errno(m
);
1121 log_link_message_warning_errno(link
, m
, r
, "Could not set link flags, ignoring");
1126 static int link_set_flags(Link
*link
) {
1127 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1128 unsigned ifi_change
= 0;
1129 unsigned ifi_flags
= 0;
1133 assert(link
->manager
);
1134 assert(link
->manager
->rtnl
);
1136 if (link
->flags
& IFF_LOOPBACK
)
1142 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1145 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1147 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1149 if (link
->network
->arp
>= 0) {
1150 ifi_change
|= IFF_NOARP
;
1151 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1154 if (link
->network
->multicast
>= 0) {
1155 ifi_change
|= IFF_MULTICAST
;
1156 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1159 if (link
->network
->allmulticast
>= 0) {
1160 ifi_change
|= IFF_ALLMULTI
;
1161 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1164 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1166 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1168 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1169 link_netlink_destroy_callback
, link
);
1171 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1178 static int link_acquire_ipv6_conf(Link
*link
) {
1184 log_link_debug(link
, "Discovering IPv6 routers");
1186 r
= sd_ndisc_start(link
->ndisc
);
1187 if (r
< 0 && r
!= -EBUSY
)
1188 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1193 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1195 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1197 r
= radv_emit_dns(link
);
1199 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1201 r
= sd_radv_start(link
->radv
);
1202 if (r
< 0 && r
!= -EBUSY
)
1203 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1206 if (link_dhcp6_enabled(link
) && IN_SET(link
->network
->dhcp6_without_ra
,
1207 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
,
1208 DHCP6_CLIENT_START_MODE_SOLICIT
)) {
1209 assert(link
->dhcp6_client
);
1210 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1212 r
= dhcp6_request_address(link
, link
->network
->dhcp6_without_ra
== DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
);
1213 if (r
< 0 && r
!= -EBUSY
)
1214 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1216 log_link_debug(link
, "Acquiring DHCPv6 lease");
1219 r
= dhcp6_request_prefix_delegation(link
);
1221 return log_link_warning_errno(link
, r
, "Failed to request DHCPv6 prefix delegation: %m");
1226 static int link_acquire_ipv4_conf(Link
*link
) {
1230 assert(link
->manager
);
1231 assert(link
->manager
->event
);
1233 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1234 assert(link
->ipv4ll
);
1236 log_link_debug(link
, "Acquiring IPv4 link-local address");
1238 r
= sd_ipv4ll_start(link
->ipv4ll
);
1240 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1243 if (link
->dhcp_client
) {
1244 log_link_debug(link
, "Acquiring DHCPv4 lease");
1246 r
= sd_dhcp_client_start(link
->dhcp_client
);
1248 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1254 static int link_acquire_conf(Link
*link
) {
1259 r
= link_acquire_ipv4_conf(link
);
1263 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1264 r
= link_acquire_ipv6_conf(link
);
1269 r
= link_lldp_emit_start(link
);
1271 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1276 bool link_has_carrier(Link
*link
) {
1277 /* see Documentation/networking/operstates.txt in the kernel sources */
1279 if (link
->kernel_operstate
== IF_OPER_UP
)
1282 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1283 /* operstate may not be implemented, so fall back to flags */
1284 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1285 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1291 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1296 link
->setting_genmode
= false;
1298 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1301 r
= sd_netlink_message_get_errno(m
);
1303 log_link_message_warning_errno(link
, m
, r
, "Could not set address genmode for interface, ignoring");
1305 log_link_debug(link
, "Setting address genmode done.");
1307 if (link
->state
== LINK_STATE_INITIALIZED
) {
1308 r
= link_configure_continue(link
);
1310 link_enter_failed(link
);
1316 static int link_configure_addrgen_mode(Link
*link
) {
1317 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1318 uint8_t ipv6ll_mode
;
1322 assert(link
->network
);
1323 assert(link
->manager
);
1324 assert(link
->manager
->rtnl
);
1326 if (!socket_ipv6_is_supported() || link
->setting_genmode
)
1329 log_link_debug(link
, "Setting address genmode for link");
1331 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1333 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1335 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1337 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1339 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1341 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1343 if (!link_ipv6ll_enabled(link
))
1344 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1345 else if (link
->network
->ipv6ll_address_gen_mode
< 0) {
1346 r
= sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
);
1348 /* The file may not exist. And even if it exists, when stable_secret is unset,
1349 * reading the file fails with EIO. */
1350 log_link_debug_errno(link
, r
, "Failed to read sysctl property stable_secret: %m");
1352 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1354 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1356 ipv6ll_mode
= link
->network
->ipv6ll_address_gen_mode
;
1358 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1360 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1362 r
= sd_netlink_message_close_container(req
);
1364 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1366 r
= sd_netlink_message_close_container(req
);
1368 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1370 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1371 link_netlink_destroy_callback
, link
);
1373 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1376 link
->setting_genmode
= true;
1381 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1386 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1389 r
= sd_netlink_message_get_errno(m
);
1391 /* we warn but don't fail the link, as it may be brought up later */
1392 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
1397 static int link_up(Link
*link
) {
1398 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1402 assert(link
->network
);
1403 assert(link
->manager
);
1404 assert(link
->manager
->rtnl
);
1406 log_link_debug(link
, "Bringing link up");
1408 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1410 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1412 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1414 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1416 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1417 link_netlink_destroy_callback
, link
);
1419 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1426 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1431 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1434 r
= sd_netlink_message_get_errno(m
);
1436 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
1441 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1442 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1446 assert(link
->manager
);
1447 assert(link
->manager
->rtnl
);
1449 log_link_debug(link
, "Bringing link down");
1451 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1452 RTM_SETLINK
, link
->ifindex
);
1454 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1456 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1458 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1460 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1461 callback
?: link_down_handler
,
1462 link_netlink_destroy_callback
, link
);
1464 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1471 static int link_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1476 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1479 r
= sd_netlink_message_get_errno(m
);
1481 log_link_message_warning_errno(link
, m
, r
, "Could not set group for the interface");
1486 static int link_set_group(Link
*link
) {
1487 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1491 assert(link
->network
);
1492 assert(link
->manager
);
1493 assert(link
->manager
->rtnl
);
1495 if (link
->network
->group
<= 0)
1498 log_link_debug(link
, "Setting group");
1500 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1502 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1504 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
1506 return log_link_error_errno(link
, r
, "Could not set link group: %m");
1508 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_group_handler
,
1509 link_netlink_destroy_callback
, link
);
1511 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1518 static int link_handle_bound_to_list(Link
*link
) {
1521 bool required_up
= false;
1522 bool link_is_up
= false;
1526 if (hashmap_isempty(link
->bound_to_links
))
1529 if (link
->flags
& IFF_UP
)
1532 HASHMAP_FOREACH (l
, link
->bound_to_links
)
1533 if (link_has_carrier(l
)) {
1538 if (!required_up
&& link_is_up
) {
1539 r
= link_down(link
, NULL
);
1542 } else if (required_up
&& !link_is_up
) {
1551 static int link_handle_bound_by_list(Link
*link
) {
1557 if (hashmap_isempty(link
->bound_by_links
))
1560 HASHMAP_FOREACH (l
, link
->bound_by_links
) {
1561 r
= link_handle_bound_to_list(l
);
1569 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1575 if (link
== carrier
)
1578 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1581 r
= hashmap_ensure_allocated(h
, NULL
);
1585 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1592 static int link_new_bound_by_list(Link
*link
) {
1596 bool list_updated
= false;
1599 assert(link
->manager
);
1603 HASHMAP_FOREACH(carrier
, m
->links
) {
1604 if (!carrier
->network
)
1607 if (strv_isempty(carrier
->network
->bind_carrier
))
1610 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
1611 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1615 list_updated
= true;
1622 HASHMAP_FOREACH(carrier
, link
->bound_by_links
) {
1623 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1627 link_dirty(carrier
);
1633 static int link_new_bound_to_list(Link
*link
) {
1637 bool list_updated
= false;
1640 assert(link
->manager
);
1645 if (strv_isempty(link
->network
->bind_carrier
))
1650 HASHMAP_FOREACH (carrier
, m
->links
) {
1651 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
1652 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1656 list_updated
= true;
1663 HASHMAP_FOREACH (carrier
, link
->bound_to_links
) {
1664 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1668 link_dirty(carrier
);
1674 static int link_new_carrier_maps(Link
*link
) {
1677 r
= link_new_bound_by_list(link
);
1681 r
= link_handle_bound_by_list(link
);
1685 r
= link_new_bound_to_list(link
);
1689 r
= link_handle_bound_to_list(link
);
1696 static void link_free_bound_to_list(Link
*link
) {
1699 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
) {
1700 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1702 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1703 link_dirty(bound_to
);
1709 static void link_free_bound_by_list(Link
*link
) {
1712 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
) {
1713 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1715 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1716 link_dirty(bound_by
);
1717 link_handle_bound_to_list(bound_by
);
1724 static void link_free_carrier_maps(Link
*link
) {
1725 bool list_updated
= false;
1729 if (!hashmap_isempty(link
->bound_to_links
)) {
1730 link_free_bound_to_list(link
);
1731 list_updated
= true;
1734 if (!hashmap_isempty(link
->bound_by_links
)) {
1735 link_free_bound_by_list(link
);
1736 list_updated
= true;
1745 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1752 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1756 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
1764 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
1769 if (!link
->manager
|| !netdev
)
1772 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
1775 link_unref(set_remove(master
->slaves
, link
));
1778 static void link_detach_from_manager(Link
*link
) {
1779 if (!link
|| !link
->manager
)
1782 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1785 /* The following must be called at last. */
1786 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
1790 void link_drop(Link
*link
) {
1791 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1794 link_set_state(link
, LINK_STATE_LINGER
);
1796 link_free_carrier_maps(link
);
1798 if (link
->network
) {
1799 link_drop_from_master(link
, link
->network
->bridge
);
1800 link_drop_from_master(link
, link
->network
->bond
);
1803 log_link_debug(link
, "Link removed");
1805 (void) unlink(link
->state_file
);
1806 link_detach_from_manager(link
);
1809 static int link_joined(Link
*link
) {
1813 assert(link
->network
);
1815 if (!hashmap_isempty(link
->bound_to_links
)) {
1816 r
= link_handle_bound_to_list(link
);
1819 } else if (!(link
->flags
& IFF_UP
)) {
1822 link_enter_failed(link
);
1827 if (link
->network
->bridge
) {
1828 r
= link_set_bridge(link
);
1830 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1832 r
= link_append_to_master(link
, link
->network
->bridge
);
1834 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
1837 if (link
->network
->bond
) {
1838 r
= link_set_bond(link
);
1840 log_link_error_errno(link
, r
, "Could not set bond message: %m");
1842 r
= link_append_to_master(link
, link
->network
->bond
);
1844 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
1847 r
= link_set_bridge_vlan(link
);
1849 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
1851 /* Skip setting up addresses until it gets carrier,
1852 or it would try to set addresses twice,
1853 which is bad for non-idempotent steps. */
1854 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
1857 link_set_state(link
, LINK_STATE_CONFIGURING
);
1859 r
= link_acquire_conf(link
);
1863 return link_set_static_configs(link
);
1866 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1870 assert(link
->network
);
1871 assert(link
->enslaving
> 0);
1875 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1878 r
= sd_netlink_message_get_errno(m
);
1879 if (r
< 0 && r
!= -EEXIST
) {
1880 log_link_message_warning_errno(link
, m
, r
, "Could not join netdev");
1881 link_enter_failed(link
);
1885 log_link_debug(link
, "Joined netdev");
1887 if (link
->enslaving
== 0) {
1888 r
= link_joined(link
);
1890 link_enter_failed(link
);
1896 static int link_enter_join_netdev(Link
*link
) {
1901 assert(link
->network
);
1902 assert(link
->state
== LINK_STATE_INITIALIZED
);
1904 link_set_state(link
, LINK_STATE_CONFIGURING
);
1907 link
->enslaving
= 0;
1909 if (link
->network
->bond
) {
1910 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
1911 link
->network
->bond
->ifindex
== link
->master_ifindex
)
1912 return link_joined(link
);
1914 log_struct(LOG_DEBUG
,
1915 LOG_LINK_INTERFACE(link
),
1916 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1917 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
1921 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1923 log_struct_errno(LOG_WARNING
, r
,
1924 LOG_LINK_INTERFACE(link
),
1925 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1926 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
1927 link_enter_failed(link
);
1932 if (link
->network
->bridge
) {
1933 log_struct(LOG_DEBUG
,
1934 LOG_LINK_INTERFACE(link
),
1935 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1936 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
1940 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1942 log_struct_errno(LOG_WARNING
, r
,
1943 LOG_LINK_INTERFACE(link
),
1944 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1945 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
1946 link_enter_failed(link
);
1951 if (link
->network
->vrf
) {
1952 log_struct(LOG_DEBUG
,
1953 LOG_LINK_INTERFACE(link
),
1954 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
1955 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
1959 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
1961 log_struct_errno(LOG_WARNING
, r
,
1962 LOG_LINK_INTERFACE(link
),
1963 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
1964 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
1965 link_enter_failed(link
);
1970 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
1972 if (netdev
->ifindex
> 0)
1973 /* Assume already enslaved. */
1976 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
1979 log_struct(LOG_DEBUG
,
1980 LOG_LINK_INTERFACE(link
),
1981 LOG_NETDEV_INTERFACE(netdev
),
1982 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
1986 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1988 log_struct_errno(LOG_WARNING
, r
,
1989 LOG_LINK_INTERFACE(link
),
1990 LOG_NETDEV_INTERFACE(netdev
),
1991 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
1992 link_enter_failed(link
);
1997 if (link
->enslaving
== 0)
1998 return link_joined(link
);
2003 static int link_drop_foreign_config(Link
*link
) {
2006 r
= link_drop_foreign_addresses(link
);
2010 r
= link_drop_foreign_neighbors(link
);
2014 return link_drop_foreign_routes(link
);
2017 static int link_drop_config(Link
*link
) {
2020 r
= link_drop_addresses(link
);
2024 r
= link_drop_neighbors(link
);
2028 r
= link_drop_routes(link
);
2037 int link_configure(Link
*link
) {
2041 assert(link
->network
);
2042 assert(link
->state
== LINK_STATE_INITIALIZED
);
2044 r
= link_configure_traffic_control(link
);
2048 r
= link_configure_sr_iov(link
);
2052 if (link
->iftype
== ARPHRD_CAN
)
2053 return link_configure_can(link
);
2055 r
= link_set_sysctl(link
);
2059 r
= link_set_ipv6_proxy_ndp_addresses(link
);
2063 r
= link_set_mac(link
);
2067 r
= link_set_nomaster(link
);
2071 r
= link_set_flags(link
);
2075 r
= link_set_group(link
);
2079 r
= ipv4ll_configure(link
);
2083 r
= dhcp4_configure(link
);
2087 r
= dhcp6_configure(link
);
2091 r
= ndisc_configure(link
);
2095 r
= radv_configure(link
);
2099 r
= link_lldp_rx_configure(link
);
2103 r
= link_configure_mtu(link
);
2107 r
= link_configure_addrgen_mode(link
);
2111 return link_configure_continue(link
);
2114 /* The configuration continues in this separate function, instead of
2115 * including this in the above link_configure() function, for two
2117 * 1) some devices reset the link when the mtu is set, which caused
2118 * an infinite loop here in networkd; see:
2119 * https://github.com/systemd/systemd/issues/6593
2120 * https://github.com/systemd/systemd/issues/9831
2121 * 2) if ipv6ll is disabled, then bringing the interface up must be
2122 * delayed until after we get confirmation from the kernel that
2123 * the addr_gen_mode parameter has been set (via netlink), see:
2124 * https://github.com/systemd/systemd/issues/13882
2126 static int link_configure_continue(Link
*link
) {
2130 assert(link
->network
);
2131 assert(link
->state
== LINK_STATE_INITIALIZED
);
2133 if (link
->setting_mtu
|| link
->setting_genmode
)
2136 /* Drop foreign config, but ignore loopback or critical devices.
2137 * We do not want to remove loopback address or addresses used for root NFS. */
2138 if (!(link
->flags
& IFF_LOOPBACK
) &&
2139 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2140 r
= link_drop_foreign_config(link
);
2145 /* The kernel resets ipv6 mtu after changing device mtu;
2146 * we must set this here, after we've set device mtu */
2147 r
= link_set_ipv6_mtu(link
);
2149 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface, ignoring: %m");
2151 return link_enter_join_netdev(link
);
2154 static int link_reconfigure_internal(Link
*link
, sd_netlink_message
*m
, bool force
) {
2159 _cleanup_strv_free_
char **s
= NULL
;
2161 r
= sd_netlink_message_get_errno(m
);
2165 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
2166 if (r
< 0 && r
!= -ENODATA
)
2169 strv_free_and_replace(link
->alternative_names
, s
);
2172 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
2173 link
->ifname
, link
->alternative_names
, link
->driver
,
2174 &link
->hw_addr
.addr
.ether
, &link
->permanent_mac
,
2175 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
2177 link_enter_unmanaged(link
);
2179 } else if (r
== 0 && network
->unmanaged
) {
2180 link_enter_unmanaged(link
);
2185 if (link
->network
== network
&& !force
)
2188 log_link_info(link
, "Re-configuring with %s", network
->filename
);
2190 /* Dropping old .network file */
2191 r
= link_stop_engines(link
, false);
2195 r
= link_drop_config(link
);
2199 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
2200 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2201 r
= link_drop_foreign_config(link
);
2206 link_free_carrier_maps(link
);
2207 link_free_engines(link
);
2208 link
->network
= network_unref(link
->network
);
2210 /* Then, apply new .network file */
2211 r
= network_apply(network
, link
);
2215 r
= link_new_carrier_maps(link
);
2219 link_set_state(link
, LINK_STATE_INITIALIZED
);
2222 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2223 * link_configure() is called later asynchronously. */
2224 r
= link_configure_duid(link
);
2228 r
= link_configure(link
);
2235 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2238 r
= link_reconfigure_internal(link
, m
, false);
2240 link_enter_failed(link
);
2245 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2248 r
= link_reconfigure_internal(link
, m
, true);
2250 link_enter_failed(link
);
2255 int link_reconfigure(Link
*link
, bool force
) {
2256 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2259 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
2262 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2267 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
2268 force
? link_force_reconfigure_handler
: link_reconfigure_handler
,
2269 link_netlink_destroy_callback
, link
);
2278 static int link_initialized_and_synced(Link
*link
) {
2283 assert(link
->ifname
);
2284 assert(link
->manager
);
2286 /* We may get called either from the asynchronous netlink callback,
2287 * or directly for link_add() if running in a container. See link_add(). */
2288 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
2291 log_link_debug(link
, "Link state is up-to-date");
2292 link_set_state(link
, LINK_STATE_INITIALIZED
);
2294 r
= link_new_bound_by_list(link
);
2298 r
= link_handle_bound_by_list(link
);
2302 if (!link
->network
) {
2303 r
= wifi_get_info(link
);
2307 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
2308 link
->ifname
, link
->alternative_names
, link
->driver
,
2309 &link
->hw_addr
.addr
.ether
, &link
->permanent_mac
,
2310 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
2312 link_enter_unmanaged(link
);
2314 } else if (r
== 0 && network
->unmanaged
) {
2315 link_enter_unmanaged(link
);
2320 if (link
->flags
& IFF_LOOPBACK
) {
2321 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2322 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2324 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2325 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2327 if (network
->dhcp_server
)
2328 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2331 r
= network_apply(network
, link
);
2336 r
= link_new_bound_to_list(link
);
2340 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
2341 * link_configure() is called later asynchronously. */
2342 r
= link_configure_duid(link
);
2346 r
= link_configure(link
);
2353 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2354 _cleanup_strv_free_
char **s
= NULL
;
2357 r
= sd_netlink_message_get_errno(m
);
2359 log_link_warning_errno(link
, r
, "Failed to wait for the interface to be initialized: %m");
2360 link_enter_failed(link
);
2364 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
2365 if (r
< 0 && r
!= -ENODATA
) {
2366 link_enter_failed(link
);
2370 strv_free_and_replace(link
->alternative_names
, s
);
2372 r
= link_initialized_and_synced(link
);
2374 link_enter_failed(link
);
2378 int link_initialized(Link
*link
, sd_device
*device
) {
2379 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2383 assert(link
->manager
);
2384 assert(link
->manager
->rtnl
);
2387 if (link
->state
!= LINK_STATE_PENDING
)
2390 if (link
->sd_device
)
2393 log_link_debug(link
, "udev initialized link");
2394 link_set_state(link
, LINK_STATE_INITIALIZED
);
2396 link
->sd_device
= sd_device_ref(device
);
2398 /* udev has initialized the link, but we don't know if we have yet
2399 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2400 * when it returns we know that the pending NEWLINKs have already been
2401 * processed and that we are up-to-date */
2403 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2408 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
2409 link_netlink_destroy_callback
, link
);
2418 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2419 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
2420 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2429 r
= link_new(m
, message
, ret
);
2435 log_link_debug(link
, "Link %d added", link
->ifindex
);
2437 if (path_is_read_only_fs("/sys") <= 0) {
2438 /* udev should be around */
2439 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2440 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
2442 log_link_warning_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
2446 r
= sd_device_get_is_initialized(device
);
2448 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
2453 log_link_debug(link
, "link pending udev initialization...");
2457 r
= device_is_renaming(device
);
2459 log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
2463 log_link_debug(link
, "Interface is being renamed, pending initialization.");
2467 r
= link_initialized(link
, device
);
2471 r
= link_initialized_and_synced(link
);
2478 link_enter_failed(link
);
2482 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2487 log_link_info(link
, "Gained IPv6LL");
2489 link
->ipv6ll_address
= *address
;
2490 link_check_ready(link
);
2492 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
2493 r
= link_acquire_ipv6_conf(link
);
2495 link_enter_failed(link
);
2503 static int link_carrier_gained(Link
*link
) {
2508 r
= wifi_get_info(link
);
2512 r
= link_reconfigure_internal(link
, NULL
, false);
2514 link_enter_failed(link
);
2519 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
2520 r
= link_acquire_conf(link
);
2522 link_enter_failed(link
);
2526 link_set_state(link
, LINK_STATE_CONFIGURING
);
2527 r
= link_set_static_configs(link
);
2532 r
= link_handle_bound_by_list(link
);
2536 if (!link
->bridge_mdb_configured
) {
2537 r
= link_set_bridge_mdb(link
);
2542 if (streq_ptr(link
->kind
, "bridge")) {
2545 SET_FOREACH(slave
, link
->slaves
) {
2546 if (slave
->bridge_mdb_configured
)
2549 r
= link_set_bridge_mdb(slave
);
2551 link_enter_failed(slave
);
2558 static int link_carrier_lost(Link
*link
) {
2563 if (link
->network
&& link
->network
->ignore_carrier_loss
)
2566 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
2567 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
2568 * configuration and stop the clients as well. */
2569 if (link
->setting_mtu
)
2572 r
= link_stop_engines(link
, false);
2574 link_enter_failed(link
);
2578 r
= link_drop_config(link
);
2582 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
2583 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2584 r
= link_drop_foreign_config(link
);
2589 r
= link_handle_bound_by_list(link
);
2596 int link_carrier_reset(Link
*link
) {
2601 if (link_has_carrier(link
)) {
2602 r
= link_carrier_lost(link
);
2606 r
= link_carrier_gained(link
);
2610 log_link_info(link
, "Reset carrier");
2616 /* This is called every time an interface admin state changes to up;
2617 * specifically, when IFF_UP flag changes from unset to set */
2618 static int link_admin_state_up(Link
*link
) {
2621 /* We set the ipv6 mtu after the device mtu, but the kernel resets
2622 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
2623 * ipv6_mtu_set prevents this from trying to set it too early before
2624 * the link->network has been setup; we only need to reset it
2625 * here if we've already set it during normal initialization. */
2626 if (link
->ipv6_mtu_set
) {
2627 r
= link_set_ipv6_mtu(link
);
2635 int link_update(Link
*link
, sd_netlink_message
*m
) {
2636 _cleanup_strv_free_
char **s
= NULL
;
2637 hw_addr_data hw_addr
;
2640 bool had_carrier
, carrier_gained
, carrier_lost
, link_was_admin_up
;
2644 assert(link
->ifname
);
2647 if (link
->state
== LINK_STATE_LINGER
) {
2648 log_link_info(link
, "Link re-added");
2649 link_set_state(link
, LINK_STATE_CONFIGURING
);
2651 r
= link_new_carrier_maps(link
);
2656 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2657 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2658 Manager
*manager
= link
->manager
;
2660 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
2663 r
= link_add(manager
, m
, &link
);
2668 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
2670 strv_free_and_replace(link
->alternative_names
, s
);
2672 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2673 if (r
>= 0 && mtu
> 0) {
2675 if (link
->original_mtu
== 0) {
2676 link
->original_mtu
= mtu
;
2677 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2680 if (link
->dhcp_client
) {
2681 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2684 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2688 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
2690 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
2694 /* The kernel may broadcast NEWLINK messages without the MAC address
2695 set, simply ignore them. */
2696 r
= netlink_message_read_hw_addr(m
, IFLA_ADDRESS
, &hw_addr
);
2697 if (r
>= 0 && (link
->hw_addr
.length
!= hw_addr
.length
||
2698 memcmp(link
->hw_addr
.addr
.bytes
, hw_addr
.addr
.bytes
, hw_addr
.length
) != 0)) {
2700 memcpy(link
->hw_addr
.addr
.bytes
, hw_addr
.addr
.bytes
, hw_addr
.length
);
2702 log_link_debug(link
, "Gained new hardware address: %s", HW_ADDR_TO_STR(&hw_addr
));
2704 r
= ipv4ll_update_mac(link
);
2706 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2708 r
= dhcp4_update_mac(link
);
2710 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2712 r
= dhcp6_update_mac(link
);
2714 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2716 r
= radv_update_mac(link
);
2718 return log_link_warning_errno(link
, r
, "Could not update MAC address for Router Advertisement: %m");
2721 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->hw_addr
.addr
.ether
);
2723 return log_link_warning_errno(link
, r
, "Could not update MAC for NDisc: %m");
2726 r
= ipv4_dad_update_mac(link
);
2728 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4 ACD client: %m");
2731 old_master
= link
->master_ifindex
;
2732 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
2734 link_was_admin_up
= link
->flags
& IFF_UP
;
2735 had_carrier
= link_has_carrier(link
);
2737 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
2741 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
2742 log_link_info(link
, "Link UP");
2744 r
= link_admin_state_up(link
);
2747 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
))
2748 log_link_info(link
, "Link DOWN");
2750 r
= link_update_lldp(link
);
2754 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2755 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2757 if (carrier_gained
) {
2758 log_link_info(link
, "Gained carrier");
2760 r
= link_carrier_gained(link
);
2763 } else if (carrier_lost
) {
2764 log_link_info(link
, "Lost carrier");
2766 r
= link_carrier_lost(link
);
2774 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2781 if (hashmap_isempty(h
))
2785 HASHMAP_FOREACH(link
, h
) {
2789 fprintf(f
, "%i", link
->ifindex
);
2796 static void link_save_dns(Link
*link
, FILE *f
, struct in_addr_full
**dns
, unsigned n_dns
, bool *space
) {
2797 for (unsigned j
= 0; j
< n_dns
; j
++) {
2800 if (dns
[j
]->ifindex
!= 0 && dns
[j
]->ifindex
!= link
->ifindex
)
2803 str
= in_addr_full_to_string(dns
[j
]);
2814 static void serialize_addresses(
2819 sd_dhcp_lease
*lease
,
2821 sd_dhcp_lease_server_type what
,
2822 sd_dhcp6_lease
*lease6
,
2824 int (*lease6_get_addr
)(sd_dhcp6_lease
*, const struct in6_addr
**),
2825 int (*lease6_get_fqdn
)(sd_dhcp6_lease
*, char ***)) {
2828 bool _space
= false;
2833 fprintf(f
, "%s=", lvalue
);
2834 fputstrv(f
, addresses
, NULL
, space
);
2836 if (lease
&& conditional
) {
2837 const struct in_addr
*lease_addresses
;
2839 r
= sd_dhcp_lease_get_servers(lease
, what
, &lease_addresses
);
2841 serialize_in_addrs(f
, lease_addresses
, r
, space
, in4_addr_is_non_local
);
2844 if (lease6
&& conditional6
&& lease6_get_addr
) {
2845 const struct in6_addr
*in6_addrs
;
2847 r
= lease6_get_addr(lease6
, &in6_addrs
);
2849 serialize_in6_addrs(f
, in6_addrs
, r
, space
);
2852 if (lease6
&& conditional6
&& lease6_get_fqdn
) {
2855 r
= lease6_get_fqdn(lease6
, &in6_hosts
);
2857 fputstrv(f
, in6_hosts
, NULL
, space
);
2864 int link_save(Link
*link
) {
2865 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
2866 _cleanup_free_
char *temp_path
= NULL
;
2867 _cleanup_fclose_
FILE *f
= NULL
;
2871 assert(link
->state_file
);
2872 assert(link
->lease_file
);
2873 assert(link
->manager
);
2875 if (link
->state
== LINK_STATE_LINGER
) {
2876 (void) unlink(link
->state_file
);
2880 link_lldp_save(link
);
2882 admin_state
= link_state_to_string(link
->state
);
2883 assert(admin_state
);
2885 oper_state
= link_operstate_to_string(link
->operstate
);
2888 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
2889 assert(carrier_state
);
2891 address_state
= link_address_state_to_string(link
->address_state
);
2892 assert(address_state
);
2894 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2898 (void) fchmod(fileno(f
), 0644);
2901 "# This is private data. Do not parse.\n"
2904 "CARRIER_STATE=%s\n"
2905 "ADDRESS_STATE=%s\n",
2906 admin_state
, oper_state
, carrier_state
, address_state
);
2908 if (link
->network
) {
2909 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
2910 const char *dhcp_domainname
= NULL
, *p
;
2913 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
2914 yes_no(link
->network
->required_for_online
));
2916 LinkOperationalStateRange st
= link
->network
->required_operstate_for_online
;
2917 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
2918 strempty(link_operstate_to_string(st
.min
)),
2919 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? ":" : "",
2920 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? strempty(link_operstate_to_string(st
.max
)) : "");
2922 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2924 /************************************************************/
2928 if (link
->n_dns
!= (unsigned) -1)
2929 link_save_dns(link
, f
, link
->dns
, link
->n_dns
, &space
);
2931 link_save_dns(link
, f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
2933 serialize_addresses(f
, NULL
, &space
,
2936 link
->network
->dhcp_use_dns
,
2939 link
->network
->dhcp6_use_dns
,
2940 sd_dhcp6_lease_get_dns
,
2943 /* Make sure to flush out old entries before we use the NDisc data */
2946 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
2949 SET_FOREACH(dd
, link
->ndisc_rdnss
)
2950 serialize_in6_addrs(f
, &dd
->address
, 1, &space
);
2955 /************************************************************/
2957 serialize_addresses(f
, "NTP", NULL
,
2958 link
->ntp
?: link
->network
->ntp
,
2960 link
->network
->dhcp_use_ntp
,
2963 link
->network
->dhcp6_use_ntp
,
2964 sd_dhcp6_lease_get_ntp_addrs
,
2965 sd_dhcp6_lease_get_ntp_fqdn
);
2967 serialize_addresses(f
, "SIP", NULL
,
2970 link
->network
->dhcp_use_sip
,
2972 NULL
, false, NULL
, NULL
);
2974 /************************************************************/
2976 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
2977 if (link
->dhcp_lease
) {
2978 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
2979 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
2981 if (link
->dhcp6_lease
)
2982 (void) sd_dhcp6_lease_get_domains(link
->dhcp6_lease
, &dhcp6_domains
);
2985 fputs("DOMAINS=", f
);
2987 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
)
2988 fputs_with_space(f
, p
, NULL
, &space
);
2990 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
2991 if (dhcp_domainname
)
2992 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2994 fputstrv(f
, dhcp_domains
, NULL
, &space
);
2996 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2999 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
3002 SET_FOREACH(dd
, link
->ndisc_dnssl
)
3003 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3008 /************************************************************/
3010 fputs("ROUTE_DOMAINS=", f
);
3012 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
)
3013 fputs_with_space(f
, p
, NULL
, &space
);
3015 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3016 if (dhcp_domainname
)
3017 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3019 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3021 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3024 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3027 SET_FOREACH(dd
, link
->ndisc_dnssl
)
3028 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3033 /************************************************************/
3035 fprintf(f
, "LLMNR=%s\n",
3036 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
3038 /************************************************************/
3040 fprintf(f
, "MDNS=%s\n",
3041 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
3043 /************************************************************/
3045 int dns_default_route
=
3046 link
->dns_default_route
>= 0 ? link
->dns_default_route
:
3047 link
->network
->dns_default_route
;
3048 if (dns_default_route
>= 0)
3049 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route
));
3051 /************************************************************/
3053 DnsOverTlsMode dns_over_tls_mode
=
3054 link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
? link
->dns_over_tls_mode
:
3055 link
->network
->dns_over_tls_mode
;
3056 if (dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3057 fprintf(f
, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode
));
3059 /************************************************************/
3061 DnssecMode dnssec_mode
=
3062 link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
? link
->dnssec_mode
:
3063 link
->network
->dnssec_mode
;
3064 if (dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3065 fprintf(f
, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode
));
3067 /************************************************************/
3069 Set
*nta_anchors
= link
->dnssec_negative_trust_anchors
;
3070 if (set_isempty(nta_anchors
))
3071 nta_anchors
= link
->network
->dnssec_negative_trust_anchors
;
3073 if (!set_isempty(nta_anchors
)) {
3076 fputs("DNSSEC_NTA=", f
);
3078 SET_FOREACH(n
, nta_anchors
)
3079 fputs_with_space(f
, n
, NULL
, &space
);
3084 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3085 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3087 if (link
->dhcp_lease
) {
3088 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3096 (void) unlink(link
->lease_file
);
3098 r
= link_serialize_dhcp6_client(link
, f
);
3102 r
= fflush_and_check(f
);
3106 if (rename(temp_path
, link
->state_file
) < 0) {
3114 (void) unlink(link
->state_file
);
3116 (void) unlink(temp_path
);
3118 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3121 /* The serialized state in /run is no longer up-to-date. */
3122 void link_dirty(Link
*link
) {
3127 /* mark manager dirty as link is dirty */
3128 manager_dirty(link
->manager
);
3130 r
= set_ensure_put(&link
->manager
->dirty_links
, NULL
, link
);
3132 /* Ignore allocation errors and don't take another ref if the link was already dirty */
3137 /* The serialized state in /run is up-to-date */
3138 void link_clean(Link
*link
) {
3140 assert(link
->manager
);
3142 link_unref(set_remove(link
->manager
->dirty_links
, link
));
3145 int link_save_and_clean(Link
*link
) {
3148 r
= link_save(link
);
3156 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3157 [LINK_STATE_PENDING
] = "pending",
3158 [LINK_STATE_INITIALIZED
] = "initialized",
3159 [LINK_STATE_CONFIGURING
] = "configuring",
3160 [LINK_STATE_CONFIGURED
] = "configured",
3161 [LINK_STATE_UNMANAGED
] = "unmanaged",
3162 [LINK_STATE_FAILED
] = "failed",
3163 [LINK_STATE_LINGER
] = "linger",
3166 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3168 int log_link_message_full_errno(Link
*link
, sd_netlink_message
*m
, int level
, int err
, const char *msg
) {
3169 const char *err_msg
= NULL
;
3171 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
3172 return log_link_full_errno(link
, level
, err
,
3176 err_msg
&& !endswith(err_msg
, ".") ? "." : "",
3177 err_msg
? " " : "");