1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
7 #include "alloc-util.h"
9 #include "dhcp-identifier.h"
10 #include "dhcp-lease-internal.h"
14 #include "missing_network.h"
15 #include "netdev/vrf.h"
16 #include "netlink-util.h"
17 #include "network-internal.h"
18 #include "networkd-can.h"
19 #include "networkd-ipv6-proxy-ndp.h"
20 #include "networkd-lldp-tx.h"
21 #include "networkd-manager.h"
22 #include "networkd-ndisc.h"
23 #include "networkd-neighbor.h"
24 #include "networkd-radv.h"
25 #include "networkd-routing-policy-rule.h"
27 #include "socket-util.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
31 #include "sysctl-util.h"
32 #include "tmpfile-util.h"
33 #include "udev-util.h"
37 uint32_t link_get_vrf_table(Link
*link
) {
38 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
41 uint32_t link_get_dhcp_route_table(Link
*link
) {
42 /* When the interface is part of an VRF use the VRFs routing table, unless
43 * another table is explicitly specified. */
44 if (link
->network
->dhcp_route_table_set
)
45 return link
->network
->dhcp_route_table
;
46 return link_get_vrf_table(link
);
49 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
50 if (link
->network
->ipv6_accept_ra_route_table_set
)
51 return link
->network
->ipv6_accept_ra_route_table
;
52 return link_get_vrf_table(link
);
55 DUID
* link_get_duid(Link
*link
) {
56 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
57 return &link
->network
->duid
;
59 return &link
->manager
->duid
;
62 static bool link_dhcp6_enabled(Link
*link
) {
65 if (!socket_ipv6_is_supported())
68 if (link
->flags
& IFF_LOOPBACK
)
74 if (link
->network
->bond
)
77 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
80 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
83 static bool link_dhcp4_enabled(Link
*link
) {
86 if (link
->flags
& IFF_LOOPBACK
)
92 if (link
->network
->bond
)
95 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
98 static bool link_dhcp4_server_enabled(Link
*link
) {
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (link
->network
->bond
)
110 return link
->network
->dhcp_server
;
113 bool link_ipv4ll_enabled(Link
*link
) {
116 if (link
->flags
& IFF_LOOPBACK
)
122 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
125 if (link
->network
->bond
)
128 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
131 bool link_ipv4ll_fallback_enabled(Link
*link
) {
134 if (link
->flags
& IFF_LOOPBACK
)
140 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
143 if (link
->network
->bond
)
146 return link
->network
->link_local
& ADDRESS_FAMILY_FALLBACK_IPV4
;
149 static bool link_ipv6ll_enabled(Link
*link
) {
152 if (!socket_ipv6_is_supported())
155 if (link
->flags
& IFF_LOOPBACK
)
161 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
164 if (link
->network
->bond
)
167 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
170 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
173 static bool link_ipv6_enabled(Link
*link
) {
176 if (!socket_ipv6_is_supported())
179 if (link
->network
->bond
)
182 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
185 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
186 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
189 static bool link_radv_enabled(Link
*link
) {
192 if (!link_ipv6ll_enabled(link
))
195 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
198 static bool link_ipv4_forward_enabled(Link
*link
) {
201 if (link
->flags
& IFF_LOOPBACK
)
207 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
210 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
213 static bool link_ipv6_forward_enabled(Link
*link
) {
216 if (!socket_ipv6_is_supported())
219 if (link
->flags
& IFF_LOOPBACK
)
225 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
228 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
231 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
234 static bool link_proxy_arp_enabled(Link
*link
) {
237 if (link
->flags
& IFF_LOOPBACK
)
243 if (link
->network
->proxy_arp
< 0)
249 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
252 if (!socket_ipv6_is_supported())
255 if (link
->flags
& IFF_LOOPBACK
)
261 if (!link_ipv6ll_enabled(link
))
264 /* If unset use system default (enabled if local forwarding is disabled.
265 * disabled if local forwarding is enabled).
266 * If set, ignore or enforce RA independent of local forwarding state.
268 if (link
->network
->ipv6_accept_ra
< 0)
269 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
270 return !link_ipv6_forward_enabled(link
);
271 else if (link
->network
->ipv6_accept_ra
> 0)
272 /* accept RA even if ip_forward is enabled */
279 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
282 if (!socket_ipv6_is_supported())
283 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
285 if (link
->flags
& IFF_LOOPBACK
)
286 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
289 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
291 return link
->network
->ipv6_privacy_extensions
;
294 static int link_enable_ipv6(Link
*link
) {
298 if (link
->flags
& IFF_LOOPBACK
)
301 disabled
= !link_ipv6_enabled(link
);
303 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
305 log_link_warning_errno(link
, r
, "Cannot %s IPv6: %m", enable_disable(!disabled
));
307 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
312 static bool link_is_enslaved(Link
*link
) {
313 if (link
->flags
& IFF_SLAVE
)
314 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
317 if (!link
->enslaved_raw
)
323 if (link
->network
->bridge
)
324 /* TODO: support the case when link is not managed by networkd. */
330 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
336 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
339 link_update_operstate(master
, true);
342 void link_update_operstate(Link
*link
, bool also_update_master
) {
343 LinkOperationalState operstate
;
348 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
349 operstate
= LINK_OPERSTATE_DORMANT
;
350 else if (link_has_carrier(link
)) {
352 uint8_t scope
= RT_SCOPE_NOWHERE
;
354 /* if we have carrier, check what addresses we have */
355 SET_FOREACH(address
, link
->addresses
, i
) {
356 if (!address_is_ready(address
))
359 if (address
->scope
< scope
)
360 scope
= address
->scope
;
363 /* for operstate we also take foreign addresses into account */
364 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
365 if (!address_is_ready(address
))
368 if (address
->scope
< scope
)
369 scope
= address
->scope
;
372 if (scope
< RT_SCOPE_SITE
)
373 /* universally accessible addresses found */
374 operstate
= LINK_OPERSTATE_ROUTABLE
;
375 else if (scope
< RT_SCOPE_HOST
)
376 /* only link or site local addresses found */
377 operstate
= LINK_OPERSTATE_DEGRADED
;
379 /* no useful addresses found */
380 operstate
= LINK_OPERSTATE_CARRIER
;
381 } else if (link
->flags
& IFF_UP
)
382 operstate
= LINK_OPERSTATE_NO_CARRIER
;
384 operstate
= LINK_OPERSTATE_OFF
;
386 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
387 link_is_enslaved(link
))
388 operstate
= LINK_OPERSTATE_ENSLAVED
;
390 if (operstate
>= LINK_OPERSTATE_CARRIER
) {
393 SET_FOREACH(slave
, link
->slaves
, i
) {
394 link_update_operstate(slave
, false);
396 if (slave
->operstate
< LINK_OPERSTATE_CARRIER
)
397 operstate
= LINK_OPERSTATE_DEGRADED_CARRIER
;
401 if (link
->operstate
!= operstate
) {
402 link
->operstate
= operstate
;
403 link_send_changed(link
, "OperationalState", NULL
);
407 if (also_update_master
&& link
->network
) {
408 link_update_master_operstate(link
, link
->network
->bond
);
409 link_update_master_operstate(link
, link
->network
->bridge
);
413 #define FLAG_STRING(string, flag, old, new) \
414 (((old ^ new) & flag) \
415 ? ((old & flag) ? (" -" string) : (" +" string)) \
418 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
419 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
425 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
427 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
429 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
431 /* if we got a message without operstate, take it to mean
432 the state was unchanged */
433 operstate
= link
->kernel_operstate
;
435 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
438 if (link
->flags
!= flags
) {
439 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",
440 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
441 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
442 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
443 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
444 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
445 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
446 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
447 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
448 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
449 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
450 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
451 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
452 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
453 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
454 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
455 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
456 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
457 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
458 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
460 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
461 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
462 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
463 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
464 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
465 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
466 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
467 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
469 /* link flags are currently at most 18 bits, let's align to
471 if (unknown_flags_added
)
473 "Unknown link flags gained: %#.5x (ignoring)",
474 unknown_flags_added
);
476 if (unknown_flags_removed
)
478 "Unknown link flags lost: %#.5x (ignoring)",
479 unknown_flags_removed
);
483 link
->kernel_operstate
= operstate
;
485 link_update_operstate(link
, true);
490 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
491 _cleanup_(link_unrefp
) Link
*link
= NULL
;
493 const char *ifname
, *kind
= NULL
;
495 unsigned short iftype
;
501 /* check for link kind */
502 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
504 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
505 r
= sd_netlink_message_exit_container(message
);
510 r
= sd_netlink_message_get_type(message
, &type
);
513 else if (type
!= RTM_NEWLINK
)
516 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
519 else if (ifindex
<= 0)
522 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
526 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
537 .state
= LINK_STATE_PENDING
,
538 .rtnl_extended_attrs
= true,
543 link
->ifname
= strdup(ifname
);
548 link
->kind
= strdup(kind
);
553 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
555 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
557 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
559 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
561 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
564 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
567 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
570 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
574 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
578 r
= link_update_flags(link
, message
);
582 *ret
= TAKE_PTR(link
);
587 static Link
*link_free(Link
*link
) {
592 link
->routes
= set_free_with_destructor(link
->routes
, route_free
);
593 link
->routes_foreign
= set_free_with_destructor(link
->routes_foreign
, route_free
);
595 link
->addresses
= set_free_with_destructor(link
->addresses
, address_free
);
596 link
->addresses_foreign
= set_free_with_destructor(link
->addresses_foreign
, address_free
);
598 while ((address
= link
->pool_addresses
)) {
599 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
600 address_free(address
);
603 sd_dhcp_server_unref(link
->dhcp_server
);
604 sd_dhcp_client_unref(link
->dhcp_client
);
605 sd_dhcp_lease_unref(link
->dhcp_lease
);
607 link_lldp_emit_stop(link
);
609 free(link
->lease_file
);
611 sd_lldp_unref(link
->lldp
);
612 free(link
->lldp_file
);
616 sd_ipv4ll_unref(link
->ipv4ll
);
617 sd_dhcp6_client_unref(link
->dhcp6_client
);
618 sd_ndisc_unref(link
->ndisc
);
619 sd_radv_unref(link
->radv
);
624 (void) unlink(link
->state_file
);
625 free(link
->state_file
);
627 sd_device_unref(link
->sd_device
);
629 hashmap_free(link
->bound_to_links
);
630 hashmap_free(link
->bound_by_links
);
632 set_free_with_destructor(link
->slaves
, link_unref
);
634 network_unref(link
->network
);
639 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
641 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
648 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
657 static void link_set_state(Link
*link
, LinkState state
) {
660 if (link
->state
== state
)
663 log_link_debug(link
, "State changed: %s -> %s",
664 link_state_to_string(link
->state
),
665 link_state_to_string(state
));
669 link_send_changed(link
, "AdministrativeState", NULL
);
672 static void link_enter_unmanaged(Link
*link
) {
675 log_link_debug(link
, "Unmanaged");
677 link_set_state(link
, LINK_STATE_UNMANAGED
);
682 int link_stop_clients(Link
*link
) {
686 assert(link
->manager
);
687 assert(link
->manager
->event
);
689 if (link
->dhcp_client
) {
690 k
= sd_dhcp_client_stop(link
->dhcp_client
);
692 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
696 k
= sd_ipv4ll_stop(link
->ipv4ll
);
698 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
701 if (link
->dhcp6_client
) {
702 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
704 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
708 k
= sd_ndisc_stop(link
->ndisc
);
710 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
714 k
= sd_radv_stop(link
->radv
);
716 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
719 link_lldp_emit_stop(link
);
723 void link_enter_failed(Link
*link
) {
726 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
729 log_link_warning(link
, "Failed");
731 link_set_state(link
, LINK_STATE_FAILED
);
733 link_stop_clients(link
);
738 static Address
* link_find_dhcp_server_address(Link
*link
) {
742 assert(link
->network
);
744 /* The first statically configured address if there is any */
745 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
747 if (address
->family
!= AF_INET
)
750 if (in_addr_is_null(address
->family
, &address
->in_addr
))
756 /* If that didn't work, find a suitable address we got from the pool */
757 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
758 if (address
->family
!= AF_INET
)
767 static int link_join_netdevs_after_configured(Link
*link
) {
772 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
773 if (netdev
->ifindex
> 0)
774 /* Assume already enslaved. */
777 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_AFTER_CONFIGURED
)
780 log_struct(LOG_DEBUG
,
781 LOG_LINK_INTERFACE(link
),
782 LOG_NETDEV_INTERFACE(netdev
),
783 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
785 r
= netdev_join(netdev
, link
, NULL
);
787 return log_struct_errno(LOG_WARNING
, r
,
788 LOG_LINK_INTERFACE(link
),
789 LOG_NETDEV_INTERFACE(netdev
),
790 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
796 static void link_enter_configured(Link
*link
) {
798 assert(link
->network
);
800 if (link
->state
!= LINK_STATE_CONFIGURING
)
803 log_link_info(link
, "Configured");
805 link_set_state(link
, LINK_STATE_CONFIGURED
);
807 (void) link_join_netdevs_after_configured(link
);
812 static int link_request_set_routing_policy_rule(Link
*link
) {
813 RoutingPolicyRule
*rule
, *rrule
= NULL
;
817 assert(link
->network
);
819 link_set_state(link
, LINK_STATE_CONFIGURING
);
820 link
->routing_policy_rules_configured
= false;
822 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
823 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
824 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
825 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
827 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
831 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
833 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
834 link_enter_failed(link
);
838 link
->routing_policy_rule_messages
++;
841 routing_policy_rule_purge(link
->manager
, link
);
842 if (link
->routing_policy_rule_messages
== 0) {
843 link
->routing_policy_rules_configured
= true;
844 link_check_ready(link
);
846 log_link_debug(link
, "Setting routing policy rules");
851 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
855 assert(link
->route_messages
> 0);
856 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
857 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
859 link
->route_messages
--;
861 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
864 r
= sd_netlink_message_get_errno(m
);
865 if (r
< 0 && r
!= -EEXIST
)
866 log_link_warning_errno(link
, r
, "Could not set route: %m");
868 if (link
->route_messages
== 0) {
869 log_link_debug(link
, "Routes set");
870 link
->static_routes_configured
= true;
871 link_check_ready(link
);
877 int link_request_set_routes(Link
*link
) {
879 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
880 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
887 assert(link
->network
);
888 assert(link
->addresses_configured
);
889 assert(link
->address_messages
== 0);
890 assert(link
->state
!= _LINK_STATE_INVALID
);
892 link_set_state(link
, LINK_STATE_CONFIGURING
);
893 link
->static_routes_configured
= false;
895 r
= link_request_set_routing_policy_rule(link
);
899 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
900 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
901 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
903 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
906 r
= route_configure(rt
, link
, route_handler
);
908 log_link_warning_errno(link
, r
, "Could not set routes: %m");
909 link_enter_failed(link
);
913 link
->route_messages
++;
916 if (link
->route_messages
== 0) {
917 link
->static_routes_configured
= true;
918 link_check_ready(link
);
920 log_link_debug(link
, "Setting routes");
925 void link_check_ready(Link
*link
) {
931 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
937 if (!link
->addresses_configured
)
940 if (!link
->neighbors_configured
)
943 SET_FOREACH(a
, link
->addresses
, i
)
944 if (!address_is_ready(a
))
947 if (!link
->addresses_ready
) {
948 link
->addresses_ready
= true;
949 link_request_set_routes(link
);
952 if (!link
->static_routes_configured
)
955 if (!link
->routing_policy_rules_configured
)
958 if (link_ipv4ll_enabled(link
) && !(link
->ipv4ll_address
&& link
->ipv4ll_route
))
961 if (link_ipv6ll_enabled(link
) &&
962 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
965 if ((link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
)) &&
966 !(link
->dhcp4_configured
|| link
->dhcp6_configured
) &&
967 !(link_ipv4ll_fallback_enabled(link
) && link
->ipv4ll_address
&& link
->ipv4ll_route
))
968 /* When DHCP is enabled, at least one protocol must provide an address, or
969 * an IPv4ll fallback address must be configured. */
972 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
975 if (link
->state
!= LINK_STATE_CONFIGURED
)
976 link_enter_configured(link
);
981 static int link_request_set_neighbors(Link
*link
) {
986 assert(link
->network
);
987 assert(link
->state
!= _LINK_STATE_INVALID
);
989 link_set_state(link
, LINK_STATE_CONFIGURING
);
990 link
->neighbors_configured
= false;
992 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
993 r
= neighbor_configure(neighbor
, link
, NULL
);
995 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
996 link_enter_failed(link
);
1001 if (link
->neighbor_messages
== 0) {
1002 link
->neighbors_configured
= true;
1003 link_check_ready(link
);
1005 log_link_debug(link
, "Setting neighbors");
1010 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1016 assert(link
->ifname
);
1017 assert(link
->address_messages
> 0);
1018 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1019 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1021 link
->address_messages
--;
1023 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1026 r
= sd_netlink_message_get_errno(m
);
1027 if (r
< 0 && r
!= -EEXIST
)
1028 log_link_warning_errno(link
, r
, "could not set address: %m");
1030 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1032 if (link
->address_messages
== 0) {
1033 log_link_debug(link
, "Addresses set");
1034 link
->addresses_configured
= true;
1035 link_check_ready(link
);
1041 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1042 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1043 size_t n_addresses
= 0, n_allocated
= 0;
1046 log_debug("Copying DNS server information from %s", link
->ifname
);
1051 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1054 /* Only look for IPv4 addresses */
1055 if (link
->network
->dns
[i
].family
!= AF_INET
)
1058 ia
= link
->network
->dns
[i
].address
.in
;
1060 /* Never propagate obviously borked data */
1061 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1064 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1067 addresses
[n_addresses
++] = ia
;
1070 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1071 const struct in_addr
*da
= NULL
;
1074 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1077 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1080 for (j
= 0; j
< n
; j
++)
1081 if (in4_addr_is_non_local(&da
[j
]))
1082 addresses
[n_addresses
++] = da
[j
];
1086 if (n_addresses
<= 0)
1089 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1092 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1093 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1094 size_t n_addresses
= 0, n_allocated
= 0;
1100 log_debug("Copying NTP server information from %s", link
->ifname
);
1102 STRV_FOREACH(a
, link
->network
->ntp
) {
1103 union in_addr_union ia
;
1105 /* Only look for IPv4 addresses */
1106 if (in_addr_from_string(AF_INET
, *a
, &ia
) <= 0)
1109 /* Never propagate obviously borked data */
1110 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
1113 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1116 addresses
[n_addresses
++] = ia
.in
;
1119 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1120 const struct in_addr
*da
= NULL
;
1123 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1126 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1129 for (j
= 0; j
< n
; j
++)
1130 if (in4_addr_is_non_local(&da
[j
]))
1131 addresses
[n_addresses
++] = da
[j
];
1135 if (n_addresses
<= 0)
1138 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1141 static int link_set_bridge_fdb(Link
*link
) {
1142 FdbEntry
*fdb_entry
;
1145 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1146 r
= fdb_entry_configure(link
, fdb_entry
);
1148 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1154 static int link_request_set_addresses(Link
*link
) {
1155 AddressLabel
*label
;
1160 assert(link
->network
);
1161 assert(link
->state
!= _LINK_STATE_INVALID
);
1163 link_set_state(link
, LINK_STATE_CONFIGURING
);
1165 /* Reset all *_configured flags we are configuring. */
1166 link
->addresses_configured
= false;
1167 link
->addresses_ready
= false;
1168 link
->neighbors_configured
= false;
1169 link
->static_routes_configured
= false;
1170 link
->routing_policy_rules_configured
= false;
1172 r
= link_set_bridge_fdb(link
);
1176 r
= link_request_set_neighbors(link
);
1180 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1183 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1185 r
= address_configure(ad
, link
, address_handler
, update
);
1187 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1188 link_enter_failed(link
);
1192 link
->address_messages
++;
1195 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1196 r
= address_label_configure(label
, link
, NULL
, false);
1198 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1199 link_enter_failed(link
);
1203 link
->address_label_messages
++;
1206 /* now that we can figure out a default address for the dhcp server,
1208 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1210 Link
*uplink
= NULL
;
1211 bool acquired_uplink
= false;
1213 address
= link_find_dhcp_server_address(link
);
1215 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1216 link_enter_failed(link
);
1220 /* use the server address' subnet as the pool */
1221 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1222 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1227 r = sd_dhcp_server_set_router(link->dhcp_server,
1228 &main_address->in_addr.in);
1233 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1234 r
= sd_dhcp_server_set_max_lease_time(
1236 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1241 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1242 r
= sd_dhcp_server_set_default_lease_time(
1244 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1249 if (link
->network
->dhcp_server_emit_dns
) {
1251 if (link
->network
->n_dhcp_server_dns
> 0)
1252 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1254 uplink
= manager_find_uplink(link
->manager
, link
);
1255 acquired_uplink
= true;
1258 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1261 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1264 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1267 if (link
->network
->dhcp_server_emit_ntp
) {
1269 if (link
->network
->n_dhcp_server_ntp
> 0)
1270 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1272 if (!acquired_uplink
)
1273 uplink
= manager_find_uplink(link
->manager
, link
);
1276 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1279 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1283 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1286 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1288 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1290 if (link
->network
->dhcp_server_emit_timezone
) {
1291 _cleanup_free_
char *buffer
= NULL
;
1292 const char *tz
= NULL
;
1294 if (link
->network
->dhcp_server_timezone
)
1295 tz
= link
->network
->dhcp_server_timezone
;
1297 r
= get_timezone(&buffer
);
1299 log_warning_errno(r
, "Failed to determine timezone: %m");
1305 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1310 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1311 r
= sd_dhcp_server_start(link
->dhcp_server
);
1313 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1315 link_enter_failed(link
);
1321 log_link_debug(link
, "Offering DHCPv4 leases");
1324 if (link
->address_messages
== 0) {
1325 link
->addresses_configured
= true;
1326 link_check_ready(link
);
1328 log_link_debug(link
, "Setting addresses");
1333 static int link_set_bridge_vlan(Link
*link
) {
1336 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1338 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1343 static int link_set_proxy_arp(Link
*link
) {
1346 if (!link_proxy_arp_enabled(link
))
1349 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1351 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1356 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1361 log_link_debug(link
, "Set link");
1363 r
= sd_netlink_message_get_errno(m
);
1364 if (r
< 0 && r
!= -EEXIST
) {
1365 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1366 link_enter_failed(link
);
1372 static int link_configure_after_setting_mtu(Link
*link
);
1374 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1379 assert(link
->ifname
);
1381 link
->setting_mtu
= false;
1383 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1386 r
= sd_netlink_message_get_errno(m
);
1388 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1392 log_link_debug(link
, "Setting MTU done.");
1394 if (link
->state
== LINK_STATE_INITIALIZED
)
1395 (void) link_configure_after_setting_mtu(link
);
1400 int link_set_mtu(Link
*link
, uint32_t mtu
, bool force
) {
1401 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1405 assert(link
->manager
);
1406 assert(link
->manager
->rtnl
);
1408 if (mtu
== 0 || link
->setting_mtu
)
1411 if (force
? link
->mtu
== mtu
: link
->mtu
>= mtu
)
1414 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1416 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1418 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1420 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1421 * for this interface, then disable IPv6 else enable it. */
1422 (void) link_enable_ipv6(link
);
1424 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1425 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1426 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1428 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1429 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1434 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1436 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1438 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1439 link_netlink_destroy_callback
, link
);
1441 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1444 link
->setting_mtu
= true;
1449 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1454 assert(link
->ifname
);
1456 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1459 r
= sd_netlink_message_get_errno(m
);
1461 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1466 static int link_set_flags(Link
*link
) {
1467 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1468 unsigned ifi_change
= 0;
1469 unsigned ifi_flags
= 0;
1473 assert(link
->manager
);
1474 assert(link
->manager
->rtnl
);
1476 if (link
->flags
& IFF_LOOPBACK
)
1482 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1485 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1487 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1489 if (link
->network
->arp
>= 0) {
1490 ifi_change
|= IFF_NOARP
;
1491 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1494 if (link
->network
->multicast
>= 0) {
1495 ifi_change
|= IFF_MULTICAST
;
1496 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1499 if (link
->network
->allmulticast
>= 0) {
1500 ifi_change
|= IFF_ALLMULTI
;
1501 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1504 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1506 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1508 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1509 link_netlink_destroy_callback
, link
);
1511 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1518 static int link_set_bridge(Link
*link
) {
1519 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1523 assert(link
->network
);
1525 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1527 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1529 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1531 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1533 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1535 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1537 if (link
->network
->use_bpdu
>= 0) {
1538 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1540 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1543 if (link
->network
->hairpin
>= 0) {
1544 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1546 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1549 if (link
->network
->fast_leave
>= 0) {
1550 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1552 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1555 if (link
->network
->allow_port_to_be_root
>= 0) {
1556 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1558 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1561 if (link
->network
->unicast_flood
>= 0) {
1562 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1564 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1567 if (link
->network
->multicast_flood
>= 0) {
1568 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
1570 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
1573 if (link
->network
->multicast_to_unicast
>= 0) {
1574 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1576 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1579 if (link
->network
->neighbor_suppression
>= 0) {
1580 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
1582 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
1585 if (link
->network
->learning
>= 0) {
1586 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
1588 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
1591 if (link
->network
->bridge_proxy_arp
>= 0) {
1592 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP
, link
->network
->bridge_proxy_arp
);
1594 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP attribute: %m");
1597 if (link
->network
->bridge_proxy_arp_wifi
>= 0) {
1598 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP_WIFI
, link
->network
->bridge_proxy_arp_wifi
);
1600 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP_WIFI attribute: %m");
1603 if (link
->network
->cost
!= 0) {
1604 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1606 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1609 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1610 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1612 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1615 if (link
->network
->multicast_router
!= _MULTICAST_ROUTER_INVALID
) {
1616 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MULTICAST_ROUTER
, link
->network
->multicast_router
);
1618 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MULTICAST_ROUTER attribute: %m");
1621 r
= sd_netlink_message_close_container(req
);
1623 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1625 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1626 link_netlink_destroy_callback
, link
);
1628 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1635 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1640 assert(link
->ifname
);
1642 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1645 r
= sd_netlink_message_get_errno(m
);
1647 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1654 static int link_set_bond(Link
*link
) {
1655 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1659 assert(link
->network
);
1661 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1663 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1665 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1667 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1669 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1671 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1673 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1675 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1677 if (link
->network
->active_slave
) {
1678 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1680 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1683 if (link
->network
->primary_slave
) {
1684 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1686 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1689 r
= sd_netlink_message_close_container(req
);
1691 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1693 r
= sd_netlink_message_close_container(req
);
1695 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1697 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1698 link_netlink_destroy_callback
, link
);
1700 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1707 static int link_acquire_ipv6_conf(Link
*link
) {
1712 if (link_ipv6_accept_ra_enabled(link
)) {
1713 assert(link
->ndisc
);
1715 log_link_debug(link
, "Discovering IPv6 routers");
1717 r
= sd_ndisc_start(link
->ndisc
);
1718 if (r
< 0 && r
!= -EBUSY
)
1719 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1722 if (link_radv_enabled(link
)) {
1724 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1726 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1728 r
= sd_radv_start(link
->radv
);
1729 if (r
< 0 && r
!= -EBUSY
)
1730 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1733 (void) dhcp6_request_prefix_delegation(link
);
1738 static int link_acquire_ipv4_conf(Link
*link
) {
1742 assert(link
->manager
);
1743 assert(link
->manager
->event
);
1745 if (link_ipv4ll_enabled(link
)) {
1746 assert(link
->ipv4ll
);
1748 log_link_debug(link
, "Acquiring IPv4 link-local address");
1750 r
= sd_ipv4ll_start(link
->ipv4ll
);
1752 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1755 if (link_dhcp4_enabled(link
)) {
1756 assert(link
->dhcp_client
);
1758 log_link_debug(link
, "Acquiring DHCPv4 lease");
1760 r
= sd_dhcp_client_start(link
->dhcp_client
);
1762 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1768 static int link_acquire_conf(Link
*link
) {
1773 r
= link_acquire_ipv4_conf(link
);
1777 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1778 r
= link_acquire_ipv6_conf(link
);
1783 if (link_lldp_emit_enabled(link
)) {
1784 r
= link_lldp_emit_start(link
);
1786 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1792 bool link_has_carrier(Link
*link
) {
1793 /* see Documentation/networking/operstates.txt in the kernel sources */
1795 if (link
->kernel_operstate
== IF_OPER_UP
)
1798 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1799 /* operstate may not be implemented, so fall back to flags */
1800 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1806 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1811 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1814 r
= sd_netlink_message_get_errno(m
);
1816 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1821 static int link_configure_addrgen_mode(Link
*link
) {
1822 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1823 uint8_t ipv6ll_mode
;
1827 assert(link
->network
);
1828 assert(link
->manager
);
1829 assert(link
->manager
->rtnl
);
1831 log_link_debug(link
, "Setting address genmode for link");
1833 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1835 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1837 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1839 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1841 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1843 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1845 if (!link_ipv6ll_enabled(link
))
1846 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1847 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1848 /* The file may not exist. And event if it exists, when stable_secret is unset,
1849 * reading the file fails with EIO. */
1850 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1852 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1854 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1856 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1858 r
= sd_netlink_message_close_container(req
);
1860 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1862 r
= sd_netlink_message_close_container(req
);
1864 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1866 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1867 link_netlink_destroy_callback
, link
);
1869 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1876 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1881 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1884 r
= sd_netlink_message_get_errno(m
);
1886 /* we warn but don't fail the link, as it may be brought up later */
1887 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1892 static int link_up(Link
*link
) {
1893 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1897 assert(link
->network
);
1898 assert(link
->manager
);
1899 assert(link
->manager
->rtnl
);
1901 log_link_debug(link
, "Bringing link up");
1903 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1905 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1907 /* set it free if not enslaved with networkd */
1908 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1909 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1911 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1914 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1916 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1918 if (link
->network
->mac
) {
1919 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1921 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1924 if (link_ipv6_enabled(link
)) {
1925 uint8_t ipv6ll_mode
;
1927 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1929 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1931 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1932 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1934 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1936 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1937 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1939 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1942 if (!link_ipv6ll_enabled(link
))
1943 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1944 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1945 /* The file may not exist. And event if it exists, when stable_secret is unset,
1946 * reading the file fails with EIO. */
1947 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1949 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1951 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1953 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1955 r
= sd_netlink_message_close_container(req
);
1957 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1959 r
= sd_netlink_message_close_container(req
);
1961 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1964 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1965 link_netlink_destroy_callback
, link
);
1967 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1974 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1979 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1982 r
= sd_netlink_message_get_errno(m
);
1984 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1989 int link_down(Link
*link
, link_netlink_message_handler_t callback
) {
1990 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1994 assert(link
->manager
);
1995 assert(link
->manager
->rtnl
);
1997 log_link_debug(link
, "Bringing link down");
1999 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2000 RTM_SETLINK
, link
->ifindex
);
2002 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2004 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2006 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2008 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
2009 callback
?: link_down_handler
,
2010 link_netlink_destroy_callback
, link
);
2012 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2019 static int link_handle_bound_to_list(Link
*link
) {
2023 bool required_up
= false;
2024 bool link_is_up
= false;
2028 if (hashmap_isempty(link
->bound_to_links
))
2031 if (link
->flags
& IFF_UP
)
2034 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2035 if (link_has_carrier(l
)) {
2040 if (!required_up
&& link_is_up
) {
2041 r
= link_down(link
, NULL
);
2044 } else if (required_up
&& !link_is_up
) {
2053 static int link_handle_bound_by_list(Link
*link
) {
2060 if (hashmap_isempty(link
->bound_by_links
))
2063 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2064 r
= link_handle_bound_to_list(l
);
2072 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2078 if (link
== carrier
)
2081 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2084 r
= hashmap_ensure_allocated(h
, NULL
);
2088 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2095 static int link_new_bound_by_list(Link
*link
) {
2100 bool list_updated
= false;
2103 assert(link
->manager
);
2107 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2108 if (!carrier
->network
)
2111 if (strv_isempty(carrier
->network
->bind_carrier
))
2114 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2115 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2119 list_updated
= true;
2126 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2127 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2131 link_dirty(carrier
);
2137 static int link_new_bound_to_list(Link
*link
) {
2142 bool list_updated
= false;
2145 assert(link
->manager
);
2150 if (strv_isempty(link
->network
->bind_carrier
))
2155 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2156 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2157 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2161 list_updated
= true;
2168 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2169 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2173 link_dirty(carrier
);
2179 static int link_new_carrier_maps(Link
*link
) {
2182 r
= link_new_bound_by_list(link
);
2186 r
= link_handle_bound_by_list(link
);
2190 r
= link_new_bound_to_list(link
);
2194 r
= link_handle_bound_to_list(link
);
2201 static void link_free_bound_to_list(Link
*link
) {
2205 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2206 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2208 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2209 link_dirty(bound_to
);
2215 static void link_free_bound_by_list(Link
*link
) {
2219 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2220 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2222 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2223 link_dirty(bound_by
);
2224 link_handle_bound_to_list(bound_by
);
2231 static void link_free_carrier_maps(Link
*link
) {
2232 bool list_updated
= false;
2236 if (!hashmap_isempty(link
->bound_to_links
)) {
2237 link_free_bound_to_list(link
);
2238 list_updated
= true;
2241 if (!hashmap_isempty(link
->bound_by_links
)) {
2242 link_free_bound_by_list(link
);
2243 list_updated
= true;
2252 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
2259 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
2263 r
= set_ensure_allocated(&master
->slaves
, NULL
);
2267 r
= set_put(master
->slaves
, link
);
2275 static void link_drop_from_master(Link
*link
, NetDev
*netdev
) {
2280 if (!link
->manager
|| !netdev
)
2283 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
2286 link_unref(set_remove(master
->slaves
, link
));
2289 static void link_detach_from_manager(Link
*link
) {
2290 if (!link
|| !link
->manager
)
2293 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
2296 /* The following must be called at last. */
2297 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
2301 void link_drop(Link
*link
) {
2302 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2305 link_set_state(link
, LINK_STATE_LINGER
);
2307 link_free_carrier_maps(link
);
2309 if (link
->network
) {
2310 link_drop_from_master(link
, link
->network
->bridge
);
2311 link_drop_from_master(link
, link
->network
->bond
);
2314 log_link_debug(link
, "Link removed");
2316 (void) unlink(link
->state_file
);
2317 link_detach_from_manager(link
);
2320 static int link_joined(Link
*link
) {
2324 assert(link
->network
);
2326 if (!hashmap_isempty(link
->bound_to_links
)) {
2327 r
= link_handle_bound_to_list(link
);
2330 } else if (!(link
->flags
& IFF_UP
)) {
2333 link_enter_failed(link
);
2338 if (link
->network
->bridge
) {
2339 r
= link_set_bridge(link
);
2341 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2343 r
= link_append_to_master(link
, link
->network
->bridge
);
2345 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2348 if (link
->network
->bond
) {
2349 r
= link_set_bond(link
);
2351 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2353 r
= link_append_to_master(link
, link
->network
->bond
);
2355 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2358 if (link
->network
->use_br_vlan
&&
2359 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2360 r
= link_set_bridge_vlan(link
);
2362 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2365 /* Skip setting up addresses until it gets carrier,
2366 or it would try to set addresses twice,
2367 which is bad for non-idempotent steps. */
2368 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2371 return link_request_set_addresses(link
);
2374 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2378 assert(link
->network
);
2379 assert(link
->enslaving
> 0);
2380 assert(!link
->enslaved_raw
);
2384 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2387 r
= sd_netlink_message_get_errno(m
);
2388 if (r
< 0 && r
!= -EEXIST
) {
2389 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2390 link_enter_failed(link
);
2393 log_link_debug(link
, "Joined netdev");
2395 if (link
->enslaving
== 0) {
2396 link
->enslaved_raw
= true;
2403 static int link_enter_join_netdev(Link
*link
) {
2409 assert(link
->network
);
2410 assert(link
->state
== LINK_STATE_INITIALIZED
);
2412 link_set_state(link
, LINK_STATE_CONFIGURING
);
2415 link
->enslaving
= 0;
2416 link
->enslaved_raw
= false;
2418 if (link
->network
->bond
) {
2419 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2420 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2421 return link_joined(link
);
2423 log_struct(LOG_DEBUG
,
2424 LOG_LINK_INTERFACE(link
),
2425 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2426 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2430 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2432 log_struct_errno(LOG_WARNING
, r
,
2433 LOG_LINK_INTERFACE(link
),
2434 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2435 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2436 link_enter_failed(link
);
2441 if (link
->network
->bridge
) {
2442 log_struct(LOG_DEBUG
,
2443 LOG_LINK_INTERFACE(link
),
2444 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2445 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2449 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2451 log_struct_errno(LOG_WARNING
, r
,
2452 LOG_LINK_INTERFACE(link
),
2453 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2454 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2455 link_enter_failed(link
);
2460 if (link
->network
->vrf
) {
2461 log_struct(LOG_DEBUG
,
2462 LOG_LINK_INTERFACE(link
),
2463 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2464 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2468 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2470 log_struct_errno(LOG_WARNING
, r
,
2471 LOG_LINK_INTERFACE(link
),
2472 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2473 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2474 link_enter_failed(link
);
2479 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2481 if (netdev
->ifindex
> 0)
2482 /* Assume already enslaved. */
2485 if (netdev_get_create_type(netdev
) != NETDEV_CREATE_STACKED
)
2488 log_struct(LOG_DEBUG
,
2489 LOG_LINK_INTERFACE(link
),
2490 LOG_NETDEV_INTERFACE(netdev
),
2491 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2495 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2497 log_struct_errno(LOG_WARNING
, r
,
2498 LOG_LINK_INTERFACE(link
),
2499 LOG_NETDEV_INTERFACE(netdev
),
2500 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2501 link_enter_failed(link
);
2506 if (link
->enslaving
== 0)
2507 return link_joined(link
);
2512 static int link_set_ipv4_forward(Link
*link
) {
2515 if (!link_ipv4_forward_enabled(link
))
2518 /* We propagate the forwarding flag from one interface to the
2519 * global setting one way. This means: as long as at least one
2520 * interface was configured at any time that had IP forwarding
2521 * enabled the setting will stay on for good. We do this
2522 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2523 * somewhat in sync (see below). */
2525 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2527 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2532 static int link_set_ipv6_forward(Link
*link
) {
2535 if (!link_ipv6_forward_enabled(link
))
2538 /* On Linux, the IPv6 stack does not know a per-interface
2539 * packet forwarding setting: either packet forwarding is on
2540 * for all, or off for all. We hence don't bother with a
2541 * per-interface setting, but simply propagate the interface
2542 * flag, if it is set, to the global flag, one-way. Note that
2543 * while IPv4 would allow a per-interface flag, we expose the
2544 * same behaviour there and also propagate the setting from
2545 * one to all, to keep things simple (see above). */
2547 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2549 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2554 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2555 IPv6PrivacyExtensions s
;
2558 s
= link_ipv6_privacy_extensions(link
);
2562 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2564 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2569 static int link_set_ipv6_accept_ra(Link
*link
) {
2572 /* Make this a NOP if IPv6 is not available */
2573 if (!socket_ipv6_is_supported())
2576 if (link
->flags
& IFF_LOOPBACK
)
2582 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2584 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2589 static int link_set_ipv6_dad_transmits(Link
*link
) {
2592 /* Make this a NOP if IPv6 is not available */
2593 if (!socket_ipv6_is_supported())
2596 if (link
->flags
& IFF_LOOPBACK
)
2602 if (link
->network
->ipv6_dad_transmits
< 0)
2605 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2607 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2612 static int link_set_ipv6_hop_limit(Link
*link
) {
2615 /* Make this a NOP if IPv6 is not available */
2616 if (!socket_ipv6_is_supported())
2619 if (link
->flags
& IFF_LOOPBACK
)
2625 if (link
->network
->ipv6_hop_limit
< 0)
2628 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2630 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2635 static int link_set_ipv6_mtu(Link
*link
) {
2638 /* Make this a NOP if IPv6 is not available */
2639 if (!socket_ipv6_is_supported())
2642 if (link
->flags
& IFF_LOOPBACK
)
2645 if (link
->network
->ipv6_mtu
== 0)
2648 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2650 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2655 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2656 Address
*net_address
;
2664 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2665 if (address_equal(net_address
, address
))
2671 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2680 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2681 if (route_equal(net_route
, route
))
2687 static int link_drop_foreign_config(Link
*link
) {
2693 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2694 /* we consider IPv6LL addresses to be managed by the kernel */
2695 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2698 if (link_is_static_address_configured(link
, address
)) {
2699 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2701 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2703 r
= address_remove(address
, link
, NULL
);
2709 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2710 /* do not touch routes managed by the kernel */
2711 if (route
->protocol
== RTPROT_KERNEL
)
2714 if (link_is_static_route_configured(link
, route
)) {
2715 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2719 r
= route_remove(route
, link
, NULL
);
2728 static int link_drop_config(Link
*link
) {
2729 Address
*address
, *pool_address
;
2734 SET_FOREACH(address
, link
->addresses
, i
) {
2735 /* we consider IPv6LL addresses to be managed by the kernel */
2736 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2739 r
= address_remove(address
, link
, NULL
);
2743 /* If this address came from an address pool, clean up the pool */
2744 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2745 if (address_equal(address
, pool_address
)) {
2746 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2747 address_free(pool_address
);
2753 SET_FOREACH(route
, link
->routes
, i
) {
2754 /* do not touch routes managed by the kernel */
2755 if (route
->protocol
== RTPROT_KERNEL
)
2758 r
= route_remove(route
, link
, NULL
);
2768 static int link_configure(Link
*link
) {
2772 assert(link
->network
);
2773 assert(link
->state
== LINK_STATE_INITIALIZED
);
2775 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2776 return link_configure_can(link
);
2778 /* Drop foreign config, but ignore loopback or critical devices.
2779 * We do not want to remove loopback address or addresses used for root NFS. */
2780 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2781 r
= link_drop_foreign_config(link
);
2786 r
= link_set_proxy_arp(link
);
2790 r
= ipv6_proxy_ndp_addresses_configure(link
);
2794 r
= link_set_ipv4_forward(link
);
2798 r
= link_set_ipv6_forward(link
);
2802 r
= link_set_ipv6_privacy_extensions(link
);
2806 r
= link_set_ipv6_accept_ra(link
);
2810 r
= link_set_ipv6_dad_transmits(link
);
2814 r
= link_set_ipv6_hop_limit(link
);
2818 r
= link_set_flags(link
);
2822 r
= link_set_ipv6_mtu(link
);
2826 if (link_ipv4ll_enabled(link
) || link_ipv4ll_fallback_enabled(link
)) {
2827 r
= ipv4ll_configure(link
);
2832 if (link_dhcp4_enabled(link
)) {
2833 r
= dhcp4_set_promote_secondaries(link
);
2837 r
= dhcp4_configure(link
);
2842 if (link_dhcp4_server_enabled(link
)) {
2843 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2847 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2852 if (link_dhcp6_enabled(link
) ||
2853 link_ipv6_accept_ra_enabled(link
)) {
2854 r
= dhcp6_configure(link
);
2859 if (link_ipv6_accept_ra_enabled(link
)) {
2860 r
= ndisc_configure(link
);
2865 if (link_radv_enabled(link
)) {
2866 r
= radv_configure(link
);
2871 if (link_lldp_rx_enabled(link
)) {
2872 r
= link_lldp_rx_configure(link
);
2877 r
= link_set_mtu(link
, link
->network
->mtu
, link
->network
->mtu_is_set
);
2881 if (socket_ipv6_is_supported()) {
2882 r
= link_configure_addrgen_mode(link
);
2887 return link_configure_after_setting_mtu(link
);
2890 static int link_configure_after_setting_mtu(Link
*link
) {
2894 assert(link
->network
);
2895 assert(link
->state
== LINK_STATE_INITIALIZED
);
2897 if (link
->setting_mtu
)
2900 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2901 r
= link_acquire_conf(link
);
2906 return link_enter_join_netdev(link
);
2909 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2912 if (duid
->raw_data_len
> 0)
2915 if (duid
->type
!= DUID_TYPE_UUID
)
2918 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2919 duid
->raw_data_len
= sizeof(sd_id128_t
);
2924 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2925 Manager
*manager
= userdata
;
2926 const sd_bus_error
*e
;
2936 e
= sd_bus_message_get_error(m
);
2938 log_error_errno(sd_bus_error_get_errno(e
),
2939 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2944 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2948 if (sz
!= sizeof(sd_id128_t
)) {
2949 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2953 memcpy(&manager
->product_uuid
, a
, sz
);
2954 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2955 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2957 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2960 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2961 r
= link_configure(link
);
2963 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2966 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2968 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2969 * even if the method fails. */
2970 manager
->has_product_uuid
= true;
2975 static bool link_requires_uuid(Link
*link
) {
2979 assert(link
->manager
);
2980 assert(link
->network
);
2982 duid
= link_get_duid(link
);
2983 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2986 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2989 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2995 static int link_configure_duid(Link
*link
) {
3001 assert(link
->manager
);
3002 assert(link
->network
);
3005 duid
= link_get_duid(link
);
3007 if (!link_requires_uuid(link
))
3010 if (m
->has_product_uuid
) {
3011 (void) duid_set_uuid(duid
, m
->product_uuid
);
3015 if (!m
->links_requesting_uuid
) {
3016 r
= manager_request_product_uuid(m
, link
);
3021 log_link_warning_errno(link
, r
,
3022 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3026 r
= set_put(m
->links_requesting_uuid
, link
);
3030 r
= set_put(m
->duids_requesting_uuid
, duid
);
3038 static int link_initialized_and_synced(Link
*link
) {
3043 assert(link
->ifname
);
3044 assert(link
->manager
);
3046 /* We may get called either from the asynchronous netlink callback,
3047 * or directly for link_add() if running in a container. See link_add(). */
3048 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
3051 log_link_debug(link
, "Link state is up-to-date");
3052 link_set_state(link
, LINK_STATE_INITIALIZED
);
3054 r
= link_new_bound_by_list(link
);
3058 r
= link_handle_bound_by_list(link
);
3062 if (!link
->network
) {
3063 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3064 &link
->mac
, &network
);
3066 link_enter_unmanaged(link
);
3068 } else if (r
== 0 && network
->unmanaged
) {
3069 link_enter_unmanaged(link
);
3074 if (link
->flags
& IFF_LOOPBACK
) {
3075 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3076 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3078 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3079 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3081 if (network
->dhcp_server
)
3082 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3085 r
= network_apply(network
, link
);
3090 r
= link_new_bound_to_list(link
);
3094 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3095 * link_configure() is called later asynchronously. */
3096 r
= link_configure_duid(link
);
3100 r
= link_configure(link
);
3107 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3108 (void) link_initialized_and_synced(link
);
3112 int link_initialized(Link
*link
, sd_device
*device
) {
3113 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3117 assert(link
->manager
);
3118 assert(link
->manager
->rtnl
);
3121 if (link
->state
!= LINK_STATE_PENDING
)
3124 if (link
->sd_device
)
3127 log_link_debug(link
, "udev initialized link");
3128 link_set_state(link
, LINK_STATE_INITIALIZED
);
3130 link
->sd_device
= sd_device_ref(device
);
3132 /* udev has initialized the link, but we don't know if we have yet
3133 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3134 * when it returns we know that the pending NEWLINKs have already been
3135 * processed and that we are up-to-date */
3137 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3142 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3143 link_netlink_destroy_callback
, link
);
3152 static int link_load(Link
*link
) {
3153 _cleanup_free_
char *network_file
= NULL
,
3156 *dhcp4_address
= NULL
,
3157 *ipv4ll_address
= NULL
;
3158 union in_addr_union address
;
3159 union in_addr_union route_dst
;
3165 r
= parse_env_file(NULL
, link
->state_file
,
3166 "NETWORK_FILE", &network_file
,
3167 "ADDRESSES", &addresses
,
3169 "DHCP4_ADDRESS", &dhcp4_address
,
3170 "IPV4LL_ADDRESS", &ipv4ll_address
);
3171 if (r
< 0 && r
!= -ENOENT
)
3172 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3179 suffix
= strrchr(network_file
, '.');
3181 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3182 goto network_file_fail
;
3186 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3188 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3189 goto network_file_fail
;
3192 r
= network_apply(network
, link
);
3194 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3203 _cleanup_free_
char *address_str
= NULL
;
3204 char *prefixlen_str
;
3206 unsigned char prefixlen
;
3208 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3210 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3216 prefixlen_str
= strchr(address_str
, '/');
3217 if (!prefixlen_str
) {
3218 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3222 *prefixlen_str
++ = '\0';
3224 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3226 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3230 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3232 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3236 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3238 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3247 _cleanup_free_
char *route_str
= NULL
;
3248 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3250 char *prefixlen_str
;
3252 unsigned char prefixlen
, tos
, table
;
3255 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3257 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3263 prefixlen_str
= strchr(route_str
, '/');
3264 if (!prefixlen_str
) {
3265 log_link_debug(link
, "Failed to parse route %s", route_str
);
3269 *prefixlen_str
++ = '\0';
3271 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3273 log_link_debug(link
,
3274 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3279 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3281 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3285 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3287 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3289 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3290 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3291 0, route_expire_handler
, route
);
3293 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3296 route
->lifetime
= lifetime
;
3297 sd_event_source_unref(route
->expire
);
3298 route
->expire
= TAKE_PTR(expire
);
3302 if (dhcp4_address
) {
3303 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3305 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3306 goto dhcp4_address_fail
;
3309 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3311 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3313 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3315 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3320 if (ipv4ll_address
) {
3321 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3323 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3324 goto ipv4ll_address_fail
;
3327 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3329 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3331 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3333 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3336 ipv4ll_address_fail
:
3341 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3342 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3343 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3352 r
= link_new(m
, message
, ret
);
3358 log_link_debug(link
, "Link %d added", link
->ifindex
);
3360 r
= link_load(link
);
3364 if (detect_container() <= 0) {
3365 /* not in a container, udev will be around */
3366 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3367 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3369 log_link_warning_errno(link
, r
, "Could not find device: %m");
3373 r
= sd_device_get_is_initialized(device
);
3375 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3380 log_link_debug(link
, "link pending udev initialization...");
3384 r
= device_is_renaming(device
);
3386 log_link_warning_errno(link
, r
, "Failed to determine the device is renamed or not: %m");
3390 log_link_debug(link
, "Interface is under renaming, pending initialization.");
3394 r
= link_initialized(link
, device
);
3398 r
= link_initialized_and_synced(link
);
3405 link_enter_failed(link
);
3409 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3414 log_link_info(link
, "Gained IPv6LL");
3416 link
->ipv6ll_address
= *address
;
3417 link_check_ready(link
);
3419 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3420 r
= link_acquire_ipv6_conf(link
);
3422 link_enter_failed(link
);
3430 static int link_carrier_gained(Link
*link
) {
3435 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
3436 r
= link_acquire_conf(link
);
3438 link_enter_failed(link
);
3442 r
= link_request_set_addresses(link
);
3447 r
= link_handle_bound_by_list(link
);
3454 static int link_carrier_lost(Link
*link
) {
3459 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3462 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3463 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3464 * configuration and stop the clients as well. */
3465 if (link
->setting_mtu
)
3468 r
= link_stop_clients(link
);
3470 link_enter_failed(link
);
3474 if (link_dhcp4_server_enabled(link
))
3475 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3477 r
= link_drop_config(link
);
3481 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3482 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3483 r
= link_drop_foreign_config(link
);
3488 r
= link_handle_bound_by_list(link
);
3495 int link_carrier_reset(Link
*link
) {
3500 if (link_has_carrier(link
)) {
3501 r
= link_carrier_lost(link
);
3505 r
= link_carrier_gained(link
);
3509 log_link_info(link
, "Reset carrier");
3515 int link_update(Link
*link
, sd_netlink_message
*m
) {
3516 struct ether_addr mac
;
3519 bool had_carrier
, carrier_gained
, carrier_lost
;
3523 assert(link
->ifname
);
3526 if (link
->state
== LINK_STATE_LINGER
) {
3527 log_link_info(link
, "Link re-added");
3528 link_set_state(link
, LINK_STATE_CONFIGURING
);
3530 r
= link_new_carrier_maps(link
);
3535 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3536 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3537 Manager
*manager
= link
->manager
;
3539 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3542 r
= link_add(manager
, m
, &link
);
3547 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3548 if (r
>= 0 && mtu
> 0) {
3550 if (link
->original_mtu
== 0) {
3551 link
->original_mtu
= mtu
;
3552 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3555 if (link
->dhcp_client
) {
3556 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3559 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3563 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3565 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3569 /* The kernel may broadcast NEWLINK messages without the MAC address
3570 set, simply ignore them. */
3571 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3573 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3576 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3579 log_link_debug(link
, "MAC address: "
3580 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3581 mac
.ether_addr_octet
[0],
3582 mac
.ether_addr_octet
[1],
3583 mac
.ether_addr_octet
[2],
3584 mac
.ether_addr_octet
[3],
3585 mac
.ether_addr_octet
[4],
3586 mac
.ether_addr_octet
[5]);
3589 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3591 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3594 if (link
->dhcp_client
) {
3595 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3596 (const uint8_t *) &link
->mac
,
3600 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3602 r
= dhcp4_set_client_identifier(link
);
3607 if (link
->dhcp6_client
) {
3608 const DUID
* duid
= link_get_duid(link
);
3610 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3611 (const uint8_t *) &link
->mac
,
3615 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3617 if (link
->network
->iaid_set
) {
3618 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3619 link
->network
->iaid
);
3621 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3624 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3626 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3627 duid
->raw_data_len
);
3629 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3633 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3635 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3639 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3641 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3646 had_carrier
= link_has_carrier(link
);
3648 r
= link_update_flags(link
, m
);
3652 r
= link_update_lldp(link
);
3656 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3657 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3659 if (carrier_gained
) {
3660 log_link_info(link
, "Gained carrier");
3662 r
= link_carrier_gained(link
);
3665 } else if (carrier_lost
) {
3666 log_link_info(link
, "Lost carrier");
3668 r
= link_carrier_lost(link
);
3676 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3684 if (hashmap_isempty(h
))
3688 HASHMAP_FOREACH(link
, h
, i
) {
3692 fprintf(f
, "%i", link
->ifindex
);
3699 int link_save(Link
*link
) {
3700 _cleanup_free_
char *temp_path
= NULL
;
3701 _cleanup_fclose_
FILE *f
= NULL
;
3702 const char *admin_state
, *oper_state
;
3709 assert(link
->state_file
);
3710 assert(link
->lease_file
);
3711 assert(link
->manager
);
3713 if (link
->state
== LINK_STATE_LINGER
) {
3714 (void) unlink(link
->state_file
);
3718 link_lldp_save(link
);
3720 admin_state
= link_state_to_string(link
->state
);
3721 assert(admin_state
);
3723 oper_state
= link_operstate_to_string(link
->operstate
);
3726 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3730 (void) fchmod(fileno(f
), 0644);
3733 "# This is private data. Do not parse.\n"
3736 admin_state
, oper_state
);
3738 if (link
->network
) {
3740 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3741 const char *dhcp_domainname
= NULL
;
3742 char **dhcp6_domains
= NULL
;
3743 char **dhcp_domains
= NULL
;
3746 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3747 yes_no(link
->network
->required_for_online
));
3749 fprintf(f
, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n",
3750 strempty(link_operstate_to_string(link
->network
->required_operstate_for_online
)));
3752 if (link
->dhcp6_client
) {
3753 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3754 if (r
< 0 && r
!= -ENOMSG
)
3755 log_link_debug(link
, "No DHCPv6 lease");
3758 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3763 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3764 _cleanup_free_
char *b
= NULL
;
3766 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3767 &link
->network
->dns
[j
].address
, &b
);
3769 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3779 if (link
->network
->dhcp_use_dns
&&
3781 const struct in_addr
*addresses
;
3783 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3785 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3789 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3790 struct in6_addr
*in6_addrs
;
3792 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3796 serialize_in6_addrs(f
, in6_addrs
, r
);
3801 /* Make sure to flush out old entries before we use the NDISC data */
3804 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3807 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3811 serialize_in6_addrs(f
, &dd
->address
, 1);
3820 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3822 if (link
->network
->dhcp_use_ntp
&&
3824 const struct in_addr
*addresses
;
3826 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3828 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
3832 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3833 struct in6_addr
*in6_addrs
;
3836 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3841 serialize_in6_addrs(f
, in6_addrs
, r
);
3845 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3847 fputstrv(f
, hosts
, NULL
, &space
);
3852 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3853 if (link
->dhcp_lease
) {
3854 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3855 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3858 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3861 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
3863 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3866 if (dhcp_domainname
)
3867 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3869 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3871 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3873 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3874 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3879 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
3881 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3884 if (dhcp_domainname
)
3885 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3887 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3889 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3891 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3892 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3897 fprintf(f
, "LLMNR=%s\n",
3898 resolve_support_to_string(link
->network
->llmnr
));
3899 fprintf(f
, "MDNS=%s\n",
3900 resolve_support_to_string(link
->network
->mdns
));
3901 if (link
->network
->dns_default_route
>= 0)
3902 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3904 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3905 fprintf(f
, "DNS_OVER_TLS=%s\n",
3906 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3908 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3909 fprintf(f
, "DNSSEC=%s\n",
3910 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3912 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3915 fputs("DNSSEC_NTA=", f
);
3917 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3918 fputs_with_space(f
, n
, NULL
, &space
);
3922 fputs("ADDRESSES=", f
);
3924 SET_FOREACH(a
, link
->addresses
, i
) {
3925 _cleanup_free_
char *address_str
= NULL
;
3927 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3931 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3936 fputs("ROUTES=", f
);
3938 SET_FOREACH(route
, link
->routes
, i
) {
3939 _cleanup_free_
char *route_str
= NULL
;
3941 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3945 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3946 space
? " " : "", route_str
,
3947 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3954 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3955 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3957 if (link
->dhcp_lease
) {
3958 struct in_addr address
;
3959 const char *tz
= NULL
;
3961 assert(link
->network
);
3963 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3965 fprintf(f
, "TIMEZONE=%s\n", tz
);
3967 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3969 fputs("DHCP4_ADDRESS=", f
);
3970 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3974 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3982 (void) unlink(link
->lease_file
);
3985 struct in_addr address
;
3987 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3989 fputs("IPV4LL_ADDRESS=", f
);
3990 serialize_in_addrs(f
, &address
, 1, false, NULL
);
3995 r
= fflush_and_check(f
);
3999 if (rename(temp_path
, link
->state_file
) < 0) {
4007 (void) unlink(link
->state_file
);
4009 (void) unlink(temp_path
);
4011 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4014 /* The serialized state in /run is no longer up-to-date. */
4015 void link_dirty(Link
*link
) {
4020 /* mark manager dirty as link is dirty */
4021 manager_dirty(link
->manager
);
4023 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4025 /* allocation errors are ignored */
4028 r
= set_put(link
->manager
->dirty_links
, link
);
4030 /* don't take another ref if the link was already dirty */
4036 /* The serialized state in /run is up-to-date */
4037 void link_clean(Link
*link
) {
4039 assert(link
->manager
);
4041 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4044 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4045 [LINK_STATE_PENDING
] = "pending",
4046 [LINK_STATE_INITIALIZED
] = "initialized",
4047 [LINK_STATE_CONFIGURING
] = "configuring",
4048 [LINK_STATE_CONFIGURED
] = "configured",
4049 [LINK_STATE_UNMANAGED
] = "unmanaged",
4050 [LINK_STATE_FAILED
] = "failed",
4051 [LINK_STATE_LINGER
] = "linger",
4054 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);