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 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1074 if (n_addresses
<= 0)
1077 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1080 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1081 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1082 size_t n_addresses
= 0, n_allocated
= 0;
1088 log_debug("Copying NTP server information from %s", link
->ifname
);
1090 STRV_FOREACH(a
, link
->network
->ntp
) {
1093 /* Only look for IPv4 addresses */
1094 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1097 /* Never propagate obviously borked data */
1098 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1101 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1104 addresses
[n_addresses
++] = ia
;
1107 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1108 const struct in_addr
*da
= NULL
;
1111 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1114 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1117 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1122 if (n_addresses
<= 0)
1125 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1128 static int link_set_bridge_fdb(Link
*link
) {
1129 FdbEntry
*fdb_entry
;
1132 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1133 r
= fdb_entry_configure(link
, fdb_entry
);
1135 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1141 static int link_request_set_addresses(Link
*link
) {
1142 AddressLabel
*label
;
1147 assert(link
->network
);
1148 assert(link
->state
!= _LINK_STATE_INVALID
);
1150 link_set_state(link
, LINK_STATE_CONFIGURING
);
1152 /* Reset all *_configured flags we are configuring. */
1153 link
->addresses_configured
= false;
1154 link
->addresses_ready
= false;
1155 link
->neighbors_configured
= false;
1156 link
->static_routes_configured
= false;
1157 link
->routing_policy_rules_configured
= false;
1159 r
= link_set_bridge_fdb(link
);
1163 r
= link_request_set_neighbors(link
);
1167 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1170 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1172 r
= address_configure(ad
, link
, address_handler
, update
);
1174 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1175 link_enter_failed(link
);
1179 link
->address_messages
++;
1182 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1183 r
= address_label_configure(label
, link
, NULL
, false);
1185 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1186 link_enter_failed(link
);
1190 link
->address_label_messages
++;
1193 /* now that we can figure out a default address for the dhcp server,
1195 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1197 Link
*uplink
= NULL
;
1198 bool acquired_uplink
= false;
1200 address
= link_find_dhcp_server_address(link
);
1202 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1203 link_enter_failed(link
);
1207 /* use the server address' subnet as the pool */
1208 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1209 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1214 r = sd_dhcp_server_set_router(link->dhcp_server,
1215 &main_address->in_addr.in);
1220 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1221 r
= sd_dhcp_server_set_max_lease_time(
1223 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1228 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1229 r
= sd_dhcp_server_set_default_lease_time(
1231 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1236 if (link
->network
->dhcp_server_emit_dns
) {
1238 if (link
->network
->n_dhcp_server_dns
> 0)
1239 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1241 uplink
= manager_find_uplink(link
->manager
, link
);
1242 acquired_uplink
= true;
1245 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1248 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1251 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1254 if (link
->network
->dhcp_server_emit_ntp
) {
1256 if (link
->network
->n_dhcp_server_ntp
> 0)
1257 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1259 if (!acquired_uplink
)
1260 uplink
= manager_find_uplink(link
->manager
, link
);
1263 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1266 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1270 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1273 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1275 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1277 if (link
->network
->dhcp_server_emit_timezone
) {
1278 _cleanup_free_
char *buffer
= NULL
;
1279 const char *tz
= NULL
;
1281 if (link
->network
->dhcp_server_timezone
)
1282 tz
= link
->network
->dhcp_server_timezone
;
1284 r
= get_timezone(&buffer
);
1286 log_warning_errno(r
, "Failed to determine timezone: %m");
1292 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1297 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1298 r
= sd_dhcp_server_start(link
->dhcp_server
);
1300 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1302 link_enter_failed(link
);
1308 log_link_debug(link
, "Offering DHCPv4 leases");
1311 if (link
->address_messages
== 0) {
1312 link
->addresses_configured
= true;
1313 link_check_ready(link
);
1315 log_link_debug(link
, "Setting addresses");
1320 static int link_set_bridge_vlan(Link
*link
) {
1323 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1325 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1330 static int link_set_proxy_arp(Link
*link
) {
1331 const char *p
= NULL
;
1334 if (!link_proxy_arp_enabled(link
))
1337 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1339 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1341 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1346 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1351 log_link_debug(link
, "Set link");
1353 r
= sd_netlink_message_get_errno(m
);
1354 if (r
< 0 && r
!= -EEXIST
) {
1355 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1356 link_enter_failed(link
);
1362 static int link_configure_after_setting_mtu(Link
*link
);
1364 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1369 assert(link
->ifname
);
1371 link
->setting_mtu
= false;
1373 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1376 r
= sd_netlink_message_get_errno(m
);
1378 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1382 log_link_debug(link
, "Setting MTU done.");
1384 if (link
->state
== LINK_STATE_PENDING
)
1385 (void) link_configure_after_setting_mtu(link
);
1390 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1391 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1395 assert(link
->manager
);
1396 assert(link
->manager
->rtnl
);
1398 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1401 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1403 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1405 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1407 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1408 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1409 (void) link_enable_ipv6(link
);
1411 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1412 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1413 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1415 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1416 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1421 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1423 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1425 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1426 link_netlink_destroy_callback
, link
);
1428 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1431 link
->setting_mtu
= true;
1436 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1441 assert(link
->ifname
);
1443 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1446 r
= sd_netlink_message_get_errno(m
);
1448 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1453 static int link_set_flags(Link
*link
) {
1454 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1455 unsigned ifi_change
= 0;
1456 unsigned ifi_flags
= 0;
1460 assert(link
->manager
);
1461 assert(link
->manager
->rtnl
);
1463 if (link
->flags
& IFF_LOOPBACK
)
1469 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1472 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1474 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1476 if (link
->network
->arp
>= 0) {
1477 ifi_change
|= IFF_NOARP
;
1478 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1481 if (link
->network
->multicast
>= 0) {
1482 ifi_change
|= IFF_MULTICAST
;
1483 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1486 if (link
->network
->allmulticast
>= 0) {
1487 ifi_change
|= IFF_ALLMULTI
;
1488 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1491 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1493 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1495 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1496 link_netlink_destroy_callback
, link
);
1498 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1505 static int link_set_bridge(Link
*link
) {
1506 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1510 assert(link
->network
);
1512 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1514 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1516 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1518 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1520 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1522 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1524 if (link
->network
->use_bpdu
>= 0) {
1525 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1527 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1530 if (link
->network
->hairpin
>= 0) {
1531 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1533 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1536 if (link
->network
->fast_leave
>= 0) {
1537 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1539 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1542 if (link
->network
->allow_port_to_be_root
>= 0) {
1543 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1545 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1549 if (link
->network
->unicast_flood
>= 0) {
1550 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1552 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1555 if (link
->network
->multicast_to_unicast
>= 0) {
1556 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1558 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1561 if (link
->network
->cost
!= 0) {
1562 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1564 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1567 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1568 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1570 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1573 r
= sd_netlink_message_close_container(req
);
1575 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1577 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1578 link_netlink_destroy_callback
, link
);
1580 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1587 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1592 assert(link
->ifname
);
1594 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1597 r
= sd_netlink_message_get_errno(m
);
1599 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1606 static int link_set_bond(Link
*link
) {
1607 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1611 assert(link
->network
);
1613 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1615 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1617 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1619 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1621 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1623 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1625 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1627 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1629 if (link
->network
->active_slave
) {
1630 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1632 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1635 if (link
->network
->primary_slave
) {
1636 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1638 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1641 r
= sd_netlink_message_close_container(req
);
1643 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1645 r
= sd_netlink_message_close_container(req
);
1647 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1649 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1650 link_netlink_destroy_callback
, link
);
1652 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1659 static int link_append_bond_slave(Link
*link
) {
1664 assert(link
->network
);
1665 assert(link
->network
->bond
);
1667 r
= link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
);
1671 r
= hashmap_ensure_allocated(&master
->bond_slaves
, NULL
);
1675 r
= hashmap_put(master
->bond_slaves
, INT_TO_PTR(link
->ifindex
), link
);
1682 static int link_lldp_save(Link
*link
) {
1683 _cleanup_free_
char *temp_path
= NULL
;
1684 _cleanup_fclose_
FILE *f
= NULL
;
1685 sd_lldp_neighbor
**l
= NULL
;
1689 assert(link
->lldp_file
);
1692 (void) unlink(link
->lldp_file
);
1696 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1700 (void) unlink(link
->lldp_file
);
1706 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1710 fchmod(fileno(f
), 0644);
1712 for (i
= 0; i
< n
; i
++) {
1717 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1722 (void) fwrite(&u
, 1, sizeof(u
), f
);
1723 (void) fwrite(p
, 1, sz
, f
);
1726 r
= fflush_and_check(f
);
1730 if (rename(temp_path
, link
->lldp_file
) < 0) {
1737 (void) unlink(link
->lldp_file
);
1739 (void) unlink(temp_path
);
1741 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1745 for (i
= 0; i
< n
; i
++)
1746 sd_lldp_neighbor_unref(l
[i
]);
1753 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1754 Link
*link
= userdata
;
1759 (void) link_lldp_save(link
);
1761 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1762 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1764 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1766 r
= link_lldp_emit_start(link
);
1768 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1772 static int link_acquire_ipv6_conf(Link
*link
) {
1777 if (link_ipv6_accept_ra_enabled(link
)) {
1778 assert(link
->ndisc
);
1780 log_link_debug(link
, "Discovering IPv6 routers");
1782 r
= sd_ndisc_start(link
->ndisc
);
1783 if (r
< 0 && r
!= -EBUSY
)
1784 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1787 if (link_radv_enabled(link
)) {
1789 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1791 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1793 r
= sd_radv_start(link
->radv
);
1794 if (r
< 0 && r
!= -EBUSY
)
1795 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1798 (void) dhcp6_request_prefix_delegation(link
);
1803 static int link_acquire_ipv4_conf(Link
*link
) {
1807 assert(link
->network
);
1808 assert(link
->manager
);
1809 assert(link
->manager
->event
);
1811 if (link_ipv4ll_enabled(link
)) {
1812 assert(link
->ipv4ll
);
1814 log_link_debug(link
, "Acquiring IPv4 link-local address");
1816 r
= sd_ipv4ll_start(link
->ipv4ll
);
1818 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1821 if (link_dhcp4_enabled(link
)) {
1822 assert(link
->dhcp_client
);
1824 log_link_debug(link
, "Acquiring DHCPv4 lease");
1826 r
= sd_dhcp_client_start(link
->dhcp_client
);
1828 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1834 static int link_acquire_conf(Link
*link
) {
1839 r
= link_acquire_ipv4_conf(link
);
1843 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1844 r
= link_acquire_ipv6_conf(link
);
1849 if (link_lldp_emit_enabled(link
)) {
1850 r
= link_lldp_emit_start(link
);
1852 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1858 bool link_has_carrier(Link
*link
) {
1859 /* see Documentation/networking/operstates.txt in the kernel sources */
1861 if (link
->kernel_operstate
== IF_OPER_UP
)
1864 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1865 /* operstate may not be implemented, so fall back to flags */
1866 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1872 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1877 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1880 r
= sd_netlink_message_get_errno(m
);
1882 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1887 static int link_configure_addrgen_mode(Link
*link
) {
1888 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1889 uint8_t ipv6ll_mode
;
1893 assert(link
->network
);
1894 assert(link
->manager
);
1895 assert(link
->manager
->rtnl
);
1897 log_link_debug(link
, "Setting address genmode for link");
1899 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1901 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1903 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1905 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1907 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1909 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1911 if (!link_ipv6ll_enabled(link
))
1912 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1914 const char *p
= NULL
;
1915 _cleanup_free_
char *stable_secret
= NULL
;
1917 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1919 /* The file may not exist. And event if it exists, when stable_secret is unset,
1920 * then reading the file fails and EIO is returned. */
1921 r
= read_one_line_file(p
, &stable_secret
);
1923 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1925 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1928 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1930 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1932 r
= sd_netlink_message_close_container(req
);
1934 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1936 r
= sd_netlink_message_close_container(req
);
1938 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1940 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1941 link_netlink_destroy_callback
, link
);
1943 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1950 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1955 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1958 r
= sd_netlink_message_get_errno(m
);
1960 /* we warn but don't fail the link, as it may be brought up later */
1961 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1966 static int link_up(Link
*link
) {
1967 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1971 assert(link
->network
);
1972 assert(link
->manager
);
1973 assert(link
->manager
->rtnl
);
1975 log_link_debug(link
, "Bringing link up");
1977 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1979 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1981 /* set it free if not enslaved with networkd */
1982 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1983 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1985 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1988 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1990 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1992 if (link
->network
->mac
) {
1993 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1995 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1998 if (link_ipv6_enabled(link
)) {
1999 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2001 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2003 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2004 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2006 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2008 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2009 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2011 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2014 r
= sd_netlink_message_close_container(req
);
2016 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2018 r
= sd_netlink_message_close_container(req
);
2020 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2023 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2024 link_netlink_destroy_callback
, link
);
2026 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2033 static int link_up_can(Link
*link
) {
2034 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2039 log_link_debug(link
, "Bringing CAN link up");
2041 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2043 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2045 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2047 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2049 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2050 link_netlink_destroy_callback
, link
);
2052 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2059 static int link_set_can(Link
*link
) {
2060 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2064 assert(link
->network
);
2065 assert(link
->manager
);
2066 assert(link
->manager
->rtnl
);
2068 log_link_debug(link
, "link_set_can");
2070 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2072 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2074 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2076 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2078 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2080 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2082 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2084 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2086 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2087 struct can_bittiming bt
= {
2088 .bitrate
= link
->network
->can_bitrate
,
2089 .sample_point
= link
->network
->can_sample_point
,
2092 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2093 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2097 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2098 if (link
->network
->can_sample_point
> 0)
2099 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2101 log_link_debug(link
, "Using default sample point");
2103 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2105 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2108 if (link
->network
->can_restart_us
> 0) {
2109 char time_string
[FORMAT_TIMESPAN_MAX
];
2110 uint64_t restart_ms
;
2112 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2115 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2117 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2119 if (restart_ms
> UINT32_MAX
) {
2120 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2124 log_link_debug(link
, "Setting restart = %s", time_string
);
2126 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2128 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2131 r
= sd_netlink_message_close_container(m
);
2133 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2135 r
= sd_netlink_message_close_container(m
);
2137 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2139 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2140 link_netlink_destroy_callback
, link
);
2142 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2146 if (!(link
->flags
& IFF_UP
)) {
2147 r
= link_up_can(link
);
2149 link_enter_failed(link
);
2154 log_link_debug(link
, "link_set_can done");
2159 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2164 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2167 r
= sd_netlink_message_get_errno(m
);
2169 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2171 if (streq_ptr(link
->kind
, "can"))
2177 int link_down(Link
*link
) {
2178 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2182 assert(link
->manager
);
2183 assert(link
->manager
->rtnl
);
2185 log_link_debug(link
, "Bringing link down");
2187 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2188 RTM_SETLINK
, link
->ifindex
);
2190 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2192 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2194 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2196 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2197 link_netlink_destroy_callback
, link
);
2199 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2206 static int link_handle_bound_to_list(Link
*link
) {
2210 bool required_up
= false;
2211 bool link_is_up
= false;
2215 if (hashmap_isempty(link
->bound_to_links
))
2218 if (link
->flags
& IFF_UP
)
2221 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2222 if (link_has_carrier(l
)) {
2227 if (!required_up
&& link_is_up
) {
2228 r
= link_down(link
);
2231 } else if (required_up
&& !link_is_up
) {
2240 static int link_handle_bound_by_list(Link
*link
) {
2247 if (hashmap_isempty(link
->bound_by_links
))
2250 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2251 r
= link_handle_bound_to_list(l
);
2259 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2265 if (link
== carrier
)
2268 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2271 r
= hashmap_ensure_allocated(h
, NULL
);
2275 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2282 static int link_new_bound_by_list(Link
*link
) {
2287 bool list_updated
= false;
2290 assert(link
->manager
);
2294 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2295 if (!carrier
->network
)
2298 if (strv_isempty(carrier
->network
->bind_carrier
))
2301 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2302 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2306 list_updated
= true;
2313 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2314 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2318 link_dirty(carrier
);
2324 static int link_new_bound_to_list(Link
*link
) {
2329 bool list_updated
= false;
2332 assert(link
->manager
);
2337 if (strv_isempty(link
->network
->bind_carrier
))
2342 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2343 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2344 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2348 list_updated
= true;
2355 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2356 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2360 link_dirty(carrier
);
2366 static int link_new_carrier_maps(Link
*link
) {
2369 r
= link_new_bound_by_list(link
);
2373 r
= link_handle_bound_by_list(link
);
2377 r
= link_new_bound_to_list(link
);
2381 r
= link_handle_bound_to_list(link
);
2388 static void link_free_bound_to_list(Link
*link
) {
2392 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2393 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2395 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2396 link_dirty(bound_to
);
2402 static void link_free_bound_by_list(Link
*link
) {
2406 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2407 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2409 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2410 link_dirty(bound_by
);
2411 link_handle_bound_to_list(bound_by
);
2418 static void link_free_carrier_maps(Link
*link
) {
2419 bool list_updated
= false;
2423 if (!hashmap_isempty(link
->bound_to_links
)) {
2424 link_free_bound_to_list(link
);
2425 list_updated
= true;
2428 if (!hashmap_isempty(link
->bound_by_links
)) {
2429 link_free_bound_by_list(link
);
2430 list_updated
= true;
2439 void link_drop(Link
*link
) {
2440 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2443 link_set_state(link
, LINK_STATE_LINGER
);
2445 link_free_carrier_maps(link
);
2447 log_link_debug(link
, "Link removed");
2449 (void) unlink(link
->state_file
);
2451 link_detach_from_manager(link
);
2458 static int link_joined(Link
*link
) {
2462 assert(link
->network
);
2464 if (!hashmap_isempty(link
->bound_to_links
)) {
2465 r
= link_handle_bound_to_list(link
);
2468 } else if (!(link
->flags
& IFF_UP
)) {
2471 link_enter_failed(link
);
2476 if (link
->network
->bridge
) {
2477 r
= link_set_bridge(link
);
2479 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2482 if (link
->network
->bond
) {
2483 r
= link_set_bond(link
);
2485 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2487 r
= link_append_bond_slave(link
);
2489 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2492 if (link
->network
->use_br_vlan
&&
2493 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2494 r
= link_set_bridge_vlan(link
);
2496 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2499 /* Skip setting up addresses until it gets carrier,
2500 or it would try to set addresses twice,
2501 which is bad for non-idempotent steps. */
2502 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2505 return link_request_set_addresses(link
);
2508 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2512 assert(link
->network
);
2516 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2519 r
= sd_netlink_message_get_errno(m
);
2520 if (r
< 0 && r
!= -EEXIST
) {
2521 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2522 link_enter_failed(link
);
2525 log_link_debug(link
, "Joined netdev");
2527 if (link
->enslaving
<= 0)
2533 static int link_enter_join_netdev(Link
*link
) {
2539 assert(link
->network
);
2540 assert(link
->state
== LINK_STATE_PENDING
);
2542 link_set_state(link
, LINK_STATE_CONFIGURING
);
2546 if (!link
->network
->bridge
&&
2547 !link
->network
->bond
&&
2548 !link
->network
->vrf
&&
2549 hashmap_isempty(link
->network
->stacked_netdevs
))
2550 return link_joined(link
);
2552 if (link
->network
->bond
) {
2553 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2554 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2555 return link_joined(link
);
2557 log_struct(LOG_DEBUG
,
2558 LOG_LINK_INTERFACE(link
),
2559 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2560 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2562 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2564 log_struct_errno(LOG_WARNING
, r
,
2565 LOG_LINK_INTERFACE(link
),
2566 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2567 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2568 link_enter_failed(link
);
2575 if (link
->network
->bridge
) {
2576 log_struct(LOG_DEBUG
,
2577 LOG_LINK_INTERFACE(link
),
2578 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2579 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2581 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2583 log_struct_errno(LOG_WARNING
, r
,
2584 LOG_LINK_INTERFACE(link
),
2585 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2586 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2587 link_enter_failed(link
);
2594 if (link
->network
->vrf
) {
2595 log_struct(LOG_DEBUG
,
2596 LOG_LINK_INTERFACE(link
),
2597 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2598 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2600 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2602 log_struct_errno(LOG_WARNING
, r
,
2603 LOG_LINK_INTERFACE(link
),
2604 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2605 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2606 link_enter_failed(link
);
2613 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2615 if (netdev
->ifindex
> 0) {
2620 log_struct(LOG_DEBUG
,
2621 LOG_LINK_INTERFACE(link
),
2622 LOG_NETDEV_INTERFACE(netdev
),
2623 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2625 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2627 log_struct_errno(LOG_WARNING
, r
,
2628 LOG_LINK_INTERFACE(link
),
2629 LOG_NETDEV_INTERFACE(netdev
),
2630 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2631 link_enter_failed(link
);
2641 static int link_set_ipv4_forward(Link
*link
) {
2644 if (!link_ipv4_forward_enabled(link
))
2647 /* We propagate the forwarding flag from one interface to the
2648 * global setting one way. This means: as long as at least one
2649 * interface was configured at any time that had IP forwarding
2650 * enabled the setting will stay on for good. We do this
2651 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2652 * somewhat in sync (see below). */
2654 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2656 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2661 static int link_set_ipv6_forward(Link
*link
) {
2664 if (!link_ipv6_forward_enabled(link
))
2667 /* On Linux, the IPv6 stack does not know a per-interface
2668 * packet forwarding setting: either packet forwarding is on
2669 * for all, or off for all. We hence don't bother with a
2670 * per-interface setting, but simply propagate the interface
2671 * flag, if it is set, to the global flag, one-way. Note that
2672 * while IPv4 would allow a per-interface flag, we expose the
2673 * same behaviour there and also propagate the setting from
2674 * one to all, to keep things simple (see above). */
2676 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2678 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2683 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2684 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2685 IPv6PrivacyExtensions s
;
2686 const char *p
= NULL
;
2689 s
= link_ipv6_privacy_extensions(link
);
2693 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2694 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2696 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2698 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2703 static int link_set_ipv6_accept_ra(Link
*link
) {
2704 const char *p
= NULL
;
2707 /* Make this a NOP if IPv6 is not available */
2708 if (!socket_ipv6_is_supported())
2711 if (link
->flags
& IFF_LOOPBACK
)
2717 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2719 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2720 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2722 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2727 static int link_set_ipv6_dad_transmits(Link
*link
) {
2728 char buf
[DECIMAL_STR_MAX(int) + 1];
2729 const char *p
= NULL
;
2732 /* Make this a NOP if IPv6 is not available */
2733 if (!socket_ipv6_is_supported())
2736 if (link
->flags
& IFF_LOOPBACK
)
2742 if (link
->network
->ipv6_dad_transmits
< 0)
2745 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2746 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2748 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2750 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2755 static int link_set_ipv6_hop_limit(Link
*link
) {
2756 char buf
[DECIMAL_STR_MAX(int) + 1];
2757 const char *p
= NULL
;
2760 /* Make this a NOP if IPv6 is not available */
2761 if (!socket_ipv6_is_supported())
2764 if (link
->flags
& IFF_LOOPBACK
)
2770 if (link
->network
->ipv6_hop_limit
< 0)
2773 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2774 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2776 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2778 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2783 static int link_set_ipv6_mtu(Link
*link
) {
2784 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2785 const char *p
= NULL
;
2788 /* Make this a NOP if IPv6 is not available */
2789 if (!socket_ipv6_is_supported())
2792 if (link
->flags
& IFF_LOOPBACK
)
2795 if (link
->network
->ipv6_mtu
== 0)
2798 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2800 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2802 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2804 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2809 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2810 Address
*net_address
;
2818 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2819 if (address_equal(net_address
, address
))
2825 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2834 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2835 if (route_equal(net_route
, route
))
2841 static int link_drop_foreign_config(Link
*link
) {
2847 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2848 /* we consider IPv6LL addresses to be managed by the kernel */
2849 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2852 if (link_is_static_address_configured(link
, address
)) {
2853 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2855 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2857 r
= address_remove(address
, link
, NULL
);
2863 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2864 /* do not touch routes managed by the kernel */
2865 if (route
->protocol
== RTPROT_KERNEL
)
2868 if (link_is_static_route_configured(link
, route
)) {
2869 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2873 r
= route_remove(route
, link
, NULL
);
2882 static int link_drop_config(Link
*link
) {
2883 Address
*address
, *pool_address
;
2888 SET_FOREACH(address
, link
->addresses
, i
) {
2889 /* we consider IPv6LL addresses to be managed by the kernel */
2890 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2893 r
= address_remove(address
, link
, NULL
);
2897 /* If this address came from an address pool, clean up the pool */
2898 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2899 if (address_equal(address
, pool_address
)) {
2900 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2901 address_free(pool_address
);
2907 SET_FOREACH(route
, link
->routes
, i
) {
2908 /* do not touch routes managed by the kernel */
2909 if (route
->protocol
== RTPROT_KERNEL
)
2912 r
= route_remove(route
, link
, NULL
);
2922 static int link_update_lldp(Link
*link
) {
2930 if (link
->flags
& IFF_UP
) {
2931 r
= sd_lldp_start(link
->lldp
);
2933 log_link_debug(link
, "Started LLDP.");
2935 r
= sd_lldp_stop(link
->lldp
);
2937 log_link_debug(link
, "Stopped LLDP.");
2943 static int link_configure_can(Link
*link
) {
2946 if (streq_ptr(link
->kind
, "can")) {
2947 /* The CAN interface must be down to configure bitrate, etc... */
2948 if ((link
->flags
& IFF_UP
)) {
2949 r
= link_down(link
);
2951 link_enter_failed(link
);
2958 return link_set_can(link
);
2961 if (!(link
->flags
& IFF_UP
)) {
2962 r
= link_up_can(link
);
2964 link_enter_failed(link
);
2972 static int link_configure(Link
*link
) {
2976 assert(link
->network
);
2977 assert(link
->state
== LINK_STATE_PENDING
);
2979 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2980 return link_configure_can(link
);
2982 /* Drop foreign config, but ignore loopback or critical devices.
2983 * We do not want to remove loopback address or addresses used for root NFS. */
2984 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2985 r
= link_drop_foreign_config(link
);
2990 r
= link_set_proxy_arp(link
);
2994 r
= ipv6_proxy_ndp_addresses_configure(link
);
2998 r
= link_set_ipv4_forward(link
);
3002 r
= link_set_ipv6_forward(link
);
3006 r
= link_set_ipv6_privacy_extensions(link
);
3010 r
= link_set_ipv6_accept_ra(link
);
3014 r
= link_set_ipv6_dad_transmits(link
);
3018 r
= link_set_ipv6_hop_limit(link
);
3022 r
= link_set_flags(link
);
3026 r
= link_set_ipv6_mtu(link
);
3030 if (link_ipv4ll_enabled(link
)) {
3031 r
= ipv4ll_configure(link
);
3036 if (link_dhcp4_enabled(link
)) {
3037 r
= dhcp4_set_promote_secondaries(link
);
3041 r
= dhcp4_configure(link
);
3046 if (link_dhcp4_server_enabled(link
)) {
3047 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3051 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3056 if (link_dhcp6_enabled(link
) ||
3057 link_ipv6_accept_ra_enabled(link
)) {
3058 r
= dhcp6_configure(link
);
3063 if (link_ipv6_accept_ra_enabled(link
)) {
3064 r
= ndisc_configure(link
);
3069 if (link_radv_enabled(link
)) {
3070 r
= radv_configure(link
);
3075 if (link_lldp_rx_enabled(link
)) {
3076 r
= sd_lldp_new(&link
->lldp
);
3080 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3084 r
= sd_lldp_match_capabilities(link
->lldp
,
3085 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3086 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3087 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3091 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3095 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3099 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3103 r
= link_update_lldp(link
);
3108 if (link
->network
->mtu
> 0) {
3109 r
= link_set_mtu(link
, link
->network
->mtu
);
3114 if (socket_ipv6_is_supported()) {
3115 r
= link_configure_addrgen_mode(link
);
3120 return link_configure_after_setting_mtu(link
);
3123 static int link_configure_after_setting_mtu(Link
*link
) {
3127 assert(link
->network
);
3128 assert(link
->state
== LINK_STATE_PENDING
);
3130 if (link
->setting_mtu
)
3133 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3134 r
= link_acquire_conf(link
);
3139 return link_enter_join_netdev(link
);
3142 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3145 if (duid
->raw_data_len
> 0)
3148 if (duid
->type
!= DUID_TYPE_UUID
)
3151 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3152 duid
->raw_data_len
= sizeof(sd_id128_t
);
3157 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3158 Manager
*manager
= userdata
;
3159 const sd_bus_error
*e
;
3169 e
= sd_bus_message_get_error(m
);
3171 log_error_errno(sd_bus_error_get_errno(e
),
3172 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3177 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3181 if (sz
!= sizeof(sd_id128_t
)) {
3182 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3186 memcpy(&manager
->product_uuid
, a
, sz
);
3187 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3188 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3190 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3193 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3194 r
= link_configure(link
);
3196 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3199 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3201 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3202 * even if the method fails. */
3203 manager
->has_product_uuid
= true;
3208 static bool link_requires_uuid(Link
*link
) {
3212 assert(link
->manager
);
3213 assert(link
->network
);
3215 duid
= link_get_duid(link
);
3216 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3219 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3222 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3228 static int link_configure_duid(Link
*link
) {
3234 assert(link
->manager
);
3235 assert(link
->network
);
3238 duid
= link_get_duid(link
);
3240 if (!link_requires_uuid(link
))
3243 if (m
->has_product_uuid
) {
3244 (void) duid_set_uuid(duid
, m
->product_uuid
);
3248 if (!m
->links_requesting_uuid
) {
3249 r
= manager_request_product_uuid(m
, link
);
3254 log_link_warning_errno(link
, r
,
3255 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3259 r
= set_put(m
->links_requesting_uuid
, link
);
3263 r
= set_put(m
->duids_requesting_uuid
, duid
);
3271 static int link_initialized_and_synced(Link
*link
) {
3276 assert(link
->ifname
);
3277 assert(link
->manager
);
3279 if (link
->state
!= LINK_STATE_PENDING
)
3282 log_link_debug(link
, "Link state is up-to-date");
3284 r
= link_new_bound_by_list(link
);
3288 r
= link_handle_bound_by_list(link
);
3292 if (!link
->network
) {
3293 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3294 &link
->mac
, &network
);
3296 link_enter_unmanaged(link
);
3298 } else if (r
== 0 && network
->unmanaged
) {
3299 link_enter_unmanaged(link
);
3304 if (link
->flags
& IFF_LOOPBACK
) {
3305 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3306 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3308 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3309 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3311 if (network
->dhcp_server
)
3312 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3315 r
= network_apply(network
, link
);
3320 r
= link_new_bound_to_list(link
);
3324 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3325 * link_configure() is called later asynchronously. */
3326 r
= link_configure_duid(link
);
3330 r
= link_configure(link
);
3337 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3338 (void) link_initialized_and_synced(link
);
3342 int link_initialized(Link
*link
, sd_device
*device
) {
3343 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3347 assert(link
->manager
);
3348 assert(link
->manager
->rtnl
);
3351 if (link
->state
!= LINK_STATE_PENDING
)
3354 if (link
->sd_device
)
3357 log_link_debug(link
, "udev initialized link");
3359 link
->sd_device
= sd_device_ref(device
);
3361 /* udev has initialized the link, but we don't know if we have yet
3362 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3363 * when it returns we know that the pending NEWLINKs have already been
3364 * processed and that we are up-to-date */
3366 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3371 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3372 link_netlink_destroy_callback
, link
);
3381 static int link_load(Link
*link
) {
3382 _cleanup_free_
char *network_file
= NULL
,
3385 *dhcp4_address
= NULL
,
3386 *ipv4ll_address
= NULL
;
3387 union in_addr_union address
;
3388 union in_addr_union route_dst
;
3394 r
= parse_env_file(NULL
, link
->state_file
,
3395 "NETWORK_FILE", &network_file
,
3396 "ADDRESSES", &addresses
,
3398 "DHCP4_ADDRESS", &dhcp4_address
,
3399 "IPV4LL_ADDRESS", &ipv4ll_address
);
3400 if (r
< 0 && r
!= -ENOENT
)
3401 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3408 suffix
= strrchr(network_file
, '.');
3410 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3411 goto network_file_fail
;
3415 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3417 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3418 goto network_file_fail
;
3421 r
= network_apply(network
, link
);
3423 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3432 _cleanup_free_
char *address_str
= NULL
;
3433 char *prefixlen_str
;
3435 unsigned char prefixlen
;
3437 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3439 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3445 prefixlen_str
= strchr(address_str
, '/');
3446 if (!prefixlen_str
) {
3447 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3451 *prefixlen_str
++ = '\0';
3453 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3455 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3459 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3461 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3465 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3467 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3476 _cleanup_free_
char *route_str
= NULL
;
3477 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3479 char *prefixlen_str
;
3481 unsigned char prefixlen
, tos
, table
;
3484 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3486 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3492 prefixlen_str
= strchr(route_str
, '/');
3493 if (!prefixlen_str
) {
3494 log_link_debug(link
, "Failed to parse route %s", route_str
);
3498 *prefixlen_str
++ = '\0';
3500 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3502 log_link_debug(link
,
3503 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3508 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3510 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3514 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3516 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3518 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3519 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3520 0, route_expire_handler
, route
);
3522 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3525 route
->lifetime
= lifetime
;
3526 sd_event_source_unref(route
->expire
);
3527 route
->expire
= TAKE_PTR(expire
);
3531 if (dhcp4_address
) {
3532 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3534 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3535 goto dhcp4_address_fail
;
3538 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3540 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3542 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3544 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3549 if (ipv4ll_address
) {
3550 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3552 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3553 goto ipv4ll_address_fail
;
3556 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3558 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3560 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3562 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3565 ipv4ll_address_fail
:
3570 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3571 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3572 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3581 r
= link_new(m
, message
, ret
);
3587 log_link_debug(link
, "Link %d added", link
->ifindex
);
3589 r
= link_load(link
);
3593 if (detect_container() <= 0) {
3594 /* not in a container, udev will be around */
3595 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3596 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3598 log_link_warning_errno(link
, r
, "Could not find device: %m");
3602 r
= sd_device_get_is_initialized(device
);
3604 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3609 log_link_debug(link
, "link pending udev initialization...");
3613 r
= link_initialized(link
, device
);
3617 r
= link_initialized_and_synced(link
);
3624 link_enter_failed(link
);
3628 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3633 log_link_info(link
, "Gained IPv6LL");
3635 link
->ipv6ll_address
= *address
;
3636 link_check_ready(link
);
3638 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3639 r
= link_acquire_ipv6_conf(link
);
3641 link_enter_failed(link
);
3649 static int link_carrier_gained(Link
*link
) {
3654 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3655 r
= link_acquire_conf(link
);
3657 link_enter_failed(link
);
3661 r
= link_request_set_addresses(link
);
3666 r
= link_handle_bound_by_list(link
);
3673 static int link_carrier_lost(Link
*link
) {
3678 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3681 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3682 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3683 * configuration and stop the clients as well. */
3684 if (link
->setting_mtu
)
3687 r
= link_stop_clients(link
);
3689 link_enter_failed(link
);
3693 if (link_dhcp4_server_enabled(link
))
3694 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3696 r
= link_drop_config(link
);
3700 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3701 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3702 r
= link_drop_foreign_config(link
);
3707 r
= link_handle_bound_by_list(link
);
3714 int link_carrier_reset(Link
*link
) {
3719 if (link_has_carrier(link
)) {
3720 r
= link_carrier_lost(link
);
3724 r
= link_carrier_gained(link
);
3728 log_link_info(link
, "Reset carrier");
3734 int link_update(Link
*link
, sd_netlink_message
*m
) {
3735 struct ether_addr mac
;
3738 bool had_carrier
, carrier_gained
, carrier_lost
;
3742 assert(link
->ifname
);
3745 if (link
->state
== LINK_STATE_LINGER
) {
3746 log_link_info(link
, "Link readded");
3747 link_set_state(link
, LINK_STATE_CONFIGURING
);
3749 r
= link_new_carrier_maps(link
);
3754 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3755 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3756 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3758 if (link
->state
== LINK_STATE_PENDING
) {
3759 r
= free_and_strdup(&link
->ifname
, ifname
);
3763 Manager
*manager
= link
->manager
;
3766 r
= link_add(manager
, m
, &link
);
3772 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3773 if (r
>= 0 && mtu
> 0) {
3775 if (link
->original_mtu
== 0) {
3776 link
->original_mtu
= mtu
;
3777 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3780 if (link
->dhcp_client
) {
3781 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3784 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3788 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3790 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3794 /* The kernel may broadcast NEWLINK messages without the MAC address
3795 set, simply ignore them. */
3796 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3798 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3801 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3804 log_link_debug(link
, "MAC address: "
3805 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3806 mac
.ether_addr_octet
[0],
3807 mac
.ether_addr_octet
[1],
3808 mac
.ether_addr_octet
[2],
3809 mac
.ether_addr_octet
[3],
3810 mac
.ether_addr_octet
[4],
3811 mac
.ether_addr_octet
[5]);
3814 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3816 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3819 if (link
->dhcp_client
) {
3820 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3821 (const uint8_t *) &link
->mac
,
3825 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3827 r
= dhcp4_set_client_identifier(link
);
3832 if (link
->dhcp6_client
) {
3833 const DUID
* duid
= link_get_duid(link
);
3835 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3836 (const uint8_t *) &link
->mac
,
3840 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3842 if (link
->network
->iaid_set
) {
3843 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3844 link
->network
->iaid
);
3846 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3849 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3851 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3852 duid
->raw_data_len
);
3854 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3858 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3860 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3864 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3866 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3871 had_carrier
= link_has_carrier(link
);
3873 r
= link_update_flags(link
, m
);
3877 r
= link_update_lldp(link
);
3881 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3882 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3884 if (carrier_gained
) {
3885 log_link_info(link
, "Gained carrier");
3887 r
= link_carrier_gained(link
);
3890 } else if (carrier_lost
) {
3891 log_link_info(link
, "Lost carrier");
3893 r
= link_carrier_lost(link
);
3901 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3909 if (hashmap_isempty(h
))
3913 HASHMAP_FOREACH(link
, h
, i
) {
3917 fprintf(f
, "%i", link
->ifindex
);
3924 int link_save(Link
*link
) {
3925 _cleanup_free_
char *temp_path
= NULL
;
3926 _cleanup_fclose_
FILE *f
= NULL
;
3927 const char *admin_state
, *oper_state
;
3934 assert(link
->state_file
);
3935 assert(link
->lease_file
);
3936 assert(link
->manager
);
3938 if (link
->state
== LINK_STATE_LINGER
) {
3939 unlink(link
->state_file
);
3943 link_lldp_save(link
);
3945 admin_state
= link_state_to_string(link
->state
);
3946 assert(admin_state
);
3948 oper_state
= link_operstate_to_string(link
->operstate
);
3951 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3955 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3956 (void) fchmod(fileno(f
), 0644);
3959 "# This is private data. Do not parse.\n"
3962 admin_state
, oper_state
);
3964 if (link
->network
) {
3966 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3967 const char *dhcp_domainname
= NULL
;
3968 char **dhcp6_domains
= NULL
;
3969 char **dhcp_domains
= NULL
;
3972 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3973 yes_no(link
->network
->required_for_online
));
3975 if (link
->dhcp6_client
) {
3976 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3977 if (r
< 0 && r
!= -ENOMSG
)
3978 log_link_debug(link
, "No DHCPv6 lease");
3981 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3986 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3987 _cleanup_free_
char *b
= NULL
;
3989 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3990 &link
->network
->dns
[j
].address
, &b
);
3992 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4002 if (link
->network
->dhcp_use_dns
&&
4004 const struct in_addr
*addresses
;
4006 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4010 serialize_in_addrs(f
, addresses
, r
);
4015 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4016 struct in6_addr
*in6_addrs
;
4018 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4022 serialize_in6_addrs(f
, in6_addrs
, r
);
4027 /* Make sure to flush out old entries before we use the NDISC data */
4030 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4033 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4037 serialize_in6_addrs(f
, &dd
->address
, 1);
4046 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4048 if (link
->network
->dhcp_use_ntp
&&
4050 const struct in_addr
*addresses
;
4052 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4056 serialize_in_addrs(f
, addresses
, r
);
4061 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4062 struct in6_addr
*in6_addrs
;
4065 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4070 serialize_in6_addrs(f
, in6_addrs
, r
);
4074 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4076 fputstrv(f
, hosts
, NULL
, &space
);
4081 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4082 if (link
->dhcp_lease
) {
4083 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4084 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4087 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4090 fputs("DOMAINS=", f
);
4092 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
4094 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4097 if (dhcp_domainname
)
4098 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4100 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4102 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4104 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4105 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4110 fputs("ROUTE_DOMAINS=", f
);
4112 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
4114 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4117 if (dhcp_domainname
)
4118 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4120 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4122 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4124 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4125 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4130 fprintf(f
, "LLMNR=%s\n",
4131 resolve_support_to_string(link
->network
->llmnr
));
4132 fprintf(f
, "MDNS=%s\n",
4133 resolve_support_to_string(link
->network
->mdns
));
4134 if (link
->network
->dns_default_route
>= 0)
4135 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4137 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4138 fprintf(f
, "DNS_OVER_TLS=%s\n",
4139 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4141 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4142 fprintf(f
, "DNSSEC=%s\n",
4143 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4145 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4148 fputs("DNSSEC_NTA=", f
);
4150 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4151 fputs_with_space(f
, n
, NULL
, &space
);
4155 fputs("ADDRESSES=", f
);
4157 SET_FOREACH(a
, link
->addresses
, i
) {
4158 _cleanup_free_
char *address_str
= NULL
;
4160 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4164 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4169 fputs("ROUTES=", f
);
4171 SET_FOREACH(route
, link
->routes
, i
) {
4172 _cleanup_free_
char *route_str
= NULL
;
4174 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4178 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4179 space
? " " : "", route_str
,
4180 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4187 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4188 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4190 if (link
->dhcp_lease
) {
4191 struct in_addr address
;
4192 const char *tz
= NULL
;
4194 assert(link
->network
);
4196 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4198 fprintf(f
, "TIMEZONE=%s\n", tz
);
4200 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4202 fputs("DHCP4_ADDRESS=", f
);
4203 serialize_in_addrs(f
, &address
, 1);
4207 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4215 unlink(link
->lease_file
);
4218 struct in_addr address
;
4220 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4222 fputs("IPV4LL_ADDRESS=", f
);
4223 serialize_in_addrs(f
, &address
, 1);
4228 r
= fflush_and_check(f
);
4232 if (rename(temp_path
, link
->state_file
) < 0) {
4240 (void) unlink(link
->state_file
);
4242 (void) unlink(temp_path
);
4244 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4247 /* The serialized state in /run is no longer up-to-date. */
4248 void link_dirty(Link
*link
) {
4253 /* mark manager dirty as link is dirty */
4254 manager_dirty(link
->manager
);
4256 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4258 /* allocation errors are ignored */
4261 r
= set_put(link
->manager
->dirty_links
, link
);
4263 /* don't take another ref if the link was already dirty */
4269 /* The serialized state in /run is up-to-date */
4270 void link_clean(Link
*link
) {
4272 assert(link
->manager
);
4274 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4277 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4278 [LINK_STATE_PENDING
] = "pending",
4279 [LINK_STATE_CONFIGURING
] = "configuring",
4280 [LINK_STATE_CONFIGURED
] = "configured",
4281 [LINK_STATE_UNMANAGED
] = "unmanaged",
4282 [LINK_STATE_FAILED
] = "failed",
4283 [LINK_STATE_LINGER
] = "linger",
4286 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4288 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4289 [LINK_OPERSTATE_OFF
] = "off",
4290 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4291 [LINK_OPERSTATE_DORMANT
] = "dormant",
4292 [LINK_OPERSTATE_CARRIER
] = "carrier",
4293 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4294 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
4295 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4298 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);