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"
15 #include "netlink-util.h"
16 #include "network-internal.h"
17 #include "networkd-ipv6-proxy-ndp.h"
18 #include "networkd-lldp-tx.h"
19 #include "networkd-manager.h"
20 #include "networkd-ndisc.h"
21 #include "networkd-radv.h"
22 #include "networkd-routing-policy-rule.h"
24 #include "socket-util.h"
25 #include "stdio-util.h"
26 #include "string-table.h"
31 DUID
* link_get_duid(Link
*link
) {
32 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
33 return &link
->network
->duid
;
35 return &link
->manager
->duid
;
38 static bool link_dhcp6_enabled(Link
*link
) {
41 if (!socket_ipv6_is_supported())
44 if (link
->flags
& IFF_LOOPBACK
)
50 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
53 static bool link_dhcp4_enabled(Link
*link
) {
56 if (link
->flags
& IFF_LOOPBACK
)
62 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
65 static bool link_dhcp4_server_enabled(Link
*link
) {
68 if (link
->flags
& IFF_LOOPBACK
)
74 return link
->network
->dhcp_server
;
77 static bool link_ipv4ll_enabled(Link
*link
) {
80 if (link
->flags
& IFF_LOOPBACK
)
86 if (streq_ptr(link
->kind
, "wireguard"))
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 static bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 if (streq_ptr(link
->kind
, "wireguard"))
107 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
110 static bool link_ipv6_enabled(Link
*link
) {
113 if (!socket_ipv6_is_supported())
116 if (link
->network
->bridge
)
119 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
120 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
123 static bool link_radv_enabled(Link
*link
) {
126 if (!link_ipv6ll_enabled(link
))
129 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
132 static bool link_lldp_rx_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
138 if (link
->iftype
!= ARPHRD_ETHER
)
144 /* LLDP should be handled on bridge slaves as those have a direct
145 * connection to their peers not on the bridge master. Linux doesn't
146 * even (by default) forward lldp packets to the bridge master.*/
147 if (streq_ptr("bridge", link
->kind
))
150 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
153 static bool link_lldp_emit_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
159 if (link
->iftype
!= ARPHRD_ETHER
)
165 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
168 static bool link_ipv4_forward_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
180 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
183 static bool link_ipv6_forward_enabled(Link
*link
) {
186 if (!socket_ipv6_is_supported())
189 if (link
->flags
& IFF_LOOPBACK
)
195 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
198 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
201 static bool link_proxy_arp_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
210 if (link
->network
->proxy_arp
< 0)
216 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
219 if (!socket_ipv6_is_supported())
222 if (link
->flags
& IFF_LOOPBACK
)
228 if (!link_ipv6ll_enabled(link
))
231 /* If unset use system default (enabled if local forwarding is disabled.
232 * disabled if local forwarding is enabled).
233 * If set, ignore or enforce RA independent of local forwarding state.
235 if (link
->network
->ipv6_accept_ra
< 0)
236 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
237 return !link_ipv6_forward_enabled(link
);
238 else if (link
->network
->ipv6_accept_ra
> 0)
239 /* accept RA even if ip_forward is enabled */
246 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
249 if (!socket_ipv6_is_supported())
250 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
252 if (link
->flags
& IFF_LOOPBACK
)
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
256 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
258 return link
->network
->ipv6_privacy_extensions
;
261 static int link_enable_ipv6(Link
*link
) {
262 const char *p
= NULL
;
266 if (link
->flags
& IFF_LOOPBACK
)
269 disabled
= !link_ipv6_enabled(link
);
271 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
273 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
275 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
276 enable_disable(!disabled
), link
->ifname
);
278 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
283 void link_update_operstate(Link
*link
) {
284 LinkOperationalState operstate
;
287 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
288 operstate
= LINK_OPERSTATE_DORMANT
;
289 else if (link_has_carrier(link
)) {
291 uint8_t scope
= RT_SCOPE_NOWHERE
;
294 /* if we have carrier, check what addresses we have */
295 SET_FOREACH(address
, link
->addresses
, i
) {
296 if (!address_is_ready(address
))
299 if (address
->scope
< scope
)
300 scope
= address
->scope
;
303 /* for operstate we also take foreign addresses into account */
304 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
305 if (!address_is_ready(address
))
308 if (address
->scope
< scope
)
309 scope
= address
->scope
;
312 if (scope
< RT_SCOPE_SITE
)
313 /* universally accessible addresses found */
314 operstate
= LINK_OPERSTATE_ROUTABLE
;
315 else if (scope
< RT_SCOPE_HOST
)
316 /* only link or site local addresses found */
317 operstate
= LINK_OPERSTATE_DEGRADED
;
319 /* no useful addresses found */
320 operstate
= LINK_OPERSTATE_CARRIER
;
321 } else if (link
->flags
& IFF_UP
)
322 operstate
= LINK_OPERSTATE_NO_CARRIER
;
324 operstate
= LINK_OPERSTATE_OFF
;
326 if (link
->operstate
!= operstate
) {
327 link
->operstate
= operstate
;
328 link_send_changed(link
, "OperationalState", NULL
);
333 #define FLAG_STRING(string, flag, old, new) \
334 (((old ^ new) & flag) \
335 ? ((old & flag) ? (" -" string) : (" +" string)) \
338 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
339 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
345 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
347 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
349 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
351 /* if we got a message without operstate, take it to mean
352 the state was unchanged */
353 operstate
= link
->kernel_operstate
;
355 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
358 if (link
->flags
!= flags
) {
359 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",
360 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
361 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
362 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
363 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
364 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
365 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
366 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
367 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
368 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
369 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
370 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
371 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
372 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
373 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
374 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
375 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
376 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
377 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
378 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
380 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
381 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
382 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
383 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
384 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
385 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
386 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
387 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
389 /* link flags are currently at most 18 bits, let's align to
391 if (unknown_flags_added
)
393 "Unknown link flags gained: %#.5x (ignoring)",
394 unknown_flags_added
);
396 if (unknown_flags_removed
)
398 "Unknown link flags lost: %#.5x (ignoring)",
399 unknown_flags_removed
);
403 link
->kernel_operstate
= operstate
;
405 link_update_operstate(link
);
410 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
412 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
413 _cleanup_(link_unrefp
) Link
*link
= NULL
;
415 const char *ifname
, *kind
= NULL
;
417 unsigned short iftype
;
423 /* check for link kind */
424 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
426 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
427 r
= sd_netlink_message_exit_container(message
);
432 r
= sd_netlink_message_get_type(message
, &type
);
435 else if (type
!= RTM_NEWLINK
)
438 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
441 else if (ifindex
<= 0)
444 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
448 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
452 link
= new0(Link
, 1);
457 link
->manager
= manager
;
458 link
->state
= LINK_STATE_PENDING
;
459 link
->rtnl_extended_attrs
= true;
460 link
->ifindex
= ifindex
;
461 link
->iftype
= iftype
;
462 link
->ifname
= strdup(ifname
);
467 link
->kind
= strdup(kind
);
472 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
474 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
476 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
478 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
480 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
483 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
486 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
489 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
493 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
497 r
= link_update_flags(link
, message
);
501 *ret
= TAKE_PTR(link
);
506 static Link
*link_free(Link
*link
) {
514 while ((route
= set_first(link
->routes
)))
517 while ((route
= set_first(link
->routes_foreign
)))
520 link
->routes
= set_free(link
->routes
);
521 link
->routes_foreign
= set_free(link
->routes_foreign
);
523 while ((address
= set_first(link
->addresses
)))
524 address_free(address
);
526 while ((address
= set_first(link
->addresses_foreign
)))
527 address_free(address
);
529 link
->addresses
= set_free(link
->addresses
);
530 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
532 while ((address
= link
->pool_addresses
)) {
533 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
534 address_free(address
);
537 sd_dhcp_server_unref(link
->dhcp_server
);
538 sd_dhcp_client_unref(link
->dhcp_client
);
539 sd_dhcp_lease_unref(link
->dhcp_lease
);
541 link_lldp_emit_stop(link
);
543 free(link
->lease_file
);
545 sd_lldp_unref(link
->lldp
);
546 free(link
->lldp_file
);
550 sd_ipv4ll_unref(link
->ipv4ll
);
551 sd_dhcp6_client_unref(link
->dhcp6_client
);
552 sd_ndisc_unref(link
->ndisc
);
553 sd_radv_unref(link
->radv
);
556 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
557 set_remove(link
->manager
->links_requesting_uuid
, link
);
565 (void) unlink(link
->state_file
);
566 free(link
->state_file
);
568 sd_device_unref(link
->sd_device
);
570 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
571 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
572 hashmap_free(link
->bound_to_links
);
574 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
575 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
576 hashmap_free(link
->bound_by_links
);
581 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
583 void link_netlink_destroy_callback(void *userdata
) {
584 Link
*link
= userdata
;
591 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
598 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
607 static void link_set_state(Link
*link
, LinkState state
) {
610 if (link
->state
== state
)
615 link_send_changed(link
, "AdministrativeState", NULL
);
618 static void link_enter_unmanaged(Link
*link
) {
621 log_link_debug(link
, "Unmanaged");
623 link_set_state(link
, LINK_STATE_UNMANAGED
);
628 static int link_stop_clients(Link
*link
) {
632 assert(link
->manager
);
633 assert(link
->manager
->event
);
635 if (link
->dhcp_client
) {
636 k
= sd_dhcp_client_stop(link
->dhcp_client
);
638 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
642 k
= sd_ipv4ll_stop(link
->ipv4ll
);
644 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
647 if (link
->dhcp6_client
) {
648 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
650 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
654 k
= sd_ndisc_stop(link
->ndisc
);
656 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
660 k
= sd_radv_stop(link
->radv
);
662 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
665 link_lldp_emit_stop(link
);
669 void link_enter_failed(Link
*link
) {
672 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
675 log_link_warning(link
, "Failed");
677 link_set_state(link
, LINK_STATE_FAILED
);
679 link_stop_clients(link
);
684 static Address
* link_find_dhcp_server_address(Link
*link
) {
688 assert(link
->network
);
690 /* The first statically configured address if there is any */
691 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
693 if (address
->family
!= AF_INET
)
696 if (in_addr_is_null(address
->family
, &address
->in_addr
))
702 /* If that didn't work, find a suitable address we got from the pool */
703 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
704 if (address
->family
!= AF_INET
)
713 static void link_enter_configured(Link
*link
) {
715 assert(link
->network
);
717 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
720 log_link_info(link
, "Configured");
722 link_set_state(link
, LINK_STATE_CONFIGURED
);
727 void link_check_ready(Link
*link
) {
733 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
739 if (!link
->static_routes_configured
)
742 if (!link
->routing_policy_rules_configured
)
745 if (link_ipv4ll_enabled(link
))
746 if (!link
->ipv4ll_address
||
750 if (!link
->network
->bridge
) {
752 if (link_ipv6ll_enabled(link
))
753 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
756 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
757 !link
->dhcp4_configured
) ||
758 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
759 !link
->dhcp6_configured
) ||
760 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
761 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
764 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
768 SET_FOREACH(a
, link
->addresses
, i
)
769 if (!address_is_ready(a
))
772 if (link
->state
!= LINK_STATE_CONFIGURED
)
773 link_enter_configured(link
);
778 static int link_set_routing_policy_rule(Link
*link
) {
779 RoutingPolicyRule
*rule
, *rrule
= NULL
;
783 assert(link
->network
);
785 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
786 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
787 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
789 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
793 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
795 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
796 link_enter_failed(link
);
800 link
->routing_policy_rule_messages
++;
803 routing_policy_rule_purge(link
->manager
, link
);
804 if (link
->routing_policy_rule_messages
== 0) {
805 link
->routing_policy_rules_configured
= true;
806 link_check_ready(link
);
808 log_link_debug(link
, "Setting routing policy rules");
813 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
814 Link
*link
= userdata
;
818 assert(link
->route_messages
> 0);
819 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
820 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
823 link
->route_messages
--;
825 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
828 r
= sd_netlink_message_get_errno(m
);
829 if (r
< 0 && r
!= -EEXIST
)
830 log_link_warning_errno(link
, r
, "Could not set route: %m");
832 if (link
->route_messages
== 0) {
833 log_link_debug(link
, "Routes set");
834 link
->static_routes_configured
= true;
835 link_check_ready(link
);
841 static int link_enter_set_routes(Link
*link
) {
846 assert(link
->network
);
847 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
849 (void) link_set_routing_policy_rule(link
);
851 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
853 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
854 r
= route_configure(rt
, link
, route_handler
);
856 log_link_warning_errno(link
, r
, "Could not set routes: %m");
857 link_enter_failed(link
);
861 link
->route_messages
++;
864 if (link
->route_messages
== 0) {
865 link
->static_routes_configured
= true;
866 link_check_ready(link
);
868 log_link_debug(link
, "Setting routes");
873 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
874 Link
*link
= userdata
;
879 assert(link
->ifname
);
881 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
884 r
= sd_netlink_message_get_errno(m
);
885 if (r
< 0 && r
!= -ESRCH
)
886 log_link_warning_errno(link
, r
, "Could not drop route: %m");
891 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
892 Link
*link
= userdata
;
898 assert(link
->ifname
);
899 assert(link
->address_messages
> 0);
900 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
901 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
903 link
->address_messages
--;
905 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
908 r
= sd_netlink_message_get_errno(m
);
909 if (r
< 0 && r
!= -EEXIST
)
910 log_link_warning_errno(link
, r
, "could not set address: %m");
912 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
914 if (link
->address_messages
== 0) {
915 log_link_debug(link
, "Addresses set");
916 link_enter_set_routes(link
);
922 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
923 Link
*link
= userdata
;
929 assert(link
->ifname
);
930 assert(link
->address_label_messages
> 0);
932 link
->address_label_messages
--;
934 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
937 r
= sd_netlink_message_get_errno(m
);
938 if (r
< 0 && r
!= -EEXIST
)
939 log_link_warning_errno(link
, r
, "could not set address label: %m");
941 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
943 if (link
->address_label_messages
== 0)
944 log_link_debug(link
, "Addresses label set");
949 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
950 _cleanup_free_
struct in_addr
*addresses
= NULL
;
951 size_t n_addresses
= 0, n_allocated
= 0;
954 log_debug("Copying DNS server information from %s", link
->ifname
);
959 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
962 /* Only look for IPv4 addresses */
963 if (link
->network
->dns
[i
].family
!= AF_INET
)
966 ia
= link
->network
->dns
[i
].address
.in
;
968 /* Never propagate obviously borked data */
969 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
972 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
975 addresses
[n_addresses
++] = ia
;
978 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
979 const struct in_addr
*da
= NULL
;
982 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
985 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
988 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
993 if (n_addresses
<= 0)
996 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
999 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1000 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1001 size_t n_addresses
= 0, n_allocated
= 0;
1007 log_debug("Copying NTP server information from %s", link
->ifname
);
1009 STRV_FOREACH(a
, link
->network
->ntp
) {
1012 /* Only look for IPv4 addresses */
1013 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1016 /* Never propagate obviously borked data */
1017 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1020 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1023 addresses
[n_addresses
++] = ia
;
1026 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1027 const struct in_addr
*da
= NULL
;
1030 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1033 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1036 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1041 if (n_addresses
<= 0)
1044 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1047 static int link_set_bridge_fdb(Link
*link
) {
1048 FdbEntry
*fdb_entry
;
1051 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1052 r
= fdb_entry_configure(link
, fdb_entry
);
1054 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1060 static int link_enter_set_addresses(Link
*link
) {
1061 AddressLabel
*label
;
1066 assert(link
->network
);
1067 assert(link
->state
!= _LINK_STATE_INVALID
);
1069 r
= link_set_bridge_fdb(link
);
1073 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1075 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1076 r
= address_configure(ad
, link
, address_handler
, false);
1078 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1079 link_enter_failed(link
);
1083 link
->address_messages
++;
1086 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1087 r
= address_label_configure(label
, link
, address_label_handler
, false);
1089 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1090 link_enter_failed(link
);
1094 link
->address_label_messages
++;
1097 /* now that we can figure out a default address for the dhcp server,
1099 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1101 Link
*uplink
= NULL
;
1102 bool acquired_uplink
= false;
1104 address
= link_find_dhcp_server_address(link
);
1106 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1107 link_enter_failed(link
);
1111 /* use the server address' subnet as the pool */
1112 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1113 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1118 r = sd_dhcp_server_set_router(link->dhcp_server,
1119 &main_address->in_addr.in);
1124 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1125 r
= sd_dhcp_server_set_max_lease_time(
1127 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1132 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1133 r
= sd_dhcp_server_set_default_lease_time(
1135 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1140 if (link
->network
->dhcp_server_emit_dns
) {
1142 if (link
->network
->n_dhcp_server_dns
> 0)
1143 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1145 uplink
= manager_find_uplink(link
->manager
, link
);
1146 acquired_uplink
= true;
1149 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1152 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1155 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1158 if (link
->network
->dhcp_server_emit_ntp
) {
1160 if (link
->network
->n_dhcp_server_ntp
> 0)
1161 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1163 if (!acquired_uplink
)
1164 uplink
= manager_find_uplink(link
->manager
, link
);
1167 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1170 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1174 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1177 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1179 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1181 if (link
->network
->dhcp_server_emit_timezone
) {
1182 _cleanup_free_
char *buffer
= NULL
;
1183 const char *tz
= NULL
;
1185 if (link
->network
->dhcp_server_timezone
)
1186 tz
= link
->network
->dhcp_server_timezone
;
1188 r
= get_timezone(&buffer
);
1190 log_warning_errno(r
, "Failed to determine timezone: %m");
1196 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1202 r
= sd_dhcp_server_start(link
->dhcp_server
);
1204 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1206 link_enter_failed(link
);
1211 log_link_debug(link
, "Offering DHCPv4 leases");
1214 if (link
->address_messages
== 0)
1215 link_enter_set_routes(link
);
1217 log_link_debug(link
, "Setting addresses");
1222 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1223 Link
*link
= userdata
;
1228 assert(link
->ifname
);
1230 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1233 r
= sd_netlink_message_get_errno(m
);
1234 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1235 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1240 static int link_set_bridge_vlan(Link
*link
) {
1243 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1245 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1250 static int link_set_proxy_arp(Link
*link
) {
1251 const char *p
= NULL
;
1254 if (!link_proxy_arp_enabled(link
))
1257 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1259 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1261 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1266 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1267 Link
*link
= userdata
;
1272 log_link_debug(link
, "Set link");
1274 r
= sd_netlink_message_get_errno(m
);
1275 if (r
< 0 && r
!= -EEXIST
) {
1276 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1277 link_enter_failed(link
);
1283 static int link_configure_after_setting_mtu(Link
*link
);
1285 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1286 Link
*link
= userdata
;
1291 assert(link
->ifname
);
1293 link
->setting_mtu
= false;
1295 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1298 r
= sd_netlink_message_get_errno(m
);
1300 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1304 log_link_debug(link
, "Setting MTU done.");
1306 if (link
->state
== LINK_STATE_PENDING
)
1307 (void) link_configure_after_setting_mtu(link
);
1312 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1313 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1317 assert(link
->manager
);
1318 assert(link
->manager
->rtnl
);
1320 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1323 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1325 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1327 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1329 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1330 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1331 (void) link_enable_ipv6(link
);
1333 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1334 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1335 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1337 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1338 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1343 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1345 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1347 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1348 link_netlink_destroy_callback
, link
, 0, __func__
);
1350 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1353 link
->setting_mtu
= true;
1358 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1359 Link
*link
= userdata
;
1364 assert(link
->ifname
);
1366 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1369 r
= sd_netlink_message_get_errno(m
);
1371 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1376 static int link_set_flags(Link
*link
) {
1377 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1378 unsigned ifi_change
= 0;
1379 unsigned ifi_flags
= 0;
1383 assert(link
->manager
);
1384 assert(link
->manager
->rtnl
);
1386 if (link
->flags
& IFF_LOOPBACK
)
1392 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1395 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1397 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1399 if (link
->network
->arp
>= 0) {
1400 ifi_change
|= IFF_NOARP
;
1401 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1404 if (link
->network
->multicast
>= 0) {
1405 ifi_change
|= IFF_MULTICAST
;
1406 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1409 if (link
->network
->allmulticast
>= 0) {
1410 ifi_change
|= IFF_ALLMULTI
;
1411 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1414 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1416 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1418 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1419 link_netlink_destroy_callback
, link
, 0, __func__
);
1421 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1428 static int link_set_bridge(Link
*link
) {
1429 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1433 assert(link
->network
);
1435 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1437 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1439 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1441 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1443 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1445 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1447 if (link
->network
->use_bpdu
>= 0) {
1448 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1450 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1453 if (link
->network
->hairpin
>= 0) {
1454 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1456 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1459 if (link
->network
->fast_leave
>= 0) {
1460 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1462 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1465 if (link
->network
->allow_port_to_be_root
>= 0) {
1466 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1468 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1472 if (link
->network
->unicast_flood
>= 0) {
1473 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1475 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1479 if (link
->network
->cost
!= 0) {
1480 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1482 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1484 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1485 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1487 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1490 r
= sd_netlink_message_close_container(req
);
1492 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1494 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1495 link_netlink_destroy_callback
, link
, 0, __func__
);
1497 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1504 static int link_bond_set(Link
*link
) {
1505 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1509 assert(link
->network
);
1511 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1513 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1515 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1517 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1519 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1521 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1523 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1525 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1527 if (link
->network
->active_slave
) {
1528 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1530 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1533 if (link
->network
->primary_slave
) {
1534 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1536 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1539 r
= sd_netlink_message_close_container(req
);
1541 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1543 r
= sd_netlink_message_close_container(req
);
1545 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1547 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1548 link_netlink_destroy_callback
, link
, 0, __func__
);
1550 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1557 static int link_lldp_save(Link
*link
) {
1558 _cleanup_free_
char *temp_path
= NULL
;
1559 _cleanup_fclose_
FILE *f
= NULL
;
1560 sd_lldp_neighbor
**l
= NULL
;
1564 assert(link
->lldp_file
);
1567 (void) unlink(link
->lldp_file
);
1571 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1575 (void) unlink(link
->lldp_file
);
1581 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1585 fchmod(fileno(f
), 0644);
1587 for (i
= 0; i
< n
; i
++) {
1592 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1597 (void) fwrite(&u
, 1, sizeof(u
), f
);
1598 (void) fwrite(p
, 1, sz
, f
);
1601 r
= fflush_and_check(f
);
1605 if (rename(temp_path
, link
->lldp_file
) < 0) {
1612 (void) unlink(link
->lldp_file
);
1614 (void) unlink(temp_path
);
1616 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1620 for (i
= 0; i
< n
; i
++)
1621 sd_lldp_neighbor_unref(l
[i
]);
1628 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1629 Link
*link
= userdata
;
1634 (void) link_lldp_save(link
);
1636 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1637 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1639 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1641 r
= link_lldp_emit_start(link
);
1643 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1647 static int link_acquire_ipv6_conf(Link
*link
) {
1652 if (link_ipv6_accept_ra_enabled(link
)) {
1653 assert(link
->ndisc
);
1655 log_link_debug(link
, "Discovering IPv6 routers");
1657 r
= sd_ndisc_start(link
->ndisc
);
1658 if (r
< 0 && r
!= -EBUSY
)
1659 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1662 if (link_radv_enabled(link
)) {
1664 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1666 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1668 r
= sd_radv_start(link
->radv
);
1669 if (r
< 0 && r
!= -EBUSY
)
1670 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1673 (void) dhcp6_request_prefix_delegation(link
);
1678 static int link_acquire_ipv4_conf(Link
*link
) {
1682 assert(link
->network
);
1683 assert(link
->manager
);
1684 assert(link
->manager
->event
);
1686 if (link_ipv4ll_enabled(link
)) {
1687 assert(link
->ipv4ll
);
1689 log_link_debug(link
, "Acquiring IPv4 link-local address");
1691 r
= sd_ipv4ll_start(link
->ipv4ll
);
1693 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1696 if (link_dhcp4_enabled(link
)) {
1697 assert(link
->dhcp_client
);
1699 log_link_debug(link
, "Acquiring DHCPv4 lease");
1701 r
= sd_dhcp_client_start(link
->dhcp_client
);
1703 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1709 static int link_acquire_conf(Link
*link
) {
1714 r
= link_acquire_ipv4_conf(link
);
1718 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1719 r
= link_acquire_ipv6_conf(link
);
1724 if (link_lldp_emit_enabled(link
)) {
1725 r
= link_lldp_emit_start(link
);
1727 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1733 bool link_has_carrier(Link
*link
) {
1734 /* see Documentation/networking/operstates.txt in the kernel sources */
1736 if (link
->kernel_operstate
== IF_OPER_UP
)
1739 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1740 /* operstate may not be implemented, so fall back to flags */
1741 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1747 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1748 Link
*link
= userdata
;
1753 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1756 r
= sd_netlink_message_get_errno(m
);
1758 /* we warn but don't fail the link, as it may be brought up later */
1759 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1764 int link_up(Link
*link
) {
1765 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1766 uint8_t ipv6ll_mode
;
1770 assert(link
->network
);
1771 assert(link
->manager
);
1772 assert(link
->manager
->rtnl
);
1774 log_link_debug(link
, "Bringing link up");
1776 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1778 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1780 /* set it free if not enslaved with networkd */
1781 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1782 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1784 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1787 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1789 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1791 if (link
->network
->mac
) {
1792 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1794 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1797 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1799 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1801 if (link_ipv6_enabled(link
)) {
1802 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1803 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1805 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1807 if (!link_ipv6ll_enabled(link
))
1808 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1810 const char *p
= NULL
;
1811 _cleanup_free_
char *stable_secret
= NULL
;
1813 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1814 r
= read_one_line_file(p
, &stable_secret
);
1817 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1819 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1821 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1823 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1825 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1826 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1828 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1831 r
= sd_netlink_message_close_container(req
);
1833 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1836 r
= sd_netlink_message_close_container(req
);
1838 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1840 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1841 link_netlink_destroy_callback
, link
, 0, __func__
);
1843 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1850 static int link_up_can(Link
*link
) {
1851 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1856 log_link_debug(link
, "Bringing CAN link up");
1858 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1860 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1862 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1864 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1866 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1867 link_netlink_destroy_callback
, link
, 0, __func__
);
1869 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1876 static int link_set_can(Link
*link
) {
1877 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1881 assert(link
->network
);
1882 assert(link
->manager
);
1883 assert(link
->manager
->rtnl
);
1885 log_link_debug(link
, "link_set_can");
1887 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1889 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1891 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1893 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1895 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1897 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1899 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1901 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1903 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1904 struct can_bittiming bt
= {
1905 .bitrate
= link
->network
->can_bitrate
,
1906 .sample_point
= link
->network
->can_sample_point
,
1909 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1910 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1914 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1915 if (link
->network
->can_sample_point
> 0)
1916 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1918 log_link_debug(link
, "Using default sample point");
1920 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1922 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1925 if (link
->network
->can_restart_us
> 0) {
1926 char time_string
[FORMAT_TIMESPAN_MAX
];
1927 uint64_t restart_ms
;
1929 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1932 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1934 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1936 if (restart_ms
> UINT32_MAX
) {
1937 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1941 log_link_debug(link
, "Setting restart = %s", time_string
);
1943 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1945 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1948 r
= sd_netlink_message_close_container(m
);
1950 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1952 r
= sd_netlink_message_close_container(m
);
1954 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1956 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1957 link_netlink_destroy_callback
, link
, 0, __func__
);
1959 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1963 if (!(link
->flags
& IFF_UP
)) {
1964 r
= link_up_can(link
);
1966 link_enter_failed(link
);
1971 log_link_debug(link
, "link_set_can done");
1976 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1977 Link
*link
= userdata
;
1982 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1985 r
= sd_netlink_message_get_errno(m
);
1987 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1989 if (streq_ptr(link
->kind
, "can"))
1995 int link_down(Link
*link
) {
1996 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2000 assert(link
->manager
);
2001 assert(link
->manager
->rtnl
);
2003 log_link_debug(link
, "Bringing link down");
2005 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2006 RTM_SETLINK
, link
->ifindex
);
2008 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2010 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2012 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2014 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2015 link_netlink_destroy_callback
, link
, 0, __func__
);
2017 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2024 static int link_handle_bound_to_list(Link
*link
) {
2028 bool required_up
= false;
2029 bool link_is_up
= false;
2033 if (hashmap_isempty(link
->bound_to_links
))
2036 if (link
->flags
& IFF_UP
)
2039 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2040 if (link_has_carrier(l
)) {
2045 if (!required_up
&& link_is_up
) {
2046 r
= link_down(link
);
2049 } else if (required_up
&& !link_is_up
) {
2058 static int link_handle_bound_by_list(Link
*link
) {
2065 if (hashmap_isempty(link
->bound_by_links
))
2068 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2069 r
= link_handle_bound_to_list(l
);
2077 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2083 if (link
== carrier
)
2086 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2089 r
= hashmap_ensure_allocated(h
, NULL
);
2093 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2100 static int link_new_bound_by_list(Link
*link
) {
2105 bool list_updated
= false;
2108 assert(link
->manager
);
2112 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2113 if (!carrier
->network
)
2116 if (strv_isempty(carrier
->network
->bind_carrier
))
2119 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2120 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2124 list_updated
= true;
2131 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2132 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2136 link_dirty(carrier
);
2142 static int link_new_bound_to_list(Link
*link
) {
2147 bool list_updated
= false;
2150 assert(link
->manager
);
2155 if (strv_isempty(link
->network
->bind_carrier
))
2160 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2161 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2162 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2166 list_updated
= true;
2173 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2174 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2178 link_dirty(carrier
);
2184 static int link_new_carrier_maps(Link
*link
) {
2187 r
= link_new_bound_by_list(link
);
2191 r
= link_handle_bound_by_list(link
);
2195 r
= link_new_bound_to_list(link
);
2199 r
= link_handle_bound_to_list(link
);
2206 static void link_free_bound_to_list(Link
*link
) {
2210 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2211 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2213 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2214 link_dirty(bound_to
);
2220 static void link_free_bound_by_list(Link
*link
) {
2224 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2225 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2227 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2228 link_dirty(bound_by
);
2229 link_handle_bound_to_list(bound_by
);
2236 static void link_free_carrier_maps(Link
*link
) {
2237 bool list_updated
= false;
2241 if (!hashmap_isempty(link
->bound_to_links
)) {
2242 link_free_bound_to_list(link
);
2243 list_updated
= true;
2246 if (!hashmap_isempty(link
->bound_by_links
)) {
2247 link_free_bound_by_list(link
);
2248 list_updated
= true;
2257 void link_drop(Link
*link
) {
2258 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2261 link_set_state(link
, LINK_STATE_LINGER
);
2263 link_free_carrier_maps(link
);
2265 log_link_debug(link
, "Link removed");
2267 (void) unlink(link
->state_file
);
2273 static int link_joined(Link
*link
) {
2277 assert(link
->network
);
2279 if (!hashmap_isempty(link
->bound_to_links
)) {
2280 r
= link_handle_bound_to_list(link
);
2283 } else if (!(link
->flags
& IFF_UP
)) {
2286 link_enter_failed(link
);
2291 if (link
->network
->bridge
) {
2292 r
= link_set_bridge(link
);
2294 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2297 if (link
->network
->bond
) {
2298 r
= link_bond_set(link
);
2300 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2303 if (link
->network
->use_br_vlan
&&
2304 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2305 r
= link_set_bridge_vlan(link
);
2307 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2310 /* Skip setting up addresses until it gets carrier,
2311 or it would try to set addresses twice,
2312 which is bad for non-idempotent steps. */
2313 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2316 return link_enter_set_addresses(link
);
2319 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2320 Link
*link
= userdata
;
2324 assert(link
->network
);
2328 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2331 r
= sd_netlink_message_get_errno(m
);
2332 if (r
< 0 && r
!= -EEXIST
) {
2333 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2334 link_enter_failed(link
);
2337 log_link_debug(link
, "Joined netdev");
2339 if (link
->enslaving
<= 0)
2345 static int link_enter_join_netdev(Link
*link
) {
2351 assert(link
->network
);
2352 assert(link
->state
== LINK_STATE_PENDING
);
2354 link_set_state(link
, LINK_STATE_ENSLAVING
);
2358 if (!link
->network
->bridge
&&
2359 !link
->network
->bond
&&
2360 !link
->network
->vrf
&&
2361 hashmap_isempty(link
->network
->stacked_netdevs
))
2362 return link_joined(link
);
2364 if (link
->network
->bond
) {
2365 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2366 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2367 return link_joined(link
);
2369 log_struct(LOG_DEBUG
,
2370 LOG_LINK_INTERFACE(link
),
2371 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2372 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2374 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2376 log_struct_errno(LOG_WARNING
, r
,
2377 LOG_LINK_INTERFACE(link
),
2378 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2379 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2380 link_enter_failed(link
);
2387 if (link
->network
->bridge
) {
2388 log_struct(LOG_DEBUG
,
2389 LOG_LINK_INTERFACE(link
),
2390 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2391 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2393 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2395 log_struct_errno(LOG_WARNING
, r
,
2396 LOG_LINK_INTERFACE(link
),
2397 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2398 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2399 link_enter_failed(link
);
2406 if (link
->network
->vrf
) {
2407 log_struct(LOG_DEBUG
,
2408 LOG_LINK_INTERFACE(link
),
2409 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2410 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2412 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2414 log_struct_errno(LOG_WARNING
, r
,
2415 LOG_LINK_INTERFACE(link
),
2416 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2417 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2418 link_enter_failed(link
);
2425 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2427 if (netdev
->ifindex
> 0) {
2432 log_struct(LOG_DEBUG
,
2433 LOG_LINK_INTERFACE(link
),
2434 LOG_NETDEV_INTERFACE(netdev
),
2435 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2437 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2439 log_struct_errno(LOG_WARNING
, r
,
2440 LOG_LINK_INTERFACE(link
),
2441 LOG_NETDEV_INTERFACE(netdev
),
2442 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2443 link_enter_failed(link
);
2453 static int link_set_ipv4_forward(Link
*link
) {
2456 if (!link_ipv4_forward_enabled(link
))
2459 /* We propagate the forwarding flag from one interface to the
2460 * global setting one way. This means: as long as at least one
2461 * interface was configured at any time that had IP forwarding
2462 * enabled the setting will stay on for good. We do this
2463 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2464 * somewhat in sync (see below). */
2466 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2468 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2473 static int link_set_ipv6_forward(Link
*link
) {
2476 if (!link_ipv6_forward_enabled(link
))
2479 /* On Linux, the IPv6 stack does not know a per-interface
2480 * packet forwarding setting: either packet forwarding is on
2481 * for all, or off for all. We hence don't bother with a
2482 * per-interface setting, but simply propagate the interface
2483 * flag, if it is set, to the global flag, one-way. Note that
2484 * while IPv4 would allow a per-interface flag, we expose the
2485 * same behaviour there and also propagate the setting from
2486 * one to all, to keep things simple (see above). */
2488 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2490 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2495 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2496 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2497 IPv6PrivacyExtensions s
;
2498 const char *p
= NULL
;
2501 s
= link_ipv6_privacy_extensions(link
);
2505 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2506 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2508 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2510 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2515 static int link_set_ipv6_accept_ra(Link
*link
) {
2516 const char *p
= NULL
;
2519 /* Make this a NOP if IPv6 is not available */
2520 if (!socket_ipv6_is_supported())
2523 if (link
->flags
& IFF_LOOPBACK
)
2529 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2531 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2532 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2534 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2539 static int link_set_ipv6_dad_transmits(Link
*link
) {
2540 char buf
[DECIMAL_STR_MAX(int) + 1];
2541 const char *p
= NULL
;
2544 /* Make this a NOP if IPv6 is not available */
2545 if (!socket_ipv6_is_supported())
2548 if (link
->flags
& IFF_LOOPBACK
)
2554 if (link
->network
->ipv6_dad_transmits
< 0)
2557 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2558 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2560 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2562 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2567 static int link_set_ipv6_hop_limit(Link
*link
) {
2568 char buf
[DECIMAL_STR_MAX(int) + 1];
2569 const char *p
= NULL
;
2572 /* Make this a NOP if IPv6 is not available */
2573 if (!socket_ipv6_is_supported())
2576 if (link
->flags
& IFF_LOOPBACK
)
2582 if (link
->network
->ipv6_hop_limit
< 0)
2585 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2586 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2588 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2590 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2595 static int link_set_ipv6_mtu(Link
*link
) {
2596 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2597 const char *p
= NULL
;
2600 /* Make this a NOP if IPv6 is not available */
2601 if (!socket_ipv6_is_supported())
2604 if (link
->flags
& IFF_LOOPBACK
)
2607 if (link
->network
->ipv6_mtu
== 0)
2610 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2612 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2614 r
= write_string_file(p
, buf
, 0);
2616 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2621 static int link_drop_foreign_config(Link
*link
) {
2627 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2628 /* we consider IPv6LL addresses to be managed by the kernel */
2629 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2632 r
= address_remove(address
, link
, link_address_remove_handler
);
2637 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2638 /* do not touch routes managed by the kernel */
2639 if (route
->protocol
== RTPROT_KERNEL
)
2642 r
= route_remove(route
, link
, link_route_remove_handler
);
2650 static int link_drop_config(Link
*link
) {
2651 Address
*address
, *pool_address
;
2656 SET_FOREACH(address
, link
->addresses
, i
) {
2657 /* we consider IPv6LL addresses to be managed by the kernel */
2658 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2661 r
= address_remove(address
, link
, link_address_remove_handler
);
2665 /* If this address came from an address pool, clean up the pool */
2666 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2667 if (address_equal(address
, pool_address
)) {
2668 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2669 address_free(pool_address
);
2675 SET_FOREACH(route
, link
->routes
, i
) {
2676 /* do not touch routes managed by the kernel */
2677 if (route
->protocol
== RTPROT_KERNEL
)
2680 r
= route_remove(route
, link
, link_route_remove_handler
);
2690 static int link_update_lldp(Link
*link
) {
2698 if (link
->flags
& IFF_UP
) {
2699 r
= sd_lldp_start(link
->lldp
);
2701 log_link_debug(link
, "Started LLDP.");
2703 r
= sd_lldp_stop(link
->lldp
);
2705 log_link_debug(link
, "Stopped LLDP.");
2711 static int link_configure_can(Link
*link
) {
2714 if (streq_ptr(link
->kind
, "can")) {
2715 /* The CAN interface must be down to configure bitrate, etc... */
2716 if ((link
->flags
& IFF_UP
)) {
2717 r
= link_down(link
);
2719 link_enter_failed(link
);
2726 return link_set_can(link
);
2729 if (!(link
->flags
& IFF_UP
)) {
2730 r
= link_up_can(link
);
2732 link_enter_failed(link
);
2740 static int link_configure(Link
*link
) {
2744 assert(link
->network
);
2745 assert(link
->state
== LINK_STATE_PENDING
);
2747 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2748 return link_configure_can(link
);
2750 /* Drop foreign config, but ignore loopback or critical devices.
2751 * We do not want to remove loopback address or addresses used for root NFS. */
2752 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2753 r
= link_drop_foreign_config(link
);
2758 r
= link_set_proxy_arp(link
);
2762 r
= ipv6_proxy_ndp_addresses_configure(link
);
2766 r
= link_set_ipv4_forward(link
);
2770 r
= link_set_ipv6_forward(link
);
2774 r
= link_set_ipv6_privacy_extensions(link
);
2778 r
= link_set_ipv6_accept_ra(link
);
2782 r
= link_set_ipv6_dad_transmits(link
);
2786 r
= link_set_ipv6_hop_limit(link
);
2790 r
= link_set_flags(link
);
2794 r
= link_set_ipv6_mtu(link
);
2798 if (link_ipv4ll_enabled(link
)) {
2799 r
= ipv4ll_configure(link
);
2804 if (link_dhcp4_enabled(link
)) {
2805 r
= dhcp4_set_promote_secondaries(link
);
2809 r
= dhcp4_configure(link
);
2814 if (link_dhcp4_server_enabled(link
)) {
2815 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2819 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2824 if (link_dhcp6_enabled(link
) ||
2825 link_ipv6_accept_ra_enabled(link
)) {
2826 r
= dhcp6_configure(link
);
2831 if (link_ipv6_accept_ra_enabled(link
)) {
2832 r
= ndisc_configure(link
);
2837 if (link_radv_enabled(link
)) {
2838 r
= radv_configure(link
);
2843 if (link_lldp_rx_enabled(link
)) {
2844 r
= sd_lldp_new(&link
->lldp
);
2848 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2852 r
= sd_lldp_match_capabilities(link
->lldp
,
2853 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2854 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2855 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2859 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2863 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2867 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2871 r
= link_update_lldp(link
);
2876 if (link
->network
->mtu
> 0) {
2877 r
= link_set_mtu(link
, link
->network
->mtu
);
2882 return link_configure_after_setting_mtu(link
);
2885 static int link_configure_after_setting_mtu(Link
*link
) {
2889 assert(link
->network
);
2890 assert(link
->state
== LINK_STATE_PENDING
);
2892 if (link
->setting_mtu
)
2895 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2896 r
= link_acquire_conf(link
);
2901 return link_enter_join_netdev(link
);
2904 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2907 if (duid
->raw_data_len
> 0)
2910 if (duid
->type
!= DUID_TYPE_UUID
)
2913 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2914 duid
->raw_data_len
= sizeof(sd_id128_t
);
2919 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2920 Manager
*manager
= userdata
;
2921 const sd_bus_error
*e
;
2931 e
= sd_bus_message_get_error(m
);
2933 log_error_errno(sd_bus_error_get_errno(e
),
2934 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2939 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2943 if (sz
!= sizeof(sd_id128_t
)) {
2944 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2948 memcpy(&manager
->product_uuid
, a
, sz
);
2949 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2950 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2952 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2955 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2956 r
= link_configure(link
);
2958 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2961 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2963 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2964 * even if the method fails. */
2965 manager
->has_product_uuid
= true;
2970 static bool link_requires_uuid(Link
*link
) {
2974 assert(link
->manager
);
2975 assert(link
->network
);
2977 duid
= link_get_duid(link
);
2978 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2981 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2984 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2990 static int link_configure_duid(Link
*link
) {
2996 assert(link
->manager
);
2997 assert(link
->network
);
3000 duid
= link_get_duid(link
);
3002 if (!link_requires_uuid(link
))
3005 if (m
->has_product_uuid
) {
3006 (void) duid_set_uuid(duid
, m
->product_uuid
);
3010 if (!m
->links_requesting_uuid
) {
3011 r
= manager_request_product_uuid(m
, link
);
3016 log_link_warning_errno(link
, r
,
3017 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3021 r
= set_put(m
->links_requesting_uuid
, link
);
3025 r
= set_put(m
->duids_requesting_uuid
, duid
);
3033 static int link_initialized_and_synced(Link
*link
) {
3038 assert(link
->ifname
);
3039 assert(link
->manager
);
3041 if (link
->state
!= LINK_STATE_PENDING
)
3044 log_link_debug(link
, "Link state is up-to-date");
3046 r
= link_new_bound_by_list(link
);
3050 r
= link_handle_bound_by_list(link
);
3054 if (!link
->network
) {
3055 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3056 &link
->mac
, &network
);
3058 link_enter_unmanaged(link
);
3060 } else if (r
== 0 && network
->unmanaged
) {
3061 link_enter_unmanaged(link
);
3066 if (link
->flags
& IFF_LOOPBACK
) {
3067 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3068 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3070 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3071 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3073 if (network
->dhcp_server
)
3074 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3077 r
= network_apply(network
, link
);
3082 r
= link_new_bound_to_list(link
);
3086 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3087 * link_configure() is called later asynchronously. */
3088 r
= link_configure_duid(link
);
3092 r
= link_configure(link
);
3099 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
3100 (void) link_initialized_and_synced(userdata
);
3104 int link_initialized(Link
*link
, sd_device
*device
) {
3105 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3109 assert(link
->manager
);
3110 assert(link
->manager
->rtnl
);
3113 if (link
->state
!= LINK_STATE_PENDING
)
3116 if (link
->sd_device
)
3119 log_link_debug(link
, "udev initialized link");
3121 link
->sd_device
= sd_device_ref(device
);
3123 /* udev has initialized the link, but we don't know if we have yet
3124 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3125 * when it returns we know that the pending NEWLINKs have already been
3126 * processed and that we are up-to-date */
3128 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3133 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3134 link_netlink_destroy_callback
, link
, 0, __func__
);
3143 static int link_load(Link
*link
) {
3144 _cleanup_free_
char *network_file
= NULL
,
3147 *dhcp4_address
= NULL
,
3148 *ipv4ll_address
= NULL
;
3149 union in_addr_union address
;
3150 union in_addr_union route_dst
;
3156 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
3157 "NETWORK_FILE", &network_file
,
3158 "ADDRESSES", &addresses
,
3160 "DHCP4_ADDRESS", &dhcp4_address
,
3161 "IPV4LL_ADDRESS", &ipv4ll_address
,
3163 if (r
< 0 && r
!= -ENOENT
)
3164 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3171 suffix
= strrchr(network_file
, '.');
3173 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3174 goto network_file_fail
;
3178 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3180 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3181 goto network_file_fail
;
3184 r
= network_apply(network
, link
);
3186 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3195 _cleanup_free_
char *address_str
= NULL
;
3196 char *prefixlen_str
;
3198 unsigned char prefixlen
;
3200 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3202 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3208 prefixlen_str
= strchr(address_str
, '/');
3209 if (!prefixlen_str
) {
3210 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3214 *prefixlen_str
++ = '\0';
3216 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3218 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3222 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3224 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3228 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3230 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3239 _cleanup_free_
char *route_str
= NULL
;
3240 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3242 char *prefixlen_str
;
3244 unsigned char prefixlen
, tos
, table
;
3247 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3249 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3255 prefixlen_str
= strchr(route_str
, '/');
3256 if (!prefixlen_str
) {
3257 log_link_debug(link
, "Failed to parse route %s", route_str
);
3261 *prefixlen_str
++ = '\0';
3263 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3265 log_link_debug(link
,
3266 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3271 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3273 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3277 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3279 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3281 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3282 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3283 0, route_expire_handler
, route
);
3285 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3288 route
->lifetime
= lifetime
;
3289 sd_event_source_unref(route
->expire
);
3290 route
->expire
= TAKE_PTR(expire
);
3294 if (dhcp4_address
) {
3295 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3297 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3298 goto dhcp4_address_fail
;
3301 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3303 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3305 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3307 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3312 if (ipv4ll_address
) {
3313 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3315 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3316 goto ipv4ll_address_fail
;
3319 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3321 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3323 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3325 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3328 ipv4ll_address_fail
:
3333 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3334 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3335 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3344 r
= link_new(m
, message
, ret
);
3350 log_link_debug(link
, "Link %d added", link
->ifindex
);
3352 r
= link_load(link
);
3356 if (detect_container() <= 0) {
3357 /* not in a container, udev will be around */
3358 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3359 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3361 log_link_warning_errno(link
, r
, "Could not find device: %m");
3365 r
= sd_device_get_is_initialized(device
, &initialized
);
3367 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3372 log_link_debug(link
, "link pending udev initialization...");
3376 r
= link_initialized(link
, device
);
3380 r
= link_initialized_and_synced(link
);
3387 link_enter_failed(link
);
3391 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3396 log_link_info(link
, "Gained IPv6LL");
3398 link
->ipv6ll_address
= *address
;
3399 link_check_ready(link
);
3401 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3402 r
= link_acquire_ipv6_conf(link
);
3404 link_enter_failed(link
);
3412 static int link_carrier_gained(Link
*link
) {
3417 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3418 r
= link_acquire_conf(link
);
3420 link_enter_failed(link
);
3424 r
= link_enter_set_addresses(link
);
3429 r
= link_handle_bound_by_list(link
);
3436 static int link_carrier_lost(Link
*link
) {
3441 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3442 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3443 * configuration and stop the clients as well. */
3444 if (link
->setting_mtu
)
3447 r
= link_stop_clients(link
);
3449 link_enter_failed(link
);
3453 if (link_dhcp4_server_enabled(link
))
3454 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3456 r
= link_drop_config(link
);
3460 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3461 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3462 r
= link_drop_foreign_config(link
);
3467 r
= link_handle_bound_by_list(link
);
3474 int link_carrier_reset(Link
*link
) {
3479 if (link_has_carrier(link
)) {
3480 r
= link_carrier_lost(link
);
3484 r
= link_carrier_gained(link
);
3488 log_link_info(link
, "Reset carrier");
3494 int link_update(Link
*link
, sd_netlink_message
*m
) {
3495 struct ether_addr mac
;
3498 bool had_carrier
, carrier_gained
, carrier_lost
;
3502 assert(link
->ifname
);
3505 if (link
->state
== LINK_STATE_LINGER
) {
3506 log_link_info(link
, "Link readded");
3507 link_set_state(link
, LINK_STATE_ENSLAVING
);
3509 r
= link_new_carrier_maps(link
);
3514 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3515 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3516 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3518 if (link
->state
== LINK_STATE_PENDING
) {
3519 r
= free_and_strdup(&link
->ifname
, ifname
);
3523 Manager
*manager
= link
->manager
;
3526 r
= link_add(manager
, m
, &link
);
3532 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3533 if (r
>= 0 && mtu
> 0) {
3535 if (link
->original_mtu
== 0) {
3536 link
->original_mtu
= mtu
;
3537 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3540 if (link
->dhcp_client
) {
3541 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3544 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3548 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3550 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3554 /* The kernel may broadcast NEWLINK messages without the MAC address
3555 set, simply ignore them. */
3556 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3558 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3561 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3564 log_link_debug(link
, "MAC address: "
3565 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3566 mac
.ether_addr_octet
[0],
3567 mac
.ether_addr_octet
[1],
3568 mac
.ether_addr_octet
[2],
3569 mac
.ether_addr_octet
[3],
3570 mac
.ether_addr_octet
[4],
3571 mac
.ether_addr_octet
[5]);
3574 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3576 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3579 if (link
->dhcp_client
) {
3580 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3581 (const uint8_t *) &link
->mac
,
3585 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3587 r
= dhcp4_set_client_identifier(link
);
3592 if (link
->dhcp6_client
) {
3593 const DUID
* duid
= link_get_duid(link
);
3595 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3596 (const uint8_t *) &link
->mac
,
3600 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3602 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3603 link
->network
->iaid
);
3605 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3607 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3609 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3610 duid
->raw_data_len
);
3612 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3616 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3618 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3622 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3624 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3629 had_carrier
= link_has_carrier(link
);
3631 r
= link_update_flags(link
, m
);
3635 r
= link_update_lldp(link
);
3639 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3640 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3642 if (carrier_gained
) {
3643 log_link_info(link
, "Gained carrier");
3645 r
= link_carrier_gained(link
);
3648 } else if (carrier_lost
) {
3649 log_link_info(link
, "Lost carrier");
3651 r
= link_carrier_lost(link
);
3659 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3667 if (hashmap_isempty(h
))
3671 HASHMAP_FOREACH(link
, h
, i
) {
3675 fprintf(f
, "%i", link
->ifindex
);
3682 int link_save(Link
*link
) {
3683 _cleanup_free_
char *temp_path
= NULL
;
3684 _cleanup_fclose_
FILE *f
= NULL
;
3685 const char *admin_state
, *oper_state
;
3692 assert(link
->state_file
);
3693 assert(link
->lease_file
);
3694 assert(link
->manager
);
3696 if (link
->state
== LINK_STATE_LINGER
) {
3697 unlink(link
->state_file
);
3701 link_lldp_save(link
);
3703 admin_state
= link_state_to_string(link
->state
);
3704 assert(admin_state
);
3706 oper_state
= link_operstate_to_string(link
->operstate
);
3709 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3713 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3714 (void) fchmod(fileno(f
), 0644);
3717 "# This is private data. Do not parse.\n"
3720 admin_state
, oper_state
);
3722 if (link
->network
) {
3724 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3725 const char *dhcp_domainname
= NULL
;
3726 char **dhcp6_domains
= NULL
;
3727 char **dhcp_domains
= NULL
;
3730 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3731 yes_no(link
->network
->required_for_online
));
3733 if (link
->dhcp6_client
) {
3734 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3735 if (r
< 0 && r
!= -ENOMSG
)
3736 log_link_debug(link
, "No DHCPv6 lease");
3739 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3744 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3745 _cleanup_free_
char *b
= NULL
;
3747 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3748 &link
->network
->dns
[j
].address
, &b
);
3750 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3760 if (link
->network
->dhcp_use_dns
&&
3762 const struct in_addr
*addresses
;
3764 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3768 serialize_in_addrs(f
, addresses
, r
);
3773 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3774 struct in6_addr
*in6_addrs
;
3776 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3780 serialize_in6_addrs(f
, in6_addrs
, r
);
3785 /* Make sure to flush out old entries before we use the NDISC data */
3788 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3791 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3795 serialize_in6_addrs(f
, &dd
->address
, 1);
3804 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3806 if (link
->network
->dhcp_use_ntp
&&
3808 const struct in_addr
*addresses
;
3810 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3814 serialize_in_addrs(f
, addresses
, r
);
3819 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3820 struct in6_addr
*in6_addrs
;
3823 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3828 serialize_in6_addrs(f
, in6_addrs
, r
);
3832 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3834 fputstrv(f
, hosts
, NULL
, &space
);
3839 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3840 if (link
->dhcp_lease
) {
3841 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3842 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3845 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3848 fputs("DOMAINS=", f
);
3850 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3852 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3855 if (dhcp_domainname
)
3856 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3858 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3860 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3862 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3863 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3868 fputs("ROUTE_DOMAINS=", f
);
3870 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3872 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3875 if (dhcp_domainname
)
3876 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3878 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3880 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3882 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3883 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3888 fprintf(f
, "LLMNR=%s\n",
3889 resolve_support_to_string(link
->network
->llmnr
));
3890 fprintf(f
, "MDNS=%s\n",
3891 resolve_support_to_string(link
->network
->mdns
));
3893 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3894 fprintf(f
, "DNS_OVER_TLS=%s\n",
3895 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3897 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3898 fprintf(f
, "DNSSEC=%s\n",
3899 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3901 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3904 fputs("DNSSEC_NTA=", f
);
3906 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3907 fputs_with_space(f
, n
, NULL
, &space
);
3911 fputs("ADDRESSES=", f
);
3913 SET_FOREACH(a
, link
->addresses
, i
) {
3914 _cleanup_free_
char *address_str
= NULL
;
3916 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3920 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3925 fputs("ROUTES=", f
);
3927 SET_FOREACH(route
, link
->routes
, i
) {
3928 _cleanup_free_
char *route_str
= NULL
;
3930 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3934 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3935 space
? " " : "", route_str
,
3936 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3943 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3944 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3946 if (link
->dhcp_lease
) {
3947 struct in_addr address
;
3948 const char *tz
= NULL
;
3950 assert(link
->network
);
3952 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3954 fprintf(f
, "TIMEZONE=%s\n", tz
);
3956 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3958 fputs("DHCP4_ADDRESS=", f
);
3959 serialize_in_addrs(f
, &address
, 1);
3963 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3971 unlink(link
->lease_file
);
3974 struct in_addr address
;
3976 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3978 fputs("IPV4LL_ADDRESS=", f
);
3979 serialize_in_addrs(f
, &address
, 1);
3984 r
= fflush_and_check(f
);
3988 if (rename(temp_path
, link
->state_file
) < 0) {
3996 (void) unlink(link
->state_file
);
3998 (void) unlink(temp_path
);
4000 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4003 /* The serialized state in /run is no longer up-to-date. */
4004 void link_dirty(Link
*link
) {
4009 /* mark manager dirty as link is dirty */
4010 manager_dirty(link
->manager
);
4012 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4014 /* allocation errors are ignored */
4017 r
= set_put(link
->manager
->dirty_links
, link
);
4019 /* don't take another ref if the link was already dirty */
4025 /* The serialized state in /run is up-to-date */
4026 void link_clean(Link
*link
) {
4028 assert(link
->manager
);
4030 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4033 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4034 [LINK_STATE_PENDING
] = "pending",
4035 [LINK_STATE_ENSLAVING
] = "configuring",
4036 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4037 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4038 [LINK_STATE_CONFIGURED
] = "configured",
4039 [LINK_STATE_UNMANAGED
] = "unmanaged",
4040 [LINK_STATE_FAILED
] = "failed",
4041 [LINK_STATE_LINGER
] = "linger",
4044 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4046 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4047 [LINK_OPERSTATE_OFF
] = "off",
4048 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4049 [LINK_OPERSTATE_DORMANT
] = "dormant",
4050 [LINK_OPERSTATE_CARRIER
] = "carrier",
4051 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4052 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4055 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);