1 /* SPDX-License-Identifier: LGPL-2.1+ */
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-can.h"
24 #include "networkd-dhcp-server.h"
25 #include "networkd-dhcp4.h"
26 #include "networkd-dhcp6.h"
27 #include "networkd-ipv4ll.h"
28 #include "networkd-ipv6-proxy-ndp.h"
29 #include "networkd-link-bus.h"
30 #include "networkd-link.h"
31 #include "networkd-lldp-tx.h"
32 #include "networkd-manager.h"
33 #include "networkd-ndisc.h"
34 #include "networkd-neighbor.h"
35 #include "networkd-sriov.h"
36 #include "networkd-radv.h"
37 #include "networkd-routing-policy-rule.h"
38 #include "networkd-wifi.h"
40 #include "socket-util.h"
41 #include "stat-util.h"
42 #include "stdio-util.h"
43 #include "string-table.h"
45 #include "sysctl-util.h"
47 #include "tmpfile-util.h"
48 #include "udev-util.h"
52 uint32_t link_get_vrf_table(Link
*link
) {
53 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
56 uint32_t link_get_dhcp_route_table(Link
*link
) {
57 /* When the interface is part of an VRF use the VRFs routing table, unless
58 * another table is explicitly specified. */
59 if (link
->network
->dhcp_route_table_set
)
60 return link
->network
->dhcp_route_table
;
61 return link_get_vrf_table(link
);
64 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
65 if (link
->network
->ipv6_accept_ra_route_table_set
)
66 return link
->network
->ipv6_accept_ra_route_table
;
67 return link_get_vrf_table(link
);
70 DUID
* link_get_duid(Link
*link
) {
71 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
72 return &link
->network
->duid
;
74 return &link
->manager
->duid
;
77 static bool link_dhcp6_enabled(Link
*link
) {
80 if (!socket_ipv6_is_supported())
83 if (link
->flags
& IFF_LOOPBACK
)
89 if (link
->network
->bond
)
92 if (link
->iftype
== ARPHRD_CAN
)
95 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
98 static bool link_dhcp4_enabled(Link
*link
) {
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (link
->network
->bond
)
110 if (link
->iftype
== ARPHRD_CAN
)
113 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
116 static bool link_dhcp4_server_enabled(Link
*link
) {
119 if (link
->flags
& IFF_LOOPBACK
)
125 if (link
->network
->bond
)
128 if (link
->iftype
== ARPHRD_CAN
)
131 return link
->network
->dhcp_server
;
134 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
136 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->iftype
== ARPHRD_CAN
)
147 if (STRPTR_IN_SET(link
->kind
,
148 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
149 "vti6", "nlmon", "xfrm"))
152 /* L3 or L3S mode do not support ARP. */
153 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
156 if (link
->network
->bond
)
159 return link
->network
->link_local
& mask
;
162 static bool link_ipv6ll_enabled(Link
*link
) {
165 if (!socket_ipv6_is_supported())
168 if (link
->flags
& IFF_LOOPBACK
)
174 if (link
->iftype
== ARPHRD_CAN
)
177 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
180 if (link
->network
->bond
)
183 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
186 static bool link_ipv6_enabled(Link
*link
) {
189 if (!socket_ipv6_is_supported())
192 if (link
->network
->bond
)
195 if (link
->iftype
== ARPHRD_CAN
)
198 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
199 if (link_ipv6ll_enabled(link
))
202 if (network_has_static_ipv6_configurations(link
->network
))
208 static bool link_radv_enabled(Link
*link
) {
211 if (!link_ipv6ll_enabled(link
))
214 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
217 static bool link_ipv4_forward_enabled(Link
*link
) {
220 if (link
->flags
& IFF_LOOPBACK
)
226 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
229 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
232 static bool link_ipv6_forward_enabled(Link
*link
) {
235 if (!socket_ipv6_is_supported())
238 if (link
->flags
& IFF_LOOPBACK
)
244 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
247 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
250 static bool link_proxy_arp_enabled(Link
*link
) {
253 if (link
->flags
& IFF_LOOPBACK
)
259 if (link
->network
->proxy_arp
< 0)
265 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
268 if (!socket_ipv6_is_supported())
271 if (link
->flags
& IFF_LOOPBACK
)
277 if (!link_ipv6ll_enabled(link
))
280 /* If unset use system default (enabled if local forwarding is disabled.
281 * disabled if local forwarding is enabled).
282 * If set, ignore or enforce RA independent of local forwarding state.
284 if (link
->network
->ipv6_accept_ra
< 0)
285 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
286 return !link_ipv6_forward_enabled(link
);
287 else if (link
->network
->ipv6_accept_ra
> 0)
288 /* accept RA even if ip_forward is enabled */
295 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
298 if (!socket_ipv6_is_supported())
299 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
301 if (link
->flags
& IFF_LOOPBACK
)
302 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
305 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
307 return link
->network
->ipv6_privacy_extensions
;
310 static int link_update_ipv6_sysctl(Link
*link
) {
314 if (link
->flags
& IFF_LOOPBACK
)
317 enabled
= link_ipv6_enabled(link
);
319 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", false);
321 return log_link_warning_errno(link
, r
, "Cannot enable IPv6: %m");
323 log_link_info(link
, "IPv6 successfully enabled");
329 static bool link_is_enslaved(Link
*link
) {
330 if (link
->flags
& IFF_SLAVE
)
331 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
337 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
340 /* TODO: add conditions for other netdevs. */
345 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
351 if (netdev
->ifindex
<= 0)
354 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
357 link_update_operstate(master
, true);
360 void link_update_operstate(Link
*link
, bool also_update_master
) {
361 LinkOperationalState operstate
;
362 LinkCarrierState carrier_state
;
363 LinkAddressState address_state
;
364 _cleanup_strv_free_
char **p
= NULL
;
365 uint8_t scope
= RT_SCOPE_NOWHERE
;
366 bool changed
= false;
372 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
373 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
374 else if (link_has_carrier(link
)) {
375 if (link_is_enslaved(link
))
376 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
378 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
379 } else if (link
->flags
& IFF_UP
)
380 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
382 carrier_state
= LINK_CARRIER_STATE_OFF
;
384 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
387 SET_FOREACH(slave
, link
->slaves
, i
) {
388 link_update_operstate(slave
, false);
390 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
391 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
395 SET_FOREACH(address
, link
->addresses
, i
) {
396 if (!address_is_ready(address
))
399 if (address
->scope
< scope
)
400 scope
= address
->scope
;
403 /* for operstate we also take foreign addresses into account */
404 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
405 if (!address_is_ready(address
))
408 if (address
->scope
< scope
)
409 scope
= address
->scope
;
412 if (scope
< RT_SCOPE_SITE
)
413 /* universally accessible addresses found */
414 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
415 else if (scope
< RT_SCOPE_HOST
)
416 /* only link or site local addresses found */
417 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
419 /* no useful addresses found */
420 address_state
= LINK_ADDRESS_STATE_OFF
;
422 /* Mapping of address and carrier state vs operational state
424 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
425 * ------------------------------------------------------------------------------
426 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
427 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
428 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
431 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
432 operstate
= (LinkOperationalState
) carrier_state
;
433 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
434 operstate
= LINK_OPERSTATE_ROUTABLE
;
435 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
436 operstate
= LINK_OPERSTATE_DEGRADED
;
438 operstate
= LINK_OPERSTATE_ENSLAVED
;
440 if (link
->carrier_state
!= carrier_state
) {
441 link
->carrier_state
= carrier_state
;
443 if (strv_extend(&p
, "CarrierState") < 0)
447 if (link
->address_state
!= address_state
) {
448 link
->address_state
= address_state
;
450 if (strv_extend(&p
, "AddressState") < 0)
454 if (link
->operstate
!= operstate
) {
455 link
->operstate
= operstate
;
457 if (strv_extend(&p
, "OperationalState") < 0)
462 link_send_changed_strv(link
, p
);
466 if (also_update_master
&& link
->network
) {
467 link_update_master_operstate(link
, link
->network
->bond
);
468 link_update_master_operstate(link
, link
->network
->bridge
);
472 #define FLAG_STRING(string, flag, old, new) \
473 (((old ^ new) & flag) \
474 ? ((old & flag) ? (" -" string) : (" +" string)) \
477 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
478 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
484 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
486 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
488 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
490 /* if we got a message without operstate, take it to mean
491 the state was unchanged */
492 operstate
= link
->kernel_operstate
;
494 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
497 if (link
->flags
!= flags
) {
498 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",
499 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
500 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
501 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
502 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
503 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
504 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
505 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
506 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
507 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
508 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
509 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
510 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
511 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
512 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
513 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
514 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
515 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
516 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
517 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
519 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
520 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
521 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
522 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
523 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
524 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
525 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
526 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
528 /* link flags are currently at most 18 bits, let's align to
530 if (unknown_flags_added
)
532 "Unknown link flags gained: %#.5x (ignoring)",
533 unknown_flags_added
);
535 if (unknown_flags_removed
)
537 "Unknown link flags lost: %#.5x (ignoring)",
538 unknown_flags_removed
);
542 link
->kernel_operstate
= operstate
;
544 link_update_operstate(link
, true);
549 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
550 _cleanup_(link_unrefp
) Link
*link
= NULL
;
551 const char *ifname
, *kind
= NULL
;
552 unsigned short iftype
;
560 /* check for link kind */
561 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
563 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
564 r
= sd_netlink_message_exit_container(message
);
569 r
= sd_netlink_message_get_type(message
, &type
);
572 else if (type
!= RTM_NEWLINK
)
575 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
578 else if (ifindex
<= 0)
581 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
585 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
596 .state
= LINK_STATE_PENDING
,
600 .n_dns
= (unsigned) -1,
601 .dns_default_route
= -1,
602 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
603 .mdns
= _RESOLVE_SUPPORT_INVALID
,
604 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
605 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
608 link
->ifname
= strdup(ifname
);
613 link
->kind
= strdup(kind
);
618 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
620 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
622 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
624 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
626 r
= ethtool_get_permanent_macaddr(&manager
->ethtool_fd
, link
->ifname
, &link
->permanent_mac
);
628 log_link_debug_errno(link
, r
, "Permanent MAC address not found for new device, continuing without: %m");
630 r
= ethtool_get_driver(&manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
632 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
634 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &link
->alternative_names
);
635 if (r
< 0 && r
!= -ENODATA
)
638 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
641 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
644 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
647 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
651 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
655 r
= link_update_flags(link
, message
, false);
659 *ret
= TAKE_PTR(link
);
664 void link_ntp_settings_clear(Link
*link
) {
665 link
->ntp
= strv_free(link
->ntp
);
668 void link_dns_settings_clear(Link
*link
) {
669 if (link
->n_dns
!= (unsigned) -1)
670 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
671 in_addr_full_free(link
->dns
[i
]);
672 link
->dns
= mfree(link
->dns
);
673 link
->n_dns
= (unsigned) -1;
675 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
676 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
678 link
->dns_default_route
= -1;
679 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
680 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
681 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
682 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
684 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
687 static void link_free_engines(Link
*link
) {
691 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
692 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
693 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
695 link
->lldp
= sd_lldp_unref(link
->lldp
);
699 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
700 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
701 link
->dhcp6_lease
= sd_dhcp6_lease_unref(link
->dhcp6_lease
);
702 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
703 link
->radv
= sd_radv_unref(link
->radv
);
706 static Link
*link_free(Link
*link
) {
711 link_ntp_settings_clear(link
);
712 link_dns_settings_clear(link
);
714 link
->routes
= set_free(link
->routes
);
715 link
->routes_foreign
= set_free(link
->routes_foreign
);
716 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
717 link
->dhcp_routes_old
= set_free(link
->dhcp_routes_old
);
718 link
->dhcp6_routes
= set_free(link
->dhcp6_routes
);
719 link
->dhcp6_routes_old
= set_free(link
->dhcp6_routes_old
);
720 link
->dhcp6_pd_routes
= set_free(link
->dhcp6_pd_routes
);
721 link
->dhcp6_pd_routes_old
= set_free(link
->dhcp6_pd_routes_old
);
722 link
->ndisc_routes
= set_free(link
->ndisc_routes
);
723 link
->ndisc_routes_old
= set_free(link
->ndisc_routes_old
);
725 link
->nexthops
= set_free(link
->nexthops
);
726 link
->nexthops_foreign
= set_free(link
->nexthops_foreign
);
728 link
->neighbors
= set_free(link
->neighbors
);
729 link
->neighbors_foreign
= set_free(link
->neighbors_foreign
);
731 link
->addresses
= set_free(link
->addresses
);
732 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
733 link
->static_addresses
= set_free(link
->static_addresses
);
734 link
->dhcp6_addresses
= set_free(link
->dhcp6_addresses
);
735 link
->dhcp6_addresses_old
= set_free(link
->dhcp6_addresses_old
);
736 link
->dhcp6_pd_addresses
= set_free(link
->dhcp6_pd_addresses
);
737 link
->dhcp6_pd_addresses_old
= set_free(link
->dhcp6_pd_addresses_old
);
738 link
->ndisc_addresses
= set_free(link
->ndisc_addresses
);
739 link
->ndisc_addresses_old
= set_free(link
->ndisc_addresses_old
);
741 while ((address
= link
->pool_addresses
)) {
742 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
743 address_free(address
);
746 link_lldp_emit_stop(link
);
747 link_free_engines(link
);
748 free(link
->lease_file
);
749 free(link
->lldp_file
);
752 strv_free(link
->alternative_names
);
757 (void) unlink(link
->state_file
);
758 free(link
->state_file
);
760 sd_device_unref(link
->sd_device
);
762 hashmap_free(link
->bound_to_links
);
763 hashmap_free(link
->bound_by_links
);
765 set_free_with_destructor(link
->slaves
, link_unref
);
767 network_unref(link
->network
);
772 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
774 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
781 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
790 void link_set_state(Link
*link
, LinkState state
) {
793 if (link
->state
== state
)
796 log_link_debug(link
, "State changed: %s -> %s",
797 link_state_to_string(link
->state
),
798 link_state_to_string(state
));
802 link_send_changed(link
, "AdministrativeState", NULL
);
805 static void link_enter_unmanaged(Link
*link
) {
808 link_set_state(link
, LINK_STATE_UNMANAGED
);
813 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
818 assert(link
->manager
);
819 assert(link
->manager
->event
);
821 bool keep_dhcp
= may_keep_dhcp
&&
823 (link
->manager
->restarting
||
824 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
826 if (link
->dhcp_client
&& !keep_dhcp
) {
827 k
= sd_dhcp_client_stop(link
->dhcp_client
);
829 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
833 k
= sd_ipv4ll_stop(link
->ipv4ll
);
835 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
839 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
)
840 if (ad
->acd
&& sd_ipv4acd_is_running(ad
->acd
) == 0) {
841 k
= sd_ipv4acd_stop(ad
->acd
);
843 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
846 if (link
->dhcp6_client
) {
847 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
849 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
852 if (link_dhcp6_pd_is_enabled(link
)) {
853 k
= dhcp6_pd_remove(link
);
855 r
= log_link_warning_errno(link
, k
, "Could not remove DHCPv6 PD addresses and routes: %m");
859 k
= sd_ndisc_stop(link
->ndisc
);
861 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
865 k
= sd_radv_stop(link
->radv
);
867 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
870 link_lldp_emit_stop(link
);
874 void link_enter_failed(Link
*link
) {
877 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
880 log_link_warning(link
, "Failed");
882 link_set_state(link
, LINK_STATE_FAILED
);
884 link_stop_clients(link
, false);
889 static int link_join_netdevs_after_configured(Link
*link
) {
894 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
895 if (netdev
->ifindex
> 0)
896 /* Assume already enslaved. */
899 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
902 log_struct(LOG_DEBUG
,
903 LOG_LINK_INTERFACE(link
),
904 LOG_NETDEV_INTERFACE(netdev
),
905 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
907 r
= netdev_join(netdev
, link
, NULL
);
909 return log_struct_errno(LOG_WARNING
, r
,
910 LOG_LINK_INTERFACE(link
),
911 LOG_NETDEV_INTERFACE(netdev
),
912 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
918 static void link_enter_configured(Link
*link
) {
920 assert(link
->network
);
922 if (link
->state
!= LINK_STATE_CONFIGURING
)
925 link_set_state(link
, LINK_STATE_CONFIGURED
);
927 (void) link_join_netdevs_after_configured(link
);
932 static int link_request_set_routing_policy_rule(Link
*link
) {
933 RoutingPolicyRule
*rule
, *rrule
= NULL
;
937 assert(link
->network
);
939 link
->routing_policy_rules_configured
= false;
941 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
942 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
945 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
949 r
= routing_policy_rule_configure(rule
, link
, NULL
);
951 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
953 link
->routing_policy_rule_messages
++;
956 routing_policy_rule_purge(link
->manager
, link
);
957 if (link
->routing_policy_rule_messages
== 0)
958 link
->routing_policy_rules_configured
= true;
960 log_link_debug(link
, "Setting routing policy rules");
961 link_set_state(link
, LINK_STATE_CONFIGURING
);
967 static int nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
971 assert(link
->nexthop_messages
> 0);
972 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
973 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
975 link
->nexthop_messages
--;
977 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
980 r
= sd_netlink_message_get_errno(m
);
981 if (r
< 0 && r
!= -EEXIST
) {
982 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
983 link_enter_failed(link
);
987 if (link
->nexthop_messages
== 0) {
988 log_link_debug(link
, "Nexthop set");
989 link
->static_nexthops_configured
= true;
990 link_check_ready(link
);
996 static int link_request_set_nexthop(Link
*link
) {
1000 link
->static_nexthops_configured
= false;
1002 LIST_FOREACH(nexthops
, nh
, link
->network
->static_nexthops
) {
1003 r
= nexthop_configure(nh
, link
, nexthop_handler
);
1005 return log_link_warning_errno(link
, r
, "Could not set nexthop: %m");
1007 link
->nexthop_messages
++;
1010 if (link
->nexthop_messages
== 0) {
1011 link
->static_nexthops_configured
= true;
1012 link_check_ready(link
);
1014 log_link_debug(link
, "Setting nexthop");
1015 link_set_state(link
, LINK_STATE_CONFIGURING
);
1021 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1025 assert(link
->route_messages
> 0);
1026 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1027 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1029 link
->route_messages
--;
1031 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1034 r
= sd_netlink_message_get_errno(m
);
1035 if (r
< 0 && r
!= -EEXIST
) {
1036 log_link_message_warning_errno(link
, m
, r
, "Could not set route");
1037 link_enter_failed(link
);
1041 if (link
->route_messages
== 0) {
1042 log_link_debug(link
, "Routes set");
1043 link
->static_routes_configured
= true;
1044 link_request_set_nexthop(link
);
1050 int link_request_set_routes(Link
*link
) {
1052 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
1053 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
1060 assert(link
->network
);
1061 assert(link
->state
!= _LINK_STATE_INVALID
);
1063 link
->static_routes_configured
= false;
1065 if (!link
->addresses_ready
)
1068 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
1069 /* During configuring addresses, the link lost its carrier. As networkd is dropping
1070 * the addresses now, let's not configure the routes either. */
1073 r
= link_request_set_routing_policy_rule(link
);
1077 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
1078 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
1079 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
1080 if (rt
->gateway_from_dhcp
)
1083 if ((in_addr_is_null(rt
->family
, &rt
->gw
) && ordered_set_isempty(rt
->multipath_routes
)) != (phase
== PHASE_NON_GATEWAY
))
1086 r
= route_configure(rt
, link
, route_handler
, NULL
);
1088 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
1090 link
->route_messages
++;
1093 if (link
->route_messages
== 0) {
1094 link
->static_routes_configured
= true;
1095 link_request_set_nexthop(link
);
1097 log_link_debug(link
, "Setting routes");
1098 link_set_state(link
, LINK_STATE_CONFIGURING
);
1104 void link_check_ready(Link
*link
) {
1110 if (link
->state
== LINK_STATE_CONFIGURED
)
1113 if (link
->state
!= LINK_STATE_CONFIGURING
) {
1114 log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
1121 if (!link
->addresses_configured
) {
1122 log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
1126 if (!link
->neighbors_configured
) {
1127 log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
1131 SET_FOREACH(a
, link
->addresses
, i
)
1132 if (!address_is_ready(a
)) {
1133 _cleanup_free_
char *str
= NULL
;
1135 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
1136 log_link_debug(link
, "%s(): an address %s/%d is not ready.", __func__
, strnull(str
), a
->prefixlen
);
1140 if (!link
->static_routes_configured
) {
1141 log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
1145 if (!link
->static_nexthops_configured
) {
1146 log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
1150 if (!link
->routing_policy_rules_configured
) {
1151 log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
1155 if (!link
->tc_configured
) {
1156 log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
1160 if (!link
->sr_iov_configured
) {
1161 log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
1165 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1167 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !link
->ipv4ll_address_configured
) {
1168 log_link_debug(link
, "%s(): IPv4LL is not configured.", __func__
);
1172 if (link_ipv6ll_enabled(link
) &&
1173 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1174 log_link_debug(link
, "%s(): IPv6LL is not configured.", __func__
);
1178 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1179 !link
->dhcp_address
&& set_isempty(link
->dhcp6_addresses
) && set_isempty(link
->ndisc_addresses
) &&
1180 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
1181 log_link_debug(link
, "%s(): DHCP4 or DHCP6 is enabled but no dynamic address is assigned yet.", __func__
);
1185 if (link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
) || link_dhcp6_pd_is_enabled(link
) || link_ipv6_accept_ra_enabled(link
)) {
1186 if (!link
->dhcp4_configured
&&
1187 !(link
->dhcp6_address_configured
&& link
->dhcp6_route_configured
) &&
1188 !(link
->dhcp6_pd_address_configured
&& link
->dhcp6_pd_route_configured
) &&
1189 !(link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
) &&
1190 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
1191 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
1192 * an IPv4ll fallback address must be configured. */
1193 log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
1197 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",
1199 yes_no(link
->dhcp4_configured
),
1200 yes_no(link
->dhcp6_address_configured
),
1201 yes_no(link
->dhcp6_route_configured
),
1202 yes_no(link
->dhcp6_pd_address_configured
),
1203 yes_no(link
->dhcp6_pd_route_configured
),
1204 yes_no(link
->ndisc_addresses_configured
),
1205 yes_no(link
->ndisc_routes_configured
));
1209 link_enter_configured(link
);
1214 static int link_request_set_neighbors(Link
*link
) {
1219 assert(link
->network
);
1220 assert(link
->state
!= _LINK_STATE_INVALID
);
1222 link
->neighbors_configured
= false;
1224 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1225 r
= neighbor_configure(neighbor
, link
, NULL
);
1227 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1230 if (link
->neighbor_messages
== 0) {
1231 link
->neighbors_configured
= true;
1232 link_check_ready(link
);
1234 log_link_debug(link
, "Setting neighbors");
1235 link_set_state(link
, LINK_STATE_CONFIGURING
);
1241 static int link_set_bridge_fdb(Link
*link
) {
1242 FdbEntry
*fdb_entry
;
1245 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1246 r
= fdb_entry_configure(link
, fdb_entry
);
1248 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1254 static int static_address_ready_callback(Address
*address
) {
1260 assert(address
->link
);
1262 link
= address
->link
;
1264 if (!link
->addresses_configured
)
1267 SET_FOREACH(a
, link
->static_addresses
, i
)
1268 if (!address_is_ready(a
)) {
1269 _cleanup_free_
char *str
= NULL
;
1271 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
1272 log_link_debug(link
, "an address %s/%u is not ready", strnull(str
), a
->prefixlen
);
1276 /* This should not be called again */
1277 SET_FOREACH(a
, link
->static_addresses
, i
)
1280 link
->addresses_ready
= true;
1282 return link_request_set_routes(link
);
1285 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1291 assert(link
->ifname
);
1292 assert(link
->address_messages
> 0);
1293 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1294 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1296 link
->address_messages
--;
1298 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1301 r
= sd_netlink_message_get_errno(m
);
1302 if (r
< 0 && r
!= -EEXIST
) {
1303 log_link_message_warning_errno(link
, m
, r
, "Could not set address");
1304 link_enter_failed(link
);
1307 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1309 if (link
->address_messages
== 0) {
1312 log_link_debug(link
, "Addresses set");
1313 link
->addresses_configured
= true;
1315 /* When all static addresses are already ready, then static_address_ready_callback()
1316 * will not be called automatically. So, call it here. */
1317 a
= set_first(link
->static_addresses
);
1319 log_link_warning(link
, "No static address is stored.");
1320 link_enter_failed(link
);
1324 log_link_warning(link
, "Address ready callback is not set.");
1325 link_enter_failed(link
);
1330 link_enter_failed(link
);
1336 static int static_address_configure(Address
*address
, Link
*link
, bool update
) {
1343 r
= address_configure(address
, link
, address_handler
, update
, &ret
);
1345 return log_link_warning_errno(link
, r
, "Could not configure static address: %m");
1347 link
->address_messages
++;
1349 r
= set_ensure_put(&link
->static_addresses
, &address_hash_ops
, ret
);
1351 return log_link_warning_errno(link
, r
, "Failed to store static address: %m");
1353 ret
->callback
= static_address_ready_callback
;
1358 static int link_request_set_addresses(Link
*link
) {
1359 AddressLabel
*label
;
1365 assert(link
->network
);
1366 assert(link
->state
!= _LINK_STATE_INVALID
);
1368 /* Reset all *_configured flags we are configuring. */
1369 link
->addresses_configured
= false;
1370 link
->addresses_ready
= false;
1371 link
->neighbors_configured
= false;
1372 link
->static_routes_configured
= false;
1373 link
->static_nexthops_configured
= false;
1374 link
->routing_policy_rules_configured
= false;
1376 r
= link_set_bridge_fdb(link
);
1380 r
= link_request_set_neighbors(link
);
1384 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1387 if (ad
->family
== AF_INET6
&& !in_addr_is_null(ad
->family
, &ad
->in_addr_peer
))
1388 update
= address_get(link
, ad
->family
, &ad
->in_addr_peer
, ad
->prefixlen
, NULL
) > 0;
1390 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1392 r
= static_address_configure(ad
, link
, update
);
1397 if (link
->network
->router_prefix_delegation
& RADV_PREFIX_DELEGATION_STATIC
)
1398 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
1399 _cleanup_(address_freep
) Address
*address
= NULL
;
1404 r
= address_new(&address
);
1408 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1410 return log_link_warning_errno(link
, r
, "Could not get RA prefix: %m");
1412 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1414 return log_link_warning_errno(link
, r
, "Could not generate EUI64 address: %m");
1416 address
->family
= AF_INET6
;
1417 r
= static_address_configure(address
, link
, true);
1422 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1423 r
= address_label_configure(label
, link
, NULL
, false);
1425 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1427 link
->address_label_messages
++;
1430 /* now that we can figure out a default address for the dhcp server, start it */
1431 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1432 r
= dhcp4_server_configure(link
);
1435 log_link_debug(link
, "Offering DHCPv4 leases");
1438 if (link
->address_messages
== 0) {
1439 link
->addresses_configured
= true;
1440 link
->addresses_ready
= true;
1441 r
= link_request_set_routes(link
);
1445 log_link_debug(link
, "Setting addresses");
1446 link_set_state(link
, LINK_STATE_CONFIGURING
);
1452 static int link_set_bridge_vlan(Link
*link
) {
1455 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1457 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1462 static int link_set_proxy_arp(Link
*link
) {
1465 if (!link_proxy_arp_enabled(link
))
1468 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1470 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1475 static int link_configure_continue(Link
*link
);
1477 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1482 assert(link
->ifname
);
1484 link
->setting_mtu
= false;
1486 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1489 r
= sd_netlink_message_get_errno(m
);
1491 log_link_message_warning_errno(link
, m
, r
, "Could not set MTU, ignoring");
1493 log_link_debug(link
, "Setting MTU done.");
1495 if (link
->state
== LINK_STATE_INITIALIZED
) {
1496 r
= link_configure_continue(link
);
1498 link_enter_failed(link
);
1504 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1505 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1509 assert(link
->manager
);
1510 assert(link
->manager
->rtnl
);
1512 if (mtu
== 0 || link
->setting_mtu
)
1515 if (link
->mtu
== mtu
)
1518 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1520 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1522 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1524 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1525 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1526 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1528 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1529 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes");
1534 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1536 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1538 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1539 link_netlink_destroy_callback
, link
);
1541 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1544 link
->setting_mtu
= true;
1549 static bool link_reduces_vlan_mtu(Link
*link
) {
1550 /* See netif_reduces_vlan_mtu() in kernel. */
1551 return streq_ptr(link
->kind
, "macsec");
1554 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1559 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1560 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1561 /* See vlan_dev_change_mtu() in kernel. */
1562 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1564 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1565 /* See macvlan_change_mtu() in kernel. */
1571 static int link_configure_mtu(Link
*link
) {
1575 assert(link
->network
);
1577 if (link
->network
->mtu
> 0)
1578 return link_set_mtu(link
, link
->network
->mtu
);
1580 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1581 if (link
->mtu
>= mtu
)
1584 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1585 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1588 return link_set_mtu(link
, mtu
);
1591 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1596 assert(link
->ifname
);
1598 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1601 r
= sd_netlink_message_get_errno(m
);
1603 log_link_message_warning_errno(link
, m
, r
, "Could not set link flags, ignoring");
1608 static int link_set_flags(Link
*link
) {
1609 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1610 unsigned ifi_change
= 0;
1611 unsigned ifi_flags
= 0;
1615 assert(link
->manager
);
1616 assert(link
->manager
->rtnl
);
1618 if (link
->flags
& IFF_LOOPBACK
)
1624 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1627 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1629 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1631 if (link
->network
->arp
>= 0) {
1632 ifi_change
|= IFF_NOARP
;
1633 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1636 if (link
->network
->multicast
>= 0) {
1637 ifi_change
|= IFF_MULTICAST
;
1638 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1641 if (link
->network
->allmulticast
>= 0) {
1642 ifi_change
|= IFF_ALLMULTI
;
1643 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1646 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1648 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1650 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_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_acquire_ipv6_conf(Link
*link
) {
1665 if (link_ipv6_accept_ra_enabled(link
)) {
1666 assert(link
->ndisc
);
1668 log_link_debug(link
, "Discovering IPv6 routers");
1670 r
= sd_ndisc_start(link
->ndisc
);
1671 if (r
< 0 && r
!= -EBUSY
)
1672 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1675 if (link_radv_enabled(link
)) {
1677 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1679 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1681 r
= radv_emit_dns(link
);
1683 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1685 r
= sd_radv_start(link
->radv
);
1686 if (r
< 0 && r
!= -EBUSY
)
1687 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1690 if (link_dhcp6_enabled(link
) && IN_SET(link
->network
->dhcp6_without_ra
,
1691 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
,
1692 DHCP6_CLIENT_START_MODE_SOLICIT
)) {
1693 assert(link
->dhcp6_client
);
1694 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1696 r
= dhcp6_request_address(link
, link
->network
->dhcp6_without_ra
== DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
);
1697 if (r
< 0 && r
!= -EBUSY
)
1698 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1700 log_link_debug(link
, "Acquiring DHCPv6 lease");
1703 r
= dhcp6_request_prefix_delegation(link
);
1705 return log_link_warning_errno(link
, r
, "Failed to request DHCPv6 prefix delegation: %m");
1710 static int link_acquire_ipv4_conf(Link
*link
) {
1714 assert(link
->manager
);
1715 assert(link
->manager
->event
);
1717 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1718 assert(link
->ipv4ll
);
1720 log_link_debug(link
, "Acquiring IPv4 link-local address");
1722 r
= sd_ipv4ll_start(link
->ipv4ll
);
1724 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1727 if (link_dhcp4_enabled(link
)) {
1728 assert(link
->dhcp_client
);
1730 log_link_debug(link
, "Acquiring DHCPv4 lease");
1732 r
= sd_dhcp_client_start(link
->dhcp_client
);
1734 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1740 static int link_acquire_conf(Link
*link
) {
1745 r
= link_acquire_ipv4_conf(link
);
1749 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1750 r
= link_acquire_ipv6_conf(link
);
1755 if (link_lldp_emit_enabled(link
)) {
1756 r
= link_lldp_emit_start(link
);
1758 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1764 bool link_has_carrier(Link
*link
) {
1765 /* see Documentation/networking/operstates.txt in the kernel sources */
1767 if (link
->kernel_operstate
== IF_OPER_UP
)
1770 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1771 /* operstate may not be implemented, so fall back to flags */
1772 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1773 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1779 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1784 link
->setting_genmode
= false;
1786 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1789 r
= sd_netlink_message_get_errno(m
);
1791 log_link_message_warning_errno(link
, m
, r
, "Could not set address genmode for interface, ignoring");
1793 log_link_debug(link
, "Setting address genmode done.");
1795 if (link
->state
== LINK_STATE_INITIALIZED
) {
1796 r
= link_configure_continue(link
);
1798 link_enter_failed(link
);
1804 static int link_configure_addrgen_mode(Link
*link
) {
1805 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1806 uint8_t ipv6ll_mode
;
1810 assert(link
->network
);
1811 assert(link
->manager
);
1812 assert(link
->manager
->rtnl
);
1814 if (!socket_ipv6_is_supported() || link
->setting_genmode
)
1817 log_link_debug(link
, "Setting address genmode for link");
1819 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1821 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1823 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1825 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1827 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1829 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1831 if (!link_ipv6ll_enabled(link
))
1832 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1833 else if (link
->network
->ipv6ll_address_gen_mode
< 0) {
1834 r
= sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
);
1836 /* The file may not exist. And even if it exists, when stable_secret is unset,
1837 * reading the file fails with EIO. */
1838 log_link_debug_errno(link
, r
, "Failed to read sysctl property stable_secret: %m");
1840 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1842 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1844 ipv6ll_mode
= link
->network
->ipv6ll_address_gen_mode
;
1846 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1848 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1850 r
= sd_netlink_message_close_container(req
);
1852 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1854 r
= sd_netlink_message_close_container(req
);
1856 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1858 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1859 link_netlink_destroy_callback
, link
);
1861 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1864 link
->setting_genmode
= true;
1869 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1874 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1877 r
= sd_netlink_message_get_errno(m
);
1879 /* we warn but don't fail the link, as it may be brought up later */
1880 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
1885 static int link_up(Link
*link
) {
1886 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1890 assert(link
->network
);
1891 assert(link
->manager
);
1892 assert(link
->manager
->rtnl
);
1894 log_link_debug(link
, "Bringing link up");
1896 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1898 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1900 /* set it free if not enslaved with networkd */
1901 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1902 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1904 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1907 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1909 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1911 if (link
->network
->mac
) {
1912 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1914 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1917 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1918 link_netlink_destroy_callback
, link
);
1920 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1927 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1932 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1935 r
= sd_netlink_message_get_errno(m
);
1937 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
1942 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1943 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1947 assert(link
->manager
);
1948 assert(link
->manager
->rtnl
);
1950 log_link_debug(link
, "Bringing link down");
1952 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1953 RTM_SETLINK
, link
->ifindex
);
1955 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1957 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1959 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1961 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1962 callback
?: link_down_handler
,
1963 link_netlink_destroy_callback
, link
);
1965 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1972 static int link_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1977 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1980 r
= sd_netlink_message_get_errno(m
);
1982 log_link_message_warning_errno(link
, m
, r
, "Could not set group for the interface");
1987 static int link_set_group(Link
*link
) {
1988 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1992 assert(link
->network
);
1993 assert(link
->manager
);
1994 assert(link
->manager
->rtnl
);
1996 if (link
->network
->group
<= 0)
1999 log_link_debug(link
, "Setting group");
2001 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2003 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2005 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
2007 return log_link_error_errno(link
, r
, "Could not set link group: %m");
2009 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_group_handler
,
2010 link_netlink_destroy_callback
, link
);
2012 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2019 static int link_handle_bound_to_list(Link
*link
) {
2023 bool required_up
= false;
2024 bool link_is_up
= false;
2028 if (hashmap_isempty(link
->bound_to_links
))
2031 if (link
->flags
& IFF_UP
)
2034 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2035 if (link_has_carrier(l
)) {
2040 if (!required_up
&& link_is_up
) {
2041 r
= link_down(link
, NULL
);
2044 } else if (required_up
&& !link_is_up
) {
2053 static int link_handle_bound_by_list(Link
*link
) {
2060 if (hashmap_isempty(link
->bound_by_links
))
2063 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2064 r
= link_handle_bound_to_list(l
);
2072 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2078 if (link
== carrier
)
2081 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2084 r
= hashmap_ensure_allocated(h
, NULL
);
2088 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2095 static int link_new_bound_by_list(Link
*link
) {
2100 bool list_updated
= false;
2103 assert(link
->manager
);
2107 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2108 if (!carrier
->network
)
2111 if (strv_isempty(carrier
->network
->bind_carrier
))
2114 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
2115 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2119 list_updated
= true;
2126 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2127 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2131 link_dirty(carrier
);
2137 static int link_new_bound_to_list(Link
*link
) {
2142 bool list_updated
= false;
2145 assert(link
->manager
);
2150 if (strv_isempty(link
->network
->bind_carrier
))
2155 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2156 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
2157 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2161 list_updated
= true;
2168 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2169 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2173 link_dirty(carrier
);
2179 static int link_new_carrier_maps(Link
*link
) {
2182 r
= link_new_bound_by_list(link
);
2186 r
= link_handle_bound_by_list(link
);
2190 r
= link_new_bound_to_list(link
);
2194 r
= link_handle_bound_to_list(link
);
2201 static void link_free_bound_to_list(Link
*link
) {
2205 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2206 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2208 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2209 link_dirty(bound_to
);
2215 static void link_free_bound_by_list(Link
*link
) {
2219 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2220 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2222 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2223 link_dirty(bound_by
);
2224 link_handle_bound_to_list(bound_by
);
2231 static void link_free_carrier_maps(Link
*link
) {
2232 bool list_updated
= false;
2236 if (!hashmap_isempty(link
->bound_to_links
)) {
2237 link_free_bound_to_list(link
);
2238 list_updated
= true;
2241 if (!hashmap_isempty(link
->bound_by_links
)) {
2242 link_free_bound_by_list(link
);
2243 list_updated
= true;
2252 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2259 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2263 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
2271 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2276 if (!link
->manager
|| !netdev
)
2279 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2282 link_unref(set_remove(master
->slaves
, link
));
2285 static void link_detach_from_manager(Link
*link
) {
2286 if (!link
|| !link
->manager
)
2289 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2292 /* The following must be called at last. */
2293 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2297 void link_drop(Link
*link
) {
2298 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2301 link_set_state(link
, LINK_STATE_LINGER
);
2303 link_free_carrier_maps(link
);
2305 if (link
->network
) {
2306 link_drop_from_master(link
, link
->network
->bridge
);
2307 link_drop_from_master(link
, link
->network
->bond
);
2310 log_link_debug(link
, "Link removed");
2312 (void) unlink(link
->state_file
);
2313 link_detach_from_manager(link
);
2316 static int link_joined(Link
*link
) {
2320 assert(link
->network
);
2322 if (!hashmap_isempty(link
->bound_to_links
)) {
2323 r
= link_handle_bound_to_list(link
);
2326 } else if (!(link
->flags
& IFF_UP
)) {
2329 link_enter_failed(link
);
2334 if (link
->network
->bridge
) {
2335 r
= link_set_bridge(link
);
2337 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2339 r
= link_append_to_master(link
, link
->network
->bridge
);
2341 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2344 if (link
->network
->bond
) {
2345 r
= link_set_bond(link
);
2347 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2349 r
= link_append_to_master(link
, link
->network
->bond
);
2351 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2354 if (link
->network
->use_br_vlan
&&
2355 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2356 r
= link_set_bridge_vlan(link
);
2358 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2361 /* Skip setting up addresses until it gets carrier,
2362 or it would try to set addresses twice,
2363 which is bad for non-idempotent steps. */
2364 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2367 link_set_state(link
, LINK_STATE_CONFIGURING
);
2368 return link_request_set_addresses(link
);
2371 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2375 assert(link
->network
);
2376 assert(link
->enslaving
> 0);
2380 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2383 r
= sd_netlink_message_get_errno(m
);
2384 if (r
< 0 && r
!= -EEXIST
) {
2385 log_link_message_warning_errno(link
, m
, r
, "Could not join netdev");
2386 link_enter_failed(link
);
2390 log_link_debug(link
, "Joined netdev");
2392 if (link
->enslaving
== 0) {
2393 r
= link_joined(link
);
2395 link_enter_failed(link
);
2401 static int link_enter_join_netdev(Link
*link
) {
2407 assert(link
->network
);
2408 assert(link
->state
== LINK_STATE_INITIALIZED
);
2410 link_set_state(link
, LINK_STATE_CONFIGURING
);
2413 link
->enslaving
= 0;
2415 if (link
->network
->bond
) {
2416 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2417 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2418 return link_joined(link
);
2420 log_struct(LOG_DEBUG
,
2421 LOG_LINK_INTERFACE(link
),
2422 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2423 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2427 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2429 log_struct_errno(LOG_WARNING
, r
,
2430 LOG_LINK_INTERFACE(link
),
2431 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2432 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2433 link_enter_failed(link
);
2438 if (link
->network
->bridge
) {
2439 log_struct(LOG_DEBUG
,
2440 LOG_LINK_INTERFACE(link
),
2441 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2442 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2446 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2448 log_struct_errno(LOG_WARNING
, r
,
2449 LOG_LINK_INTERFACE(link
),
2450 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2451 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2452 link_enter_failed(link
);
2457 if (link
->network
->vrf
) {
2458 log_struct(LOG_DEBUG
,
2459 LOG_LINK_INTERFACE(link
),
2460 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2461 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2465 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2467 log_struct_errno(LOG_WARNING
, r
,
2468 LOG_LINK_INTERFACE(link
),
2469 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2470 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2471 link_enter_failed(link
);
2476 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2478 if (netdev
->ifindex
> 0)
2479 /* Assume already enslaved. */
2482 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2485 log_struct(LOG_DEBUG
,
2486 LOG_LINK_INTERFACE(link
),
2487 LOG_NETDEV_INTERFACE(netdev
),
2488 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2492 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2494 log_struct_errno(LOG_WARNING
, r
,
2495 LOG_LINK_INTERFACE(link
),
2496 LOG_NETDEV_INTERFACE(netdev
),
2497 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2498 link_enter_failed(link
);
2503 if (link
->enslaving
== 0)
2504 return link_joined(link
);
2509 static int link_set_ipv4_forward(Link
*link
) {
2512 if (!link_ipv4_forward_enabled(link
))
2515 /* We propagate the forwarding flag from one interface to the
2516 * global setting one way. This means: as long as at least one
2517 * interface was configured at any time that had IP forwarding
2518 * enabled the setting will stay on for good. We do this
2519 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2520 * somewhat in sync (see below). */
2522 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2524 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2529 static int link_set_ipv6_forward(Link
*link
) {
2532 if (!link_ipv6_forward_enabled(link
))
2535 /* On Linux, the IPv6 stack does not know a per-interface
2536 * packet forwarding setting: either packet forwarding is on
2537 * for all, or off for all. We hence don't bother with a
2538 * per-interface setting, but simply propagate the interface
2539 * flag, if it is set, to the global flag, one-way. Note that
2540 * while IPv4 would allow a per-interface flag, we expose the
2541 * same behaviour there and also propagate the setting from
2542 * one to all, to keep things simple (see above). */
2544 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2546 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2551 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2552 IPv6PrivacyExtensions s
;
2555 s
= link_ipv6_privacy_extensions(link
);
2559 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2561 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2566 static int link_set_ipv6_accept_ra(Link
*link
) {
2569 /* Make this a NOP if IPv6 is not available */
2570 if (!socket_ipv6_is_supported())
2573 if (link
->flags
& IFF_LOOPBACK
)
2579 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2581 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2586 static int link_set_ipv6_dad_transmits(Link
*link
) {
2589 /* Make this a NOP if IPv6 is not available */
2590 if (!socket_ipv6_is_supported())
2593 if (link
->flags
& IFF_LOOPBACK
)
2599 if (link
->network
->ipv6_dad_transmits
< 0)
2602 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2604 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2609 static int link_set_ipv6_hop_limit(Link
*link
) {
2612 /* Make this a NOP if IPv6 is not available */
2613 if (!socket_ipv6_is_supported())
2616 if (link
->flags
& IFF_LOOPBACK
)
2622 if (link
->network
->ipv6_hop_limit
< 0)
2625 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2627 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2632 static int link_set_ipv6_mtu(Link
*link
) {
2635 /* Make this a NOP if IPv6 is not available */
2636 if (!socket_ipv6_is_supported())
2639 if (link
->flags
& IFF_LOOPBACK
)
2642 if (link
->network
->ipv6_mtu
== 0)
2645 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2646 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2647 if (link
->network
->ipv6_mtu
< IPV6_MIN_MTU
) {
2648 log_link_notice(link
, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU
)" byte minimum required");
2649 link
->network
->ipv6_mtu
= IPV6_MIN_MTU
;
2652 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2654 if (link
->mtu
< link
->network
->ipv6_mtu
)
2655 log_link_warning(link
, "Cannot set IPv6 MTU %"PRIu32
" higher than device MTU %"PRIu32
,
2656 link
->network
->ipv6_mtu
, link
->mtu
);
2658 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2661 link
->ipv6_mtu_set
= true;
2666 static int link_set_ipv4_accept_local(Link
*link
) {
2669 if (link
->flags
& IFF_LOOPBACK
)
2672 if (link
->network
->ipv4_accept_local
< 0)
2675 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "accept_local", link
->network
->ipv4_accept_local
);
2677 log_link_warning_errno(link
, r
, "Cannot set IPv4 accept_local flag for interface: %m");
2682 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2683 Address
*net_address
;
2691 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2692 if (address_equal(net_address
, address
))
2694 else if (address
->family
== AF_INET6
&& net_address
->family
== AF_INET6
&&
2695 in_addr_equal(AF_INET6
, &address
->in_addr
, &net_address
->in_addr_peer
) > 0)
2701 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2702 Neighbor
*net_neighbor
;
2710 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2711 if (neighbor_equal(net_neighbor
, neighbor
))
2717 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2726 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2727 if (route_equal(net_route
, route
))
2733 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2740 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2743 /* Even when the address is leased from a DHCP server, networkd assign the address
2744 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2745 * corresponding routes with RTPROT_DHCP. */
2746 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2747 if (route
->protocol
!= RTPROT_DHCP
)
2750 if (address
->family
!= route
->family
)
2753 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2760 static int link_enumerate_ipv6_tentative_addresses(Link
*link
) {
2761 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2762 sd_netlink_message
*addr
;
2766 assert(link
->manager
);
2767 assert(link
->manager
->rtnl
);
2769 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, 0, AF_INET6
);
2773 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
2777 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
2778 unsigned char flags
;
2781 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
2783 log_link_warning_errno(link
, r
, "rtnl: invalid ifindex, ignoring: %m");
2785 } else if (link
->ifindex
!= ifindex
)
2788 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
2790 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
2792 } else if (!(flags
& IFA_F_TENTATIVE
))
2795 log_link_debug(link
, "Found tentative ipv6 link-local address");
2796 (void) manager_rtnl_process_address(link
->manager
->rtnl
, addr
, link
->manager
);
2802 static int link_drop_foreign_config(Link
*link
) {
2809 /* The kernel doesn't notify us about tentative addresses;
2810 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2811 if (!link_ipv6ll_enabled(link
)) {
2812 r
= link_enumerate_ipv6_tentative_addresses(link
);
2817 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2818 /* we consider IPv6LL addresses to be managed by the kernel */
2819 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2822 if (link_address_is_dynamic(link
, address
)) {
2823 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2825 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2828 if (link_is_static_address_configured(link
, address
)) {
2829 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2831 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2833 r
= address_remove(address
, link
, NULL
);
2839 SET_FOREACH(neighbor
, link
->neighbors_foreign
, i
) {
2840 if (link_is_neighbor_configured(link
, neighbor
)) {
2841 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2845 r
= neighbor_remove(neighbor
, link
, NULL
);
2851 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2852 /* do not touch routes managed by the kernel */
2853 if (route
->protocol
== RTPROT_KERNEL
)
2856 /* do not touch multicast route added by kernel */
2857 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2858 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2859 if (route
->protocol
== RTPROT_BOOT
&&
2860 route
->family
== AF_INET6
&&
2861 route
->dst_prefixlen
== 8 &&
2862 in_addr_equal(AF_INET6
, &route
->dst
, &(union in_addr_union
) { .in6
= {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}} }))
2865 if (route
->protocol
== RTPROT_STATIC
&& link
->network
&&
2866 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2869 if (route
->protocol
== RTPROT_DHCP
&& link
->network
&&
2870 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2873 if (link_is_static_route_configured(link
, route
)) {
2874 r
= route_add(link
, route
, NULL
);
2878 r
= route_remove(route
, link
, NULL
);
2887 static int link_drop_config(Link
*link
) {
2888 Address
*address
, *pool_address
;
2894 SET_FOREACH(address
, link
->addresses
, i
) {
2895 /* we consider IPv6LL addresses to be managed by the kernel */
2896 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2899 r
= address_remove(address
, link
, NULL
);
2903 /* If this address came from an address pool, clean up the pool */
2904 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2905 if (address_equal(address
, pool_address
)) {
2906 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2907 address_free(pool_address
);
2913 SET_FOREACH(neighbor
, link
->neighbors
, i
) {
2914 r
= neighbor_remove(neighbor
, link
, NULL
);
2919 SET_FOREACH(route
, link
->routes
, i
) {
2920 /* do not touch routes managed by the kernel */
2921 if (route
->protocol
== RTPROT_KERNEL
)
2924 r
= route_remove(route
, link
, NULL
);
2934 static int link_configure_ipv4_dad(Link
*link
) {
2939 assert(link
->network
);
2941 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
)
2942 if (address
->family
== AF_INET
&&
2943 FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
2944 r
= configure_ipv4_duplicate_address_detection(link
, address
);
2946 return log_link_error_errno(link
, r
, "Failed to configure IPv4ACD: %m");
2952 static int link_configure_traffic_control(Link
*link
) {
2957 link
->tc_configured
= false;
2958 link
->tc_messages
= 0;
2960 ORDERED_HASHMAP_FOREACH(tc
, link
->network
->tc_by_section
, i
) {
2961 r
= traffic_control_configure(link
, tc
);
2966 if (link
->tc_messages
== 0)
2967 link
->tc_configured
= true;
2969 log_link_debug(link
, "Configuring traffic control");
2974 static int link_configure_sr_iov(Link
*link
) {
2979 link
->sr_iov_configured
= false;
2980 link
->sr_iov_messages
= 0;
2982 ORDERED_HASHMAP_FOREACH(sr_iov
, link
->network
->sr_iov_by_section
, i
) {
2983 r
= sr_iov_configure(link
, sr_iov
);
2988 if (link
->sr_iov_messages
== 0)
2989 link
->sr_iov_configured
= true;
2991 log_link_debug(link
, "Configuring SR-IOV");
2996 static int link_configure(Link
*link
) {
3000 assert(link
->network
);
3001 assert(link
->state
== LINK_STATE_INITIALIZED
);
3003 r
= link_configure_traffic_control(link
);
3007 r
= link_configure_sr_iov(link
);
3011 if (link
->iftype
== ARPHRD_CAN
)
3012 return link_configure_can(link
);
3014 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
3015 * for this interface, then enable IPv6 */
3016 (void) link_update_ipv6_sysctl(link
);
3018 r
= link_set_proxy_arp(link
);
3022 r
= ipv6_proxy_ndp_addresses_configure(link
);
3026 r
= link_set_ipv4_forward(link
);
3030 r
= link_set_ipv6_forward(link
);
3034 r
= link_set_ipv6_privacy_extensions(link
);
3038 r
= link_set_ipv6_accept_ra(link
);
3042 r
= link_set_ipv6_dad_transmits(link
);
3046 r
= link_set_ipv6_hop_limit(link
);
3050 r
= link_set_ipv4_accept_local(link
);
3054 r
= link_set_flags(link
);
3058 r
= link_set_group(link
);
3062 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
3063 r
= ipv4ll_configure(link
);
3068 if (link_dhcp4_enabled(link
)) {
3069 r
= dhcp4_set_promote_secondaries(link
);
3073 r
= dhcp4_configure(link
);
3078 if (link_dhcp4_server_enabled(link
)) {
3079 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3083 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3088 if (link_dhcp6_enabled(link
) ||
3089 link_ipv6_accept_ra_enabled(link
)) {
3090 r
= dhcp6_configure(link
);
3095 if (link_ipv6_accept_ra_enabled(link
)) {
3096 r
= ndisc_configure(link
);
3101 if (link_radv_enabled(link
)) {
3102 r
= radv_configure(link
);
3107 if (link_lldp_rx_enabled(link
)) {
3108 r
= link_lldp_rx_configure(link
);
3113 r
= link_configure_mtu(link
);
3117 r
= link_configure_addrgen_mode(link
);
3121 r
= link_configure_ipv4_dad(link
);
3125 return link_configure_continue(link
);
3128 /* The configuration continues in this separate function, instead of
3129 * including this in the above link_configure() function, for two
3131 * 1) some devices reset the link when the mtu is set, which caused
3132 * an infinite loop here in networkd; see:
3133 * https://github.com/systemd/systemd/issues/6593
3134 * https://github.com/systemd/systemd/issues/9831
3135 * 2) if ipv6ll is disabled, then bringing the interface up must be
3136 * delayed until after we get confirmation from the kernel that
3137 * the addr_gen_mode parameter has been set (via netlink), see:
3138 * https://github.com/systemd/systemd/issues/13882
3140 static int link_configure_continue(Link
*link
) {
3144 assert(link
->network
);
3145 assert(link
->state
== LINK_STATE_INITIALIZED
);
3147 if (link
->setting_mtu
|| link
->setting_genmode
)
3150 /* Drop foreign config, but ignore loopback or critical devices.
3151 * We do not want to remove loopback address or addresses used for root NFS. */
3152 if (!(link
->flags
& IFF_LOOPBACK
) &&
3153 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
3154 r
= link_drop_foreign_config(link
);
3159 /* The kernel resets ipv6 mtu after changing device mtu;
3160 * we must set this here, after we've set device mtu */
3161 r
= link_set_ipv6_mtu(link
);
3165 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3166 r
= link_acquire_conf(link
);
3171 return link_enter_join_netdev(link
);
3174 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3177 if (duid
->raw_data_len
> 0)
3180 if (duid
->type
!= DUID_TYPE_UUID
)
3183 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3184 duid
->raw_data_len
= sizeof(sd_id128_t
);
3189 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3190 Manager
*manager
= userdata
;
3191 const sd_bus_error
*e
;
3201 e
= sd_bus_message_get_error(m
);
3203 log_error_errno(sd_bus_error_get_errno(e
),
3204 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3209 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3213 if (sz
!= sizeof(sd_id128_t
)) {
3214 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3218 memcpy(&manager
->product_uuid
, a
, sz
);
3219 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3220 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3222 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3225 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3228 r
= link_configure(link
);
3230 link_enter_failed(link
);
3233 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3235 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3236 * even if the method fails. */
3237 manager
->has_product_uuid
= true;
3242 static bool link_requires_uuid(Link
*link
) {
3246 assert(link
->manager
);
3247 assert(link
->network
);
3249 duid
= link_get_duid(link
);
3250 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3253 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3256 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3262 static int link_configure_duid(Link
*link
) {
3268 assert(link
->manager
);
3269 assert(link
->network
);
3272 duid
= link_get_duid(link
);
3274 if (!link_requires_uuid(link
))
3277 if (m
->has_product_uuid
) {
3278 (void) duid_set_uuid(duid
, m
->product_uuid
);
3282 if (!m
->links_requesting_uuid
) {
3283 r
= manager_request_product_uuid(m
, link
);
3288 log_link_warning_errno(link
, r
,
3289 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3293 r
= set_put(m
->links_requesting_uuid
, link
);
3299 r
= set_put(m
->duids_requesting_uuid
, duid
);
3307 static int link_reconfigure_internal(Link
*link
, sd_netlink_message
*m
, bool force
) {
3312 _cleanup_strv_free_
char **s
= NULL
;
3314 r
= sd_netlink_message_get_errno(m
);
3318 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3319 if (r
< 0 && r
!= -ENODATA
)
3322 strv_free_and_replace(link
->alternative_names
, s
);
3325 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3326 link
->ifname
, link
->alternative_names
, link
->driver
,
3327 &link
->mac
, &link
->permanent_mac
,
3328 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3330 link_enter_unmanaged(link
);
3332 } else if (r
== 0 && network
->unmanaged
) {
3333 link_enter_unmanaged(link
);
3338 if (link
->network
== network
&& !force
)
3341 log_link_info(link
, "Re-configuring with %s", network
->filename
);
3343 /* Dropping old .network file */
3344 r
= link_stop_clients(link
, false);
3348 if (link_dhcp4_server_enabled(link
))
3349 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3351 r
= link_drop_config(link
);
3355 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3356 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3357 r
= link_drop_foreign_config(link
);
3362 link_free_carrier_maps(link
);
3363 link_free_engines(link
);
3364 link
->network
= network_unref(link
->network
);
3366 /* Then, apply new .network file */
3367 r
= network_apply(network
, link
);
3371 r
= link_new_carrier_maps(link
);
3375 link_set_state(link
, LINK_STATE_INITIALIZED
);
3378 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3379 * link_configure() is called later asynchronously. */
3380 r
= link_configure_duid(link
);
3384 r
= link_configure(link
);
3391 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3394 r
= link_reconfigure_internal(link
, m
, false);
3396 link_enter_failed(link
);
3401 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3404 r
= link_reconfigure_internal(link
, m
, true);
3406 link_enter_failed(link
);
3411 int link_reconfigure(Link
*link
, bool force
) {
3412 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3415 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
3418 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3423 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
3424 force
? link_force_reconfigure_handler
: link_reconfigure_handler
,
3425 link_netlink_destroy_callback
, link
);
3434 static int link_initialized_and_synced(Link
*link
) {
3439 assert(link
->ifname
);
3440 assert(link
->manager
);
3442 /* We may get called either from the asynchronous netlink callback,
3443 * or directly for link_add() if running in a container. See link_add(). */
3444 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3447 log_link_debug(link
, "Link state is up-to-date");
3448 link_set_state(link
, LINK_STATE_INITIALIZED
);
3450 r
= link_new_bound_by_list(link
);
3454 r
= link_handle_bound_by_list(link
);
3458 if (!link
->network
) {
3459 r
= wifi_get_info(link
);
3463 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3464 link
->ifname
, link
->alternative_names
, link
->driver
,
3465 &link
->mac
, &link
->permanent_mac
,
3466 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3468 link_enter_unmanaged(link
);
3470 } else if (r
== 0 && network
->unmanaged
) {
3471 link_enter_unmanaged(link
);
3476 if (link
->flags
& IFF_LOOPBACK
) {
3477 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3478 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3480 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3481 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3483 if (network
->dhcp_server
)
3484 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3487 r
= network_apply(network
, link
);
3492 r
= link_new_bound_to_list(link
);
3496 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3497 * link_configure() is called later asynchronously. */
3498 r
= link_configure_duid(link
);
3502 r
= link_configure(link
);
3509 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3510 _cleanup_strv_free_
char **s
= NULL
;
3513 r
= sd_netlink_message_get_errno(m
);
3515 log_link_warning_errno(link
, r
, "Failed to wait for the interface to be initialized: %m");
3516 link_enter_failed(link
);
3520 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3521 if (r
< 0 && r
!= -ENODATA
) {
3522 link_enter_failed(link
);
3526 strv_free_and_replace(link
->alternative_names
, s
);
3528 r
= link_initialized_and_synced(link
);
3530 link_enter_failed(link
);
3534 int link_initialized(Link
*link
, sd_device
*device
) {
3535 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3539 assert(link
->manager
);
3540 assert(link
->manager
->rtnl
);
3543 if (link
->state
!= LINK_STATE_PENDING
)
3546 if (link
->sd_device
)
3549 log_link_debug(link
, "udev initialized link");
3550 link_set_state(link
, LINK_STATE_INITIALIZED
);
3552 link
->sd_device
= sd_device_ref(device
);
3554 /* udev has initialized the link, but we don't know if we have yet
3555 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3556 * when it returns we know that the pending NEWLINKs have already been
3557 * processed and that we are up-to-date */
3559 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3564 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3565 link_netlink_destroy_callback
, link
);
3574 static int link_load(Link
*link
) {
3575 _cleanup_free_
char *network_file
= NULL
,
3578 *dhcp4_address
= NULL
,
3579 *ipv4ll_address
= NULL
;
3580 union in_addr_union address
;
3585 r
= parse_env_file(NULL
, link
->state_file
,
3586 "NETWORK_FILE", &network_file
,
3587 "ADDRESSES", &addresses
,
3589 "DHCP4_ADDRESS", &dhcp4_address
,
3590 "IPV4LL_ADDRESS", &ipv4ll_address
);
3591 if (r
< 0 && r
!= -ENOENT
)
3592 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3599 suffix
= strrchr(network_file
, '.');
3601 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3602 goto network_file_fail
;
3606 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3608 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3609 goto network_file_fail
;
3612 r
= network_apply(network
, link
);
3614 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3619 for (const char *p
= addresses
; p
; ) {
3620 _cleanup_free_
char *address_str
= NULL
;
3621 char *prefixlen_str
;
3623 unsigned char prefixlen
;
3625 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3627 log_link_warning_errno(link
, r
, "failed to parse ADDRESSES: %m");
3631 prefixlen_str
= strchr(address_str
, '/');
3632 if (!prefixlen_str
) {
3633 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3636 *prefixlen_str
++ = '\0';
3638 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3640 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3644 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3646 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3650 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3652 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3655 for (const char *p
= routes
; p
; ) {
3656 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3657 _cleanup_(route_freep
) Route
*tmp
= NULL
;
3658 _cleanup_free_
char *route_str
= NULL
;
3659 char *prefixlen_str
;
3662 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3664 log_link_debug_errno(link
, r
, "failed to parse ROUTES: %m");
3668 prefixlen_str
= strchr(route_str
, '/');
3669 if (!prefixlen_str
) {
3670 log_link_debug(link
, "Failed to parse route %s", route_str
);
3673 *prefixlen_str
++ = '\0';
3675 r
= route_new(&tmp
);
3679 r
= sscanf(prefixlen_str
,
3680 "%hhu/%hhu/%"SCNu32
"/%"PRIu32
"/"USEC_FMT
,
3681 &tmp
->dst_prefixlen
,
3687 log_link_debug(link
,
3688 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3693 r
= in_addr_from_string_auto(route_str
, &tmp
->family
, &tmp
->dst
);
3695 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3699 r
= route_add(link
, tmp
, &route
);
3701 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3703 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3704 r
= sd_event_add_time(link
->manager
->event
, &expire
,
3705 clock_boottime_or_monotonic(),
3706 route
->lifetime
, 0, route_expire_handler
, route
);
3708 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3711 sd_event_source_unref(route
->expire
);
3712 route
->expire
= TAKE_PTR(expire
);
3715 if (dhcp4_address
) {
3716 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3718 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3719 goto dhcp4_address_fail
;
3722 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3724 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3726 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
3728 return log_link_error_errno(link
, r
, "Failed to attach DHCPv4 event: %m");
3730 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3732 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3737 if (ipv4ll_address
) {
3738 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3740 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3741 goto ipv4ll_address_fail
;
3744 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3746 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3748 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
3750 return log_link_error_errno(link
, r
, "Failed to attach IPv4LL event: %m");
3752 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3754 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3757 ipv4ll_address_fail
:
3762 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3763 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3764 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3773 r
= link_new(m
, message
, ret
);
3779 log_link_debug(link
, "Link %d added", link
->ifindex
);
3781 r
= link_load(link
);
3785 if (path_is_read_only_fs("/sys") <= 0) {
3786 /* udev should be around */
3787 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3788 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3790 log_link_warning_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
3794 r
= sd_device_get_is_initialized(device
);
3796 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
3801 log_link_debug(link
, "link pending udev initialization...");
3805 r
= device_is_renaming(device
);
3807 log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
3811 log_link_debug(link
, "Interface is being renamed, pending initialization.");
3815 r
= link_initialized(link
, device
);
3819 r
= link_initialized_and_synced(link
);
3826 link_enter_failed(link
);
3830 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3835 log_link_info(link
, "Gained IPv6LL");
3837 link
->ipv6ll_address
= *address
;
3838 link_check_ready(link
);
3840 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3841 r
= link_acquire_ipv6_conf(link
);
3843 link_enter_failed(link
);
3851 static int link_carrier_gained(Link
*link
) {
3856 r
= wifi_get_info(link
);
3860 r
= link_reconfigure_internal(link
, NULL
, false);
3862 link_enter_failed(link
);
3867 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3868 r
= link_acquire_conf(link
);
3870 link_enter_failed(link
);
3874 link_set_state(link
, LINK_STATE_CONFIGURING
);
3875 r
= link_request_set_addresses(link
);
3880 r
= link_handle_bound_by_list(link
);
3887 static int link_carrier_lost(Link
*link
) {
3892 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3895 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3896 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3897 * configuration and stop the clients as well. */
3898 if (link
->setting_mtu
)
3901 r
= link_stop_clients(link
, false);
3903 link_enter_failed(link
);
3907 if (link_dhcp4_server_enabled(link
))
3908 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3910 r
= link_drop_config(link
);
3914 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3915 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3916 r
= link_drop_foreign_config(link
);
3921 r
= link_handle_bound_by_list(link
);
3928 int link_carrier_reset(Link
*link
) {
3933 if (link_has_carrier(link
)) {
3934 r
= link_carrier_lost(link
);
3938 r
= link_carrier_gained(link
);
3942 log_link_info(link
, "Reset carrier");
3948 /* This is called every time an interface admin state changes to up;
3949 * specifically, when IFF_UP flag changes from unset to set */
3950 static int link_admin_state_up(Link
*link
) {
3953 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3954 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3955 * ipv6_mtu_set prevents this from trying to set it too early before
3956 * the link->network has been setup; we only need to reset it
3957 * here if we've already set it during normal initialization. */
3958 if (link
->ipv6_mtu_set
) {
3959 r
= link_set_ipv6_mtu(link
);
3967 int link_update(Link
*link
, sd_netlink_message
*m
) {
3968 _cleanup_strv_free_
char **s
= NULL
;
3969 struct ether_addr mac
;
3972 bool had_carrier
, carrier_gained
, carrier_lost
, link_was_admin_up
;
3976 assert(link
->ifname
);
3979 if (link
->state
== LINK_STATE_LINGER
) {
3980 log_link_info(link
, "Link re-added");
3981 link_set_state(link
, LINK_STATE_CONFIGURING
);
3983 r
= link_new_carrier_maps(link
);
3988 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3989 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3990 Manager
*manager
= link
->manager
;
3992 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3995 r
= link_add(manager
, m
, &link
);
4000 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
4002 strv_free_and_replace(link
->alternative_names
, s
);
4004 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
4005 if (r
>= 0 && mtu
> 0) {
4007 if (link
->original_mtu
== 0) {
4008 link
->original_mtu
= mtu
;
4009 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
4012 if (link
->dhcp_client
) {
4013 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
4016 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
4020 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
4022 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
4026 /* The kernel may broadcast NEWLINK messages without the MAC address
4027 set, simply ignore them. */
4028 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
4029 if (r
>= 0 && memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
, ETH_ALEN
) != 0) {
4031 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
, ETH_ALEN
);
4033 log_link_debug(link
, "Gained new MAC address: "
4034 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
4035 mac
.ether_addr_octet
[0],
4036 mac
.ether_addr_octet
[1],
4037 mac
.ether_addr_octet
[2],
4038 mac
.ether_addr_octet
[3],
4039 mac
.ether_addr_octet
[4],
4040 mac
.ether_addr_octet
[5]);
4043 bool restart
= sd_ipv4ll_is_running(link
->ipv4ll
) > 0;
4046 r
= sd_ipv4ll_stop(link
->ipv4ll
);
4048 return log_link_warning_errno(link
, r
, "Could not stop IPv4LL client: %m");
4051 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
4053 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
4056 r
= sd_ipv4ll_start(link
->ipv4ll
);
4058 return log_link_warning_errno(link
, r
, "Could not restart IPv4LL client: %m");
4062 if (link
->dhcp_client
) {
4063 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
4064 (const uint8_t *) &link
->mac
,
4068 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
4070 r
= dhcp4_set_client_identifier(link
);
4072 return log_link_warning_errno(link
, r
, "Could not set DHCP client identifier: %m");
4075 if (link
->dhcp6_client
) {
4076 const DUID
* duid
= link_get_duid(link
);
4077 bool restart
= sd_dhcp6_client_is_running(link
->dhcp6_client
) > 0;
4080 r
= sd_dhcp6_client_stop(link
->dhcp6_client
);
4082 return log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
4085 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
4086 (const uint8_t *) &link
->mac
,
4090 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
4092 if (link
->network
->iaid_set
) {
4093 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
, link
->network
->iaid
);
4095 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
4098 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
4100 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
4101 duid
->raw_data_len
);
4103 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
4106 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
4108 return log_link_warning_errno(link
, r
, "Could not restart DHCPv6 client: %m");
4113 bool restart
= sd_radv_is_running(link
->radv
);
4116 r
= sd_radv_stop(link
->radv
);
4118 return log_link_warning_errno(link
, r
, "Could not stop Router Advertisement: %m");
4121 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
4123 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
4126 r
= sd_radv_start(link
->radv
);
4128 return log_link_warning_errno(link
, r
, "Could not restart Router Advertisement: %m");
4133 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
4135 return log_link_warning_errno(link
, r
, "Could not update MAC for NDisc: %m");
4139 old_master
= link
->master_ifindex
;
4140 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
4142 link_was_admin_up
= link
->flags
& IFF_UP
;
4143 had_carrier
= link_has_carrier(link
);
4145 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
4149 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
4150 log_link_info(link
, "Link UP");
4152 r
= link_admin_state_up(link
);
4155 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
))
4156 log_link_info(link
, "Link DOWN");
4158 r
= link_update_lldp(link
);
4162 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
4163 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
4165 if (carrier_gained
) {
4166 log_link_info(link
, "Gained carrier");
4168 r
= link_carrier_gained(link
);
4171 } else if (carrier_lost
) {
4172 log_link_info(link
, "Lost carrier");
4174 r
= link_carrier_lost(link
);
4182 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
4190 if (hashmap_isempty(h
))
4194 HASHMAP_FOREACH(link
, h
, i
) {
4198 fprintf(f
, "%i", link
->ifindex
);
4205 static void link_save_dns(Link
*link
, FILE *f
, struct in_addr_full
**dns
, unsigned n_dns
, bool *space
) {
4206 for (unsigned j
= 0; j
< n_dns
; j
++) {
4209 if (dns
[j
]->ifindex
!= 0 && dns
[j
]->ifindex
!= link
->ifindex
)
4212 str
= in_addr_full_to_string(dns
[j
]);
4223 static void serialize_addresses(
4228 sd_dhcp_lease
*lease
,
4230 sd_dhcp_lease_server_type what
,
4231 sd_dhcp6_lease
*lease6
,
4233 int (*lease6_get_addr
)(sd_dhcp6_lease
*, const struct in6_addr
**),
4234 int (*lease6_get_fqdn
)(sd_dhcp6_lease
*, char ***)) {
4237 bool _space
= false;
4242 fprintf(f
, "%s=", lvalue
);
4243 fputstrv(f
, addresses
, NULL
, space
);
4245 if (lease
&& conditional
) {
4246 const struct in_addr
*lease_addresses
;
4248 r
= sd_dhcp_lease_get_servers(lease
, what
, &lease_addresses
);
4250 serialize_in_addrs(f
, lease_addresses
, r
, space
, in4_addr_is_non_local
);
4253 if (lease6
&& conditional6
&& lease6_get_addr
) {
4254 const struct in6_addr
*in6_addrs
;
4256 r
= lease6_get_addr(lease6
, &in6_addrs
);
4258 serialize_in6_addrs(f
, in6_addrs
, r
, space
);
4261 if (lease6
&& conditional6
&& lease6_get_fqdn
) {
4264 r
= lease6_get_fqdn(lease6
, &in6_hosts
);
4266 fputstrv(f
, in6_hosts
, NULL
, space
);
4273 int link_save(Link
*link
) {
4274 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
4275 _cleanup_free_
char *temp_path
= NULL
;
4276 _cleanup_fclose_
FILE *f
= NULL
;
4283 assert(link
->state_file
);
4284 assert(link
->lease_file
);
4285 assert(link
->manager
);
4287 if (link
->state
== LINK_STATE_LINGER
) {
4288 (void) unlink(link
->state_file
);
4292 link_lldp_save(link
);
4294 admin_state
= link_state_to_string(link
->state
);
4295 assert(admin_state
);
4297 oper_state
= link_operstate_to_string(link
->operstate
);
4300 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
4301 assert(carrier_state
);
4303 address_state
= link_address_state_to_string(link
->address_state
);
4304 assert(address_state
);
4306 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4310 (void) fchmod(fileno(f
), 0644);
4313 "# This is private data. Do not parse.\n"
4316 "CARRIER_STATE=%s\n"
4317 "ADDRESS_STATE=%s\n",
4318 admin_state
, oper_state
, carrier_state
, address_state
);
4320 if (link
->network
) {
4321 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
4322 const char *dhcp_domainname
= NULL
, *p
;
4325 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4326 yes_no(link
->network
->required_for_online
));
4328 LinkOperationalStateRange st
= link
->network
->required_operstate_for_online
;
4329 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4330 strempty(link_operstate_to_string(st
.min
)),
4331 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? ":" : "",
4332 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? strempty(link_operstate_to_string(st
.max
)) : "");
4334 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4336 /************************************************************/
4340 if (link
->n_dns
!= (unsigned) -1)
4341 link_save_dns(link
, f
, link
->dns
, link
->n_dns
, &space
);
4343 link_save_dns(link
, f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
4345 serialize_addresses(f
, NULL
, &space
,
4348 link
->network
->dhcp_use_dns
,
4351 link
->network
->dhcp6_use_dns
,
4352 sd_dhcp6_lease_get_dns
,
4355 /* Make sure to flush out old entries before we use the NDisc data */
4358 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4361 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
)
4362 serialize_in6_addrs(f
, &dd
->address
, 1, &space
);
4367 /************************************************************/
4369 serialize_addresses(f
, "NTP", NULL
,
4370 link
->ntp
?: link
->network
->ntp
,
4372 link
->network
->dhcp_use_ntp
,
4375 link
->network
->dhcp6_use_ntp
,
4376 sd_dhcp6_lease_get_ntp_addrs
,
4377 sd_dhcp6_lease_get_ntp_fqdn
);
4379 serialize_addresses(f
, "SIP", NULL
,
4382 link
->network
->dhcp_use_sip
,
4384 NULL
, false, NULL
, NULL
);
4386 /************************************************************/
4388 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4389 if (link
->dhcp_lease
) {
4390 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4391 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4393 if (link
->dhcp6_lease
)
4394 (void) sd_dhcp6_lease_get_domains(link
->dhcp6_lease
, &dhcp6_domains
);
4397 fputs("DOMAINS=", f
);
4399 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
4400 fputs_with_space(f
, p
, NULL
, &space
);
4402 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4403 if (dhcp_domainname
)
4404 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4406 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4408 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4411 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
4414 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4415 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4420 /************************************************************/
4422 fputs("ROUTE_DOMAINS=", f
);
4424 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
4425 fputs_with_space(f
, p
, NULL
, &space
);
4427 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4428 if (dhcp_domainname
)
4429 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4431 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4433 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4436 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4439 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4440 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4445 /************************************************************/
4447 fprintf(f
, "LLMNR=%s\n",
4448 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
4450 /************************************************************/
4452 fprintf(f
, "MDNS=%s\n",
4453 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
4455 /************************************************************/
4457 int dns_default_route
=
4458 link
->dns_default_route
>= 0 ? link
->dns_default_route
:
4459 link
->network
->dns_default_route
;
4460 if (dns_default_route
>= 0)
4461 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route
));
4463 /************************************************************/
4465 DnsOverTlsMode dns_over_tls_mode
=
4466 link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
? link
->dns_over_tls_mode
:
4467 link
->network
->dns_over_tls_mode
;
4468 if (dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4469 fprintf(f
, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode
));
4471 /************************************************************/
4473 DnssecMode dnssec_mode
=
4474 link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
? link
->dnssec_mode
:
4475 link
->network
->dnssec_mode
;
4476 if (dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4477 fprintf(f
, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode
));
4479 /************************************************************/
4481 Set
*nta_anchors
= link
->dnssec_negative_trust_anchors
;
4482 if (set_isempty(nta_anchors
))
4483 nta_anchors
= link
->network
->dnssec_negative_trust_anchors
;
4485 if (!set_isempty(nta_anchors
)) {
4488 fputs("DNSSEC_NTA=", f
);
4490 SET_FOREACH(n
, nta_anchors
, i
)
4491 fputs_with_space(f
, n
, NULL
, &space
);
4495 /************************************************************/
4497 fputs("ADDRESSES=", f
);
4499 SET_FOREACH(a
, link
->addresses
, i
) {
4500 _cleanup_free_
char *address_str
= NULL
;
4502 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4506 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4511 /************************************************************/
4513 fputs("ROUTES=", f
);
4515 SET_FOREACH(route
, link
->routes
, i
) {
4516 _cleanup_free_
char *route_str
= NULL
;
4518 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4522 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4523 space
? " " : "", route_str
,
4524 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4531 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4532 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4534 if (link
->dhcp_lease
) {
4535 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4543 (void) unlink(link
->lease_file
);
4546 struct in_addr address
;
4548 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4550 fputs("IPV4LL_ADDRESS=", f
);
4551 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4556 if (link
->dhcp6_client
) {
4557 _cleanup_free_
char *duid
= NULL
;
4560 r
= sd_dhcp6_client_get_iaid(link
->dhcp6_client
, &iaid
);
4562 fprintf(f
, "DHCP6_CLIENT_IAID=0x%x\n", iaid
);
4564 r
= sd_dhcp6_client_duid_as_string(link
->dhcp6_client
, &duid
);
4566 fprintf(f
, "DHCP6_CLIENT_DUID=%s\n", duid
);
4569 r
= fflush_and_check(f
);
4573 if (rename(temp_path
, link
->state_file
) < 0) {
4581 (void) unlink(link
->state_file
);
4583 (void) unlink(temp_path
);
4585 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4588 /* The serialized state in /run is no longer up-to-date. */
4589 void link_dirty(Link
*link
) {
4594 /* mark manager dirty as link is dirty */
4595 manager_dirty(link
->manager
);
4597 r
= set_ensure_put(&link
->manager
->dirty_links
, NULL
, link
);
4599 /* Ignore allocation errors and don't take another ref if the link was already dirty */
4604 /* The serialized state in /run is up-to-date */
4605 void link_clean(Link
*link
) {
4607 assert(link
->manager
);
4609 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4612 int link_save_and_clean(Link
*link
) {
4615 r
= link_save(link
);
4623 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4624 [LINK_STATE_PENDING
] = "pending",
4625 [LINK_STATE_INITIALIZED
] = "initialized",
4626 [LINK_STATE_CONFIGURING
] = "configuring",
4627 [LINK_STATE_CONFIGURED
] = "configured",
4628 [LINK_STATE_UNMANAGED
] = "unmanaged",
4629 [LINK_STATE_FAILED
] = "failed",
4630 [LINK_STATE_LINGER
] = "linger",
4633 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4635 int log_link_message_full_errno(Link
*link
, sd_netlink_message
*m
, int level
, int err
, const char *msg
) {
4636 const char *err_msg
= NULL
;
4638 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
4639 return log_link_full(link
, level
, err
,
4643 err_msg
&& !endswith(err_msg
, ".") ? "." : "",
4644 err_msg
? " " : "");