1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
16 #include "missing_network.h"
17 #include "netdev/vrf.h"
18 #include "netlink-util.h"
19 #include "network-internal.h"
20 #include "networkd-ipv6-proxy-ndp.h"
21 #include "networkd-lldp-tx.h"
22 #include "networkd-manager.h"
23 #include "networkd-ndisc.h"
24 #include "networkd-neighbor.h"
25 #include "networkd-radv.h"
26 #include "networkd-routing-policy-rule.h"
28 #include "socket-util.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
32 #include "tmpfile-util.h"
36 uint32_t link_get_vrf_table(Link
*link
) {
37 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
40 uint32_t link_get_dhcp_route_table(Link
*link
) {
41 /* When the interface is part of an VRF use the VRFs routing table, unless
42 * another table is explicitly specified. */
43 if (link
->network
->dhcp_route_table_set
)
44 return link
->network
->dhcp_route_table
;
45 return link_get_vrf_table(link
);
48 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
49 if (link
->network
->ipv6_accept_ra_route_table_set
)
50 return link
->network
->ipv6_accept_ra_route_table
;
51 return link_get_vrf_table(link
);
54 DUID
* link_get_duid(Link
*link
) {
55 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
56 return &link
->network
->duid
;
58 return &link
->manager
->duid
;
61 static bool link_dhcp6_enabled(Link
*link
) {
64 if (!socket_ipv6_is_supported())
67 if (link
->flags
& IFF_LOOPBACK
)
73 if (link
->network
->bond
)
76 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
79 static bool link_dhcp4_enabled(Link
*link
) {
82 if (link
->flags
& IFF_LOOPBACK
)
88 if (link
->network
->bond
)
91 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
94 static bool link_dhcp4_server_enabled(Link
*link
) {
97 if (link
->flags
& IFF_LOOPBACK
)
103 if (link
->network
->bond
)
106 return link
->network
->dhcp_server
;
109 static bool link_ipv4ll_enabled(Link
*link
) {
112 if (link
->flags
& IFF_LOOPBACK
)
118 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
121 if (link
->network
->bond
)
124 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
127 static bool link_ipv6ll_enabled(Link
*link
) {
130 if (!socket_ipv6_is_supported())
133 if (link
->flags
& IFF_LOOPBACK
)
139 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
142 if (link
->network
->bond
)
145 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
148 static bool link_ipv6_enabled(Link
*link
) {
151 if (!socket_ipv6_is_supported())
154 if (link
->network
->bridge
|| link
->network
->bond
)
157 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
158 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
161 static bool link_radv_enabled(Link
*link
) {
164 if (!link_ipv6ll_enabled(link
))
167 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
170 static bool link_lldp_rx_enabled(Link
*link
) {
173 if (link
->flags
& IFF_LOOPBACK
)
176 if (link
->iftype
!= ARPHRD_ETHER
)
182 /* LLDP should be handled on bridge slaves as those have a direct
183 * connection to their peers not on the bridge master. Linux doesn't
184 * even (by default) forward lldp packets to the bridge master.*/
185 if (streq_ptr("bridge", link
->kind
))
188 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
191 static bool link_lldp_emit_enabled(Link
*link
) {
194 if (link
->flags
& IFF_LOOPBACK
)
197 if (link
->iftype
!= ARPHRD_ETHER
)
203 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
206 static bool link_ipv4_forward_enabled(Link
*link
) {
209 if (link
->flags
& IFF_LOOPBACK
)
215 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
218 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
221 static bool link_ipv6_forward_enabled(Link
*link
) {
224 if (!socket_ipv6_is_supported())
227 if (link
->flags
& IFF_LOOPBACK
)
233 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
236 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
239 static bool link_proxy_arp_enabled(Link
*link
) {
242 if (link
->flags
& IFF_LOOPBACK
)
248 if (link
->network
->proxy_arp
< 0)
254 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
257 if (!socket_ipv6_is_supported())
260 if (link
->flags
& IFF_LOOPBACK
)
266 if (!link_ipv6ll_enabled(link
))
269 /* If unset use system default (enabled if local forwarding is disabled.
270 * disabled if local forwarding is enabled).
271 * If set, ignore or enforce RA independent of local forwarding state.
273 if (link
->network
->ipv6_accept_ra
< 0)
274 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
275 return !link_ipv6_forward_enabled(link
);
276 else if (link
->network
->ipv6_accept_ra
> 0)
277 /* accept RA even if ip_forward is enabled */
284 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
287 if (!socket_ipv6_is_supported())
288 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
290 if (link
->flags
& IFF_LOOPBACK
)
291 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
294 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
296 return link
->network
->ipv6_privacy_extensions
;
299 static int link_enable_ipv6(Link
*link
) {
300 const char *p
= NULL
;
304 if (link
->flags
& IFF_LOOPBACK
)
307 disabled
= !link_ipv6_enabled(link
);
309 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
311 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
313 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
314 enable_disable(!disabled
), link
->ifname
);
316 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
321 void link_update_operstate(Link
*link
, bool also_update_bond_master
) {
322 LinkOperationalState operstate
;
326 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
327 operstate
= LINK_OPERSTATE_DORMANT
;
328 else if (link_has_carrier(link
)) {
330 uint8_t scope
= RT_SCOPE_NOWHERE
;
333 /* if we have carrier, check what addresses we have */
334 SET_FOREACH(address
, link
->addresses
, i
) {
335 if (!address_is_ready(address
))
338 if (address
->scope
< scope
)
339 scope
= address
->scope
;
342 /* for operstate we also take foreign addresses into account */
343 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
344 if (!address_is_ready(address
))
347 if (address
->scope
< scope
)
348 scope
= address
->scope
;
351 if (scope
< RT_SCOPE_SITE
)
352 /* universally accessible addresses found */
353 operstate
= LINK_OPERSTATE_ROUTABLE
;
354 else if (scope
< RT_SCOPE_HOST
)
355 /* only link or site local addresses found */
356 operstate
= LINK_OPERSTATE_DEGRADED
;
358 /* no useful addresses found */
359 operstate
= LINK_OPERSTATE_CARRIER
;
360 } else if (link
->flags
& IFF_UP
)
361 operstate
= LINK_OPERSTATE_NO_CARRIER
;
363 operstate
= LINK_OPERSTATE_OFF
;
365 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
366 link
->flags
& IFF_SLAVE
)
367 operstate
= LINK_OPERSTATE_ENSLAVED
;
369 if (IN_SET(operstate
, LINK_OPERSTATE_CARRIER
, LINK_OPERSTATE_ENSLAVED
, LINK_OPERSTATE_ROUTABLE
) &&
370 !hashmap_isempty(link
->bond_slaves
)) {
374 HASHMAP_FOREACH(slave
, link
->bond_slaves
, i
) {
375 link_update_operstate(slave
, false);
377 if (IN_SET(slave
->operstate
,
378 LINK_OPERSTATE_OFF
, LINK_OPERSTATE_NO_CARRIER
, LINK_OPERSTATE_DORMANT
))
379 operstate
= LINK_OPERSTATE_DEGRADED
;
383 if (link
->operstate
!= operstate
) {
384 link
->operstate
= operstate
;
385 link_send_changed(link
, "OperationalState", NULL
);
389 if (also_update_bond_master
&& link
->network
&& link
->network
->bond
) {
392 if (link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
) < 0)
395 link_update_operstate(master
, true);
399 #define FLAG_STRING(string, flag, old, new) \
400 (((old ^ new) & flag) \
401 ? ((old & flag) ? (" -" string) : (" +" string)) \
404 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
405 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
411 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
413 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
415 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
417 /* if we got a message without operstate, take it to mean
418 the state was unchanged */
419 operstate
= link
->kernel_operstate
;
421 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
424 if (link
->flags
!= flags
) {
425 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",
426 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
427 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
428 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
429 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
430 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
431 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
432 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
433 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
434 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
435 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
436 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
437 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
438 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
439 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
440 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
441 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
442 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
443 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
444 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
446 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
447 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
448 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
449 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
450 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
451 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
452 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
453 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
455 /* link flags are currently at most 18 bits, let's align to
457 if (unknown_flags_added
)
459 "Unknown link flags gained: %#.5x (ignoring)",
460 unknown_flags_added
);
462 if (unknown_flags_removed
)
464 "Unknown link flags lost: %#.5x (ignoring)",
465 unknown_flags_removed
);
469 link
->kernel_operstate
= operstate
;
471 link_update_operstate(link
, true);
476 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
478 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
479 _cleanup_(link_unrefp
) Link
*link
= NULL
;
481 const char *ifname
, *kind
= NULL
;
483 unsigned short iftype
;
489 /* check for link kind */
490 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
492 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
493 r
= sd_netlink_message_exit_container(message
);
498 r
= sd_netlink_message_get_type(message
, &type
);
501 else if (type
!= RTM_NEWLINK
)
504 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
507 else if (ifindex
<= 0)
510 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
514 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
525 .state
= LINK_STATE_PENDING
,
526 .rtnl_extended_attrs
= true,
531 link
->ifname
= strdup(ifname
);
536 link
->kind
= strdup(kind
);
541 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
543 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
545 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
547 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
549 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
552 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
555 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
558 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
562 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
566 r
= link_update_flags(link
, message
);
570 *ret
= TAKE_PTR(link
);
575 static void link_detach_from_manager(Link
*link
) {
576 if (!link
|| !link
->manager
)
579 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
580 set_remove(link
->manager
->links_requesting_uuid
, link
);
584 static Link
*link_free(Link
*link
) {
592 while ((route
= set_first(link
->routes
)))
595 while ((route
= set_first(link
->routes_foreign
)))
598 link
->routes
= set_free(link
->routes
);
599 link
->routes_foreign
= set_free(link
->routes_foreign
);
601 while ((address
= set_first(link
->addresses
)))
602 address_free(address
);
604 while ((address
= set_first(link
->addresses_foreign
)))
605 address_free(address
);
607 link
->addresses
= set_free(link
->addresses
);
608 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
610 while ((address
= link
->pool_addresses
)) {
611 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
612 address_free(address
);
615 sd_dhcp_server_unref(link
->dhcp_server
);
616 sd_dhcp_client_unref(link
->dhcp_client
);
617 sd_dhcp_lease_unref(link
->dhcp_lease
);
619 link_lldp_emit_stop(link
);
621 free(link
->lease_file
);
623 sd_lldp_unref(link
->lldp
);
624 free(link
->lldp_file
);
628 sd_ipv4ll_unref(link
->ipv4ll
);
629 sd_dhcp6_client_unref(link
->dhcp6_client
);
630 sd_ndisc_unref(link
->ndisc
);
631 sd_radv_unref(link
->radv
);
633 link_detach_from_manager(link
);
639 (void) unlink(link
->state_file
);
640 free(link
->state_file
);
642 sd_device_unref(link
->sd_device
);
644 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
645 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
646 hashmap_free(link
->bound_to_links
);
648 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
649 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
650 hashmap_free(link
->bound_by_links
);
652 hashmap_free(link
->bond_slaves
);
657 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
659 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
666 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
675 static void link_set_state(Link
*link
, LinkState state
) {
678 if (link
->state
== state
)
683 link_send_changed(link
, "AdministrativeState", NULL
);
686 static void link_enter_unmanaged(Link
*link
) {
689 log_link_debug(link
, "Unmanaged");
691 link_set_state(link
, LINK_STATE_UNMANAGED
);
696 static int link_stop_clients(Link
*link
) {
700 assert(link
->manager
);
701 assert(link
->manager
->event
);
703 if (link
->dhcp_client
) {
704 k
= sd_dhcp_client_stop(link
->dhcp_client
);
706 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
710 k
= sd_ipv4ll_stop(link
->ipv4ll
);
712 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
715 if (link
->dhcp6_client
) {
716 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
718 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
722 k
= sd_ndisc_stop(link
->ndisc
);
724 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
728 k
= sd_radv_stop(link
->radv
);
730 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
733 link_lldp_emit_stop(link
);
737 void link_enter_failed(Link
*link
) {
740 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
743 log_link_warning(link
, "Failed");
745 link_set_state(link
, LINK_STATE_FAILED
);
747 link_stop_clients(link
);
752 static Address
* link_find_dhcp_server_address(Link
*link
) {
756 assert(link
->network
);
758 /* The first statically configured address if there is any */
759 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
761 if (address
->family
!= AF_INET
)
764 if (in_addr_is_null(address
->family
, &address
->in_addr
))
770 /* If that didn't work, find a suitable address we got from the pool */
771 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
772 if (address
->family
!= AF_INET
)
781 static void link_enter_configured(Link
*link
) {
783 assert(link
->network
);
785 if (link
->state
!= LINK_STATE_CONFIGURING
)
788 log_link_info(link
, "Configured");
790 link_set_state(link
, LINK_STATE_CONFIGURED
);
795 static int link_request_set_routing_policy_rule(Link
*link
) {
796 RoutingPolicyRule
*rule
, *rrule
= NULL
;
800 assert(link
->network
);
802 link_set_state(link
, LINK_STATE_CONFIGURING
);
803 link
->routing_policy_rules_configured
= false;
805 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
806 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
807 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
808 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
810 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
814 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
816 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
817 link_enter_failed(link
);
821 link
->routing_policy_rule_messages
++;
824 routing_policy_rule_purge(link
->manager
, link
);
825 if (link
->routing_policy_rule_messages
== 0) {
826 link
->routing_policy_rules_configured
= true;
827 link_check_ready(link
);
829 log_link_debug(link
, "Setting routing policy rules");
834 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
838 assert(link
->route_messages
> 0);
839 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
840 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
842 link
->route_messages
--;
844 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
847 r
= sd_netlink_message_get_errno(m
);
848 if (r
< 0 && r
!= -EEXIST
)
849 log_link_warning_errno(link
, r
, "Could not set route: %m");
851 if (link
->route_messages
== 0) {
852 log_link_debug(link
, "Routes set");
853 link
->static_routes_configured
= true;
854 link_check_ready(link
);
860 static int link_request_set_routes(Link
*link
) {
862 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
863 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
870 assert(link
->network
);
871 assert(link
->addresses_configured
);
872 assert(link
->address_messages
== 0);
873 assert(link
->state
!= _LINK_STATE_INVALID
);
875 link_set_state(link
, LINK_STATE_CONFIGURING
);
876 link
->static_routes_configured
= false;
878 r
= link_request_set_routing_policy_rule(link
);
882 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
883 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
884 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
886 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
889 r
= route_configure(rt
, link
, route_handler
);
891 log_link_warning_errno(link
, r
, "Could not set routes: %m");
892 link_enter_failed(link
);
896 link
->route_messages
++;
899 if (link
->route_messages
== 0) {
900 link
->static_routes_configured
= true;
901 link_check_ready(link
);
903 log_link_debug(link
, "Setting routes");
908 void link_check_ready(Link
*link
) {
914 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
920 if (!link
->addresses_configured
)
923 if (!link
->neighbors_configured
)
926 SET_FOREACH(a
, link
->addresses
, i
)
927 if (!address_is_ready(a
))
930 if (!link
->addresses_ready
) {
931 link
->addresses_ready
= true;
932 link_request_set_routes(link
);
935 if (!link
->static_routes_configured
)
938 if (!link
->routing_policy_rules_configured
)
941 if (link_ipv4ll_enabled(link
))
942 if (!link
->ipv4ll_address
||
946 if (!link
->network
->bridge
) {
948 if (link_ipv6ll_enabled(link
) &&
949 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
952 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
953 !link
->dhcp4_configured
) ||
954 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
955 !link
->dhcp6_configured
) ||
956 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
957 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
960 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
964 if (link
->state
!= LINK_STATE_CONFIGURED
)
965 link_enter_configured(link
);
970 static int link_request_set_neighbors(Link
*link
) {
975 assert(link
->network
);
976 assert(link
->state
!= _LINK_STATE_INVALID
);
978 link_set_state(link
, LINK_STATE_CONFIGURING
);
979 link
->neighbors_configured
= false;
981 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
982 r
= neighbor_configure(neighbor
, link
, NULL
);
984 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
985 link_enter_failed(link
);
990 if (link
->neighbor_messages
== 0) {
991 link
->neighbors_configured
= true;
992 link_check_ready(link
);
994 log_link_debug(link
, "Setting neighbors");
999 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1005 assert(link
->ifname
);
1006 assert(link
->address_messages
> 0);
1007 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1008 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1010 link
->address_messages
--;
1012 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1015 r
= sd_netlink_message_get_errno(m
);
1016 if (r
< 0 && r
!= -EEXIST
)
1017 log_link_warning_errno(link
, r
, "could not set address: %m");
1019 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1021 if (link
->address_messages
== 0) {
1022 log_link_debug(link
, "Addresses set");
1023 link
->addresses_configured
= true;
1024 link_check_ready(link
);
1030 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1031 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1032 size_t n_addresses
= 0, n_allocated
= 0;
1035 log_debug("Copying DNS server information from %s", link
->ifname
);
1040 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1043 /* Only look for IPv4 addresses */
1044 if (link
->network
->dns
[i
].family
!= AF_INET
)
1047 ia
= link
->network
->dns
[i
].address
.in
;
1049 /* Never propagate obviously borked data */
1050 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1053 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1056 addresses
[n_addresses
++] = ia
;
1059 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1060 const struct in_addr
*da
= NULL
;
1063 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1066 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1069 for (j
= 0; j
< n
; j
++)
1070 if (in4_addr_is_non_local(&da
[j
]))
1071 addresses
[n_addresses
++] = da
[j
];
1075 if (n_addresses
<= 0)
1078 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1081 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1082 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1083 size_t n_addresses
= 0, n_allocated
= 0;
1089 log_debug("Copying NTP server information from %s", link
->ifname
);
1091 STRV_FOREACH(a
, link
->network
->ntp
) {
1094 /* Only look for IPv4 addresses */
1095 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1098 /* Never propagate obviously borked data */
1099 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1102 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1105 addresses
[n_addresses
++] = ia
;
1108 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1109 const struct in_addr
*da
= NULL
;
1112 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1115 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1118 for (j
= 0; j
< n
; j
++)
1119 if (in4_addr_is_non_local(&da
[j
]))
1120 addresses
[n_addresses
++] = da
[j
];
1124 if (n_addresses
<= 0)
1127 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1130 static int link_set_bridge_fdb(Link
*link
) {
1131 FdbEntry
*fdb_entry
;
1134 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1135 r
= fdb_entry_configure(link
, fdb_entry
);
1137 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1143 static int link_request_set_addresses(Link
*link
) {
1144 AddressLabel
*label
;
1149 assert(link
->network
);
1150 assert(link
->state
!= _LINK_STATE_INVALID
);
1152 link_set_state(link
, LINK_STATE_CONFIGURING
);
1154 /* Reset all *_configured flags we are configuring. */
1155 link
->addresses_configured
= false;
1156 link
->addresses_ready
= false;
1157 link
->neighbors_configured
= false;
1158 link
->static_routes_configured
= false;
1159 link
->routing_policy_rules_configured
= false;
1161 r
= link_set_bridge_fdb(link
);
1165 r
= link_request_set_neighbors(link
);
1169 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1172 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1174 r
= address_configure(ad
, link
, address_handler
, update
);
1176 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1177 link_enter_failed(link
);
1181 link
->address_messages
++;
1184 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1185 r
= address_label_configure(label
, link
, NULL
, false);
1187 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1188 link_enter_failed(link
);
1192 link
->address_label_messages
++;
1195 /* now that we can figure out a default address for the dhcp server,
1197 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1199 Link
*uplink
= NULL
;
1200 bool acquired_uplink
= false;
1202 address
= link_find_dhcp_server_address(link
);
1204 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1205 link_enter_failed(link
);
1209 /* use the server address' subnet as the pool */
1210 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1211 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1216 r = sd_dhcp_server_set_router(link->dhcp_server,
1217 &main_address->in_addr.in);
1222 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1223 r
= sd_dhcp_server_set_max_lease_time(
1225 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1230 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1231 r
= sd_dhcp_server_set_default_lease_time(
1233 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1238 if (link
->network
->dhcp_server_emit_dns
) {
1240 if (link
->network
->n_dhcp_server_dns
> 0)
1241 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1243 uplink
= manager_find_uplink(link
->manager
, link
);
1244 acquired_uplink
= true;
1247 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1250 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1253 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1256 if (link
->network
->dhcp_server_emit_ntp
) {
1258 if (link
->network
->n_dhcp_server_ntp
> 0)
1259 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1261 if (!acquired_uplink
)
1262 uplink
= manager_find_uplink(link
->manager
, link
);
1265 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1268 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1272 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1275 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1277 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1279 if (link
->network
->dhcp_server_emit_timezone
) {
1280 _cleanup_free_
char *buffer
= NULL
;
1281 const char *tz
= NULL
;
1283 if (link
->network
->dhcp_server_timezone
)
1284 tz
= link
->network
->dhcp_server_timezone
;
1286 r
= get_timezone(&buffer
);
1288 log_warning_errno(r
, "Failed to determine timezone: %m");
1294 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1299 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1300 r
= sd_dhcp_server_start(link
->dhcp_server
);
1302 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1304 link_enter_failed(link
);
1310 log_link_debug(link
, "Offering DHCPv4 leases");
1313 if (link
->address_messages
== 0) {
1314 link
->addresses_configured
= true;
1315 link_check_ready(link
);
1317 log_link_debug(link
, "Setting addresses");
1322 static int link_set_bridge_vlan(Link
*link
) {
1325 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1327 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1332 static int link_set_proxy_arp(Link
*link
) {
1333 const char *p
= NULL
;
1336 if (!link_proxy_arp_enabled(link
))
1339 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1341 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1343 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1348 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1353 log_link_debug(link
, "Set link");
1355 r
= sd_netlink_message_get_errno(m
);
1356 if (r
< 0 && r
!= -EEXIST
) {
1357 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1358 link_enter_failed(link
);
1364 static int link_configure_after_setting_mtu(Link
*link
);
1366 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1371 assert(link
->ifname
);
1373 link
->setting_mtu
= false;
1375 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1378 r
= sd_netlink_message_get_errno(m
);
1380 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1384 log_link_debug(link
, "Setting MTU done.");
1386 if (link
->state
== LINK_STATE_PENDING
)
1387 (void) link_configure_after_setting_mtu(link
);
1392 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1393 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1397 assert(link
->manager
);
1398 assert(link
->manager
->rtnl
);
1400 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1403 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1405 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1407 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1409 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1410 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1411 (void) link_enable_ipv6(link
);
1413 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1414 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1415 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1417 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1418 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1423 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1425 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1427 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1428 link_netlink_destroy_callback
, link
);
1430 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1433 link
->setting_mtu
= true;
1438 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1443 assert(link
->ifname
);
1445 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1448 r
= sd_netlink_message_get_errno(m
);
1450 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1455 static int link_set_flags(Link
*link
) {
1456 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1457 unsigned ifi_change
= 0;
1458 unsigned ifi_flags
= 0;
1462 assert(link
->manager
);
1463 assert(link
->manager
->rtnl
);
1465 if (link
->flags
& IFF_LOOPBACK
)
1471 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1474 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1476 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1478 if (link
->network
->arp
>= 0) {
1479 ifi_change
|= IFF_NOARP
;
1480 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1483 if (link
->network
->multicast
>= 0) {
1484 ifi_change
|= IFF_MULTICAST
;
1485 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1488 if (link
->network
->allmulticast
>= 0) {
1489 ifi_change
|= IFF_ALLMULTI
;
1490 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1493 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1495 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1497 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1498 link_netlink_destroy_callback
, link
);
1500 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1507 static int link_set_bridge(Link
*link
) {
1508 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1512 assert(link
->network
);
1514 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1516 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1518 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1520 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1522 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1524 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1526 if (link
->network
->use_bpdu
>= 0) {
1527 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1529 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1532 if (link
->network
->hairpin
>= 0) {
1533 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1535 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1538 if (link
->network
->fast_leave
>= 0) {
1539 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1541 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1544 if (link
->network
->allow_port_to_be_root
>= 0) {
1545 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1547 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1551 if (link
->network
->unicast_flood
>= 0) {
1552 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1554 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1557 if (link
->network
->multicast_to_unicast
>= 0) {
1558 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1560 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1563 if (link
->network
->cost
!= 0) {
1564 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1566 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1569 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1570 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1572 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1575 r
= sd_netlink_message_close_container(req
);
1577 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1579 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1580 link_netlink_destroy_callback
, link
);
1582 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1589 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1594 assert(link
->ifname
);
1596 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1599 r
= sd_netlink_message_get_errno(m
);
1601 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1608 static int link_set_bond(Link
*link
) {
1609 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1613 assert(link
->network
);
1615 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1617 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1619 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1621 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1623 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1625 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1627 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1629 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1631 if (link
->network
->active_slave
) {
1632 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1634 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1637 if (link
->network
->primary_slave
) {
1638 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1640 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1643 r
= sd_netlink_message_close_container(req
);
1645 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1647 r
= sd_netlink_message_close_container(req
);
1649 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1651 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1652 link_netlink_destroy_callback
, link
);
1654 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1661 static int link_append_bond_slave(Link
*link
) {
1666 assert(link
->network
);
1667 assert(link
->network
->bond
);
1669 r
= link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
);
1673 r
= hashmap_ensure_allocated(&master
->bond_slaves
, NULL
);
1677 r
= hashmap_put(master
->bond_slaves
, INT_TO_PTR(link
->ifindex
), link
);
1684 static int link_lldp_save(Link
*link
) {
1685 _cleanup_free_
char *temp_path
= NULL
;
1686 _cleanup_fclose_
FILE *f
= NULL
;
1687 sd_lldp_neighbor
**l
= NULL
;
1691 assert(link
->lldp_file
);
1694 (void) unlink(link
->lldp_file
);
1698 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1702 (void) unlink(link
->lldp_file
);
1708 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1712 fchmod(fileno(f
), 0644);
1714 for (i
= 0; i
< n
; i
++) {
1719 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1724 (void) fwrite(&u
, 1, sizeof(u
), f
);
1725 (void) fwrite(p
, 1, sz
, f
);
1728 r
= fflush_and_check(f
);
1732 if (rename(temp_path
, link
->lldp_file
) < 0) {
1739 (void) unlink(link
->lldp_file
);
1741 (void) unlink(temp_path
);
1743 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1747 for (i
= 0; i
< n
; i
++)
1748 sd_lldp_neighbor_unref(l
[i
]);
1755 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1756 Link
*link
= userdata
;
1761 (void) link_lldp_save(link
);
1763 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1764 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1766 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1768 r
= link_lldp_emit_start(link
);
1770 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1774 static int link_acquire_ipv6_conf(Link
*link
) {
1779 if (link_ipv6_accept_ra_enabled(link
)) {
1780 assert(link
->ndisc
);
1782 log_link_debug(link
, "Discovering IPv6 routers");
1784 r
= sd_ndisc_start(link
->ndisc
);
1785 if (r
< 0 && r
!= -EBUSY
)
1786 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1789 if (link_radv_enabled(link
)) {
1791 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1793 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1795 r
= sd_radv_start(link
->radv
);
1796 if (r
< 0 && r
!= -EBUSY
)
1797 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1800 (void) dhcp6_request_prefix_delegation(link
);
1805 static int link_acquire_ipv4_conf(Link
*link
) {
1809 assert(link
->network
);
1810 assert(link
->manager
);
1811 assert(link
->manager
->event
);
1813 if (link_ipv4ll_enabled(link
)) {
1814 assert(link
->ipv4ll
);
1816 log_link_debug(link
, "Acquiring IPv4 link-local address");
1818 r
= sd_ipv4ll_start(link
->ipv4ll
);
1820 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1823 if (link_dhcp4_enabled(link
)) {
1824 assert(link
->dhcp_client
);
1826 log_link_debug(link
, "Acquiring DHCPv4 lease");
1828 r
= sd_dhcp_client_start(link
->dhcp_client
);
1830 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1836 static int link_acquire_conf(Link
*link
) {
1841 r
= link_acquire_ipv4_conf(link
);
1845 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1846 r
= link_acquire_ipv6_conf(link
);
1851 if (link_lldp_emit_enabled(link
)) {
1852 r
= link_lldp_emit_start(link
);
1854 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1860 bool link_has_carrier(Link
*link
) {
1861 /* see Documentation/networking/operstates.txt in the kernel sources */
1863 if (link
->kernel_operstate
== IF_OPER_UP
)
1866 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1867 /* operstate may not be implemented, so fall back to flags */
1868 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1874 static int link_address_genmode_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_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1889 static int link_configure_addrgen_mode(Link
*link
) {
1890 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1891 uint8_t ipv6ll_mode
;
1895 assert(link
->network
);
1896 assert(link
->manager
);
1897 assert(link
->manager
->rtnl
);
1899 log_link_debug(link
, "Setting address genmode for link");
1901 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1903 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1905 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1907 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1909 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1911 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1913 if (!link_ipv6ll_enabled(link
))
1914 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1916 const char *p
= NULL
;
1917 _cleanup_free_
char *stable_secret
= NULL
;
1919 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1921 /* The file may not exist. And event if it exists, when stable_secret is unset,
1922 * then reading the file fails and EIO is returned. */
1923 r
= read_one_line_file(p
, &stable_secret
);
1925 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1927 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1930 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1932 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1934 r
= sd_netlink_message_close_container(req
);
1936 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1938 r
= sd_netlink_message_close_container(req
);
1940 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1942 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1943 link_netlink_destroy_callback
, link
);
1945 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1952 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1957 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1960 r
= sd_netlink_message_get_errno(m
);
1962 /* we warn but don't fail the link, as it may be brought up later */
1963 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1968 static int link_up(Link
*link
) {
1969 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1973 assert(link
->network
);
1974 assert(link
->manager
);
1975 assert(link
->manager
->rtnl
);
1977 log_link_debug(link
, "Bringing link up");
1979 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1981 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1983 /* set it free if not enslaved with networkd */
1984 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1985 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1987 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1990 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1992 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1994 if (link
->network
->mac
) {
1995 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1997 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2000 if (link_ipv6_enabled(link
)) {
2001 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2003 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2005 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2006 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2008 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2010 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2011 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2013 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2016 r
= sd_netlink_message_close_container(req
);
2018 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2020 r
= sd_netlink_message_close_container(req
);
2022 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2025 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2026 link_netlink_destroy_callback
, link
);
2028 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2035 static int link_up_can(Link
*link
) {
2036 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2041 log_link_debug(link
, "Bringing CAN link up");
2043 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2045 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2047 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2049 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2051 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2052 link_netlink_destroy_callback
, link
);
2054 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2061 static int link_set_can(Link
*link
) {
2062 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2066 assert(link
->network
);
2067 assert(link
->manager
);
2068 assert(link
->manager
->rtnl
);
2070 log_link_debug(link
, "link_set_can");
2072 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2074 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2076 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2078 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2080 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2082 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2084 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2086 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2088 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2089 struct can_bittiming bt
= {
2090 .bitrate
= link
->network
->can_bitrate
,
2091 .sample_point
= link
->network
->can_sample_point
,
2094 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2095 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2099 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2100 if (link
->network
->can_sample_point
> 0)
2101 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2103 log_link_debug(link
, "Using default sample point");
2105 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2107 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2110 if (link
->network
->can_restart_us
> 0) {
2111 char time_string
[FORMAT_TIMESPAN_MAX
];
2112 uint64_t restart_ms
;
2114 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2117 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2119 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2121 if (restart_ms
> UINT32_MAX
) {
2122 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2126 log_link_debug(link
, "Setting restart = %s", time_string
);
2128 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2130 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2133 r
= sd_netlink_message_close_container(m
);
2135 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2137 r
= sd_netlink_message_close_container(m
);
2139 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2141 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2142 link_netlink_destroy_callback
, link
);
2144 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2148 if (!(link
->flags
& IFF_UP
)) {
2149 r
= link_up_can(link
);
2151 link_enter_failed(link
);
2156 log_link_debug(link
, "link_set_can done");
2161 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2166 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2169 r
= sd_netlink_message_get_errno(m
);
2171 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2173 if (streq_ptr(link
->kind
, "can"))
2179 int link_down(Link
*link
) {
2180 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2184 assert(link
->manager
);
2185 assert(link
->manager
->rtnl
);
2187 log_link_debug(link
, "Bringing link down");
2189 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2190 RTM_SETLINK
, link
->ifindex
);
2192 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2194 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2196 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2198 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2199 link_netlink_destroy_callback
, link
);
2201 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2208 static int link_handle_bound_to_list(Link
*link
) {
2212 bool required_up
= false;
2213 bool link_is_up
= false;
2217 if (hashmap_isempty(link
->bound_to_links
))
2220 if (link
->flags
& IFF_UP
)
2223 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2224 if (link_has_carrier(l
)) {
2229 if (!required_up
&& link_is_up
) {
2230 r
= link_down(link
);
2233 } else if (required_up
&& !link_is_up
) {
2242 static int link_handle_bound_by_list(Link
*link
) {
2249 if (hashmap_isempty(link
->bound_by_links
))
2252 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2253 r
= link_handle_bound_to_list(l
);
2261 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2267 if (link
== carrier
)
2270 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2273 r
= hashmap_ensure_allocated(h
, NULL
);
2277 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2284 static int link_new_bound_by_list(Link
*link
) {
2289 bool list_updated
= false;
2292 assert(link
->manager
);
2296 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2297 if (!carrier
->network
)
2300 if (strv_isempty(carrier
->network
->bind_carrier
))
2303 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2304 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2308 list_updated
= true;
2315 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2316 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2320 link_dirty(carrier
);
2326 static int link_new_bound_to_list(Link
*link
) {
2331 bool list_updated
= false;
2334 assert(link
->manager
);
2339 if (strv_isempty(link
->network
->bind_carrier
))
2344 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2345 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2346 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2350 list_updated
= true;
2357 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2358 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2362 link_dirty(carrier
);
2368 static int link_new_carrier_maps(Link
*link
) {
2371 r
= link_new_bound_by_list(link
);
2375 r
= link_handle_bound_by_list(link
);
2379 r
= link_new_bound_to_list(link
);
2383 r
= link_handle_bound_to_list(link
);
2390 static void link_free_bound_to_list(Link
*link
) {
2394 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2395 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2397 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2398 link_dirty(bound_to
);
2404 static void link_free_bound_by_list(Link
*link
) {
2408 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2409 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2411 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2412 link_dirty(bound_by
);
2413 link_handle_bound_to_list(bound_by
);
2420 static void link_free_carrier_maps(Link
*link
) {
2421 bool list_updated
= false;
2425 if (!hashmap_isempty(link
->bound_to_links
)) {
2426 link_free_bound_to_list(link
);
2427 list_updated
= true;
2430 if (!hashmap_isempty(link
->bound_by_links
)) {
2431 link_free_bound_by_list(link
);
2432 list_updated
= true;
2441 void link_drop(Link
*link
) {
2442 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2445 link_set_state(link
, LINK_STATE_LINGER
);
2447 link_free_carrier_maps(link
);
2449 log_link_debug(link
, "Link removed");
2451 (void) unlink(link
->state_file
);
2453 link_detach_from_manager(link
);
2460 static int link_joined(Link
*link
) {
2464 assert(link
->network
);
2466 if (!hashmap_isempty(link
->bound_to_links
)) {
2467 r
= link_handle_bound_to_list(link
);
2470 } else if (!(link
->flags
& IFF_UP
)) {
2473 link_enter_failed(link
);
2478 if (link
->network
->bridge
) {
2479 r
= link_set_bridge(link
);
2481 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2484 if (link
->network
->bond
) {
2485 r
= link_set_bond(link
);
2487 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2489 r
= link_append_bond_slave(link
);
2491 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2494 if (link
->network
->use_br_vlan
&&
2495 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2496 r
= link_set_bridge_vlan(link
);
2498 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2501 /* Skip setting up addresses until it gets carrier,
2502 or it would try to set addresses twice,
2503 which is bad for non-idempotent steps. */
2504 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2507 return link_request_set_addresses(link
);
2510 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2514 assert(link
->network
);
2518 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2521 r
= sd_netlink_message_get_errno(m
);
2522 if (r
< 0 && r
!= -EEXIST
) {
2523 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2524 link_enter_failed(link
);
2527 log_link_debug(link
, "Joined netdev");
2529 if (link
->enslaving
<= 0)
2535 static int link_enter_join_netdev(Link
*link
) {
2541 assert(link
->network
);
2542 assert(link
->state
== LINK_STATE_PENDING
);
2544 link_set_state(link
, LINK_STATE_CONFIGURING
);
2548 if (!link
->network
->bridge
&&
2549 !link
->network
->bond
&&
2550 !link
->network
->vrf
&&
2551 hashmap_isempty(link
->network
->stacked_netdevs
))
2552 return link_joined(link
);
2554 if (link
->network
->bond
) {
2555 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2556 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2557 return link_joined(link
);
2559 log_struct(LOG_DEBUG
,
2560 LOG_LINK_INTERFACE(link
),
2561 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2562 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2564 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2566 log_struct_errno(LOG_WARNING
, r
,
2567 LOG_LINK_INTERFACE(link
),
2568 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2569 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2570 link_enter_failed(link
);
2577 if (link
->network
->bridge
) {
2578 log_struct(LOG_DEBUG
,
2579 LOG_LINK_INTERFACE(link
),
2580 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2581 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2583 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2585 log_struct_errno(LOG_WARNING
, r
,
2586 LOG_LINK_INTERFACE(link
),
2587 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2588 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2589 link_enter_failed(link
);
2596 if (link
->network
->vrf
) {
2597 log_struct(LOG_DEBUG
,
2598 LOG_LINK_INTERFACE(link
),
2599 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2600 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2602 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2604 log_struct_errno(LOG_WARNING
, r
,
2605 LOG_LINK_INTERFACE(link
),
2606 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2607 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2608 link_enter_failed(link
);
2615 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2617 if (netdev
->ifindex
> 0) {
2622 log_struct(LOG_DEBUG
,
2623 LOG_LINK_INTERFACE(link
),
2624 LOG_NETDEV_INTERFACE(netdev
),
2625 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2627 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2629 log_struct_errno(LOG_WARNING
, r
,
2630 LOG_LINK_INTERFACE(link
),
2631 LOG_NETDEV_INTERFACE(netdev
),
2632 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2633 link_enter_failed(link
);
2643 static int link_set_ipv4_forward(Link
*link
) {
2646 if (!link_ipv4_forward_enabled(link
))
2649 /* We propagate the forwarding flag from one interface to the
2650 * global setting one way. This means: as long as at least one
2651 * interface was configured at any time that had IP forwarding
2652 * enabled the setting will stay on for good. We do this
2653 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2654 * somewhat in sync (see below). */
2656 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2658 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2663 static int link_set_ipv6_forward(Link
*link
) {
2666 if (!link_ipv6_forward_enabled(link
))
2669 /* On Linux, the IPv6 stack does not know a per-interface
2670 * packet forwarding setting: either packet forwarding is on
2671 * for all, or off for all. We hence don't bother with a
2672 * per-interface setting, but simply propagate the interface
2673 * flag, if it is set, to the global flag, one-way. Note that
2674 * while IPv4 would allow a per-interface flag, we expose the
2675 * same behaviour there and also propagate the setting from
2676 * one to all, to keep things simple (see above). */
2678 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2680 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2685 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2686 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2687 IPv6PrivacyExtensions s
;
2688 const char *p
= NULL
;
2691 s
= link_ipv6_privacy_extensions(link
);
2695 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2696 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2698 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2700 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2705 static int link_set_ipv6_accept_ra(Link
*link
) {
2706 const char *p
= NULL
;
2709 /* Make this a NOP if IPv6 is not available */
2710 if (!socket_ipv6_is_supported())
2713 if (link
->flags
& IFF_LOOPBACK
)
2719 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2721 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2722 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2724 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2729 static int link_set_ipv6_dad_transmits(Link
*link
) {
2730 char buf
[DECIMAL_STR_MAX(int) + 1];
2731 const char *p
= NULL
;
2734 /* Make this a NOP if IPv6 is not available */
2735 if (!socket_ipv6_is_supported())
2738 if (link
->flags
& IFF_LOOPBACK
)
2744 if (link
->network
->ipv6_dad_transmits
< 0)
2747 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2748 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2750 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2752 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2757 static int link_set_ipv6_hop_limit(Link
*link
) {
2758 char buf
[DECIMAL_STR_MAX(int) + 1];
2759 const char *p
= NULL
;
2762 /* Make this a NOP if IPv6 is not available */
2763 if (!socket_ipv6_is_supported())
2766 if (link
->flags
& IFF_LOOPBACK
)
2772 if (link
->network
->ipv6_hop_limit
< 0)
2775 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2776 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2778 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2780 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2785 static int link_set_ipv6_mtu(Link
*link
) {
2786 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2787 const char *p
= NULL
;
2790 /* Make this a NOP if IPv6 is not available */
2791 if (!socket_ipv6_is_supported())
2794 if (link
->flags
& IFF_LOOPBACK
)
2797 if (link
->network
->ipv6_mtu
== 0)
2800 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2802 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2804 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2806 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2811 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2812 Address
*net_address
;
2820 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2821 if (address_equal(net_address
, address
))
2827 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2836 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2837 if (route_equal(net_route
, route
))
2843 static int link_drop_foreign_config(Link
*link
) {
2849 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2850 /* we consider IPv6LL addresses to be managed by the kernel */
2851 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2854 if (link_is_static_address_configured(link
, address
)) {
2855 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2857 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2859 r
= address_remove(address
, link
, NULL
);
2865 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2866 /* do not touch routes managed by the kernel */
2867 if (route
->protocol
== RTPROT_KERNEL
)
2870 if (link_is_static_route_configured(link
, route
)) {
2871 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2875 r
= route_remove(route
, link
, NULL
);
2884 static int link_drop_config(Link
*link
) {
2885 Address
*address
, *pool_address
;
2890 SET_FOREACH(address
, link
->addresses
, i
) {
2891 /* we consider IPv6LL addresses to be managed by the kernel */
2892 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2895 r
= address_remove(address
, link
, NULL
);
2899 /* If this address came from an address pool, clean up the pool */
2900 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2901 if (address_equal(address
, pool_address
)) {
2902 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2903 address_free(pool_address
);
2909 SET_FOREACH(route
, link
->routes
, i
) {
2910 /* do not touch routes managed by the kernel */
2911 if (route
->protocol
== RTPROT_KERNEL
)
2914 r
= route_remove(route
, link
, NULL
);
2924 static int link_update_lldp(Link
*link
) {
2932 if (link
->flags
& IFF_UP
) {
2933 r
= sd_lldp_start(link
->lldp
);
2935 log_link_debug(link
, "Started LLDP.");
2937 r
= sd_lldp_stop(link
->lldp
);
2939 log_link_debug(link
, "Stopped LLDP.");
2945 static int link_configure_can(Link
*link
) {
2948 if (streq_ptr(link
->kind
, "can")) {
2949 /* The CAN interface must be down to configure bitrate, etc... */
2950 if ((link
->flags
& IFF_UP
)) {
2951 r
= link_down(link
);
2953 link_enter_failed(link
);
2960 return link_set_can(link
);
2963 if (!(link
->flags
& IFF_UP
)) {
2964 r
= link_up_can(link
);
2966 link_enter_failed(link
);
2974 static int link_configure(Link
*link
) {
2978 assert(link
->network
);
2979 assert(link
->state
== LINK_STATE_PENDING
);
2981 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2982 return link_configure_can(link
);
2984 /* Drop foreign config, but ignore loopback or critical devices.
2985 * We do not want to remove loopback address or addresses used for root NFS. */
2986 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2987 r
= link_drop_foreign_config(link
);
2992 r
= link_set_proxy_arp(link
);
2996 r
= ipv6_proxy_ndp_addresses_configure(link
);
3000 r
= link_set_ipv4_forward(link
);
3004 r
= link_set_ipv6_forward(link
);
3008 r
= link_set_ipv6_privacy_extensions(link
);
3012 r
= link_set_ipv6_accept_ra(link
);
3016 r
= link_set_ipv6_dad_transmits(link
);
3020 r
= link_set_ipv6_hop_limit(link
);
3024 r
= link_set_flags(link
);
3028 r
= link_set_ipv6_mtu(link
);
3032 if (link_ipv4ll_enabled(link
)) {
3033 r
= ipv4ll_configure(link
);
3038 if (link_dhcp4_enabled(link
)) {
3039 r
= dhcp4_set_promote_secondaries(link
);
3043 r
= dhcp4_configure(link
);
3048 if (link_dhcp4_server_enabled(link
)) {
3049 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3053 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3058 if (link_dhcp6_enabled(link
) ||
3059 link_ipv6_accept_ra_enabled(link
)) {
3060 r
= dhcp6_configure(link
);
3065 if (link_ipv6_accept_ra_enabled(link
)) {
3066 r
= ndisc_configure(link
);
3071 if (link_radv_enabled(link
)) {
3072 r
= radv_configure(link
);
3077 if (link_lldp_rx_enabled(link
)) {
3078 r
= sd_lldp_new(&link
->lldp
);
3082 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3086 r
= sd_lldp_match_capabilities(link
->lldp
,
3087 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3088 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3089 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3093 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3097 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3101 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3105 r
= link_update_lldp(link
);
3110 if (link
->network
->mtu
> 0) {
3111 r
= link_set_mtu(link
, link
->network
->mtu
);
3116 if (socket_ipv6_is_supported()) {
3117 r
= link_configure_addrgen_mode(link
);
3122 return link_configure_after_setting_mtu(link
);
3125 static int link_configure_after_setting_mtu(Link
*link
) {
3129 assert(link
->network
);
3130 assert(link
->state
== LINK_STATE_PENDING
);
3132 if (link
->setting_mtu
)
3135 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3136 r
= link_acquire_conf(link
);
3141 return link_enter_join_netdev(link
);
3144 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3147 if (duid
->raw_data_len
> 0)
3150 if (duid
->type
!= DUID_TYPE_UUID
)
3153 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3154 duid
->raw_data_len
= sizeof(sd_id128_t
);
3159 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3160 Manager
*manager
= userdata
;
3161 const sd_bus_error
*e
;
3171 e
= sd_bus_message_get_error(m
);
3173 log_error_errno(sd_bus_error_get_errno(e
),
3174 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3179 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3183 if (sz
!= sizeof(sd_id128_t
)) {
3184 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3188 memcpy(&manager
->product_uuid
, a
, sz
);
3189 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3190 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3192 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3195 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3196 r
= link_configure(link
);
3198 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3201 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3203 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3204 * even if the method fails. */
3205 manager
->has_product_uuid
= true;
3210 static bool link_requires_uuid(Link
*link
) {
3214 assert(link
->manager
);
3215 assert(link
->network
);
3217 duid
= link_get_duid(link
);
3218 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3221 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3224 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3230 static int link_configure_duid(Link
*link
) {
3236 assert(link
->manager
);
3237 assert(link
->network
);
3240 duid
= link_get_duid(link
);
3242 if (!link_requires_uuid(link
))
3245 if (m
->has_product_uuid
) {
3246 (void) duid_set_uuid(duid
, m
->product_uuid
);
3250 if (!m
->links_requesting_uuid
) {
3251 r
= manager_request_product_uuid(m
, link
);
3256 log_link_warning_errno(link
, r
,
3257 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3261 r
= set_put(m
->links_requesting_uuid
, link
);
3265 r
= set_put(m
->duids_requesting_uuid
, duid
);
3273 static int link_initialized_and_synced(Link
*link
) {
3278 assert(link
->ifname
);
3279 assert(link
->manager
);
3281 if (link
->state
!= LINK_STATE_PENDING
)
3284 log_link_debug(link
, "Link state is up-to-date");
3286 r
= link_new_bound_by_list(link
);
3290 r
= link_handle_bound_by_list(link
);
3294 if (!link
->network
) {
3295 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3296 &link
->mac
, &network
);
3298 link_enter_unmanaged(link
);
3300 } else if (r
== 0 && network
->unmanaged
) {
3301 link_enter_unmanaged(link
);
3306 if (link
->flags
& IFF_LOOPBACK
) {
3307 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3308 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3310 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3311 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3313 if (network
->dhcp_server
)
3314 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3317 r
= network_apply(network
, link
);
3322 r
= link_new_bound_to_list(link
);
3326 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3327 * link_configure() is called later asynchronously. */
3328 r
= link_configure_duid(link
);
3332 r
= link_configure(link
);
3339 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3340 (void) link_initialized_and_synced(link
);
3344 int link_initialized(Link
*link
, sd_device
*device
) {
3345 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3349 assert(link
->manager
);
3350 assert(link
->manager
->rtnl
);
3353 if (link
->state
!= LINK_STATE_PENDING
)
3356 if (link
->sd_device
)
3359 log_link_debug(link
, "udev initialized link");
3361 link
->sd_device
= sd_device_ref(device
);
3363 /* udev has initialized the link, but we don't know if we have yet
3364 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3365 * when it returns we know that the pending NEWLINKs have already been
3366 * processed and that we are up-to-date */
3368 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3373 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3374 link_netlink_destroy_callback
, link
);
3383 static int link_load(Link
*link
) {
3384 _cleanup_free_
char *network_file
= NULL
,
3387 *dhcp4_address
= NULL
,
3388 *ipv4ll_address
= NULL
;
3389 union in_addr_union address
;
3390 union in_addr_union route_dst
;
3396 r
= parse_env_file(NULL
, link
->state_file
,
3397 "NETWORK_FILE", &network_file
,
3398 "ADDRESSES", &addresses
,
3400 "DHCP4_ADDRESS", &dhcp4_address
,
3401 "IPV4LL_ADDRESS", &ipv4ll_address
);
3402 if (r
< 0 && r
!= -ENOENT
)
3403 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3410 suffix
= strrchr(network_file
, '.');
3412 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3413 goto network_file_fail
;
3417 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3419 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3420 goto network_file_fail
;
3423 r
= network_apply(network
, link
);
3425 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3434 _cleanup_free_
char *address_str
= NULL
;
3435 char *prefixlen_str
;
3437 unsigned char prefixlen
;
3439 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3441 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3447 prefixlen_str
= strchr(address_str
, '/');
3448 if (!prefixlen_str
) {
3449 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3453 *prefixlen_str
++ = '\0';
3455 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3457 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3461 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3463 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3467 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3469 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3478 _cleanup_free_
char *route_str
= NULL
;
3479 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3481 char *prefixlen_str
;
3483 unsigned char prefixlen
, tos
, table
;
3486 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3488 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3494 prefixlen_str
= strchr(route_str
, '/');
3495 if (!prefixlen_str
) {
3496 log_link_debug(link
, "Failed to parse route %s", route_str
);
3500 *prefixlen_str
++ = '\0';
3502 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3504 log_link_debug(link
,
3505 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3510 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3512 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3516 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3518 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3520 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3521 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3522 0, route_expire_handler
, route
);
3524 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3527 route
->lifetime
= lifetime
;
3528 sd_event_source_unref(route
->expire
);
3529 route
->expire
= TAKE_PTR(expire
);
3533 if (dhcp4_address
) {
3534 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3536 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3537 goto dhcp4_address_fail
;
3540 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3542 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3544 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3546 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3551 if (ipv4ll_address
) {
3552 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3554 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3555 goto ipv4ll_address_fail
;
3558 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3560 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3562 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3564 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3567 ipv4ll_address_fail
:
3572 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3573 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3574 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3583 r
= link_new(m
, message
, ret
);
3589 log_link_debug(link
, "Link %d added", link
->ifindex
);
3591 r
= link_load(link
);
3595 if (detect_container() <= 0) {
3596 /* not in a container, udev will be around */
3597 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3598 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3600 log_link_warning_errno(link
, r
, "Could not find device: %m");
3604 r
= sd_device_get_is_initialized(device
);
3606 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3611 log_link_debug(link
, "link pending udev initialization...");
3615 r
= link_initialized(link
, device
);
3619 r
= link_initialized_and_synced(link
);
3626 link_enter_failed(link
);
3630 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3635 log_link_info(link
, "Gained IPv6LL");
3637 link
->ipv6ll_address
= *address
;
3638 link_check_ready(link
);
3640 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3641 r
= link_acquire_ipv6_conf(link
);
3643 link_enter_failed(link
);
3651 static int link_carrier_gained(Link
*link
) {
3656 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3657 r
= link_acquire_conf(link
);
3659 link_enter_failed(link
);
3663 r
= link_request_set_addresses(link
);
3668 r
= link_handle_bound_by_list(link
);
3675 static int link_carrier_lost(Link
*link
) {
3680 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3683 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3684 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3685 * configuration and stop the clients as well. */
3686 if (link
->setting_mtu
)
3689 r
= link_stop_clients(link
);
3691 link_enter_failed(link
);
3695 if (link_dhcp4_server_enabled(link
))
3696 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3698 r
= link_drop_config(link
);
3702 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3703 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3704 r
= link_drop_foreign_config(link
);
3709 r
= link_handle_bound_by_list(link
);
3716 int link_carrier_reset(Link
*link
) {
3721 if (link_has_carrier(link
)) {
3722 r
= link_carrier_lost(link
);
3726 r
= link_carrier_gained(link
);
3730 log_link_info(link
, "Reset carrier");
3736 int link_update(Link
*link
, sd_netlink_message
*m
) {
3737 struct ether_addr mac
;
3740 bool had_carrier
, carrier_gained
, carrier_lost
;
3744 assert(link
->ifname
);
3747 if (link
->state
== LINK_STATE_LINGER
) {
3748 log_link_info(link
, "Link readded");
3749 link_set_state(link
, LINK_STATE_CONFIGURING
);
3751 r
= link_new_carrier_maps(link
);
3756 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3757 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3758 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3760 if (link
->state
== LINK_STATE_PENDING
) {
3761 r
= free_and_strdup(&link
->ifname
, ifname
);
3765 Manager
*manager
= link
->manager
;
3768 r
= link_add(manager
, m
, &link
);
3774 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3775 if (r
>= 0 && mtu
> 0) {
3777 if (link
->original_mtu
== 0) {
3778 link
->original_mtu
= mtu
;
3779 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3782 if (link
->dhcp_client
) {
3783 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3786 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3790 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3792 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3796 /* The kernel may broadcast NEWLINK messages without the MAC address
3797 set, simply ignore them. */
3798 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3800 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3803 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3806 log_link_debug(link
, "MAC address: "
3807 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3808 mac
.ether_addr_octet
[0],
3809 mac
.ether_addr_octet
[1],
3810 mac
.ether_addr_octet
[2],
3811 mac
.ether_addr_octet
[3],
3812 mac
.ether_addr_octet
[4],
3813 mac
.ether_addr_octet
[5]);
3816 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3818 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3821 if (link
->dhcp_client
) {
3822 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3823 (const uint8_t *) &link
->mac
,
3827 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3829 r
= dhcp4_set_client_identifier(link
);
3834 if (link
->dhcp6_client
) {
3835 const DUID
* duid
= link_get_duid(link
);
3837 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3838 (const uint8_t *) &link
->mac
,
3842 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3844 if (link
->network
->iaid_set
) {
3845 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3846 link
->network
->iaid
);
3848 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3851 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3853 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3854 duid
->raw_data_len
);
3856 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3860 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3862 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3866 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3868 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3873 had_carrier
= link_has_carrier(link
);
3875 r
= link_update_flags(link
, m
);
3879 r
= link_update_lldp(link
);
3883 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3884 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3886 if (carrier_gained
) {
3887 log_link_info(link
, "Gained carrier");
3889 r
= link_carrier_gained(link
);
3892 } else if (carrier_lost
) {
3893 log_link_info(link
, "Lost carrier");
3895 r
= link_carrier_lost(link
);
3903 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3911 if (hashmap_isempty(h
))
3915 HASHMAP_FOREACH(link
, h
, i
) {
3919 fprintf(f
, "%i", link
->ifindex
);
3926 int link_save(Link
*link
) {
3927 _cleanup_free_
char *temp_path
= NULL
;
3928 _cleanup_fclose_
FILE *f
= NULL
;
3929 const char *admin_state
, *oper_state
;
3936 assert(link
->state_file
);
3937 assert(link
->lease_file
);
3938 assert(link
->manager
);
3940 if (link
->state
== LINK_STATE_LINGER
) {
3941 unlink(link
->state_file
);
3945 link_lldp_save(link
);
3947 admin_state
= link_state_to_string(link
->state
);
3948 assert(admin_state
);
3950 oper_state
= link_operstate_to_string(link
->operstate
);
3953 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3957 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3958 (void) fchmod(fileno(f
), 0644);
3961 "# This is private data. Do not parse.\n"
3964 admin_state
, oper_state
);
3966 if (link
->network
) {
3968 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3969 const char *dhcp_domainname
= NULL
;
3970 char **dhcp6_domains
= NULL
;
3971 char **dhcp_domains
= NULL
;
3974 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3975 yes_no(link
->network
->required_for_online
));
3977 if (link
->dhcp6_client
) {
3978 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3979 if (r
< 0 && r
!= -ENOMSG
)
3980 log_link_debug(link
, "No DHCPv6 lease");
3983 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3988 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3989 _cleanup_free_
char *b
= NULL
;
3991 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3992 &link
->network
->dns
[j
].address
, &b
);
3994 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4004 if (link
->network
->dhcp_use_dns
&&
4006 const struct in_addr
*addresses
;
4008 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4010 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4014 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4015 struct in6_addr
*in6_addrs
;
4017 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4021 serialize_in6_addrs(f
, in6_addrs
, r
);
4026 /* Make sure to flush out old entries before we use the NDISC data */
4029 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4032 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4036 serialize_in6_addrs(f
, &dd
->address
, 1);
4045 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4047 if (link
->network
->dhcp_use_ntp
&&
4049 const struct in_addr
*addresses
;
4051 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4053 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4057 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4058 struct in6_addr
*in6_addrs
;
4061 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4066 serialize_in6_addrs(f
, in6_addrs
, r
);
4070 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4072 fputstrv(f
, hosts
, NULL
, &space
);
4077 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4078 if (link
->dhcp_lease
) {
4079 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4080 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4083 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4086 fputs("DOMAINS=", f
);
4088 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
4090 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4093 if (dhcp_domainname
)
4094 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4096 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4098 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4100 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4101 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4106 fputs("ROUTE_DOMAINS=", f
);
4108 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
4110 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4113 if (dhcp_domainname
)
4114 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4116 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4118 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4120 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4121 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4126 fprintf(f
, "LLMNR=%s\n",
4127 resolve_support_to_string(link
->network
->llmnr
));
4128 fprintf(f
, "MDNS=%s\n",
4129 resolve_support_to_string(link
->network
->mdns
));
4130 if (link
->network
->dns_default_route
>= 0)
4131 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4133 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4134 fprintf(f
, "DNS_OVER_TLS=%s\n",
4135 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4137 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4138 fprintf(f
, "DNSSEC=%s\n",
4139 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4141 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4144 fputs("DNSSEC_NTA=", f
);
4146 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4147 fputs_with_space(f
, n
, NULL
, &space
);
4151 fputs("ADDRESSES=", f
);
4153 SET_FOREACH(a
, link
->addresses
, i
) {
4154 _cleanup_free_
char *address_str
= NULL
;
4156 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4160 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4165 fputs("ROUTES=", f
);
4167 SET_FOREACH(route
, link
->routes
, i
) {
4168 _cleanup_free_
char *route_str
= NULL
;
4170 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4174 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4175 space
? " " : "", route_str
,
4176 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4183 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4184 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4186 if (link
->dhcp_lease
) {
4187 struct in_addr address
;
4188 const char *tz
= NULL
;
4190 assert(link
->network
);
4192 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4194 fprintf(f
, "TIMEZONE=%s\n", tz
);
4196 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4198 fputs("DHCP4_ADDRESS=", f
);
4199 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4203 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4211 unlink(link
->lease_file
);
4214 struct in_addr address
;
4216 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4218 fputs("IPV4LL_ADDRESS=", f
);
4219 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4224 r
= fflush_and_check(f
);
4228 if (rename(temp_path
, link
->state_file
) < 0) {
4236 (void) unlink(link
->state_file
);
4238 (void) unlink(temp_path
);
4240 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4243 /* The serialized state in /run is no longer up-to-date. */
4244 void link_dirty(Link
*link
) {
4249 /* mark manager dirty as link is dirty */
4250 manager_dirty(link
->manager
);
4252 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4254 /* allocation errors are ignored */
4257 r
= set_put(link
->manager
->dirty_links
, link
);
4259 /* don't take another ref if the link was already dirty */
4265 /* The serialized state in /run is up-to-date */
4266 void link_clean(Link
*link
) {
4268 assert(link
->manager
);
4270 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4273 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4274 [LINK_STATE_PENDING
] = "pending",
4275 [LINK_STATE_CONFIGURING
] = "configuring",
4276 [LINK_STATE_CONFIGURED
] = "configured",
4277 [LINK_STATE_UNMANAGED
] = "unmanaged",
4278 [LINK_STATE_FAILED
] = "failed",
4279 [LINK_STATE_LINGER
] = "linger",
4282 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4284 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4285 [LINK_OPERSTATE_OFF
] = "off",
4286 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4287 [LINK_OPERSTATE_DORMANT
] = "dormant",
4288 [LINK_OPERSTATE_CARRIER
] = "carrier",
4289 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4290 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
4291 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4294 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);