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 void link_netlink_destroy_callback(void *userdata
) {
596 Link
*link
= userdata
;
603 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
610 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
619 static void link_set_state(Link
*link
, LinkState state
) {
622 if (link
->state
== state
)
627 link_send_changed(link
, "AdministrativeState", NULL
);
630 static void link_enter_unmanaged(Link
*link
) {
633 log_link_debug(link
, "Unmanaged");
635 link_set_state(link
, LINK_STATE_UNMANAGED
);
640 static int link_stop_clients(Link
*link
) {
644 assert(link
->manager
);
645 assert(link
->manager
->event
);
647 if (link
->dhcp_client
) {
648 k
= sd_dhcp_client_stop(link
->dhcp_client
);
650 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
654 k
= sd_ipv4ll_stop(link
->ipv4ll
);
656 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
659 if (link
->dhcp6_client
) {
660 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
662 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
666 k
= sd_ndisc_stop(link
->ndisc
);
668 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
672 k
= sd_radv_stop(link
->radv
);
674 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
677 link_lldp_emit_stop(link
);
681 void link_enter_failed(Link
*link
) {
684 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
687 log_link_warning(link
, "Failed");
689 link_set_state(link
, LINK_STATE_FAILED
);
691 link_stop_clients(link
);
696 static Address
* link_find_dhcp_server_address(Link
*link
) {
700 assert(link
->network
);
702 /* The first statically configured address if there is any */
703 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
705 if (address
->family
!= AF_INET
)
708 if (in_addr_is_null(address
->family
, &address
->in_addr
))
714 /* If that didn't work, find a suitable address we got from the pool */
715 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
716 if (address
->family
!= AF_INET
)
725 static void link_enter_configured(Link
*link
) {
727 assert(link
->network
);
729 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
732 log_link_info(link
, "Configured");
734 link_set_state(link
, LINK_STATE_CONFIGURED
);
739 void link_check_ready(Link
*link
) {
745 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
751 if (!link
->static_routes_configured
)
754 if (!link
->routing_policy_rules_configured
)
757 if (link_ipv4ll_enabled(link
))
758 if (!link
->ipv4ll_address
||
762 if (!link
->network
->bridge
) {
764 if (link_ipv6ll_enabled(link
))
765 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
768 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
769 !link
->dhcp4_configured
) ||
770 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
771 !link
->dhcp6_configured
) ||
772 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
773 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
776 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
780 SET_FOREACH(a
, link
->addresses
, i
)
781 if (!address_is_ready(a
))
784 if (link
->state
!= LINK_STATE_CONFIGURED
)
785 link_enter_configured(link
);
790 static int link_set_routing_policy_rule(Link
*link
) {
791 RoutingPolicyRule
*rule
, *rrule
= NULL
;
795 assert(link
->network
);
797 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
798 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
799 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
800 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
802 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
806 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
808 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
809 link_enter_failed(link
);
813 link
->routing_policy_rule_messages
++;
816 routing_policy_rule_purge(link
->manager
, link
);
817 if (link
->routing_policy_rule_messages
== 0) {
818 link
->routing_policy_rules_configured
= true;
819 link_check_ready(link
);
821 log_link_debug(link
, "Setting routing policy rules");
826 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
827 Link
*link
= userdata
;
831 assert(link
->route_messages
> 0);
832 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
833 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
836 link
->route_messages
--;
838 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
841 r
= sd_netlink_message_get_errno(m
);
842 if (r
< 0 && r
!= -EEXIST
)
843 log_link_warning_errno(link
, r
, "Could not set route: %m");
845 if (link
->route_messages
== 0) {
846 log_link_debug(link
, "Routes set");
847 link
->static_routes_configured
= true;
848 link_check_ready(link
);
854 static int link_enter_set_routes(Link
*link
) {
856 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
857 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
864 assert(link
->network
);
865 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
867 (void) link_set_routing_policy_rule(link
);
869 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
871 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
872 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
873 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
875 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
878 r
= route_configure(rt
, link
, route_handler
);
880 log_link_warning_errno(link
, r
, "Could not set routes: %m");
881 link_enter_failed(link
);
885 link
->route_messages
++;
888 if (link
->route_messages
== 0) {
889 link
->static_routes_configured
= true;
890 link_check_ready(link
);
892 log_link_debug(link
, "Setting routes");
897 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
898 Link
*link
= userdata
;
903 assert(link
->ifname
);
905 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
908 r
= sd_netlink_message_get_errno(m
);
909 if (r
< 0 && r
!= -ESRCH
)
910 log_link_warning_errno(link
, r
, "Could not drop route: %m");
915 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
916 Link
*link
= userdata
;
922 assert(link
->ifname
);
923 assert(link
->address_messages
> 0);
924 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
925 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
927 link
->address_messages
--;
929 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
932 r
= sd_netlink_message_get_errno(m
);
933 if (r
< 0 && r
!= -EEXIST
)
934 log_link_warning_errno(link
, r
, "could not set address: %m");
936 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
938 if (link
->address_messages
== 0) {
939 log_link_debug(link
, "Addresses set");
940 link_enter_set_routes(link
);
946 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
947 Link
*link
= userdata
;
953 assert(link
->ifname
);
954 assert(link
->address_label_messages
> 0);
956 link
->address_label_messages
--;
958 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
961 r
= sd_netlink_message_get_errno(m
);
962 if (r
< 0 && r
!= -EEXIST
)
963 log_link_warning_errno(link
, r
, "could not set address label: %m");
965 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
967 if (link
->address_label_messages
== 0)
968 log_link_debug(link
, "Addresses label set");
973 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
974 _cleanup_free_
struct in_addr
*addresses
= NULL
;
975 size_t n_addresses
= 0, n_allocated
= 0;
978 log_debug("Copying DNS server information from %s", link
->ifname
);
983 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
986 /* Only look for IPv4 addresses */
987 if (link
->network
->dns
[i
].family
!= AF_INET
)
990 ia
= link
->network
->dns
[i
].address
.in
;
992 /* Never propagate obviously borked data */
993 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
996 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
999 addresses
[n_addresses
++] = ia
;
1002 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1003 const struct in_addr
*da
= NULL
;
1006 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1009 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1012 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1017 if (n_addresses
<= 0)
1020 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1023 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1024 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1025 size_t n_addresses
= 0, n_allocated
= 0;
1031 log_debug("Copying NTP server information from %s", link
->ifname
);
1033 STRV_FOREACH(a
, link
->network
->ntp
) {
1036 /* Only look for IPv4 addresses */
1037 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1040 /* Never propagate obviously borked data */
1041 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1044 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1047 addresses
[n_addresses
++] = ia
;
1050 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1051 const struct in_addr
*da
= NULL
;
1054 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1057 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1060 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1065 if (n_addresses
<= 0)
1068 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1071 static int link_set_bridge_fdb(Link
*link
) {
1072 FdbEntry
*fdb_entry
;
1075 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1076 r
= fdb_entry_configure(link
, fdb_entry
);
1078 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1084 static int link_enter_set_addresses(Link
*link
) {
1085 AddressLabel
*label
;
1090 assert(link
->network
);
1091 assert(link
->state
!= _LINK_STATE_INVALID
);
1093 r
= link_set_bridge_fdb(link
);
1097 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1099 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1100 r
= address_configure(ad
, link
, address_handler
, false);
1102 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1103 link_enter_failed(link
);
1107 link
->address_messages
++;
1110 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1111 r
= address_label_configure(label
, link
, address_label_handler
, false);
1113 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1114 link_enter_failed(link
);
1118 link
->address_label_messages
++;
1121 /* now that we can figure out a default address for the dhcp server,
1123 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1125 Link
*uplink
= NULL
;
1126 bool acquired_uplink
= false;
1128 address
= link_find_dhcp_server_address(link
);
1130 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1131 link_enter_failed(link
);
1135 /* use the server address' subnet as the pool */
1136 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1137 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1142 r = sd_dhcp_server_set_router(link->dhcp_server,
1143 &main_address->in_addr.in);
1148 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1149 r
= sd_dhcp_server_set_max_lease_time(
1151 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1156 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1157 r
= sd_dhcp_server_set_default_lease_time(
1159 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1164 if (link
->network
->dhcp_server_emit_dns
) {
1166 if (link
->network
->n_dhcp_server_dns
> 0)
1167 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1169 uplink
= manager_find_uplink(link
->manager
, link
);
1170 acquired_uplink
= true;
1173 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1176 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1179 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1182 if (link
->network
->dhcp_server_emit_ntp
) {
1184 if (link
->network
->n_dhcp_server_ntp
> 0)
1185 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1187 if (!acquired_uplink
)
1188 uplink
= manager_find_uplink(link
->manager
, link
);
1191 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1194 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1198 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1201 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1203 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1205 if (link
->network
->dhcp_server_emit_timezone
) {
1206 _cleanup_free_
char *buffer
= NULL
;
1207 const char *tz
= NULL
;
1209 if (link
->network
->dhcp_server_timezone
)
1210 tz
= link
->network
->dhcp_server_timezone
;
1212 r
= get_timezone(&buffer
);
1214 log_warning_errno(r
, "Failed to determine timezone: %m");
1220 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1226 r
= sd_dhcp_server_start(link
->dhcp_server
);
1228 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1230 link_enter_failed(link
);
1235 log_link_debug(link
, "Offering DHCPv4 leases");
1238 if (link
->address_messages
== 0)
1239 link_enter_set_routes(link
);
1241 log_link_debug(link
, "Setting addresses");
1246 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1247 Link
*link
= userdata
;
1252 assert(link
->ifname
);
1254 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1257 r
= sd_netlink_message_get_errno(m
);
1258 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1259 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1264 static int link_set_bridge_vlan(Link
*link
) {
1267 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1269 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1274 static int link_set_proxy_arp(Link
*link
) {
1275 const char *p
= NULL
;
1278 if (!link_proxy_arp_enabled(link
))
1281 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1283 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1285 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1290 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1291 Link
*link
= userdata
;
1296 log_link_debug(link
, "Set link");
1298 r
= sd_netlink_message_get_errno(m
);
1299 if (r
< 0 && r
!= -EEXIST
) {
1300 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1301 link_enter_failed(link
);
1307 static int link_configure_after_setting_mtu(Link
*link
);
1309 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1310 Link
*link
= userdata
;
1315 assert(link
->ifname
);
1317 link
->setting_mtu
= false;
1319 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1322 r
= sd_netlink_message_get_errno(m
);
1324 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1328 log_link_debug(link
, "Setting MTU done.");
1330 if (link
->state
== LINK_STATE_PENDING
)
1331 (void) link_configure_after_setting_mtu(link
);
1336 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1337 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1341 assert(link
->manager
);
1342 assert(link
->manager
->rtnl
);
1344 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1347 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1349 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1351 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1353 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1354 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1355 (void) link_enable_ipv6(link
);
1357 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1358 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1359 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1361 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1362 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1367 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1369 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1371 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1372 link_netlink_destroy_callback
, link
, 0, __func__
);
1374 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1377 link
->setting_mtu
= true;
1382 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1383 Link
*link
= userdata
;
1388 assert(link
->ifname
);
1390 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1393 r
= sd_netlink_message_get_errno(m
);
1395 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1400 static int link_set_flags(Link
*link
) {
1401 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1402 unsigned ifi_change
= 0;
1403 unsigned ifi_flags
= 0;
1407 assert(link
->manager
);
1408 assert(link
->manager
->rtnl
);
1410 if (link
->flags
& IFF_LOOPBACK
)
1416 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1419 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1421 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1423 if (link
->network
->arp
>= 0) {
1424 ifi_change
|= IFF_NOARP
;
1425 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1428 if (link
->network
->multicast
>= 0) {
1429 ifi_change
|= IFF_MULTICAST
;
1430 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1433 if (link
->network
->allmulticast
>= 0) {
1434 ifi_change
|= IFF_ALLMULTI
;
1435 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1438 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1440 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1442 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1443 link_netlink_destroy_callback
, link
, 0, __func__
);
1445 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1452 static int link_set_bridge(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_SETLINK
, link
->ifindex
);
1461 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1463 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1465 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1467 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1469 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1471 if (link
->network
->use_bpdu
>= 0) {
1472 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1474 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1477 if (link
->network
->hairpin
>= 0) {
1478 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1480 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1483 if (link
->network
->fast_leave
>= 0) {
1484 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1486 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1489 if (link
->network
->allow_port_to_be_root
>= 0) {
1490 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1492 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1496 if (link
->network
->unicast_flood
>= 0) {
1497 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1499 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1503 if (link
->network
->cost
!= 0) {
1504 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1506 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1508 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1509 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1511 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1514 r
= sd_netlink_message_close_container(req
);
1516 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1518 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1519 link_netlink_destroy_callback
, link
, 0, __func__
);
1521 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1528 static int link_bond_set(Link
*link
) {
1529 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1533 assert(link
->network
);
1535 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1537 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1539 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1541 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1543 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1545 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1547 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1549 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1551 if (link
->network
->active_slave
) {
1552 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1554 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1557 if (link
->network
->primary_slave
) {
1558 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1560 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1563 r
= sd_netlink_message_close_container(req
);
1565 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1567 r
= sd_netlink_message_close_container(req
);
1569 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1571 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1572 link_netlink_destroy_callback
, link
, 0, __func__
);
1574 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1581 static int link_lldp_save(Link
*link
) {
1582 _cleanup_free_
char *temp_path
= NULL
;
1583 _cleanup_fclose_
FILE *f
= NULL
;
1584 sd_lldp_neighbor
**l
= NULL
;
1588 assert(link
->lldp_file
);
1591 (void) unlink(link
->lldp_file
);
1595 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1599 (void) unlink(link
->lldp_file
);
1605 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1609 fchmod(fileno(f
), 0644);
1611 for (i
= 0; i
< n
; i
++) {
1616 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1621 (void) fwrite(&u
, 1, sizeof(u
), f
);
1622 (void) fwrite(p
, 1, sz
, f
);
1625 r
= fflush_and_check(f
);
1629 if (rename(temp_path
, link
->lldp_file
) < 0) {
1636 (void) unlink(link
->lldp_file
);
1638 (void) unlink(temp_path
);
1640 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1644 for (i
= 0; i
< n
; i
++)
1645 sd_lldp_neighbor_unref(l
[i
]);
1652 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1653 Link
*link
= userdata
;
1658 (void) link_lldp_save(link
);
1660 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1661 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1663 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1665 r
= link_lldp_emit_start(link
);
1667 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1671 static int link_acquire_ipv6_conf(Link
*link
) {
1676 if (link_ipv6_accept_ra_enabled(link
)) {
1677 assert(link
->ndisc
);
1679 log_link_debug(link
, "Discovering IPv6 routers");
1681 r
= sd_ndisc_start(link
->ndisc
);
1682 if (r
< 0 && r
!= -EBUSY
)
1683 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1686 if (link_radv_enabled(link
)) {
1688 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1690 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1692 r
= sd_radv_start(link
->radv
);
1693 if (r
< 0 && r
!= -EBUSY
)
1694 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1697 (void) dhcp6_request_prefix_delegation(link
);
1702 static int link_acquire_ipv4_conf(Link
*link
) {
1706 assert(link
->network
);
1707 assert(link
->manager
);
1708 assert(link
->manager
->event
);
1710 if (link_ipv4ll_enabled(link
)) {
1711 assert(link
->ipv4ll
);
1713 log_link_debug(link
, "Acquiring IPv4 link-local address");
1715 r
= sd_ipv4ll_start(link
->ipv4ll
);
1717 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1720 if (link_dhcp4_enabled(link
)) {
1721 assert(link
->dhcp_client
);
1723 log_link_debug(link
, "Acquiring DHCPv4 lease");
1725 r
= sd_dhcp_client_start(link
->dhcp_client
);
1727 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1733 static int link_acquire_conf(Link
*link
) {
1738 r
= link_acquire_ipv4_conf(link
);
1742 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1743 r
= link_acquire_ipv6_conf(link
);
1748 if (link_lldp_emit_enabled(link
)) {
1749 r
= link_lldp_emit_start(link
);
1751 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1757 bool link_has_carrier(Link
*link
) {
1758 /* see Documentation/networking/operstates.txt in the kernel sources */
1760 if (link
->kernel_operstate
== IF_OPER_UP
)
1763 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1764 /* operstate may not be implemented, so fall back to flags */
1765 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1771 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1772 Link
*link
= userdata
;
1777 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1780 r
= sd_netlink_message_get_errno(m
);
1782 /* we warn but don't fail the link, as it may be brought up later */
1783 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1788 int link_up(Link
*link
) {
1789 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1790 uint8_t ipv6ll_mode
;
1794 assert(link
->network
);
1795 assert(link
->manager
);
1796 assert(link
->manager
->rtnl
);
1798 log_link_debug(link
, "Bringing link up");
1800 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1802 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1804 /* set it free if not enslaved with networkd */
1805 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1806 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1808 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1811 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1813 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1815 if (link
->network
->mac
) {
1816 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1818 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1821 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1823 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1825 if (link_ipv6_enabled(link
)) {
1826 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1827 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1829 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1831 if (!link_ipv6ll_enabled(link
))
1832 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1834 const char *p
= NULL
;
1835 _cleanup_free_
char *stable_secret
= NULL
;
1837 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1838 r
= read_one_line_file(p
, &stable_secret
);
1841 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1843 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1845 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1847 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1849 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1850 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1852 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1855 r
= sd_netlink_message_close_container(req
);
1857 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1860 r
= sd_netlink_message_close_container(req
);
1862 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1864 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1865 link_netlink_destroy_callback
, link
, 0, __func__
);
1867 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1874 static int link_up_can(Link
*link
) {
1875 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1880 log_link_debug(link
, "Bringing CAN link up");
1882 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1884 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1886 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1888 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1890 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1891 link_netlink_destroy_callback
, link
, 0, __func__
);
1893 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1900 static int link_set_can(Link
*link
) {
1901 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1905 assert(link
->network
);
1906 assert(link
->manager
);
1907 assert(link
->manager
->rtnl
);
1909 log_link_debug(link
, "link_set_can");
1911 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1913 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1915 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1917 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1919 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1921 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1923 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1925 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1927 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1928 struct can_bittiming bt
= {
1929 .bitrate
= link
->network
->can_bitrate
,
1930 .sample_point
= link
->network
->can_sample_point
,
1933 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1934 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1938 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1939 if (link
->network
->can_sample_point
> 0)
1940 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1942 log_link_debug(link
, "Using default sample point");
1944 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1946 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1949 if (link
->network
->can_restart_us
> 0) {
1950 char time_string
[FORMAT_TIMESPAN_MAX
];
1951 uint64_t restart_ms
;
1953 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1956 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1958 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1960 if (restart_ms
> UINT32_MAX
) {
1961 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1965 log_link_debug(link
, "Setting restart = %s", time_string
);
1967 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1969 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1972 r
= sd_netlink_message_close_container(m
);
1974 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1976 r
= sd_netlink_message_close_container(m
);
1978 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1980 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1981 link_netlink_destroy_callback
, link
, 0, __func__
);
1983 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1987 if (!(link
->flags
& IFF_UP
)) {
1988 r
= link_up_can(link
);
1990 link_enter_failed(link
);
1995 log_link_debug(link
, "link_set_can done");
2000 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2001 Link
*link
= userdata
;
2006 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2009 r
= sd_netlink_message_get_errno(m
);
2011 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2013 if (streq_ptr(link
->kind
, "can"))
2019 int link_down(Link
*link
) {
2020 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2024 assert(link
->manager
);
2025 assert(link
->manager
->rtnl
);
2027 log_link_debug(link
, "Bringing link down");
2029 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2030 RTM_SETLINK
, link
->ifindex
);
2032 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2034 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2036 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2038 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2039 link_netlink_destroy_callback
, link
, 0, __func__
);
2041 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2048 static int link_handle_bound_to_list(Link
*link
) {
2052 bool required_up
= false;
2053 bool link_is_up
= false;
2057 if (hashmap_isempty(link
->bound_to_links
))
2060 if (link
->flags
& IFF_UP
)
2063 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2064 if (link_has_carrier(l
)) {
2069 if (!required_up
&& link_is_up
) {
2070 r
= link_down(link
);
2073 } else if (required_up
&& !link_is_up
) {
2082 static int link_handle_bound_by_list(Link
*link
) {
2089 if (hashmap_isempty(link
->bound_by_links
))
2092 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2093 r
= link_handle_bound_to_list(l
);
2101 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2107 if (link
== carrier
)
2110 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2113 r
= hashmap_ensure_allocated(h
, NULL
);
2117 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2124 static int link_new_bound_by_list(Link
*link
) {
2129 bool list_updated
= false;
2132 assert(link
->manager
);
2136 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2137 if (!carrier
->network
)
2140 if (strv_isempty(carrier
->network
->bind_carrier
))
2143 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2144 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2148 list_updated
= true;
2155 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2156 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2160 link_dirty(carrier
);
2166 static int link_new_bound_to_list(Link
*link
) {
2171 bool list_updated
= false;
2174 assert(link
->manager
);
2179 if (strv_isempty(link
->network
->bind_carrier
))
2184 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2185 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2186 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2190 list_updated
= true;
2197 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2198 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2202 link_dirty(carrier
);
2208 static int link_new_carrier_maps(Link
*link
) {
2211 r
= link_new_bound_by_list(link
);
2215 r
= link_handle_bound_by_list(link
);
2219 r
= link_new_bound_to_list(link
);
2223 r
= link_handle_bound_to_list(link
);
2230 static void link_free_bound_to_list(Link
*link
) {
2234 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2235 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2237 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2238 link_dirty(bound_to
);
2244 static void link_free_bound_by_list(Link
*link
) {
2248 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2249 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2251 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2252 link_dirty(bound_by
);
2253 link_handle_bound_to_list(bound_by
);
2260 static void link_free_carrier_maps(Link
*link
) {
2261 bool list_updated
= false;
2265 if (!hashmap_isempty(link
->bound_to_links
)) {
2266 link_free_bound_to_list(link
);
2267 list_updated
= true;
2270 if (!hashmap_isempty(link
->bound_by_links
)) {
2271 link_free_bound_by_list(link
);
2272 list_updated
= true;
2281 void link_drop(Link
*link
) {
2282 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2285 link_set_state(link
, LINK_STATE_LINGER
);
2287 link_free_carrier_maps(link
);
2289 log_link_debug(link
, "Link removed");
2291 (void) unlink(link
->state_file
);
2293 link_detach_from_manager(link
);
2300 static int link_joined(Link
*link
) {
2304 assert(link
->network
);
2306 if (!hashmap_isempty(link
->bound_to_links
)) {
2307 r
= link_handle_bound_to_list(link
);
2310 } else if (!(link
->flags
& IFF_UP
)) {
2313 link_enter_failed(link
);
2318 if (link
->network
->bridge
) {
2319 r
= link_set_bridge(link
);
2321 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2324 if (link
->network
->bond
) {
2325 r
= link_bond_set(link
);
2327 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2330 if (link
->network
->use_br_vlan
&&
2331 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2332 r
= link_set_bridge_vlan(link
);
2334 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2337 /* Skip setting up addresses until it gets carrier,
2338 or it would try to set addresses twice,
2339 which is bad for non-idempotent steps. */
2340 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2343 return link_enter_set_addresses(link
);
2346 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2347 Link
*link
= userdata
;
2351 assert(link
->network
);
2355 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2358 r
= sd_netlink_message_get_errno(m
);
2359 if (r
< 0 && r
!= -EEXIST
) {
2360 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2361 link_enter_failed(link
);
2364 log_link_debug(link
, "Joined netdev");
2366 if (link
->enslaving
<= 0)
2372 static int link_enter_join_netdev(Link
*link
) {
2378 assert(link
->network
);
2379 assert(link
->state
== LINK_STATE_PENDING
);
2381 link_set_state(link
, LINK_STATE_ENSLAVING
);
2385 if (!link
->network
->bridge
&&
2386 !link
->network
->bond
&&
2387 !link
->network
->vrf
&&
2388 hashmap_isempty(link
->network
->stacked_netdevs
))
2389 return link_joined(link
);
2391 if (link
->network
->bond
) {
2392 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2393 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2394 return link_joined(link
);
2396 log_struct(LOG_DEBUG
,
2397 LOG_LINK_INTERFACE(link
),
2398 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2399 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2401 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2403 log_struct_errno(LOG_WARNING
, r
,
2404 LOG_LINK_INTERFACE(link
),
2405 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2406 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2407 link_enter_failed(link
);
2414 if (link
->network
->bridge
) {
2415 log_struct(LOG_DEBUG
,
2416 LOG_LINK_INTERFACE(link
),
2417 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2418 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2420 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2422 log_struct_errno(LOG_WARNING
, r
,
2423 LOG_LINK_INTERFACE(link
),
2424 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2425 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2426 link_enter_failed(link
);
2433 if (link
->network
->vrf
) {
2434 log_struct(LOG_DEBUG
,
2435 LOG_LINK_INTERFACE(link
),
2436 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2437 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2439 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2441 log_struct_errno(LOG_WARNING
, r
,
2442 LOG_LINK_INTERFACE(link
),
2443 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2444 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2445 link_enter_failed(link
);
2452 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2454 if (netdev
->ifindex
> 0) {
2459 log_struct(LOG_DEBUG
,
2460 LOG_LINK_INTERFACE(link
),
2461 LOG_NETDEV_INTERFACE(netdev
),
2462 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2464 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2466 log_struct_errno(LOG_WARNING
, r
,
2467 LOG_LINK_INTERFACE(link
),
2468 LOG_NETDEV_INTERFACE(netdev
),
2469 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2470 link_enter_failed(link
);
2480 static int link_set_ipv4_forward(Link
*link
) {
2483 if (!link_ipv4_forward_enabled(link
))
2486 /* We propagate the forwarding flag from one interface to the
2487 * global setting one way. This means: as long as at least one
2488 * interface was configured at any time that had IP forwarding
2489 * enabled the setting will stay on for good. We do this
2490 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2491 * somewhat in sync (see below). */
2493 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2495 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2500 static int link_set_ipv6_forward(Link
*link
) {
2503 if (!link_ipv6_forward_enabled(link
))
2506 /* On Linux, the IPv6 stack does not know a per-interface
2507 * packet forwarding setting: either packet forwarding is on
2508 * for all, or off for all. We hence don't bother with a
2509 * per-interface setting, but simply propagate the interface
2510 * flag, if it is set, to the global flag, one-way. Note that
2511 * while IPv4 would allow a per-interface flag, we expose the
2512 * same behaviour there and also propagate the setting from
2513 * one to all, to keep things simple (see above). */
2515 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2517 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2522 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2523 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2524 IPv6PrivacyExtensions s
;
2525 const char *p
= NULL
;
2528 s
= link_ipv6_privacy_extensions(link
);
2532 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2533 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2535 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2537 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2542 static int link_set_ipv6_accept_ra(Link
*link
) {
2543 const char *p
= NULL
;
2546 /* Make this a NOP if IPv6 is not available */
2547 if (!socket_ipv6_is_supported())
2550 if (link
->flags
& IFF_LOOPBACK
)
2556 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2558 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2559 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2561 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2566 static int link_set_ipv6_dad_transmits(Link
*link
) {
2567 char buf
[DECIMAL_STR_MAX(int) + 1];
2568 const char *p
= NULL
;
2571 /* Make this a NOP if IPv6 is not available */
2572 if (!socket_ipv6_is_supported())
2575 if (link
->flags
& IFF_LOOPBACK
)
2581 if (link
->network
->ipv6_dad_transmits
< 0)
2584 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2585 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2587 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2589 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2594 static int link_set_ipv6_hop_limit(Link
*link
) {
2595 char buf
[DECIMAL_STR_MAX(int) + 1];
2596 const char *p
= NULL
;
2599 /* Make this a NOP if IPv6 is not available */
2600 if (!socket_ipv6_is_supported())
2603 if (link
->flags
& IFF_LOOPBACK
)
2609 if (link
->network
->ipv6_hop_limit
< 0)
2612 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2613 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2615 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2617 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2622 static int link_set_ipv6_mtu(Link
*link
) {
2623 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2624 const char *p
= NULL
;
2627 /* Make this a NOP if IPv6 is not available */
2628 if (!socket_ipv6_is_supported())
2631 if (link
->flags
& IFF_LOOPBACK
)
2634 if (link
->network
->ipv6_mtu
== 0)
2637 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2639 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2641 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2643 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2648 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2649 Address
*net_address
;
2657 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2658 if (address_equal(net_address
, address
))
2664 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2673 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2674 if (route_equal(net_route
, route
))
2680 static int link_drop_foreign_config(Link
*link
) {
2686 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2687 /* we consider IPv6LL addresses to be managed by the kernel */
2688 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2691 if (link_is_static_address_configured(link
, address
)) {
2692 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2694 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2696 r
= address_remove(address
, link
, link_address_remove_handler
);
2702 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2703 /* do not touch routes managed by the kernel */
2704 if (route
->protocol
== RTPROT_KERNEL
)
2707 if (link_is_static_route_configured(link
, route
)) {
2708 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2712 r
= route_remove(route
, link
, link_route_remove_handler
);
2721 static int link_drop_config(Link
*link
) {
2722 Address
*address
, *pool_address
;
2727 SET_FOREACH(address
, link
->addresses
, i
) {
2728 /* we consider IPv6LL addresses to be managed by the kernel */
2729 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2732 r
= address_remove(address
, link
, link_address_remove_handler
);
2736 /* If this address came from an address pool, clean up the pool */
2737 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2738 if (address_equal(address
, pool_address
)) {
2739 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2740 address_free(pool_address
);
2746 SET_FOREACH(route
, link
->routes
, i
) {
2747 /* do not touch routes managed by the kernel */
2748 if (route
->protocol
== RTPROT_KERNEL
)
2751 r
= route_remove(route
, link
, link_route_remove_handler
);
2761 static int link_update_lldp(Link
*link
) {
2769 if (link
->flags
& IFF_UP
) {
2770 r
= sd_lldp_start(link
->lldp
);
2772 log_link_debug(link
, "Started LLDP.");
2774 r
= sd_lldp_stop(link
->lldp
);
2776 log_link_debug(link
, "Stopped LLDP.");
2782 static int link_configure_can(Link
*link
) {
2785 if (streq_ptr(link
->kind
, "can")) {
2786 /* The CAN interface must be down to configure bitrate, etc... */
2787 if ((link
->flags
& IFF_UP
)) {
2788 r
= link_down(link
);
2790 link_enter_failed(link
);
2797 return link_set_can(link
);
2800 if (!(link
->flags
& IFF_UP
)) {
2801 r
= link_up_can(link
);
2803 link_enter_failed(link
);
2811 static int link_configure(Link
*link
) {
2815 assert(link
->network
);
2816 assert(link
->state
== LINK_STATE_PENDING
);
2818 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2819 return link_configure_can(link
);
2821 /* Drop foreign config, but ignore loopback or critical devices.
2822 * We do not want to remove loopback address or addresses used for root NFS. */
2823 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2824 r
= link_drop_foreign_config(link
);
2829 r
= link_set_proxy_arp(link
);
2833 r
= ipv6_proxy_ndp_addresses_configure(link
);
2837 r
= link_set_ipv4_forward(link
);
2841 r
= link_set_ipv6_forward(link
);
2845 r
= link_set_ipv6_privacy_extensions(link
);
2849 r
= link_set_ipv6_accept_ra(link
);
2853 r
= link_set_ipv6_dad_transmits(link
);
2857 r
= link_set_ipv6_hop_limit(link
);
2861 r
= link_set_flags(link
);
2865 r
= link_set_ipv6_mtu(link
);
2869 if (link_ipv4ll_enabled(link
)) {
2870 r
= ipv4ll_configure(link
);
2875 if (link_dhcp4_enabled(link
)) {
2876 r
= dhcp4_set_promote_secondaries(link
);
2880 r
= dhcp4_configure(link
);
2885 if (link_dhcp4_server_enabled(link
)) {
2886 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2890 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2895 if (link_dhcp6_enabled(link
) ||
2896 link_ipv6_accept_ra_enabled(link
)) {
2897 r
= dhcp6_configure(link
);
2902 if (link_ipv6_accept_ra_enabled(link
)) {
2903 r
= ndisc_configure(link
);
2908 if (link_radv_enabled(link
)) {
2909 r
= radv_configure(link
);
2914 if (link_lldp_rx_enabled(link
)) {
2915 r
= sd_lldp_new(&link
->lldp
);
2919 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2923 r
= sd_lldp_match_capabilities(link
->lldp
,
2924 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2925 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2926 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2930 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2934 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2938 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2942 r
= link_update_lldp(link
);
2947 if (link
->network
->mtu
> 0) {
2948 r
= link_set_mtu(link
, link
->network
->mtu
);
2953 return link_configure_after_setting_mtu(link
);
2956 static int link_configure_after_setting_mtu(Link
*link
) {
2960 assert(link
->network
);
2961 assert(link
->state
== LINK_STATE_PENDING
);
2963 if (link
->setting_mtu
)
2966 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2967 r
= link_acquire_conf(link
);
2972 return link_enter_join_netdev(link
);
2975 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2978 if (duid
->raw_data_len
> 0)
2981 if (duid
->type
!= DUID_TYPE_UUID
)
2984 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2985 duid
->raw_data_len
= sizeof(sd_id128_t
);
2990 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2991 Manager
*manager
= userdata
;
2992 const sd_bus_error
*e
;
3002 e
= sd_bus_message_get_error(m
);
3004 log_error_errno(sd_bus_error_get_errno(e
),
3005 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3010 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3014 if (sz
!= sizeof(sd_id128_t
)) {
3015 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3019 memcpy(&manager
->product_uuid
, a
, sz
);
3020 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3021 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3023 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3026 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3027 r
= link_configure(link
);
3029 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3032 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3034 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3035 * even if the method fails. */
3036 manager
->has_product_uuid
= true;
3041 static bool link_requires_uuid(Link
*link
) {
3045 assert(link
->manager
);
3046 assert(link
->network
);
3048 duid
= link_get_duid(link
);
3049 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3052 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3055 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3061 static int link_configure_duid(Link
*link
) {
3067 assert(link
->manager
);
3068 assert(link
->network
);
3071 duid
= link_get_duid(link
);
3073 if (!link_requires_uuid(link
))
3076 if (m
->has_product_uuid
) {
3077 (void) duid_set_uuid(duid
, m
->product_uuid
);
3081 if (!m
->links_requesting_uuid
) {
3082 r
= manager_request_product_uuid(m
, link
);
3087 log_link_warning_errno(link
, r
,
3088 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3092 r
= set_put(m
->links_requesting_uuid
, link
);
3096 r
= set_put(m
->duids_requesting_uuid
, duid
);
3104 static int link_initialized_and_synced(Link
*link
) {
3109 assert(link
->ifname
);
3110 assert(link
->manager
);
3112 if (link
->state
!= LINK_STATE_PENDING
)
3115 log_link_debug(link
, "Link state is up-to-date");
3117 r
= link_new_bound_by_list(link
);
3121 r
= link_handle_bound_by_list(link
);
3125 if (!link
->network
) {
3126 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3127 &link
->mac
, &network
);
3129 link_enter_unmanaged(link
);
3131 } else if (r
== 0 && network
->unmanaged
) {
3132 link_enter_unmanaged(link
);
3137 if (link
->flags
& IFF_LOOPBACK
) {
3138 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3139 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3141 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3142 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3144 if (network
->dhcp_server
)
3145 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3148 r
= network_apply(network
, link
);
3153 r
= link_new_bound_to_list(link
);
3157 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3158 * link_configure() is called later asynchronously. */
3159 r
= link_configure_duid(link
);
3163 r
= link_configure(link
);
3170 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
3171 (void) link_initialized_and_synced(userdata
);
3175 int link_initialized(Link
*link
, sd_device
*device
) {
3176 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3180 assert(link
->manager
);
3181 assert(link
->manager
->rtnl
);
3184 if (link
->state
!= LINK_STATE_PENDING
)
3187 if (link
->sd_device
)
3190 log_link_debug(link
, "udev initialized link");
3192 link
->sd_device
= sd_device_ref(device
);
3194 /* udev has initialized the link, but we don't know if we have yet
3195 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3196 * when it returns we know that the pending NEWLINKs have already been
3197 * processed and that we are up-to-date */
3199 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3204 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3205 link_netlink_destroy_callback
, link
, 0, __func__
);
3214 static int link_load(Link
*link
) {
3215 _cleanup_free_
char *network_file
= NULL
,
3218 *dhcp4_address
= NULL
,
3219 *ipv4ll_address
= NULL
;
3220 union in_addr_union address
;
3221 union in_addr_union route_dst
;
3227 r
= parse_env_file(NULL
, link
->state_file
,
3228 "NETWORK_FILE", &network_file
,
3229 "ADDRESSES", &addresses
,
3231 "DHCP4_ADDRESS", &dhcp4_address
,
3232 "IPV4LL_ADDRESS", &ipv4ll_address
);
3233 if (r
< 0 && r
!= -ENOENT
)
3234 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3241 suffix
= strrchr(network_file
, '.');
3243 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3244 goto network_file_fail
;
3248 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3250 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3251 goto network_file_fail
;
3254 r
= network_apply(network
, link
);
3256 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3265 _cleanup_free_
char *address_str
= NULL
;
3266 char *prefixlen_str
;
3268 unsigned char prefixlen
;
3270 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3272 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3278 prefixlen_str
= strchr(address_str
, '/');
3279 if (!prefixlen_str
) {
3280 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3284 *prefixlen_str
++ = '\0';
3286 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3288 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3292 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3294 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3298 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3300 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3309 _cleanup_free_
char *route_str
= NULL
;
3310 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3312 char *prefixlen_str
;
3314 unsigned char prefixlen
, tos
, table
;
3317 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3319 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3325 prefixlen_str
= strchr(route_str
, '/');
3326 if (!prefixlen_str
) {
3327 log_link_debug(link
, "Failed to parse route %s", route_str
);
3331 *prefixlen_str
++ = '\0';
3333 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3335 log_link_debug(link
,
3336 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3341 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3343 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3347 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3349 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3351 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3352 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3353 0, route_expire_handler
, route
);
3355 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3358 route
->lifetime
= lifetime
;
3359 sd_event_source_unref(route
->expire
);
3360 route
->expire
= TAKE_PTR(expire
);
3364 if (dhcp4_address
) {
3365 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3367 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3368 goto dhcp4_address_fail
;
3371 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3373 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3375 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3377 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3382 if (ipv4ll_address
) {
3383 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3385 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3386 goto ipv4ll_address_fail
;
3389 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3391 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3393 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3395 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3398 ipv4ll_address_fail
:
3403 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3404 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3405 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3414 r
= link_new(m
, message
, ret
);
3420 log_link_debug(link
, "Link %d added", link
->ifindex
);
3422 r
= link_load(link
);
3426 if (detect_container() <= 0) {
3427 /* not in a container, udev will be around */
3428 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3429 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3431 log_link_warning_errno(link
, r
, "Could not find device: %m");
3435 r
= sd_device_get_is_initialized(device
);
3437 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3442 log_link_debug(link
, "link pending udev initialization...");
3446 r
= link_initialized(link
, device
);
3450 r
= link_initialized_and_synced(link
);
3457 link_enter_failed(link
);
3461 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3466 log_link_info(link
, "Gained IPv6LL");
3468 link
->ipv6ll_address
= *address
;
3469 link_check_ready(link
);
3471 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3472 r
= link_acquire_ipv6_conf(link
);
3474 link_enter_failed(link
);
3482 static int link_carrier_gained(Link
*link
) {
3487 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3488 r
= link_acquire_conf(link
);
3490 link_enter_failed(link
);
3494 r
= link_enter_set_addresses(link
);
3499 r
= link_handle_bound_by_list(link
);
3506 static int link_carrier_lost(Link
*link
) {
3511 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3512 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3513 * configuration and stop the clients as well. */
3514 if (link
->setting_mtu
)
3517 r
= link_stop_clients(link
);
3519 link_enter_failed(link
);
3523 if (link_dhcp4_server_enabled(link
))
3524 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3526 r
= link_drop_config(link
);
3530 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3531 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3532 r
= link_drop_foreign_config(link
);
3537 r
= link_handle_bound_by_list(link
);
3544 int link_carrier_reset(Link
*link
) {
3549 if (link_has_carrier(link
)) {
3550 r
= link_carrier_lost(link
);
3554 r
= link_carrier_gained(link
);
3558 log_link_info(link
, "Reset carrier");
3564 int link_update(Link
*link
, sd_netlink_message
*m
) {
3565 struct ether_addr mac
;
3568 bool had_carrier
, carrier_gained
, carrier_lost
;
3572 assert(link
->ifname
);
3575 if (link
->state
== LINK_STATE_LINGER
) {
3576 log_link_info(link
, "Link readded");
3577 link_set_state(link
, LINK_STATE_ENSLAVING
);
3579 r
= link_new_carrier_maps(link
);
3584 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3585 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3586 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3588 if (link
->state
== LINK_STATE_PENDING
) {
3589 r
= free_and_strdup(&link
->ifname
, ifname
);
3593 Manager
*manager
= link
->manager
;
3596 r
= link_add(manager
, m
, &link
);
3602 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3603 if (r
>= 0 && mtu
> 0) {
3605 if (link
->original_mtu
== 0) {
3606 link
->original_mtu
= mtu
;
3607 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3610 if (link
->dhcp_client
) {
3611 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3614 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3618 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3620 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3624 /* The kernel may broadcast NEWLINK messages without the MAC address
3625 set, simply ignore them. */
3626 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3628 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3631 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3634 log_link_debug(link
, "MAC address: "
3635 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3636 mac
.ether_addr_octet
[0],
3637 mac
.ether_addr_octet
[1],
3638 mac
.ether_addr_octet
[2],
3639 mac
.ether_addr_octet
[3],
3640 mac
.ether_addr_octet
[4],
3641 mac
.ether_addr_octet
[5]);
3644 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3646 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3649 if (link
->dhcp_client
) {
3650 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3651 (const uint8_t *) &link
->mac
,
3655 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3657 r
= dhcp4_set_client_identifier(link
);
3662 if (link
->dhcp6_client
) {
3663 const DUID
* duid
= link_get_duid(link
);
3665 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3666 (const uint8_t *) &link
->mac
,
3670 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3672 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3673 link
->network
->iaid
);
3675 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3677 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3679 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3680 duid
->raw_data_len
);
3682 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3686 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3688 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3692 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3694 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3699 had_carrier
= link_has_carrier(link
);
3701 r
= link_update_flags(link
, m
);
3705 r
= link_update_lldp(link
);
3709 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3710 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3712 if (carrier_gained
) {
3713 log_link_info(link
, "Gained carrier");
3715 r
= link_carrier_gained(link
);
3718 } else if (carrier_lost
) {
3719 log_link_info(link
, "Lost carrier");
3721 r
= link_carrier_lost(link
);
3729 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3737 if (hashmap_isempty(h
))
3741 HASHMAP_FOREACH(link
, h
, i
) {
3745 fprintf(f
, "%i", link
->ifindex
);
3752 int link_save(Link
*link
) {
3753 _cleanup_free_
char *temp_path
= NULL
;
3754 _cleanup_fclose_
FILE *f
= NULL
;
3755 const char *admin_state
, *oper_state
;
3762 assert(link
->state_file
);
3763 assert(link
->lease_file
);
3764 assert(link
->manager
);
3766 if (link
->state
== LINK_STATE_LINGER
) {
3767 unlink(link
->state_file
);
3771 link_lldp_save(link
);
3773 admin_state
= link_state_to_string(link
->state
);
3774 assert(admin_state
);
3776 oper_state
= link_operstate_to_string(link
->operstate
);
3779 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3783 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3784 (void) fchmod(fileno(f
), 0644);
3787 "# This is private data. Do not parse.\n"
3790 admin_state
, oper_state
);
3792 if (link
->network
) {
3794 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3795 const char *dhcp_domainname
= NULL
;
3796 char **dhcp6_domains
= NULL
;
3797 char **dhcp_domains
= NULL
;
3800 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3801 yes_no(link
->network
->required_for_online
));
3803 if (link
->dhcp6_client
) {
3804 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3805 if (r
< 0 && r
!= -ENOMSG
)
3806 log_link_debug(link
, "No DHCPv6 lease");
3809 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3814 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3815 _cleanup_free_
char *b
= NULL
;
3817 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3818 &link
->network
->dns
[j
].address
, &b
);
3820 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3830 if (link
->network
->dhcp_use_dns
&&
3832 const struct in_addr
*addresses
;
3834 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3838 serialize_in_addrs(f
, addresses
, r
);
3843 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3844 struct in6_addr
*in6_addrs
;
3846 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3850 serialize_in6_addrs(f
, in6_addrs
, r
);
3855 /* Make sure to flush out old entries before we use the NDISC data */
3858 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3861 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3865 serialize_in6_addrs(f
, &dd
->address
, 1);
3874 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3876 if (link
->network
->dhcp_use_ntp
&&
3878 const struct in_addr
*addresses
;
3880 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3884 serialize_in_addrs(f
, addresses
, r
);
3889 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3890 struct in6_addr
*in6_addrs
;
3893 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3898 serialize_in6_addrs(f
, in6_addrs
, r
);
3902 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3904 fputstrv(f
, hosts
, NULL
, &space
);
3909 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3910 if (link
->dhcp_lease
) {
3911 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3912 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3915 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3918 fputs("DOMAINS=", f
);
3920 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3922 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3925 if (dhcp_domainname
)
3926 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3928 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3930 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3932 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3933 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3938 fputs("ROUTE_DOMAINS=", f
);
3940 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3942 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3945 if (dhcp_domainname
)
3946 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3948 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3950 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3952 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3953 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3958 fprintf(f
, "LLMNR=%s\n",
3959 resolve_support_to_string(link
->network
->llmnr
));
3960 fprintf(f
, "MDNS=%s\n",
3961 resolve_support_to_string(link
->network
->mdns
));
3963 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3964 fprintf(f
, "DNS_OVER_TLS=%s\n",
3965 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3967 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3968 fprintf(f
, "DNSSEC=%s\n",
3969 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3971 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3974 fputs("DNSSEC_NTA=", f
);
3976 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3977 fputs_with_space(f
, n
, NULL
, &space
);
3981 fputs("ADDRESSES=", f
);
3983 SET_FOREACH(a
, link
->addresses
, i
) {
3984 _cleanup_free_
char *address_str
= NULL
;
3986 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3990 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3995 fputs("ROUTES=", f
);
3997 SET_FOREACH(route
, link
->routes
, i
) {
3998 _cleanup_free_
char *route_str
= NULL
;
4000 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4004 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4005 space
? " " : "", route_str
,
4006 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4013 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4014 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4016 if (link
->dhcp_lease
) {
4017 struct in_addr address
;
4018 const char *tz
= NULL
;
4020 assert(link
->network
);
4022 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4024 fprintf(f
, "TIMEZONE=%s\n", tz
);
4026 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4028 fputs("DHCP4_ADDRESS=", f
);
4029 serialize_in_addrs(f
, &address
, 1);
4033 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4041 unlink(link
->lease_file
);
4044 struct in_addr address
;
4046 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4048 fputs("IPV4LL_ADDRESS=", f
);
4049 serialize_in_addrs(f
, &address
, 1);
4054 r
= fflush_and_check(f
);
4058 if (rename(temp_path
, link
->state_file
) < 0) {
4066 (void) unlink(link
->state_file
);
4068 (void) unlink(temp_path
);
4070 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4073 /* The serialized state in /run is no longer up-to-date. */
4074 void link_dirty(Link
*link
) {
4079 /* mark manager dirty as link is dirty */
4080 manager_dirty(link
->manager
);
4082 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4084 /* allocation errors are ignored */
4087 r
= set_put(link
->manager
->dirty_links
, link
);
4089 /* don't take another ref if the link was already dirty */
4095 /* The serialized state in /run is up-to-date */
4096 void link_clean(Link
*link
) {
4098 assert(link
->manager
);
4100 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4103 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4104 [LINK_STATE_PENDING
] = "pending",
4105 [LINK_STATE_ENSLAVING
] = "configuring",
4106 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4107 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4108 [LINK_STATE_CONFIGURED
] = "configured",
4109 [LINK_STATE_UNMANAGED
] = "unmanaged",
4110 [LINK_STATE_FAILED
] = "failed",
4111 [LINK_STATE_LINGER
] = "linger",
4114 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4116 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4117 [LINK_OPERSTATE_OFF
] = "off",
4118 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4119 [LINK_OPERSTATE_DORMANT
] = "dormant",
4120 [LINK_OPERSTATE_CARRIER
] = "carrier",
4121 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4122 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4125 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);