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 "netlink-util.h"
17 #include "network-internal.h"
18 #include "networkd-ipv6-proxy-ndp.h"
19 #include "networkd-lldp-tx.h"
20 #include "networkd-manager.h"
21 #include "networkd-ndisc.h"
22 #include "networkd-radv.h"
23 #include "networkd-routing-policy-rule.h"
25 #include "socket-util.h"
26 #include "stdio-util.h"
27 #include "string-table.h"
29 #include "tmpfile-util.h"
33 DUID
* link_get_duid(Link
*link
) {
34 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
35 return &link
->network
->duid
;
37 return &link
->manager
->duid
;
40 static bool link_dhcp6_enabled(Link
*link
) {
43 if (!socket_ipv6_is_supported())
46 if (link
->flags
& IFF_LOOPBACK
)
52 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
55 static bool link_dhcp4_enabled(Link
*link
) {
58 if (link
->flags
& IFF_LOOPBACK
)
64 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
67 static bool link_dhcp4_server_enabled(Link
*link
) {
70 if (link
->flags
& IFF_LOOPBACK
)
76 return link
->network
->dhcp_server
;
79 static bool link_ipv4ll_enabled(Link
*link
) {
82 if (link
->flags
& IFF_LOOPBACK
)
88 if (streq_ptr(link
->kind
, "wireguard"))
91 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
94 static bool link_ipv6ll_enabled(Link
*link
) {
97 if (!socket_ipv6_is_supported())
100 if (link
->flags
& IFF_LOOPBACK
)
106 if (streq_ptr(link
->kind
, "wireguard"))
109 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
112 static bool link_ipv6_enabled(Link
*link
) {
115 if (!socket_ipv6_is_supported())
118 if (link
->network
->bridge
)
121 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
122 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
125 static bool link_radv_enabled(Link
*link
) {
128 if (!link_ipv6ll_enabled(link
))
131 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
134 static bool link_lldp_rx_enabled(Link
*link
) {
137 if (link
->flags
& IFF_LOOPBACK
)
140 if (link
->iftype
!= ARPHRD_ETHER
)
146 /* LLDP should be handled on bridge slaves as those have a direct
147 * connection to their peers not on the bridge master. Linux doesn't
148 * even (by default) forward lldp packets to the bridge master.*/
149 if (streq_ptr("bridge", link
->kind
))
152 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
155 static bool link_lldp_emit_enabled(Link
*link
) {
158 if (link
->flags
& IFF_LOOPBACK
)
161 if (link
->iftype
!= ARPHRD_ETHER
)
167 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
170 static bool link_ipv4_forward_enabled(Link
*link
) {
173 if (link
->flags
& IFF_LOOPBACK
)
179 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
182 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
185 static bool link_ipv6_forward_enabled(Link
*link
) {
188 if (!socket_ipv6_is_supported())
191 if (link
->flags
& IFF_LOOPBACK
)
197 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
200 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
203 static bool link_proxy_arp_enabled(Link
*link
) {
206 if (link
->flags
& IFF_LOOPBACK
)
212 if (link
->network
->proxy_arp
< 0)
218 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
221 if (!socket_ipv6_is_supported())
224 if (link
->flags
& IFF_LOOPBACK
)
230 if (!link_ipv6ll_enabled(link
))
233 /* If unset use system default (enabled if local forwarding is disabled.
234 * disabled if local forwarding is enabled).
235 * If set, ignore or enforce RA independent of local forwarding state.
237 if (link
->network
->ipv6_accept_ra
< 0)
238 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
239 return !link_ipv6_forward_enabled(link
);
240 else if (link
->network
->ipv6_accept_ra
> 0)
241 /* accept RA even if ip_forward is enabled */
248 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
251 if (!socket_ipv6_is_supported())
252 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
254 if (link
->flags
& IFF_LOOPBACK
)
255 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
258 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
260 return link
->network
->ipv6_privacy_extensions
;
263 static int link_enable_ipv6(Link
*link
) {
264 const char *p
= NULL
;
268 if (link
->flags
& IFF_LOOPBACK
)
271 disabled
= !link_ipv6_enabled(link
);
273 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
275 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
277 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
278 enable_disable(!disabled
), link
->ifname
);
280 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
285 void link_update_operstate(Link
*link
) {
286 LinkOperationalState operstate
;
289 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
290 operstate
= LINK_OPERSTATE_DORMANT
;
291 else if (link_has_carrier(link
)) {
293 uint8_t scope
= RT_SCOPE_NOWHERE
;
296 /* if we have carrier, check what addresses we have */
297 SET_FOREACH(address
, link
->addresses
, i
) {
298 if (!address_is_ready(address
))
301 if (address
->scope
< scope
)
302 scope
= address
->scope
;
305 /* for operstate we also take foreign addresses into account */
306 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
307 if (!address_is_ready(address
))
310 if (address
->scope
< scope
)
311 scope
= address
->scope
;
314 if (scope
< RT_SCOPE_SITE
)
315 /* universally accessible addresses found */
316 operstate
= LINK_OPERSTATE_ROUTABLE
;
317 else if (scope
< RT_SCOPE_HOST
)
318 /* only link or site local addresses found */
319 operstate
= LINK_OPERSTATE_DEGRADED
;
321 /* no useful addresses found */
322 operstate
= LINK_OPERSTATE_CARRIER
;
323 } else if (link
->flags
& IFF_UP
)
324 operstate
= LINK_OPERSTATE_NO_CARRIER
;
326 operstate
= LINK_OPERSTATE_OFF
;
328 if (link
->operstate
!= operstate
) {
329 link
->operstate
= operstate
;
330 link_send_changed(link
, "OperationalState", NULL
);
335 #define FLAG_STRING(string, flag, old, new) \
336 (((old ^ new) & flag) \
337 ? ((old & flag) ? (" -" string) : (" +" string)) \
340 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
341 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
347 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
349 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
351 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
353 /* if we got a message without operstate, take it to mean
354 the state was unchanged */
355 operstate
= link
->kernel_operstate
;
357 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
360 if (link
->flags
!= flags
) {
361 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",
362 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
363 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
364 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
365 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
366 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
367 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
368 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
369 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
370 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
371 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
372 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
373 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
374 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
375 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
376 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
377 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
378 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
379 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
380 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
382 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
383 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
384 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
385 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
386 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
387 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
388 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
389 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
391 /* link flags are currently at most 18 bits, let's align to
393 if (unknown_flags_added
)
395 "Unknown link flags gained: %#.5x (ignoring)",
396 unknown_flags_added
);
398 if (unknown_flags_removed
)
400 "Unknown link flags lost: %#.5x (ignoring)",
401 unknown_flags_removed
);
405 link
->kernel_operstate
= operstate
;
407 link_update_operstate(link
);
412 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
414 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
415 _cleanup_(link_unrefp
) Link
*link
= NULL
;
417 const char *ifname
, *kind
= NULL
;
419 unsigned short iftype
;
425 /* check for link kind */
426 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
428 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
429 r
= sd_netlink_message_exit_container(message
);
434 r
= sd_netlink_message_get_type(message
, &type
);
437 else if (type
!= RTM_NEWLINK
)
440 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
443 else if (ifindex
<= 0)
446 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
450 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
461 .state
= LINK_STATE_PENDING
,
462 .rtnl_extended_attrs
= true,
467 link
->ifname
= strdup(ifname
);
472 link
->kind
= strdup(kind
);
477 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
479 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
481 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
483 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
485 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
488 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
491 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
494 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
498 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
502 r
= link_update_flags(link
, message
);
506 *ret
= TAKE_PTR(link
);
511 static void link_detach_from_manager(Link
*link
) {
512 if (!link
|| !link
->manager
)
515 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
516 set_remove(link
->manager
->links_requesting_uuid
, link
);
519 link
->manager
= NULL
;
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_SETTING_ROUTES
)
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
->static_routes_configured
)
746 if (!link
->routing_policy_rules_configured
)
749 if (link_ipv4ll_enabled(link
))
750 if (!link
->ipv4ll_address
||
754 if (!link
->network
->bridge
) {
756 if (link_ipv6ll_enabled(link
))
757 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
760 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
761 !link
->dhcp4_configured
) ||
762 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
763 !link
->dhcp6_configured
) ||
764 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
765 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
768 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
772 SET_FOREACH(a
, link
->addresses
, i
)
773 if (!address_is_ready(a
))
776 if (link
->state
!= LINK_STATE_CONFIGURED
)
777 link_enter_configured(link
);
782 static int link_set_routing_policy_rule(Link
*link
) {
783 RoutingPolicyRule
*rule
, *rrule
= NULL
;
787 assert(link
->network
);
789 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
790 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
791 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
792 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
794 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
798 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
800 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
801 link_enter_failed(link
);
805 link
->routing_policy_rule_messages
++;
808 routing_policy_rule_purge(link
->manager
, link
);
809 if (link
->routing_policy_rule_messages
== 0) {
810 link
->routing_policy_rules_configured
= true;
811 link_check_ready(link
);
813 log_link_debug(link
, "Setting routing policy rules");
818 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
822 assert(link
->route_messages
> 0);
823 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
824 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
827 link
->route_messages
--;
829 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
832 r
= sd_netlink_message_get_errno(m
);
833 if (r
< 0 && r
!= -EEXIST
)
834 log_link_warning_errno(link
, r
, "Could not set route: %m");
836 if (link
->route_messages
== 0) {
837 log_link_debug(link
, "Routes set");
838 link
->static_routes_configured
= true;
839 link_check_ready(link
);
845 static int link_enter_set_routes(Link
*link
) {
847 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
848 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
855 assert(link
->network
);
856 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
858 (void) link_set_routing_policy_rule(link
);
860 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
862 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
863 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
864 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
866 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
869 r
= route_configure(rt
, link
, route_handler
);
871 log_link_warning_errno(link
, r
, "Could not set routes: %m");
872 link_enter_failed(link
);
876 link
->route_messages
++;
879 if (link
->route_messages
== 0) {
880 link
->static_routes_configured
= true;
881 link_check_ready(link
);
883 log_link_debug(link
, "Setting routes");
888 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
894 assert(link
->ifname
);
895 assert(link
->address_messages
> 0);
896 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
897 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
899 link
->address_messages
--;
901 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
904 r
= sd_netlink_message_get_errno(m
);
905 if (r
< 0 && r
!= -EEXIST
)
906 log_link_warning_errno(link
, r
, "could not set address: %m");
908 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
910 if (link
->address_messages
== 0) {
911 log_link_debug(link
, "Addresses set");
912 link_enter_set_routes(link
);
918 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
919 _cleanup_free_
struct in_addr
*addresses
= NULL
;
920 size_t n_addresses
= 0, n_allocated
= 0;
923 log_debug("Copying DNS server information from %s", link
->ifname
);
928 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
931 /* Only look for IPv4 addresses */
932 if (link
->network
->dns
[i
].family
!= AF_INET
)
935 ia
= link
->network
->dns
[i
].address
.in
;
937 /* Never propagate obviously borked data */
938 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
941 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
944 addresses
[n_addresses
++] = ia
;
947 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
948 const struct in_addr
*da
= NULL
;
951 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
954 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
957 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
962 if (n_addresses
<= 0)
965 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
968 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
969 _cleanup_free_
struct in_addr
*addresses
= NULL
;
970 size_t n_addresses
= 0, n_allocated
= 0;
976 log_debug("Copying NTP server information from %s", link
->ifname
);
978 STRV_FOREACH(a
, link
->network
->ntp
) {
981 /* Only look for IPv4 addresses */
982 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
985 /* Never propagate obviously borked data */
986 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
989 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
992 addresses
[n_addresses
++] = ia
;
995 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
996 const struct in_addr
*da
= NULL
;
999 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1002 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1005 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1010 if (n_addresses
<= 0)
1013 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1016 static int link_set_bridge_fdb(Link
*link
) {
1017 FdbEntry
*fdb_entry
;
1020 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1021 r
= fdb_entry_configure(link
, fdb_entry
);
1023 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1029 static int link_enter_set_addresses(Link
*link
) {
1030 AddressLabel
*label
;
1035 assert(link
->network
);
1036 assert(link
->state
!= _LINK_STATE_INVALID
);
1038 r
= link_set_bridge_fdb(link
);
1042 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1044 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1045 r
= address_configure(ad
, link
, address_handler
, false);
1047 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1048 link_enter_failed(link
);
1052 link
->address_messages
++;
1055 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1056 r
= address_label_configure(label
, link
, NULL
, false);
1058 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1059 link_enter_failed(link
);
1063 link
->address_label_messages
++;
1066 /* now that we can figure out a default address for the dhcp server,
1068 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1070 Link
*uplink
= NULL
;
1071 bool acquired_uplink
= false;
1073 address
= link_find_dhcp_server_address(link
);
1075 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1076 link_enter_failed(link
);
1080 /* use the server address' subnet as the pool */
1081 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1082 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1087 r = sd_dhcp_server_set_router(link->dhcp_server,
1088 &main_address->in_addr.in);
1093 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1094 r
= sd_dhcp_server_set_max_lease_time(
1096 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1101 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1102 r
= sd_dhcp_server_set_default_lease_time(
1104 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1109 if (link
->network
->dhcp_server_emit_dns
) {
1111 if (link
->network
->n_dhcp_server_dns
> 0)
1112 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1114 uplink
= manager_find_uplink(link
->manager
, link
);
1115 acquired_uplink
= true;
1118 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1121 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1124 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1127 if (link
->network
->dhcp_server_emit_ntp
) {
1129 if (link
->network
->n_dhcp_server_ntp
> 0)
1130 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1132 if (!acquired_uplink
)
1133 uplink
= manager_find_uplink(link
->manager
, link
);
1136 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1139 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1143 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1146 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1148 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1150 if (link
->network
->dhcp_server_emit_timezone
) {
1151 _cleanup_free_
char *buffer
= NULL
;
1152 const char *tz
= NULL
;
1154 if (link
->network
->dhcp_server_timezone
)
1155 tz
= link
->network
->dhcp_server_timezone
;
1157 r
= get_timezone(&buffer
);
1159 log_warning_errno(r
, "Failed to determine timezone: %m");
1165 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1171 r
= sd_dhcp_server_start(link
->dhcp_server
);
1173 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1175 link_enter_failed(link
);
1180 log_link_debug(link
, "Offering DHCPv4 leases");
1183 if (link
->address_messages
== 0)
1184 link_enter_set_routes(link
);
1186 log_link_debug(link
, "Setting addresses");
1191 static int link_set_bridge_vlan(Link
*link
) {
1194 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1196 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1201 static int link_set_proxy_arp(Link
*link
) {
1202 const char *p
= NULL
;
1205 if (!link_proxy_arp_enabled(link
))
1208 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1210 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1212 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1217 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1222 log_link_debug(link
, "Set link");
1224 r
= sd_netlink_message_get_errno(m
);
1225 if (r
< 0 && r
!= -EEXIST
) {
1226 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1227 link_enter_failed(link
);
1233 static int link_configure_after_setting_mtu(Link
*link
);
1235 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1240 assert(link
->ifname
);
1242 link
->setting_mtu
= false;
1244 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1247 r
= sd_netlink_message_get_errno(m
);
1249 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1253 log_link_debug(link
, "Setting MTU done.");
1255 if (link
->state
== LINK_STATE_PENDING
)
1256 (void) link_configure_after_setting_mtu(link
);
1261 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1262 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1266 assert(link
->manager
);
1267 assert(link
->manager
->rtnl
);
1269 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1272 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1274 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1276 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1278 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1279 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1280 (void) link_enable_ipv6(link
);
1282 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1283 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1284 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1286 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1287 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1292 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1294 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1296 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1297 link_netlink_destroy_callback
, link
);
1299 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1302 link
->setting_mtu
= true;
1307 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1312 assert(link
->ifname
);
1314 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1317 r
= sd_netlink_message_get_errno(m
);
1319 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1324 static int link_set_flags(Link
*link
) {
1325 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1326 unsigned ifi_change
= 0;
1327 unsigned ifi_flags
= 0;
1331 assert(link
->manager
);
1332 assert(link
->manager
->rtnl
);
1334 if (link
->flags
& IFF_LOOPBACK
)
1340 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1343 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1345 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1347 if (link
->network
->arp
>= 0) {
1348 ifi_change
|= IFF_NOARP
;
1349 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1352 if (link
->network
->multicast
>= 0) {
1353 ifi_change
|= IFF_MULTICAST
;
1354 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1357 if (link
->network
->allmulticast
>= 0) {
1358 ifi_change
|= IFF_ALLMULTI
;
1359 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1362 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1364 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1366 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1367 link_netlink_destroy_callback
, link
);
1369 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1376 static int link_set_bridge(Link
*link
) {
1377 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1381 assert(link
->network
);
1383 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1385 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1387 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1389 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1391 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1393 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1395 if (link
->network
->use_bpdu
>= 0) {
1396 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1398 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1401 if (link
->network
->hairpin
>= 0) {
1402 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1404 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1407 if (link
->network
->fast_leave
>= 0) {
1408 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1410 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1413 if (link
->network
->allow_port_to_be_root
>= 0) {
1414 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1416 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1420 if (link
->network
->unicast_flood
>= 0) {
1421 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1423 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1427 if (link
->network
->cost
!= 0) {
1428 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1430 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1432 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1433 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1435 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1438 r
= sd_netlink_message_close_container(req
);
1440 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1442 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1443 link_netlink_destroy_callback
, link
);
1445 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1452 static int link_bond_set(Link
*link
) {
1453 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1457 assert(link
->network
);
1459 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1461 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1463 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1465 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1467 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1469 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1471 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1473 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1475 if (link
->network
->active_slave
) {
1476 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1478 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1481 if (link
->network
->primary_slave
) {
1482 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1484 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1487 r
= sd_netlink_message_close_container(req
);
1489 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1491 r
= sd_netlink_message_close_container(req
);
1493 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1495 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1496 link_netlink_destroy_callback
, link
);
1498 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1505 static int link_lldp_save(Link
*link
) {
1506 _cleanup_free_
char *temp_path
= NULL
;
1507 _cleanup_fclose_
FILE *f
= NULL
;
1508 sd_lldp_neighbor
**l
= NULL
;
1512 assert(link
->lldp_file
);
1515 (void) unlink(link
->lldp_file
);
1519 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1523 (void) unlink(link
->lldp_file
);
1529 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1533 fchmod(fileno(f
), 0644);
1535 for (i
= 0; i
< n
; i
++) {
1540 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1545 (void) fwrite(&u
, 1, sizeof(u
), f
);
1546 (void) fwrite(p
, 1, sz
, f
);
1549 r
= fflush_and_check(f
);
1553 if (rename(temp_path
, link
->lldp_file
) < 0) {
1560 (void) unlink(link
->lldp_file
);
1562 (void) unlink(temp_path
);
1564 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1568 for (i
= 0; i
< n
; i
++)
1569 sd_lldp_neighbor_unref(l
[i
]);
1576 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1577 Link
*link
= userdata
;
1582 (void) link_lldp_save(link
);
1584 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1585 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1587 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1589 r
= link_lldp_emit_start(link
);
1591 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1595 static int link_acquire_ipv6_conf(Link
*link
) {
1600 if (link_ipv6_accept_ra_enabled(link
)) {
1601 assert(link
->ndisc
);
1603 log_link_debug(link
, "Discovering IPv6 routers");
1605 r
= sd_ndisc_start(link
->ndisc
);
1606 if (r
< 0 && r
!= -EBUSY
)
1607 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1610 if (link_radv_enabled(link
)) {
1612 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1614 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1616 r
= sd_radv_start(link
->radv
);
1617 if (r
< 0 && r
!= -EBUSY
)
1618 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1621 (void) dhcp6_request_prefix_delegation(link
);
1626 static int link_acquire_ipv4_conf(Link
*link
) {
1630 assert(link
->network
);
1631 assert(link
->manager
);
1632 assert(link
->manager
->event
);
1634 if (link_ipv4ll_enabled(link
)) {
1635 assert(link
->ipv4ll
);
1637 log_link_debug(link
, "Acquiring IPv4 link-local address");
1639 r
= sd_ipv4ll_start(link
->ipv4ll
);
1641 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1644 if (link_dhcp4_enabled(link
)) {
1645 assert(link
->dhcp_client
);
1647 log_link_debug(link
, "Acquiring DHCPv4 lease");
1649 r
= sd_dhcp_client_start(link
->dhcp_client
);
1651 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1657 static int link_acquire_conf(Link
*link
) {
1662 r
= link_acquire_ipv4_conf(link
);
1666 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1667 r
= link_acquire_ipv6_conf(link
);
1672 if (link_lldp_emit_enabled(link
)) {
1673 r
= link_lldp_emit_start(link
);
1675 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1681 bool link_has_carrier(Link
*link
) {
1682 /* see Documentation/networking/operstates.txt in the kernel sources */
1684 if (link
->kernel_operstate
== IF_OPER_UP
)
1687 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1688 /* operstate may not be implemented, so fall back to flags */
1689 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1695 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1700 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1703 r
= sd_netlink_message_get_errno(m
);
1705 /* we warn but don't fail the link, as it may be brought up later */
1706 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1711 int link_up(Link
*link
) {
1712 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1713 uint8_t ipv6ll_mode
;
1717 assert(link
->network
);
1718 assert(link
->manager
);
1719 assert(link
->manager
->rtnl
);
1721 log_link_debug(link
, "Bringing link up");
1723 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1725 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1727 /* set it free if not enslaved with networkd */
1728 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1729 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1731 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1734 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1736 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1738 if (link
->network
->mac
) {
1739 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1741 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1744 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1746 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1748 if (link_ipv6_enabled(link
)) {
1749 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1750 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1752 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1754 if (!link_ipv6ll_enabled(link
))
1755 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1757 const char *p
= NULL
;
1758 _cleanup_free_
char *stable_secret
= NULL
;
1760 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1761 r
= read_one_line_file(p
, &stable_secret
);
1764 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1766 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1768 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1770 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1772 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1773 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1775 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1778 r
= sd_netlink_message_close_container(req
);
1780 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1783 r
= sd_netlink_message_close_container(req
);
1785 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1787 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1788 link_netlink_destroy_callback
, link
);
1790 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1797 static int link_up_can(Link
*link
) {
1798 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1803 log_link_debug(link
, "Bringing CAN link up");
1805 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1807 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1809 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1811 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1813 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1814 link_netlink_destroy_callback
, link
);
1816 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1823 static int link_set_can(Link
*link
) {
1824 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1828 assert(link
->network
);
1829 assert(link
->manager
);
1830 assert(link
->manager
->rtnl
);
1832 log_link_debug(link
, "link_set_can");
1834 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1836 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1838 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1840 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1842 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1844 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1846 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1848 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1850 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1851 struct can_bittiming bt
= {
1852 .bitrate
= link
->network
->can_bitrate
,
1853 .sample_point
= link
->network
->can_sample_point
,
1856 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1857 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1861 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1862 if (link
->network
->can_sample_point
> 0)
1863 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1865 log_link_debug(link
, "Using default sample point");
1867 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1869 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1872 if (link
->network
->can_restart_us
> 0) {
1873 char time_string
[FORMAT_TIMESPAN_MAX
];
1874 uint64_t restart_ms
;
1876 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1879 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1881 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1883 if (restart_ms
> UINT32_MAX
) {
1884 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1888 log_link_debug(link
, "Setting restart = %s", time_string
);
1890 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1892 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1895 r
= sd_netlink_message_close_container(m
);
1897 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1899 r
= sd_netlink_message_close_container(m
);
1901 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1903 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1904 link_netlink_destroy_callback
, link
);
1906 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1910 if (!(link
->flags
& IFF_UP
)) {
1911 r
= link_up_can(link
);
1913 link_enter_failed(link
);
1918 log_link_debug(link
, "link_set_can done");
1923 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1928 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1931 r
= sd_netlink_message_get_errno(m
);
1933 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1935 if (streq_ptr(link
->kind
, "can"))
1941 int link_down(Link
*link
) {
1942 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1946 assert(link
->manager
);
1947 assert(link
->manager
->rtnl
);
1949 log_link_debug(link
, "Bringing link down");
1951 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1952 RTM_SETLINK
, link
->ifindex
);
1954 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1956 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1958 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1960 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
1961 link_netlink_destroy_callback
, link
);
1963 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1970 static int link_handle_bound_to_list(Link
*link
) {
1974 bool required_up
= false;
1975 bool link_is_up
= false;
1979 if (hashmap_isempty(link
->bound_to_links
))
1982 if (link
->flags
& IFF_UP
)
1985 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1986 if (link_has_carrier(l
)) {
1991 if (!required_up
&& link_is_up
) {
1992 r
= link_down(link
);
1995 } else if (required_up
&& !link_is_up
) {
2004 static int link_handle_bound_by_list(Link
*link
) {
2011 if (hashmap_isempty(link
->bound_by_links
))
2014 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2015 r
= link_handle_bound_to_list(l
);
2023 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2029 if (link
== carrier
)
2032 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2035 r
= hashmap_ensure_allocated(h
, NULL
);
2039 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2046 static int link_new_bound_by_list(Link
*link
) {
2051 bool list_updated
= false;
2054 assert(link
->manager
);
2058 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2059 if (!carrier
->network
)
2062 if (strv_isempty(carrier
->network
->bind_carrier
))
2065 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2066 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2070 list_updated
= true;
2077 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2078 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2082 link_dirty(carrier
);
2088 static int link_new_bound_to_list(Link
*link
) {
2093 bool list_updated
= false;
2096 assert(link
->manager
);
2101 if (strv_isempty(link
->network
->bind_carrier
))
2106 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2107 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2108 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2112 list_updated
= true;
2119 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2120 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2124 link_dirty(carrier
);
2130 static int link_new_carrier_maps(Link
*link
) {
2133 r
= link_new_bound_by_list(link
);
2137 r
= link_handle_bound_by_list(link
);
2141 r
= link_new_bound_to_list(link
);
2145 r
= link_handle_bound_to_list(link
);
2152 static void link_free_bound_to_list(Link
*link
) {
2156 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2157 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2159 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2160 link_dirty(bound_to
);
2166 static void link_free_bound_by_list(Link
*link
) {
2170 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2171 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2173 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2174 link_dirty(bound_by
);
2175 link_handle_bound_to_list(bound_by
);
2182 static void link_free_carrier_maps(Link
*link
) {
2183 bool list_updated
= false;
2187 if (!hashmap_isempty(link
->bound_to_links
)) {
2188 link_free_bound_to_list(link
);
2189 list_updated
= true;
2192 if (!hashmap_isempty(link
->bound_by_links
)) {
2193 link_free_bound_by_list(link
);
2194 list_updated
= true;
2203 void link_drop(Link
*link
) {
2204 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2207 link_set_state(link
, LINK_STATE_LINGER
);
2209 link_free_carrier_maps(link
);
2211 log_link_debug(link
, "Link removed");
2213 (void) unlink(link
->state_file
);
2215 link_detach_from_manager(link
);
2222 static int link_joined(Link
*link
) {
2226 assert(link
->network
);
2228 if (!hashmap_isempty(link
->bound_to_links
)) {
2229 r
= link_handle_bound_to_list(link
);
2232 } else if (!(link
->flags
& IFF_UP
)) {
2235 link_enter_failed(link
);
2240 if (link
->network
->bridge
) {
2241 r
= link_set_bridge(link
);
2243 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2246 if (link
->network
->bond
) {
2247 r
= link_bond_set(link
);
2249 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2252 if (link
->network
->use_br_vlan
&&
2253 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2254 r
= link_set_bridge_vlan(link
);
2256 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2259 /* Skip setting up addresses until it gets carrier,
2260 or it would try to set addresses twice,
2261 which is bad for non-idempotent steps. */
2262 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2265 return link_enter_set_addresses(link
);
2268 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2272 assert(link
->network
);
2276 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2279 r
= sd_netlink_message_get_errno(m
);
2280 if (r
< 0 && r
!= -EEXIST
) {
2281 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2282 link_enter_failed(link
);
2285 log_link_debug(link
, "Joined netdev");
2287 if (link
->enslaving
<= 0)
2293 static int link_enter_join_netdev(Link
*link
) {
2299 assert(link
->network
);
2300 assert(link
->state
== LINK_STATE_PENDING
);
2302 link_set_state(link
, LINK_STATE_ENSLAVING
);
2306 if (!link
->network
->bridge
&&
2307 !link
->network
->bond
&&
2308 !link
->network
->vrf
&&
2309 hashmap_isempty(link
->network
->stacked_netdevs
))
2310 return link_joined(link
);
2312 if (link
->network
->bond
) {
2313 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2314 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2315 return link_joined(link
);
2317 log_struct(LOG_DEBUG
,
2318 LOG_LINK_INTERFACE(link
),
2319 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2320 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2322 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2324 log_struct_errno(LOG_WARNING
, r
,
2325 LOG_LINK_INTERFACE(link
),
2326 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2327 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2328 link_enter_failed(link
);
2335 if (link
->network
->bridge
) {
2336 log_struct(LOG_DEBUG
,
2337 LOG_LINK_INTERFACE(link
),
2338 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2339 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2341 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2343 log_struct_errno(LOG_WARNING
, r
,
2344 LOG_LINK_INTERFACE(link
),
2345 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2346 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2347 link_enter_failed(link
);
2354 if (link
->network
->vrf
) {
2355 log_struct(LOG_DEBUG
,
2356 LOG_LINK_INTERFACE(link
),
2357 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2358 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2360 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2362 log_struct_errno(LOG_WARNING
, r
,
2363 LOG_LINK_INTERFACE(link
),
2364 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2365 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2366 link_enter_failed(link
);
2373 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2375 if (netdev
->ifindex
> 0) {
2380 log_struct(LOG_DEBUG
,
2381 LOG_LINK_INTERFACE(link
),
2382 LOG_NETDEV_INTERFACE(netdev
),
2383 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2385 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2387 log_struct_errno(LOG_WARNING
, r
,
2388 LOG_LINK_INTERFACE(link
),
2389 LOG_NETDEV_INTERFACE(netdev
),
2390 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2391 link_enter_failed(link
);
2401 static int link_set_ipv4_forward(Link
*link
) {
2404 if (!link_ipv4_forward_enabled(link
))
2407 /* We propagate the forwarding flag from one interface to the
2408 * global setting one way. This means: as long as at least one
2409 * interface was configured at any time that had IP forwarding
2410 * enabled the setting will stay on for good. We do this
2411 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2412 * somewhat in sync (see below). */
2414 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2416 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2421 static int link_set_ipv6_forward(Link
*link
) {
2424 if (!link_ipv6_forward_enabled(link
))
2427 /* On Linux, the IPv6 stack does not know a per-interface
2428 * packet forwarding setting: either packet forwarding is on
2429 * for all, or off for all. We hence don't bother with a
2430 * per-interface setting, but simply propagate the interface
2431 * flag, if it is set, to the global flag, one-way. Note that
2432 * while IPv4 would allow a per-interface flag, we expose the
2433 * same behaviour there and also propagate the setting from
2434 * one to all, to keep things simple (see above). */
2436 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2438 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2443 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2444 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2445 IPv6PrivacyExtensions s
;
2446 const char *p
= NULL
;
2449 s
= link_ipv6_privacy_extensions(link
);
2453 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2454 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2456 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2458 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2463 static int link_set_ipv6_accept_ra(Link
*link
) {
2464 const char *p
= NULL
;
2467 /* Make this a NOP if IPv6 is not available */
2468 if (!socket_ipv6_is_supported())
2471 if (link
->flags
& IFF_LOOPBACK
)
2477 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2479 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2480 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2482 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2487 static int link_set_ipv6_dad_transmits(Link
*link
) {
2488 char buf
[DECIMAL_STR_MAX(int) + 1];
2489 const char *p
= NULL
;
2492 /* Make this a NOP if IPv6 is not available */
2493 if (!socket_ipv6_is_supported())
2496 if (link
->flags
& IFF_LOOPBACK
)
2502 if (link
->network
->ipv6_dad_transmits
< 0)
2505 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2506 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2508 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2510 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2515 static int link_set_ipv6_hop_limit(Link
*link
) {
2516 char buf
[DECIMAL_STR_MAX(int) + 1];
2517 const char *p
= NULL
;
2520 /* Make this a NOP if IPv6 is not available */
2521 if (!socket_ipv6_is_supported())
2524 if (link
->flags
& IFF_LOOPBACK
)
2530 if (link
->network
->ipv6_hop_limit
< 0)
2533 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2534 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2536 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2538 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2543 static int link_set_ipv6_mtu(Link
*link
) {
2544 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2545 const char *p
= NULL
;
2548 /* Make this a NOP if IPv6 is not available */
2549 if (!socket_ipv6_is_supported())
2552 if (link
->flags
& IFF_LOOPBACK
)
2555 if (link
->network
->ipv6_mtu
== 0)
2558 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2560 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2562 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2564 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2569 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2570 Address
*net_address
;
2578 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2579 if (address_equal(net_address
, address
))
2585 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2594 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2595 if (route_equal(net_route
, route
))
2601 static int link_drop_foreign_config(Link
*link
) {
2607 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2608 /* we consider IPv6LL addresses to be managed by the kernel */
2609 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2612 if (link_is_static_address_configured(link
, address
)) {
2613 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2615 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2617 r
= address_remove(address
, link
, NULL
);
2623 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2624 /* do not touch routes managed by the kernel */
2625 if (route
->protocol
== RTPROT_KERNEL
)
2628 if (link_is_static_route_configured(link
, route
)) {
2629 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2633 r
= route_remove(route
, link
, NULL
);
2642 static int link_drop_config(Link
*link
) {
2643 Address
*address
, *pool_address
;
2648 SET_FOREACH(address
, link
->addresses
, i
) {
2649 /* we consider IPv6LL addresses to be managed by the kernel */
2650 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2653 r
= address_remove(address
, link
, NULL
);
2657 /* If this address came from an address pool, clean up the pool */
2658 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2659 if (address_equal(address
, pool_address
)) {
2660 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2661 address_free(pool_address
);
2667 SET_FOREACH(route
, link
->routes
, i
) {
2668 /* do not touch routes managed by the kernel */
2669 if (route
->protocol
== RTPROT_KERNEL
)
2672 r
= route_remove(route
, link
, NULL
);
2682 static int link_update_lldp(Link
*link
) {
2690 if (link
->flags
& IFF_UP
) {
2691 r
= sd_lldp_start(link
->lldp
);
2693 log_link_debug(link
, "Started LLDP.");
2695 r
= sd_lldp_stop(link
->lldp
);
2697 log_link_debug(link
, "Stopped LLDP.");
2703 static int link_configure_can(Link
*link
) {
2706 if (streq_ptr(link
->kind
, "can")) {
2707 /* The CAN interface must be down to configure bitrate, etc... */
2708 if ((link
->flags
& IFF_UP
)) {
2709 r
= link_down(link
);
2711 link_enter_failed(link
);
2718 return link_set_can(link
);
2721 if (!(link
->flags
& IFF_UP
)) {
2722 r
= link_up_can(link
);
2724 link_enter_failed(link
);
2732 static int link_configure(Link
*link
) {
2736 assert(link
->network
);
2737 assert(link
->state
== LINK_STATE_PENDING
);
2739 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2740 return link_configure_can(link
);
2742 /* Drop foreign config, but ignore loopback or critical devices.
2743 * We do not want to remove loopback address or addresses used for root NFS. */
2744 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2745 r
= link_drop_foreign_config(link
);
2750 r
= link_set_proxy_arp(link
);
2754 r
= ipv6_proxy_ndp_addresses_configure(link
);
2758 r
= link_set_ipv4_forward(link
);
2762 r
= link_set_ipv6_forward(link
);
2766 r
= link_set_ipv6_privacy_extensions(link
);
2770 r
= link_set_ipv6_accept_ra(link
);
2774 r
= link_set_ipv6_dad_transmits(link
);
2778 r
= link_set_ipv6_hop_limit(link
);
2782 r
= link_set_flags(link
);
2786 r
= link_set_ipv6_mtu(link
);
2790 if (link_ipv4ll_enabled(link
)) {
2791 r
= ipv4ll_configure(link
);
2796 if (link_dhcp4_enabled(link
)) {
2797 r
= dhcp4_set_promote_secondaries(link
);
2801 r
= dhcp4_configure(link
);
2806 if (link_dhcp4_server_enabled(link
)) {
2807 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2811 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2816 if (link_dhcp6_enabled(link
) ||
2817 link_ipv6_accept_ra_enabled(link
)) {
2818 r
= dhcp6_configure(link
);
2823 if (link_ipv6_accept_ra_enabled(link
)) {
2824 r
= ndisc_configure(link
);
2829 if (link_radv_enabled(link
)) {
2830 r
= radv_configure(link
);
2835 if (link_lldp_rx_enabled(link
)) {
2836 r
= sd_lldp_new(&link
->lldp
);
2840 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2844 r
= sd_lldp_match_capabilities(link
->lldp
,
2845 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2846 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2847 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2851 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2855 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2859 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2863 r
= link_update_lldp(link
);
2868 if (link
->network
->mtu
> 0) {
2869 r
= link_set_mtu(link
, link
->network
->mtu
);
2874 return link_configure_after_setting_mtu(link
);
2877 static int link_configure_after_setting_mtu(Link
*link
) {
2881 assert(link
->network
);
2882 assert(link
->state
== LINK_STATE_PENDING
);
2884 if (link
->setting_mtu
)
2887 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2888 r
= link_acquire_conf(link
);
2893 return link_enter_join_netdev(link
);
2896 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2899 if (duid
->raw_data_len
> 0)
2902 if (duid
->type
!= DUID_TYPE_UUID
)
2905 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2906 duid
->raw_data_len
= sizeof(sd_id128_t
);
2911 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2912 Manager
*manager
= userdata
;
2913 const sd_bus_error
*e
;
2923 e
= sd_bus_message_get_error(m
);
2925 log_error_errno(sd_bus_error_get_errno(e
),
2926 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2931 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2935 if (sz
!= sizeof(sd_id128_t
)) {
2936 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2940 memcpy(&manager
->product_uuid
, a
, sz
);
2941 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2942 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2944 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2947 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2948 r
= link_configure(link
);
2950 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2953 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2955 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2956 * even if the method fails. */
2957 manager
->has_product_uuid
= true;
2962 static bool link_requires_uuid(Link
*link
) {
2966 assert(link
->manager
);
2967 assert(link
->network
);
2969 duid
= link_get_duid(link
);
2970 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2973 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2976 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2982 static int link_configure_duid(Link
*link
) {
2988 assert(link
->manager
);
2989 assert(link
->network
);
2992 duid
= link_get_duid(link
);
2994 if (!link_requires_uuid(link
))
2997 if (m
->has_product_uuid
) {
2998 (void) duid_set_uuid(duid
, m
->product_uuid
);
3002 if (!m
->links_requesting_uuid
) {
3003 r
= manager_request_product_uuid(m
, link
);
3008 log_link_warning_errno(link
, r
,
3009 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3013 r
= set_put(m
->links_requesting_uuid
, link
);
3017 r
= set_put(m
->duids_requesting_uuid
, duid
);
3025 static int link_initialized_and_synced(Link
*link
) {
3030 assert(link
->ifname
);
3031 assert(link
->manager
);
3033 if (link
->state
!= LINK_STATE_PENDING
)
3036 log_link_debug(link
, "Link state is up-to-date");
3038 r
= link_new_bound_by_list(link
);
3042 r
= link_handle_bound_by_list(link
);
3046 if (!link
->network
) {
3047 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3048 &link
->mac
, &network
);
3050 link_enter_unmanaged(link
);
3052 } else if (r
== 0 && network
->unmanaged
) {
3053 link_enter_unmanaged(link
);
3058 if (link
->flags
& IFF_LOOPBACK
) {
3059 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3060 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3062 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3063 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3065 if (network
->dhcp_server
)
3066 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3069 r
= network_apply(network
, link
);
3074 r
= link_new_bound_to_list(link
);
3078 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3079 * link_configure() is called later asynchronously. */
3080 r
= link_configure_duid(link
);
3084 r
= link_configure(link
);
3091 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3092 (void) link_initialized_and_synced(link
);
3096 int link_initialized(Link
*link
, sd_device
*device
) {
3097 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3101 assert(link
->manager
);
3102 assert(link
->manager
->rtnl
);
3105 if (link
->state
!= LINK_STATE_PENDING
)
3108 if (link
->sd_device
)
3111 log_link_debug(link
, "udev initialized link");
3113 link
->sd_device
= sd_device_ref(device
);
3115 /* udev has initialized the link, but we don't know if we have yet
3116 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3117 * when it returns we know that the pending NEWLINKs have already been
3118 * processed and that we are up-to-date */
3120 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3125 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3126 link_netlink_destroy_callback
, link
);
3135 static int link_load(Link
*link
) {
3136 _cleanup_free_
char *network_file
= NULL
,
3139 *dhcp4_address
= NULL
,
3140 *ipv4ll_address
= NULL
;
3141 union in_addr_union address
;
3142 union in_addr_union route_dst
;
3148 r
= parse_env_file(NULL
, link
->state_file
,
3149 "NETWORK_FILE", &network_file
,
3150 "ADDRESSES", &addresses
,
3152 "DHCP4_ADDRESS", &dhcp4_address
,
3153 "IPV4LL_ADDRESS", &ipv4ll_address
);
3154 if (r
< 0 && r
!= -ENOENT
)
3155 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3162 suffix
= strrchr(network_file
, '.');
3164 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3165 goto network_file_fail
;
3169 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3171 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3172 goto network_file_fail
;
3175 r
= network_apply(network
, link
);
3177 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3186 _cleanup_free_
char *address_str
= NULL
;
3187 char *prefixlen_str
;
3189 unsigned char prefixlen
;
3191 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3193 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3199 prefixlen_str
= strchr(address_str
, '/');
3200 if (!prefixlen_str
) {
3201 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3205 *prefixlen_str
++ = '\0';
3207 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3209 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3213 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3215 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3219 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3221 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3230 _cleanup_free_
char *route_str
= NULL
;
3231 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3233 char *prefixlen_str
;
3235 unsigned char prefixlen
, tos
, table
;
3238 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3240 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3246 prefixlen_str
= strchr(route_str
, '/');
3247 if (!prefixlen_str
) {
3248 log_link_debug(link
, "Failed to parse route %s", route_str
);
3252 *prefixlen_str
++ = '\0';
3254 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3256 log_link_debug(link
,
3257 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3262 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3264 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3268 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3270 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3272 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3273 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3274 0, route_expire_handler
, route
);
3276 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3279 route
->lifetime
= lifetime
;
3280 sd_event_source_unref(route
->expire
);
3281 route
->expire
= TAKE_PTR(expire
);
3285 if (dhcp4_address
) {
3286 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3288 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3289 goto dhcp4_address_fail
;
3292 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3294 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3296 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3298 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3303 if (ipv4ll_address
) {
3304 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3306 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3307 goto ipv4ll_address_fail
;
3310 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3312 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3314 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3316 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3319 ipv4ll_address_fail
:
3324 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3325 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3326 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3335 r
= link_new(m
, message
, ret
);
3341 log_link_debug(link
, "Link %d added", link
->ifindex
);
3343 r
= link_load(link
);
3347 if (detect_container() <= 0) {
3348 /* not in a container, udev will be around */
3349 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3350 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3352 log_link_warning_errno(link
, r
, "Could not find device: %m");
3356 r
= sd_device_get_is_initialized(device
);
3358 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3363 log_link_debug(link
, "link pending udev initialization...");
3367 r
= link_initialized(link
, device
);
3371 r
= link_initialized_and_synced(link
);
3378 link_enter_failed(link
);
3382 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3387 log_link_info(link
, "Gained IPv6LL");
3389 link
->ipv6ll_address
= *address
;
3390 link_check_ready(link
);
3392 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3393 r
= link_acquire_ipv6_conf(link
);
3395 link_enter_failed(link
);
3403 static int link_carrier_gained(Link
*link
) {
3408 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3409 r
= link_acquire_conf(link
);
3411 link_enter_failed(link
);
3415 r
= link_enter_set_addresses(link
);
3420 r
= link_handle_bound_by_list(link
);
3427 static int link_carrier_lost(Link
*link
) {
3432 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3433 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3434 * configuration and stop the clients as well. */
3435 if (link
->setting_mtu
)
3438 r
= link_stop_clients(link
);
3440 link_enter_failed(link
);
3444 if (link_dhcp4_server_enabled(link
))
3445 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3447 r
= link_drop_config(link
);
3451 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3452 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3453 r
= link_drop_foreign_config(link
);
3458 r
= link_handle_bound_by_list(link
);
3465 int link_carrier_reset(Link
*link
) {
3470 if (link_has_carrier(link
)) {
3471 r
= link_carrier_lost(link
);
3475 r
= link_carrier_gained(link
);
3479 log_link_info(link
, "Reset carrier");
3485 int link_update(Link
*link
, sd_netlink_message
*m
) {
3486 struct ether_addr mac
;
3489 bool had_carrier
, carrier_gained
, carrier_lost
;
3493 assert(link
->ifname
);
3496 if (link
->state
== LINK_STATE_LINGER
) {
3497 log_link_info(link
, "Link readded");
3498 link_set_state(link
, LINK_STATE_ENSLAVING
);
3500 r
= link_new_carrier_maps(link
);
3505 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3506 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3507 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3509 if (link
->state
== LINK_STATE_PENDING
) {
3510 r
= free_and_strdup(&link
->ifname
, ifname
);
3514 Manager
*manager
= link
->manager
;
3517 r
= link_add(manager
, m
, &link
);
3523 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3524 if (r
>= 0 && mtu
> 0) {
3526 if (link
->original_mtu
== 0) {
3527 link
->original_mtu
= mtu
;
3528 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3531 if (link
->dhcp_client
) {
3532 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3535 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3539 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3541 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3545 /* The kernel may broadcast NEWLINK messages without the MAC address
3546 set, simply ignore them. */
3547 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3549 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3552 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3555 log_link_debug(link
, "MAC address: "
3556 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3557 mac
.ether_addr_octet
[0],
3558 mac
.ether_addr_octet
[1],
3559 mac
.ether_addr_octet
[2],
3560 mac
.ether_addr_octet
[3],
3561 mac
.ether_addr_octet
[4],
3562 mac
.ether_addr_octet
[5]);
3565 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3567 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3570 if (link
->dhcp_client
) {
3571 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3572 (const uint8_t *) &link
->mac
,
3576 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3578 r
= dhcp4_set_client_identifier(link
);
3583 if (link
->dhcp6_client
) {
3584 const DUID
* duid
= link_get_duid(link
);
3586 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3587 (const uint8_t *) &link
->mac
,
3591 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3593 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3594 link
->network
->iaid
);
3596 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3598 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3600 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3601 duid
->raw_data_len
);
3603 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3607 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3609 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3613 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3615 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3620 had_carrier
= link_has_carrier(link
);
3622 r
= link_update_flags(link
, m
);
3626 r
= link_update_lldp(link
);
3630 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3631 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3633 if (carrier_gained
) {
3634 log_link_info(link
, "Gained carrier");
3636 r
= link_carrier_gained(link
);
3639 } else if (carrier_lost
) {
3640 log_link_info(link
, "Lost carrier");
3642 r
= link_carrier_lost(link
);
3650 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3658 if (hashmap_isempty(h
))
3662 HASHMAP_FOREACH(link
, h
, i
) {
3666 fprintf(f
, "%i", link
->ifindex
);
3673 int link_save(Link
*link
) {
3674 _cleanup_free_
char *temp_path
= NULL
;
3675 _cleanup_fclose_
FILE *f
= NULL
;
3676 const char *admin_state
, *oper_state
;
3683 assert(link
->state_file
);
3684 assert(link
->lease_file
);
3685 assert(link
->manager
);
3687 if (link
->state
== LINK_STATE_LINGER
) {
3688 unlink(link
->state_file
);
3692 link_lldp_save(link
);
3694 admin_state
= link_state_to_string(link
->state
);
3695 assert(admin_state
);
3697 oper_state
= link_operstate_to_string(link
->operstate
);
3700 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3704 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3705 (void) fchmod(fileno(f
), 0644);
3708 "# This is private data. Do not parse.\n"
3711 admin_state
, oper_state
);
3713 if (link
->network
) {
3715 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3716 const char *dhcp_domainname
= NULL
;
3717 char **dhcp6_domains
= NULL
;
3718 char **dhcp_domains
= NULL
;
3721 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3722 yes_no(link
->network
->required_for_online
));
3724 if (link
->dhcp6_client
) {
3725 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3726 if (r
< 0 && r
!= -ENOMSG
)
3727 log_link_debug(link
, "No DHCPv6 lease");
3730 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3735 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3736 _cleanup_free_
char *b
= NULL
;
3738 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3739 &link
->network
->dns
[j
].address
, &b
);
3741 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3751 if (link
->network
->dhcp_use_dns
&&
3753 const struct in_addr
*addresses
;
3755 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3759 serialize_in_addrs(f
, addresses
, r
);
3764 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3765 struct in6_addr
*in6_addrs
;
3767 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3771 serialize_in6_addrs(f
, in6_addrs
, r
);
3776 /* Make sure to flush out old entries before we use the NDISC data */
3779 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3782 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3786 serialize_in6_addrs(f
, &dd
->address
, 1);
3795 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3797 if (link
->network
->dhcp_use_ntp
&&
3799 const struct in_addr
*addresses
;
3801 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3805 serialize_in_addrs(f
, addresses
, r
);
3810 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3811 struct in6_addr
*in6_addrs
;
3814 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3819 serialize_in6_addrs(f
, in6_addrs
, r
);
3823 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3825 fputstrv(f
, hosts
, NULL
, &space
);
3830 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3831 if (link
->dhcp_lease
) {
3832 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3833 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3836 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3839 fputs("DOMAINS=", f
);
3841 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3843 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3846 if (dhcp_domainname
)
3847 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3849 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3851 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3853 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3854 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3859 fputs("ROUTE_DOMAINS=", f
);
3861 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3863 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3866 if (dhcp_domainname
)
3867 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3869 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3871 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3873 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3874 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3879 fprintf(f
, "LLMNR=%s\n",
3880 resolve_support_to_string(link
->network
->llmnr
));
3881 fprintf(f
, "MDNS=%s\n",
3882 resolve_support_to_string(link
->network
->mdns
));
3884 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3885 fprintf(f
, "DNS_OVER_TLS=%s\n",
3886 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3888 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3889 fprintf(f
, "DNSSEC=%s\n",
3890 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3892 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3895 fputs("DNSSEC_NTA=", f
);
3897 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3898 fputs_with_space(f
, n
, NULL
, &space
);
3902 fputs("ADDRESSES=", f
);
3904 SET_FOREACH(a
, link
->addresses
, i
) {
3905 _cleanup_free_
char *address_str
= NULL
;
3907 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3911 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3916 fputs("ROUTES=", f
);
3918 SET_FOREACH(route
, link
->routes
, i
) {
3919 _cleanup_free_
char *route_str
= NULL
;
3921 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3925 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3926 space
? " " : "", route_str
,
3927 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3934 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3935 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3937 if (link
->dhcp_lease
) {
3938 struct in_addr address
;
3939 const char *tz
= NULL
;
3941 assert(link
->network
);
3943 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3945 fprintf(f
, "TIMEZONE=%s\n", tz
);
3947 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3949 fputs("DHCP4_ADDRESS=", f
);
3950 serialize_in_addrs(f
, &address
, 1);
3954 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3962 unlink(link
->lease_file
);
3965 struct in_addr address
;
3967 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3969 fputs("IPV4LL_ADDRESS=", f
);
3970 serialize_in_addrs(f
, &address
, 1);
3975 r
= fflush_and_check(f
);
3979 if (rename(temp_path
, link
->state_file
) < 0) {
3987 (void) unlink(link
->state_file
);
3989 (void) unlink(temp_path
);
3991 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3994 /* The serialized state in /run is no longer up-to-date. */
3995 void link_dirty(Link
*link
) {
4000 /* mark manager dirty as link is dirty */
4001 manager_dirty(link
->manager
);
4003 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4005 /* allocation errors are ignored */
4008 r
= set_put(link
->manager
->dirty_links
, link
);
4010 /* don't take another ref if the link was already dirty */
4016 /* The serialized state in /run is up-to-date */
4017 void link_clean(Link
*link
) {
4019 assert(link
->manager
);
4021 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4024 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4025 [LINK_STATE_PENDING
] = "pending",
4026 [LINK_STATE_ENSLAVING
] = "configuring",
4027 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4028 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4029 [LINK_STATE_CONFIGURED
] = "configured",
4030 [LINK_STATE_UNMANAGED
] = "unmanaged",
4031 [LINK_STATE_FAILED
] = "failed",
4032 [LINK_STATE_LINGER
] = "linger",
4035 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4037 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4038 [LINK_OPERSTATE_OFF
] = "off",
4039 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4040 [LINK_OPERSTATE_DORMANT
] = "dormant",
4041 [LINK_OPERSTATE_CARRIER
] = "carrier",
4042 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4043 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4046 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);