1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
8 #include "alloc-util.h"
12 #include "dhcp-identifier.h"
13 #include "dhcp-lease-internal.h"
15 #include "ethtool-util.h"
19 #include "missing_network.h"
20 #include "netlink-util.h"
21 #include "network-internal.h"
22 #include "networkd-can.h"
23 #include "networkd-dhcp-server.h"
24 #include "networkd-dhcp4.h"
25 #include "networkd-dhcp6.h"
26 #include "networkd-ipv4ll.h"
27 #include "networkd-ipv6-proxy-ndp.h"
28 #include "networkd-link-bus.h"
29 #include "networkd-link.h"
30 #include "networkd-lldp-tx.h"
31 #include "networkd-manager.h"
32 #include "networkd-ndisc.h"
33 #include "networkd-neighbor.h"
34 #include "networkd-radv.h"
35 #include "networkd-routing-policy-rule.h"
36 #include "networkd-wifi.h"
38 #include "socket-util.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
41 #include "string-table.h"
43 #include "sysctl-util.h"
45 #include "tmpfile-util.h"
46 #include "udev-util.h"
50 uint32_t link_get_vrf_table(Link
*link
) {
51 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
54 uint32_t link_get_dhcp_route_table(Link
*link
) {
55 /* When the interface is part of an VRF use the VRFs routing table, unless
56 * another table is explicitly specified. */
57 if (link
->network
->dhcp_route_table_set
)
58 return link
->network
->dhcp_route_table
;
59 return link_get_vrf_table(link
);
62 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
63 if (link
->network
->ipv6_accept_ra_route_table_set
)
64 return link
->network
->ipv6_accept_ra_route_table
;
65 return link_get_vrf_table(link
);
68 DUID
* link_get_duid(Link
*link
) {
69 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
70 return &link
->network
->duid
;
72 return &link
->manager
->duid
;
75 static bool link_dhcp6_enabled(Link
*link
) {
78 if (!socket_ipv6_is_supported())
81 if (link
->flags
& IFF_LOOPBACK
)
87 if (link
->network
->bond
)
90 if (link
->iftype
== ARPHRD_CAN
)
93 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
96 static bool link_dhcp4_enabled(Link
*link
) {
99 if (link
->flags
& IFF_LOOPBACK
)
105 if (link
->network
->bond
)
108 if (link
->iftype
== ARPHRD_CAN
)
111 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
114 static bool link_dhcp4_server_enabled(Link
*link
) {
117 if (link
->flags
& IFF_LOOPBACK
)
123 if (link
->network
->bond
)
126 if (link
->iftype
== ARPHRD_CAN
)
129 return link
->network
->dhcp_server
;
132 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
134 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
136 if (link
->flags
& IFF_LOOPBACK
)
142 if (link
->iftype
== ARPHRD_CAN
)
145 if (STRPTR_IN_SET(link
->kind
,
146 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
147 "vti6", "nlmon", "xfrm"))
150 /* L3 or L3S mode do not support ARP. */
151 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
154 if (link
->network
->bond
)
157 return link
->network
->link_local
& mask
;
160 static bool link_ipv6ll_enabled(Link
*link
) {
163 if (!socket_ipv6_is_supported())
166 if (link
->flags
& IFF_LOOPBACK
)
172 if (link
->iftype
== ARPHRD_CAN
)
175 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
178 if (link
->network
->bond
)
181 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
184 static bool link_ipv6_enabled(Link
*link
) {
187 if (!socket_ipv6_is_supported())
190 if (link
->network
->bond
)
193 if (link
->iftype
== ARPHRD_CAN
)
196 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
197 if (link_ipv6ll_enabled(link
))
200 if (network_has_static_ipv6_configurations(link
->network
))
206 static bool link_radv_enabled(Link
*link
) {
209 if (!link_ipv6ll_enabled(link
))
212 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
215 static bool link_ipv4_forward_enabled(Link
*link
) {
218 if (link
->flags
& IFF_LOOPBACK
)
224 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
227 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
230 static bool link_ipv6_forward_enabled(Link
*link
) {
233 if (!socket_ipv6_is_supported())
236 if (link
->flags
& IFF_LOOPBACK
)
242 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
245 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
248 static bool link_proxy_arp_enabled(Link
*link
) {
251 if (link
->flags
& IFF_LOOPBACK
)
257 if (link
->network
->proxy_arp
< 0)
263 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
266 if (!socket_ipv6_is_supported())
269 if (link
->flags
& IFF_LOOPBACK
)
275 if (!link_ipv6ll_enabled(link
))
278 /* If unset use system default (enabled if local forwarding is disabled.
279 * disabled if local forwarding is enabled).
280 * If set, ignore or enforce RA independent of local forwarding state.
282 if (link
->network
->ipv6_accept_ra
< 0)
283 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
284 return !link_ipv6_forward_enabled(link
);
285 else if (link
->network
->ipv6_accept_ra
> 0)
286 /* accept RA even if ip_forward is enabled */
293 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
296 if (!socket_ipv6_is_supported())
297 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
299 if (link
->flags
& IFF_LOOPBACK
)
300 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
303 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
305 return link
->network
->ipv6_privacy_extensions
;
308 static int link_update_ipv6_sysctl(Link
*link
) {
312 if (link
->flags
& IFF_LOOPBACK
)
315 enabled
= link_ipv6_enabled(link
);
317 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", false);
319 return log_link_warning_errno(link
, r
, "Cannot enable IPv6: %m");
321 log_link_info(link
, "IPv6 successfully enabled");
327 static bool link_is_enslaved(Link
*link
) {
328 if (link
->flags
& IFF_SLAVE
)
329 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
335 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
338 /* TODO: add conditions for other netdevs. */
343 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
349 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
352 link_update_operstate(master
, true);
355 void link_update_operstate(Link
*link
, bool also_update_master
) {
356 LinkOperationalState operstate
;
357 LinkCarrierState carrier_state
;
358 LinkAddressState address_state
;
359 _cleanup_strv_free_
char **p
= NULL
;
360 uint8_t scope
= RT_SCOPE_NOWHERE
;
361 bool changed
= false;
367 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
368 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
369 else if (link_has_carrier(link
)) {
370 if (link_is_enslaved(link
))
371 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
373 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
374 } else if (link
->flags
& IFF_UP
)
375 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
377 carrier_state
= LINK_CARRIER_STATE_OFF
;
379 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
382 SET_FOREACH(slave
, link
->slaves
, i
) {
383 link_update_operstate(slave
, false);
385 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
386 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
390 SET_FOREACH(address
, link
->addresses
, i
) {
391 if (!address_is_ready(address
))
394 if (address
->scope
< scope
)
395 scope
= address
->scope
;
398 /* for operstate we also take foreign addresses into account */
399 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
400 if (!address_is_ready(address
))
403 if (address
->scope
< scope
)
404 scope
= address
->scope
;
407 if (scope
< RT_SCOPE_SITE
)
408 /* universally accessible addresses found */
409 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
410 else if (scope
< RT_SCOPE_HOST
)
411 /* only link or site local addresses found */
412 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
414 /* no useful addresses found */
415 address_state
= LINK_ADDRESS_STATE_OFF
;
417 /* Mapping of address and carrier state vs operational state
419 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
420 * ------------------------------------------------------------------------------
421 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
422 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
423 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
426 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
427 operstate
= (LinkOperationalState
) carrier_state
;
428 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
429 operstate
= LINK_OPERSTATE_ROUTABLE
;
430 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
431 operstate
= LINK_OPERSTATE_DEGRADED
;
433 operstate
= LINK_OPERSTATE_ENSLAVED
;
435 if (link
->carrier_state
!= carrier_state
) {
436 link
->carrier_state
= carrier_state
;
438 if (strv_extend(&p
, "CarrierState") < 0)
442 if (link
->address_state
!= address_state
) {
443 link
->address_state
= address_state
;
445 if (strv_extend(&p
, "AddressState") < 0)
449 if (link
->operstate
!= operstate
) {
450 link
->operstate
= operstate
;
452 if (strv_extend(&p
, "OperationalState") < 0)
457 link_send_changed_strv(link
, p
);
461 if (also_update_master
&& link
->network
) {
462 link_update_master_operstate(link
, link
->network
->bond
);
463 link_update_master_operstate(link
, link
->network
->bridge
);
467 #define FLAG_STRING(string, flag, old, new) \
468 (((old ^ new) & flag) \
469 ? ((old & flag) ? (" -" string) : (" +" string)) \
472 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
473 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
479 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
481 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
483 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
485 /* if we got a message without operstate, take it to mean
486 the state was unchanged */
487 operstate
= link
->kernel_operstate
;
489 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
492 if (link
->flags
!= flags
) {
493 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",
494 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
495 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
496 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
497 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
498 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
499 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
500 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
501 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
502 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
503 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
504 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
505 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
506 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
507 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
508 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
509 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
510 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
511 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
512 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
514 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
515 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
516 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
517 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
518 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
519 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
520 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
521 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
523 /* link flags are currently at most 18 bits, let's align to
525 if (unknown_flags_added
)
527 "Unknown link flags gained: %#.5x (ignoring)",
528 unknown_flags_added
);
530 if (unknown_flags_removed
)
532 "Unknown link flags lost: %#.5x (ignoring)",
533 unknown_flags_removed
);
537 link
->kernel_operstate
= operstate
;
539 link_update_operstate(link
, true);
544 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
545 _cleanup_(link_unrefp
) Link
*link
= NULL
;
546 const char *ifname
, *kind
= NULL
;
547 unsigned short iftype
;
555 /* check for link kind */
556 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
558 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
559 r
= sd_netlink_message_exit_container(message
);
564 r
= sd_netlink_message_get_type(message
, &type
);
567 else if (type
!= RTM_NEWLINK
)
570 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
573 else if (ifindex
<= 0)
576 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
580 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
591 .state
= LINK_STATE_PENDING
,
595 .n_dns
= (unsigned) -1,
596 .dns_default_route
= -1,
597 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
598 .mdns
= _RESOLVE_SUPPORT_INVALID
,
599 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
600 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
603 link
->ifname
= strdup(ifname
);
608 link
->kind
= strdup(kind
);
613 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
615 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
617 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
619 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
621 r
= ethtool_get_permanent_macaddr(NULL
, link
->ifname
, &link
->permanent_mac
);
623 log_link_debug_errno(link
, r
, "Permanent MAC address not found for new device, continuing without: %m");
625 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &link
->alternative_names
);
626 if (r
< 0 && r
!= -ENODATA
)
629 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
632 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
635 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
638 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
642 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
646 r
= link_update_flags(link
, message
, false);
650 *ret
= TAKE_PTR(link
);
655 void link_ntp_settings_clear(Link
*link
) {
656 link
->ntp
= strv_free(link
->ntp
);
659 void link_dns_settings_clear(Link
*link
) {
660 link
->dns
= mfree(link
->dns
);
661 link
->n_dns
= (unsigned) -1;
663 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
664 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
666 link
->dns_default_route
= -1;
667 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
668 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
669 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
670 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
672 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
675 static void link_free_engines(Link
*link
) {
679 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
680 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
681 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
682 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
684 link
->lldp
= sd_lldp_unref(link
->lldp
);
688 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
689 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
690 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
691 link
->radv
= sd_radv_unref(link
->radv
);
694 static Link
*link_free(Link
*link
) {
699 link_ntp_settings_clear(link
);
700 link_dns_settings_clear(link
);
702 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
703 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
705 link
->nexthops
= set_free_with_destructor(link
->nexthops
, nexthop_free
);
706 link
->nexthops_foreign
= set_free_with_destructor(link
->nexthops_foreign
, nexthop_free
);
708 link
->neighbors
= set_free_with_destructor(link
->neighbors
, neighbor_free
);
709 link
->neighbors_foreign
= set_free_with_destructor(link
->neighbors_foreign
, neighbor_free
);
711 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
712 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
714 while ((address
= link
->pool_addresses
)) {
715 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
716 address_free(address
);
719 link_lldp_emit_stop(link
);
720 link_free_engines(link
);
721 free(link
->lease_file
);
722 free(link
->lldp_file
);
725 strv_free(link
->alternative_names
);
729 (void) unlink(link
->state_file
);
730 free(link
->state_file
);
732 sd_device_unref(link
->sd_device
);
734 hashmap_free(link
->bound_to_links
);
735 hashmap_free(link
->bound_by_links
);
737 set_free_with_destructor(link
->slaves
, link_unref
);
739 network_unref(link
->network
);
744 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
746 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
753 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
762 void link_set_state(Link
*link
, LinkState state
) {
765 if (link
->state
== state
)
768 log_link_debug(link
, "State changed: %s -> %s",
769 link_state_to_string(link
->state
),
770 link_state_to_string(state
));
774 link_send_changed(link
, "AdministrativeState", NULL
);
777 static void link_enter_unmanaged(Link
*link
) {
780 link_set_state(link
, LINK_STATE_UNMANAGED
);
785 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
790 assert(link
->manager
);
791 assert(link
->manager
->event
);
793 dhcp4_release_old_lease(link
);
795 bool keep_dhcp
= may_keep_dhcp
&&
797 (link
->manager
->restarting
||
798 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
800 if (link
->dhcp_client
&& !keep_dhcp
) {
801 k
= sd_dhcp_client_stop(link
->dhcp_client
);
803 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
807 k
= sd_ipv4ll_stop(link
->ipv4ll
);
809 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
813 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
)
814 if (ad
->acd
&& sd_ipv4acd_is_running(ad
->acd
) == 0) {
815 k
= sd_ipv4acd_stop(ad
->acd
);
817 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
820 if (link
->dhcp6_client
) {
821 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
823 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
827 k
= sd_ndisc_stop(link
->ndisc
);
829 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
833 k
= sd_radv_stop(link
->radv
);
835 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
838 link_lldp_emit_stop(link
);
842 void link_enter_failed(Link
*link
) {
845 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
848 log_link_warning(link
, "Failed");
850 link_set_state(link
, LINK_STATE_FAILED
);
852 link_stop_clients(link
, false);
857 static int link_join_netdevs_after_configured(Link
*link
) {
862 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
863 if (netdev
->ifindex
> 0)
864 /* Assume already enslaved. */
867 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
870 log_struct(LOG_DEBUG
,
871 LOG_LINK_INTERFACE(link
),
872 LOG_NETDEV_INTERFACE(netdev
),
873 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
875 r
= netdev_join(netdev
, link
, NULL
);
877 return log_struct_errno(LOG_WARNING
, r
,
878 LOG_LINK_INTERFACE(link
),
879 LOG_NETDEV_INTERFACE(netdev
),
880 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
886 static void link_enter_configured(Link
*link
) {
888 assert(link
->network
);
890 if (link
->state
!= LINK_STATE_CONFIGURING
)
893 link_set_state(link
, LINK_STATE_CONFIGURED
);
895 (void) link_join_netdevs_after_configured(link
);
900 static int link_request_set_routing_policy_rule(Link
*link
) {
901 RoutingPolicyRule
*rule
, *rrule
= NULL
;
905 assert(link
->network
);
907 link
->routing_policy_rules_configured
= false;
909 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
910 r
= routing_policy_rule_get(link
->manager
, rule
, &rrule
);
913 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
917 r
= routing_policy_rule_configure(rule
, link
, NULL
);
919 return log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
921 link
->routing_policy_rule_messages
++;
924 routing_policy_rule_purge(link
->manager
, link
);
925 if (link
->routing_policy_rule_messages
== 0) {
926 link
->routing_policy_rules_configured
= true;
927 link_check_ready(link
);
929 log_link_debug(link
, "Setting routing policy rules");
930 link_set_state(link
, LINK_STATE_CONFIGURING
);
936 static int nexthop_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
940 assert(link
->nexthop_messages
> 0);
941 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
942 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
944 link
->nexthop_messages
--;
946 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
949 r
= sd_netlink_message_get_errno(m
);
950 if (r
< 0 && r
!= -EEXIST
) {
951 log_link_message_warning_errno(link
, m
, r
, "Could not set nexthop");
952 link_enter_failed(link
);
956 if (link
->nexthop_messages
== 0) {
957 log_link_debug(link
, "Nexthop set");
958 link
->static_nexthops_configured
= true;
959 link_check_ready(link
);
965 int link_request_set_nexthop(Link
*link
) {
969 LIST_FOREACH(nexthops
, nh
, link
->network
->static_nexthops
) {
970 r
= nexthop_configure(nh
, link
, nexthop_handler
);
972 return log_link_warning_errno(link
, r
, "Could not set nexthop: %m");
974 link
->nexthop_messages
++;
977 if (link
->nexthop_messages
== 0) {
978 link
->static_nexthops_configured
= true;
979 link_check_ready(link
);
981 log_link_debug(link
, "Setting nexthop");
982 link_set_state(link
, LINK_STATE_CONFIGURING
);
988 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
992 assert(link
->route_messages
> 0);
993 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
994 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
996 link
->route_messages
--;
998 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1001 r
= sd_netlink_message_get_errno(m
);
1002 if (r
< 0 && r
!= -EEXIST
) {
1003 log_link_message_warning_errno(link
, m
, r
, "Could not set route");
1004 link_enter_failed(link
);
1008 if (link
->route_messages
== 0) {
1009 log_link_debug(link
, "Routes set");
1010 link
->static_routes_configured
= true;
1011 link_check_ready(link
);
1017 int link_request_set_routes(Link
*link
) {
1019 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
1020 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
1027 assert(link
->network
);
1028 assert(link
->addresses_configured
);
1029 assert(link
->address_messages
== 0);
1030 assert(link
->state
!= _LINK_STATE_INVALID
);
1032 link
->static_routes_configured
= false;
1033 link
->static_routes_ready
= false;
1035 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
1036 /* During configuring addresses, the link lost its carrier. As networkd is dropping
1037 * the addresses now, let's not configure the routes either. */
1040 r
= link_request_set_routing_policy_rule(link
);
1044 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
1045 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
1046 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
1047 if (rt
->gateway_from_dhcp
)
1050 if ((in_addr_is_null(rt
->family
, &rt
->gw
) && ordered_set_isempty(rt
->multipath_routes
)) != (phase
== PHASE_NON_GATEWAY
))
1053 r
= route_configure(rt
, link
, route_handler
);
1055 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
1057 link
->route_messages
++;
1060 if (link
->route_messages
== 0) {
1061 link
->static_routes_configured
= true;
1062 link_check_ready(link
);
1064 log_link_debug(link
, "Setting routes");
1065 link_set_state(link
, LINK_STATE_CONFIGURING
);
1071 void link_check_ready(Link
*link
) {
1078 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1084 if (!link
->addresses_configured
)
1087 if (!link
->neighbors_configured
)
1090 SET_FOREACH(a
, link
->addresses
, i
)
1091 if (!address_is_ready(a
))
1094 if (!link
->addresses_ready
) {
1095 link
->addresses_ready
= true;
1096 r
= link_request_set_routes(link
);
1098 link_enter_failed(link
);
1102 if (!link
->static_routes_configured
)
1105 if (!link
->static_routes_ready
) {
1106 link
->static_routes_ready
= true;
1107 r
= link_request_set_nexthop(link
);
1109 link_enter_failed(link
);
1113 if (!link
->static_nexthops_configured
)
1116 if (!link
->routing_policy_rules_configured
)
1119 if (!link
->tc_configured
)
1122 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1124 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !link
->ipv4ll_address
)
1127 if (link_ipv6ll_enabled(link
) &&
1128 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
1131 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1132 !link
->dhcp4_configured
&&
1133 !link
->dhcp6_configured
&&
1134 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address
))
1135 /* When DHCP is enabled, at least one protocol must provide an address, or
1136 * an IPv4ll fallback address must be configured. */
1139 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1143 if (link
->state
!= LINK_STATE_CONFIGURED
)
1144 link_enter_configured(link
);
1149 static int link_request_set_neighbors(Link
*link
) {
1154 assert(link
->network
);
1155 assert(link
->state
!= _LINK_STATE_INVALID
);
1157 link
->neighbors_configured
= false;
1159 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1160 r
= neighbor_configure(neighbor
, link
, NULL
);
1162 return log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1165 if (link
->neighbor_messages
== 0) {
1166 link
->neighbors_configured
= true;
1167 link_check_ready(link
);
1169 log_link_debug(link
, "Setting neighbors");
1170 link_set_state(link
, LINK_STATE_CONFIGURING
);
1176 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1182 assert(link
->ifname
);
1183 assert(link
->address_messages
> 0);
1184 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1185 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1187 link
->address_messages
--;
1189 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1192 r
= sd_netlink_message_get_errno(m
);
1193 if (r
< 0 && r
!= -EEXIST
) {
1194 log_link_message_warning_errno(link
, m
, r
, "Could not set address");
1195 link_enter_failed(link
);
1198 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1200 if (link
->address_messages
== 0) {
1201 log_link_debug(link
, "Addresses set");
1202 link
->addresses_configured
= true;
1203 link_check_ready(link
);
1209 static int link_set_bridge_fdb(Link
*link
) {
1210 FdbEntry
*fdb_entry
;
1213 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1214 r
= fdb_entry_configure(link
, fdb_entry
);
1216 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1222 static int link_request_set_addresses(Link
*link
) {
1223 AddressLabel
*label
;
1229 assert(link
->network
);
1230 assert(link
->state
!= _LINK_STATE_INVALID
);
1232 /* Reset all *_configured flags we are configuring. */
1233 link
->addresses_configured
= false;
1234 link
->addresses_ready
= false;
1235 link
->neighbors_configured
= false;
1236 link
->static_routes_configured
= false;
1237 link
->static_routes_ready
= false;
1238 link
->static_nexthops_configured
= false;
1239 link
->routing_policy_rules_configured
= false;
1241 r
= link_set_bridge_fdb(link
);
1245 r
= link_request_set_neighbors(link
);
1249 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1252 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1254 r
= address_configure(ad
, link
, address_handler
, update
);
1256 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1258 link
->address_messages
++;
1261 if (IN_SET(link
->network
->router_prefix_delegation
,
1262 RADV_PREFIX_DELEGATION_STATIC
,
1263 RADV_PREFIX_DELEGATION_BOTH
))
1264 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
1265 _cleanup_(address_freep
) Address
*address
= NULL
;
1270 r
= address_new(&address
);
1272 return log_link_error_errno(link
, r
, "Could not allocate address: %m");
1274 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1278 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1282 address
->family
= AF_INET6
;
1283 r
= address_configure(address
, link
, address_handler
, true);
1285 return log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1287 link
->address_messages
++;
1290 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1291 r
= address_label_configure(label
, link
, NULL
, false);
1293 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1295 link
->address_label_messages
++;
1298 /* now that we can figure out a default address for the dhcp server,
1300 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1301 r
= dhcp4_server_configure(link
);
1304 log_link_debug(link
, "Offering DHCPv4 leases");
1307 if (link
->address_messages
== 0) {
1308 link
->addresses_configured
= true;
1309 link_check_ready(link
);
1311 log_link_debug(link
, "Setting addresses");
1312 link_set_state(link
, LINK_STATE_CONFIGURING
);
1318 static int link_set_bridge_vlan(Link
*link
) {
1321 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1323 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1328 static int link_set_proxy_arp(Link
*link
) {
1331 if (!link_proxy_arp_enabled(link
))
1334 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1336 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1341 static int link_configure_continue(Link
*link
);
1343 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1348 assert(link
->ifname
);
1350 link
->setting_mtu
= false;
1352 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1355 r
= sd_netlink_message_get_errno(m
);
1357 log_link_message_warning_errno(link
, m
, r
, "Could not set MTU, ignoring");
1359 log_link_debug(link
, "Setting MTU done.");
1361 if (link
->state
== LINK_STATE_INITIALIZED
) {
1362 r
= link_configure_continue(link
);
1364 link_enter_failed(link
);
1370 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1371 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1375 assert(link
->manager
);
1376 assert(link
->manager
->rtnl
);
1378 if (mtu
== 0 || link
->setting_mtu
)
1381 if (link
->mtu
== mtu
)
1384 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1386 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1388 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1390 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1391 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1392 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1394 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1395 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes");
1400 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1402 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1404 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1405 link_netlink_destroy_callback
, link
);
1407 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1410 link
->setting_mtu
= true;
1415 static bool link_reduces_vlan_mtu(Link
*link
) {
1416 /* See netif_reduces_vlan_mtu() in kernel. */
1417 return streq_ptr(link
->kind
, "macsec");
1420 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1425 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
, i
)
1426 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1427 /* See vlan_dev_change_mtu() in kernel. */
1428 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1430 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1431 /* See macvlan_change_mtu() in kernel. */
1437 static int link_configure_mtu(Link
*link
) {
1441 assert(link
->network
);
1443 if (link
->network
->mtu
> 0)
1444 return link_set_mtu(link
, link
->network
->mtu
);
1446 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1447 if (link
->mtu
>= mtu
)
1450 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1451 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1454 return link_set_mtu(link
, mtu
);
1457 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1462 assert(link
->ifname
);
1464 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1467 r
= sd_netlink_message_get_errno(m
);
1469 log_link_message_warning_errno(link
, m
, r
, "Could not set link flags, ignoring");
1474 static int link_set_flags(Link
*link
) {
1475 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1476 unsigned ifi_change
= 0;
1477 unsigned ifi_flags
= 0;
1481 assert(link
->manager
);
1482 assert(link
->manager
->rtnl
);
1484 if (link
->flags
& IFF_LOOPBACK
)
1490 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1493 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1495 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1497 if (link
->network
->arp
>= 0) {
1498 ifi_change
|= IFF_NOARP
;
1499 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1502 if (link
->network
->multicast
>= 0) {
1503 ifi_change
|= IFF_MULTICAST
;
1504 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1507 if (link
->network
->allmulticast
>= 0) {
1508 ifi_change
|= IFF_ALLMULTI
;
1509 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1512 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1514 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1516 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1517 link_netlink_destroy_callback
, link
);
1519 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1526 static int link_acquire_ipv6_conf(Link
*link
) {
1531 if (link_ipv6_accept_ra_enabled(link
)) {
1532 assert(link
->ndisc
);
1534 log_link_debug(link
, "Discovering IPv6 routers");
1536 r
= sd_ndisc_start(link
->ndisc
);
1537 if (r
< 0 && r
!= -EBUSY
)
1538 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1541 if (link_radv_enabled(link
)) {
1543 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1545 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1547 r
= radv_emit_dns(link
);
1549 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1551 r
= sd_radv_start(link
->radv
);
1552 if (r
< 0 && r
!= -EBUSY
)
1553 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1556 if (link_dhcp6_enabled(link
) && link
->network
->dhcp6_without_ra
) {
1557 assert(link
->dhcp6_client
);
1558 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1560 r
= dhcp6_request_address(link
, true);
1561 if (r
< 0 && r
!= -EBUSY
)
1562 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1564 log_link_debug(link
, "Acquiring DHCPv6 lease");
1567 (void) dhcp6_request_prefix_delegation(link
);
1572 static int link_acquire_ipv4_conf(Link
*link
) {
1576 assert(link
->manager
);
1577 assert(link
->manager
->event
);
1579 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1580 assert(link
->ipv4ll
);
1582 log_link_debug(link
, "Acquiring IPv4 link-local address");
1584 r
= sd_ipv4ll_start(link
->ipv4ll
);
1586 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1589 if (link_dhcp4_enabled(link
)) {
1590 assert(link
->dhcp_client
);
1592 log_link_debug(link
, "Acquiring DHCPv4 lease");
1594 r
= sd_dhcp_client_start(link
->dhcp_client
);
1596 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1602 static int link_acquire_conf(Link
*link
) {
1607 r
= link_acquire_ipv4_conf(link
);
1611 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1612 r
= link_acquire_ipv6_conf(link
);
1617 if (link_lldp_emit_enabled(link
)) {
1618 r
= link_lldp_emit_start(link
);
1620 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1626 bool link_has_carrier(Link
*link
) {
1627 /* see Documentation/networking/operstates.txt in the kernel sources */
1629 if (link
->kernel_operstate
== IF_OPER_UP
)
1632 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1633 /* operstate may not be implemented, so fall back to flags */
1634 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1635 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1641 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1646 link
->setting_genmode
= false;
1648 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1651 r
= sd_netlink_message_get_errno(m
);
1653 log_link_message_warning_errno(link
, m
, r
, "Could not set address genmode for interface, ignoring");
1655 log_link_debug(link
, "Setting address genmode done.");
1657 if (link
->state
== LINK_STATE_INITIALIZED
) {
1658 r
= link_configure_continue(link
);
1660 link_enter_failed(link
);
1666 static int link_configure_addrgen_mode(Link
*link
) {
1667 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1668 uint8_t ipv6ll_mode
;
1672 assert(link
->network
);
1673 assert(link
->manager
);
1674 assert(link
->manager
->rtnl
);
1676 if (!socket_ipv6_is_supported() || link
->setting_genmode
)
1679 log_link_debug(link
, "Setting address genmode for link");
1681 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1683 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1685 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1687 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1689 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1691 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1693 if (!link_ipv6ll_enabled(link
))
1694 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1695 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1696 /* The file may not exist. And even if it exists, when stable_secret is unset,
1697 * reading the file fails with EIO. */
1698 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1700 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1702 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1704 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1706 r
= sd_netlink_message_close_container(req
);
1708 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1710 r
= sd_netlink_message_close_container(req
);
1712 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1714 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1715 link_netlink_destroy_callback
, link
);
1717 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1720 link
->setting_genmode
= true;
1725 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1730 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1733 r
= sd_netlink_message_get_errno(m
);
1735 /* we warn but don't fail the link, as it may be brought up later */
1736 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
1741 static int link_up(Link
*link
) {
1742 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1746 assert(link
->network
);
1747 assert(link
->manager
);
1748 assert(link
->manager
->rtnl
);
1750 log_link_debug(link
, "Bringing link up");
1752 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1754 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1756 /* set it free if not enslaved with networkd */
1757 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1758 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1760 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1763 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1765 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1767 if (link
->network
->mac
) {
1768 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1770 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1773 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1774 link_netlink_destroy_callback
, link
);
1776 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1783 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1788 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1791 r
= sd_netlink_message_get_errno(m
);
1793 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
1798 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1799 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1803 assert(link
->manager
);
1804 assert(link
->manager
->rtnl
);
1806 log_link_debug(link
, "Bringing link down");
1808 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1809 RTM_SETLINK
, link
->ifindex
);
1811 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1813 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1815 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1817 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1818 callback
?: link_down_handler
,
1819 link_netlink_destroy_callback
, link
);
1821 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1828 static int link_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1833 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1836 r
= sd_netlink_message_get_errno(m
);
1838 log_link_message_warning_errno(link
, m
, r
, "Could not set group for the interface");
1843 static int link_set_group(Link
*link
) {
1844 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1848 assert(link
->network
);
1849 assert(link
->manager
);
1850 assert(link
->manager
->rtnl
);
1852 if (link
->network
->group
<= 0)
1855 log_link_debug(link
, "Setting group");
1857 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1859 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1861 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
1863 return log_link_error_errno(link
, r
, "Could not set link group: %m");
1865 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_group_handler
,
1866 link_netlink_destroy_callback
, link
);
1868 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1875 static int link_handle_bound_to_list(Link
*link
) {
1879 bool required_up
= false;
1880 bool link_is_up
= false;
1884 if (hashmap_isempty(link
->bound_to_links
))
1887 if (link
->flags
& IFF_UP
)
1890 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1891 if (link_has_carrier(l
)) {
1896 if (!required_up
&& link_is_up
) {
1897 r
= link_down(link
, NULL
);
1900 } else if (required_up
&& !link_is_up
) {
1909 static int link_handle_bound_by_list(Link
*link
) {
1916 if (hashmap_isempty(link
->bound_by_links
))
1919 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1920 r
= link_handle_bound_to_list(l
);
1928 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1934 if (link
== carrier
)
1937 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1940 r
= hashmap_ensure_allocated(h
, NULL
);
1944 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1951 static int link_new_bound_by_list(Link
*link
) {
1956 bool list_updated
= false;
1959 assert(link
->manager
);
1963 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1964 if (!carrier
->network
)
1967 if (strv_isempty(carrier
->network
->bind_carrier
))
1970 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
1971 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1975 list_updated
= true;
1982 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1983 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1987 link_dirty(carrier
);
1993 static int link_new_bound_to_list(Link
*link
) {
1998 bool list_updated
= false;
2001 assert(link
->manager
);
2006 if (strv_isempty(link
->network
->bind_carrier
))
2011 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2012 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
2013 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2017 list_updated
= true;
2024 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2025 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2029 link_dirty(carrier
);
2035 static int link_new_carrier_maps(Link
*link
) {
2038 r
= link_new_bound_by_list(link
);
2042 r
= link_handle_bound_by_list(link
);
2046 r
= link_new_bound_to_list(link
);
2050 r
= link_handle_bound_to_list(link
);
2057 static void link_free_bound_to_list(Link
*link
) {
2061 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2062 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2064 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2065 link_dirty(bound_to
);
2071 static void link_free_bound_by_list(Link
*link
) {
2075 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2076 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2078 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2079 link_dirty(bound_by
);
2080 link_handle_bound_to_list(bound_by
);
2087 static void link_free_carrier_maps(Link
*link
) {
2088 bool list_updated
= false;
2092 if (!hashmap_isempty(link
->bound_to_links
)) {
2093 link_free_bound_to_list(link
);
2094 list_updated
= true;
2097 if (!hashmap_isempty(link
->bound_by_links
)) {
2098 link_free_bound_by_list(link
);
2099 list_updated
= true;
2108 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2115 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2119 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2123 r
= set_put(master
->slaves
, link
);
2131 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2136 if (!link
->manager
|| !netdev
)
2139 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2142 link_unref(set_remove(master
->slaves
, link
));
2145 static void link_detach_from_manager(Link
*link
) {
2146 if (!link
|| !link
->manager
)
2149 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2152 /* The following must be called at last. */
2153 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2157 void link_drop(Link
*link
) {
2158 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2161 link_set_state(link
, LINK_STATE_LINGER
);
2163 link_free_carrier_maps(link
);
2165 if (link
->network
) {
2166 link_drop_from_master(link
, link
->network
->bridge
);
2167 link_drop_from_master(link
, link
->network
->bond
);
2170 log_link_debug(link
, "Link removed");
2172 (void) unlink(link
->state_file
);
2173 link_detach_from_manager(link
);
2176 static int link_joined(Link
*link
) {
2180 assert(link
->network
);
2182 if (!hashmap_isempty(link
->bound_to_links
)) {
2183 r
= link_handle_bound_to_list(link
);
2186 } else if (!(link
->flags
& IFF_UP
)) {
2189 link_enter_failed(link
);
2194 if (link
->network
->bridge
) {
2195 r
= link_set_bridge(link
);
2197 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2199 r
= link_append_to_master(link
, link
->network
->bridge
);
2201 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2204 if (link
->network
->bond
) {
2205 r
= link_set_bond(link
);
2207 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2209 r
= link_append_to_master(link
, link
->network
->bond
);
2211 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2214 if (link
->network
->use_br_vlan
&&
2215 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2216 r
= link_set_bridge_vlan(link
);
2218 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2221 /* Skip setting up addresses until it gets carrier,
2222 or it would try to set addresses twice,
2223 which is bad for non-idempotent steps. */
2224 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2227 link_set_state(link
, LINK_STATE_CONFIGURING
);
2228 return link_request_set_addresses(link
);
2231 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2235 assert(link
->network
);
2236 assert(link
->enslaving
> 0);
2240 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2243 r
= sd_netlink_message_get_errno(m
);
2244 if (r
< 0 && r
!= -EEXIST
) {
2245 log_link_message_warning_errno(link
, m
, r
, "Could not join netdev");
2246 link_enter_failed(link
);
2250 log_link_debug(link
, "Joined netdev");
2252 if (link
->enslaving
== 0) {
2253 r
= link_joined(link
);
2255 link_enter_failed(link
);
2261 static int link_enter_join_netdev(Link
*link
) {
2267 assert(link
->network
);
2268 assert(link
->state
== LINK_STATE_INITIALIZED
);
2270 link_set_state(link
, LINK_STATE_CONFIGURING
);
2273 link
->enslaving
= 0;
2275 if (link
->network
->bond
) {
2276 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2277 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2278 return link_joined(link
);
2280 log_struct(LOG_DEBUG
,
2281 LOG_LINK_INTERFACE(link
),
2282 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2283 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2287 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2289 log_struct_errno(LOG_WARNING
, r
,
2290 LOG_LINK_INTERFACE(link
),
2291 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2292 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2293 link_enter_failed(link
);
2298 if (link
->network
->bridge
) {
2299 log_struct(LOG_DEBUG
,
2300 LOG_LINK_INTERFACE(link
),
2301 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2302 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2306 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2308 log_struct_errno(LOG_WARNING
, r
,
2309 LOG_LINK_INTERFACE(link
),
2310 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2311 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2312 link_enter_failed(link
);
2317 if (link
->network
->vrf
) {
2318 log_struct(LOG_DEBUG
,
2319 LOG_LINK_INTERFACE(link
),
2320 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2321 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2325 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2327 log_struct_errno(LOG_WARNING
, r
,
2328 LOG_LINK_INTERFACE(link
),
2329 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2330 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2331 link_enter_failed(link
);
2336 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2338 if (netdev
->ifindex
> 0)
2339 /* Assume already enslaved. */
2342 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2345 log_struct(LOG_DEBUG
,
2346 LOG_LINK_INTERFACE(link
),
2347 LOG_NETDEV_INTERFACE(netdev
),
2348 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2352 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2354 log_struct_errno(LOG_WARNING
, r
,
2355 LOG_LINK_INTERFACE(link
),
2356 LOG_NETDEV_INTERFACE(netdev
),
2357 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2358 link_enter_failed(link
);
2363 if (link
->enslaving
== 0)
2364 return link_joined(link
);
2369 static int link_set_ipv4_forward(Link
*link
) {
2372 if (!link_ipv4_forward_enabled(link
))
2375 /* We propagate the forwarding flag from one interface to the
2376 * global setting one way. This means: as long as at least one
2377 * interface was configured at any time that had IP forwarding
2378 * enabled the setting will stay on for good. We do this
2379 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2380 * somewhat in sync (see below). */
2382 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2384 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2389 static int link_set_ipv6_forward(Link
*link
) {
2392 if (!link_ipv6_forward_enabled(link
))
2395 /* On Linux, the IPv6 stack does not know a per-interface
2396 * packet forwarding setting: either packet forwarding is on
2397 * for all, or off for all. We hence don't bother with a
2398 * per-interface setting, but simply propagate the interface
2399 * flag, if it is set, to the global flag, one-way. Note that
2400 * while IPv4 would allow a per-interface flag, we expose the
2401 * same behaviour there and also propagate the setting from
2402 * one to all, to keep things simple (see above). */
2404 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2406 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2411 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2412 IPv6PrivacyExtensions s
;
2415 s
= link_ipv6_privacy_extensions(link
);
2419 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2421 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2426 static int link_set_ipv6_accept_ra(Link
*link
) {
2429 /* Make this a NOP if IPv6 is not available */
2430 if (!socket_ipv6_is_supported())
2433 if (link
->flags
& IFF_LOOPBACK
)
2439 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2441 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2446 static int link_set_ipv6_dad_transmits(Link
*link
) {
2449 /* Make this a NOP if IPv6 is not available */
2450 if (!socket_ipv6_is_supported())
2453 if (link
->flags
& IFF_LOOPBACK
)
2459 if (link
->network
->ipv6_dad_transmits
< 0)
2462 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2464 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2469 static int link_set_ipv6_hop_limit(Link
*link
) {
2472 /* Make this a NOP if IPv6 is not available */
2473 if (!socket_ipv6_is_supported())
2476 if (link
->flags
& IFF_LOOPBACK
)
2482 if (link
->network
->ipv6_hop_limit
< 0)
2485 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2487 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2492 static int link_set_ipv6_mtu(Link
*link
) {
2495 /* Make this a NOP if IPv6 is not available */
2496 if (!socket_ipv6_is_supported())
2499 if (link
->flags
& IFF_LOOPBACK
)
2502 if (link
->network
->ipv6_mtu
== 0)
2505 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2506 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2507 if (link
->network
->ipv6_mtu
< IPV6_MIN_MTU
) {
2508 log_link_notice(link
, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU
)" byte minimum required");
2509 link
->network
->ipv6_mtu
= IPV6_MIN_MTU
;
2512 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2514 if (link
->mtu
< link
->network
->ipv6_mtu
)
2515 log_link_warning(link
, "Cannot set IPv6 MTU %"PRIu32
" higher than device MTU %"PRIu32
,
2516 link
->network
->ipv6_mtu
, link
->mtu
);
2518 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2521 link
->ipv6_mtu_set
= true;
2526 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2527 Address
*net_address
;
2535 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2536 if (address_equal(net_address
, address
))
2542 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2543 Neighbor
*net_neighbor
;
2551 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2552 if (neighbor_equal(net_neighbor
, neighbor
))
2558 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2567 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2568 if (route_equal(net_route
, route
))
2574 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2581 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2584 /* Even when the address is leased from a DHCP server, networkd assign the address
2585 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2586 * corresponding routes with RTPROT_DHCP. */
2587 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2588 if (route
->protocol
!= RTPROT_DHCP
)
2591 if (address
->family
!= route
->family
)
2594 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2601 static int link_enumerate_ipv6_tentative_addresses(Link
*link
) {
2602 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2603 sd_netlink_message
*addr
;
2607 assert(link
->manager
);
2608 assert(link
->manager
->rtnl
);
2610 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, 0, AF_INET6
);
2614 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
2618 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
2619 unsigned char flags
;
2622 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
2624 log_link_warning_errno(link
, r
, "rtnl: invalid ifindex, ignoring: %m");
2626 } else if (link
->ifindex
!= ifindex
)
2629 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
2631 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
2633 } else if (!(flags
& IFA_F_TENTATIVE
))
2636 log_link_debug(link
, "Found tentative ipv6 link-local address");
2637 (void) manager_rtnl_process_address(link
->manager
->rtnl
, addr
, link
->manager
);
2643 static int link_drop_foreign_config(Link
*link
) {
2650 /* The kernel doesn't notify us about tentative addresses;
2651 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2652 if (!link_ipv6ll_enabled(link
)) {
2653 r
= link_enumerate_ipv6_tentative_addresses(link
);
2658 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2659 /* we consider IPv6LL addresses to be managed by the kernel */
2660 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2663 if (link_address_is_dynamic(link
, address
)) {
2664 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2666 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2669 if (link_is_static_address_configured(link
, address
)) {
2670 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2672 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2674 r
= address_remove(address
, link
, NULL
);
2680 SET_FOREACH(neighbor
, link
->neighbors_foreign
, i
) {
2681 if (link_is_neighbor_configured(link
, neighbor
)) {
2682 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2686 r
= neighbor_remove(neighbor
, link
, NULL
);
2692 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2693 /* do not touch routes managed by the kernel */
2694 if (route
->protocol
== RTPROT_KERNEL
)
2697 /* do not touch multicast route added by kernel */
2698 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2699 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2700 if (route
->protocol
== RTPROT_BOOT
&&
2701 route
->family
== AF_INET6
&&
2702 route
->dst_prefixlen
== 8 &&
2703 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 }}} }))
2706 if (route
->protocol
== RTPROT_STATIC
&& link
->network
&&
2707 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2710 if (route
->protocol
== RTPROT_DHCP
&& link
->network
&&
2711 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2714 if (link_is_static_route_configured(link
, route
)) {
2715 r
= route_add(link
, route
, NULL
);
2719 r
= route_remove(route
, link
, NULL
);
2728 static int link_drop_config(Link
*link
) {
2729 Address
*address
, *pool_address
;
2735 SET_FOREACH(address
, link
->addresses
, i
) {
2736 /* we consider IPv6LL addresses to be managed by the kernel */
2737 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2740 r
= address_remove(address
, link
, NULL
);
2744 /* If this address came from an address pool, clean up the pool */
2745 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2746 if (address_equal(address
, pool_address
)) {
2747 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2748 address_free(pool_address
);
2754 SET_FOREACH(neighbor
, link
->neighbors
, i
) {
2755 r
= neighbor_remove(neighbor
, link
, NULL
);
2760 SET_FOREACH(route
, link
->routes
, i
) {
2761 /* do not touch routes managed by the kernel */
2762 if (route
->protocol
== RTPROT_KERNEL
)
2765 r
= route_remove(route
, link
, NULL
);
2775 static int link_configure_ipv4_dad(Link
*link
) {
2780 assert(link
->network
);
2782 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
)
2783 if (address
->family
== AF_INET
&&
2784 FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
2785 r
= configure_ipv4_duplicate_address_detection(link
, address
);
2787 return log_link_error_errno(link
, r
, "Failed to configure IPv4ACD: %m");
2793 static int link_configure_traffic_control(Link
*link
) {
2798 link
->tc_configured
= false;
2799 link
->tc_messages
= 0;
2801 ORDERED_HASHMAP_FOREACH(tc
, link
->network
->tc_by_section
, i
) {
2802 r
= traffic_control_configure(link
, tc
);
2807 if (link
->tc_messages
== 0)
2808 link
->tc_configured
= true;
2810 log_link_debug(link
, "Configuring traffic control");
2815 static int link_configure(Link
*link
) {
2819 assert(link
->network
);
2820 assert(link
->state
== LINK_STATE_INITIALIZED
);
2822 r
= link_configure_traffic_control(link
);
2826 if (link
->iftype
== ARPHRD_CAN
)
2827 return link_configure_can(link
);
2829 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2830 * for this interface, then enable IPv6 */
2831 (void) link_update_ipv6_sysctl(link
);
2833 r
= link_set_proxy_arp(link
);
2837 r
= ipv6_proxy_ndp_addresses_configure(link
);
2841 r
= link_set_ipv4_forward(link
);
2845 r
= link_set_ipv6_forward(link
);
2849 r
= link_set_ipv6_privacy_extensions(link
);
2853 r
= link_set_ipv6_accept_ra(link
);
2857 r
= link_set_ipv6_dad_transmits(link
);
2861 r
= link_set_ipv6_hop_limit(link
);
2865 r
= link_set_flags(link
);
2869 r
= link_set_group(link
);
2873 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2874 r
= ipv4ll_configure(link
);
2879 if (link_dhcp4_enabled(link
)) {
2880 r
= dhcp4_set_promote_secondaries(link
);
2884 r
= dhcp4_configure(link
);
2889 if (link_dhcp4_server_enabled(link
)) {
2890 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2894 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2899 if (link_dhcp6_enabled(link
) ||
2900 link_ipv6_accept_ra_enabled(link
)) {
2901 r
= dhcp6_configure(link
);
2906 if (link_ipv6_accept_ra_enabled(link
)) {
2907 r
= ndisc_configure(link
);
2912 if (link_radv_enabled(link
)) {
2913 r
= radv_configure(link
);
2918 if (link_lldp_rx_enabled(link
)) {
2919 r
= link_lldp_rx_configure(link
);
2924 r
= link_configure_mtu(link
);
2928 r
= link_configure_addrgen_mode(link
);
2932 r
= link_configure_ipv4_dad(link
);
2936 return link_configure_continue(link
);
2939 /* The configuration continues in this separate function, instead of
2940 * including this in the above link_configure() function, for two
2942 * 1) some devices reset the link when the mtu is set, which caused
2943 * an infinite loop here in networkd; see:
2944 * https://github.com/systemd/systemd/issues/6593
2945 * https://github.com/systemd/systemd/issues/9831
2946 * 2) if ipv6ll is disabled, then bringing the interface up must be
2947 * delayed until after we get confirmation from the kernel that
2948 * the addr_gen_mode parameter has been set (via netlink), see:
2949 * https://github.com/systemd/systemd/issues/13882
2951 static int link_configure_continue(Link
*link
) {
2955 assert(link
->network
);
2956 assert(link
->state
== LINK_STATE_INITIALIZED
);
2958 if (link
->setting_mtu
|| link
->setting_genmode
)
2961 /* Drop foreign config, but ignore loopback or critical devices.
2962 * We do not want to remove loopback address or addresses used for root NFS. */
2963 if (!(link
->flags
& IFF_LOOPBACK
) &&
2964 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
2965 r
= link_drop_foreign_config(link
);
2970 /* The kernel resets ipv6 mtu after changing device mtu;
2971 * we must set this here, after we've set device mtu */
2972 r
= link_set_ipv6_mtu(link
);
2976 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2977 r
= link_acquire_conf(link
);
2982 return link_enter_join_netdev(link
);
2985 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2988 if (duid
->raw_data_len
> 0)
2991 if (duid
->type
!= DUID_TYPE_UUID
)
2994 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2995 duid
->raw_data_len
= sizeof(sd_id128_t
);
3000 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3001 Manager
*manager
= userdata
;
3002 const sd_bus_error
*e
;
3012 e
= sd_bus_message_get_error(m
);
3014 log_error_errno(sd_bus_error_get_errno(e
),
3015 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3020 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3024 if (sz
!= sizeof(sd_id128_t
)) {
3025 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3029 memcpy(&manager
->product_uuid
, a
, sz
);
3030 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3031 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3033 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3036 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3039 r
= link_configure(link
);
3041 link_enter_failed(link
);
3044 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3046 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3047 * even if the method fails. */
3048 manager
->has_product_uuid
= true;
3053 static bool link_requires_uuid(Link
*link
) {
3057 assert(link
->manager
);
3058 assert(link
->network
);
3060 duid
= link_get_duid(link
);
3061 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3064 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3067 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3073 static int link_configure_duid(Link
*link
) {
3079 assert(link
->manager
);
3080 assert(link
->network
);
3083 duid
= link_get_duid(link
);
3085 if (!link_requires_uuid(link
))
3088 if (m
->has_product_uuid
) {
3089 (void) duid_set_uuid(duid
, m
->product_uuid
);
3093 if (!m
->links_requesting_uuid
) {
3094 r
= manager_request_product_uuid(m
, link
);
3099 log_link_warning_errno(link
, r
,
3100 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3104 r
= set_put(m
->links_requesting_uuid
, link
);
3108 r
= set_put(m
->duids_requesting_uuid
, duid
);
3118 static int link_reconfigure_internal(Link
*link
, sd_netlink_message
*m
, bool force
) {
3123 _cleanup_strv_free_
char **s
= NULL
;
3125 r
= sd_netlink_message_get_errno(m
);
3129 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3130 if (r
< 0 && r
!= -ENODATA
)
3133 strv_free_and_replace(link
->alternative_names
, s
);
3136 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
, link
->ifname
, link
->alternative_names
,
3137 &link
->mac
, &link
->permanent_mac
, link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3139 link_enter_unmanaged(link
);
3141 } else if (r
== 0 && network
->unmanaged
) {
3142 link_enter_unmanaged(link
);
3147 if (link
->network
== network
&& !force
)
3150 log_link_info(link
, "Re-configuring with %s", network
->filename
);
3152 /* Dropping old .network file */
3153 r
= link_stop_clients(link
, false);
3157 if (link_dhcp4_server_enabled(link
))
3158 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3160 r
= link_drop_config(link
);
3164 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3165 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3166 r
= link_drop_foreign_config(link
);
3171 link_free_carrier_maps(link
);
3172 link_free_engines(link
);
3173 link
->network
= network_unref(link
->network
);
3175 /* Then, apply new .network file */
3176 r
= network_apply(network
, link
);
3180 r
= link_new_carrier_maps(link
);
3184 link_set_state(link
, LINK_STATE_INITIALIZED
);
3187 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3188 * link_configure() is called later asynchronously. */
3189 r
= link_configure_duid(link
);
3193 r
= link_configure(link
);
3200 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3203 r
= link_reconfigure_internal(link
, m
, false);
3205 link_enter_failed(link
);
3210 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3213 r
= link_reconfigure_internal(link
, m
, true);
3215 link_enter_failed(link
);
3220 int link_reconfigure(Link
*link
, bool force
) {
3221 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3224 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
3227 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3232 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
3233 force
? link_force_reconfigure_handler
: link_reconfigure_handler
,
3234 link_netlink_destroy_callback
, link
);
3243 static int link_initialized_and_synced(Link
*link
) {
3248 assert(link
->ifname
);
3249 assert(link
->manager
);
3251 /* We may get called either from the asynchronous netlink callback,
3252 * or directly for link_add() if running in a container. See link_add(). */
3253 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3256 log_link_debug(link
, "Link state is up-to-date");
3257 link_set_state(link
, LINK_STATE_INITIALIZED
);
3259 r
= link_new_bound_by_list(link
);
3263 r
= link_handle_bound_by_list(link
);
3267 if (!link
->network
) {
3268 r
= wifi_get_info(link
);
3272 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
, link
->ifname
, link
->alternative_names
,
3273 &link
->mac
, &link
->permanent_mac
, link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3275 link_enter_unmanaged(link
);
3277 } else if (r
== 0 && network
->unmanaged
) {
3278 link_enter_unmanaged(link
);
3283 if (link
->flags
& IFF_LOOPBACK
) {
3284 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3285 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3287 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3288 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3290 if (network
->dhcp_server
)
3291 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3294 r
= network_apply(network
, link
);
3299 r
= link_new_bound_to_list(link
);
3303 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3304 * link_configure() is called later asynchronously. */
3305 r
= link_configure_duid(link
);
3309 r
= link_configure(link
);
3316 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3317 _cleanup_strv_free_
char **s
= NULL
;
3320 r
= sd_netlink_message_get_errno(m
);
3322 log_link_warning_errno(link
, r
, "Failed to wait for the interface to be initialized: %m");
3323 link_enter_failed(link
);
3327 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3328 if (r
< 0 && r
!= -ENODATA
) {
3329 link_enter_failed(link
);
3333 strv_free_and_replace(link
->alternative_names
, s
);
3335 r
= link_initialized_and_synced(link
);
3337 link_enter_failed(link
);
3341 int link_initialized(Link
*link
, sd_device
*device
) {
3342 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3346 assert(link
->manager
);
3347 assert(link
->manager
->rtnl
);
3350 if (link
->state
!= LINK_STATE_PENDING
)
3353 if (link
->sd_device
)
3356 log_link_debug(link
, "udev initialized link");
3357 link_set_state(link
, LINK_STATE_INITIALIZED
);
3359 link
->sd_device
= sd_device_ref(device
);
3361 /* udev has initialized the link, but we don't know if we have yet
3362 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3363 * when it returns we know that the pending NEWLINKs have already been
3364 * processed and that we are up-to-date */
3366 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3371 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3372 link_netlink_destroy_callback
, link
);
3381 static int link_load(Link
*link
) {
3382 _cleanup_free_
char *network_file
= NULL
,
3385 *dhcp4_address
= NULL
,
3386 *ipv4ll_address
= NULL
;
3387 union in_addr_union address
;
3393 r
= parse_env_file(NULL
, link
->state_file
,
3394 "NETWORK_FILE", &network_file
,
3395 "ADDRESSES", &addresses
,
3397 "DHCP4_ADDRESS", &dhcp4_address
,
3398 "IPV4LL_ADDRESS", &ipv4ll_address
);
3399 if (r
< 0 && r
!= -ENOENT
)
3400 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3407 suffix
= strrchr(network_file
, '.');
3409 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3410 goto network_file_fail
;
3414 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3416 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3417 goto network_file_fail
;
3420 r
= network_apply(network
, link
);
3422 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3431 _cleanup_free_
char *address_str
= NULL
;
3432 char *prefixlen_str
;
3434 unsigned char prefixlen
;
3436 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3438 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3444 prefixlen_str
= strchr(address_str
, '/');
3445 if (!prefixlen_str
) {
3446 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3450 *prefixlen_str
++ = '\0';
3452 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3454 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3458 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3460 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3464 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3466 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3474 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3475 _cleanup_(route_freep
) Route
*tmp
= NULL
;
3476 _cleanup_free_
char *route_str
= NULL
;
3477 char *prefixlen_str
;
3480 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3482 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3488 prefixlen_str
= strchr(route_str
, '/');
3489 if (!prefixlen_str
) {
3490 log_link_debug(link
, "Failed to parse route %s", route_str
);
3494 *prefixlen_str
++ = '\0';
3496 r
= route_new(&tmp
);
3500 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%"PRIu32
"/"USEC_FMT
, &tmp
->dst_prefixlen
, &tmp
->tos
, &tmp
->priority
, &tmp
->table
, &tmp
->lifetime
);
3502 log_link_debug(link
,
3503 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3508 r
= in_addr_from_string_auto(route_str
, &tmp
->family
, &tmp
->dst
);
3510 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3514 r
= route_add(link
, tmp
, &route
);
3516 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3518 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3519 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), route
->lifetime
,
3520 0, route_expire_handler
, route
);
3522 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3525 sd_event_source_unref(route
->expire
);
3526 route
->expire
= TAKE_PTR(expire
);
3530 if (dhcp4_address
) {
3531 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3533 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3534 goto dhcp4_address_fail
;
3537 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3539 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3541 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
3543 return log_link_error_errno(link
, r
, "Failed to attach DHCPv4 event: %m");
3545 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3547 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3552 if (ipv4ll_address
) {
3553 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3555 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3556 goto ipv4ll_address_fail
;
3559 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3561 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3563 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
3565 return log_link_error_errno(link
, r
, "Failed to attach IPv4LL event: %m");
3567 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3569 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3572 ipv4ll_address_fail
:
3577 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3578 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3579 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3588 r
= link_new(m
, message
, ret
);
3594 log_link_debug(link
, "Link %d added", link
->ifindex
);
3596 r
= link_load(link
);
3600 if (path_is_read_only_fs("/sys") <= 0) {
3601 /* udev should be around */
3602 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3603 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3605 log_link_warning_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
3609 r
= sd_device_get_is_initialized(device
);
3611 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
3616 log_link_debug(link
, "link pending udev initialization...");
3620 r
= device_is_renaming(device
);
3622 log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
3626 log_link_debug(link
, "Interface is being renamed, pending initialization.");
3630 r
= link_initialized(link
, device
);
3634 r
= link_initialized_and_synced(link
);
3641 link_enter_failed(link
);
3645 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3650 log_link_info(link
, "Gained IPv6LL");
3652 link
->ipv6ll_address
= *address
;
3653 link_check_ready(link
);
3655 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3656 r
= link_acquire_ipv6_conf(link
);
3658 link_enter_failed(link
);
3666 static int link_carrier_gained(Link
*link
) {
3671 r
= wifi_get_info(link
);
3675 r
= link_reconfigure_internal(link
, NULL
, false);
3677 link_enter_failed(link
);
3682 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3683 r
= link_acquire_conf(link
);
3685 link_enter_failed(link
);
3689 link_set_state(link
, LINK_STATE_CONFIGURING
);
3690 r
= link_request_set_addresses(link
);
3695 r
= link_handle_bound_by_list(link
);
3702 static int link_carrier_lost(Link
*link
) {
3707 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3710 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3711 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3712 * configuration and stop the clients as well. */
3713 if (link
->setting_mtu
)
3716 r
= link_stop_clients(link
, false);
3718 link_enter_failed(link
);
3722 if (link_dhcp4_server_enabled(link
))
3723 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3725 r
= link_drop_config(link
);
3729 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3730 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3731 r
= link_drop_foreign_config(link
);
3736 r
= link_handle_bound_by_list(link
);
3743 int link_carrier_reset(Link
*link
) {
3748 if (link_has_carrier(link
)) {
3749 r
= link_carrier_lost(link
);
3753 r
= link_carrier_gained(link
);
3757 log_link_info(link
, "Reset carrier");
3763 /* This is called every time an interface admin state changes to up;
3764 * specifically, when IFF_UP flag changes from unset to set */
3765 static int link_admin_state_up(Link
*link
) {
3768 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3769 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3770 * ipv6_mtu_set prevents this from trying to set it too early before
3771 * the link->network has been setup; we only need to reset it
3772 * here if we've already set it during normal initialization. */
3773 if (link
->ipv6_mtu_set
) {
3774 r
= link_set_ipv6_mtu(link
);
3782 int link_update(Link
*link
, sd_netlink_message
*m
) {
3783 _cleanup_strv_free_
char **s
= NULL
;
3784 struct ether_addr mac
;
3787 bool had_carrier
, carrier_gained
, carrier_lost
, link_was_admin_up
;
3791 assert(link
->ifname
);
3794 if (link
->state
== LINK_STATE_LINGER
) {
3795 log_link_info(link
, "Link re-added");
3796 link_set_state(link
, LINK_STATE_CONFIGURING
);
3798 r
= link_new_carrier_maps(link
);
3803 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3804 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3805 Manager
*manager
= link
->manager
;
3807 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3810 r
= link_add(manager
, m
, &link
);
3815 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3817 strv_free_and_replace(link
->alternative_names
, s
);
3819 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3820 if (r
>= 0 && mtu
> 0) {
3822 if (link
->original_mtu
== 0) {
3823 link
->original_mtu
= mtu
;
3824 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3827 if (link
->dhcp_client
) {
3828 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3831 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3835 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3837 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3841 /* The kernel may broadcast NEWLINK messages without the MAC address
3842 set, simply ignore them. */
3843 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3845 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3848 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3851 log_link_debug(link
, "MAC address: "
3852 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3853 mac
.ether_addr_octet
[0],
3854 mac
.ether_addr_octet
[1],
3855 mac
.ether_addr_octet
[2],
3856 mac
.ether_addr_octet
[3],
3857 mac
.ether_addr_octet
[4],
3858 mac
.ether_addr_octet
[5]);
3861 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3863 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3866 if (link
->dhcp_client
) {
3867 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3868 (const uint8_t *) &link
->mac
,
3872 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3874 r
= dhcp4_set_client_identifier(link
);
3879 if (link
->dhcp6_client
) {
3880 const DUID
* duid
= link_get_duid(link
);
3882 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3883 (const uint8_t *) &link
->mac
,
3887 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3889 if (link
->network
->iaid_set
) {
3890 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3891 link
->network
->iaid
);
3893 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3896 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3898 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3899 duid
->raw_data_len
);
3901 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3905 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3907 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3911 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3913 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3918 old_master
= link
->master_ifindex
;
3919 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
3921 link_was_admin_up
= link
->flags
& IFF_UP
;
3922 had_carrier
= link_has_carrier(link
);
3924 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
3928 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
3929 log_link_info(link
, "Link UP");
3931 r
= link_admin_state_up(link
);
3934 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
))
3935 log_link_info(link
, "Link DOWN");
3937 r
= link_update_lldp(link
);
3941 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3942 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3944 if (carrier_gained
) {
3945 log_link_info(link
, "Gained carrier");
3947 r
= link_carrier_gained(link
);
3950 } else if (carrier_lost
) {
3951 log_link_info(link
, "Lost carrier");
3953 r
= link_carrier_lost(link
);
3961 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3969 if (hashmap_isempty(h
))
3973 HASHMAP_FOREACH(link
, h
, i
) {
3977 fprintf(f
, "%i", link
->ifindex
);
3984 static void link_save_dns(FILE *f
, struct in_addr_data
*dns
, unsigned n_dns
, bool *space
) {
3985 for (unsigned j
= 0; j
< n_dns
; j
++) {
3986 _cleanup_free_
char *b
= NULL
;
3989 r
= in_addr_to_string(dns
[j
].family
, &dns
[j
].address
, &b
);
3991 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4002 static void serialize_addresses(
4007 sd_dhcp_lease
*lease
,
4009 sd_dhcp_lease_info what
,
4010 sd_dhcp6_lease
*lease6
,
4012 int (*lease6_get_addr
)(sd_dhcp6_lease
*, const struct in6_addr
**),
4013 int (*lease6_get_fqdn
)(sd_dhcp6_lease
*, char ***)) {
4016 bool _space
= false;
4021 fprintf(f
, "%s=", lvalue
);
4022 fputstrv(f
, addresses
, NULL
, space
);
4024 if (lease
&& conditional
) {
4025 const struct in_addr
*lease_addresses
;
4027 r
= sd_dhcp_lease_get_servers(lease
, what
, &lease_addresses
);
4029 if (serialize_in_addrs(f
, lease_addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4033 if (lease6
&& conditional6
&& lease6_get_addr
) {
4034 const struct in6_addr
*in6_addrs
;
4036 r
= lease6_get_addr(lease6
, &in6_addrs
);
4040 serialize_in6_addrs(f
, in6_addrs
, r
);
4045 if (lease6
&& conditional6
&& lease6_get_fqdn
) {
4048 r
= lease6_get_fqdn(lease6
, &in6_hosts
);
4050 fputstrv(f
, in6_hosts
, NULL
, space
);
4057 int link_save(Link
*link
) {
4058 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
4059 _cleanup_free_
char *temp_path
= NULL
;
4060 _cleanup_fclose_
FILE *f
= NULL
;
4067 assert(link
->state_file
);
4068 assert(link
->lease_file
);
4069 assert(link
->manager
);
4071 if (link
->state
== LINK_STATE_LINGER
) {
4072 (void) unlink(link
->state_file
);
4076 link_lldp_save(link
);
4078 admin_state
= link_state_to_string(link
->state
);
4079 assert(admin_state
);
4081 oper_state
= link_operstate_to_string(link
->operstate
);
4084 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
4085 assert(carrier_state
);
4087 address_state
= link_address_state_to_string(link
->address_state
);
4088 assert(address_state
);
4090 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4094 (void) fchmod(fileno(f
), 0644);
4097 "# This is private data. Do not parse.\n"
4100 "CARRIER_STATE=%s\n"
4101 "ADDRESS_STATE=%s\n",
4102 admin_state
, oper_state
, carrier_state
, address_state
);
4104 if (link
->network
) {
4105 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
4106 const char *dhcp_domainname
= NULL
, *p
;
4107 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
4110 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4111 yes_no(link
->network
->required_for_online
));
4113 LinkOperationalStateRange st
= link
->network
->required_operstate_for_online
;
4114 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4115 strempty(link_operstate_to_string(st
.min
)),
4116 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? ":" : "",
4117 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? strempty(link_operstate_to_string(st
.max
)) : "");
4119 if (link
->dhcp6_client
) {
4120 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
4121 if (r
< 0 && r
!= -ENOMSG
)
4122 log_link_debug_errno(link
, r
, "Failed to get DHCPv6 lease: %m");
4125 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4127 /************************************************************/
4131 if (link
->n_dns
!= (unsigned) -1)
4132 link_save_dns(f
, link
->dns
, link
->n_dns
, &space
);
4134 link_save_dns(f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
4136 serialize_addresses(f
, NULL
, &space
,
4139 link
->network
->dhcp_use_dns
,
4140 SD_DHCP_LEASE_DNS_SERVERS
,
4142 link
->network
->dhcp6_use_dns
,
4143 sd_dhcp6_lease_get_dns
,
4146 /* Make sure to flush out old entries before we use the NDISC data */
4149 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4152 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4156 serialize_in6_addrs(f
, &dd
->address
, 1);
4163 /************************************************************/
4165 serialize_addresses(f
, "NTP", NULL
,
4166 link
->ntp
?: link
->network
->ntp
,
4168 link
->network
->dhcp_use_ntp
,
4169 SD_DHCP_LEASE_NTP_SERVERS
,
4171 link
->network
->dhcp6_use_ntp
,
4172 sd_dhcp6_lease_get_ntp_addrs
,
4173 sd_dhcp6_lease_get_ntp_fqdn
);
4175 serialize_addresses(f
, "SIP", NULL
,
4178 link
->network
->dhcp_use_sip
,
4179 SD_DHCP_LEASE_SIP_SERVERS
,
4180 false, NULL
, NULL
, NULL
);
4182 serialize_addresses(f
, "POP3_SERVERS", NULL
,
4183 link
->network
->pop3
,
4186 SD_DHCP_LEASE_POP3_SERVERS
,
4187 false, NULL
, NULL
, NULL
);
4189 serialize_addresses(f
, "SMTP_SERVERS", NULL
,
4190 link
->network
->smtp
,
4193 SD_DHCP_LEASE_SMTP_SERVERS
,
4194 false, NULL
, NULL
, NULL
);
4196 serialize_addresses(f
, "LPR_SERVERS", NULL
,
4200 SD_DHCP_LEASE_LPR_SERVERS
,
4201 false, NULL
, NULL
, NULL
);
4203 /************************************************************/
4205 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4206 if (link
->dhcp_lease
) {
4207 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4208 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4211 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4214 fputs("DOMAINS=", f
);
4216 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
, i
)
4217 fputs_with_space(f
, p
, NULL
, &space
);
4219 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4220 if (dhcp_domainname
)
4221 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4223 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4225 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4228 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
4231 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4232 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4237 /************************************************************/
4239 fputs("ROUTE_DOMAINS=", f
);
4241 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
, i
)
4242 fputs_with_space(f
, p
, NULL
, &space
);
4244 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4245 if (dhcp_domainname
)
4246 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4248 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4250 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4253 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4256 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4257 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4262 /************************************************************/
4264 fprintf(f
, "LLMNR=%s\n",
4265 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
4267 /************************************************************/
4269 fprintf(f
, "MDNS=%s\n",
4270 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
4272 /************************************************************/
4274 int dns_default_route
=
4275 link
->dns_default_route
>= 0 ? link
->dns_default_route
:
4276 link
->network
->dns_default_route
;
4277 if (dns_default_route
>= 0)
4278 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route
));
4280 /************************************************************/
4282 DnsOverTlsMode dns_over_tls_mode
=
4283 link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
? link
->dns_over_tls_mode
:
4284 link
->network
->dns_over_tls_mode
;
4285 if (dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4286 fprintf(f
, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode
));
4288 /************************************************************/
4290 DnssecMode dnssec_mode
=
4291 link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
? link
->dnssec_mode
:
4292 link
->network
->dnssec_mode
;
4293 if (dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4294 fprintf(f
, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode
));
4296 /************************************************************/
4298 Set
*nta_anchors
= link
->dnssec_negative_trust_anchors
;
4299 if (set_isempty(nta_anchors
))
4300 nta_anchors
= link
->network
->dnssec_negative_trust_anchors
;
4302 if (!set_isempty(nta_anchors
)) {
4305 fputs("DNSSEC_NTA=", f
);
4307 SET_FOREACH(n
, nta_anchors
, i
)
4308 fputs_with_space(f
, n
, NULL
, &space
);
4312 /************************************************************/
4314 fputs("ADDRESSES=", f
);
4316 SET_FOREACH(a
, link
->addresses
, i
) {
4317 _cleanup_free_
char *address_str
= NULL
;
4319 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4323 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4328 /************************************************************/
4330 fputs("ROUTES=", f
);
4332 SET_FOREACH(route
, link
->routes
, i
) {
4333 _cleanup_free_
char *route_str
= NULL
;
4335 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4339 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4340 space
? " " : "", route_str
,
4341 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4348 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4349 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4351 if (link
->dhcp_lease
) {
4352 struct in_addr address
;
4353 const char *tz
= NULL
;
4355 assert(link
->network
);
4357 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4359 fprintf(f
, "TIMEZONE=%s\n", tz
);
4361 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4363 fputs("DHCP4_ADDRESS=", f
);
4364 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4368 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4376 (void) unlink(link
->lease_file
);
4379 struct in_addr address
;
4381 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4383 fputs("IPV4LL_ADDRESS=", f
);
4384 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4389 r
= fflush_and_check(f
);
4393 if (rename(temp_path
, link
->state_file
) < 0) {
4401 (void) unlink(link
->state_file
);
4403 (void) unlink(temp_path
);
4405 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4408 /* The serialized state in /run is no longer up-to-date. */
4409 void link_dirty(Link
*link
) {
4414 /* mark manager dirty as link is dirty */
4415 manager_dirty(link
->manager
);
4417 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4419 /* allocation errors are ignored */
4422 r
= set_put(link
->manager
->dirty_links
, link
);
4424 /* don't take another ref if the link was already dirty */
4430 /* The serialized state in /run is up-to-date */
4431 void link_clean(Link
*link
) {
4433 assert(link
->manager
);
4435 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4438 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4439 [LINK_STATE_PENDING
] = "pending",
4440 [LINK_STATE_INITIALIZED
] = "initialized",
4441 [LINK_STATE_CONFIGURING
] = "configuring",
4442 [LINK_STATE_CONFIGURED
] = "configured",
4443 [LINK_STATE_UNMANAGED
] = "unmanaged",
4444 [LINK_STATE_FAILED
] = "failed",
4445 [LINK_STATE_LINGER
] = "linger",
4448 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4450 int log_link_message_full_errno(Link
*link
, sd_netlink_message
*m
, int level
, int err
, const char *msg
) {
4451 const char *err_msg
= NULL
;
4453 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
4454 return log_link_full(link
, level
, err
,
4458 err_msg
&& !endswith(err_msg
, ".") ? "." : "",
4459 err_msg
? " " : "");