1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
16 #include "missing_network.h"
17 #include "netlink-util.h"
18 #include "network-internal.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 "tmpfile-util.h"
35 DUID
* link_get_duid(Link
*link
) {
36 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
37 return &link
->network
->duid
;
39 return &link
->manager
->duid
;
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (!socket_ipv6_is_supported())
48 if (link
->flags
& IFF_LOOPBACK
)
54 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
57 static bool link_dhcp4_enabled(Link
*link
) {
60 if (link
->flags
& IFF_LOOPBACK
)
66 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
69 static bool link_dhcp4_server_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->dhcp_server
;
81 static bool link_ipv4ll_enabled(Link
*link
) {
84 if (link
->flags
& IFF_LOOPBACK
)
90 if (streq_ptr(link
->kind
, "wireguard"))
93 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
96 static bool link_ipv6ll_enabled(Link
*link
) {
99 if (!socket_ipv6_is_supported())
102 if (link
->flags
& IFF_LOOPBACK
)
108 if (streq_ptr(link
->kind
, "wireguard"))
111 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
114 static bool link_ipv6_enabled(Link
*link
) {
117 if (!socket_ipv6_is_supported())
120 if (link
->network
->bridge
)
123 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
124 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
127 static bool link_radv_enabled(Link
*link
) {
130 if (!link_ipv6ll_enabled(link
))
133 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
136 static bool link_lldp_rx_enabled(Link
*link
) {
139 if (link
->flags
& IFF_LOOPBACK
)
142 if (link
->iftype
!= ARPHRD_ETHER
)
148 /* LLDP should be handled on bridge slaves as those have a direct
149 * connection to their peers not on the bridge master. Linux doesn't
150 * even (by default) forward lldp packets to the bridge master.*/
151 if (streq_ptr("bridge", link
->kind
))
154 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
157 static bool link_lldp_emit_enabled(Link
*link
) {
160 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->iftype
!= ARPHRD_ETHER
)
169 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
172 static bool link_ipv4_forward_enabled(Link
*link
) {
175 if (link
->flags
& IFF_LOOPBACK
)
181 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
184 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
187 static bool link_ipv6_forward_enabled(Link
*link
) {
190 if (!socket_ipv6_is_supported())
193 if (link
->flags
& IFF_LOOPBACK
)
199 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
202 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
205 static bool link_proxy_arp_enabled(Link
*link
) {
208 if (link
->flags
& IFF_LOOPBACK
)
214 if (link
->network
->proxy_arp
< 0)
220 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
223 if (!socket_ipv6_is_supported())
226 if (link
->flags
& IFF_LOOPBACK
)
232 if (!link_ipv6ll_enabled(link
))
235 /* If unset use system default (enabled if local forwarding is disabled.
236 * disabled if local forwarding is enabled).
237 * If set, ignore or enforce RA independent of local forwarding state.
239 if (link
->network
->ipv6_accept_ra
< 0)
240 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
241 return !link_ipv6_forward_enabled(link
);
242 else if (link
->network
->ipv6_accept_ra
> 0)
243 /* accept RA even if ip_forward is enabled */
250 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
253 if (!socket_ipv6_is_supported())
254 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
256 if (link
->flags
& IFF_LOOPBACK
)
257 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
260 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
262 return link
->network
->ipv6_privacy_extensions
;
265 static int link_enable_ipv6(Link
*link
) {
266 const char *p
= NULL
;
270 if (link
->flags
& IFF_LOOPBACK
)
273 disabled
= !link_ipv6_enabled(link
);
275 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
277 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
279 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
280 enable_disable(!disabled
), link
->ifname
);
282 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
287 void link_update_operstate(Link
*link
) {
288 LinkOperationalState operstate
;
291 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
292 operstate
= LINK_OPERSTATE_DORMANT
;
293 else if (link_has_carrier(link
)) {
295 uint8_t scope
= RT_SCOPE_NOWHERE
;
298 /* if we have carrier, check what addresses we have */
299 SET_FOREACH(address
, link
->addresses
, i
) {
300 if (!address_is_ready(address
))
303 if (address
->scope
< scope
)
304 scope
= address
->scope
;
307 /* for operstate we also take foreign addresses into account */
308 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
309 if (!address_is_ready(address
))
312 if (address
->scope
< scope
)
313 scope
= address
->scope
;
316 if (scope
< RT_SCOPE_SITE
)
317 /* universally accessible addresses found */
318 operstate
= LINK_OPERSTATE_ROUTABLE
;
319 else if (scope
< RT_SCOPE_HOST
)
320 /* only link or site local addresses found */
321 operstate
= LINK_OPERSTATE_DEGRADED
;
323 /* no useful addresses found */
324 operstate
= LINK_OPERSTATE_CARRIER
;
325 } else if (link
->flags
& IFF_UP
)
326 operstate
= LINK_OPERSTATE_NO_CARRIER
;
328 operstate
= LINK_OPERSTATE_OFF
;
330 if (link
->operstate
!= operstate
) {
331 link
->operstate
= operstate
;
332 link_send_changed(link
, "OperationalState", NULL
);
337 #define FLAG_STRING(string, flag, old, new) \
338 (((old ^ new) & flag) \
339 ? ((old & flag) ? (" -" string) : (" +" string)) \
342 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
343 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
349 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
351 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
353 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
355 /* if we got a message without operstate, take it to mean
356 the state was unchanged */
357 operstate
= link
->kernel_operstate
;
359 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
362 if (link
->flags
!= flags
) {
363 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",
364 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
365 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
366 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
367 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
368 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
369 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
370 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
371 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
372 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
373 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
374 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
375 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
376 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
377 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
378 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
379 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
380 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
381 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
382 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
384 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
385 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
386 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
387 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
388 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
389 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
390 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
391 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
393 /* link flags are currently at most 18 bits, let's align to
395 if (unknown_flags_added
)
397 "Unknown link flags gained: %#.5x (ignoring)",
398 unknown_flags_added
);
400 if (unknown_flags_removed
)
402 "Unknown link flags lost: %#.5x (ignoring)",
403 unknown_flags_removed
);
407 link
->kernel_operstate
= operstate
;
409 link_update_operstate(link
);
414 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
416 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
417 _cleanup_(link_unrefp
) Link
*link
= NULL
;
419 const char *ifname
, *kind
= NULL
;
421 unsigned short iftype
;
427 /* check for link kind */
428 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
430 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
431 r
= sd_netlink_message_exit_container(message
);
436 r
= sd_netlink_message_get_type(message
, &type
);
439 else if (type
!= RTM_NEWLINK
)
442 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
445 else if (ifindex
<= 0)
448 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
452 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
463 .state
= LINK_STATE_PENDING
,
464 .rtnl_extended_attrs
= true,
469 link
->ifname
= strdup(ifname
);
474 link
->kind
= strdup(kind
);
479 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
481 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
483 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
485 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
487 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
490 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
493 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
496 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
500 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
504 r
= link_update_flags(link
, message
);
508 *ret
= TAKE_PTR(link
);
513 static void link_detach_from_manager(Link
*link
) {
514 if (!link
|| !link
->manager
)
517 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
518 set_remove(link
->manager
->links_requesting_uuid
, link
);
522 static Link
*link_free(Link
*link
) {
530 while ((route
= set_first(link
->routes
)))
533 while ((route
= set_first(link
->routes_foreign
)))
536 link
->routes
= set_free(link
->routes
);
537 link
->routes_foreign
= set_free(link
->routes_foreign
);
539 while ((address
= set_first(link
->addresses
)))
540 address_free(address
);
542 while ((address
= set_first(link
->addresses_foreign
)))
543 address_free(address
);
545 link
->addresses
= set_free(link
->addresses
);
546 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
548 while ((address
= link
->pool_addresses
)) {
549 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
550 address_free(address
);
553 sd_dhcp_server_unref(link
->dhcp_server
);
554 sd_dhcp_client_unref(link
->dhcp_client
);
555 sd_dhcp_lease_unref(link
->dhcp_lease
);
557 link_lldp_emit_stop(link
);
559 free(link
->lease_file
);
561 sd_lldp_unref(link
->lldp
);
562 free(link
->lldp_file
);
566 sd_ipv4ll_unref(link
->ipv4ll
);
567 sd_dhcp6_client_unref(link
->dhcp6_client
);
568 sd_ndisc_unref(link
->ndisc
);
569 sd_radv_unref(link
->radv
);
571 link_detach_from_manager(link
);
577 (void) unlink(link
->state_file
);
578 free(link
->state_file
);
580 sd_device_unref(link
->sd_device
);
582 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
583 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
584 hashmap_free(link
->bound_to_links
);
586 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
587 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
588 hashmap_free(link
->bound_by_links
);
593 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
595 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
602 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
611 static void link_set_state(Link
*link
, LinkState state
) {
614 if (link
->state
== state
)
619 link_send_changed(link
, "AdministrativeState", NULL
);
622 static void link_enter_unmanaged(Link
*link
) {
625 log_link_debug(link
, "Unmanaged");
627 link_set_state(link
, LINK_STATE_UNMANAGED
);
632 static int link_stop_clients(Link
*link
) {
636 assert(link
->manager
);
637 assert(link
->manager
->event
);
639 if (link
->dhcp_client
) {
640 k
= sd_dhcp_client_stop(link
->dhcp_client
);
642 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
646 k
= sd_ipv4ll_stop(link
->ipv4ll
);
648 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
651 if (link
->dhcp6_client
) {
652 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
654 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
658 k
= sd_ndisc_stop(link
->ndisc
);
660 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
664 k
= sd_radv_stop(link
->radv
);
666 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
669 link_lldp_emit_stop(link
);
673 void link_enter_failed(Link
*link
) {
676 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
679 log_link_warning(link
, "Failed");
681 link_set_state(link
, LINK_STATE_FAILED
);
683 link_stop_clients(link
);
688 static Address
* link_find_dhcp_server_address(Link
*link
) {
692 assert(link
->network
);
694 /* The first statically configured address if there is any */
695 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
697 if (address
->family
!= AF_INET
)
700 if (in_addr_is_null(address
->family
, &address
->in_addr
))
706 /* If that didn't work, find a suitable address we got from the pool */
707 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
708 if (address
->family
!= AF_INET
)
717 static void link_enter_configured(Link
*link
) {
719 assert(link
->network
);
721 if (link
->state
!= LINK_STATE_CONFIGURING
)
724 log_link_info(link
, "Configured");
726 link_set_state(link
, LINK_STATE_CONFIGURED
);
731 void link_check_ready(Link
*link
) {
737 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
743 if (!link
->addresses_configured
)
746 if (!link
->neighbors_configured
)
749 if (!link
->static_routes_configured
)
752 if (!link
->routing_policy_rules_configured
)
755 if (link_ipv4ll_enabled(link
))
756 if (!link
->ipv4ll_address
||
760 if (!link
->network
->bridge
) {
762 if (link_ipv6ll_enabled(link
))
763 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
766 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
767 !link
->dhcp4_configured
) ||
768 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
769 !link
->dhcp6_configured
) ||
770 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
771 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
774 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
778 SET_FOREACH(a
, link
->addresses
, i
)
779 if (!address_is_ready(a
))
782 if (link
->state
!= LINK_STATE_CONFIGURED
)
783 link_enter_configured(link
);
788 static int link_request_set_routing_policy_rule(Link
*link
) {
789 RoutingPolicyRule
*rule
, *rrule
= NULL
;
793 assert(link
->network
);
795 link_set_state(link
, LINK_STATE_CONFIGURING
);
796 link
->routing_policy_rules_configured
= false;
798 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
799 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
800 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
801 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
803 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
807 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
809 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
810 link_enter_failed(link
);
814 link
->routing_policy_rule_messages
++;
817 routing_policy_rule_purge(link
->manager
, link
);
818 if (link
->routing_policy_rule_messages
== 0) {
819 link
->routing_policy_rules_configured
= true;
820 link_check_ready(link
);
822 log_link_debug(link
, "Setting routing policy rules");
827 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
831 assert(link
->route_messages
> 0);
832 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
833 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
835 link
->route_messages
--;
837 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
840 r
= sd_netlink_message_get_errno(m
);
841 if (r
< 0 && r
!= -EEXIST
)
842 log_link_warning_errno(link
, r
, "Could not set route: %m");
844 if (link
->route_messages
== 0) {
845 log_link_debug(link
, "Routes set");
846 link
->static_routes_configured
= true;
847 link_check_ready(link
);
853 static int link_request_set_routes(Link
*link
) {
855 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
856 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
863 assert(link
->network
);
864 assert(link
->addresses_configured
);
865 assert(link
->address_messages
== 0);
866 assert(link
->state
!= _LINK_STATE_INVALID
);
868 link_set_state(link
, LINK_STATE_CONFIGURING
);
869 link
->static_routes_configured
= false;
871 r
= link_request_set_routing_policy_rule(link
);
875 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
876 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
877 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
879 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
882 r
= route_configure(rt
, link
, route_handler
);
884 log_link_warning_errno(link
, r
, "Could not set routes: %m");
885 link_enter_failed(link
);
889 link
->route_messages
++;
892 if (link
->route_messages
== 0) {
893 link
->static_routes_configured
= true;
894 link_check_ready(link
);
896 log_link_debug(link
, "Setting routes");
901 static int link_request_set_neighbors(Link
*link
) {
906 assert(link
->network
);
907 assert(link
->state
!= _LINK_STATE_INVALID
);
909 link_set_state(link
, LINK_STATE_CONFIGURING
);
910 link
->neighbors_configured
= false;
912 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
913 r
= neighbor_configure(neighbor
, link
, NULL
);
915 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
916 link_enter_failed(link
);
921 if (link
->neighbor_messages
== 0) {
922 link
->neighbors_configured
= true;
923 link_check_ready(link
);
925 log_link_debug(link
, "Setting neighbors");
930 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
936 assert(link
->ifname
);
937 assert(link
->address_messages
> 0);
938 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
939 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
941 link
->address_messages
--;
943 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
946 r
= sd_netlink_message_get_errno(m
);
947 if (r
< 0 && r
!= -EEXIST
)
948 log_link_warning_errno(link
, r
, "could not set address: %m");
950 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
952 if (link
->address_messages
== 0) {
953 log_link_debug(link
, "Addresses set");
954 link
->addresses_configured
= true;
955 link_request_set_routes(link
);
961 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
962 _cleanup_free_
struct in_addr
*addresses
= NULL
;
963 size_t n_addresses
= 0, n_allocated
= 0;
966 log_debug("Copying DNS server information from %s", link
->ifname
);
971 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
974 /* Only look for IPv4 addresses */
975 if (link
->network
->dns
[i
].family
!= AF_INET
)
978 ia
= link
->network
->dns
[i
].address
.in
;
980 /* Never propagate obviously borked data */
981 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
984 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
987 addresses
[n_addresses
++] = ia
;
990 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
991 const struct in_addr
*da
= NULL
;
994 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
997 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1000 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1005 if (n_addresses
<= 0)
1008 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1011 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1012 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1013 size_t n_addresses
= 0, n_allocated
= 0;
1019 log_debug("Copying NTP server information from %s", link
->ifname
);
1021 STRV_FOREACH(a
, link
->network
->ntp
) {
1024 /* Only look for IPv4 addresses */
1025 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1028 /* Never propagate obviously borked data */
1029 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1032 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1035 addresses
[n_addresses
++] = ia
;
1038 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1039 const struct in_addr
*da
= NULL
;
1042 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1045 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1048 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1053 if (n_addresses
<= 0)
1056 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1059 static int link_set_bridge_fdb(Link
*link
) {
1060 FdbEntry
*fdb_entry
;
1063 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1064 r
= fdb_entry_configure(link
, fdb_entry
);
1066 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1072 static int link_request_set_addresses(Link
*link
) {
1073 AddressLabel
*label
;
1078 assert(link
->network
);
1079 assert(link
->state
!= _LINK_STATE_INVALID
);
1081 link_set_state(link
, LINK_STATE_CONFIGURING
);
1083 /* Reset all *_configured flags we are configuring. */
1084 link
->addresses_configured
= false;
1085 link
->neighbors_configured
= false;
1086 link
->static_routes_configured
= false;
1087 link
->routing_policy_rules_configured
= false;
1089 r
= link_set_bridge_fdb(link
);
1093 r
= link_request_set_neighbors(link
);
1097 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1098 r
= address_configure(ad
, link
, address_handler
, false);
1100 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1101 link_enter_failed(link
);
1105 link
->address_messages
++;
1108 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1109 r
= address_label_configure(label
, link
, NULL
, false);
1111 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1112 link_enter_failed(link
);
1116 link
->address_label_messages
++;
1119 /* now that we can figure out a default address for the dhcp server,
1121 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1123 Link
*uplink
= NULL
;
1124 bool acquired_uplink
= false;
1126 address
= link_find_dhcp_server_address(link
);
1128 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1129 link_enter_failed(link
);
1133 /* use the server address' subnet as the pool */
1134 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1135 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1140 r = sd_dhcp_server_set_router(link->dhcp_server,
1141 &main_address->in_addr.in);
1146 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1147 r
= sd_dhcp_server_set_max_lease_time(
1149 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1154 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1155 r
= sd_dhcp_server_set_default_lease_time(
1157 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1162 if (link
->network
->dhcp_server_emit_dns
) {
1164 if (link
->network
->n_dhcp_server_dns
> 0)
1165 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1167 uplink
= manager_find_uplink(link
->manager
, link
);
1168 acquired_uplink
= true;
1171 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1174 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1177 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1180 if (link
->network
->dhcp_server_emit_ntp
) {
1182 if (link
->network
->n_dhcp_server_ntp
> 0)
1183 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1185 if (!acquired_uplink
)
1186 uplink
= manager_find_uplink(link
->manager
, link
);
1189 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1192 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1196 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1199 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1201 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1203 if (link
->network
->dhcp_server_emit_timezone
) {
1204 _cleanup_free_
char *buffer
= NULL
;
1205 const char *tz
= NULL
;
1207 if (link
->network
->dhcp_server_timezone
)
1208 tz
= link
->network
->dhcp_server_timezone
;
1210 r
= get_timezone(&buffer
);
1212 log_warning_errno(r
, "Failed to determine timezone: %m");
1218 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1223 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1224 r
= sd_dhcp_server_start(link
->dhcp_server
);
1226 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1228 link_enter_failed(link
);
1234 log_link_debug(link
, "Offering DHCPv4 leases");
1237 if (link
->address_messages
== 0) {
1238 link
->addresses_configured
= true;
1239 link_request_set_routes(link
);
1241 log_link_debug(link
, "Setting addresses");
1246 static int link_set_bridge_vlan(Link
*link
) {
1249 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1251 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1256 static int link_set_proxy_arp(Link
*link
) {
1257 const char *p
= NULL
;
1260 if (!link_proxy_arp_enabled(link
))
1263 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1265 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1267 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1272 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1277 log_link_debug(link
, "Set link");
1279 r
= sd_netlink_message_get_errno(m
);
1280 if (r
< 0 && r
!= -EEXIST
) {
1281 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1282 link_enter_failed(link
);
1288 static int link_configure_after_setting_mtu(Link
*link
);
1290 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1295 assert(link
->ifname
);
1297 link
->setting_mtu
= false;
1299 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1302 r
= sd_netlink_message_get_errno(m
);
1304 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1308 log_link_debug(link
, "Setting MTU done.");
1310 if (link
->state
== LINK_STATE_PENDING
)
1311 (void) link_configure_after_setting_mtu(link
);
1316 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1317 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1321 assert(link
->manager
);
1322 assert(link
->manager
->rtnl
);
1324 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1327 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1329 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1331 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1333 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1334 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1335 (void) link_enable_ipv6(link
);
1337 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1338 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1339 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1341 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1342 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1347 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1349 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1351 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1352 link_netlink_destroy_callback
, link
);
1354 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1357 link
->setting_mtu
= true;
1362 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1367 assert(link
->ifname
);
1369 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1372 r
= sd_netlink_message_get_errno(m
);
1374 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1379 static int link_set_flags(Link
*link
) {
1380 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1381 unsigned ifi_change
= 0;
1382 unsigned ifi_flags
= 0;
1386 assert(link
->manager
);
1387 assert(link
->manager
->rtnl
);
1389 if (link
->flags
& IFF_LOOPBACK
)
1395 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1398 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1400 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1402 if (link
->network
->arp
>= 0) {
1403 ifi_change
|= IFF_NOARP
;
1404 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1407 if (link
->network
->multicast
>= 0) {
1408 ifi_change
|= IFF_MULTICAST
;
1409 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1412 if (link
->network
->allmulticast
>= 0) {
1413 ifi_change
|= IFF_ALLMULTI
;
1414 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1417 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1419 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1421 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1422 link_netlink_destroy_callback
, link
);
1424 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1431 static int link_set_bridge(Link
*link
) {
1432 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1436 assert(link
->network
);
1438 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1440 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1442 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1444 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1446 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1448 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1450 if (link
->network
->use_bpdu
>= 0) {
1451 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1453 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1456 if (link
->network
->hairpin
>= 0) {
1457 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1459 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1462 if (link
->network
->fast_leave
>= 0) {
1463 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1465 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1468 if (link
->network
->allow_port_to_be_root
>= 0) {
1469 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1471 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1475 if (link
->network
->unicast_flood
>= 0) {
1476 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1478 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1481 if (link
->network
->multicast_to_unicast
>= 0) {
1482 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1484 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1487 if (link
->network
->cost
!= 0) {
1488 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1490 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1493 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1494 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1496 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1499 r
= sd_netlink_message_close_container(req
);
1501 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1503 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1504 link_netlink_destroy_callback
, link
);
1506 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1513 static int link_bond_set(Link
*link
) {
1514 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1518 assert(link
->network
);
1520 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1522 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1524 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1526 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1528 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1530 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1532 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1534 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1536 if (link
->network
->active_slave
) {
1537 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1539 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1542 if (link
->network
->primary_slave
) {
1543 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1545 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1548 r
= sd_netlink_message_close_container(req
);
1550 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1552 r
= sd_netlink_message_close_container(req
);
1554 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1556 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1557 link_netlink_destroy_callback
, link
);
1559 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1566 static int link_lldp_save(Link
*link
) {
1567 _cleanup_free_
char *temp_path
= NULL
;
1568 _cleanup_fclose_
FILE *f
= NULL
;
1569 sd_lldp_neighbor
**l
= NULL
;
1573 assert(link
->lldp_file
);
1576 (void) unlink(link
->lldp_file
);
1580 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1584 (void) unlink(link
->lldp_file
);
1590 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1594 fchmod(fileno(f
), 0644);
1596 for (i
= 0; i
< n
; i
++) {
1601 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1606 (void) fwrite(&u
, 1, sizeof(u
), f
);
1607 (void) fwrite(p
, 1, sz
, f
);
1610 r
= fflush_and_check(f
);
1614 if (rename(temp_path
, link
->lldp_file
) < 0) {
1621 (void) unlink(link
->lldp_file
);
1623 (void) unlink(temp_path
);
1625 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1629 for (i
= 0; i
< n
; i
++)
1630 sd_lldp_neighbor_unref(l
[i
]);
1637 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1638 Link
*link
= userdata
;
1643 (void) link_lldp_save(link
);
1645 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1646 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1648 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1650 r
= link_lldp_emit_start(link
);
1652 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1656 static int link_acquire_ipv6_conf(Link
*link
) {
1661 if (link_ipv6_accept_ra_enabled(link
)) {
1662 assert(link
->ndisc
);
1664 log_link_debug(link
, "Discovering IPv6 routers");
1666 r
= sd_ndisc_start(link
->ndisc
);
1667 if (r
< 0 && r
!= -EBUSY
)
1668 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1671 if (link_radv_enabled(link
)) {
1673 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1675 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1677 r
= sd_radv_start(link
->radv
);
1678 if (r
< 0 && r
!= -EBUSY
)
1679 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1682 (void) dhcp6_request_prefix_delegation(link
);
1687 static int link_acquire_ipv4_conf(Link
*link
) {
1691 assert(link
->network
);
1692 assert(link
->manager
);
1693 assert(link
->manager
->event
);
1695 if (link_ipv4ll_enabled(link
)) {
1696 assert(link
->ipv4ll
);
1698 log_link_debug(link
, "Acquiring IPv4 link-local address");
1700 r
= sd_ipv4ll_start(link
->ipv4ll
);
1702 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1705 if (link_dhcp4_enabled(link
)) {
1706 assert(link
->dhcp_client
);
1708 log_link_debug(link
, "Acquiring DHCPv4 lease");
1710 r
= sd_dhcp_client_start(link
->dhcp_client
);
1712 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1718 static int link_acquire_conf(Link
*link
) {
1723 r
= link_acquire_ipv4_conf(link
);
1727 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1728 r
= link_acquire_ipv6_conf(link
);
1733 if (link_lldp_emit_enabled(link
)) {
1734 r
= link_lldp_emit_start(link
);
1736 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1742 bool link_has_carrier(Link
*link
) {
1743 /* see Documentation/networking/operstates.txt in the kernel sources */
1745 if (link
->kernel_operstate
== IF_OPER_UP
)
1748 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1749 /* operstate may not be implemented, so fall back to flags */
1750 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1756 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1761 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1764 r
= sd_netlink_message_get_errno(m
);
1766 /* we warn but don't fail the link, as it may be brought up later */
1767 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1772 int link_up(Link
*link
) {
1773 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1774 uint8_t ipv6ll_mode
;
1778 assert(link
->network
);
1779 assert(link
->manager
);
1780 assert(link
->manager
->rtnl
);
1782 log_link_debug(link
, "Bringing link up");
1784 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1786 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1788 /* set it free if not enslaved with networkd */
1789 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1790 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1792 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1795 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1797 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1799 if (link
->network
->mac
) {
1800 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1802 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1805 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1807 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1809 if (link_ipv6_enabled(link
)) {
1810 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1811 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1813 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1815 if (!link_ipv6ll_enabled(link
))
1816 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1818 const char *p
= NULL
;
1819 _cleanup_free_
char *stable_secret
= NULL
;
1821 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1822 r
= read_one_line_file(p
, &stable_secret
);
1825 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1827 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1829 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1831 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1833 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1834 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1836 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1839 r
= sd_netlink_message_close_container(req
);
1841 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1844 r
= sd_netlink_message_close_container(req
);
1846 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1848 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1849 link_netlink_destroy_callback
, link
);
1851 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1858 static int link_up_can(Link
*link
) {
1859 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1864 log_link_debug(link
, "Bringing CAN link up");
1866 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1868 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1870 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1872 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1874 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1875 link_netlink_destroy_callback
, link
);
1877 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1884 static int link_set_can(Link
*link
) {
1885 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1889 assert(link
->network
);
1890 assert(link
->manager
);
1891 assert(link
->manager
->rtnl
);
1893 log_link_debug(link
, "link_set_can");
1895 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1897 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1899 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1901 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1903 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1905 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1907 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1909 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1911 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1912 struct can_bittiming bt
= {
1913 .bitrate
= link
->network
->can_bitrate
,
1914 .sample_point
= link
->network
->can_sample_point
,
1917 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1918 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1922 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1923 if (link
->network
->can_sample_point
> 0)
1924 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1926 log_link_debug(link
, "Using default sample point");
1928 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1930 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1933 if (link
->network
->can_restart_us
> 0) {
1934 char time_string
[FORMAT_TIMESPAN_MAX
];
1935 uint64_t restart_ms
;
1937 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1940 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1942 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1944 if (restart_ms
> UINT32_MAX
) {
1945 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1949 log_link_debug(link
, "Setting restart = %s", time_string
);
1951 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1953 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1956 r
= sd_netlink_message_close_container(m
);
1958 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1960 r
= sd_netlink_message_close_container(m
);
1962 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1964 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1965 link_netlink_destroy_callback
, link
);
1967 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1971 if (!(link
->flags
& IFF_UP
)) {
1972 r
= link_up_can(link
);
1974 link_enter_failed(link
);
1979 log_link_debug(link
, "link_set_can done");
1984 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1989 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1992 r
= sd_netlink_message_get_errno(m
);
1994 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1996 if (streq_ptr(link
->kind
, "can"))
2002 int link_down(Link
*link
) {
2003 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2007 assert(link
->manager
);
2008 assert(link
->manager
->rtnl
);
2010 log_link_debug(link
, "Bringing link down");
2012 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2013 RTM_SETLINK
, link
->ifindex
);
2015 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2017 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2019 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2021 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2022 link_netlink_destroy_callback
, link
);
2024 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2031 static int link_handle_bound_to_list(Link
*link
) {
2035 bool required_up
= false;
2036 bool link_is_up
= false;
2040 if (hashmap_isempty(link
->bound_to_links
))
2043 if (link
->flags
& IFF_UP
)
2046 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2047 if (link_has_carrier(l
)) {
2052 if (!required_up
&& link_is_up
) {
2053 r
= link_down(link
);
2056 } else if (required_up
&& !link_is_up
) {
2065 static int link_handle_bound_by_list(Link
*link
) {
2072 if (hashmap_isempty(link
->bound_by_links
))
2075 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2076 r
= link_handle_bound_to_list(l
);
2084 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2090 if (link
== carrier
)
2093 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2096 r
= hashmap_ensure_allocated(h
, NULL
);
2100 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2107 static int link_new_bound_by_list(Link
*link
) {
2112 bool list_updated
= false;
2115 assert(link
->manager
);
2119 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2120 if (!carrier
->network
)
2123 if (strv_isempty(carrier
->network
->bind_carrier
))
2126 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2127 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2131 list_updated
= true;
2138 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2139 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2143 link_dirty(carrier
);
2149 static int link_new_bound_to_list(Link
*link
) {
2154 bool list_updated
= false;
2157 assert(link
->manager
);
2162 if (strv_isempty(link
->network
->bind_carrier
))
2167 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2168 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2169 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2173 list_updated
= true;
2180 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2181 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2185 link_dirty(carrier
);
2191 static int link_new_carrier_maps(Link
*link
) {
2194 r
= link_new_bound_by_list(link
);
2198 r
= link_handle_bound_by_list(link
);
2202 r
= link_new_bound_to_list(link
);
2206 r
= link_handle_bound_to_list(link
);
2213 static void link_free_bound_to_list(Link
*link
) {
2217 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2218 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2220 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2221 link_dirty(bound_to
);
2227 static void link_free_bound_by_list(Link
*link
) {
2231 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2232 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2234 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2235 link_dirty(bound_by
);
2236 link_handle_bound_to_list(bound_by
);
2243 static void link_free_carrier_maps(Link
*link
) {
2244 bool list_updated
= false;
2248 if (!hashmap_isempty(link
->bound_to_links
)) {
2249 link_free_bound_to_list(link
);
2250 list_updated
= true;
2253 if (!hashmap_isempty(link
->bound_by_links
)) {
2254 link_free_bound_by_list(link
);
2255 list_updated
= true;
2264 void link_drop(Link
*link
) {
2265 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2268 link_set_state(link
, LINK_STATE_LINGER
);
2270 link_free_carrier_maps(link
);
2272 log_link_debug(link
, "Link removed");
2274 (void) unlink(link
->state_file
);
2276 link_detach_from_manager(link
);
2283 static int link_joined(Link
*link
) {
2287 assert(link
->network
);
2289 if (!hashmap_isempty(link
->bound_to_links
)) {
2290 r
= link_handle_bound_to_list(link
);
2293 } else if (!(link
->flags
& IFF_UP
)) {
2296 link_enter_failed(link
);
2301 if (link
->network
->bridge
) {
2302 r
= link_set_bridge(link
);
2304 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2307 if (link
->network
->bond
) {
2308 r
= link_bond_set(link
);
2310 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2313 if (link
->network
->use_br_vlan
&&
2314 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2315 r
= link_set_bridge_vlan(link
);
2317 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2320 /* Skip setting up addresses until it gets carrier,
2321 or it would try to set addresses twice,
2322 which is bad for non-idempotent steps. */
2323 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2326 return link_request_set_addresses(link
);
2329 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2333 assert(link
->network
);
2337 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2340 r
= sd_netlink_message_get_errno(m
);
2341 if (r
< 0 && r
!= -EEXIST
) {
2342 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2343 link_enter_failed(link
);
2346 log_link_debug(link
, "Joined netdev");
2348 if (link
->enslaving
<= 0)
2354 static int link_enter_join_netdev(Link
*link
) {
2360 assert(link
->network
);
2361 assert(link
->state
== LINK_STATE_PENDING
);
2363 link_set_state(link
, LINK_STATE_CONFIGURING
);
2367 if (!link
->network
->bridge
&&
2368 !link
->network
->bond
&&
2369 !link
->network
->vrf
&&
2370 hashmap_isempty(link
->network
->stacked_netdevs
))
2371 return link_joined(link
);
2373 if (link
->network
->bond
) {
2374 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2375 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2376 return link_joined(link
);
2378 log_struct(LOG_DEBUG
,
2379 LOG_LINK_INTERFACE(link
),
2380 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2381 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2383 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2385 log_struct_errno(LOG_WARNING
, r
,
2386 LOG_LINK_INTERFACE(link
),
2387 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2388 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2389 link_enter_failed(link
);
2396 if (link
->network
->bridge
) {
2397 log_struct(LOG_DEBUG
,
2398 LOG_LINK_INTERFACE(link
),
2399 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2400 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2402 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2404 log_struct_errno(LOG_WARNING
, r
,
2405 LOG_LINK_INTERFACE(link
),
2406 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2407 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2408 link_enter_failed(link
);
2415 if (link
->network
->vrf
) {
2416 log_struct(LOG_DEBUG
,
2417 LOG_LINK_INTERFACE(link
),
2418 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2419 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2421 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2423 log_struct_errno(LOG_WARNING
, r
,
2424 LOG_LINK_INTERFACE(link
),
2425 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2426 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2427 link_enter_failed(link
);
2434 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2436 if (netdev
->ifindex
> 0) {
2441 log_struct(LOG_DEBUG
,
2442 LOG_LINK_INTERFACE(link
),
2443 LOG_NETDEV_INTERFACE(netdev
),
2444 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2446 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2448 log_struct_errno(LOG_WARNING
, r
,
2449 LOG_LINK_INTERFACE(link
),
2450 LOG_NETDEV_INTERFACE(netdev
),
2451 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2452 link_enter_failed(link
);
2462 static int link_set_ipv4_forward(Link
*link
) {
2465 if (!link_ipv4_forward_enabled(link
))
2468 /* We propagate the forwarding flag from one interface to the
2469 * global setting one way. This means: as long as at least one
2470 * interface was configured at any time that had IP forwarding
2471 * enabled the setting will stay on for good. We do this
2472 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2473 * somewhat in sync (see below). */
2475 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2477 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2482 static int link_set_ipv6_forward(Link
*link
) {
2485 if (!link_ipv6_forward_enabled(link
))
2488 /* On Linux, the IPv6 stack does not know a per-interface
2489 * packet forwarding setting: either packet forwarding is on
2490 * for all, or off for all. We hence don't bother with a
2491 * per-interface setting, but simply propagate the interface
2492 * flag, if it is set, to the global flag, one-way. Note that
2493 * while IPv4 would allow a per-interface flag, we expose the
2494 * same behaviour there and also propagate the setting from
2495 * one to all, to keep things simple (see above). */
2497 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2499 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2504 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2505 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2506 IPv6PrivacyExtensions s
;
2507 const char *p
= NULL
;
2510 s
= link_ipv6_privacy_extensions(link
);
2514 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2515 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2517 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2519 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2524 static int link_set_ipv6_accept_ra(Link
*link
) {
2525 const char *p
= NULL
;
2528 /* Make this a NOP if IPv6 is not available */
2529 if (!socket_ipv6_is_supported())
2532 if (link
->flags
& IFF_LOOPBACK
)
2538 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2540 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2541 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2543 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2548 static int link_set_ipv6_dad_transmits(Link
*link
) {
2549 char buf
[DECIMAL_STR_MAX(int) + 1];
2550 const char *p
= NULL
;
2553 /* Make this a NOP if IPv6 is not available */
2554 if (!socket_ipv6_is_supported())
2557 if (link
->flags
& IFF_LOOPBACK
)
2563 if (link
->network
->ipv6_dad_transmits
< 0)
2566 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2567 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2569 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2571 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2576 static int link_set_ipv6_hop_limit(Link
*link
) {
2577 char buf
[DECIMAL_STR_MAX(int) + 1];
2578 const char *p
= NULL
;
2581 /* Make this a NOP if IPv6 is not available */
2582 if (!socket_ipv6_is_supported())
2585 if (link
->flags
& IFF_LOOPBACK
)
2591 if (link
->network
->ipv6_hop_limit
< 0)
2594 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2595 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2597 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2599 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2604 static int link_set_ipv6_mtu(Link
*link
) {
2605 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2606 const char *p
= NULL
;
2609 /* Make this a NOP if IPv6 is not available */
2610 if (!socket_ipv6_is_supported())
2613 if (link
->flags
& IFF_LOOPBACK
)
2616 if (link
->network
->ipv6_mtu
== 0)
2619 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2621 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2623 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2625 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2630 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2631 Address
*net_address
;
2639 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2640 if (address_equal(net_address
, address
))
2646 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2655 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2656 if (route_equal(net_route
, route
))
2662 static int link_drop_foreign_config(Link
*link
) {
2668 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2669 /* we consider IPv6LL addresses to be managed by the kernel */
2670 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2673 if (link_is_static_address_configured(link
, address
)) {
2674 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2676 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2678 r
= address_remove(address
, link
, NULL
);
2684 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2685 /* do not touch routes managed by the kernel */
2686 if (route
->protocol
== RTPROT_KERNEL
)
2689 if (link_is_static_route_configured(link
, route
)) {
2690 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2694 r
= route_remove(route
, link
, NULL
);
2703 static int link_drop_config(Link
*link
) {
2704 Address
*address
, *pool_address
;
2709 SET_FOREACH(address
, link
->addresses
, i
) {
2710 /* we consider IPv6LL addresses to be managed by the kernel */
2711 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2714 r
= address_remove(address
, link
, NULL
);
2718 /* If this address came from an address pool, clean up the pool */
2719 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2720 if (address_equal(address
, pool_address
)) {
2721 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2722 address_free(pool_address
);
2728 SET_FOREACH(route
, link
->routes
, i
) {
2729 /* do not touch routes managed by the kernel */
2730 if (route
->protocol
== RTPROT_KERNEL
)
2733 r
= route_remove(route
, link
, NULL
);
2743 static int link_update_lldp(Link
*link
) {
2751 if (link
->flags
& IFF_UP
) {
2752 r
= sd_lldp_start(link
->lldp
);
2754 log_link_debug(link
, "Started LLDP.");
2756 r
= sd_lldp_stop(link
->lldp
);
2758 log_link_debug(link
, "Stopped LLDP.");
2764 static int link_configure_can(Link
*link
) {
2767 if (streq_ptr(link
->kind
, "can")) {
2768 /* The CAN interface must be down to configure bitrate, etc... */
2769 if ((link
->flags
& IFF_UP
)) {
2770 r
= link_down(link
);
2772 link_enter_failed(link
);
2779 return link_set_can(link
);
2782 if (!(link
->flags
& IFF_UP
)) {
2783 r
= link_up_can(link
);
2785 link_enter_failed(link
);
2793 static int link_configure(Link
*link
) {
2797 assert(link
->network
);
2798 assert(link
->state
== LINK_STATE_PENDING
);
2800 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2801 return link_configure_can(link
);
2803 /* Drop foreign config, but ignore loopback or critical devices.
2804 * We do not want to remove loopback address or addresses used for root NFS. */
2805 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2806 r
= link_drop_foreign_config(link
);
2811 r
= link_set_proxy_arp(link
);
2815 r
= ipv6_proxy_ndp_addresses_configure(link
);
2819 r
= link_set_ipv4_forward(link
);
2823 r
= link_set_ipv6_forward(link
);
2827 r
= link_set_ipv6_privacy_extensions(link
);
2831 r
= link_set_ipv6_accept_ra(link
);
2835 r
= link_set_ipv6_dad_transmits(link
);
2839 r
= link_set_ipv6_hop_limit(link
);
2843 r
= link_set_flags(link
);
2847 r
= link_set_ipv6_mtu(link
);
2851 if (link_ipv4ll_enabled(link
)) {
2852 r
= ipv4ll_configure(link
);
2857 if (link_dhcp4_enabled(link
)) {
2858 r
= dhcp4_set_promote_secondaries(link
);
2862 r
= dhcp4_configure(link
);
2867 if (link_dhcp4_server_enabled(link
)) {
2868 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2872 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2877 if (link_dhcp6_enabled(link
) ||
2878 link_ipv6_accept_ra_enabled(link
)) {
2879 r
= dhcp6_configure(link
);
2884 if (link_ipv6_accept_ra_enabled(link
)) {
2885 r
= ndisc_configure(link
);
2890 if (link_radv_enabled(link
)) {
2891 r
= radv_configure(link
);
2896 if (link_lldp_rx_enabled(link
)) {
2897 r
= sd_lldp_new(&link
->lldp
);
2901 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2905 r
= sd_lldp_match_capabilities(link
->lldp
,
2906 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2907 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2908 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2912 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2916 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2920 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2924 r
= link_update_lldp(link
);
2929 if (link
->network
->mtu
> 0) {
2930 r
= link_set_mtu(link
, link
->network
->mtu
);
2935 return link_configure_after_setting_mtu(link
);
2938 static int link_configure_after_setting_mtu(Link
*link
) {
2942 assert(link
->network
);
2943 assert(link
->state
== LINK_STATE_PENDING
);
2945 if (link
->setting_mtu
)
2948 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2949 r
= link_acquire_conf(link
);
2954 return link_enter_join_netdev(link
);
2957 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2960 if (duid
->raw_data_len
> 0)
2963 if (duid
->type
!= DUID_TYPE_UUID
)
2966 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2967 duid
->raw_data_len
= sizeof(sd_id128_t
);
2972 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2973 Manager
*manager
= userdata
;
2974 const sd_bus_error
*e
;
2984 e
= sd_bus_message_get_error(m
);
2986 log_error_errno(sd_bus_error_get_errno(e
),
2987 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2992 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2996 if (sz
!= sizeof(sd_id128_t
)) {
2997 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3001 memcpy(&manager
->product_uuid
, a
, sz
);
3002 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3003 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3005 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3008 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3009 r
= link_configure(link
);
3011 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3014 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3016 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3017 * even if the method fails. */
3018 manager
->has_product_uuid
= true;
3023 static bool link_requires_uuid(Link
*link
) {
3027 assert(link
->manager
);
3028 assert(link
->network
);
3030 duid
= link_get_duid(link
);
3031 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3034 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3037 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3043 static int link_configure_duid(Link
*link
) {
3049 assert(link
->manager
);
3050 assert(link
->network
);
3053 duid
= link_get_duid(link
);
3055 if (!link_requires_uuid(link
))
3058 if (m
->has_product_uuid
) {
3059 (void) duid_set_uuid(duid
, m
->product_uuid
);
3063 if (!m
->links_requesting_uuid
) {
3064 r
= manager_request_product_uuid(m
, link
);
3069 log_link_warning_errno(link
, r
,
3070 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3074 r
= set_put(m
->links_requesting_uuid
, link
);
3078 r
= set_put(m
->duids_requesting_uuid
, duid
);
3086 static int link_initialized_and_synced(Link
*link
) {
3091 assert(link
->ifname
);
3092 assert(link
->manager
);
3094 if (link
->state
!= LINK_STATE_PENDING
)
3097 log_link_debug(link
, "Link state is up-to-date");
3099 r
= link_new_bound_by_list(link
);
3103 r
= link_handle_bound_by_list(link
);
3107 if (!link
->network
) {
3108 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3109 &link
->mac
, &network
);
3111 link_enter_unmanaged(link
);
3113 } else if (r
== 0 && network
->unmanaged
) {
3114 link_enter_unmanaged(link
);
3119 if (link
->flags
& IFF_LOOPBACK
) {
3120 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3121 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3123 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3124 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3126 if (network
->dhcp_server
)
3127 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3130 r
= network_apply(network
, link
);
3135 r
= link_new_bound_to_list(link
);
3139 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3140 * link_configure() is called later asynchronously. */
3141 r
= link_configure_duid(link
);
3145 r
= link_configure(link
);
3152 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3153 (void) link_initialized_and_synced(link
);
3157 int link_initialized(Link
*link
, sd_device
*device
) {
3158 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3162 assert(link
->manager
);
3163 assert(link
->manager
->rtnl
);
3166 if (link
->state
!= LINK_STATE_PENDING
)
3169 if (link
->sd_device
)
3172 log_link_debug(link
, "udev initialized link");
3174 link
->sd_device
= sd_device_ref(device
);
3176 /* udev has initialized the link, but we don't know if we have yet
3177 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3178 * when it returns we know that the pending NEWLINKs have already been
3179 * processed and that we are up-to-date */
3181 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3186 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3187 link_netlink_destroy_callback
, link
);
3196 static int link_load(Link
*link
) {
3197 _cleanup_free_
char *network_file
= NULL
,
3200 *dhcp4_address
= NULL
,
3201 *ipv4ll_address
= NULL
;
3202 union in_addr_union address
;
3203 union in_addr_union route_dst
;
3209 r
= parse_env_file(NULL
, link
->state_file
,
3210 "NETWORK_FILE", &network_file
,
3211 "ADDRESSES", &addresses
,
3213 "DHCP4_ADDRESS", &dhcp4_address
,
3214 "IPV4LL_ADDRESS", &ipv4ll_address
);
3215 if (r
< 0 && r
!= -ENOENT
)
3216 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3223 suffix
= strrchr(network_file
, '.');
3225 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3226 goto network_file_fail
;
3230 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3232 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3233 goto network_file_fail
;
3236 r
= network_apply(network
, link
);
3238 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3247 _cleanup_free_
char *address_str
= NULL
;
3248 char *prefixlen_str
;
3250 unsigned char prefixlen
;
3252 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3254 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3260 prefixlen_str
= strchr(address_str
, '/');
3261 if (!prefixlen_str
) {
3262 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3266 *prefixlen_str
++ = '\0';
3268 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3270 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3274 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3276 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3280 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3282 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3291 _cleanup_free_
char *route_str
= NULL
;
3292 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3294 char *prefixlen_str
;
3296 unsigned char prefixlen
, tos
, table
;
3299 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3301 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3307 prefixlen_str
= strchr(route_str
, '/');
3308 if (!prefixlen_str
) {
3309 log_link_debug(link
, "Failed to parse route %s", route_str
);
3313 *prefixlen_str
++ = '\0';
3315 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3317 log_link_debug(link
,
3318 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3323 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3325 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3329 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3331 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3333 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3334 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3335 0, route_expire_handler
, route
);
3337 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3340 route
->lifetime
= lifetime
;
3341 sd_event_source_unref(route
->expire
);
3342 route
->expire
= TAKE_PTR(expire
);
3346 if (dhcp4_address
) {
3347 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3349 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3350 goto dhcp4_address_fail
;
3353 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3355 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3357 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3359 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3364 if (ipv4ll_address
) {
3365 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3367 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3368 goto ipv4ll_address_fail
;
3371 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3373 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3375 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3377 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3380 ipv4ll_address_fail
:
3385 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3386 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3387 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3396 r
= link_new(m
, message
, ret
);
3402 log_link_debug(link
, "Link %d added", link
->ifindex
);
3404 r
= link_load(link
);
3408 if (detect_container() <= 0) {
3409 /* not in a container, udev will be around */
3410 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3411 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3413 log_link_warning_errno(link
, r
, "Could not find device: %m");
3417 r
= sd_device_get_is_initialized(device
);
3419 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3424 log_link_debug(link
, "link pending udev initialization...");
3428 r
= link_initialized(link
, device
);
3432 r
= link_initialized_and_synced(link
);
3439 link_enter_failed(link
);
3443 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3448 log_link_info(link
, "Gained IPv6LL");
3450 link
->ipv6ll_address
= *address
;
3451 link_check_ready(link
);
3453 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3454 r
= link_acquire_ipv6_conf(link
);
3456 link_enter_failed(link
);
3464 static int link_carrier_gained(Link
*link
) {
3469 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3470 r
= link_acquire_conf(link
);
3472 link_enter_failed(link
);
3476 r
= link_request_set_addresses(link
);
3481 r
= link_handle_bound_by_list(link
);
3488 static int link_carrier_lost(Link
*link
) {
3493 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3494 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3495 * configuration and stop the clients as well. */
3496 if (link
->setting_mtu
)
3499 r
= link_stop_clients(link
);
3501 link_enter_failed(link
);
3505 if (link_dhcp4_server_enabled(link
))
3506 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3508 r
= link_drop_config(link
);
3512 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3513 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3514 r
= link_drop_foreign_config(link
);
3519 r
= link_handle_bound_by_list(link
);
3526 int link_carrier_reset(Link
*link
) {
3531 if (link_has_carrier(link
)) {
3532 r
= link_carrier_lost(link
);
3536 r
= link_carrier_gained(link
);
3540 log_link_info(link
, "Reset carrier");
3546 int link_update(Link
*link
, sd_netlink_message
*m
) {
3547 struct ether_addr mac
;
3550 bool had_carrier
, carrier_gained
, carrier_lost
;
3554 assert(link
->ifname
);
3557 if (link
->state
== LINK_STATE_LINGER
) {
3558 log_link_info(link
, "Link readded");
3559 link_set_state(link
, LINK_STATE_CONFIGURING
);
3561 r
= link_new_carrier_maps(link
);
3566 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3567 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3568 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3570 if (link
->state
== LINK_STATE_PENDING
) {
3571 r
= free_and_strdup(&link
->ifname
, ifname
);
3575 Manager
*manager
= link
->manager
;
3578 r
= link_add(manager
, m
, &link
);
3584 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3585 if (r
>= 0 && mtu
> 0) {
3587 if (link
->original_mtu
== 0) {
3588 link
->original_mtu
= mtu
;
3589 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3592 if (link
->dhcp_client
) {
3593 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3596 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3600 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3602 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3606 /* The kernel may broadcast NEWLINK messages without the MAC address
3607 set, simply ignore them. */
3608 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3610 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3613 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3616 log_link_debug(link
, "MAC address: "
3617 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3618 mac
.ether_addr_octet
[0],
3619 mac
.ether_addr_octet
[1],
3620 mac
.ether_addr_octet
[2],
3621 mac
.ether_addr_octet
[3],
3622 mac
.ether_addr_octet
[4],
3623 mac
.ether_addr_octet
[5]);
3626 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3628 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3631 if (link
->dhcp_client
) {
3632 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3633 (const uint8_t *) &link
->mac
,
3637 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3639 r
= dhcp4_set_client_identifier(link
);
3644 if (link
->dhcp6_client
) {
3645 const DUID
* duid
= link_get_duid(link
);
3647 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3648 (const uint8_t *) &link
->mac
,
3652 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3654 if (link
->network
->iaid_set
) {
3655 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3656 link
->network
->iaid
);
3658 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3661 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3663 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3664 duid
->raw_data_len
);
3666 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3670 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3672 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3676 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3678 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3683 had_carrier
= link_has_carrier(link
);
3685 r
= link_update_flags(link
, m
);
3689 r
= link_update_lldp(link
);
3693 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3694 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3696 if (carrier_gained
) {
3697 log_link_info(link
, "Gained carrier");
3699 r
= link_carrier_gained(link
);
3702 } else if (carrier_lost
) {
3703 log_link_info(link
, "Lost carrier");
3705 r
= link_carrier_lost(link
);
3713 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3721 if (hashmap_isempty(h
))
3725 HASHMAP_FOREACH(link
, h
, i
) {
3729 fprintf(f
, "%i", link
->ifindex
);
3736 int link_save(Link
*link
) {
3737 _cleanup_free_
char *temp_path
= NULL
;
3738 _cleanup_fclose_
FILE *f
= NULL
;
3739 const char *admin_state
, *oper_state
;
3746 assert(link
->state_file
);
3747 assert(link
->lease_file
);
3748 assert(link
->manager
);
3750 if (link
->state
== LINK_STATE_LINGER
) {
3751 unlink(link
->state_file
);
3755 link_lldp_save(link
);
3757 admin_state
= link_state_to_string(link
->state
);
3758 assert(admin_state
);
3760 oper_state
= link_operstate_to_string(link
->operstate
);
3763 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3767 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3768 (void) fchmod(fileno(f
), 0644);
3771 "# This is private data. Do not parse.\n"
3774 admin_state
, oper_state
);
3776 if (link
->network
) {
3778 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3779 const char *dhcp_domainname
= NULL
;
3780 char **dhcp6_domains
= NULL
;
3781 char **dhcp_domains
= NULL
;
3784 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3785 yes_no(link
->network
->required_for_online
));
3787 if (link
->dhcp6_client
) {
3788 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3789 if (r
< 0 && r
!= -ENOMSG
)
3790 log_link_debug(link
, "No DHCPv6 lease");
3793 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3798 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3799 _cleanup_free_
char *b
= NULL
;
3801 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3802 &link
->network
->dns
[j
].address
, &b
);
3804 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3814 if (link
->network
->dhcp_use_dns
&&
3816 const struct in_addr
*addresses
;
3818 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3822 serialize_in_addrs(f
, addresses
, r
);
3827 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3828 struct in6_addr
*in6_addrs
;
3830 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3834 serialize_in6_addrs(f
, in6_addrs
, r
);
3839 /* Make sure to flush out old entries before we use the NDISC data */
3842 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3845 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3849 serialize_in6_addrs(f
, &dd
->address
, 1);
3858 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3860 if (link
->network
->dhcp_use_ntp
&&
3862 const struct in_addr
*addresses
;
3864 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3868 serialize_in_addrs(f
, addresses
, r
);
3873 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3874 struct in6_addr
*in6_addrs
;
3877 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3882 serialize_in6_addrs(f
, in6_addrs
, r
);
3886 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3888 fputstrv(f
, hosts
, NULL
, &space
);
3893 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3894 if (link
->dhcp_lease
) {
3895 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3896 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3899 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3902 fputs("DOMAINS=", f
);
3904 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3906 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3909 if (dhcp_domainname
)
3910 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3912 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3914 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3916 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3917 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3922 fputs("ROUTE_DOMAINS=", f
);
3924 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3926 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3929 if (dhcp_domainname
)
3930 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3932 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3934 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3936 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3937 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3942 fprintf(f
, "LLMNR=%s\n",
3943 resolve_support_to_string(link
->network
->llmnr
));
3944 fprintf(f
, "MDNS=%s\n",
3945 resolve_support_to_string(link
->network
->mdns
));
3946 if (link
->network
->dns_default_route
>= 0)
3947 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
3949 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3950 fprintf(f
, "DNS_OVER_TLS=%s\n",
3951 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3953 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3954 fprintf(f
, "DNSSEC=%s\n",
3955 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3957 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3960 fputs("DNSSEC_NTA=", f
);
3962 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3963 fputs_with_space(f
, n
, NULL
, &space
);
3967 fputs("ADDRESSES=", f
);
3969 SET_FOREACH(a
, link
->addresses
, i
) {
3970 _cleanup_free_
char *address_str
= NULL
;
3972 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3976 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3981 fputs("ROUTES=", f
);
3983 SET_FOREACH(route
, link
->routes
, i
) {
3984 _cleanup_free_
char *route_str
= NULL
;
3986 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3990 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3991 space
? " " : "", route_str
,
3992 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3999 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4000 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4002 if (link
->dhcp_lease
) {
4003 struct in_addr address
;
4004 const char *tz
= NULL
;
4006 assert(link
->network
);
4008 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4010 fprintf(f
, "TIMEZONE=%s\n", tz
);
4012 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4014 fputs("DHCP4_ADDRESS=", f
);
4015 serialize_in_addrs(f
, &address
, 1);
4019 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4027 unlink(link
->lease_file
);
4030 struct in_addr address
;
4032 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4034 fputs("IPV4LL_ADDRESS=", f
);
4035 serialize_in_addrs(f
, &address
, 1);
4040 r
= fflush_and_check(f
);
4044 if (rename(temp_path
, link
->state_file
) < 0) {
4052 (void) unlink(link
->state_file
);
4054 (void) unlink(temp_path
);
4056 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4059 /* The serialized state in /run is no longer up-to-date. */
4060 void link_dirty(Link
*link
) {
4065 /* mark manager dirty as link is dirty */
4066 manager_dirty(link
->manager
);
4068 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4070 /* allocation errors are ignored */
4073 r
= set_put(link
->manager
->dirty_links
, link
);
4075 /* don't take another ref if the link was already dirty */
4081 /* The serialized state in /run is up-to-date */
4082 void link_clean(Link
*link
) {
4084 assert(link
->manager
);
4086 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4089 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4090 [LINK_STATE_PENDING
] = "pending",
4091 [LINK_STATE_CONFIGURING
] = "configuring",
4092 [LINK_STATE_CONFIGURED
] = "configured",
4093 [LINK_STATE_UNMANAGED
] = "unmanaged",
4094 [LINK_STATE_FAILED
] = "failed",
4095 [LINK_STATE_LINGER
] = "linger",
4098 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4100 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4101 [LINK_OPERSTATE_OFF
] = "off",
4102 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4103 [LINK_OPERSTATE_DORMANT
] = "dormant",
4104 [LINK_OPERSTATE_CARRIER
] = "carrier",
4105 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4106 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4109 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);