1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6 #include <linux/if_link.h>
9 #include "alloc-util.h"
13 #include "dhcp-identifier.h"
14 #include "dhcp-lease-internal.h"
16 #include "ethtool-util.h"
20 #include "missing_network.h"
21 #include "netlink-util.h"
22 #include "network-internal.h"
23 #include "networkd-can.h"
24 #include "networkd-dhcp-server.h"
25 #include "networkd-dhcp4.h"
26 #include "networkd-dhcp6.h"
27 #include "networkd-ipv4ll.h"
28 #include "networkd-ipv6-proxy-ndp.h"
29 #include "networkd-link-bus.h"
30 #include "networkd-link.h"
31 #include "networkd-lldp-tx.h"
32 #include "networkd-manager.h"
33 #include "networkd-ndisc.h"
34 #include "networkd-neighbor.h"
35 #include "networkd-nexthop.h"
36 #include "networkd-sriov.h"
37 #include "networkd-radv.h"
38 #include "networkd-routing-policy-rule.h"
39 #include "networkd-wifi.h"
41 #include "socket-util.h"
42 #include "stat-util.h"
43 #include "stdio-util.h"
44 #include "string-table.h"
46 #include "sysctl-util.h"
48 #include "tmpfile-util.h"
49 #include "udev-util.h"
53 uint32_t link_get_vrf_table(Link
*link
) {
54 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
57 uint32_t link_get_dhcp_route_table(Link
*link
) {
58 /* When the interface is part of an VRF use the VRFs routing table, unless
59 * another table is explicitly specified. */
60 if (link
->network
->dhcp_route_table_set
)
61 return link
->network
->dhcp_route_table
;
62 return link_get_vrf_table(link
);
65 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
66 if (link
->network
->ipv6_accept_ra_route_table_set
)
67 return link
->network
->ipv6_accept_ra_route_table
;
68 return link_get_vrf_table(link
);
71 DUID
* link_get_duid(Link
*link
) {
72 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
73 return &link
->network
->duid
;
75 return &link
->manager
->duid
;
78 static bool link_dhcp6_enabled(Link
*link
) {
81 if (!socket_ipv6_is_supported())
84 if (link
->flags
& IFF_LOOPBACK
)
90 if (link
->network
->bond
)
93 if (link
->iftype
== ARPHRD_CAN
)
96 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
99 static bool link_dhcp4_enabled(Link
*link
) {
102 if (link
->flags
& IFF_LOOPBACK
)
108 if (link
->network
->bond
)
111 if (link
->iftype
== ARPHRD_CAN
)
114 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
117 static bool link_dhcp4_server_enabled(Link
*link
) {
120 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->network
->bond
)
129 if (link
->iftype
== ARPHRD_CAN
)
132 return link
->network
->dhcp_server
;
135 bool link_ipv4ll_enabled(Link
*link
, AddressFamily mask
) {
137 assert((mask
& ~(ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) == 0);
139 if (link
->flags
& IFF_LOOPBACK
)
145 if (link
->iftype
== ARPHRD_CAN
)
148 if (STRPTR_IN_SET(link
->kind
,
149 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
150 "vti6", "nlmon", "xfrm", "bareudp"))
153 /* L3 or L3S mode do not support ARP. */
154 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
157 if (link
->network
->bond
)
160 return link
->network
->link_local
& mask
;
163 static bool link_ipv6ll_enabled(Link
*link
) {
166 if (!socket_ipv6_is_supported())
169 if (link
->flags
& IFF_LOOPBACK
)
175 if (link
->iftype
== ARPHRD_CAN
)
178 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
181 if (link
->network
->bond
)
184 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
187 static bool link_ipv6_enabled(Link
*link
) {
190 if (!socket_ipv6_is_supported())
193 if (link
->network
->bond
)
196 if (link
->iftype
== ARPHRD_CAN
)
199 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
200 if (link_ipv6ll_enabled(link
))
203 if (network_has_static_ipv6_configurations(link
->network
))
209 static bool link_radv_enabled(Link
*link
) {
212 if (!link_ipv6ll_enabled(link
))
215 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
218 static bool link_ipv4_forward_enabled(Link
*link
) {
221 if (link
->flags
& IFF_LOOPBACK
)
227 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
230 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
233 static bool link_ipv6_forward_enabled(Link
*link
) {
236 if (!socket_ipv6_is_supported())
239 if (link
->flags
& IFF_LOOPBACK
)
245 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_INVALID
)
248 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
251 static bool link_proxy_arp_enabled(Link
*link
) {
254 if (link
->flags
& IFF_LOOPBACK
)
260 if (link
->network
->proxy_arp
< 0)
266 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
269 if (!socket_ipv6_is_supported())
272 if (link
->flags
& IFF_LOOPBACK
)
278 if (!link_ipv6ll_enabled(link
))
281 /* If unset use system default (enabled if local forwarding is disabled.
282 * disabled if local forwarding is enabled).
283 * If set, ignore or enforce RA independent of local forwarding state.
285 if (link
->network
->ipv6_accept_ra
< 0)
286 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
287 return !link_ipv6_forward_enabled(link
);
288 else if (link
->network
->ipv6_accept_ra
> 0)
289 /* accept RA even if ip_forward is enabled */
296 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
299 if (!socket_ipv6_is_supported())
300 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
302 if (link
->flags
& IFF_LOOPBACK
)
303 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
306 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
308 return link
->network
->ipv6_privacy_extensions
;
311 static int link_update_ipv6_sysctl(Link
*link
) {
315 if (link
->flags
& IFF_LOOPBACK
)
318 enabled
= link_ipv6_enabled(link
);
320 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", false);
322 return log_link_warning_errno(link
, r
, "Cannot enable IPv6: %m");
324 log_link_info(link
, "IPv6 successfully enabled");
330 static bool link_is_enslaved(Link
*link
) {
331 if (link
->flags
& IFF_SLAVE
)
332 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
338 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
341 /* TODO: add conditions for other netdevs. */
346 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
352 if (netdev
->ifindex
<= 0)
355 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
358 link_update_operstate(master
, true);
361 void link_update_operstate(Link
*link
, bool also_update_master
) {
362 LinkOperationalState operstate
;
363 LinkCarrierState carrier_state
;
364 LinkAddressState address_state
;
365 _cleanup_strv_free_
char **p
= NULL
;
366 uint8_t scope
= RT_SCOPE_NOWHERE
;
367 bool changed
= false;
372 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
373 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
374 else if (link_has_carrier(link
)) {
375 if (link_is_enslaved(link
))
376 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
378 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
379 } else if (link
->flags
& IFF_UP
)
380 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
382 carrier_state
= LINK_CARRIER_STATE_OFF
;
384 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
387 SET_FOREACH(slave
, link
->slaves
) {
388 link_update_operstate(slave
, false);
390 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
391 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
395 SET_FOREACH(address
, link
->addresses
) {
396 if (!address_is_ready(address
))
399 if (address
->scope
< scope
)
400 scope
= address
->scope
;
403 /* for operstate we also take foreign addresses into account */
404 SET_FOREACH(address
, link
->addresses_foreign
) {
405 if (!address_is_ready(address
))
408 if (address
->scope
< scope
)
409 scope
= address
->scope
;
412 if (scope
< RT_SCOPE_SITE
)
413 /* universally accessible addresses found */
414 address_state
= LINK_ADDRESS_STATE_ROUTABLE
;
415 else if (scope
< RT_SCOPE_HOST
)
416 /* only link or site local addresses found */
417 address_state
= LINK_ADDRESS_STATE_DEGRADED
;
419 /* no useful addresses found */
420 address_state
= LINK_ADDRESS_STATE_OFF
;
422 /* Mapping of address and carrier state vs operational state
424 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
425 * ------------------------------------------------------------------------------
426 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
427 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
428 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
431 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
432 operstate
= (LinkOperationalState
) carrier_state
;
433 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
434 operstate
= LINK_OPERSTATE_ROUTABLE
;
435 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
436 operstate
= LINK_OPERSTATE_DEGRADED
;
438 operstate
= LINK_OPERSTATE_ENSLAVED
;
440 if (link
->carrier_state
!= carrier_state
) {
441 link
->carrier_state
= carrier_state
;
443 if (strv_extend(&p
, "CarrierState") < 0)
447 if (link
->address_state
!= address_state
) {
448 link
->address_state
= address_state
;
450 if (strv_extend(&p
, "AddressState") < 0)
454 if (link
->operstate
!= operstate
) {
455 link
->operstate
= operstate
;
457 if (strv_extend(&p
, "OperationalState") < 0)
462 link_send_changed_strv(link
, p
);
466 if (also_update_master
&& link
->network
) {
467 link_update_master_operstate(link
, link
->network
->bond
);
468 link_update_master_operstate(link
, link
->network
->bridge
);
472 #define FLAG_STRING(string, flag, old, new) \
473 (((old ^ new) & flag) \
474 ? ((old & flag) ? (" -" string) : (" +" string)) \
477 static int link_update_flags(Link
*link
, sd_netlink_message
*m
, bool force_update_operstate
) {
478 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
484 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
486 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
488 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
490 /* if we got a message without operstate, take it to mean
491 the state was unchanged */
492 operstate
= link
->kernel_operstate
;
494 if (!force_update_operstate
&& (link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
497 if (link
->flags
!= flags
) {
498 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
499 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
500 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
501 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
502 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
503 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
504 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
505 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
506 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
507 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
508 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
509 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
510 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
511 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
512 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
513 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
514 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
515 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
516 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
517 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
519 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
520 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
521 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
522 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
523 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
524 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
525 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
526 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
528 /* link flags are currently at most 18 bits, let's align to
530 if (unknown_flags_added
)
532 "Unknown link flags gained: %#.5x (ignoring)",
533 unknown_flags_added
);
535 if (unknown_flags_removed
)
537 "Unknown link flags lost: %#.5x (ignoring)",
538 unknown_flags_removed
);
542 link
->kernel_operstate
= operstate
;
544 link_update_operstate(link
, true);
549 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
550 _cleanup_(link_unrefp
) Link
*link
= NULL
;
551 const char *ifname
, *kind
= NULL
;
552 unsigned short iftype
;
560 /* check for link kind */
561 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
563 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
564 r
= sd_netlink_message_exit_container(message
);
569 r
= sd_netlink_message_get_type(message
, &type
);
572 else if (type
!= RTM_NEWLINK
)
575 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
578 else if (ifindex
<= 0)
581 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
585 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
596 .state
= LINK_STATE_PENDING
,
600 .n_dns
= (unsigned) -1,
601 .dns_default_route
= -1,
602 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
603 .mdns
= _RESOLVE_SUPPORT_INVALID
,
604 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
605 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
608 link
->ifname
= strdup(ifname
);
613 link
->kind
= strdup(kind
);
618 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
620 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
622 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
624 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
626 r
= ethtool_get_permanent_macaddr(&manager
->ethtool_fd
, link
->ifname
, &link
->permanent_mac
);
628 log_link_debug_errno(link
, r
, "Permanent MAC address not found for new device, continuing without: %m");
630 r
= ethtool_get_driver(&manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
632 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
634 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &link
->alternative_names
);
635 if (r
< 0 && r
!= -ENODATA
)
638 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
641 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
644 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
647 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
651 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
655 r
= link_update_flags(link
, message
, false);
659 *ret
= TAKE_PTR(link
);
664 void link_ntp_settings_clear(Link
*link
) {
665 link
->ntp
= strv_free(link
->ntp
);
668 void link_dns_settings_clear(Link
*link
) {
669 if (link
->n_dns
!= (unsigned) -1)
670 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
671 in_addr_full_free(link
->dns
[i
]);
672 link
->dns
= mfree(link
->dns
);
673 link
->n_dns
= (unsigned) -1;
675 link
->search_domains
= ordered_set_free_free(link
->search_domains
);
676 link
->route_domains
= ordered_set_free_free(link
->route_domains
);
678 link
->dns_default_route
= -1;
679 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
680 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
681 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
682 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
684 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
687 static void link_free_engines(Link
*link
) {
691 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
692 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
693 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
695 link
->lldp
= sd_lldp_unref(link
->lldp
);
699 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
700 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
701 link
->dhcp6_lease
= sd_dhcp6_lease_unref(link
->dhcp6_lease
);
702 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
703 link
->radv
= sd_radv_unref(link
->radv
);
706 static Link
*link_free(Link
*link
) {
711 link_ntp_settings_clear(link
);
712 link_dns_settings_clear(link
);
714 link
->routes
= set_free(link
->routes
);
715 link
->routes_foreign
= set_free(link
->routes_foreign
);
716 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
717 link
->dhcp_routes_old
= set_free(link
->dhcp_routes_old
);
718 link
->dhcp6_routes
= set_free(link
->dhcp6_routes
);
719 link
->dhcp6_routes_old
= set_free(link
->dhcp6_routes_old
);
720 link
->dhcp6_pd_routes
= set_free(link
->dhcp6_pd_routes
);
721 link
->dhcp6_pd_routes_old
= set_free(link
->dhcp6_pd_routes_old
);
722 link
->ndisc_routes
= set_free(link
->ndisc_routes
);
724 link
->nexthops
= set_free(link
->nexthops
);
725 link
->nexthops_foreign
= set_free(link
->nexthops_foreign
);
727 link
->neighbors
= set_free(link
->neighbors
);
728 link
->neighbors_foreign
= set_free(link
->neighbors_foreign
);
730 link
->addresses
= set_free(link
->addresses
);
731 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
732 link
->static_addresses
= set_free(link
->static_addresses
);
733 link
->dhcp6_addresses
= set_free(link
->dhcp6_addresses
);
734 link
->dhcp6_addresses_old
= set_free(link
->dhcp6_addresses_old
);
735 link
->dhcp6_pd_addresses
= set_free(link
->dhcp6_pd_addresses
);
736 link
->dhcp6_pd_addresses_old
= set_free(link
->dhcp6_pd_addresses_old
);
737 link
->ndisc_addresses
= set_free(link
->ndisc_addresses
);
739 while ((address
= link
->pool_addresses
)) {
740 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
741 address_free(address
);
744 link_lldp_emit_stop(link
);
745 link_free_engines(link
);
746 free(link
->lease_file
);
747 free(link
->lldp_file
);
750 strv_free(link
->alternative_names
);
755 (void) unlink(link
->state_file
);
756 free(link
->state_file
);
758 sd_device_unref(link
->sd_device
);
760 hashmap_free(link
->bound_to_links
);
761 hashmap_free(link
->bound_by_links
);
763 set_free_with_destructor(link
->slaves
, link_unref
);
765 network_unref(link
->network
);
770 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
772 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
779 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
788 void link_set_state(Link
*link
, LinkState state
) {
791 if (link
->state
== state
)
794 log_link_debug(link
, "State changed: %s -> %s",
795 link_state_to_string(link
->state
),
796 link_state_to_string(state
));
800 link_send_changed(link
, "AdministrativeState", NULL
);
803 static void link_enter_unmanaged(Link
*link
) {
806 link_set_state(link
, LINK_STATE_UNMANAGED
);
811 int link_stop_clients(Link
*link
, bool may_keep_dhcp
) {
816 assert(link
->manager
);
817 assert(link
->manager
->event
);
819 bool keep_dhcp
= may_keep_dhcp
&&
821 (link
->manager
->restarting
||
822 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
824 if (link
->dhcp_client
&& !keep_dhcp
) {
825 k
= sd_dhcp_client_stop(link
->dhcp_client
);
827 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
831 k
= sd_ipv4ll_stop(link
->ipv4ll
);
833 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
837 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
)
838 if (ad
->acd
&& sd_ipv4acd_is_running(ad
->acd
) == 0) {
839 k
= sd_ipv4acd_stop(ad
->acd
);
841 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
844 if (link
->dhcp6_client
) {
845 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
847 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
850 if (link_dhcp6_pd_is_enabled(link
)) {
851 k
= dhcp6_pd_remove(link
);
853 r
= log_link_warning_errno(link
, k
, "Could not remove DHCPv6 PD addresses and routes: %m");
857 k
= sd_ndisc_stop(link
->ndisc
);
859 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
863 k
= sd_radv_stop(link
->radv
);
865 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
868 link_lldp_emit_stop(link
);
872 void link_enter_failed(Link
*link
) {
875 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
878 log_link_warning(link
, "Failed");
880 link_set_state(link
, LINK_STATE_FAILED
);
882 link_stop_clients(link
, false);
887 static int link_join_netdevs_after_configured(Link
*link
) {
891 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
892 if (netdev
->ifindex
> 0)
893 /* Assume already enslaved. */
896 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
899 log_struct(LOG_DEBUG
,
900 LOG_LINK_INTERFACE(link
),
901 LOG_NETDEV_INTERFACE(netdev
),
902 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
904 r
= netdev_join(netdev
, link
, NULL
);
906 return log_struct_errno(LOG_WARNING
, r
,
907 LOG_LINK_INTERFACE(link
),
908 LOG_NETDEV_INTERFACE(netdev
),
909 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
915 static void link_enter_configured(Link
*link
) {
917 assert(link
->network
);
919 if (link
->state
!= LINK_STATE_CONFIGURING
)
922 link_set_state(link
, LINK_STATE_CONFIGURED
);
924 (void) link_join_netdevs_after_configured(link
);
929 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
933 assert(link
->route_messages
> 0);
934 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
935 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
937 link
->route_messages
--;
939 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
942 r
= sd_netlink_message_get_errno(m
);
943 if (r
< 0 && r
!= -EEXIST
) {
944 log_link_message_warning_errno(link
, m
, r
, "Could not set route");
945 link_enter_failed(link
);
949 if (link
->route_messages
== 0) {
950 log_link_debug(link
, "Routes set");
951 link
->static_routes_configured
= true;
952 link_set_nexthop(link
);
958 int link_request_set_routes(Link
*link
) {
960 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
961 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
968 assert(link
->network
);
969 assert(link
->state
!= _LINK_STATE_INVALID
);
971 link
->static_routes_configured
= false;
973 if (!link
->addresses_ready
)
976 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
977 /* During configuring addresses, the link lost its carrier. As networkd is dropping
978 * the addresses now, let's not configure the routes either. */
981 r
= link_set_routing_policy_rules(link
);
985 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
986 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
987 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
988 if (rt
->gateway_from_dhcp
)
991 if ((in_addr_is_null(rt
->family
, &rt
->gw
) && ordered_set_isempty(rt
->multipath_routes
)) != (phase
== PHASE_NON_GATEWAY
))
994 r
= route_configure(rt
, link
, route_handler
, NULL
);
996 return log_link_warning_errno(link
, r
, "Could not set routes: %m");
998 link
->route_messages
++;
1001 if (link
->route_messages
== 0) {
1002 link
->static_routes_configured
= true;
1003 link_set_nexthop(link
);
1005 log_link_debug(link
, "Setting routes");
1006 link_set_state(link
, LINK_STATE_CONFIGURING
);
1012 void link_check_ready(Link
*link
) {
1017 if (link
->state
== LINK_STATE_CONFIGURED
)
1020 if (link
->state
!= LINK_STATE_CONFIGURING
) {
1021 log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
1028 if (!link
->addresses_configured
) {
1029 log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
1033 if (!link
->neighbors_configured
) {
1034 log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
1038 SET_FOREACH(a
, link
->addresses
)
1039 if (!address_is_ready(a
)) {
1040 _cleanup_free_
char *str
= NULL
;
1042 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
1043 log_link_debug(link
, "%s(): an address %s/%d is not ready.", __func__
, strnull(str
), a
->prefixlen
);
1047 if (!link
->static_routes_configured
) {
1048 log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
1052 if (!link
->static_nexthops_configured
) {
1053 log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
1057 if (!link
->routing_policy_rules_configured
) {
1058 log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
1062 if (!link
->tc_configured
) {
1063 log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
1067 if (!link
->sr_iov_configured
) {
1068 log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
1072 if (!link
->bridge_mdb_configured
) {
1073 log_link_debug(link
, "%s(): Bridge MDB is not configured.", __func__
);
1077 if (link_has_carrier(link
) || !link
->network
->configure_without_carrier
) {
1078 bool has_ndisc_address
= false;
1081 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
) && !link
->ipv4ll_address_configured
) {
1082 log_link_debug(link
, "%s(): IPv4LL is not configured.", __func__
);
1086 if (link_ipv6ll_enabled(link
) &&
1087 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1088 log_link_debug(link
, "%s(): IPv6LL is not configured.", __func__
);
1092 SET_FOREACH(n
, link
->ndisc_addresses
)
1094 has_ndisc_address
= true;
1098 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
1099 !link
->dhcp_address
&& set_isempty(link
->dhcp6_addresses
) && !has_ndisc_address
&&
1100 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
1101 log_link_debug(link
, "%s(): DHCP4 or DHCP6 is enabled but no dynamic address is assigned yet.", __func__
);
1105 if (link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
) || link_dhcp6_pd_is_enabled(link
) || link_ipv6_accept_ra_enabled(link
)) {
1106 if (!link
->dhcp4_configured
&&
1107 !(link
->dhcp6_address_configured
&& link
->dhcp6_route_configured
) &&
1108 !(link
->dhcp6_pd_address_configured
&& link
->dhcp6_pd_route_configured
) &&
1109 !(link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
) &&
1110 !(link_ipv4ll_enabled(link
, ADDRESS_FAMILY_FALLBACK_IPV4
) && link
->ipv4ll_address_configured
)) {
1111 /* When DHCP or RA is enabled, at least one protocol must provide an address, or
1112 * an IPv4ll fallback address must be configured. */
1113 log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
1117 log_link_debug(link
, "%s(): dhcp4:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc_addresses:%s ndisc_routes:%s",
1119 yes_no(link
->dhcp4_configured
),
1120 yes_no(link
->dhcp6_address_configured
),
1121 yes_no(link
->dhcp6_route_configured
),
1122 yes_no(link
->dhcp6_pd_address_configured
),
1123 yes_no(link
->dhcp6_pd_route_configured
),
1124 yes_no(link
->ndisc_addresses_configured
),
1125 yes_no(link
->ndisc_routes_configured
));
1129 link_enter_configured(link
);
1134 static int link_set_bridge_fdb(Link
*link
) {
1135 FdbEntry
*fdb_entry
;
1138 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1139 r
= fdb_entry_configure(link
, fdb_entry
);
1141 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1147 static int static_address_ready_callback(Address
*address
) {
1152 assert(address
->link
);
1154 link
= address
->link
;
1156 if (!link
->addresses_configured
)
1159 SET_FOREACH(a
, link
->static_addresses
)
1160 if (!address_is_ready(a
)) {
1161 _cleanup_free_
char *str
= NULL
;
1163 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
1164 log_link_debug(link
, "an address %s/%u is not ready", strnull(str
), a
->prefixlen
);
1168 /* This should not be called again */
1169 SET_FOREACH(a
, link
->static_addresses
)
1172 link
->addresses_ready
= true;
1174 return link_request_set_routes(link
);
1177 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1183 assert(link
->ifname
);
1184 assert(link
->address_messages
> 0);
1185 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1186 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1188 link
->address_messages
--;
1190 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1193 r
= sd_netlink_message_get_errno(m
);
1194 if (r
< 0 && r
!= -EEXIST
) {
1195 log_link_message_warning_errno(link
, m
, r
, "Could not set address");
1196 link_enter_failed(link
);
1199 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1201 if (link
->address_messages
== 0) {
1204 log_link_debug(link
, "Addresses set");
1205 link
->addresses_configured
= true;
1207 /* When all static addresses are already ready, then static_address_ready_callback()
1208 * will not be called automatically. So, call it here. */
1209 a
= set_first(link
->static_addresses
);
1211 log_link_warning(link
, "No static address is stored.");
1212 link_enter_failed(link
);
1216 log_link_warning(link
, "Address ready callback is not set.");
1217 link_enter_failed(link
);
1222 link_enter_failed(link
);
1228 static int static_address_configure(Address
*address
, Link
*link
, bool update
) {
1235 r
= address_configure(address
, link
, address_handler
, update
, &ret
);
1237 return log_link_warning_errno(link
, r
, "Could not configure static address: %m");
1239 link
->address_messages
++;
1241 r
= set_ensure_put(&link
->static_addresses
, &address_hash_ops
, ret
);
1243 return log_link_warning_errno(link
, r
, "Failed to store static address: %m");
1245 ret
->callback
= static_address_ready_callback
;
1250 static int link_request_set_addresses(Link
*link
) {
1251 AddressLabel
*label
;
1257 assert(link
->network
);
1258 assert(link
->state
!= _LINK_STATE_INVALID
);
1260 if (link
->address_remove_messages
!= 0) {
1261 log_link_debug(link
, "Removing old addresses, new addresses will be configured later.");
1262 link
->request_static_addresses
= true;
1266 /* Reset all *_configured flags we are configuring. */
1267 link
->request_static_addresses
= false;
1268 link
->addresses_configured
= false;
1269 link
->addresses_ready
= false;
1270 link
->neighbors_configured
= false;
1271 link
->static_routes_configured
= false;
1272 link
->static_nexthops_configured
= false;
1273 link
->routing_policy_rules_configured
= false;
1275 r
= link_set_bridge_fdb(link
);
1279 r
= link_set_bridge_mdb(link
);
1283 r
= link_set_neighbors(link
);
1287 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1290 if (ad
->family
== AF_INET6
&& !in_addr_is_null(ad
->family
, &ad
->in_addr_peer
))
1291 update
= address_get(link
, ad
->family
, &ad
->in_addr_peer
, ad
->prefixlen
, NULL
) > 0;
1293 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1295 r
= static_address_configure(ad
, link
, update
);
1300 if (link
->network
->router_prefix_delegation
& RADV_PREFIX_DELEGATION_STATIC
)
1301 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
1302 _cleanup_(address_freep
) Address
*address
= NULL
;
1307 r
= address_new(&address
);
1311 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1313 return log_link_warning_errno(link
, r
, "Could not get RA prefix: %m");
1315 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1317 return log_link_warning_errno(link
, r
, "Could not generate EUI64 address: %m");
1319 address
->family
= AF_INET6
;
1320 r
= static_address_configure(address
, link
, true);
1325 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1326 r
= address_label_configure(label
, link
, NULL
, false);
1328 return log_link_warning_errno(link
, r
, "Could not set address label: %m");
1330 link
->address_label_messages
++;
1333 /* now that we can figure out a default address for the dhcp server, start it */
1334 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1335 r
= dhcp4_server_configure(link
);
1338 log_link_debug(link
, "Offering DHCPv4 leases");
1341 if (link
->address_messages
== 0) {
1342 link
->addresses_configured
= true;
1343 link
->addresses_ready
= true;
1344 r
= link_request_set_routes(link
);
1348 log_link_debug(link
, "Setting addresses");
1349 link_set_state(link
, LINK_STATE_CONFIGURING
);
1355 static int link_set_bridge_vlan(Link
*link
) {
1358 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1360 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1365 static int link_set_proxy_arp(Link
*link
) {
1368 if (!link_proxy_arp_enabled(link
))
1371 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1373 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1378 static int link_configure_continue(Link
*link
);
1380 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1385 assert(link
->ifname
);
1387 link
->setting_mtu
= false;
1389 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1392 r
= sd_netlink_message_get_errno(m
);
1394 log_link_message_warning_errno(link
, m
, r
, "Could not set MTU, ignoring");
1396 log_link_debug(link
, "Setting MTU done.");
1398 if (link
->state
== LINK_STATE_INITIALIZED
) {
1399 r
= link_configure_continue(link
);
1401 link_enter_failed(link
);
1407 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1408 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1412 assert(link
->manager
);
1413 assert(link
->manager
->rtnl
);
1415 if (mtu
== 0 || link
->setting_mtu
)
1418 if (link
->mtu
== mtu
)
1421 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1423 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1425 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1427 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1428 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1429 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1431 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1432 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes");
1437 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1439 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1441 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1442 link_netlink_destroy_callback
, link
);
1444 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1447 link
->setting_mtu
= true;
1452 static bool link_reduces_vlan_mtu(Link
*link
) {
1453 /* See netif_reduces_vlan_mtu() in kernel. */
1454 return streq_ptr(link
->kind
, "macsec");
1457 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
1461 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
)
1462 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
1463 /* See vlan_dev_change_mtu() in kernel. */
1464 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
1466 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
1467 /* See macvlan_change_mtu() in kernel. */
1473 static int link_configure_mtu(Link
*link
) {
1477 assert(link
->network
);
1479 if (link
->network
->mtu
> 0)
1480 return link_set_mtu(link
, link
->network
->mtu
);
1482 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
1483 if (link
->mtu
>= mtu
)
1486 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
1487 "If it is not desired, then please explicitly specify MTUBytes= setting.",
1490 return link_set_mtu(link
, mtu
);
1493 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1498 assert(link
->ifname
);
1500 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1503 r
= sd_netlink_message_get_errno(m
);
1505 log_link_message_warning_errno(link
, m
, r
, "Could not set link flags, ignoring");
1510 static int link_set_flags(Link
*link
) {
1511 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1512 unsigned ifi_change
= 0;
1513 unsigned ifi_flags
= 0;
1517 assert(link
->manager
);
1518 assert(link
->manager
->rtnl
);
1520 if (link
->flags
& IFF_LOOPBACK
)
1526 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1529 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1531 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1533 if (link
->network
->arp
>= 0) {
1534 ifi_change
|= IFF_NOARP
;
1535 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1538 if (link
->network
->multicast
>= 0) {
1539 ifi_change
|= IFF_MULTICAST
;
1540 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1543 if (link
->network
->allmulticast
>= 0) {
1544 ifi_change
|= IFF_ALLMULTI
;
1545 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1548 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1550 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1552 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1553 link_netlink_destroy_callback
, link
);
1555 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1562 static int link_acquire_ipv6_conf(Link
*link
) {
1567 if (link_ipv6_accept_ra_enabled(link
)) {
1568 assert(link
->ndisc
);
1570 log_link_debug(link
, "Discovering IPv6 routers");
1572 r
= sd_ndisc_start(link
->ndisc
);
1573 if (r
< 0 && r
!= -EBUSY
)
1574 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1577 if (link_radv_enabled(link
)) {
1579 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1581 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1583 r
= radv_emit_dns(link
);
1585 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
1587 r
= sd_radv_start(link
->radv
);
1588 if (r
< 0 && r
!= -EBUSY
)
1589 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1592 if (link_dhcp6_enabled(link
) && IN_SET(link
->network
->dhcp6_without_ra
,
1593 DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
,
1594 DHCP6_CLIENT_START_MODE_SOLICIT
)) {
1595 assert(link
->dhcp6_client
);
1596 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1598 r
= dhcp6_request_address(link
, link
->network
->dhcp6_without_ra
== DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST
);
1599 if (r
< 0 && r
!= -EBUSY
)
1600 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1602 log_link_debug(link
, "Acquiring DHCPv6 lease");
1605 r
= dhcp6_request_prefix_delegation(link
);
1607 return log_link_warning_errno(link
, r
, "Failed to request DHCPv6 prefix delegation: %m");
1612 static int link_acquire_ipv4_conf(Link
*link
) {
1616 assert(link
->manager
);
1617 assert(link
->manager
->event
);
1619 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
)) {
1620 assert(link
->ipv4ll
);
1622 log_link_debug(link
, "Acquiring IPv4 link-local address");
1624 r
= sd_ipv4ll_start(link
->ipv4ll
);
1626 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1629 if (link_dhcp4_enabled(link
)) {
1630 assert(link
->dhcp_client
);
1632 log_link_debug(link
, "Acquiring DHCPv4 lease");
1634 r
= sd_dhcp_client_start(link
->dhcp_client
);
1636 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1642 static int link_acquire_conf(Link
*link
) {
1647 r
= link_acquire_ipv4_conf(link
);
1651 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1652 r
= link_acquire_ipv6_conf(link
);
1657 if (link_lldp_emit_enabled(link
)) {
1658 r
= link_lldp_emit_start(link
);
1660 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1666 bool link_has_carrier(Link
*link
) {
1667 /* see Documentation/networking/operstates.txt in the kernel sources */
1669 if (link
->kernel_operstate
== IF_OPER_UP
)
1672 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1673 /* operstate may not be implemented, so fall back to flags */
1674 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1675 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1681 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1686 link
->setting_genmode
= false;
1688 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1691 r
= sd_netlink_message_get_errno(m
);
1693 log_link_message_warning_errno(link
, m
, r
, "Could not set address genmode for interface, ignoring");
1695 log_link_debug(link
, "Setting address genmode done.");
1697 if (link
->state
== LINK_STATE_INITIALIZED
) {
1698 r
= link_configure_continue(link
);
1700 link_enter_failed(link
);
1706 static int link_configure_addrgen_mode(Link
*link
) {
1707 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1708 uint8_t ipv6ll_mode
;
1712 assert(link
->network
);
1713 assert(link
->manager
);
1714 assert(link
->manager
->rtnl
);
1716 if (!socket_ipv6_is_supported() || link
->setting_genmode
)
1719 log_link_debug(link
, "Setting address genmode for link");
1721 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1723 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1725 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1727 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1729 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1731 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1733 if (!link_ipv6ll_enabled(link
))
1734 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1735 else if (link
->network
->ipv6ll_address_gen_mode
< 0) {
1736 r
= sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
);
1738 /* The file may not exist. And even if it exists, when stable_secret is unset,
1739 * reading the file fails with EIO. */
1740 log_link_debug_errno(link
, r
, "Failed to read sysctl property stable_secret: %m");
1742 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1744 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1746 ipv6ll_mode
= link
->network
->ipv6ll_address_gen_mode
;
1748 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1750 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1752 r
= sd_netlink_message_close_container(req
);
1754 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1756 r
= sd_netlink_message_close_container(req
);
1758 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1760 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1761 link_netlink_destroy_callback
, link
);
1763 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1766 link
->setting_genmode
= true;
1771 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1776 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1779 r
= sd_netlink_message_get_errno(m
);
1781 /* we warn but don't fail the link, as it may be brought up later */
1782 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
1787 static int link_up(Link
*link
) {
1788 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1792 assert(link
->network
);
1793 assert(link
->manager
);
1794 assert(link
->manager
->rtnl
);
1796 log_link_debug(link
, "Bringing link up");
1798 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1800 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1802 /* set it free if not enslaved with networkd */
1803 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1804 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1806 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1809 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1811 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1813 if (link
->network
->mac
) {
1814 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1816 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1819 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1820 link_netlink_destroy_callback
, link
);
1822 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1829 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1834 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1837 r
= sd_netlink_message_get_errno(m
);
1839 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
1844 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1845 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1849 assert(link
->manager
);
1850 assert(link
->manager
->rtnl
);
1852 log_link_debug(link
, "Bringing link down");
1854 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1855 RTM_SETLINK
, link
->ifindex
);
1857 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1859 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1861 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1863 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
1864 callback
?: link_down_handler
,
1865 link_netlink_destroy_callback
, link
);
1867 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1874 static int link_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1879 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1882 r
= sd_netlink_message_get_errno(m
);
1884 log_link_message_warning_errno(link
, m
, r
, "Could not set group for the interface");
1889 static int link_set_group(Link
*link
) {
1890 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1894 assert(link
->network
);
1895 assert(link
->manager
);
1896 assert(link
->manager
->rtnl
);
1898 if (link
->network
->group
<= 0)
1901 log_link_debug(link
, "Setting group");
1903 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1905 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1907 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
1909 return log_link_error_errno(link
, r
, "Could not set link group: %m");
1911 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_group_handler
,
1912 link_netlink_destroy_callback
, link
);
1914 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1921 static int link_handle_bound_to_list(Link
*link
) {
1924 bool required_up
= false;
1925 bool link_is_up
= false;
1929 if (hashmap_isempty(link
->bound_to_links
))
1932 if (link
->flags
& IFF_UP
)
1935 HASHMAP_FOREACH (l
, link
->bound_to_links
)
1936 if (link_has_carrier(l
)) {
1941 if (!required_up
&& link_is_up
) {
1942 r
= link_down(link
, NULL
);
1945 } else if (required_up
&& !link_is_up
) {
1954 static int link_handle_bound_by_list(Link
*link
) {
1960 if (hashmap_isempty(link
->bound_by_links
))
1963 HASHMAP_FOREACH (l
, link
->bound_by_links
) {
1964 r
= link_handle_bound_to_list(l
);
1972 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1978 if (link
== carrier
)
1981 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1984 r
= hashmap_ensure_allocated(h
, NULL
);
1988 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1995 static int link_new_bound_by_list(Link
*link
) {
1999 bool list_updated
= false;
2002 assert(link
->manager
);
2006 HASHMAP_FOREACH(carrier
, m
->links
) {
2007 if (!carrier
->network
)
2010 if (strv_isempty(carrier
->network
->bind_carrier
))
2013 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
2014 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2018 list_updated
= true;
2025 HASHMAP_FOREACH(carrier
, link
->bound_by_links
) {
2026 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2030 link_dirty(carrier
);
2036 static int link_new_bound_to_list(Link
*link
) {
2040 bool list_updated
= false;
2043 assert(link
->manager
);
2048 if (strv_isempty(link
->network
->bind_carrier
))
2053 HASHMAP_FOREACH (carrier
, m
->links
) {
2054 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
2055 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2059 list_updated
= true;
2066 HASHMAP_FOREACH (carrier
, link
->bound_to_links
) {
2067 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2071 link_dirty(carrier
);
2077 static int link_new_carrier_maps(Link
*link
) {
2080 r
= link_new_bound_by_list(link
);
2084 r
= link_handle_bound_by_list(link
);
2088 r
= link_new_bound_to_list(link
);
2092 r
= link_handle_bound_to_list(link
);
2099 static void link_free_bound_to_list(Link
*link
) {
2102 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
) {
2103 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2105 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2106 link_dirty(bound_to
);
2112 static void link_free_bound_by_list(Link
*link
) {
2115 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
) {
2116 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2118 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2119 link_dirty(bound_by
);
2120 link_handle_bound_to_list(bound_by
);
2127 static void link_free_carrier_maps(Link
*link
) {
2128 bool list_updated
= false;
2132 if (!hashmap_isempty(link
->bound_to_links
)) {
2133 link_free_bound_to_list(link
);
2134 list_updated
= true;
2137 if (!hashmap_isempty(link
->bound_by_links
)) {
2138 link_free_bound_by_list(link
);
2139 list_updated
= true;
2148 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2155 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2159 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
2167 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2172 if (!link
->manager
|| !netdev
)
2175 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2178 link_unref(set_remove(master
->slaves
, link
));
2181 static void link_detach_from_manager(Link
*link
) {
2182 if (!link
|| !link
->manager
)
2185 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2188 /* The following must be called at last. */
2189 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2193 void link_drop(Link
*link
) {
2194 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2197 link_set_state(link
, LINK_STATE_LINGER
);
2199 link_free_carrier_maps(link
);
2201 if (link
->network
) {
2202 link_drop_from_master(link
, link
->network
->bridge
);
2203 link_drop_from_master(link
, link
->network
->bond
);
2206 log_link_debug(link
, "Link removed");
2208 (void) unlink(link
->state_file
);
2209 link_detach_from_manager(link
);
2212 static int link_joined(Link
*link
) {
2216 assert(link
->network
);
2218 if (!hashmap_isempty(link
->bound_to_links
)) {
2219 r
= link_handle_bound_to_list(link
);
2222 } else if (!(link
->flags
& IFF_UP
)) {
2225 link_enter_failed(link
);
2230 if (link
->network
->bridge
) {
2231 r
= link_set_bridge(link
);
2233 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2235 r
= link_append_to_master(link
, link
->network
->bridge
);
2237 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2240 if (link
->network
->bond
) {
2241 r
= link_set_bond(link
);
2243 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2245 r
= link_append_to_master(link
, link
->network
->bond
);
2247 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2250 if (link
->network
->use_br_vlan
&&
2251 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2252 r
= link_set_bridge_vlan(link
);
2254 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2257 /* Skip setting up addresses until it gets carrier,
2258 or it would try to set addresses twice,
2259 which is bad for non-idempotent steps. */
2260 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2263 link_set_state(link
, LINK_STATE_CONFIGURING
);
2264 return link_request_set_addresses(link
);
2267 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2271 assert(link
->network
);
2272 assert(link
->enslaving
> 0);
2276 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2279 r
= sd_netlink_message_get_errno(m
);
2280 if (r
< 0 && r
!= -EEXIST
) {
2281 log_link_message_warning_errno(link
, m
, r
, "Could not join netdev");
2282 link_enter_failed(link
);
2286 log_link_debug(link
, "Joined netdev");
2288 if (link
->enslaving
== 0) {
2289 r
= link_joined(link
);
2291 link_enter_failed(link
);
2297 static int link_enter_join_netdev(Link
*link
) {
2302 assert(link
->network
);
2303 assert(link
->state
== LINK_STATE_INITIALIZED
);
2305 link_set_state(link
, LINK_STATE_CONFIGURING
);
2308 link
->enslaving
= 0;
2310 if (link
->network
->bond
) {
2311 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2312 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2313 return link_joined(link
);
2315 log_struct(LOG_DEBUG
,
2316 LOG_LINK_INTERFACE(link
),
2317 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2318 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2322 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2324 log_struct_errno(LOG_WARNING
, r
,
2325 LOG_LINK_INTERFACE(link
),
2326 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2327 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2328 link_enter_failed(link
);
2333 if (link
->network
->bridge
) {
2334 log_struct(LOG_DEBUG
,
2335 LOG_LINK_INTERFACE(link
),
2336 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2337 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2341 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2343 log_struct_errno(LOG_WARNING
, r
,
2344 LOG_LINK_INTERFACE(link
),
2345 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2346 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2347 link_enter_failed(link
);
2352 if (link
->network
->vrf
) {
2353 log_struct(LOG_DEBUG
,
2354 LOG_LINK_INTERFACE(link
),
2355 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2356 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2360 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2362 log_struct_errno(LOG_WARNING
, r
,
2363 LOG_LINK_INTERFACE(link
),
2364 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2365 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2366 link_enter_failed(link
);
2371 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
2373 if (netdev
->ifindex
> 0)
2374 /* Assume already enslaved. */
2377 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2380 log_struct(LOG_DEBUG
,
2381 LOG_LINK_INTERFACE(link
),
2382 LOG_NETDEV_INTERFACE(netdev
),
2383 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2387 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2389 log_struct_errno(LOG_WARNING
, r
,
2390 LOG_LINK_INTERFACE(link
),
2391 LOG_NETDEV_INTERFACE(netdev
),
2392 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2393 link_enter_failed(link
);
2398 if (link
->enslaving
== 0)
2399 return link_joined(link
);
2404 static int link_set_ipv4_forward(Link
*link
) {
2407 if (!link_ipv4_forward_enabled(link
))
2410 /* We propagate the forwarding flag from one interface to the
2411 * global setting one way. This means: as long as at least one
2412 * interface was configured at any time that had IP forwarding
2413 * enabled the setting will stay on for good. We do this
2414 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2415 * somewhat in sync (see below). */
2417 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2419 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2424 static int link_set_ipv6_forward(Link
*link
) {
2427 if (!link_ipv6_forward_enabled(link
))
2430 /* On Linux, the IPv6 stack does not know a per-interface
2431 * packet forwarding setting: either packet forwarding is on
2432 * for all, or off for all. We hence don't bother with a
2433 * per-interface setting, but simply propagate the interface
2434 * flag, if it is set, to the global flag, one-way. Note that
2435 * while IPv4 would allow a per-interface flag, we expose the
2436 * same behaviour there and also propagate the setting from
2437 * one to all, to keep things simple (see above). */
2439 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2441 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2446 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2447 IPv6PrivacyExtensions s
;
2450 s
= link_ipv6_privacy_extensions(link
);
2454 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2456 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2461 static int link_set_ipv6_accept_ra(Link
*link
) {
2464 /* Make this a NOP if IPv6 is not available */
2465 if (!socket_ipv6_is_supported())
2468 if (link
->flags
& IFF_LOOPBACK
)
2474 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2476 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2481 static int link_set_ipv6_dad_transmits(Link
*link
) {
2484 /* Make this a NOP if IPv6 is not available */
2485 if (!socket_ipv6_is_supported())
2488 if (link
->flags
& IFF_LOOPBACK
)
2494 if (link
->network
->ipv6_dad_transmits
< 0)
2497 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2499 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2504 static int link_set_ipv6_hop_limit(Link
*link
) {
2507 /* Make this a NOP if IPv6 is not available */
2508 if (!socket_ipv6_is_supported())
2511 if (link
->flags
& IFF_LOOPBACK
)
2517 if (link
->network
->ipv6_hop_limit
< 0)
2520 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2522 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2527 static int link_set_ipv6_mtu(Link
*link
) {
2530 /* Make this a NOP if IPv6 is not available */
2531 if (!socket_ipv6_is_supported())
2534 if (link
->flags
& IFF_LOOPBACK
)
2537 if (link
->network
->ipv6_mtu
== 0)
2540 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
2541 * on the interface. Bump up IPv6 MTU bytes to IPV6_MTU_MIN. */
2542 if (link
->network
->ipv6_mtu
< IPV6_MIN_MTU
) {
2543 log_link_notice(link
, "Bumping IPv6 MTU to "STRINGIFY(IPV6_MIN_MTU
)" byte minimum required");
2544 link
->network
->ipv6_mtu
= IPV6_MIN_MTU
;
2547 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2549 if (link
->mtu
< link
->network
->ipv6_mtu
)
2550 log_link_warning(link
, "Cannot set IPv6 MTU %"PRIu32
" higher than device MTU %"PRIu32
,
2551 link
->network
->ipv6_mtu
, link
->mtu
);
2553 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2556 link
->ipv6_mtu_set
= true;
2561 static int link_set_ipv4_accept_local(Link
*link
) {
2564 if (link
->flags
& IFF_LOOPBACK
)
2567 if (link
->network
->ipv4_accept_local
< 0)
2570 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "accept_local", link
->network
->ipv4_accept_local
);
2572 log_link_warning_errno(link
, r
, "Cannot set IPv4 accept_local flag for interface: %m");
2577 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2578 Address
*net_address
;
2586 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2587 if (address_equal(net_address
, address
))
2589 else if (address
->family
== AF_INET6
&& net_address
->family
== AF_INET6
&&
2590 in_addr_equal(AF_INET6
, &address
->in_addr
, &net_address
->in_addr_peer
) > 0)
2596 static bool link_is_neighbor_configured(Link
*link
, Neighbor
*neighbor
) {
2597 Neighbor
*net_neighbor
;
2605 LIST_FOREACH(neighbors
, net_neighbor
, link
->network
->neighbors
)
2606 if (neighbor_equal(net_neighbor
, neighbor
))
2612 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2621 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2622 if (route_equal(net_route
, route
))
2628 static bool link_address_is_dynamic(Link
*link
, Address
*address
) {
2634 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
2637 /* Even when the address is leased from a DHCP server, networkd assign the address
2638 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
2639 * corresponding routes with RTPROT_DHCP. */
2640 SET_FOREACH(route
, link
->routes_foreign
) {
2641 if (route
->protocol
!= RTPROT_DHCP
)
2644 if (address
->family
!= route
->family
)
2647 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
2654 static int link_enumerate_ipv6_tentative_addresses(Link
*link
) {
2655 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2656 sd_netlink_message
*addr
;
2660 assert(link
->manager
);
2661 assert(link
->manager
->rtnl
);
2663 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, 0, AF_INET6
);
2667 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
2671 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
2672 unsigned char flags
;
2675 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
2677 log_link_warning_errno(link
, r
, "rtnl: invalid ifindex, ignoring: %m");
2679 } else if (link
->ifindex
!= ifindex
)
2682 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
2684 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
2686 } else if (!(flags
& IFA_F_TENTATIVE
))
2689 log_link_debug(link
, "Found tentative ipv6 link-local address");
2690 (void) manager_rtnl_process_address(link
->manager
->rtnl
, addr
, link
->manager
);
2696 static int link_drop_foreign_config(Link
*link
) {
2702 /* The kernel doesn't notify us about tentative addresses;
2703 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
2704 if (!link_ipv6ll_enabled(link
)) {
2705 r
= link_enumerate_ipv6_tentative_addresses(link
);
2710 SET_FOREACH(address
, link
->addresses_foreign
) {
2711 /* we consider IPv6LL addresses to be managed by the kernel */
2712 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2715 if (link_address_is_dynamic(link
, address
)) {
2716 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2718 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2721 if (link_is_static_address_configured(link
, address
)) {
2722 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2724 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2726 r
= address_remove(address
, link
, NULL
);
2732 SET_FOREACH(neighbor
, link
->neighbors_foreign
) {
2733 if (link_is_neighbor_configured(link
, neighbor
)) {
2734 r
= neighbor_add(link
, neighbor
->family
, &neighbor
->in_addr
, &neighbor
->lladdr
, neighbor
->lladdr_size
, NULL
);
2738 r
= neighbor_remove(neighbor
, link
, NULL
);
2744 SET_FOREACH(route
, link
->routes_foreign
) {
2745 /* do not touch routes managed by the kernel */
2746 if (route
->protocol
== RTPROT_KERNEL
)
2749 /* do not touch multicast route added by kernel */
2750 /* FIXME: Why the kernel adds this route with protocol RTPROT_BOOT??? We need to investigate that.
2751 * https://tools.ietf.org/html/rfc4862#section-5.4 may explain why. */
2752 if (route
->protocol
== RTPROT_BOOT
&&
2753 route
->family
== AF_INET6
&&
2754 route
->dst_prefixlen
== 8 &&
2755 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 }}} }))
2758 if (route
->protocol
== RTPROT_STATIC
&& link
->network
&&
2759 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
2762 if (route
->protocol
== RTPROT_DHCP
&& link
->network
&&
2763 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
2766 if (link_is_static_route_configured(link
, route
)) {
2767 r
= route_add(link
, route
, NULL
);
2771 r
= route_remove(route
, link
, NULL
);
2780 static int remove_static_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2785 assert(link
->ifname
);
2786 assert(link
->address_remove_messages
> 0);
2788 link
->address_remove_messages
--;
2790 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2793 r
= sd_netlink_message_get_errno(m
);
2794 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
2795 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
2797 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
2799 if (link
->address_remove_messages
== 0 && link
->request_static_addresses
) {
2800 link_set_state(link
, LINK_STATE_CONFIGURING
);
2801 r
= link_request_set_addresses(link
);
2803 link_enter_failed(link
);
2809 static int link_drop_config(Link
*link
) {
2810 Address
*address
, *pool_address
;
2815 SET_FOREACH(address
, link
->addresses
) {
2816 /* we consider IPv6LL addresses to be managed by the kernel */
2817 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
2820 r
= address_remove(address
, link
, remove_static_address_handler
);
2824 link
->address_remove_messages
++;
2826 /* If this address came from an address pool, clean up the pool */
2827 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
)
2828 if (address_equal(address
, pool_address
)) {
2829 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2830 address_free(pool_address
);
2835 SET_FOREACH(neighbor
, link
->neighbors
) {
2836 r
= neighbor_remove(neighbor
, link
, NULL
);
2841 SET_FOREACH(route
, link
->routes
) {
2842 /* do not touch routes managed by the kernel */
2843 if (route
->protocol
== RTPROT_KERNEL
)
2846 r
= route_remove(route
, link
, NULL
);
2856 static int link_configure_ipv4_dad(Link
*link
) {
2861 assert(link
->network
);
2863 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
)
2864 if (address
->family
== AF_INET
&&
2865 FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
2866 r
= configure_ipv4_duplicate_address_detection(link
, address
);
2868 return log_link_error_errno(link
, r
, "Failed to configure IPv4ACD: %m");
2874 static int link_configure_traffic_control(Link
*link
) {
2878 link
->tc_configured
= false;
2879 link
->tc_messages
= 0;
2881 ORDERED_HASHMAP_FOREACH(tc
, link
->network
->tc_by_section
) {
2882 r
= traffic_control_configure(link
, tc
);
2887 if (link
->tc_messages
== 0)
2888 link
->tc_configured
= true;
2890 log_link_debug(link
, "Configuring traffic control");
2895 static int link_configure_sr_iov(Link
*link
) {
2899 link
->sr_iov_configured
= false;
2900 link
->sr_iov_messages
= 0;
2902 ORDERED_HASHMAP_FOREACH(sr_iov
, link
->network
->sr_iov_by_section
) {
2903 r
= sr_iov_configure(link
, sr_iov
);
2908 if (link
->sr_iov_messages
== 0)
2909 link
->sr_iov_configured
= true;
2911 log_link_debug(link
, "Configuring SR-IOV");
2916 static int link_configure(Link
*link
) {
2920 assert(link
->network
);
2921 assert(link
->state
== LINK_STATE_INITIALIZED
);
2923 r
= link_configure_traffic_control(link
);
2927 r
= link_configure_sr_iov(link
);
2931 if (link
->iftype
== ARPHRD_CAN
)
2932 return link_configure_can(link
);
2934 /* If IPv6 configured that is static IPv6 address and IPv6LL autoconfiguration is enabled
2935 * for this interface, then enable IPv6 */
2936 (void) link_update_ipv6_sysctl(link
);
2938 r
= link_set_proxy_arp(link
);
2942 r
= ipv6_proxy_ndp_addresses_configure(link
);
2946 r
= link_set_ipv4_forward(link
);
2950 r
= link_set_ipv6_forward(link
);
2954 r
= link_set_ipv6_privacy_extensions(link
);
2958 r
= link_set_ipv6_accept_ra(link
);
2962 r
= link_set_ipv6_dad_transmits(link
);
2966 r
= link_set_ipv6_hop_limit(link
);
2970 r
= link_set_ipv4_accept_local(link
);
2974 r
= link_set_flags(link
);
2978 r
= link_set_group(link
);
2982 if (link_ipv4ll_enabled(link
, ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
)) {
2983 r
= ipv4ll_configure(link
);
2988 if (link_dhcp4_enabled(link
)) {
2989 r
= dhcp4_set_promote_secondaries(link
);
2993 r
= dhcp4_configure(link
);
2998 if (link_dhcp4_server_enabled(link
)) {
2999 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3003 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3008 if (link_dhcp6_enabled(link
) ||
3009 link_ipv6_accept_ra_enabled(link
)) {
3010 r
= dhcp6_configure(link
);
3015 if (link_ipv6_accept_ra_enabled(link
)) {
3016 r
= ndisc_configure(link
);
3021 if (link_radv_enabled(link
)) {
3022 r
= radv_configure(link
);
3027 if (link_lldp_rx_enabled(link
)) {
3028 r
= link_lldp_rx_configure(link
);
3033 r
= link_configure_mtu(link
);
3037 r
= link_configure_addrgen_mode(link
);
3041 r
= link_configure_ipv4_dad(link
);
3045 return link_configure_continue(link
);
3048 /* The configuration continues in this separate function, instead of
3049 * including this in the above link_configure() function, for two
3051 * 1) some devices reset the link when the mtu is set, which caused
3052 * an infinite loop here in networkd; see:
3053 * https://github.com/systemd/systemd/issues/6593
3054 * https://github.com/systemd/systemd/issues/9831
3055 * 2) if ipv6ll is disabled, then bringing the interface up must be
3056 * delayed until after we get confirmation from the kernel that
3057 * the addr_gen_mode parameter has been set (via netlink), see:
3058 * https://github.com/systemd/systemd/issues/13882
3060 static int link_configure_continue(Link
*link
) {
3064 assert(link
->network
);
3065 assert(link
->state
== LINK_STATE_INITIALIZED
);
3067 if (link
->setting_mtu
|| link
->setting_genmode
)
3070 /* Drop foreign config, but ignore loopback or critical devices.
3071 * We do not want to remove loopback address or addresses used for root NFS. */
3072 if (!(link
->flags
& IFF_LOOPBACK
) &&
3073 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
3074 r
= link_drop_foreign_config(link
);
3079 /* The kernel resets ipv6 mtu after changing device mtu;
3080 * we must set this here, after we've set device mtu */
3081 r
= link_set_ipv6_mtu(link
);
3085 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3086 r
= link_acquire_conf(link
);
3091 return link_enter_join_netdev(link
);
3094 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3097 if (duid
->raw_data_len
> 0)
3100 if (duid
->type
!= DUID_TYPE_UUID
)
3103 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3104 duid
->raw_data_len
= sizeof(sd_id128_t
);
3109 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3110 Manager
*manager
= userdata
;
3111 const sd_bus_error
*e
;
3121 e
= sd_bus_message_get_error(m
);
3123 log_error_errno(sd_bus_error_get_errno(e
),
3124 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3129 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3133 if (sz
!= sizeof(sd_id128_t
)) {
3134 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3138 memcpy(&manager
->product_uuid
, a
, sz
);
3139 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3140 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3142 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3145 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3148 r
= link_configure(link
);
3150 link_enter_failed(link
);
3153 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3155 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3156 * even if the method fails. */
3157 manager
->has_product_uuid
= true;
3162 static bool link_requires_uuid(Link
*link
) {
3166 assert(link
->manager
);
3167 assert(link
->network
);
3169 duid
= link_get_duid(link
);
3170 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3173 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3176 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3182 static int link_configure_duid(Link
*link
) {
3188 assert(link
->manager
);
3189 assert(link
->network
);
3192 duid
= link_get_duid(link
);
3194 if (!link_requires_uuid(link
))
3197 if (m
->has_product_uuid
) {
3198 (void) duid_set_uuid(duid
, m
->product_uuid
);
3202 if (!m
->links_requesting_uuid
) {
3203 r
= manager_request_product_uuid(m
, link
);
3208 log_link_warning_errno(link
, r
,
3209 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3213 r
= set_put(m
->links_requesting_uuid
, link
);
3219 r
= set_put(m
->duids_requesting_uuid
, duid
);
3227 static int link_reconfigure_internal(Link
*link
, sd_netlink_message
*m
, bool force
) {
3232 _cleanup_strv_free_
char **s
= NULL
;
3234 r
= sd_netlink_message_get_errno(m
);
3238 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3239 if (r
< 0 && r
!= -ENODATA
)
3242 strv_free_and_replace(link
->alternative_names
, s
);
3245 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3246 link
->ifname
, link
->alternative_names
, link
->driver
,
3247 &link
->mac
, &link
->permanent_mac
,
3248 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3250 link_enter_unmanaged(link
);
3252 } else if (r
== 0 && network
->unmanaged
) {
3253 link_enter_unmanaged(link
);
3258 if (link
->network
== network
&& !force
)
3261 log_link_info(link
, "Re-configuring with %s", network
->filename
);
3263 /* Dropping old .network file */
3264 r
= link_stop_clients(link
, false);
3268 if (link_dhcp4_server_enabled(link
))
3269 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3271 r
= link_drop_config(link
);
3275 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3276 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3277 r
= link_drop_foreign_config(link
);
3282 link_free_carrier_maps(link
);
3283 link_free_engines(link
);
3284 link
->network
= network_unref(link
->network
);
3286 /* Then, apply new .network file */
3287 r
= network_apply(network
, link
);
3291 r
= link_new_carrier_maps(link
);
3295 link_set_state(link
, LINK_STATE_INITIALIZED
);
3298 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3299 * link_configure() is called later asynchronously. */
3300 r
= link_configure_duid(link
);
3304 r
= link_configure(link
);
3311 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3314 r
= link_reconfigure_internal(link
, m
, false);
3316 link_enter_failed(link
);
3321 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3324 r
= link_reconfigure_internal(link
, m
, true);
3326 link_enter_failed(link
);
3331 int link_reconfigure(Link
*link
, bool force
) {
3332 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3335 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
3338 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3343 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
3344 force
? link_force_reconfigure_handler
: link_reconfigure_handler
,
3345 link_netlink_destroy_callback
, link
);
3354 static int link_initialized_and_synced(Link
*link
) {
3359 assert(link
->ifname
);
3360 assert(link
->manager
);
3362 /* We may get called either from the asynchronous netlink callback,
3363 * or directly for link_add() if running in a container. See link_add(). */
3364 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3367 log_link_debug(link
, "Link state is up-to-date");
3368 link_set_state(link
, LINK_STATE_INITIALIZED
);
3370 r
= link_new_bound_by_list(link
);
3374 r
= link_handle_bound_by_list(link
);
3378 if (!link
->network
) {
3379 r
= wifi_get_info(link
);
3383 r
= network_get(link
->manager
, link
->iftype
, link
->sd_device
,
3384 link
->ifname
, link
->alternative_names
, link
->driver
,
3385 &link
->mac
, &link
->permanent_mac
,
3386 link
->wlan_iftype
, link
->ssid
, &link
->bssid
, &network
);
3388 link_enter_unmanaged(link
);
3390 } else if (r
== 0 && network
->unmanaged
) {
3391 link_enter_unmanaged(link
);
3396 if (link
->flags
& IFF_LOOPBACK
) {
3397 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3398 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3400 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3401 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3403 if (network
->dhcp_server
)
3404 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3407 r
= network_apply(network
, link
);
3412 r
= link_new_bound_to_list(link
);
3416 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3417 * link_configure() is called later asynchronously. */
3418 r
= link_configure_duid(link
);
3422 r
= link_configure(link
);
3429 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3430 _cleanup_strv_free_
char **s
= NULL
;
3433 r
= sd_netlink_message_get_errno(m
);
3435 log_link_warning_errno(link
, r
, "Failed to wait for the interface to be initialized: %m");
3436 link_enter_failed(link
);
3440 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3441 if (r
< 0 && r
!= -ENODATA
) {
3442 link_enter_failed(link
);
3446 strv_free_and_replace(link
->alternative_names
, s
);
3448 r
= link_initialized_and_synced(link
);
3450 link_enter_failed(link
);
3454 int link_initialized(Link
*link
, sd_device
*device
) {
3455 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3459 assert(link
->manager
);
3460 assert(link
->manager
->rtnl
);
3463 if (link
->state
!= LINK_STATE_PENDING
)
3466 if (link
->sd_device
)
3469 log_link_debug(link
, "udev initialized link");
3470 link_set_state(link
, LINK_STATE_INITIALIZED
);
3472 link
->sd_device
= sd_device_ref(device
);
3474 /* udev has initialized the link, but we don't know if we have yet
3475 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3476 * when it returns we know that the pending NEWLINKs have already been
3477 * processed and that we are up-to-date */
3479 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3484 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3485 link_netlink_destroy_callback
, link
);
3494 static int link_load(Link
*link
) {
3495 _cleanup_free_
char *network_file
= NULL
,
3498 *dhcp4_address
= NULL
,
3499 *ipv4ll_address
= NULL
;
3500 union in_addr_union address
;
3505 r
= parse_env_file(NULL
, link
->state_file
,
3506 "NETWORK_FILE", &network_file
,
3507 "ADDRESSES", &addresses
,
3509 "DHCP4_ADDRESS", &dhcp4_address
,
3510 "IPV4LL_ADDRESS", &ipv4ll_address
);
3511 if (r
< 0 && r
!= -ENOENT
)
3512 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3519 suffix
= strrchr(network_file
, '.');
3521 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3522 goto network_file_fail
;
3526 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3528 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3529 goto network_file_fail
;
3532 r
= network_apply(network
, link
);
3534 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3539 for (const char *p
= addresses
; p
; ) {
3540 _cleanup_free_
char *address_str
= NULL
;
3541 char *prefixlen_str
;
3543 unsigned char prefixlen
;
3545 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3547 log_link_warning_errno(link
, r
, "failed to parse ADDRESSES: %m");
3551 prefixlen_str
= strchr(address_str
, '/');
3552 if (!prefixlen_str
) {
3553 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3556 *prefixlen_str
++ = '\0';
3558 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3560 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3564 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3566 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3570 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3572 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3575 for (const char *p
= routes
; p
; ) {
3576 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3577 _cleanup_(route_freep
) Route
*tmp
= NULL
;
3578 _cleanup_free_
char *route_str
= NULL
;
3579 char *prefixlen_str
;
3582 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3584 log_link_debug_errno(link
, r
, "failed to parse ROUTES: %m");
3588 prefixlen_str
= strchr(route_str
, '/');
3589 if (!prefixlen_str
) {
3590 log_link_debug(link
, "Failed to parse route %s", route_str
);
3593 *prefixlen_str
++ = '\0';
3595 r
= route_new(&tmp
);
3599 r
= sscanf(prefixlen_str
,
3600 "%hhu/%hhu/%"SCNu32
"/%"PRIu32
"/"USEC_FMT
,
3601 &tmp
->dst_prefixlen
,
3607 log_link_debug(link
,
3608 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3613 r
= in_addr_from_string_auto(route_str
, &tmp
->family
, &tmp
->dst
);
3615 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3619 r
= route_add(link
, tmp
, &route
);
3621 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3623 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3624 r
= sd_event_add_time(link
->manager
->event
, &expire
,
3625 clock_boottime_or_monotonic(),
3626 route
->lifetime
, 0, route_expire_handler
, route
);
3628 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3631 sd_event_source_unref(route
->expire
);
3632 route
->expire
= TAKE_PTR(expire
);
3635 if (dhcp4_address
) {
3636 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3638 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3639 goto dhcp4_address_fail
;
3642 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3644 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3646 r
= sd_dhcp_client_attach_event(link
->dhcp_client
, NULL
, 0);
3648 return log_link_error_errno(link
, r
, "Failed to attach DHCPv4 event: %m");
3650 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3652 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3657 if (ipv4ll_address
) {
3658 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3660 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3661 goto ipv4ll_address_fail
;
3664 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3666 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3668 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
3670 return log_link_error_errno(link
, r
, "Failed to attach IPv4LL event: %m");
3672 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3674 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3677 ipv4ll_address_fail
:
3682 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3683 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3684 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3693 r
= link_new(m
, message
, ret
);
3699 log_link_debug(link
, "Link %d added", link
->ifindex
);
3701 r
= link_load(link
);
3705 if (path_is_read_only_fs("/sys") <= 0) {
3706 /* udev should be around */
3707 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3708 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3710 log_link_warning_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
3714 r
= sd_device_get_is_initialized(device
);
3716 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
3721 log_link_debug(link
, "link pending udev initialization...");
3725 r
= device_is_renaming(device
);
3727 log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
3731 log_link_debug(link
, "Interface is being renamed, pending initialization.");
3735 r
= link_initialized(link
, device
);
3739 r
= link_initialized_and_synced(link
);
3746 link_enter_failed(link
);
3750 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3755 log_link_info(link
, "Gained IPv6LL");
3757 link
->ipv6ll_address
= *address
;
3758 link_check_ready(link
);
3760 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3761 r
= link_acquire_ipv6_conf(link
);
3763 link_enter_failed(link
);
3771 static int link_carrier_gained(Link
*link
) {
3776 r
= wifi_get_info(link
);
3780 r
= link_reconfigure_internal(link
, NULL
, false);
3782 link_enter_failed(link
);
3787 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3788 r
= link_acquire_conf(link
);
3790 link_enter_failed(link
);
3794 link_set_state(link
, LINK_STATE_CONFIGURING
);
3795 r
= link_request_set_addresses(link
);
3800 r
= link_handle_bound_by_list(link
);
3804 if (!link
->bridge_mdb_configured
) {
3805 r
= link_set_bridge_mdb(link
);
3810 if (streq_ptr(link
->kind
, "bridge")) {
3813 SET_FOREACH(slave
, link
->slaves
) {
3814 if (slave
->bridge_mdb_configured
)
3817 r
= link_set_bridge_mdb(slave
);
3819 link_enter_failed(slave
);
3826 static int link_carrier_lost(Link
*link
) {
3831 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3834 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3835 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3836 * configuration and stop the clients as well. */
3837 if (link
->setting_mtu
)
3840 r
= link_stop_clients(link
, false);
3842 link_enter_failed(link
);
3846 if (link_dhcp4_server_enabled(link
))
3847 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3849 r
= link_drop_config(link
);
3853 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
3854 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3855 r
= link_drop_foreign_config(link
);
3860 r
= link_handle_bound_by_list(link
);
3867 int link_carrier_reset(Link
*link
) {
3872 if (link_has_carrier(link
)) {
3873 r
= link_carrier_lost(link
);
3877 r
= link_carrier_gained(link
);
3881 log_link_info(link
, "Reset carrier");
3887 /* This is called every time an interface admin state changes to up;
3888 * specifically, when IFF_UP flag changes from unset to set */
3889 static int link_admin_state_up(Link
*link
) {
3892 /* We set the ipv6 mtu after the device mtu, but the kernel resets
3893 * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for
3894 * ipv6_mtu_set prevents this from trying to set it too early before
3895 * the link->network has been setup; we only need to reset it
3896 * here if we've already set it during normal initialization. */
3897 if (link
->ipv6_mtu_set
) {
3898 r
= link_set_ipv6_mtu(link
);
3906 int link_update(Link
*link
, sd_netlink_message
*m
) {
3907 _cleanup_strv_free_
char **s
= NULL
;
3908 struct ether_addr mac
;
3911 bool had_carrier
, carrier_gained
, carrier_lost
, link_was_admin_up
;
3915 assert(link
->ifname
);
3918 if (link
->state
== LINK_STATE_LINGER
) {
3919 log_link_info(link
, "Link re-added");
3920 link_set_state(link
, LINK_STATE_CONFIGURING
);
3922 r
= link_new_carrier_maps(link
);
3927 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3928 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3929 Manager
*manager
= link
->manager
;
3931 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3934 r
= link_add(manager
, m
, &link
);
3939 r
= sd_netlink_message_read_strv(m
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &s
);
3941 strv_free_and_replace(link
->alternative_names
, s
);
3943 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3944 if (r
>= 0 && mtu
> 0) {
3946 if (link
->original_mtu
== 0) {
3947 link
->original_mtu
= mtu
;
3948 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3951 if (link
->dhcp_client
) {
3952 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3955 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3959 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3961 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3965 /* The kernel may broadcast NEWLINK messages without the MAC address
3966 set, simply ignore them. */
3967 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3968 if (r
>= 0 && memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
, ETH_ALEN
) != 0) {
3970 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
, ETH_ALEN
);
3972 log_link_debug(link
, "Gained new MAC address: "
3973 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3974 mac
.ether_addr_octet
[0],
3975 mac
.ether_addr_octet
[1],
3976 mac
.ether_addr_octet
[2],
3977 mac
.ether_addr_octet
[3],
3978 mac
.ether_addr_octet
[4],
3979 mac
.ether_addr_octet
[5]);
3982 bool restart
= sd_ipv4ll_is_running(link
->ipv4ll
) > 0;
3985 r
= sd_ipv4ll_stop(link
->ipv4ll
);
3987 return log_link_warning_errno(link
, r
, "Could not stop IPv4LL client: %m");
3990 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3992 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3995 r
= sd_ipv4ll_start(link
->ipv4ll
);
3997 return log_link_warning_errno(link
, r
, "Could not restart IPv4LL client: %m");
4001 if (link
->dhcp_client
) {
4002 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
4003 (const uint8_t *) &link
->mac
,
4007 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
4009 r
= dhcp4_set_client_identifier(link
);
4011 return log_link_warning_errno(link
, r
, "Could not set DHCP client identifier: %m");
4014 if (link
->dhcp6_client
) {
4015 const DUID
* duid
= link_get_duid(link
);
4016 bool restart
= sd_dhcp6_client_is_running(link
->dhcp6_client
) > 0;
4019 r
= sd_dhcp6_client_stop(link
->dhcp6_client
);
4021 return log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
4024 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
4025 (const uint8_t *) &link
->mac
,
4029 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
4031 if (link
->network
->iaid_set
) {
4032 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
, link
->network
->iaid
);
4034 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
4037 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
4039 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
4040 duid
->raw_data_len
);
4042 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
4045 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
4047 return log_link_warning_errno(link
, r
, "Could not restart DHCPv6 client: %m");
4052 bool restart
= sd_radv_is_running(link
->radv
);
4055 r
= sd_radv_stop(link
->radv
);
4057 return log_link_warning_errno(link
, r
, "Could not stop Router Advertisement: %m");
4060 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
4062 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
4065 r
= sd_radv_start(link
->radv
);
4067 return log_link_warning_errno(link
, r
, "Could not restart Router Advertisement: %m");
4072 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
4074 return log_link_warning_errno(link
, r
, "Could not update MAC for NDisc: %m");
4078 old_master
= link
->master_ifindex
;
4079 (void) sd_netlink_message_read_u32(m
, IFLA_MASTER
, (uint32_t *) &link
->master_ifindex
);
4081 link_was_admin_up
= link
->flags
& IFF_UP
;
4082 had_carrier
= link_has_carrier(link
);
4084 r
= link_update_flags(link
, m
, old_master
!= link
->master_ifindex
);
4088 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
4089 log_link_info(link
, "Link UP");
4091 r
= link_admin_state_up(link
);
4094 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
))
4095 log_link_info(link
, "Link DOWN");
4097 r
= link_update_lldp(link
);
4101 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
4102 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
4104 if (carrier_gained
) {
4105 log_link_info(link
, "Gained carrier");
4107 r
= link_carrier_gained(link
);
4110 } else if (carrier_lost
) {
4111 log_link_info(link
, "Lost carrier");
4113 r
= link_carrier_lost(link
);
4121 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
4128 if (hashmap_isempty(h
))
4132 HASHMAP_FOREACH(link
, h
) {
4136 fprintf(f
, "%i", link
->ifindex
);
4143 static void link_save_dns(Link
*link
, FILE *f
, struct in_addr_full
**dns
, unsigned n_dns
, bool *space
) {
4144 for (unsigned j
= 0; j
< n_dns
; j
++) {
4147 if (dns
[j
]->ifindex
!= 0 && dns
[j
]->ifindex
!= link
->ifindex
)
4150 str
= in_addr_full_to_string(dns
[j
]);
4161 static void serialize_addresses(
4166 sd_dhcp_lease
*lease
,
4168 sd_dhcp_lease_server_type what
,
4169 sd_dhcp6_lease
*lease6
,
4171 int (*lease6_get_addr
)(sd_dhcp6_lease
*, const struct in6_addr
**),
4172 int (*lease6_get_fqdn
)(sd_dhcp6_lease
*, char ***)) {
4175 bool _space
= false;
4180 fprintf(f
, "%s=", lvalue
);
4181 fputstrv(f
, addresses
, NULL
, space
);
4183 if (lease
&& conditional
) {
4184 const struct in_addr
*lease_addresses
;
4186 r
= sd_dhcp_lease_get_servers(lease
, what
, &lease_addresses
);
4188 serialize_in_addrs(f
, lease_addresses
, r
, space
, in4_addr_is_non_local
);
4191 if (lease6
&& conditional6
&& lease6_get_addr
) {
4192 const struct in6_addr
*in6_addrs
;
4194 r
= lease6_get_addr(lease6
, &in6_addrs
);
4196 serialize_in6_addrs(f
, in6_addrs
, r
, space
);
4199 if (lease6
&& conditional6
&& lease6_get_fqdn
) {
4202 r
= lease6_get_fqdn(lease6
, &in6_hosts
);
4204 fputstrv(f
, in6_hosts
, NULL
, space
);
4211 int link_save(Link
*link
) {
4212 const char *admin_state
, *oper_state
, *carrier_state
, *address_state
;
4213 _cleanup_free_
char *temp_path
= NULL
;
4214 _cleanup_fclose_
FILE *f
= NULL
;
4220 assert(link
->state_file
);
4221 assert(link
->lease_file
);
4222 assert(link
->manager
);
4224 if (link
->state
== LINK_STATE_LINGER
) {
4225 (void) unlink(link
->state_file
);
4229 link_lldp_save(link
);
4231 admin_state
= link_state_to_string(link
->state
);
4232 assert(admin_state
);
4234 oper_state
= link_operstate_to_string(link
->operstate
);
4237 carrier_state
= link_carrier_state_to_string(link
->carrier_state
);
4238 assert(carrier_state
);
4240 address_state
= link_address_state_to_string(link
->address_state
);
4241 assert(address_state
);
4243 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
4247 (void) fchmod(fileno(f
), 0644);
4250 "# This is private data. Do not parse.\n"
4253 "CARRIER_STATE=%s\n"
4254 "ADDRESS_STATE=%s\n",
4255 admin_state
, oper_state
, carrier_state
, address_state
);
4257 if (link
->network
) {
4258 char **dhcp6_domains
= NULL
, **dhcp_domains
= NULL
;
4259 const char *dhcp_domainname
= NULL
, *p
;
4262 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
4263 yes_no(link
->network
->required_for_online
));
4265 LinkOperationalStateRange st
= link
->network
->required_operstate_for_online
;
4266 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n",
4267 strempty(link_operstate_to_string(st
.min
)),
4268 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? ":" : "",
4269 st
.max
!= LINK_OPERSTATE_RANGE_DEFAULT
.max
? strempty(link_operstate_to_string(st
.max
)) : "");
4271 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
4273 /************************************************************/
4277 if (link
->n_dns
!= (unsigned) -1)
4278 link_save_dns(link
, f
, link
->dns
, link
->n_dns
, &space
);
4280 link_save_dns(link
, f
, link
->network
->dns
, link
->network
->n_dns
, &space
);
4282 serialize_addresses(f
, NULL
, &space
,
4285 link
->network
->dhcp_use_dns
,
4288 link
->network
->dhcp6_use_dns
,
4289 sd_dhcp6_lease_get_dns
,
4292 /* Make sure to flush out old entries before we use the NDisc data */
4295 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4298 SET_FOREACH(dd
, link
->ndisc_rdnss
)
4299 serialize_in6_addrs(f
, &dd
->address
, 1, &space
);
4304 /************************************************************/
4306 serialize_addresses(f
, "NTP", NULL
,
4307 link
->ntp
?: link
->network
->ntp
,
4309 link
->network
->dhcp_use_ntp
,
4312 link
->network
->dhcp6_use_ntp
,
4313 sd_dhcp6_lease_get_ntp_addrs
,
4314 sd_dhcp6_lease_get_ntp_fqdn
);
4316 serialize_addresses(f
, "SIP", NULL
,
4319 link
->network
->dhcp_use_sip
,
4321 NULL
, false, NULL
, NULL
);
4323 /************************************************************/
4325 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4326 if (link
->dhcp_lease
) {
4327 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4328 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4330 if (link
->dhcp6_lease
)
4331 (void) sd_dhcp6_lease_get_domains(link
->dhcp6_lease
, &dhcp6_domains
);
4334 fputs("DOMAINS=", f
);
4336 ORDERED_SET_FOREACH(p
, link
->search_domains
?: link
->network
->search_domains
)
4337 fputs_with_space(f
, p
, NULL
, &space
);
4339 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4340 if (dhcp_domainname
)
4341 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4343 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4345 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4348 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_YES
) {
4351 SET_FOREACH(dd
, link
->ndisc_dnssl
)
4352 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4357 /************************************************************/
4359 fputs("ROUTE_DOMAINS=", f
);
4361 ORDERED_SET_FOREACH(p
, link
->route_domains
?: link
->network
->route_domains
)
4362 fputs_with_space(f
, p
, NULL
, &space
);
4364 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4365 if (dhcp_domainname
)
4366 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4368 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4370 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4373 if (link
->network
->ipv6_accept_ra_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4376 SET_FOREACH(dd
, link
->ndisc_dnssl
)
4377 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4382 /************************************************************/
4384 fprintf(f
, "LLMNR=%s\n",
4385 resolve_support_to_string(link
->llmnr
>= 0 ? link
->llmnr
: link
->network
->llmnr
));
4387 /************************************************************/
4389 fprintf(f
, "MDNS=%s\n",
4390 resolve_support_to_string(link
->mdns
>= 0 ? link
->mdns
: link
->network
->mdns
));
4392 /************************************************************/
4394 int dns_default_route
=
4395 link
->dns_default_route
>= 0 ? link
->dns_default_route
:
4396 link
->network
->dns_default_route
;
4397 if (dns_default_route
>= 0)
4398 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route
));
4400 /************************************************************/
4402 DnsOverTlsMode dns_over_tls_mode
=
4403 link
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
? link
->dns_over_tls_mode
:
4404 link
->network
->dns_over_tls_mode
;
4405 if (dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4406 fprintf(f
, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode
));
4408 /************************************************************/
4410 DnssecMode dnssec_mode
=
4411 link
->dnssec_mode
!= _DNSSEC_MODE_INVALID
? link
->dnssec_mode
:
4412 link
->network
->dnssec_mode
;
4413 if (dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4414 fprintf(f
, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode
));
4416 /************************************************************/
4418 Set
*nta_anchors
= link
->dnssec_negative_trust_anchors
;
4419 if (set_isempty(nta_anchors
))
4420 nta_anchors
= link
->network
->dnssec_negative_trust_anchors
;
4422 if (!set_isempty(nta_anchors
)) {
4425 fputs("DNSSEC_NTA=", f
);
4427 SET_FOREACH(n
, nta_anchors
)
4428 fputs_with_space(f
, n
, NULL
, &space
);
4432 /************************************************************/
4434 fputs("ADDRESSES=", f
);
4436 SET_FOREACH(a
, link
->addresses
) {
4437 _cleanup_free_
char *address_str
= NULL
;
4439 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4443 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4448 /************************************************************/
4450 fputs("ROUTES=", f
);
4452 SET_FOREACH(route
, link
->routes
) {
4453 _cleanup_free_
char *route_str
= NULL
;
4455 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4459 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4460 space
? " " : "", route_str
,
4461 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4468 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4469 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4471 if (link
->dhcp_lease
) {
4472 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4480 (void) unlink(link
->lease_file
);
4483 struct in_addr address
;
4485 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4487 fputs("IPV4LL_ADDRESS=", f
);
4488 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4493 if (link
->dhcp6_client
) {
4494 _cleanup_free_
char *duid
= NULL
;
4497 r
= sd_dhcp6_client_get_iaid(link
->dhcp6_client
, &iaid
);
4499 fprintf(f
, "DHCP6_CLIENT_IAID=0x%x\n", iaid
);
4501 r
= sd_dhcp6_client_duid_as_string(link
->dhcp6_client
, &duid
);
4503 fprintf(f
, "DHCP6_CLIENT_DUID=%s\n", duid
);
4506 r
= fflush_and_check(f
);
4510 if (rename(temp_path
, link
->state_file
) < 0) {
4518 (void) unlink(link
->state_file
);
4520 (void) unlink(temp_path
);
4522 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4525 /* The serialized state in /run is no longer up-to-date. */
4526 void link_dirty(Link
*link
) {
4531 /* mark manager dirty as link is dirty */
4532 manager_dirty(link
->manager
);
4534 r
= set_ensure_put(&link
->manager
->dirty_links
, NULL
, link
);
4536 /* Ignore allocation errors and don't take another ref if the link was already dirty */
4541 /* The serialized state in /run is up-to-date */
4542 void link_clean(Link
*link
) {
4544 assert(link
->manager
);
4546 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4549 int link_save_and_clean(Link
*link
) {
4552 r
= link_save(link
);
4560 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4561 [LINK_STATE_PENDING
] = "pending",
4562 [LINK_STATE_INITIALIZED
] = "initialized",
4563 [LINK_STATE_CONFIGURING
] = "configuring",
4564 [LINK_STATE_CONFIGURED
] = "configured",
4565 [LINK_STATE_UNMANAGED
] = "unmanaged",
4566 [LINK_STATE_FAILED
] = "failed",
4567 [LINK_STATE_LINGER
] = "linger",
4570 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4572 int log_link_message_full_errno(Link
*link
, sd_netlink_message
*m
, int level
, int err
, const char *msg
) {
4573 const char *err_msg
= NULL
;
4575 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
4576 return log_link_full_errno(link
, level
, err
,
4580 err_msg
&& !endswith(err_msg
, ".") ? "." : "",
4581 err_msg
? " " : "");