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 link
->dns
= mfree(link
->dns
);
670 link
->n_dns
= (unsigned) -1;
672 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
673 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
675 link
->dns_default_route
= -1;
676 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
677 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
678 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
679 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
681 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
684 static void link_free_engines(Link
*link
) {
688 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
689 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
690 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
691 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
693 link
->lldp
= sd_lldp_unref(link
->lldp
);
697 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
698 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
699 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
700 link
->radv
= sd_radv_unref(link
->radv
);
703 static Link
*link_free(Link
*link
) {
708 link_ntp_settings_clear(link
);
709 link_dns_settings_clear(link
);
711 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
712 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
714 link
->nexthops
= set_free_with_destructor(link
->nexthops
, nexthop_free
);
715 link
->nexthops_foreign
= set_free_with_destructor(link
->nexthops_foreign
, nexthop_free
);
717 link
->neighbors
= set_free_with_destructor(link
->neighbors
, neighbor_free
);
718 link
->neighbors_foreign
= set_free_with_destructor(link
->neighbors_foreign
, neighbor_free
);
720 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
721 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
723 while ((address
= link
->pool_addresses
)) {
724 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
725 address_free(address
);
728 link_lldp_emit_stop(link
);
729 link_free_engines(link
);
730 free(link
->lease_file
);
731 free(link
->lldp_file
);
734 strv_free(link
->alternative_names
);
739 (void) unlink(link
->state_file
);
740 free(link
->state_file
);
742 sd_device_unref(link
->sd_device
);
744 hashmap_free(link
->bound_to_links
);
745 hashmap_free(link
->bound_by_links
);
747 set_free_with_destructor(link
->slaves
, link_unref
);
749 network_unref(link
->network
);
754 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
756 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
763 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
772 void link_set_state(Link
*link
, LinkState state
) {
775 if (link
->state
== state
)
778 log_link_debug(link
, "State changed: %s -> %s",
779 link_state_to_string(link
->state
),
780 link_state_to_string(state
));
784 link_send_changed(link
, "AdministrativeState", NULL
);
787 static void link_enter_unmanaged(Link
*link
) {
790 link_set_state(link
, LINK_STATE_UNMANAGED
);
795 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
800 assert(link
->manager
);
801 assert(link
->manager
->event
);
803 dhcp4_release_old_lease(link
);
805 bool keep_dhcp
= may_keep_dhcp
&&
807 (link
->manager
->restarting
||
808 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
810 if (link
->dhcp_client
&& !keep_dhcp
) {
811 k
= sd_dhcp_client_stop(link
->dhcp_client
);
813 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
817 k
= sd_ipv4ll_stop(link
->ipv4ll
);
819 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
823 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
)
824 if (ad
->acd
&& sd_ipv4acd_is_running(ad
->acd
) == 0) {
825 k
= sd_ipv4acd_stop(ad
->acd
);
827 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
830 if (link
->dhcp6_client
) {
831 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
833 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
837 k
= sd_ndisc_stop(link
->ndisc
);
839 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
843 k
= sd_radv_stop(link
->radv
);
845 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
848 link_lldp_emit_stop(link
);
852 void link_enter_failed(Link
*link
) {
855 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
858 log_link_warning(link
, "Failed");
860 link_set_state(link
, LINK_STATE_FAILED
);
862 link_stop_clients(link
, false);
867 static int link_join_netdevs_after_configured(Link
*link
) {
872 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
873 if (netdev
->ifindex
> 0)
874 /* Assume already enslaved. */
877 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
880 log_struct(LOG_DEBUG
,
881 LOG_LINK_INTERFACE(link
),
882 LOG_NETDEV_INTERFACE(netdev
),
883 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
885 r
= netdev_join(netdev
, link
, NULL
);
887 return log_struct_errno(LOG_WARNING
, r
,
888 LOG_LINK_INTERFACE(link
),
889 LOG_NETDEV_INTERFACE(netdev
),
890 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
896 static void link_enter_configured(Link
*link
) {
898 assert(link
->network
);
900 if (link
->state
!= LINK_STATE_CONFIGURING
)
903 link_set_state(link
, LINK_STATE_CONFIGURED
);
905 (void) link_join_netdevs_after_configured(link
);
910 static int link_request_set_routing_policy_rule(Link
*link
) {
911 RoutingPolicyRule
*rule
, *rrule
= NULL
;
915 assert(link
->network
);
917 link
->routing_policy_rules_configured
= false;
919 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
920 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
923 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
927 r
= routing_policy_rule_configure(rule
, link
, NULL
);
929 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
931 link
->routing_policy_rule_messages
++;
934 routing_policy_rule_purge(link
->manager
, link
);
935 if (link
->routing_policy_rule_messages
== 0) {
936 link
->routing_policy_rules_configured
= true;
937 link_check_ready(link
);
939 log_link_debug(link
, "Setting routing policy rules");
940 link_set_state(link
, LINK_STATE_CONFIGURING
);
946 static int nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
950 assert(link
->nexthop_messages
> 0);
951 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
952 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
954 link
->nexthop_messages
--;
956 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
959 r
= sd_netlink_message_get_errno(m
);
960 if (r
< 0 && r
!= -EEXIST
) {
961 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
962 link_enter_failed(link
);
966 if (link
->nexthop_messages
== 0) {
967 log_link_debug(link
, "Nexthop set");
968 link
->static_nexthops_configured
= true;
969 link_check_ready(link
);
975 static int link_request_set_nexthop(Link
*link
) {
979 LIST_FOREACH(nexthops
, nh
, link
->network
->static_nexthops
) {
980 r
= nexthop_configure(nh
, link
, nexthop_handler
);
982 return log_link_warning_errno(link
, r
, "Could not set nexthop: %m");
984 link
->nexthop_messages
++;
987 if (link
->nexthop_messages
== 0) {
988 link
->static_nexthops_configured
= true;
989 link_check_ready(link
);
991 log_link_debug(link
, "Setting nexthop");
992 link_set_state(link
, LINK_STATE_CONFIGURING
);
998 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1002 assert(link
->route_messages
> 0);
1003 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1004 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1006 link
->route_messages
--;
1008 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1011 r
= sd_netlink_message_get_errno(m
);
1012 if (r
< 0 && r
!= -EEXIST
) {
1013 log_link_message_warning_errno(link
, m
, r
, "Could not set route");
1014 link_enter_failed(link
);
1018 if (link
->route_messages
== 0) {
1019 log_link_debug(link
, "Routes set");
1020 link
->static_routes_configured
= true;
1021 link_check_ready(link
);
1027 int link_request_set_routes(Link
*link
) {
1029 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
1030 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
1037 assert(link
->network
);
1038 assert(link
->addresses_configured
);
1039 assert(link
->address_messages
== 0);
1040 assert(link
->state
!= _LINK_STATE_INVALID
);
1042 link
->static_routes_configured
= false;
1043 link
->static_routes_ready
= false;
1045 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
1046 /* During configuring addresses, the link lost its carrier. As networkd is dropping
1047 * the addresses now, let's not configure the routes either. */
1050 r
= link_request_set_routing_policy_rule(link
);
1054 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
1055 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
1056 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
1057 if (rt
->gateway_from_dhcp
)
1060 if ((in_addr_is_null(rt
->family
, &rt
->gw
) && ordered_set_isempty(rt
->multipath_routes
)) != (phase
== PHASE_NON_GATEWAY
))
1063 r
= route_configure(rt
, link
, route_handler
);
1065 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
1067 link
->route_messages
++;
1070 if (link
->route_messages
== 0) {
1071 link
->static_routes_configured
= true;
1072 link_check_ready(link
);
1074 log_link_debug(link
, "Setting routes");
1075 link_set_state(link
, LINK_STATE_CONFIGURING
);
1081 void link_check_ready(Link
*link
) {
1088 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
)) {
1089 log_link_debug(link
, "%s(): link is in failed or linger state.", __func__
);
1096 if (!link
->addresses_configured
) {
1097 log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
1101 if (!link
->neighbors_configured
) {
1102 log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
1106 SET_FOREACH(a
, link
->addresses
, i
)
1107 if (!address_is_ready(a
)) {
1108 _cleanup_free_
char *str
= NULL
;
1110 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
1111 log_link_debug(link
, "%s(): an address %s/%d is not ready.", __func__
, strnull(str
), a
->prefixlen
);
1115 if (!link
->addresses_ready
) {
1116 link
->addresses_ready
= true;
1117 r
= link_request_set_routes(link
);
1119 link_enter_failed(link
);
1120 log_link_debug(link
, "%s(): static addresses are configured. Configuring static routes.", __func__
);
1124 if (!link
->static_routes_configured
) {
1125 log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
1129 if (!link
->static_routes_ready
) {
1130 link
->static_routes_ready
= true;
1131 r
= link_request_set_nexthop(link
);
1133 link_enter_failed(link
);
1134 log_link_debug(link
, "%s(): static routes are configured. Configuring static nexthops.", __func__
);
1138 if (!link
->static_nexthops_configured
) {
1139 log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
1143 if (!link
->routing_policy_rules_configured
) {
1144 log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
1148 if (!link
->tc_configured
) {
1149 log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
1153 if (!link
->sr_iov_configured
) {
1154 log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
1158 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1160 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !link
->ipv4ll_address
) {
1161 log_link_debug(link
, "%s(): IPv4LL is not configured.", __func__
);
1165 if (link_ipv6ll_enabled(link
) &&
1166 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1167 log_link_debug(link
, "%s(): IPv6LL is not configured.", __func__
);
1171 if (link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
) || dhcp6_get_prefix_delegation(link
) || link_ipv6_accept_ra_enabled(link
)) {
1172 if (!link
->dhcp4_configured
&&
1173 !(link
->dhcp6_address_configured
&& link
->dhcp6_route_configured
) &&
1174 !(link
->dhcp6_pd_address_configured
&& link
->dhcp6_pd_route_configured
) &&
1175 !(link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
) &&
1176 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
)) {
1177 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
1178 * an IPv4ll fallback address must be configured. */
1179 log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
1183 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",
1185 yes_no(link
->dhcp4_configured
),
1186 yes_no(link
->dhcp6_address_configured
),
1187 yes_no(link
->dhcp6_route_configured
),
1188 yes_no(link
->dhcp6_pd_address_configured
),
1189 yes_no(link
->dhcp6_pd_route_configured
),
1190 yes_no(link
->ndisc_addresses_configured
),
1191 yes_no(link
->ndisc_routes_configured
));
1195 if (link
->state
!= LINK_STATE_CONFIGURED
)
1196 link_enter_configured(link
);
1201 static int link_request_set_neighbors(Link
*link
) {
1206 assert(link
->network
);
1207 assert(link
->state
!= _LINK_STATE_INVALID
);
1209 link
->neighbors_configured
= false;
1211 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1212 r
= neighbor_configure(neighbor
, link
, NULL
);
1214 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1217 if (link
->neighbor_messages
== 0) {
1218 link
->neighbors_configured
= true;
1219 link_check_ready(link
);
1221 log_link_debug(link
, "Setting neighbors");
1222 link_set_state(link
, LINK_STATE_CONFIGURING
);
1228 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1234 assert(link
->ifname
);
1235 assert(link
->address_messages
> 0);
1236 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1237 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1239 link
->address_messages
--;
1241 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1244 r
= sd_netlink_message_get_errno(m
);
1245 if (r
< 0 && r
!= -EEXIST
) {
1246 log_link_message_warning_errno(link
, m
, r
, "Could not set address");
1247 link_enter_failed(link
);
1250 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1252 if (link
->address_messages
== 0) {
1253 log_link_debug(link
, "Addresses set");
1254 link
->addresses_configured
= true;
1255 link_check_ready(link
);
1261 static int link_set_bridge_fdb(Link
*link
) {
1262 FdbEntry
*fdb_entry
;
1265 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1266 r
= fdb_entry_configure(link
, fdb_entry
);
1268 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1274 static int link_request_set_addresses(Link
*link
) {
1275 AddressLabel
*label
;
1281 assert(link
->network
);
1282 assert(link
->state
!= _LINK_STATE_INVALID
);
1284 /* Reset all *_configured flags we are configuring. */
1285 link
->addresses_configured
= false;
1286 link
->addresses_ready
= false;
1287 link
->neighbors_configured
= false;
1288 link
->static_routes_configured
= false;
1289 link
->static_routes_ready
= false;
1290 link
->static_nexthops_configured
= false;
1291 link
->routing_policy_rules_configured
= false;
1293 r
= link_set_bridge_fdb(link
);
1297 r
= link_request_set_neighbors(link
);
1301 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1304 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1306 r
= address_configure(ad
, link
, address_handler
, update
);
1308 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1310 link
->address_messages
++;
1313 if (IN_SET(link
->network
->router_prefix_delegation
,
1314 RADV_PREFIX_DELEGATION_STATIC
,
1315 RADV_PREFIX_DELEGATION_BOTH
))
1316 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
1317 _cleanup_(address_freep
) Address
*address
= NULL
;
1322 r
= address_new(&address
);
1324 return log_link_error_errno(link
, r
, "Could not allocate address: %m");
1326 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1330 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1334 address
->family
= AF_INET6
;
1335 r
= address_configure(address
, link
, address_handler
, true);
1337 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1339 link
->address_messages
++;
1342 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1343 r
= address_label_configure(label
, link
, NULL
, false);
1345 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1347 link
->address_label_messages
++;
1350 /* now that we can figure out a default address for the dhcp server,
1352 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1353 r
= dhcp4_server_configure(link
);
1356 log_link_debug(link
, "Offering DHCPv4 leases");
1359 if (link
->address_messages
== 0) {
1360 link
->addresses_configured
= true;
1361 link_check_ready(link
);
1363 log_link_debug(link
, "Setting addresses");
1364 link_set_state(link
, LINK_STATE_CONFIGURING
);
1370 static int link_set_bridge_vlan(Link
*link
) {
1373 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1375 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1380 static int link_set_proxy_arp(Link
*link
) {
1383 if (!link_proxy_arp_enabled(link
))
1386 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1388 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1393 static int link_configure_continue(Link
*link
);
1395 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1400 assert(link
->ifname
);
1402 link
->setting_mtu
= false;
1404 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1407 r
= sd_netlink_message_get_errno(m
);
1409 log_link_message_warning_errno(link
, m
, r
, "Could not set MTU, ignoring");
1411 log_link_debug(link
, "Setting MTU done.");
1413 if (link
->state
== LINK_STATE_INITIALIZED
) {
1414 r
= link_configure_continue(link
);
1416 link_enter_failed(link
);
1422 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1423 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1427 assert(link
->manager
);
1428 assert(link
->manager
->rtnl
);
1430 if (mtu
== 0 || link
->setting_mtu
)
1433 if (link
->mtu
== mtu
)
1436 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1438 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1440 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1442 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1443 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1444 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1446 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1447 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes");
1452 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1454 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1456 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1457 link_netlink_destroy_callback
, link
);
1459 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1462 link
->setting_mtu
= true;
1467 static bool link_reduces_vlan_mtu(Link
*link
) {
1468 /* See netif_reduces_vlan_mtu() in kernel. */
1469 return streq_ptr(link
->kind
, "macsec");
1472 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1477 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1478 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1479 /* See vlan_dev_change_mtu() in kernel. */
1480 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1482 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1483 /* See macvlan_change_mtu() in kernel. */
1489 static int link_configure_mtu(Link
*link
) {
1493 assert(link
->network
);
1495 if (link
->network
->mtu
> 0)
1496 return link_set_mtu(link
, link
->network
->mtu
);
1498 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1499 if (link
->mtu
>= mtu
)
1502 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1503 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1506 return link_set_mtu(link
, mtu
);
1509 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1514 assert(link
->ifname
);
1516 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1519 r
= sd_netlink_message_get_errno(m
);
1521 log_link_message_warning_errno(link
, m
, r
, "Could not set link flags, ignoring");
1526 static int link_set_flags(Link
*link
) {
1527 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1528 unsigned ifi_change
= 0;
1529 unsigned ifi_flags
= 0;
1533 assert(link
->manager
);
1534 assert(link
->manager
->rtnl
);
1536 if (link
->flags
& IFF_LOOPBACK
)
1542 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1545 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1547 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1549 if (link
->network
->arp
>= 0) {
1550 ifi_change
|= IFF_NOARP
;
1551 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1554 if (link
->network
->multicast
>= 0) {
1555 ifi_change
|= IFF_MULTICAST
;
1556 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1559 if (link
->network
->allmulticast
>= 0) {
1560 ifi_change
|= IFF_ALLMULTI
;
1561 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1564 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1566 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1568 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1569 link_netlink_destroy_callback
, link
);
1571 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1578 static int link_acquire_ipv6_conf(Link
*link
) {
1583 if (link_ipv6_accept_ra_enabled(link
)) {
1584 assert(link
->ndisc
);
1586 log_link_debug(link
, "Discovering IPv6 routers");
1588 r
= sd_ndisc_start(link
->ndisc
);
1589 if (r
< 0 && r
!= -EBUSY
)
1590 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1593 if (link_radv_enabled(link
)) {
1595 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1597 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1599 r
= radv_emit_dns(link
);
1601 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1603 r
= sd_radv_start(link
->radv
);
1604 if (r
< 0 && r
!= -EBUSY
)
1605 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1608 if (link_dhcp6_enabled(link
) && IN_SET(link
->network
->dhcp6_without_ra
,
1609 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
,
1610 DHCP6_CLIENT_START_MODE_SOLICIT
)) {
1611 assert(link
->dhcp6_client
);
1612 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1614 r
= dhcp6_request_address(link
, link
->network
->dhcp6_without_ra
== DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
);
1615 if (r
< 0 && r
!= -EBUSY
)
1616 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1618 log_link_debug(link
, "Acquiring DHCPv6 lease");
1621 (void) dhcp6_request_prefix_delegation(link
);
1626 static int link_acquire_ipv4_conf(Link
*link
) {
1630 assert(link
->manager
);
1631 assert(link
->manager
->event
);
1633 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1634 assert(link
->ipv4ll
);
1636 log_link_debug(link
, "Acquiring IPv4 link-local address");
1638 r
= sd_ipv4ll_start(link
->ipv4ll
);
1640 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1643 if (link_dhcp4_enabled(link
)) {
1644 assert(link
->dhcp_client
);
1646 log_link_debug(link
, "Acquiring DHCPv4 lease");
1648 r
= sd_dhcp_client_start(link
->dhcp_client
);
1650 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1656 static int link_acquire_conf(Link
*link
) {
1661 r
= link_acquire_ipv4_conf(link
);
1665 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1666 r
= link_acquire_ipv6_conf(link
);
1671 if (link_lldp_emit_enabled(link
)) {
1672 r
= link_lldp_emit_start(link
);
1674 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1680 bool link_has_carrier(Link
*link
) {
1681 /* see Documentation/networking/operstates.txt in the kernel sources */
1683 if (link
->kernel_operstate
== IF_OPER_UP
)
1686 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1687 /* operstate may not be implemented, so fall back to flags */
1688 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1689 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1695 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1700 link
->setting_genmode
= false;
1702 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1705 r
= sd_netlink_message_get_errno(m
);
1707 log_link_message_warning_errno(link
, m
, r
, "Could not set address genmode for interface, ignoring");
1709 log_link_debug(link
, "Setting address genmode done.");
1711 if (link
->state
== LINK_STATE_INITIALIZED
) {
1712 r
= link_configure_continue(link
);
1714 link_enter_failed(link
);
1720 static int link_configure_addrgen_mode(Link
*link
) {
1721 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1722 uint8_t ipv6ll_mode
;
1726 assert(link
->network
);
1727 assert(link
->manager
);
1728 assert(link
->manager
->rtnl
);
1730 if (!socket_ipv6_is_supported() || link
->setting_genmode
)
1733 log_link_debug(link
, "Setting address genmode for link");
1735 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1737 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1739 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1741 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1743 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1745 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1747 if (!link_ipv6ll_enabled(link
))
1748 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1749 else if (link
->network
->ipv6ll_address_gen_mode
< 0) {
1750 r
= sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
);
1752 /* The file may not exist. And even if it exists, when stable_secret is unset,
1753 * reading the file fails with EIO. */
1754 log_link_debug_errno(link
, r
, "Failed to read sysctl property stable_secret: %m");
1756 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1758 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1760 ipv6ll_mode
= link
->network
->ipv6ll_address_gen_mode
;
1762 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1764 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1766 r
= sd_netlink_message_close_container(req
);
1768 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1770 r
= sd_netlink_message_close_container(req
);
1772 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1774 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1775 link_netlink_destroy_callback
, link
);
1777 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1780 link
->setting_genmode
= true;
1785 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1790 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1793 r
= sd_netlink_message_get_errno(m
);
1795 /* we warn but don't fail the link, as it may be brought up later */
1796 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
1801 static int link_up(Link
*link
) {
1802 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1806 assert(link
->network
);
1807 assert(link
->manager
);
1808 assert(link
->manager
->rtnl
);
1810 log_link_debug(link
, "Bringing link up");
1812 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1814 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1816 /* set it free if not enslaved with networkd */
1817 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1818 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1820 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1823 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1825 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1827 if (link
->network
->mac
) {
1828 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1830 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1833 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1834 link_netlink_destroy_callback
, link
);
1836 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1843 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1848 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1851 r
= sd_netlink_message_get_errno(m
);
1853 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
1858 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1859 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1863 assert(link
->manager
);
1864 assert(link
->manager
->rtnl
);
1866 log_link_debug(link
, "Bringing link down");
1868 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1869 RTM_SETLINK
, link
->ifindex
);
1871 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1873 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1875 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1877 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1878 callback
?: link_down_handler
,
1879 link_netlink_destroy_callback
, link
);
1881 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1888 static int link_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1893 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1896 r
= sd_netlink_message_get_errno(m
);
1898 log_link_message_warning_errno(link
, m
, r
, "Could not set group for the interface");
1903 static int link_set_group(Link
*link
) {
1904 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1908 assert(link
->network
);
1909 assert(link
->manager
);
1910 assert(link
->manager
->rtnl
);
1912 if (link
->network
->group
<= 0)
1915 log_link_debug(link
, "Setting group");
1917 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1919 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1921 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
1923 return log_link_error_errno(link
, r
, "Could not set link group: %m");
1925 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_group_handler
,
1926 link_netlink_destroy_callback
, link
);
1928 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1935 static int link_handle_bound_to_list(Link
*link
) {
1939 bool required_up
= false;
1940 bool link_is_up
= false;
1944 if (hashmap_isempty(link
->bound_to_links
))
1947 if (link
->flags
& IFF_UP
)
1950 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1951 if (link_has_carrier(l
)) {
1956 if (!required_up
&& link_is_up
) {
1957 r
= link_down(link
, NULL
);
1960 } else if (required_up
&& !link_is_up
) {
1969 static int link_handle_bound_by_list(Link
*link
) {
1976 if (hashmap_isempty(link
->bound_by_links
))
1979 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1980 r
= link_handle_bound_to_list(l
);
1988 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1994 if (link
== carrier
)
1997 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2000 r
= hashmap_ensure_allocated(h
, NULL
);
2004 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2011 static int link_new_bound_by_list(Link
*link
) {
2016 bool list_updated
= false;
2019 assert(link
->manager
);
2023 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2024 if (!carrier
->network
)
2027 if (strv_isempty(carrier
->network
->bind_carrier
))
2030 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
2031 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2035 list_updated
= true;
2042 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2043 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2047 link_dirty(carrier
);
2053 static int link_new_bound_to_list(Link
*link
) {
2058 bool list_updated
= false;
2061 assert(link
->manager
);
2066 if (strv_isempty(link
->network
->bind_carrier
))
2071 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2072 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
2073 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2077 list_updated
= true;
2084 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2085 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2089 link_dirty(carrier
);
2095 static int link_new_carrier_maps(Link
*link
) {
2098 r
= link_new_bound_by_list(link
);
2102 r
= link_handle_bound_by_list(link
);
2106 r
= link_new_bound_to_list(link
);
2110 r
= link_handle_bound_to_list(link
);
2117 static void link_free_bound_to_list(Link
*link
) {
2121 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2122 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2124 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2125 link_dirty(bound_to
);
2131 static void link_free_bound_by_list(Link
*link
) {
2135 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2136 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2138 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2139 link_dirty(bound_by
);
2140 link_handle_bound_to_list(bound_by
);
2147 static void link_free_carrier_maps(Link
*link
) {
2148 bool list_updated
= false;
2152 if (!hashmap_isempty(link
->bound_to_links
)) {
2153 link_free_bound_to_list(link
);
2154 list_updated
= true;
2157 if (!hashmap_isempty(link
->bound_by_links
)) {
2158 link_free_bound_by_list(link
);
2159 list_updated
= true;
2168 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2175 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2179 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
2187 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2192 if (!link
->manager
|| !netdev
)
2195 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2198 link_unref(set_remove(master
->slaves
, link
));
2201 static void link_detach_from_manager(Link
*link
) {
2202 if (!link
|| !link
->manager
)
2205 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2208 /* The following must be called at last. */
2209 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2213 void link_drop(Link
*link
) {
2214 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2217 link_set_state(link
, LINK_STATE_LINGER
);
2219 link_free_carrier_maps(link
);
2221 if (link
->network
) {
2222 link_drop_from_master(link
, link
->network
->bridge
);
2223 link_drop_from_master(link
, link
->network
->bond
);
2226 log_link_debug(link
, "Link removed");
2228 (void) unlink(link
->state_file
);
2229 link_detach_from_manager(link
);
2232 static int link_joined(Link
*link
) {
2236 assert(link
->network
);
2238 if (!hashmap_isempty(link
->bound_to_links
)) {
2239 r
= link_handle_bound_to_list(link
);
2242 } else if (!(link
->flags
& IFF_UP
)) {
2245 link_enter_failed(link
);
2250 if (link
->network
->bridge
) {
2251 r
= link_set_bridge(link
);
2253 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2255 r
= link_append_to_master(link
, link
->network
->bridge
);
2257 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2260 if (link
->network
->bond
) {
2261 r
= link_set_bond(link
);
2263 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2265 r
= link_append_to_master(link
, link
->network
->bond
);
2267 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2270 if (link
->network
->use_br_vlan
&&
2271 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2272 r
= link_set_bridge_vlan(link
);
2274 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2277 /* Skip setting up addresses until it gets carrier,
2278 or it would try to set addresses twice,
2279 which is bad for non-idempotent steps. */
2280 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2283 link_set_state(link
, LINK_STATE_CONFIGURING
);
2284 return link_request_set_addresses(link
);
2287 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2291 assert(link
->network
);
2292 assert(link
->enslaving
> 0);
2296 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2299 r
= sd_netlink_message_get_errno(m
);
2300 if (r
< 0 && r
!= -EEXIST
) {
2301 log_link_message_warning_errno(link
, m
, r
, "Could not join netdev");
2302 link_enter_failed(link
);
2306 log_link_debug(link
, "Joined netdev");
2308 if (link
->enslaving
== 0) {
2309 r
= link_joined(link
);
2311 link_enter_failed(link
);
2317 static int link_enter_join_netdev(Link
*link
) {
2323 assert(link
->network
);
2324 assert(link
->state
== LINK_STATE_INITIALIZED
);
2326 link_set_state(link
, LINK_STATE_CONFIGURING
);
2329 link
->enslaving
= 0;
2331 if (link
->network
->bond
) {
2332 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2333 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2334 return link_joined(link
);
2336 log_struct(LOG_DEBUG
,
2337 LOG_LINK_INTERFACE(link
),
2338 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2339 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2343 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2345 log_struct_errno(LOG_WARNING
, r
,
2346 LOG_LINK_INTERFACE(link
),
2347 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2348 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2349 link_enter_failed(link
);
2354 if (link
->network
->bridge
) {
2355 log_struct(LOG_DEBUG
,
2356 LOG_LINK_INTERFACE(link
),
2357 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2358 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2362 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2364 log_struct_errno(LOG_WARNING
, r
,
2365 LOG_LINK_INTERFACE(link
),
2366 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2367 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2368 link_enter_failed(link
);
2373 if (link
->network
->vrf
) {
2374 log_struct(LOG_DEBUG
,
2375 LOG_LINK_INTERFACE(link
),
2376 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2377 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2381 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2383 log_struct_errno(LOG_WARNING
, r
,
2384 LOG_LINK_INTERFACE(link
),
2385 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2386 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2387 link_enter_failed(link
);
2392 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2394 if (netdev
->ifindex
> 0)
2395 /* Assume already enslaved. */
2398 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2401 log_struct(LOG_DEBUG
,
2402 LOG_LINK_INTERFACE(link
),
2403 LOG_NETDEV_INTERFACE(netdev
),
2404 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2408 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2410 log_struct_errno(LOG_WARNING
, r
,
2411 LOG_LINK_INTERFACE(link
),
2412 LOG_NETDEV_INTERFACE(netdev
),
2413 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2414 link_enter_failed(link
);
2419 if (link
->enslaving
== 0)
2420 return link_joined(link
);
2425 static int link_set_ipv4_forward(Link
*link
) {
2428 if (!link_ipv4_forward_enabled(link
))
2431 /* We propagate the forwarding flag from one interface to the
2432 * global setting one way. This means: as long as at least one
2433 * interface was configured at any time that had IP forwarding
2434 * enabled the setting will stay on for good. We do this
2435 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2436 * somewhat in sync (see below). */
2438 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2440 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2445 static int link_set_ipv6_forward(Link
*link
) {
2448 if (!link_ipv6_forward_enabled(link
))
2451 /* On Linux, the IPv6 stack does not know a per-interface
2452 * packet forwarding setting: either packet forwarding is on
2453 * for all, or off for all. We hence don't bother with a
2454 * per-interface setting, but simply propagate the interface
2455 * flag, if it is set, to the global flag, one-way. Note that
2456 * while IPv4 would allow a per-interface flag, we expose the
2457 * same behaviour there and also propagate the setting from
2458 * one to all, to keep things simple (see above). */
2460 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2462 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2467 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2468 IPv6PrivacyExtensions s
;
2471 s
= link_ipv6_privacy_extensions(link
);
2475 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2477 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2482 static int link_set_ipv6_accept_ra(Link
*link
) {
2485 /* Make this a NOP if IPv6 is not available */
2486 if (!socket_ipv6_is_supported())
2489 if (link
->flags
& IFF_LOOPBACK
)
2495 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2497 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2502 static int link_set_ipv6_dad_transmits(Link
*link
) {
2505 /* Make this a NOP if IPv6 is not available */
2506 if (!socket_ipv6_is_supported())
2509 if (link
->flags
& IFF_LOOPBACK
)
2515 if (link
->network
->ipv6_dad_transmits
< 0)
2518 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2520 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2525 static int link_set_ipv6_hop_limit(Link
*link
) {
2528 /* Make this a NOP if IPv6 is not available */
2529 if (!socket_ipv6_is_supported())
2532 if (link
->flags
& IFF_LOOPBACK
)
2538 if (link
->network
->ipv6_hop_limit
< 0)
2541 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2543 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2548 static int link_set_ipv6_mtu(Link
*link
) {
2551 /* Make this a NOP if IPv6 is not available */
2552 if (!socket_ipv6_is_supported())
2555 if (link
->flags
& IFF_LOOPBACK
)
2558 if (link
->network
->ipv6_mtu
== 0)
2561 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2562 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2563 if (link
->network
->ipv6_mtu
< IPV6_MIN_MTU
) {
2564 log_link_notice(link
, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU
)" byte minimum required");
2565 link
->network
->ipv6_mtu
= IPV6_MIN_MTU
;
2568 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2570 if (link
->mtu
< link
->network
->ipv6_mtu
)
2571 log_link_warning(link
, "Cannot set IPv6 MTU %"PRIu32
" higher than device MTU %"PRIu32
,
2572 link
->network
->ipv6_mtu
, link
->mtu
);
2574 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2577 link
->ipv6_mtu_set
= true;
2582 static int link_set_ipv4_accept_local(Link
*link
) {
2585 if (link
->flags
& IFF_LOOPBACK
)
2588 if (link
->network
->ipv4_accept_local
< 0)
2591 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "accept_local", link
->network
->ipv4_accept_local
);
2593 log_link_warning_errno(link
, r
, "Cannot set IPv4 accept_local flag for interface: %m");
2598 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2599 Address
*net_address
;
2607 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2608 if (address_equal(net_address
, address
))
2614 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2615 Neighbor
*net_neighbor
;
2623 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2624 if (neighbor_equal(net_neighbor
, neighbor
))
2630 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2639 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2640 if (route_equal(net_route
, route
))
2646 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2653 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2656 /* Even when the address is leased from a DHCP server, networkd assign the address
2657 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2658 * corresponding routes with RTPROT_DHCP. */
2659 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2660 if (route
->protocol
!= RTPROT_DHCP
)
2663 if (address
->family
!= route
->family
)
2666 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2673 static int link_enumerate_ipv6_tentative_addresses(Link
*link
) {
2674 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2675 sd_netlink_message
*addr
;
2679 assert(link
->manager
);
2680 assert(link
->manager
->rtnl
);
2682 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, 0, AF_INET6
);
2686 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
2690 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
2691 unsigned char flags
;
2694 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
2696 log_link_warning_errno(link
, r
, "rtnl: invalid ifindex, ignoring: %m");
2698 } else if (link
->ifindex
!= ifindex
)
2701 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
2703 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
2705 } else if (!(flags
& IFA_F_TENTATIVE
))
2708 log_link_debug(link
, "Found tentative ipv6 link-local address");
2709 (void) manager_rtnl_process_address(link
->manager
->rtnl
, addr
, link
->manager
);
2715 static int link_drop_foreign_config(Link
*link
) {
2722 /* The kernel doesn't notify us about tentative addresses;
2723 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2724 if (!link_ipv6ll_enabled(link
)) {
2725 r
= link_enumerate_ipv6_tentative_addresses(link
);
2730 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2731 /* we consider IPv6LL addresses to be managed by the kernel */
2732 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2735 if (link_address_is_dynamic(link
, address
)) {
2736 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2738 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2741 if (link_is_static_address_configured(link
, address
)) {
2742 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2744 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2746 r
= address_remove(address
, link
, NULL
);
2752 SET_FOREACH(neighbor
, link
->neighbors_foreign
, i
) {
2753 if (link_is_neighbor_configured(link
, neighbor
)) {
2754 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2758 r
= neighbor_remove(neighbor
, link
, NULL
);
2764 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2765 /* do not touch routes managed by the kernel */
2766 if (route
->protocol
== RTPROT_KERNEL
)
2769 /* do not touch multicast route added by kernel */
2770 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2771 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2772 if (route
->protocol
== RTPROT_BOOT
&&
2773 route
->family
== AF_INET6
&&
2774 route
->dst_prefixlen
== 8 &&
2775 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 }}} }))
2778 if (route
->protocol
== RTPROT_STATIC
&& link
->network
&&
2779 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2782 if (route
->protocol
== RTPROT_DHCP
&& link
->network
&&
2783 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2786 if (link_is_static_route_configured(link
, route
)) {
2787 r
= route_add(link
, route
, NULL
);
2791 r
= route_remove(route
, link
, NULL
);
2800 static int link_drop_config(Link
*link
) {
2801 Address
*address
, *pool_address
;
2807 SET_FOREACH(address
, link
->addresses
, i
) {
2808 /* we consider IPv6LL addresses to be managed by the kernel */
2809 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2812 r
= address_remove(address
, link
, NULL
);
2816 /* If this address came from an address pool, clean up the pool */
2817 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2818 if (address_equal(address
, pool_address
)) {
2819 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2820 address_free(pool_address
);
2826 SET_FOREACH(neighbor
, link
->neighbors
, i
) {
2827 r
= neighbor_remove(neighbor
, link
, NULL
);
2832 SET_FOREACH(route
, link
->routes
, i
) {
2833 /* do not touch routes managed by the kernel */
2834 if (route
->protocol
== RTPROT_KERNEL
)
2837 r
= route_remove(route
, link
, NULL
);
2847 static int link_configure_ipv4_dad(Link
*link
) {
2852 assert(link
->network
);
2854 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
)
2855 if (address
->family
== AF_INET
&&
2856 FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
2857 r
= configure_ipv4_duplicate_address_detection(link
, address
);
2859 return log_link_error_errno(link
, r
, "Failed to configure IPv4ACD: %m");
2865 static int link_configure_traffic_control(Link
*link
) {
2870 link
->tc_configured
= false;
2871 link
->tc_messages
= 0;
2873 ORDERED_HASHMAP_FOREACH(tc
, link
->network
->tc_by_section
, i
) {
2874 r
= traffic_control_configure(link
, tc
);
2879 if (link
->tc_messages
== 0)
2880 link
->tc_configured
= true;
2882 log_link_debug(link
, "Configuring traffic control");
2887 static int link_configure_sr_iov(Link
*link
) {
2892 link
->sr_iov_configured
= false;
2893 link
->sr_iov_messages
= 0;
2895 ORDERED_HASHMAP_FOREACH(sr_iov
, link
->network
->sr_iov_by_section
, i
) {
2896 r
= sr_iov_configure(link
, sr_iov
);
2901 if (link
->sr_iov_messages
== 0)
2902 link
->sr_iov_configured
= true;
2904 log_link_debug(link
, "Configuring SR-IOV");
2909 static int link_configure(Link
*link
) {
2913 assert(link
->network
);
2914 assert(link
->state
== LINK_STATE_INITIALIZED
);
2916 r
= link_configure_traffic_control(link
);
2920 r
= link_configure_sr_iov(link
);
2924 if (link
->iftype
== ARPHRD_CAN
)
2925 return link_configure_can(link
);
2927 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2928 * for this interface, then enable IPv6 */
2929 (void) link_update_ipv6_sysctl(link
);
2931 r
= link_set_proxy_arp(link
);
2935 r
= ipv6_proxy_ndp_addresses_configure(link
);
2939 r
= link_set_ipv4_forward(link
);
2943 r
= link_set_ipv6_forward(link
);
2947 r
= link_set_ipv6_privacy_extensions(link
);
2951 r
= link_set_ipv6_accept_ra(link
);
2955 r
= link_set_ipv6_dad_transmits(link
);
2959 r
= link_set_ipv6_hop_limit(link
);
2963 r
= link_set_ipv4_accept_local(link
);
2967 r
= link_set_flags(link
);
2971 r
= link_set_group(link
);
2975 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2976 r
= ipv4ll_configure(link
);
2981 if (link_dhcp4_enabled(link
)) {
2982 r
= dhcp4_set_promote_secondaries(link
);
2986 r
= dhcp4_configure(link
);
2991 if (link_dhcp4_server_enabled(link
)) {
2992 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2996 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3001 if (link_dhcp6_enabled(link
) ||
3002 link_ipv6_accept_ra_enabled(link
)) {
3003 r
= dhcp6_configure(link
);
3008 if (link_ipv6_accept_ra_enabled(link
)) {
3009 r
= ndisc_configure(link
);
3014 if (link_radv_enabled(link
)) {
3015 r
= radv_configure(link
);
3020 if (link_lldp_rx_enabled(link
)) {
3021 r
= link_lldp_rx_configure(link
);
3026 r
= link_configure_mtu(link
);
3030 r
= link_configure_addrgen_mode(link
);
3034 r
= link_configure_ipv4_dad(link
);
3038 return link_configure_continue(link
);
3041 /* The configuration continues in this separate function, instead of
3042 * including this in the above link_configure() function, for two
3044 * 1) some devices reset the link when the mtu is set, which caused
3045 * an infinite loop here in networkd; see:
3046 * https://github.com/systemd/systemd/issues/6593
3047 * https://github.com/systemd/systemd/issues/9831
3048 * 2) if ipv6ll is disabled, then bringing the interface up must be
3049 * delayed until after we get confirmation from the kernel that
3050 * the addr_gen_mode parameter has been set (via netlink), see:
3051 * https://github.com/systemd/systemd/issues/13882
3053 static int link_configure_continue(Link
*link
) {
3057 assert(link
->network
);
3058 assert(link
->state
== LINK_STATE_INITIALIZED
);
3060 if (link
->setting_mtu
|| link
->setting_genmode
)
3063 /* Drop foreign config, but ignore loopback or critical devices.
3064 * We do not want to remove loopback address or addresses used for root NFS. */
3065 if (!(link
->flags
& IFF_LOOPBACK
) &&
3066 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
3067 r
= link_drop_foreign_config(link
);
3072 /* The kernel resets ipv6 mtu after changing device mtu;
3073 * we must set this here, after we've set device mtu */
3074 r
= link_set_ipv6_mtu(link
);
3078 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3079 r
= link_acquire_conf(link
);
3084 return link_enter_join_netdev(link
);
3087 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3090 if (duid
->raw_data_len
> 0)
3093 if (duid
->type
!= DUID_TYPE_UUID
)
3096 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3097 duid
->raw_data_len
= sizeof(sd_id128_t
);
3102 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3103 Manager
*manager
= userdata
;
3104 const sd_bus_error
*e
;
3114 e
= sd_bus_message_get_error(m
);
3116 log_error_errno(sd_bus_error_get_errno(e
),
3117 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3122 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3126 if (sz
!= sizeof(sd_id128_t
)) {
3127 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3131 memcpy(&manager
->product_uuid
, a
, sz
);
3132 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3133 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3135 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3138 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3141 r
= link_configure(link
);
3143 link_enter_failed(link
);
3146 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3148 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3149 * even if the method fails. */
3150 manager
->has_product_uuid
= true;
3155 static bool link_requires_uuid(Link
*link
) {
3159 assert(link
->manager
);
3160 assert(link
->network
);
3162 duid
= link_get_duid(link
);
3163 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3166 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3169 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3175 static int link_configure_duid(Link
*link
) {
3181 assert(link
->manager
);
3182 assert(link
->network
);
3185 duid
= link_get_duid(link
);
3187 if (!link_requires_uuid(link
))
3190 if (m
->has_product_uuid
) {
3191 (void) duid_set_uuid(duid
, m
->product_uuid
);
3195 if (!m
->links_requesting_uuid
) {
3196 r
= manager_request_product_uuid(m
, link
);
3201 log_link_warning_errno(link
, r
,
3202 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3206 r
= set_put(m
->links_requesting_uuid
, link
);
3212 r
= set_put(m
->duids_requesting_uuid
, duid
);
3220 static int link_reconfigure_internal(Link
*link
, sd_netlink_message
*m
, bool force
) {
3225 _cleanup_strv_free_
char **s
= NULL
;
3227 r
= sd_netlink_message_get_errno(m
);
3231 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3232 if (r
< 0 && r
!= -ENODATA
)
3235 strv_free_and_replace(link
->alternative_names
, s
);
3238 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3239 link
->ifname
, link
->alternative_names
, link
->driver
,
3240 &link
->mac
, &link
->permanent_mac
,
3241 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3243 link_enter_unmanaged(link
);
3245 } else if (r
== 0 && network
->unmanaged
) {
3246 link_enter_unmanaged(link
);
3251 if (link
->network
== network
&& !force
)
3254 log_link_info(link
, "Re-configuring with %s", network
->filename
);
3256 /* Dropping old .network file */
3257 r
= link_stop_clients(link
, false);
3261 if (link_dhcp4_server_enabled(link
))
3262 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3264 r
= link_drop_config(link
);
3268 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3269 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3270 r
= link_drop_foreign_config(link
);
3275 link_free_carrier_maps(link
);
3276 link_free_engines(link
);
3277 link
->network
= network_unref(link
->network
);
3279 /* Then, apply new .network file */
3280 r
= network_apply(network
, link
);
3284 r
= link_new_carrier_maps(link
);
3288 link_set_state(link
, LINK_STATE_INITIALIZED
);
3291 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3292 * link_configure() is called later asynchronously. */
3293 r
= link_configure_duid(link
);
3297 r
= link_configure(link
);
3304 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3307 r
= link_reconfigure_internal(link
, m
, false);
3309 link_enter_failed(link
);
3314 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3317 r
= link_reconfigure_internal(link
, m
, true);
3319 link_enter_failed(link
);
3324 int link_reconfigure(Link
*link
, bool force
) {
3325 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3328 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
3331 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3336 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
3337 force
? link_force_reconfigure_handler
: link_reconfigure_handler
,
3338 link_netlink_destroy_callback
, link
);
3347 static int link_initialized_and_synced(Link
*link
) {
3352 assert(link
->ifname
);
3353 assert(link
->manager
);
3355 /* We may get called either from the asynchronous netlink callback,
3356 * or directly for link_add() if running in a container. See link_add(). */
3357 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3360 log_link_debug(link
, "Link state is up-to-date");
3361 link_set_state(link
, LINK_STATE_INITIALIZED
);
3363 r
= link_new_bound_by_list(link
);
3367 r
= link_handle_bound_by_list(link
);
3371 if (!link
->network
) {
3372 r
= wifi_get_info(link
);
3376 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3377 link
->ifname
, link
->alternative_names
, link
->driver
,
3378 &link
->mac
, &link
->permanent_mac
,
3379 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3381 link_enter_unmanaged(link
);
3383 } else if (r
== 0 && network
->unmanaged
) {
3384 link_enter_unmanaged(link
);
3389 if (link
->flags
& IFF_LOOPBACK
) {
3390 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3391 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3393 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3394 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3396 if (network
->dhcp_server
)
3397 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3400 r
= network_apply(network
, link
);
3405 r
= link_new_bound_to_list(link
);
3409 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3410 * link_configure() is called later asynchronously. */
3411 r
= link_configure_duid(link
);
3415 r
= link_configure(link
);
3422 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3423 _cleanup_strv_free_
char **s
= NULL
;
3426 r
= sd_netlink_message_get_errno(m
);
3428 log_link_warning_errno(link
, r
, "Failed to wait for the interface to be initialized: %m");
3429 link_enter_failed(link
);
3433 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3434 if (r
< 0 && r
!= -ENODATA
) {
3435 link_enter_failed(link
);
3439 strv_free_and_replace(link
->alternative_names
, s
);
3441 r
= link_initialized_and_synced(link
);
3443 link_enter_failed(link
);
3447 int link_initialized(Link
*link
, sd_device
*device
) {
3448 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3452 assert(link
->manager
);
3453 assert(link
->manager
->rtnl
);
3456 if (link
->state
!= LINK_STATE_PENDING
)
3459 if (link
->sd_device
)
3462 log_link_debug(link
, "udev initialized link");
3463 link_set_state(link
, LINK_STATE_INITIALIZED
);
3465 link
->sd_device
= sd_device_ref(device
);
3467 /* udev has initialized the link, but we don't know if we have yet
3468 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3469 * when it returns we know that the pending NEWLINKs have already been
3470 * processed and that we are up-to-date */
3472 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3477 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3478 link_netlink_destroy_callback
, link
);
3487 static int link_load(Link
*link
) {
3488 _cleanup_free_
char *network_file
= NULL
,
3491 *dhcp4_address
= NULL
,
3492 *ipv4ll_address
= NULL
;
3493 union in_addr_union address
;
3498 r
= parse_env_file(NULL
, link
->state_file
,
3499 "NETWORK_FILE", &network_file
,
3500 "ADDRESSES", &addresses
,
3502 "DHCP4_ADDRESS", &dhcp4_address
,
3503 "IPV4LL_ADDRESS", &ipv4ll_address
);
3504 if (r
< 0 && r
!= -ENOENT
)
3505 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3512 suffix
= strrchr(network_file
, '.');
3514 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3515 goto network_file_fail
;
3519 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3521 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3522 goto network_file_fail
;
3525 r
= network_apply(network
, link
);
3527 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3532 for (const char *p
= addresses
; p
; ) {
3533 _cleanup_free_
char *address_str
= NULL
;
3534 char *prefixlen_str
;
3536 unsigned char prefixlen
;
3538 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3540 log_link_warning_errno(link
, r
, "failed to parse ADDRESSES: %m");
3544 prefixlen_str
= strchr(address_str
, '/');
3545 if (!prefixlen_str
) {
3546 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3549 *prefixlen_str
++ = '\0';
3551 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3553 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3557 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3559 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3563 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3565 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3568 for (const char *p
= routes
; p
; ) {
3569 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3570 _cleanup_(route_freep
) Route
*tmp
= NULL
;
3571 _cleanup_free_
char *route_str
= NULL
;
3572 char *prefixlen_str
;
3575 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3577 log_link_debug_errno(link
, r
, "failed to parse ROUTES: %m");
3581 prefixlen_str
= strchr(route_str
, '/');
3582 if (!prefixlen_str
) {
3583 log_link_debug(link
, "Failed to parse route %s", route_str
);
3586 *prefixlen_str
++ = '\0';
3588 r
= route_new(&tmp
);
3592 r
= sscanf(prefixlen_str
,
3593 "%hhu/%hhu/%"SCNu32
"/%"PRIu32
"/"USEC_FMT
,
3594 &tmp
->dst_prefixlen
,
3600 log_link_debug(link
,
3601 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3606 r
= in_addr_from_string_auto(route_str
, &tmp
->family
, &tmp
->dst
);
3608 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3612 r
= route_add(link
, tmp
, &route
);
3614 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3616 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3617 r
= sd_event_add_time(link
->manager
->event
, &expire
,
3618 clock_boottime_or_monotonic(),
3619 route
->lifetime
, 0, route_expire_handler
, route
);
3621 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3624 sd_event_source_unref(route
->expire
);
3625 route
->expire
= TAKE_PTR(expire
);
3628 if (dhcp4_address
) {
3629 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3631 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3632 goto dhcp4_address_fail
;
3635 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3637 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3639 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
3641 return log_link_error_errno(link
, r
, "Failed to attach DHCPv4 event: %m");
3643 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3645 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3650 if (ipv4ll_address
) {
3651 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3653 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3654 goto ipv4ll_address_fail
;
3657 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3659 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3661 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
3663 return log_link_error_errno(link
, r
, "Failed to attach IPv4LL event: %m");
3665 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3667 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3670 ipv4ll_address_fail
:
3675 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3676 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3677 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3686 r
= link_new(m
, message
, ret
);
3692 log_link_debug(link
, "Link %d added", link
->ifindex
);
3694 r
= link_load(link
);
3698 if (path_is_read_only_fs("/sys") <= 0) {
3699 /* udev should be around */
3700 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3701 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3703 log_link_warning_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
3707 r
= sd_device_get_is_initialized(device
);
3709 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
3714 log_link_debug(link
, "link pending udev initialization...");
3718 r
= device_is_renaming(device
);
3720 log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
3724 log_link_debug(link
, "Interface is being renamed, pending initialization.");
3728 r
= link_initialized(link
, device
);
3732 r
= link_initialized_and_synced(link
);
3739 link_enter_failed(link
);
3743 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3748 log_link_info(link
, "Gained IPv6LL");
3750 link
->ipv6ll_address
= *address
;
3751 link_check_ready(link
);
3753 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3754 r
= link_acquire_ipv6_conf(link
);
3756 link_enter_failed(link
);
3764 static int link_carrier_gained(Link
*link
) {
3769 r
= wifi_get_info(link
);
3773 r
= link_reconfigure_internal(link
, NULL
, false);
3775 link_enter_failed(link
);
3780 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3781 r
= link_acquire_conf(link
);
3783 link_enter_failed(link
);
3787 link_set_state(link
, LINK_STATE_CONFIGURING
);
3788 r
= link_request_set_addresses(link
);
3793 r
= link_handle_bound_by_list(link
);
3800 static int link_carrier_lost(Link
*link
) {
3805 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3808 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3809 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3810 * configuration and stop the clients as well. */
3811 if (link
->setting_mtu
)
3814 r
= link_stop_clients(link
, false);
3816 link_enter_failed(link
);
3820 if (link_dhcp4_server_enabled(link
))
3821 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3823 r
= link_drop_config(link
);
3827 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3828 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3829 r
= link_drop_foreign_config(link
);
3834 r
= link_handle_bound_by_list(link
);
3841 int link_carrier_reset(Link
*link
) {
3846 if (link_has_carrier(link
)) {
3847 r
= link_carrier_lost(link
);
3851 r
= link_carrier_gained(link
);
3855 log_link_info(link
, "Reset carrier");
3861 /* This is called every time an interface admin state changes to up;
3862 * specifically, when IFF_UP flag changes from unset to set */
3863 static int link_admin_state_up(Link
*link
) {
3866 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3867 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3868 * ipv6_mtu_set prevents this from trying to set it too early before
3869 * the link->network has been setup; we only need to reset it
3870 * here if we've already set it during normal initialization. */
3871 if (link
->ipv6_mtu_set
) {
3872 r
= link_set_ipv6_mtu(link
);
3880 int link_update(Link
*link
, sd_netlink_message
*m
) {
3881 _cleanup_strv_free_
char **s
= NULL
;
3882 struct ether_addr mac
;
3885 bool had_carrier
, carrier_gained
, carrier_lost
, link_was_admin_up
;
3889 assert(link
->ifname
);
3892 if (link
->state
== LINK_STATE_LINGER
) {
3893 log_link_info(link
, "Link re-added");
3894 link_set_state(link
, LINK_STATE_CONFIGURING
);
3896 r
= link_new_carrier_maps(link
);
3901 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3902 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3903 Manager
*manager
= link
->manager
;
3905 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3908 r
= link_add(manager
, m
, &link
);
3913 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3915 strv_free_and_replace(link
->alternative_names
, s
);
3917 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3918 if (r
>= 0 && mtu
> 0) {
3920 if (link
->original_mtu
== 0) {
3921 link
->original_mtu
= mtu
;
3922 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3925 if (link
->dhcp_client
) {
3926 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3929 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3933 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3935 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3939 /* The kernel may broadcast NEWLINK messages without the MAC address
3940 set, simply ignore them. */
3941 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3943 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3946 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3949 log_link_debug(link
, "MAC address: "
3950 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3951 mac
.ether_addr_octet
[0],
3952 mac
.ether_addr_octet
[1],
3953 mac
.ether_addr_octet
[2],
3954 mac
.ether_addr_octet
[3],
3955 mac
.ether_addr_octet
[4],
3956 mac
.ether_addr_octet
[5]);
3959 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3961 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3964 if (link
->dhcp_client
) {
3965 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3966 (const uint8_t *) &link
->mac
,
3970 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3972 r
= dhcp4_set_client_identifier(link
);
3977 if (link
->dhcp6_client
) {
3978 const DUID
* duid
= link_get_duid(link
);
3980 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3981 (const uint8_t *) &link
->mac
,
3985 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3987 if (link
->network
->iaid_set
) {
3988 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3989 link
->network
->iaid
);
3991 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3994 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3996 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3997 duid
->raw_data_len
);
3999 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
4003 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
4005 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
4009 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
4011 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
4016 old_master
= link
->master_ifindex
;
4017 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
4019 link_was_admin_up
= link
->flags
& IFF_UP
;
4020 had_carrier
= link_has_carrier(link
);
4022 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
4026 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
4027 log_link_info(link
, "Link UP");
4029 r
= link_admin_state_up(link
);
4032 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
))
4033 log_link_info(link
, "Link DOWN");
4035 r
= link_update_lldp(link
);
4039 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
4040 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
4042 if (carrier_gained
) {
4043 log_link_info(link
, "Gained carrier");
4045 r
= link_carrier_gained(link
);
4048 } else if (carrier_lost
) {
4049 log_link_info(link
, "Lost carrier");
4051 r
= link_carrier_lost(link
);
4059 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
4067 if (hashmap_isempty(h
))
4071 HASHMAP_FOREACH(link
, h
, i
) {
4075 fprintf(f
, "%i", link
->ifindex
);
4082 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
4083 for (unsigned j
= 0; j
< n_dns
; j
++) {
4084 _cleanup_free_
char *b
= NULL
;
4087 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
4089 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4100 static void serialize_addresses(
4105 sd_dhcp_lease
*lease
,
4107 sd_dhcp_lease_server_type what
,
4108 sd_dhcp6_lease
*lease6
,
4110 int (*lease6_get_addr
)(sd_dhcp6_lease
*, const struct in6_addr
**),
4111 int (*lease6_get_fqdn
)(sd_dhcp6_lease
*, char ***)) {
4114 bool _space
= false;
4119 fprintf(f
, "%s=", lvalue
);
4120 fputstrv(f
, addresses
, NULL
, space
);
4122 if (lease
&& conditional
) {
4123 const struct in_addr
*lease_addresses
;
4125 r
= sd_dhcp_lease_get_servers(lease
, what
, &lease_addresses
);
4127 serialize_in_addrs(f
, lease_addresses
, r
, space
, in4_addr_is_non_local
);
4130 if (lease6
&& conditional6
&& lease6_get_addr
) {
4131 const struct in6_addr
*in6_addrs
;
4133 r
= lease6_get_addr(lease6
, &in6_addrs
);
4135 serialize_in6_addrs(f
, in6_addrs
, r
, space
);
4138 if (lease6
&& conditional6
&& lease6_get_fqdn
) {
4141 r
= lease6_get_fqdn(lease6
, &in6_hosts
);
4143 fputstrv(f
, in6_hosts
, NULL
, space
);
4150 int link_save(Link
*link
) {
4151 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
4152 _cleanup_free_
char *temp_path
= NULL
;
4153 _cleanup_fclose_
FILE *f
= NULL
;
4160 assert(link
->state_file
);
4161 assert(link
->lease_file
);
4162 assert(link
->manager
);
4164 if (link
->state
== LINK_STATE_LINGER
) {
4165 (void) unlink(link
->state_file
);
4169 link_lldp_save(link
);
4171 admin_state
= link_state_to_string(link
->state
);
4172 assert(admin_state
);
4174 oper_state
= link_operstate_to_string(link
->operstate
);
4177 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
4178 assert(carrier_state
);
4180 address_state
= link_address_state_to_string(link
->address_state
);
4181 assert(address_state
);
4183 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4187 (void) fchmod(fileno(f
), 0644);
4190 "# This is private data. Do not parse.\n"
4193 "CARRIER_STATE=%s\n"
4194 "ADDRESS_STATE=%s\n",
4195 admin_state
, oper_state
, carrier_state
, address_state
);
4197 if (link
->network
) {
4198 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
4199 const char *dhcp_domainname
= NULL
, *p
;
4200 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
4203 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4204 yes_no(link
->network
->required_for_online
));
4206 LinkOperationalStateRange st
= link
->network
->required_operstate_for_online
;
4207 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4208 strempty(link_operstate_to_string(st
.min
)),
4209 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? ":" : "",
4210 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? strempty(link_operstate_to_string(st
.max
)) : "");
4212 if (link
->dhcp6_client
) {
4213 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4214 if (r
< 0 && r
!= -ENOMSG
)
4215 log_link_debug_errno(link
, r
, "Failed to get DHCPv6 lease: %m");
4218 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4220 /************************************************************/
4224 if (link
->n_dns
!= (unsigned) -1)
4225 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
4227 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
4229 serialize_addresses(f
, NULL
, &space
,
4232 link
->network
->dhcp_use_dns
,
4235 link
->network
->dhcp6_use_dns
,
4236 sd_dhcp6_lease_get_dns
,
4239 /* Make sure to flush out old entries before we use the NDISC data */
4242 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4245 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
)
4246 serialize_in6_addrs(f
, &dd
->address
, 1, &space
);
4251 /************************************************************/
4253 serialize_addresses(f
, "NTP", NULL
,
4254 link
->ntp
?: link
->network
->ntp
,
4256 link
->network
->dhcp_use_ntp
,
4259 link
->network
->dhcp6_use_ntp
,
4260 sd_dhcp6_lease_get_ntp_addrs
,
4261 sd_dhcp6_lease_get_ntp_fqdn
);
4263 serialize_addresses(f
, "SIP", NULL
,
4266 link
->network
->dhcp_use_sip
,
4268 NULL
, false, NULL
, NULL
);
4270 /************************************************************/
4272 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4273 if (link
->dhcp_lease
) {
4274 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4275 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4278 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4281 fputs("DOMAINS=", f
);
4283 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
4284 fputs_with_space(f
, p
, NULL
, &space
);
4286 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4287 if (dhcp_domainname
)
4288 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4290 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4292 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4295 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
4298 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4299 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4304 /************************************************************/
4306 fputs("ROUTE_DOMAINS=", f
);
4308 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
4309 fputs_with_space(f
, p
, NULL
, &space
);
4311 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4312 if (dhcp_domainname
)
4313 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4315 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4317 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4320 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4323 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4324 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4329 /************************************************************/
4331 fprintf(f
, "LLMNR=%s\n",
4332 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
4334 /************************************************************/
4336 fprintf(f
, "MDNS=%s\n",
4337 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
4339 /************************************************************/
4341 int dns_default_route
=
4342 link
->dns_default_route
>= 0 ? link
->dns_default_route
:
4343 link
->network
->dns_default_route
;
4344 if (dns_default_route
>= 0)
4345 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route
));
4347 /************************************************************/
4349 DnsOverTlsMode dns_over_tls_mode
=
4350 link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
? link
->dns_over_tls_mode
:
4351 link
->network
->dns_over_tls_mode
;
4352 if (dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4353 fprintf(f
, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode
));
4355 /************************************************************/
4357 DnssecMode dnssec_mode
=
4358 link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
? link
->dnssec_mode
:
4359 link
->network
->dnssec_mode
;
4360 if (dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4361 fprintf(f
, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode
));
4363 /************************************************************/
4365 Set
*nta_anchors
= link
->dnssec_negative_trust_anchors
;
4366 if (set_isempty(nta_anchors
))
4367 nta_anchors
= link
->network
->dnssec_negative_trust_anchors
;
4369 if (!set_isempty(nta_anchors
)) {
4372 fputs("DNSSEC_NTA=", f
);
4374 SET_FOREACH(n
, nta_anchors
, i
)
4375 fputs_with_space(f
, n
, NULL
, &space
);
4379 /************************************************************/
4381 fputs("ADDRESSES=", f
);
4383 SET_FOREACH(a
, link
->addresses
, i
) {
4384 _cleanup_free_
char *address_str
= NULL
;
4386 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4390 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4395 /************************************************************/
4397 fputs("ROUTES=", f
);
4399 SET_FOREACH(route
, link
->routes
, i
) {
4400 _cleanup_free_
char *route_str
= NULL
;
4402 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4406 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4407 space
? " " : "", route_str
,
4408 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4415 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4416 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4418 if (link
->dhcp_lease
) {
4419 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4427 (void) unlink(link
->lease_file
);
4430 struct in_addr address
;
4432 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4434 fputs("IPV4LL_ADDRESS=", f
);
4435 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4440 if (link
->dhcp6_client
) {
4441 _cleanup_free_
char *duid
= NULL
;
4444 r
= sd_dhcp6_client_get_iaid(link
->dhcp6_client
, &iaid
);
4446 fprintf(f
, "DHCP6_CLIENT_IAID=0x%x\n", iaid
);
4448 r
= sd_dhcp6_client_duid_as_string(link
->dhcp6_client
, &duid
);
4450 fprintf(f
, "DHCP6_CLIENT_DUID=%s\n", duid
);
4453 r
= fflush_and_check(f
);
4457 if (rename(temp_path
, link
->state_file
) < 0) {
4465 (void) unlink(link
->state_file
);
4467 (void) unlink(temp_path
);
4469 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4472 /* The serialized state in /run is no longer up-to-date. */
4473 void link_dirty(Link
*link
) {
4478 /* mark manager dirty as link is dirty */
4479 manager_dirty(link
->manager
);
4481 r
= set_ensure_put(&link
->manager
->dirty_links
, NULL
, link
);
4483 /* Ignore allocation errors and don't take another ref if the link was already dirty */
4488 /* The serialized state in /run is up-to-date */
4489 void link_clean(Link
*link
) {
4491 assert(link
->manager
);
4493 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4496 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4497 [LINK_STATE_PENDING
] = "pending",
4498 [LINK_STATE_INITIALIZED
] = "initialized",
4499 [LINK_STATE_CONFIGURING
] = "configuring",
4500 [LINK_STATE_CONFIGURED
] = "configured",
4501 [LINK_STATE_UNMANAGED
] = "unmanaged",
4502 [LINK_STATE_FAILED
] = "failed",
4503 [LINK_STATE_LINGER
] = "linger",
4506 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4508 int log_link_message_full_errno(Link
*link
, sd_netlink_message
*m
, int level
, int err
, const char *msg
) {
4509 const char *err_msg
= NULL
;
4511 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
4512 return log_link_full(link
, level
, err
,
4516 err_msg
&& !endswith(err_msg
, ".") ? "." : "",
4517 err_msg
? " " : "");