1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
15 #include "netlink-util.h"
16 #include "network-internal.h"
17 #include "networkd-ipv6-proxy-ndp.h"
18 #include "networkd-lldp-tx.h"
19 #include "networkd-manager.h"
20 #include "networkd-ndisc.h"
21 #include "networkd-radv.h"
22 #include "networkd-routing-policy-rule.h"
24 #include "socket-util.h"
25 #include "stdio-util.h"
26 #include "string-table.h"
31 DUID
* link_get_duid(Link
*link
) {
32 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
33 return &link
->network
->duid
;
35 return &link
->manager
->duid
;
38 static bool link_dhcp6_enabled(Link
*link
) {
41 if (!socket_ipv6_is_supported())
44 if (link
->flags
& IFF_LOOPBACK
)
50 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
53 static bool link_dhcp4_enabled(Link
*link
) {
56 if (link
->flags
& IFF_LOOPBACK
)
62 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
65 static bool link_dhcp4_server_enabled(Link
*link
) {
68 if (link
->flags
& IFF_LOOPBACK
)
74 return link
->network
->dhcp_server
;
77 static bool link_ipv4ll_enabled(Link
*link
) {
80 if (link
->flags
& IFF_LOOPBACK
)
86 if (streq_ptr(link
->kind
, "wireguard"))
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 static bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 if (streq_ptr(link
->kind
, "wireguard"))
107 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
110 static bool link_ipv6_enabled(Link
*link
) {
113 if (!socket_ipv6_is_supported())
116 if (link
->network
->bridge
)
119 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
120 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
123 static bool link_radv_enabled(Link
*link
) {
126 if (!link_ipv6ll_enabled(link
))
129 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
132 static bool link_lldp_rx_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
138 if (link
->iftype
!= ARPHRD_ETHER
)
144 /* LLDP should be handled on bridge slaves as those have a direct
145 * connection to their peers not on the bridge master. Linux doesn't
146 * even (by default) forward lldp packets to the bridge master.*/
147 if (streq_ptr("bridge", link
->kind
))
150 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
153 static bool link_lldp_emit_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
159 if (link
->iftype
!= ARPHRD_ETHER
)
165 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
168 static bool link_ipv4_forward_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
180 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
183 static bool link_ipv6_forward_enabled(Link
*link
) {
186 if (!socket_ipv6_is_supported())
189 if (link
->flags
& IFF_LOOPBACK
)
195 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
198 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
201 static bool link_proxy_arp_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
210 if (link
->network
->proxy_arp
< 0)
216 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
219 if (!socket_ipv6_is_supported())
222 if (link
->flags
& IFF_LOOPBACK
)
228 if (!link_ipv6ll_enabled(link
))
231 /* If unset use system default (enabled if local forwarding is disabled.
232 * disabled if local forwarding is enabled).
233 * If set, ignore or enforce RA independent of local forwarding state.
235 if (link
->network
->ipv6_accept_ra
< 0)
236 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
237 return !link_ipv6_forward_enabled(link
);
238 else if (link
->network
->ipv6_accept_ra
> 0)
239 /* accept RA even if ip_forward is enabled */
246 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
249 if (!socket_ipv6_is_supported())
250 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
252 if (link
->flags
& IFF_LOOPBACK
)
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
256 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
258 return link
->network
->ipv6_privacy_extensions
;
261 static int link_enable_ipv6(Link
*link
) {
262 const char *p
= NULL
;
266 if (link
->flags
& IFF_LOOPBACK
)
269 disabled
= !link_ipv6_enabled(link
);
271 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
273 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
275 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
276 enable_disable(!disabled
), link
->ifname
);
278 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
283 void link_update_operstate(Link
*link
) {
284 LinkOperationalState operstate
;
287 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
288 operstate
= LINK_OPERSTATE_DORMANT
;
289 else if (link_has_carrier(link
)) {
291 uint8_t scope
= RT_SCOPE_NOWHERE
;
294 /* if we have carrier, check what addresses we have */
295 SET_FOREACH(address
, link
->addresses
, i
) {
296 if (!address_is_ready(address
))
299 if (address
->scope
< scope
)
300 scope
= address
->scope
;
303 /* for operstate we also take foreign addresses into account */
304 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
305 if (!address_is_ready(address
))
308 if (address
->scope
< scope
)
309 scope
= address
->scope
;
312 if (scope
< RT_SCOPE_SITE
)
313 /* universally accessible addresses found */
314 operstate
= LINK_OPERSTATE_ROUTABLE
;
315 else if (scope
< RT_SCOPE_HOST
)
316 /* only link or site local addresses found */
317 operstate
= LINK_OPERSTATE_DEGRADED
;
319 /* no useful addresses found */
320 operstate
= LINK_OPERSTATE_CARRIER
;
321 } else if (link
->flags
& IFF_UP
)
322 operstate
= LINK_OPERSTATE_NO_CARRIER
;
324 operstate
= LINK_OPERSTATE_OFF
;
326 if (link
->operstate
!= operstate
) {
327 link
->operstate
= operstate
;
328 link_send_changed(link
, "OperationalState", NULL
);
333 #define FLAG_STRING(string, flag, old, new) \
334 (((old ^ new) & flag) \
335 ? ((old & flag) ? (" -" string) : (" +" string)) \
338 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
339 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
345 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
347 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
349 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
351 /* if we got a message without operstate, take it to mean
352 the state was unchanged */
353 operstate
= link
->kernel_operstate
;
355 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
358 if (link
->flags
!= flags
) {
359 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
360 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
361 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
362 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
363 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
364 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
365 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
366 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
367 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
368 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
369 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
370 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
371 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
372 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
373 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
374 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
375 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
376 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
377 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
378 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
380 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
381 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
382 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
383 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
384 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
385 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
386 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
387 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
389 /* link flags are currently at most 18 bits, let's align to
391 if (unknown_flags_added
)
393 "Unknown link flags gained: %#.5x (ignoring)",
394 unknown_flags_added
);
396 if (unknown_flags_removed
)
398 "Unknown link flags lost: %#.5x (ignoring)",
399 unknown_flags_removed
);
403 link
->kernel_operstate
= operstate
;
405 link_update_operstate(link
);
410 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
412 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
413 _cleanup_(link_unrefp
) Link
*link
= NULL
;
415 const char *ifname
, *kind
= NULL
;
417 unsigned short iftype
;
423 /* check for link kind */
424 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
426 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
427 r
= sd_netlink_message_exit_container(message
);
432 r
= sd_netlink_message_get_type(message
, &type
);
435 else if (type
!= RTM_NEWLINK
)
438 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
441 else if (ifindex
<= 0)
444 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
448 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
459 .state
= LINK_STATE_PENDING
,
460 .rtnl_extended_attrs
= true,
465 link
->ifname
= strdup(ifname
);
470 link
->kind
= strdup(kind
);
475 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
477 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
479 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
481 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
483 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
486 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
489 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
492 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
496 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
500 r
= link_update_flags(link
, message
);
504 *ret
= TAKE_PTR(link
);
509 static void link_detach_from_manager(Link
*link
) {
510 if (!link
|| !link
->manager
)
513 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
514 set_remove(link
->manager
->links_requesting_uuid
, link
);
517 link
->manager
= NULL
;
520 static Link
*link_free(Link
*link
) {
528 while ((route
= set_first(link
->routes
)))
531 while ((route
= set_first(link
->routes_foreign
)))
534 link
->routes
= set_free(link
->routes
);
535 link
->routes_foreign
= set_free(link
->routes_foreign
);
537 while ((address
= set_first(link
->addresses
)))
538 address_free(address
);
540 while ((address
= set_first(link
->addresses_foreign
)))
541 address_free(address
);
543 link
->addresses
= set_free(link
->addresses
);
544 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
546 while ((address
= link
->pool_addresses
)) {
547 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
548 address_free(address
);
551 sd_dhcp_server_unref(link
->dhcp_server
);
552 sd_dhcp_client_unref(link
->dhcp_client
);
553 sd_dhcp_lease_unref(link
->dhcp_lease
);
555 link_lldp_emit_stop(link
);
557 free(link
->lease_file
);
559 sd_lldp_unref(link
->lldp
);
560 free(link
->lldp_file
);
564 sd_ipv4ll_unref(link
->ipv4ll
);
565 sd_dhcp6_client_unref(link
->dhcp6_client
);
566 sd_ndisc_unref(link
->ndisc
);
567 sd_radv_unref(link
->radv
);
569 link_detach_from_manager(link
);
575 (void) unlink(link
->state_file
);
576 free(link
->state_file
);
578 sd_device_unref(link
->sd_device
);
580 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
581 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
582 hashmap_free(link
->bound_to_links
);
584 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
585 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
586 hashmap_free(link
->bound_by_links
);
591 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
593 void link_netlink_destroy_callback(void *userdata
) {
594 Link
*link
= userdata
;
601 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
608 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
617 static void link_set_state(Link
*link
, LinkState state
) {
620 if (link
->state
== state
)
625 link_send_changed(link
, "AdministrativeState", NULL
);
628 static void link_enter_unmanaged(Link
*link
) {
631 log_link_debug(link
, "Unmanaged");
633 link_set_state(link
, LINK_STATE_UNMANAGED
);
638 static int link_stop_clients(Link
*link
) {
642 assert(link
->manager
);
643 assert(link
->manager
->event
);
645 if (link
->dhcp_client
) {
646 k
= sd_dhcp_client_stop(link
->dhcp_client
);
648 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
652 k
= sd_ipv4ll_stop(link
->ipv4ll
);
654 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
657 if (link
->dhcp6_client
) {
658 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
660 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
664 k
= sd_ndisc_stop(link
->ndisc
);
666 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
670 k
= sd_radv_stop(link
->radv
);
672 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
675 link_lldp_emit_stop(link
);
679 void link_enter_failed(Link
*link
) {
682 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
685 log_link_warning(link
, "Failed");
687 link_set_state(link
, LINK_STATE_FAILED
);
689 link_stop_clients(link
);
694 static Address
* link_find_dhcp_server_address(Link
*link
) {
698 assert(link
->network
);
700 /* The first statically configured address if there is any */
701 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
703 if (address
->family
!= AF_INET
)
706 if (in_addr_is_null(address
->family
, &address
->in_addr
))
712 /* If that didn't work, find a suitable address we got from the pool */
713 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
714 if (address
->family
!= AF_INET
)
723 static void link_enter_configured(Link
*link
) {
725 assert(link
->network
);
727 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
730 log_link_info(link
, "Configured");
732 link_set_state(link
, LINK_STATE_CONFIGURED
);
737 void link_check_ready(Link
*link
) {
743 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
749 if (!link
->static_routes_configured
)
752 if (!link
->routing_policy_rules_configured
)
755 if (link_ipv4ll_enabled(link
))
756 if (!link
->ipv4ll_address
||
760 if (!link
->network
->bridge
) {
762 if (link_ipv6ll_enabled(link
))
763 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
766 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
767 !link
->dhcp4_configured
) ||
768 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
769 !link
->dhcp6_configured
) ||
770 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
771 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
774 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
778 SET_FOREACH(a
, link
->addresses
, i
)
779 if (!address_is_ready(a
))
782 if (link
->state
!= LINK_STATE_CONFIGURED
)
783 link_enter_configured(link
);
788 static int link_set_routing_policy_rule(Link
*link
) {
789 RoutingPolicyRule
*rule
, *rrule
= NULL
;
793 assert(link
->network
);
795 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
796 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
797 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
799 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
803 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
805 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
806 link_enter_failed(link
);
810 link
->routing_policy_rule_messages
++;
813 routing_policy_rule_purge(link
->manager
, link
);
814 if (link
->routing_policy_rule_messages
== 0) {
815 link
->routing_policy_rules_configured
= true;
816 link_check_ready(link
);
818 log_link_debug(link
, "Setting routing policy rules");
823 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
824 Link
*link
= userdata
;
828 assert(link
->route_messages
> 0);
829 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
830 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
833 link
->route_messages
--;
835 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
838 r
= sd_netlink_message_get_errno(m
);
839 if (r
< 0 && r
!= -EEXIST
)
840 log_link_warning_errno(link
, r
, "Could not set route: %m");
842 if (link
->route_messages
== 0) {
843 log_link_debug(link
, "Routes set");
844 link
->static_routes_configured
= true;
845 link_check_ready(link
);
851 static int link_enter_set_routes(Link
*link
) {
856 assert(link
->network
);
857 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
859 (void) link_set_routing_policy_rule(link
);
861 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
863 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
864 r
= route_configure(rt
, link
, route_handler
);
866 log_link_warning_errno(link
, r
, "Could not set routes: %m");
867 link_enter_failed(link
);
871 link
->route_messages
++;
874 if (link
->route_messages
== 0) {
875 link
->static_routes_configured
= true;
876 link_check_ready(link
);
878 log_link_debug(link
, "Setting routes");
883 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
884 Link
*link
= userdata
;
889 assert(link
->ifname
);
891 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
894 r
= sd_netlink_message_get_errno(m
);
895 if (r
< 0 && r
!= -ESRCH
)
896 log_link_warning_errno(link
, r
, "Could not drop route: %m");
901 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
902 Link
*link
= userdata
;
908 assert(link
->ifname
);
909 assert(link
->address_messages
> 0);
910 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
911 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
913 link
->address_messages
--;
915 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
918 r
= sd_netlink_message_get_errno(m
);
919 if (r
< 0 && r
!= -EEXIST
)
920 log_link_warning_errno(link
, r
, "could not set address: %m");
922 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
924 if (link
->address_messages
== 0) {
925 log_link_debug(link
, "Addresses set");
926 link_enter_set_routes(link
);
932 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
933 Link
*link
= userdata
;
939 assert(link
->ifname
);
940 assert(link
->address_label_messages
> 0);
942 link
->address_label_messages
--;
944 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
947 r
= sd_netlink_message_get_errno(m
);
948 if (r
< 0 && r
!= -EEXIST
)
949 log_link_warning_errno(link
, r
, "could not set address label: %m");
951 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
953 if (link
->address_label_messages
== 0)
954 log_link_debug(link
, "Addresses label set");
959 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
960 _cleanup_free_
struct in_addr
*addresses
= NULL
;
961 size_t n_addresses
= 0, n_allocated
= 0;
964 log_debug("Copying DNS server information from %s", link
->ifname
);
969 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
972 /* Only look for IPv4 addresses */
973 if (link
->network
->dns
[i
].family
!= AF_INET
)
976 ia
= link
->network
->dns
[i
].address
.in
;
978 /* Never propagate obviously borked data */
979 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
982 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
985 addresses
[n_addresses
++] = ia
;
988 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
989 const struct in_addr
*da
= NULL
;
992 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
995 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
998 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1003 if (n_addresses
<= 0)
1006 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1009 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1010 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1011 size_t n_addresses
= 0, n_allocated
= 0;
1017 log_debug("Copying NTP server information from %s", link
->ifname
);
1019 STRV_FOREACH(a
, link
->network
->ntp
) {
1022 /* Only look for IPv4 addresses */
1023 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1026 /* Never propagate obviously borked data */
1027 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1030 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1033 addresses
[n_addresses
++] = ia
;
1036 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1037 const struct in_addr
*da
= NULL
;
1040 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1043 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1046 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1051 if (n_addresses
<= 0)
1054 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1057 static int link_set_bridge_fdb(Link
*link
) {
1058 FdbEntry
*fdb_entry
;
1061 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1062 r
= fdb_entry_configure(link
, fdb_entry
);
1064 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1070 static int link_enter_set_addresses(Link
*link
) {
1071 AddressLabel
*label
;
1076 assert(link
->network
);
1077 assert(link
->state
!= _LINK_STATE_INVALID
);
1079 r
= link_set_bridge_fdb(link
);
1083 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1085 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1086 r
= address_configure(ad
, link
, address_handler
, false);
1088 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1089 link_enter_failed(link
);
1093 link
->address_messages
++;
1096 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1097 r
= address_label_configure(label
, link
, address_label_handler
, false);
1099 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1100 link_enter_failed(link
);
1104 link
->address_label_messages
++;
1107 /* now that we can figure out a default address for the dhcp server,
1109 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1111 Link
*uplink
= NULL
;
1112 bool acquired_uplink
= false;
1114 address
= link_find_dhcp_server_address(link
);
1116 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1117 link_enter_failed(link
);
1121 /* use the server address' subnet as the pool */
1122 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1123 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1128 r = sd_dhcp_server_set_router(link->dhcp_server,
1129 &main_address->in_addr.in);
1134 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1135 r
= sd_dhcp_server_set_max_lease_time(
1137 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1142 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1143 r
= sd_dhcp_server_set_default_lease_time(
1145 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1150 if (link
->network
->dhcp_server_emit_dns
) {
1152 if (link
->network
->n_dhcp_server_dns
> 0)
1153 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1155 uplink
= manager_find_uplink(link
->manager
, link
);
1156 acquired_uplink
= true;
1159 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1162 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1165 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1168 if (link
->network
->dhcp_server_emit_ntp
) {
1170 if (link
->network
->n_dhcp_server_ntp
> 0)
1171 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1173 if (!acquired_uplink
)
1174 uplink
= manager_find_uplink(link
->manager
, link
);
1177 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1180 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1184 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1187 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1189 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1191 if (link
->network
->dhcp_server_emit_timezone
) {
1192 _cleanup_free_
char *buffer
= NULL
;
1193 const char *tz
= NULL
;
1195 if (link
->network
->dhcp_server_timezone
)
1196 tz
= link
->network
->dhcp_server_timezone
;
1198 r
= get_timezone(&buffer
);
1200 log_warning_errno(r
, "Failed to determine timezone: %m");
1206 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1212 r
= sd_dhcp_server_start(link
->dhcp_server
);
1214 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1216 link_enter_failed(link
);
1221 log_link_debug(link
, "Offering DHCPv4 leases");
1224 if (link
->address_messages
== 0)
1225 link_enter_set_routes(link
);
1227 log_link_debug(link
, "Setting addresses");
1232 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1233 Link
*link
= userdata
;
1238 assert(link
->ifname
);
1240 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1243 r
= sd_netlink_message_get_errno(m
);
1244 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1245 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1250 static int link_set_bridge_vlan(Link
*link
) {
1253 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1255 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1260 static int link_set_proxy_arp(Link
*link
) {
1261 const char *p
= NULL
;
1264 if (!link_proxy_arp_enabled(link
))
1267 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1269 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1271 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1276 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1277 Link
*link
= userdata
;
1282 log_link_debug(link
, "Set link");
1284 r
= sd_netlink_message_get_errno(m
);
1285 if (r
< 0 && r
!= -EEXIST
) {
1286 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1287 link_enter_failed(link
);
1293 static int link_configure_after_setting_mtu(Link
*link
);
1295 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1296 Link
*link
= userdata
;
1301 assert(link
->ifname
);
1303 link
->setting_mtu
= false;
1305 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1308 r
= sd_netlink_message_get_errno(m
);
1310 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1314 log_link_debug(link
, "Setting MTU done.");
1316 if (link
->state
== LINK_STATE_PENDING
)
1317 (void) link_configure_after_setting_mtu(link
);
1322 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1323 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1327 assert(link
->manager
);
1328 assert(link
->manager
->rtnl
);
1330 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1333 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1335 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1337 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1339 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1340 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1341 (void) link_enable_ipv6(link
);
1343 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1344 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1345 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1347 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1348 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1353 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1355 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1357 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1358 link_netlink_destroy_callback
, link
, 0, __func__
);
1360 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1363 link
->setting_mtu
= true;
1368 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1369 Link
*link
= userdata
;
1374 assert(link
->ifname
);
1376 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1379 r
= sd_netlink_message_get_errno(m
);
1381 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1386 static int link_set_flags(Link
*link
) {
1387 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1388 unsigned ifi_change
= 0;
1389 unsigned ifi_flags
= 0;
1393 assert(link
->manager
);
1394 assert(link
->manager
->rtnl
);
1396 if (link
->flags
& IFF_LOOPBACK
)
1402 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1405 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1407 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1409 if (link
->network
->arp
>= 0) {
1410 ifi_change
|= IFF_NOARP
;
1411 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1414 if (link
->network
->multicast
>= 0) {
1415 ifi_change
|= IFF_MULTICAST
;
1416 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1419 if (link
->network
->allmulticast
>= 0) {
1420 ifi_change
|= IFF_ALLMULTI
;
1421 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1424 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1426 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1428 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1429 link_netlink_destroy_callback
, link
, 0, __func__
);
1431 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1438 static int link_set_bridge(Link
*link
) {
1439 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1443 assert(link
->network
);
1445 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1447 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1449 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1451 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1453 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1455 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1457 if (link
->network
->use_bpdu
>= 0) {
1458 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1460 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1463 if (link
->network
->hairpin
>= 0) {
1464 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1466 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1469 if (link
->network
->fast_leave
>= 0) {
1470 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1472 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1475 if (link
->network
->allow_port_to_be_root
>= 0) {
1476 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1478 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1482 if (link
->network
->unicast_flood
>= 0) {
1483 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1485 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1489 if (link
->network
->cost
!= 0) {
1490 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1492 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1494 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1495 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1497 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1500 r
= sd_netlink_message_close_container(req
);
1502 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1504 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1505 link_netlink_destroy_callback
, link
, 0, __func__
);
1507 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1514 static int link_bond_set(Link
*link
) {
1515 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1519 assert(link
->network
);
1521 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1523 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1525 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1527 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1529 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1531 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1533 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1535 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1537 if (link
->network
->active_slave
) {
1538 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1540 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1543 if (link
->network
->primary_slave
) {
1544 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1546 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1549 r
= sd_netlink_message_close_container(req
);
1551 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1553 r
= sd_netlink_message_close_container(req
);
1555 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1557 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1558 link_netlink_destroy_callback
, link
, 0, __func__
);
1560 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1567 static int link_lldp_save(Link
*link
) {
1568 _cleanup_free_
char *temp_path
= NULL
;
1569 _cleanup_fclose_
FILE *f
= NULL
;
1570 sd_lldp_neighbor
**l
= NULL
;
1574 assert(link
->lldp_file
);
1577 (void) unlink(link
->lldp_file
);
1581 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1585 (void) unlink(link
->lldp_file
);
1591 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1595 fchmod(fileno(f
), 0644);
1597 for (i
= 0; i
< n
; i
++) {
1602 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1607 (void) fwrite(&u
, 1, sizeof(u
), f
);
1608 (void) fwrite(p
, 1, sz
, f
);
1611 r
= fflush_and_check(f
);
1615 if (rename(temp_path
, link
->lldp_file
) < 0) {
1622 (void) unlink(link
->lldp_file
);
1624 (void) unlink(temp_path
);
1626 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1630 for (i
= 0; i
< n
; i
++)
1631 sd_lldp_neighbor_unref(l
[i
]);
1638 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1639 Link
*link
= userdata
;
1644 (void) link_lldp_save(link
);
1646 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1647 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1649 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1651 r
= link_lldp_emit_start(link
);
1653 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1657 static int link_acquire_ipv6_conf(Link
*link
) {
1662 if (link_ipv6_accept_ra_enabled(link
)) {
1663 assert(link
->ndisc
);
1665 log_link_debug(link
, "Discovering IPv6 routers");
1667 r
= sd_ndisc_start(link
->ndisc
);
1668 if (r
< 0 && r
!= -EBUSY
)
1669 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1672 if (link_radv_enabled(link
)) {
1674 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1676 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1678 r
= sd_radv_start(link
->radv
);
1679 if (r
< 0 && r
!= -EBUSY
)
1680 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1683 (void) dhcp6_request_prefix_delegation(link
);
1688 static int link_acquire_ipv4_conf(Link
*link
) {
1692 assert(link
->network
);
1693 assert(link
->manager
);
1694 assert(link
->manager
->event
);
1696 if (link_ipv4ll_enabled(link
)) {
1697 assert(link
->ipv4ll
);
1699 log_link_debug(link
, "Acquiring IPv4 link-local address");
1701 r
= sd_ipv4ll_start(link
->ipv4ll
);
1703 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1706 if (link_dhcp4_enabled(link
)) {
1707 assert(link
->dhcp_client
);
1709 log_link_debug(link
, "Acquiring DHCPv4 lease");
1711 r
= sd_dhcp_client_start(link
->dhcp_client
);
1713 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1719 static int link_acquire_conf(Link
*link
) {
1724 r
= link_acquire_ipv4_conf(link
);
1728 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1729 r
= link_acquire_ipv6_conf(link
);
1734 if (link_lldp_emit_enabled(link
)) {
1735 r
= link_lldp_emit_start(link
);
1737 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1743 bool link_has_carrier(Link
*link
) {
1744 /* see Documentation/networking/operstates.txt in the kernel sources */
1746 if (link
->kernel_operstate
== IF_OPER_UP
)
1749 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1750 /* operstate may not be implemented, so fall back to flags */
1751 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1757 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1758 Link
*link
= userdata
;
1763 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1766 r
= sd_netlink_message_get_errno(m
);
1768 /* we warn but don't fail the link, as it may be brought up later */
1769 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1774 int link_up(Link
*link
) {
1775 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1776 uint8_t ipv6ll_mode
;
1780 assert(link
->network
);
1781 assert(link
->manager
);
1782 assert(link
->manager
->rtnl
);
1784 log_link_debug(link
, "Bringing link up");
1786 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1788 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1790 /* set it free if not enslaved with networkd */
1791 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1792 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1794 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1797 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1799 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1801 if (link
->network
->mac
) {
1802 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1804 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1807 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1809 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1811 if (link_ipv6_enabled(link
)) {
1812 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1813 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1815 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1817 if (!link_ipv6ll_enabled(link
))
1818 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1820 const char *p
= NULL
;
1821 _cleanup_free_
char *stable_secret
= NULL
;
1823 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1824 r
= read_one_line_file(p
, &stable_secret
);
1827 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1829 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1831 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1833 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1835 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1836 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1838 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1841 r
= sd_netlink_message_close_container(req
);
1843 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1846 r
= sd_netlink_message_close_container(req
);
1848 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1850 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1851 link_netlink_destroy_callback
, link
, 0, __func__
);
1853 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1860 static int link_up_can(Link
*link
) {
1861 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1866 log_link_debug(link
, "Bringing CAN link up");
1868 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1870 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1872 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1874 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1876 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1877 link_netlink_destroy_callback
, link
, 0, __func__
);
1879 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1886 static int link_set_can(Link
*link
) {
1887 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1891 assert(link
->network
);
1892 assert(link
->manager
);
1893 assert(link
->manager
->rtnl
);
1895 log_link_debug(link
, "link_set_can");
1897 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1899 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1901 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1903 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1905 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1907 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1909 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1911 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1913 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1914 struct can_bittiming bt
= {
1915 .bitrate
= link
->network
->can_bitrate
,
1916 .sample_point
= link
->network
->can_sample_point
,
1919 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1920 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1924 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1925 if (link
->network
->can_sample_point
> 0)
1926 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1928 log_link_debug(link
, "Using default sample point");
1930 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1932 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1935 if (link
->network
->can_restart_us
> 0) {
1936 char time_string
[FORMAT_TIMESPAN_MAX
];
1937 uint64_t restart_ms
;
1939 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1942 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1944 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1946 if (restart_ms
> UINT32_MAX
) {
1947 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1951 log_link_debug(link
, "Setting restart = %s", time_string
);
1953 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1955 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1958 r
= sd_netlink_message_close_container(m
);
1960 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1962 r
= sd_netlink_message_close_container(m
);
1964 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1966 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1967 link_netlink_destroy_callback
, link
, 0, __func__
);
1969 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1973 if (!(link
->flags
& IFF_UP
)) {
1974 r
= link_up_can(link
);
1976 link_enter_failed(link
);
1981 log_link_debug(link
, "link_set_can done");
1986 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1987 Link
*link
= userdata
;
1992 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1995 r
= sd_netlink_message_get_errno(m
);
1997 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1999 if (streq_ptr(link
->kind
, "can"))
2005 int link_down(Link
*link
) {
2006 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2010 assert(link
->manager
);
2011 assert(link
->manager
->rtnl
);
2013 log_link_debug(link
, "Bringing link down");
2015 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2016 RTM_SETLINK
, link
->ifindex
);
2018 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2020 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2022 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2024 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2025 link_netlink_destroy_callback
, link
, 0, __func__
);
2027 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2034 static int link_handle_bound_to_list(Link
*link
) {
2038 bool required_up
= false;
2039 bool link_is_up
= false;
2043 if (hashmap_isempty(link
->bound_to_links
))
2046 if (link
->flags
& IFF_UP
)
2049 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2050 if (link_has_carrier(l
)) {
2055 if (!required_up
&& link_is_up
) {
2056 r
= link_down(link
);
2059 } else if (required_up
&& !link_is_up
) {
2068 static int link_handle_bound_by_list(Link
*link
) {
2075 if (hashmap_isempty(link
->bound_by_links
))
2078 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2079 r
= link_handle_bound_to_list(l
);
2087 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2093 if (link
== carrier
)
2096 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2099 r
= hashmap_ensure_allocated(h
, NULL
);
2103 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2110 static int link_new_bound_by_list(Link
*link
) {
2115 bool list_updated
= false;
2118 assert(link
->manager
);
2122 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2123 if (!carrier
->network
)
2126 if (strv_isempty(carrier
->network
->bind_carrier
))
2129 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2130 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2134 list_updated
= true;
2141 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2142 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2146 link_dirty(carrier
);
2152 static int link_new_bound_to_list(Link
*link
) {
2157 bool list_updated
= false;
2160 assert(link
->manager
);
2165 if (strv_isempty(link
->network
->bind_carrier
))
2170 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2171 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2172 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2176 list_updated
= true;
2183 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2184 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2188 link_dirty(carrier
);
2194 static int link_new_carrier_maps(Link
*link
) {
2197 r
= link_new_bound_by_list(link
);
2201 r
= link_handle_bound_by_list(link
);
2205 r
= link_new_bound_to_list(link
);
2209 r
= link_handle_bound_to_list(link
);
2216 static void link_free_bound_to_list(Link
*link
) {
2220 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2221 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2223 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2224 link_dirty(bound_to
);
2230 static void link_free_bound_by_list(Link
*link
) {
2234 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2235 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2237 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2238 link_dirty(bound_by
);
2239 link_handle_bound_to_list(bound_by
);
2246 static void link_free_carrier_maps(Link
*link
) {
2247 bool list_updated
= false;
2251 if (!hashmap_isempty(link
->bound_to_links
)) {
2252 link_free_bound_to_list(link
);
2253 list_updated
= true;
2256 if (!hashmap_isempty(link
->bound_by_links
)) {
2257 link_free_bound_by_list(link
);
2258 list_updated
= true;
2267 void link_drop(Link
*link
) {
2268 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2271 link_set_state(link
, LINK_STATE_LINGER
);
2273 link_free_carrier_maps(link
);
2275 log_link_debug(link
, "Link removed");
2277 (void) unlink(link
->state_file
);
2279 link_detach_from_manager(link
);
2286 static int link_joined(Link
*link
) {
2290 assert(link
->network
);
2292 if (!hashmap_isempty(link
->bound_to_links
)) {
2293 r
= link_handle_bound_to_list(link
);
2296 } else if (!(link
->flags
& IFF_UP
)) {
2299 link_enter_failed(link
);
2304 if (link
->network
->bridge
) {
2305 r
= link_set_bridge(link
);
2307 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2310 if (link
->network
->bond
) {
2311 r
= link_bond_set(link
);
2313 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2316 if (link
->network
->use_br_vlan
&&
2317 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2318 r
= link_set_bridge_vlan(link
);
2320 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2323 /* Skip setting up addresses until it gets carrier,
2324 or it would try to set addresses twice,
2325 which is bad for non-idempotent steps. */
2326 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2329 return link_enter_set_addresses(link
);
2332 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2333 Link
*link
= userdata
;
2337 assert(link
->network
);
2341 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2344 r
= sd_netlink_message_get_errno(m
);
2345 if (r
< 0 && r
!= -EEXIST
) {
2346 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2347 link_enter_failed(link
);
2350 log_link_debug(link
, "Joined netdev");
2352 if (link
->enslaving
<= 0)
2358 static int link_enter_join_netdev(Link
*link
) {
2364 assert(link
->network
);
2365 assert(link
->state
== LINK_STATE_PENDING
);
2367 link_set_state(link
, LINK_STATE_ENSLAVING
);
2371 if (!link
->network
->bridge
&&
2372 !link
->network
->bond
&&
2373 !link
->network
->vrf
&&
2374 hashmap_isempty(link
->network
->stacked_netdevs
))
2375 return link_joined(link
);
2377 if (link
->network
->bond
) {
2378 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2379 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2380 return link_joined(link
);
2382 log_struct(LOG_DEBUG
,
2383 LOG_LINK_INTERFACE(link
),
2384 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2385 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2387 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2389 log_struct_errno(LOG_WARNING
, r
,
2390 LOG_LINK_INTERFACE(link
),
2391 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2392 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2393 link_enter_failed(link
);
2400 if (link
->network
->bridge
) {
2401 log_struct(LOG_DEBUG
,
2402 LOG_LINK_INTERFACE(link
),
2403 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2404 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2406 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2408 log_struct_errno(LOG_WARNING
, r
,
2409 LOG_LINK_INTERFACE(link
),
2410 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2411 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2412 link_enter_failed(link
);
2419 if (link
->network
->vrf
) {
2420 log_struct(LOG_DEBUG
,
2421 LOG_LINK_INTERFACE(link
),
2422 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2423 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2425 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2427 log_struct_errno(LOG_WARNING
, r
,
2428 LOG_LINK_INTERFACE(link
),
2429 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2430 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2431 link_enter_failed(link
);
2438 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2440 if (netdev
->ifindex
> 0) {
2445 log_struct(LOG_DEBUG
,
2446 LOG_LINK_INTERFACE(link
),
2447 LOG_NETDEV_INTERFACE(netdev
),
2448 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2450 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2452 log_struct_errno(LOG_WARNING
, r
,
2453 LOG_LINK_INTERFACE(link
),
2454 LOG_NETDEV_INTERFACE(netdev
),
2455 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2456 link_enter_failed(link
);
2466 static int link_set_ipv4_forward(Link
*link
) {
2469 if (!link_ipv4_forward_enabled(link
))
2472 /* We propagate the forwarding flag from one interface to the
2473 * global setting one way. This means: as long as at least one
2474 * interface was configured at any time that had IP forwarding
2475 * enabled the setting will stay on for good. We do this
2476 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2477 * somewhat in sync (see below). */
2479 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2481 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2486 static int link_set_ipv6_forward(Link
*link
) {
2489 if (!link_ipv6_forward_enabled(link
))
2492 /* On Linux, the IPv6 stack does not know a per-interface
2493 * packet forwarding setting: either packet forwarding is on
2494 * for all, or off for all. We hence don't bother with a
2495 * per-interface setting, but simply propagate the interface
2496 * flag, if it is set, to the global flag, one-way. Note that
2497 * while IPv4 would allow a per-interface flag, we expose the
2498 * same behaviour there and also propagate the setting from
2499 * one to all, to keep things simple (see above). */
2501 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2503 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2508 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2509 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2510 IPv6PrivacyExtensions s
;
2511 const char *p
= NULL
;
2514 s
= link_ipv6_privacy_extensions(link
);
2518 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2519 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2521 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2523 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2528 static int link_set_ipv6_accept_ra(Link
*link
) {
2529 const char *p
= NULL
;
2532 /* Make this a NOP if IPv6 is not available */
2533 if (!socket_ipv6_is_supported())
2536 if (link
->flags
& IFF_LOOPBACK
)
2542 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2544 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2545 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2547 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2552 static int link_set_ipv6_dad_transmits(Link
*link
) {
2553 char buf
[DECIMAL_STR_MAX(int) + 1];
2554 const char *p
= NULL
;
2557 /* Make this a NOP if IPv6 is not available */
2558 if (!socket_ipv6_is_supported())
2561 if (link
->flags
& IFF_LOOPBACK
)
2567 if (link
->network
->ipv6_dad_transmits
< 0)
2570 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2571 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2573 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2575 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2580 static int link_set_ipv6_hop_limit(Link
*link
) {
2581 char buf
[DECIMAL_STR_MAX(int) + 1];
2582 const char *p
= NULL
;
2585 /* Make this a NOP if IPv6 is not available */
2586 if (!socket_ipv6_is_supported())
2589 if (link
->flags
& IFF_LOOPBACK
)
2595 if (link
->network
->ipv6_hop_limit
< 0)
2598 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2599 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2601 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2603 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2608 static int link_set_ipv6_mtu(Link
*link
) {
2609 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2610 const char *p
= NULL
;
2613 /* Make this a NOP if IPv6 is not available */
2614 if (!socket_ipv6_is_supported())
2617 if (link
->flags
& IFF_LOOPBACK
)
2620 if (link
->network
->ipv6_mtu
== 0)
2623 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2625 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2627 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2629 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2634 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2635 Address
*net_address
;
2643 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2644 if (address_equal(net_address
, address
))
2650 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2659 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2660 if (route_equal(net_route
, route
))
2666 static int link_drop_foreign_config(Link
*link
) {
2672 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2673 /* we consider IPv6LL addresses to be managed by the kernel */
2674 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2677 if (link_is_static_address_configured(link
, address
)) {
2678 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2680 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2682 r
= address_remove(address
, link
, link_address_remove_handler
);
2688 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2689 /* do not touch routes managed by the kernel */
2690 if (route
->protocol
== RTPROT_KERNEL
)
2693 if (link_is_static_route_configured(link
, route
)) {
2694 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2698 r
= route_remove(route
, link
, link_route_remove_handler
);
2707 static int link_drop_config(Link
*link
) {
2708 Address
*address
, *pool_address
;
2713 SET_FOREACH(address
, link
->addresses
, i
) {
2714 /* we consider IPv6LL addresses to be managed by the kernel */
2715 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2718 r
= address_remove(address
, link
, link_address_remove_handler
);
2722 /* If this address came from an address pool, clean up the pool */
2723 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2724 if (address_equal(address
, pool_address
)) {
2725 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2726 address_free(pool_address
);
2732 SET_FOREACH(route
, link
->routes
, i
) {
2733 /* do not touch routes managed by the kernel */
2734 if (route
->protocol
== RTPROT_KERNEL
)
2737 r
= route_remove(route
, link
, link_route_remove_handler
);
2747 static int link_update_lldp(Link
*link
) {
2755 if (link
->flags
& IFF_UP
) {
2756 r
= sd_lldp_start(link
->lldp
);
2758 log_link_debug(link
, "Started LLDP.");
2760 r
= sd_lldp_stop(link
->lldp
);
2762 log_link_debug(link
, "Stopped LLDP.");
2768 static int link_configure_can(Link
*link
) {
2771 if (streq_ptr(link
->kind
, "can")) {
2772 /* The CAN interface must be down to configure bitrate, etc... */
2773 if ((link
->flags
& IFF_UP
)) {
2774 r
= link_down(link
);
2776 link_enter_failed(link
);
2783 return link_set_can(link
);
2786 if (!(link
->flags
& IFF_UP
)) {
2787 r
= link_up_can(link
);
2789 link_enter_failed(link
);
2797 static int link_configure(Link
*link
) {
2801 assert(link
->network
);
2802 assert(link
->state
== LINK_STATE_PENDING
);
2804 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2805 return link_configure_can(link
);
2807 /* Drop foreign config, but ignore loopback or critical devices.
2808 * We do not want to remove loopback address or addresses used for root NFS. */
2809 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2810 r
= link_drop_foreign_config(link
);
2815 r
= link_set_proxy_arp(link
);
2819 r
= ipv6_proxy_ndp_addresses_configure(link
);
2823 r
= link_set_ipv4_forward(link
);
2827 r
= link_set_ipv6_forward(link
);
2831 r
= link_set_ipv6_privacy_extensions(link
);
2835 r
= link_set_ipv6_accept_ra(link
);
2839 r
= link_set_ipv6_dad_transmits(link
);
2843 r
= link_set_ipv6_hop_limit(link
);
2847 r
= link_set_flags(link
);
2851 r
= link_set_ipv6_mtu(link
);
2855 if (link_ipv4ll_enabled(link
)) {
2856 r
= ipv4ll_configure(link
);
2861 if (link_dhcp4_enabled(link
)) {
2862 r
= dhcp4_set_promote_secondaries(link
);
2866 r
= dhcp4_configure(link
);
2871 if (link_dhcp4_server_enabled(link
)) {
2872 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2876 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2881 if (link_dhcp6_enabled(link
) ||
2882 link_ipv6_accept_ra_enabled(link
)) {
2883 r
= dhcp6_configure(link
);
2888 if (link_ipv6_accept_ra_enabled(link
)) {
2889 r
= ndisc_configure(link
);
2894 if (link_radv_enabled(link
)) {
2895 r
= radv_configure(link
);
2900 if (link_lldp_rx_enabled(link
)) {
2901 r
= sd_lldp_new(&link
->lldp
);
2905 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2909 r
= sd_lldp_match_capabilities(link
->lldp
,
2910 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2911 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2912 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2916 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2920 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2924 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2928 r
= link_update_lldp(link
);
2933 if (link
->network
->mtu
> 0) {
2934 r
= link_set_mtu(link
, link
->network
->mtu
);
2939 return link_configure_after_setting_mtu(link
);
2942 static int link_configure_after_setting_mtu(Link
*link
) {
2946 assert(link
->network
);
2947 assert(link
->state
== LINK_STATE_PENDING
);
2949 if (link
->setting_mtu
)
2952 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2953 r
= link_acquire_conf(link
);
2958 return link_enter_join_netdev(link
);
2961 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2964 if (duid
->raw_data_len
> 0)
2967 if (duid
->type
!= DUID_TYPE_UUID
)
2970 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2971 duid
->raw_data_len
= sizeof(sd_id128_t
);
2976 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2977 Manager
*manager
= userdata
;
2978 const sd_bus_error
*e
;
2988 e
= sd_bus_message_get_error(m
);
2990 log_error_errno(sd_bus_error_get_errno(e
),
2991 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2996 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3000 if (sz
!= sizeof(sd_id128_t
)) {
3001 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3005 memcpy(&manager
->product_uuid
, a
, sz
);
3006 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3007 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3009 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3012 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3013 r
= link_configure(link
);
3015 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3018 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3020 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3021 * even if the method fails. */
3022 manager
->has_product_uuid
= true;
3027 static bool link_requires_uuid(Link
*link
) {
3031 assert(link
->manager
);
3032 assert(link
->network
);
3034 duid
= link_get_duid(link
);
3035 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3038 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3041 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3047 static int link_configure_duid(Link
*link
) {
3053 assert(link
->manager
);
3054 assert(link
->network
);
3057 duid
= link_get_duid(link
);
3059 if (!link_requires_uuid(link
))
3062 if (m
->has_product_uuid
) {
3063 (void) duid_set_uuid(duid
, m
->product_uuid
);
3067 if (!m
->links_requesting_uuid
) {
3068 r
= manager_request_product_uuid(m
, link
);
3073 log_link_warning_errno(link
, r
,
3074 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3078 r
= set_put(m
->links_requesting_uuid
, link
);
3082 r
= set_put(m
->duids_requesting_uuid
, duid
);
3090 static int link_initialized_and_synced(Link
*link
) {
3095 assert(link
->ifname
);
3096 assert(link
->manager
);
3098 if (link
->state
!= LINK_STATE_PENDING
)
3101 log_link_debug(link
, "Link state is up-to-date");
3103 r
= link_new_bound_by_list(link
);
3107 r
= link_handle_bound_by_list(link
);
3111 if (!link
->network
) {
3112 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3113 &link
->mac
, &network
);
3115 link_enter_unmanaged(link
);
3117 } else if (r
== 0 && network
->unmanaged
) {
3118 link_enter_unmanaged(link
);
3123 if (link
->flags
& IFF_LOOPBACK
) {
3124 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3125 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3127 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3128 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3130 if (network
->dhcp_server
)
3131 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3134 r
= network_apply(network
, link
);
3139 r
= link_new_bound_to_list(link
);
3143 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3144 * link_configure() is called later asynchronously. */
3145 r
= link_configure_duid(link
);
3149 r
= link_configure(link
);
3156 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
3157 (void) link_initialized_and_synced(userdata
);
3161 int link_initialized(Link
*link
, sd_device
*device
) {
3162 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3166 assert(link
->manager
);
3167 assert(link
->manager
->rtnl
);
3170 if (link
->state
!= LINK_STATE_PENDING
)
3173 if (link
->sd_device
)
3176 log_link_debug(link
, "udev initialized link");
3178 link
->sd_device
= sd_device_ref(device
);
3180 /* udev has initialized the link, but we don't know if we have yet
3181 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3182 * when it returns we know that the pending NEWLINKs have already been
3183 * processed and that we are up-to-date */
3185 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3190 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3191 link_netlink_destroy_callback
, link
, 0, __func__
);
3200 static int link_load(Link
*link
) {
3201 _cleanup_free_
char *network_file
= NULL
,
3204 *dhcp4_address
= NULL
,
3205 *ipv4ll_address
= NULL
;
3206 union in_addr_union address
;
3207 union in_addr_union route_dst
;
3213 r
= parse_env_file(NULL
, link
->state_file
,
3214 "NETWORK_FILE", &network_file
,
3215 "ADDRESSES", &addresses
,
3217 "DHCP4_ADDRESS", &dhcp4_address
,
3218 "IPV4LL_ADDRESS", &ipv4ll_address
);
3219 if (r
< 0 && r
!= -ENOENT
)
3220 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3227 suffix
= strrchr(network_file
, '.');
3229 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3230 goto network_file_fail
;
3234 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3236 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3237 goto network_file_fail
;
3240 r
= network_apply(network
, link
);
3242 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3251 _cleanup_free_
char *address_str
= NULL
;
3252 char *prefixlen_str
;
3254 unsigned char prefixlen
;
3256 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3258 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3264 prefixlen_str
= strchr(address_str
, '/');
3265 if (!prefixlen_str
) {
3266 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3270 *prefixlen_str
++ = '\0';
3272 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3274 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3278 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3280 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3284 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3286 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3295 _cleanup_free_
char *route_str
= NULL
;
3296 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3298 char *prefixlen_str
;
3300 unsigned char prefixlen
, tos
, table
;
3303 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3305 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3311 prefixlen_str
= strchr(route_str
, '/');
3312 if (!prefixlen_str
) {
3313 log_link_debug(link
, "Failed to parse route %s", route_str
);
3317 *prefixlen_str
++ = '\0';
3319 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3321 log_link_debug(link
,
3322 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3327 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3329 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3333 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3335 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3337 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3338 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3339 0, route_expire_handler
, route
);
3341 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3344 route
->lifetime
= lifetime
;
3345 sd_event_source_unref(route
->expire
);
3346 route
->expire
= TAKE_PTR(expire
);
3350 if (dhcp4_address
) {
3351 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3353 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3354 goto dhcp4_address_fail
;
3357 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3359 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3361 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3363 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3368 if (ipv4ll_address
) {
3369 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3371 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3372 goto ipv4ll_address_fail
;
3375 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3377 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3379 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3381 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3384 ipv4ll_address_fail
:
3389 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3390 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3391 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3400 r
= link_new(m
, message
, ret
);
3406 log_link_debug(link
, "Link %d added", link
->ifindex
);
3408 r
= link_load(link
);
3412 if (detect_container() <= 0) {
3413 /* not in a container, udev will be around */
3414 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3415 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3417 log_link_warning_errno(link
, r
, "Could not find device: %m");
3421 r
= sd_device_get_is_initialized(device
);
3423 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3428 log_link_debug(link
, "link pending udev initialization...");
3432 r
= link_initialized(link
, device
);
3436 r
= link_initialized_and_synced(link
);
3443 link_enter_failed(link
);
3447 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3452 log_link_info(link
, "Gained IPv6LL");
3454 link
->ipv6ll_address
= *address
;
3455 link_check_ready(link
);
3457 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3458 r
= link_acquire_ipv6_conf(link
);
3460 link_enter_failed(link
);
3468 static int link_carrier_gained(Link
*link
) {
3473 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3474 r
= link_acquire_conf(link
);
3476 link_enter_failed(link
);
3480 r
= link_enter_set_addresses(link
);
3485 r
= link_handle_bound_by_list(link
);
3492 static int link_carrier_lost(Link
*link
) {
3497 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3498 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3499 * configuration and stop the clients as well. */
3500 if (link
->setting_mtu
)
3503 r
= link_stop_clients(link
);
3505 link_enter_failed(link
);
3509 if (link_dhcp4_server_enabled(link
))
3510 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3512 r
= link_drop_config(link
);
3516 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3517 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3518 r
= link_drop_foreign_config(link
);
3523 r
= link_handle_bound_by_list(link
);
3530 int link_carrier_reset(Link
*link
) {
3535 if (link_has_carrier(link
)) {
3536 r
= link_carrier_lost(link
);
3540 r
= link_carrier_gained(link
);
3544 log_link_info(link
, "Reset carrier");
3550 int link_update(Link
*link
, sd_netlink_message
*m
) {
3551 struct ether_addr mac
;
3554 bool had_carrier
, carrier_gained
, carrier_lost
;
3558 assert(link
->ifname
);
3561 if (link
->state
== LINK_STATE_LINGER
) {
3562 log_link_info(link
, "Link readded");
3563 link_set_state(link
, LINK_STATE_ENSLAVING
);
3565 r
= link_new_carrier_maps(link
);
3570 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3571 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3572 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3574 if (link
->state
== LINK_STATE_PENDING
) {
3575 r
= free_and_strdup(&link
->ifname
, ifname
);
3579 Manager
*manager
= link
->manager
;
3582 r
= link_add(manager
, m
, &link
);
3588 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3589 if (r
>= 0 && mtu
> 0) {
3591 if (link
->original_mtu
== 0) {
3592 link
->original_mtu
= mtu
;
3593 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3596 if (link
->dhcp_client
) {
3597 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3600 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3604 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3606 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3610 /* The kernel may broadcast NEWLINK messages without the MAC address
3611 set, simply ignore them. */
3612 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3614 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3617 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3620 log_link_debug(link
, "MAC address: "
3621 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3622 mac
.ether_addr_octet
[0],
3623 mac
.ether_addr_octet
[1],
3624 mac
.ether_addr_octet
[2],
3625 mac
.ether_addr_octet
[3],
3626 mac
.ether_addr_octet
[4],
3627 mac
.ether_addr_octet
[5]);
3630 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3632 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3635 if (link
->dhcp_client
) {
3636 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3637 (const uint8_t *) &link
->mac
,
3641 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3643 r
= dhcp4_set_client_identifier(link
);
3648 if (link
->dhcp6_client
) {
3649 const DUID
* duid
= link_get_duid(link
);
3651 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3652 (const uint8_t *) &link
->mac
,
3656 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3658 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3659 link
->network
->iaid
);
3661 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3663 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3665 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3666 duid
->raw_data_len
);
3668 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3672 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3674 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3678 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3680 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3685 had_carrier
= link_has_carrier(link
);
3687 r
= link_update_flags(link
, m
);
3691 r
= link_update_lldp(link
);
3695 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3696 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3698 if (carrier_gained
) {
3699 log_link_info(link
, "Gained carrier");
3701 r
= link_carrier_gained(link
);
3704 } else if (carrier_lost
) {
3705 log_link_info(link
, "Lost carrier");
3707 r
= link_carrier_lost(link
);
3715 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3723 if (hashmap_isempty(h
))
3727 HASHMAP_FOREACH(link
, h
, i
) {
3731 fprintf(f
, "%i", link
->ifindex
);
3738 int link_save(Link
*link
) {
3739 _cleanup_free_
char *temp_path
= NULL
;
3740 _cleanup_fclose_
FILE *f
= NULL
;
3741 const char *admin_state
, *oper_state
;
3748 assert(link
->state_file
);
3749 assert(link
->lease_file
);
3750 assert(link
->manager
);
3752 if (link
->state
== LINK_STATE_LINGER
) {
3753 unlink(link
->state_file
);
3757 link_lldp_save(link
);
3759 admin_state
= link_state_to_string(link
->state
);
3760 assert(admin_state
);
3762 oper_state
= link_operstate_to_string(link
->operstate
);
3765 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3769 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3770 (void) fchmod(fileno(f
), 0644);
3773 "# This is private data. Do not parse.\n"
3776 admin_state
, oper_state
);
3778 if (link
->network
) {
3780 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3781 const char *dhcp_domainname
= NULL
;
3782 char **dhcp6_domains
= NULL
;
3783 char **dhcp_domains
= NULL
;
3786 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3787 yes_no(link
->network
->required_for_online
));
3789 if (link
->dhcp6_client
) {
3790 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3791 if (r
< 0 && r
!= -ENOMSG
)
3792 log_link_debug(link
, "No DHCPv6 lease");
3795 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3800 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3801 _cleanup_free_
char *b
= NULL
;
3803 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3804 &link
->network
->dns
[j
].address
, &b
);
3806 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3816 if (link
->network
->dhcp_use_dns
&&
3818 const struct in_addr
*addresses
;
3820 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3824 serialize_in_addrs(f
, addresses
, r
);
3829 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3830 struct in6_addr
*in6_addrs
;
3832 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3836 serialize_in6_addrs(f
, in6_addrs
, r
);
3841 /* Make sure to flush out old entries before we use the NDISC data */
3844 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3847 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3851 serialize_in6_addrs(f
, &dd
->address
, 1);
3860 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3862 if (link
->network
->dhcp_use_ntp
&&
3864 const struct in_addr
*addresses
;
3866 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3870 serialize_in_addrs(f
, addresses
, r
);
3875 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3876 struct in6_addr
*in6_addrs
;
3879 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3884 serialize_in6_addrs(f
, in6_addrs
, r
);
3888 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3890 fputstrv(f
, hosts
, NULL
, &space
);
3895 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3896 if (link
->dhcp_lease
) {
3897 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3898 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3901 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3904 fputs("DOMAINS=", f
);
3906 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3908 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3911 if (dhcp_domainname
)
3912 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3914 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3916 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3918 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3919 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3924 fputs("ROUTE_DOMAINS=", f
);
3926 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3928 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3931 if (dhcp_domainname
)
3932 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3934 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3936 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3938 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3939 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3944 fprintf(f
, "LLMNR=%s\n",
3945 resolve_support_to_string(link
->network
->llmnr
));
3946 fprintf(f
, "MDNS=%s\n",
3947 resolve_support_to_string(link
->network
->mdns
));
3949 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3950 fprintf(f
, "DNS_OVER_TLS=%s\n",
3951 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3953 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3954 fprintf(f
, "DNSSEC=%s\n",
3955 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3957 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3960 fputs("DNSSEC_NTA=", f
);
3962 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3963 fputs_with_space(f
, n
, NULL
, &space
);
3967 fputs("ADDRESSES=", f
);
3969 SET_FOREACH(a
, link
->addresses
, i
) {
3970 _cleanup_free_
char *address_str
= NULL
;
3972 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3976 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3981 fputs("ROUTES=", f
);
3983 SET_FOREACH(route
, link
->routes
, i
) {
3984 _cleanup_free_
char *route_str
= NULL
;
3986 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3990 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3991 space
? " " : "", route_str
,
3992 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3999 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4000 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4002 if (link
->dhcp_lease
) {
4003 struct in_addr address
;
4004 const char *tz
= NULL
;
4006 assert(link
->network
);
4008 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4010 fprintf(f
, "TIMEZONE=%s\n", tz
);
4012 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4014 fputs("DHCP4_ADDRESS=", f
);
4015 serialize_in_addrs(f
, &address
, 1);
4019 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4027 unlink(link
->lease_file
);
4030 struct in_addr address
;
4032 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4034 fputs("IPV4LL_ADDRESS=", f
);
4035 serialize_in_addrs(f
, &address
, 1);
4040 r
= fflush_and_check(f
);
4044 if (rename(temp_path
, link
->state_file
) < 0) {
4052 (void) unlink(link
->state_file
);
4054 (void) unlink(temp_path
);
4056 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4059 /* The serialized state in /run is no longer up-to-date. */
4060 void link_dirty(Link
*link
) {
4065 /* mark manager dirty as link is dirty */
4066 manager_dirty(link
->manager
);
4068 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4070 /* allocation errors are ignored */
4073 r
= set_put(link
->manager
->dirty_links
, link
);
4075 /* don't take another ref if the link was already dirty */
4081 /* The serialized state in /run is up-to-date */
4082 void link_clean(Link
*link
) {
4084 assert(link
->manager
);
4086 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4089 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4090 [LINK_STATE_PENDING
] = "pending",
4091 [LINK_STATE_ENSLAVING
] = "configuring",
4092 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4093 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4094 [LINK_STATE_CONFIGURED
] = "configured",
4095 [LINK_STATE_UNMANAGED
] = "unmanaged",
4096 [LINK_STATE_FAILED
] = "failed",
4097 [LINK_STATE_LINGER
] = "linger",
4100 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4102 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4103 [LINK_OPERSTATE_OFF
] = "off",
4104 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4105 [LINK_OPERSTATE_DORMANT
] = "dormant",
4106 [LINK_OPERSTATE_CARRIER
] = "carrier",
4107 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4108 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4111 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);