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
);
452 link
= new0(Link
, 1);
457 link
->manager
= manager
;
458 link
->state
= LINK_STATE_PENDING
;
459 link
->rtnl_extended_attrs
= true;
460 link
->ifindex
= ifindex
;
461 link
->iftype
= iftype
;
462 link
->ifname
= strdup(ifname
);
467 link
->kind
= strdup(kind
);
472 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
474 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
476 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
478 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
480 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
483 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
486 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
489 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
493 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
497 r
= link_update_flags(link
, message
);
501 *ret
= TAKE_PTR(link
);
506 static void link_detach_from_manager(Link
*link
) {
507 if (!link
|| !link
->manager
)
510 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
511 set_remove(link
->manager
->links_requesting_uuid
, link
);
514 link
->manager
= NULL
;
517 static Link
*link_free(Link
*link
) {
525 while ((route
= set_first(link
->routes
)))
528 while ((route
= set_first(link
->routes_foreign
)))
531 link
->routes
= set_free(link
->routes
);
532 link
->routes_foreign
= set_free(link
->routes_foreign
);
534 while ((address
= set_first(link
->addresses
)))
535 address_free(address
);
537 while ((address
= set_first(link
->addresses_foreign
)))
538 address_free(address
);
540 link
->addresses
= set_free(link
->addresses
);
541 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
543 while ((address
= link
->pool_addresses
)) {
544 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
545 address_free(address
);
548 sd_dhcp_server_unref(link
->dhcp_server
);
549 sd_dhcp_client_unref(link
->dhcp_client
);
550 sd_dhcp_lease_unref(link
->dhcp_lease
);
552 link_lldp_emit_stop(link
);
554 free(link
->lease_file
);
556 sd_lldp_unref(link
->lldp
);
557 free(link
->lldp_file
);
561 sd_ipv4ll_unref(link
->ipv4ll
);
562 sd_dhcp6_client_unref(link
->dhcp6_client
);
563 sd_ndisc_unref(link
->ndisc
);
564 sd_radv_unref(link
->radv
);
566 link_detach_from_manager(link
);
572 (void) unlink(link
->state_file
);
573 free(link
->state_file
);
575 sd_device_unref(link
->sd_device
);
577 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
578 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
579 hashmap_free(link
->bound_to_links
);
581 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
582 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
583 hashmap_free(link
->bound_by_links
);
588 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
590 void link_netlink_destroy_callback(void *userdata
) {
591 Link
*link
= userdata
;
598 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
605 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
614 static void link_set_state(Link
*link
, LinkState state
) {
617 if (link
->state
== state
)
622 link_send_changed(link
, "AdministrativeState", NULL
);
625 static void link_enter_unmanaged(Link
*link
) {
628 log_link_debug(link
, "Unmanaged");
630 link_set_state(link
, LINK_STATE_UNMANAGED
);
635 static int link_stop_clients(Link
*link
) {
639 assert(link
->manager
);
640 assert(link
->manager
->event
);
642 if (link
->dhcp_client
) {
643 k
= sd_dhcp_client_stop(link
->dhcp_client
);
645 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
649 k
= sd_ipv4ll_stop(link
->ipv4ll
);
651 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
654 if (link
->dhcp6_client
) {
655 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
657 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
661 k
= sd_ndisc_stop(link
->ndisc
);
663 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
667 k
= sd_radv_stop(link
->radv
);
669 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
672 link_lldp_emit_stop(link
);
676 void link_enter_failed(Link
*link
) {
679 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
682 log_link_warning(link
, "Failed");
684 link_set_state(link
, LINK_STATE_FAILED
);
686 link_stop_clients(link
);
691 static Address
* link_find_dhcp_server_address(Link
*link
) {
695 assert(link
->network
);
697 /* The first statically configured address if there is any */
698 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
700 if (address
->family
!= AF_INET
)
703 if (in_addr_is_null(address
->family
, &address
->in_addr
))
709 /* If that didn't work, find a suitable address we got from the pool */
710 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
711 if (address
->family
!= AF_INET
)
720 static void link_enter_configured(Link
*link
) {
722 assert(link
->network
);
724 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
727 log_link_info(link
, "Configured");
729 link_set_state(link
, LINK_STATE_CONFIGURED
);
734 void link_check_ready(Link
*link
) {
740 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
746 if (!link
->static_routes_configured
)
749 if (!link
->routing_policy_rules_configured
)
752 if (link_ipv4ll_enabled(link
))
753 if (!link
->ipv4ll_address
||
757 if (!link
->network
->bridge
) {
759 if (link_ipv6ll_enabled(link
))
760 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
763 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
764 !link
->dhcp4_configured
) ||
765 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
766 !link
->dhcp6_configured
) ||
767 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
768 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
771 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
775 SET_FOREACH(a
, link
->addresses
, i
)
776 if (!address_is_ready(a
))
779 if (link
->state
!= LINK_STATE_CONFIGURED
)
780 link_enter_configured(link
);
785 static int link_set_routing_policy_rule(Link
*link
) {
786 RoutingPolicyRule
*rule
, *rrule
= NULL
;
790 assert(link
->network
);
792 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
793 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
794 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
796 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
800 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
802 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
803 link_enter_failed(link
);
807 link
->routing_policy_rule_messages
++;
810 routing_policy_rule_purge(link
->manager
, link
);
811 if (link
->routing_policy_rule_messages
== 0) {
812 link
->routing_policy_rules_configured
= true;
813 link_check_ready(link
);
815 log_link_debug(link
, "Setting routing policy rules");
820 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
821 Link
*link
= userdata
;
825 assert(link
->route_messages
> 0);
826 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
827 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
830 link
->route_messages
--;
832 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
835 r
= sd_netlink_message_get_errno(m
);
836 if (r
< 0 && r
!= -EEXIST
)
837 log_link_warning_errno(link
, r
, "Could not set route: %m");
839 if (link
->route_messages
== 0) {
840 log_link_debug(link
, "Routes set");
841 link
->static_routes_configured
= true;
842 link_check_ready(link
);
848 static int link_enter_set_routes(Link
*link
) {
853 assert(link
->network
);
854 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
856 (void) link_set_routing_policy_rule(link
);
858 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
860 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
861 r
= route_configure(rt
, link
, route_handler
);
863 log_link_warning_errno(link
, r
, "Could not set routes: %m");
864 link_enter_failed(link
);
868 link
->route_messages
++;
871 if (link
->route_messages
== 0) {
872 link
->static_routes_configured
= true;
873 link_check_ready(link
);
875 log_link_debug(link
, "Setting routes");
880 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
881 Link
*link
= userdata
;
886 assert(link
->ifname
);
888 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
891 r
= sd_netlink_message_get_errno(m
);
892 if (r
< 0 && r
!= -ESRCH
)
893 log_link_warning_errno(link
, r
, "Could not drop route: %m");
898 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
899 Link
*link
= userdata
;
905 assert(link
->ifname
);
906 assert(link
->address_messages
> 0);
907 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
908 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
910 link
->address_messages
--;
912 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
915 r
= sd_netlink_message_get_errno(m
);
916 if (r
< 0 && r
!= -EEXIST
)
917 log_link_warning_errno(link
, r
, "could not set address: %m");
919 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
921 if (link
->address_messages
== 0) {
922 log_link_debug(link
, "Addresses set");
923 link_enter_set_routes(link
);
929 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
930 Link
*link
= userdata
;
936 assert(link
->ifname
);
937 assert(link
->address_label_messages
> 0);
939 link
->address_label_messages
--;
941 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
944 r
= sd_netlink_message_get_errno(m
);
945 if (r
< 0 && r
!= -EEXIST
)
946 log_link_warning_errno(link
, r
, "could not set address label: %m");
948 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
950 if (link
->address_label_messages
== 0)
951 log_link_debug(link
, "Addresses label set");
956 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
957 _cleanup_free_
struct in_addr
*addresses
= NULL
;
958 size_t n_addresses
= 0, n_allocated
= 0;
961 log_debug("Copying DNS server information from %s", link
->ifname
);
966 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
969 /* Only look for IPv4 addresses */
970 if (link
->network
->dns
[i
].family
!= AF_INET
)
973 ia
= link
->network
->dns
[i
].address
.in
;
975 /* Never propagate obviously borked data */
976 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
979 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
982 addresses
[n_addresses
++] = ia
;
985 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
986 const struct in_addr
*da
= NULL
;
989 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
992 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
995 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1000 if (n_addresses
<= 0)
1003 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1006 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1007 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1008 size_t n_addresses
= 0, n_allocated
= 0;
1014 log_debug("Copying NTP server information from %s", link
->ifname
);
1016 STRV_FOREACH(a
, link
->network
->ntp
) {
1019 /* Only look for IPv4 addresses */
1020 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1023 /* Never propagate obviously borked data */
1024 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1027 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1030 addresses
[n_addresses
++] = ia
;
1033 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1034 const struct in_addr
*da
= NULL
;
1037 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1040 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1043 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1048 if (n_addresses
<= 0)
1051 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1054 static int link_set_bridge_fdb(Link
*link
) {
1055 FdbEntry
*fdb_entry
;
1058 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1059 r
= fdb_entry_configure(link
, fdb_entry
);
1061 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1067 static int link_enter_set_addresses(Link
*link
) {
1068 AddressLabel
*label
;
1073 assert(link
->network
);
1074 assert(link
->state
!= _LINK_STATE_INVALID
);
1076 r
= link_set_bridge_fdb(link
);
1080 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1082 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1083 r
= address_configure(ad
, link
, address_handler
, false);
1085 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1086 link_enter_failed(link
);
1090 link
->address_messages
++;
1093 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1094 r
= address_label_configure(label
, link
, address_label_handler
, false);
1096 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1097 link_enter_failed(link
);
1101 link
->address_label_messages
++;
1104 /* now that we can figure out a default address for the dhcp server,
1106 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1108 Link
*uplink
= NULL
;
1109 bool acquired_uplink
= false;
1111 address
= link_find_dhcp_server_address(link
);
1113 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1114 link_enter_failed(link
);
1118 /* use the server address' subnet as the pool */
1119 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1120 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1125 r = sd_dhcp_server_set_router(link->dhcp_server,
1126 &main_address->in_addr.in);
1131 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1132 r
= sd_dhcp_server_set_max_lease_time(
1134 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1139 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1140 r
= sd_dhcp_server_set_default_lease_time(
1142 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1147 if (link
->network
->dhcp_server_emit_dns
) {
1149 if (link
->network
->n_dhcp_server_dns
> 0)
1150 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1152 uplink
= manager_find_uplink(link
->manager
, link
);
1153 acquired_uplink
= true;
1156 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1159 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1162 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1165 if (link
->network
->dhcp_server_emit_ntp
) {
1167 if (link
->network
->n_dhcp_server_ntp
> 0)
1168 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1170 if (!acquired_uplink
)
1171 uplink
= manager_find_uplink(link
->manager
, link
);
1174 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1177 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1181 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1184 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1186 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1188 if (link
->network
->dhcp_server_emit_timezone
) {
1189 _cleanup_free_
char *buffer
= NULL
;
1190 const char *tz
= NULL
;
1192 if (link
->network
->dhcp_server_timezone
)
1193 tz
= link
->network
->dhcp_server_timezone
;
1195 r
= get_timezone(&buffer
);
1197 log_warning_errno(r
, "Failed to determine timezone: %m");
1203 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1209 r
= sd_dhcp_server_start(link
->dhcp_server
);
1211 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1213 link_enter_failed(link
);
1218 log_link_debug(link
, "Offering DHCPv4 leases");
1221 if (link
->address_messages
== 0)
1222 link_enter_set_routes(link
);
1224 log_link_debug(link
, "Setting addresses");
1229 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1230 Link
*link
= userdata
;
1235 assert(link
->ifname
);
1237 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1240 r
= sd_netlink_message_get_errno(m
);
1241 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1242 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1247 static int link_set_bridge_vlan(Link
*link
) {
1250 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1252 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1257 static int link_set_proxy_arp(Link
*link
) {
1258 const char *p
= NULL
;
1261 if (!link_proxy_arp_enabled(link
))
1264 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1266 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1268 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1273 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1274 Link
*link
= userdata
;
1279 log_link_debug(link
, "Set link");
1281 r
= sd_netlink_message_get_errno(m
);
1282 if (r
< 0 && r
!= -EEXIST
) {
1283 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1284 link_enter_failed(link
);
1290 static int link_configure_after_setting_mtu(Link
*link
);
1292 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1293 Link
*link
= userdata
;
1298 assert(link
->ifname
);
1300 link
->setting_mtu
= false;
1302 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1305 r
= sd_netlink_message_get_errno(m
);
1307 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1311 log_link_debug(link
, "Setting MTU done.");
1313 if (link
->state
== LINK_STATE_PENDING
)
1314 (void) link_configure_after_setting_mtu(link
);
1319 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1320 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1324 assert(link
->manager
);
1325 assert(link
->manager
->rtnl
);
1327 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1330 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1332 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1334 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1336 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1337 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1338 (void) link_enable_ipv6(link
);
1340 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1341 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1342 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1344 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1345 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1350 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1352 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1354 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1355 link_netlink_destroy_callback
, link
, 0, __func__
);
1357 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1360 link
->setting_mtu
= true;
1365 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1366 Link
*link
= userdata
;
1371 assert(link
->ifname
);
1373 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1376 r
= sd_netlink_message_get_errno(m
);
1378 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1383 static int link_set_flags(Link
*link
) {
1384 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1385 unsigned ifi_change
= 0;
1386 unsigned ifi_flags
= 0;
1390 assert(link
->manager
);
1391 assert(link
->manager
->rtnl
);
1393 if (link
->flags
& IFF_LOOPBACK
)
1399 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1402 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1404 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1406 if (link
->network
->arp
>= 0) {
1407 ifi_change
|= IFF_NOARP
;
1408 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1411 if (link
->network
->multicast
>= 0) {
1412 ifi_change
|= IFF_MULTICAST
;
1413 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1416 if (link
->network
->allmulticast
>= 0) {
1417 ifi_change
|= IFF_ALLMULTI
;
1418 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1421 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1423 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1425 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1426 link_netlink_destroy_callback
, link
, 0, __func__
);
1428 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1435 static int link_set_bridge(Link
*link
) {
1436 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1440 assert(link
->network
);
1442 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1444 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1446 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1448 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1450 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1452 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1454 if (link
->network
->use_bpdu
>= 0) {
1455 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1457 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1460 if (link
->network
->hairpin
>= 0) {
1461 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1463 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1466 if (link
->network
->fast_leave
>= 0) {
1467 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1469 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1472 if (link
->network
->allow_port_to_be_root
>= 0) {
1473 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1475 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1479 if (link
->network
->unicast_flood
>= 0) {
1480 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1482 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1486 if (link
->network
->cost
!= 0) {
1487 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1489 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1491 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1492 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1494 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1497 r
= sd_netlink_message_close_container(req
);
1499 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1501 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1502 link_netlink_destroy_callback
, link
, 0, __func__
);
1504 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1511 static int link_bond_set(Link
*link
) {
1512 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1516 assert(link
->network
);
1518 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1520 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1522 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1524 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1526 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1528 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1530 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1532 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1534 if (link
->network
->active_slave
) {
1535 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1537 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1540 if (link
->network
->primary_slave
) {
1541 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1543 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1546 r
= sd_netlink_message_close_container(req
);
1548 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1550 r
= sd_netlink_message_close_container(req
);
1552 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1554 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1555 link_netlink_destroy_callback
, link
, 0, __func__
);
1557 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1564 static int link_lldp_save(Link
*link
) {
1565 _cleanup_free_
char *temp_path
= NULL
;
1566 _cleanup_fclose_
FILE *f
= NULL
;
1567 sd_lldp_neighbor
**l
= NULL
;
1571 assert(link
->lldp_file
);
1574 (void) unlink(link
->lldp_file
);
1578 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1582 (void) unlink(link
->lldp_file
);
1588 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1592 fchmod(fileno(f
), 0644);
1594 for (i
= 0; i
< n
; i
++) {
1599 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1604 (void) fwrite(&u
, 1, sizeof(u
), f
);
1605 (void) fwrite(p
, 1, sz
, f
);
1608 r
= fflush_and_check(f
);
1612 if (rename(temp_path
, link
->lldp_file
) < 0) {
1619 (void) unlink(link
->lldp_file
);
1621 (void) unlink(temp_path
);
1623 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1627 for (i
= 0; i
< n
; i
++)
1628 sd_lldp_neighbor_unref(l
[i
]);
1635 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1636 Link
*link
= userdata
;
1641 (void) link_lldp_save(link
);
1643 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1644 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1646 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1648 r
= link_lldp_emit_start(link
);
1650 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1654 static int link_acquire_ipv6_conf(Link
*link
) {
1659 if (link_ipv6_accept_ra_enabled(link
)) {
1660 assert(link
->ndisc
);
1662 log_link_debug(link
, "Discovering IPv6 routers");
1664 r
= sd_ndisc_start(link
->ndisc
);
1665 if (r
< 0 && r
!= -EBUSY
)
1666 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1669 if (link_radv_enabled(link
)) {
1671 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1673 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1675 r
= sd_radv_start(link
->radv
);
1676 if (r
< 0 && r
!= -EBUSY
)
1677 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1680 (void) dhcp6_request_prefix_delegation(link
);
1685 static int link_acquire_ipv4_conf(Link
*link
) {
1689 assert(link
->network
);
1690 assert(link
->manager
);
1691 assert(link
->manager
->event
);
1693 if (link_ipv4ll_enabled(link
)) {
1694 assert(link
->ipv4ll
);
1696 log_link_debug(link
, "Acquiring IPv4 link-local address");
1698 r
= sd_ipv4ll_start(link
->ipv4ll
);
1700 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1703 if (link_dhcp4_enabled(link
)) {
1704 assert(link
->dhcp_client
);
1706 log_link_debug(link
, "Acquiring DHCPv4 lease");
1708 r
= sd_dhcp_client_start(link
->dhcp_client
);
1710 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1716 static int link_acquire_conf(Link
*link
) {
1721 r
= link_acquire_ipv4_conf(link
);
1725 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1726 r
= link_acquire_ipv6_conf(link
);
1731 if (link_lldp_emit_enabled(link
)) {
1732 r
= link_lldp_emit_start(link
);
1734 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1740 bool link_has_carrier(Link
*link
) {
1741 /* see Documentation/networking/operstates.txt in the kernel sources */
1743 if (link
->kernel_operstate
== IF_OPER_UP
)
1746 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1747 /* operstate may not be implemented, so fall back to flags */
1748 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1754 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1755 Link
*link
= userdata
;
1760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1763 r
= sd_netlink_message_get_errno(m
);
1765 /* we warn but don't fail the link, as it may be brought up later */
1766 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1771 int link_up(Link
*link
) {
1772 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1773 uint8_t ipv6ll_mode
;
1777 assert(link
->network
);
1778 assert(link
->manager
);
1779 assert(link
->manager
->rtnl
);
1781 log_link_debug(link
, "Bringing link up");
1783 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1785 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1787 /* set it free if not enslaved with networkd */
1788 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1789 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1791 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1794 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1796 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1798 if (link
->network
->mac
) {
1799 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1801 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1804 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1806 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1808 if (link_ipv6_enabled(link
)) {
1809 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1810 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1812 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1814 if (!link_ipv6ll_enabled(link
))
1815 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1817 const char *p
= NULL
;
1818 _cleanup_free_
char *stable_secret
= NULL
;
1820 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1821 r
= read_one_line_file(p
, &stable_secret
);
1824 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1826 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1828 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1830 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1832 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1833 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1835 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1838 r
= sd_netlink_message_close_container(req
);
1840 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1843 r
= sd_netlink_message_close_container(req
);
1845 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1847 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1848 link_netlink_destroy_callback
, link
, 0, __func__
);
1850 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1857 static int link_up_can(Link
*link
) {
1858 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1863 log_link_debug(link
, "Bringing CAN link up");
1865 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1867 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1869 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1871 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1873 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1874 link_netlink_destroy_callback
, link
, 0, __func__
);
1876 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1883 static int link_set_can(Link
*link
) {
1884 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1888 assert(link
->network
);
1889 assert(link
->manager
);
1890 assert(link
->manager
->rtnl
);
1892 log_link_debug(link
, "link_set_can");
1894 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1896 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1898 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1900 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1902 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1904 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1906 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1908 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1910 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1911 struct can_bittiming bt
= {
1912 .bitrate
= link
->network
->can_bitrate
,
1913 .sample_point
= link
->network
->can_sample_point
,
1916 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1917 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1921 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1922 if (link
->network
->can_sample_point
> 0)
1923 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1925 log_link_debug(link
, "Using default sample point");
1927 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1929 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1932 if (link
->network
->can_restart_us
> 0) {
1933 char time_string
[FORMAT_TIMESPAN_MAX
];
1934 uint64_t restart_ms
;
1936 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1939 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1941 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1943 if (restart_ms
> UINT32_MAX
) {
1944 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1948 log_link_debug(link
, "Setting restart = %s", time_string
);
1950 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1952 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1955 r
= sd_netlink_message_close_container(m
);
1957 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1959 r
= sd_netlink_message_close_container(m
);
1961 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1963 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1964 link_netlink_destroy_callback
, link
, 0, __func__
);
1966 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1970 if (!(link
->flags
& IFF_UP
)) {
1971 r
= link_up_can(link
);
1973 link_enter_failed(link
);
1978 log_link_debug(link
, "link_set_can done");
1983 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1984 Link
*link
= userdata
;
1989 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1992 r
= sd_netlink_message_get_errno(m
);
1994 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1996 if (streq_ptr(link
->kind
, "can"))
2002 int link_down(Link
*link
) {
2003 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2007 assert(link
->manager
);
2008 assert(link
->manager
->rtnl
);
2010 log_link_debug(link
, "Bringing link down");
2012 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2013 RTM_SETLINK
, link
->ifindex
);
2015 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2017 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2019 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2021 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2022 link_netlink_destroy_callback
, link
, 0, __func__
);
2024 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2031 static int link_handle_bound_to_list(Link
*link
) {
2035 bool required_up
= false;
2036 bool link_is_up
= false;
2040 if (hashmap_isempty(link
->bound_to_links
))
2043 if (link
->flags
& IFF_UP
)
2046 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2047 if (link_has_carrier(l
)) {
2052 if (!required_up
&& link_is_up
) {
2053 r
= link_down(link
);
2056 } else if (required_up
&& !link_is_up
) {
2065 static int link_handle_bound_by_list(Link
*link
) {
2072 if (hashmap_isempty(link
->bound_by_links
))
2075 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2076 r
= link_handle_bound_to_list(l
);
2084 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2090 if (link
== carrier
)
2093 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2096 r
= hashmap_ensure_allocated(h
, NULL
);
2100 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2107 static int link_new_bound_by_list(Link
*link
) {
2112 bool list_updated
= false;
2115 assert(link
->manager
);
2119 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2120 if (!carrier
->network
)
2123 if (strv_isempty(carrier
->network
->bind_carrier
))
2126 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2127 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2131 list_updated
= true;
2138 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2139 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2143 link_dirty(carrier
);
2149 static int link_new_bound_to_list(Link
*link
) {
2154 bool list_updated
= false;
2157 assert(link
->manager
);
2162 if (strv_isempty(link
->network
->bind_carrier
))
2167 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2168 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2169 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2173 list_updated
= true;
2180 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2181 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2185 link_dirty(carrier
);
2191 static int link_new_carrier_maps(Link
*link
) {
2194 r
= link_new_bound_by_list(link
);
2198 r
= link_handle_bound_by_list(link
);
2202 r
= link_new_bound_to_list(link
);
2206 r
= link_handle_bound_to_list(link
);
2213 static void link_free_bound_to_list(Link
*link
) {
2217 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2218 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2220 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2221 link_dirty(bound_to
);
2227 static void link_free_bound_by_list(Link
*link
) {
2231 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2232 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2234 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2235 link_dirty(bound_by
);
2236 link_handle_bound_to_list(bound_by
);
2243 static void link_free_carrier_maps(Link
*link
) {
2244 bool list_updated
= false;
2248 if (!hashmap_isempty(link
->bound_to_links
)) {
2249 link_free_bound_to_list(link
);
2250 list_updated
= true;
2253 if (!hashmap_isempty(link
->bound_by_links
)) {
2254 link_free_bound_by_list(link
);
2255 list_updated
= true;
2264 void link_drop(Link
*link
) {
2265 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2268 link_set_state(link
, LINK_STATE_LINGER
);
2270 link_free_carrier_maps(link
);
2272 log_link_debug(link
, "Link removed");
2274 (void) unlink(link
->state_file
);
2276 link_detach_from_manager(link
);
2283 static int link_joined(Link
*link
) {
2287 assert(link
->network
);
2289 if (!hashmap_isempty(link
->bound_to_links
)) {
2290 r
= link_handle_bound_to_list(link
);
2293 } else if (!(link
->flags
& IFF_UP
)) {
2296 link_enter_failed(link
);
2301 if (link
->network
->bridge
) {
2302 r
= link_set_bridge(link
);
2304 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2307 if (link
->network
->bond
) {
2308 r
= link_bond_set(link
);
2310 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2313 if (link
->network
->use_br_vlan
&&
2314 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2315 r
= link_set_bridge_vlan(link
);
2317 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2320 /* Skip setting up addresses until it gets carrier,
2321 or it would try to set addresses twice,
2322 which is bad for non-idempotent steps. */
2323 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2326 return link_enter_set_addresses(link
);
2329 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2330 Link
*link
= userdata
;
2334 assert(link
->network
);
2338 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2341 r
= sd_netlink_message_get_errno(m
);
2342 if (r
< 0 && r
!= -EEXIST
) {
2343 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2344 link_enter_failed(link
);
2347 log_link_debug(link
, "Joined netdev");
2349 if (link
->enslaving
<= 0)
2355 static int link_enter_join_netdev(Link
*link
) {
2361 assert(link
->network
);
2362 assert(link
->state
== LINK_STATE_PENDING
);
2364 link_set_state(link
, LINK_STATE_ENSLAVING
);
2368 if (!link
->network
->bridge
&&
2369 !link
->network
->bond
&&
2370 !link
->network
->vrf
&&
2371 hashmap_isempty(link
->network
->stacked_netdevs
))
2372 return link_joined(link
);
2374 if (link
->network
->bond
) {
2375 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2376 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2377 return link_joined(link
);
2379 log_struct(LOG_DEBUG
,
2380 LOG_LINK_INTERFACE(link
),
2381 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2382 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2384 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2386 log_struct_errno(LOG_WARNING
, r
,
2387 LOG_LINK_INTERFACE(link
),
2388 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2389 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2390 link_enter_failed(link
);
2397 if (link
->network
->bridge
) {
2398 log_struct(LOG_DEBUG
,
2399 LOG_LINK_INTERFACE(link
),
2400 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2401 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2403 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2405 log_struct_errno(LOG_WARNING
, r
,
2406 LOG_LINK_INTERFACE(link
),
2407 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2408 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2409 link_enter_failed(link
);
2416 if (link
->network
->vrf
) {
2417 log_struct(LOG_DEBUG
,
2418 LOG_LINK_INTERFACE(link
),
2419 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2420 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2422 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2424 log_struct_errno(LOG_WARNING
, r
,
2425 LOG_LINK_INTERFACE(link
),
2426 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2427 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2428 link_enter_failed(link
);
2435 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2437 if (netdev
->ifindex
> 0) {
2442 log_struct(LOG_DEBUG
,
2443 LOG_LINK_INTERFACE(link
),
2444 LOG_NETDEV_INTERFACE(netdev
),
2445 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2447 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2449 log_struct_errno(LOG_WARNING
, r
,
2450 LOG_LINK_INTERFACE(link
),
2451 LOG_NETDEV_INTERFACE(netdev
),
2452 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2453 link_enter_failed(link
);
2463 static int link_set_ipv4_forward(Link
*link
) {
2466 if (!link_ipv4_forward_enabled(link
))
2469 /* We propagate the forwarding flag from one interface to the
2470 * global setting one way. This means: as long as at least one
2471 * interface was configured at any time that had IP forwarding
2472 * enabled the setting will stay on for good. We do this
2473 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2474 * somewhat in sync (see below). */
2476 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2478 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2483 static int link_set_ipv6_forward(Link
*link
) {
2486 if (!link_ipv6_forward_enabled(link
))
2489 /* On Linux, the IPv6 stack does not know a per-interface
2490 * packet forwarding setting: either packet forwarding is on
2491 * for all, or off for all. We hence don't bother with a
2492 * per-interface setting, but simply propagate the interface
2493 * flag, if it is set, to the global flag, one-way. Note that
2494 * while IPv4 would allow a per-interface flag, we expose the
2495 * same behaviour there and also propagate the setting from
2496 * one to all, to keep things simple (see above). */
2498 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2500 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2505 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2506 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2507 IPv6PrivacyExtensions s
;
2508 const char *p
= NULL
;
2511 s
= link_ipv6_privacy_extensions(link
);
2515 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2516 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2518 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2520 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2525 static int link_set_ipv6_accept_ra(Link
*link
) {
2526 const char *p
= NULL
;
2529 /* Make this a NOP if IPv6 is not available */
2530 if (!socket_ipv6_is_supported())
2533 if (link
->flags
& IFF_LOOPBACK
)
2539 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2541 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2542 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2544 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2549 static int link_set_ipv6_dad_transmits(Link
*link
) {
2550 char buf
[DECIMAL_STR_MAX(int) + 1];
2551 const char *p
= NULL
;
2554 /* Make this a NOP if IPv6 is not available */
2555 if (!socket_ipv6_is_supported())
2558 if (link
->flags
& IFF_LOOPBACK
)
2564 if (link
->network
->ipv6_dad_transmits
< 0)
2567 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2568 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2570 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2572 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2577 static int link_set_ipv6_hop_limit(Link
*link
) {
2578 char buf
[DECIMAL_STR_MAX(int) + 1];
2579 const char *p
= NULL
;
2582 /* Make this a NOP if IPv6 is not available */
2583 if (!socket_ipv6_is_supported())
2586 if (link
->flags
& IFF_LOOPBACK
)
2592 if (link
->network
->ipv6_hop_limit
< 0)
2595 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2596 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2598 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2600 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2605 static int link_set_ipv6_mtu(Link
*link
) {
2606 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2607 const char *p
= NULL
;
2610 /* Make this a NOP if IPv6 is not available */
2611 if (!socket_ipv6_is_supported())
2614 if (link
->flags
& IFF_LOOPBACK
)
2617 if (link
->network
->ipv6_mtu
== 0)
2620 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2622 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2624 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2626 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2631 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2632 Address
*net_address
;
2640 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2641 if (address_equal(net_address
, address
))
2647 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2656 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2657 if (route_equal(net_route
, route
))
2663 static int link_drop_foreign_config(Link
*link
) {
2669 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2670 /* we consider IPv6LL addresses to be managed by the kernel */
2671 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2674 if (link_is_static_address_configured(link
, address
)) {
2675 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2677 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2679 r
= address_remove(address
, link
, link_address_remove_handler
);
2685 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2686 /* do not touch routes managed by the kernel */
2687 if (route
->protocol
== RTPROT_KERNEL
)
2690 if (link_is_static_route_configured(link
, route
)) {
2691 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2695 r
= route_remove(route
, link
, link_route_remove_handler
);
2704 static int link_drop_config(Link
*link
) {
2705 Address
*address
, *pool_address
;
2710 SET_FOREACH(address
, link
->addresses
, i
) {
2711 /* we consider IPv6LL addresses to be managed by the kernel */
2712 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2715 r
= address_remove(address
, link
, link_address_remove_handler
);
2719 /* If this address came from an address pool, clean up the pool */
2720 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2721 if (address_equal(address
, pool_address
)) {
2722 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2723 address_free(pool_address
);
2729 SET_FOREACH(route
, link
->routes
, i
) {
2730 /* do not touch routes managed by the kernel */
2731 if (route
->protocol
== RTPROT_KERNEL
)
2734 r
= route_remove(route
, link
, link_route_remove_handler
);
2744 static int link_update_lldp(Link
*link
) {
2752 if (link
->flags
& IFF_UP
) {
2753 r
= sd_lldp_start(link
->lldp
);
2755 log_link_debug(link
, "Started LLDP.");
2757 r
= sd_lldp_stop(link
->lldp
);
2759 log_link_debug(link
, "Stopped LLDP.");
2765 static int link_configure_can(Link
*link
) {
2768 if (streq_ptr(link
->kind
, "can")) {
2769 /* The CAN interface must be down to configure bitrate, etc... */
2770 if ((link
->flags
& IFF_UP
)) {
2771 r
= link_down(link
);
2773 link_enter_failed(link
);
2780 return link_set_can(link
);
2783 if (!(link
->flags
& IFF_UP
)) {
2784 r
= link_up_can(link
);
2786 link_enter_failed(link
);
2794 static int link_configure(Link
*link
) {
2798 assert(link
->network
);
2799 assert(link
->state
== LINK_STATE_PENDING
);
2801 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2802 return link_configure_can(link
);
2804 /* Drop foreign config, but ignore loopback or critical devices.
2805 * We do not want to remove loopback address or addresses used for root NFS. */
2806 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2807 r
= link_drop_foreign_config(link
);
2812 r
= link_set_proxy_arp(link
);
2816 r
= ipv6_proxy_ndp_addresses_configure(link
);
2820 r
= link_set_ipv4_forward(link
);
2824 r
= link_set_ipv6_forward(link
);
2828 r
= link_set_ipv6_privacy_extensions(link
);
2832 r
= link_set_ipv6_accept_ra(link
);
2836 r
= link_set_ipv6_dad_transmits(link
);
2840 r
= link_set_ipv6_hop_limit(link
);
2844 r
= link_set_flags(link
);
2848 r
= link_set_ipv6_mtu(link
);
2852 if (link_ipv4ll_enabled(link
)) {
2853 r
= ipv4ll_configure(link
);
2858 if (link_dhcp4_enabled(link
)) {
2859 r
= dhcp4_set_promote_secondaries(link
);
2863 r
= dhcp4_configure(link
);
2868 if (link_dhcp4_server_enabled(link
)) {
2869 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2873 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2878 if (link_dhcp6_enabled(link
) ||
2879 link_ipv6_accept_ra_enabled(link
)) {
2880 r
= dhcp6_configure(link
);
2885 if (link_ipv6_accept_ra_enabled(link
)) {
2886 r
= ndisc_configure(link
);
2891 if (link_radv_enabled(link
)) {
2892 r
= radv_configure(link
);
2897 if (link_lldp_rx_enabled(link
)) {
2898 r
= sd_lldp_new(&link
->lldp
);
2902 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2906 r
= sd_lldp_match_capabilities(link
->lldp
,
2907 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2908 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2909 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2913 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2917 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2921 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2925 r
= link_update_lldp(link
);
2930 if (link
->network
->mtu
> 0) {
2931 r
= link_set_mtu(link
, link
->network
->mtu
);
2936 return link_configure_after_setting_mtu(link
);
2939 static int link_configure_after_setting_mtu(Link
*link
) {
2943 assert(link
->network
);
2944 assert(link
->state
== LINK_STATE_PENDING
);
2946 if (link
->setting_mtu
)
2949 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2950 r
= link_acquire_conf(link
);
2955 return link_enter_join_netdev(link
);
2958 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2961 if (duid
->raw_data_len
> 0)
2964 if (duid
->type
!= DUID_TYPE_UUID
)
2967 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2968 duid
->raw_data_len
= sizeof(sd_id128_t
);
2973 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2974 Manager
*manager
= userdata
;
2975 const sd_bus_error
*e
;
2985 e
= sd_bus_message_get_error(m
);
2987 log_error_errno(sd_bus_error_get_errno(e
),
2988 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2993 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2997 if (sz
!= sizeof(sd_id128_t
)) {
2998 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3002 memcpy(&manager
->product_uuid
, a
, sz
);
3003 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3004 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3006 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3009 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3010 r
= link_configure(link
);
3012 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3015 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3017 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3018 * even if the method fails. */
3019 manager
->has_product_uuid
= true;
3024 static bool link_requires_uuid(Link
*link
) {
3028 assert(link
->manager
);
3029 assert(link
->network
);
3031 duid
= link_get_duid(link
);
3032 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3035 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3038 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3044 static int link_configure_duid(Link
*link
) {
3050 assert(link
->manager
);
3051 assert(link
->network
);
3054 duid
= link_get_duid(link
);
3056 if (!link_requires_uuid(link
))
3059 if (m
->has_product_uuid
) {
3060 (void) duid_set_uuid(duid
, m
->product_uuid
);
3064 if (!m
->links_requesting_uuid
) {
3065 r
= manager_request_product_uuid(m
, link
);
3070 log_link_warning_errno(link
, r
,
3071 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3075 r
= set_put(m
->links_requesting_uuid
, link
);
3079 r
= set_put(m
->duids_requesting_uuid
, duid
);
3087 static int link_initialized_and_synced(Link
*link
) {
3092 assert(link
->ifname
);
3093 assert(link
->manager
);
3095 if (link
->state
!= LINK_STATE_PENDING
)
3098 log_link_debug(link
, "Link state is up-to-date");
3100 r
= link_new_bound_by_list(link
);
3104 r
= link_handle_bound_by_list(link
);
3108 if (!link
->network
) {
3109 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3110 &link
->mac
, &network
);
3112 link_enter_unmanaged(link
);
3114 } else if (r
== 0 && network
->unmanaged
) {
3115 link_enter_unmanaged(link
);
3120 if (link
->flags
& IFF_LOOPBACK
) {
3121 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3122 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3124 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3125 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3127 if (network
->dhcp_server
)
3128 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3131 r
= network_apply(network
, link
);
3136 r
= link_new_bound_to_list(link
);
3140 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3141 * link_configure() is called later asynchronously. */
3142 r
= link_configure_duid(link
);
3146 r
= link_configure(link
);
3153 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
3154 (void) link_initialized_and_synced(userdata
);
3158 int link_initialized(Link
*link
, sd_device
*device
) {
3159 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3163 assert(link
->manager
);
3164 assert(link
->manager
->rtnl
);
3167 if (link
->state
!= LINK_STATE_PENDING
)
3170 if (link
->sd_device
)
3173 log_link_debug(link
, "udev initialized link");
3175 link
->sd_device
= sd_device_ref(device
);
3177 /* udev has initialized the link, but we don't know if we have yet
3178 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3179 * when it returns we know that the pending NEWLINKs have already been
3180 * processed and that we are up-to-date */
3182 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3187 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3188 link_netlink_destroy_callback
, link
, 0, __func__
);
3197 static int link_load(Link
*link
) {
3198 _cleanup_free_
char *network_file
= NULL
,
3201 *dhcp4_address
= NULL
,
3202 *ipv4ll_address
= NULL
;
3203 union in_addr_union address
;
3204 union in_addr_union route_dst
;
3210 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
3211 "NETWORK_FILE", &network_file
,
3212 "ADDRESSES", &addresses
,
3214 "DHCP4_ADDRESS", &dhcp4_address
,
3215 "IPV4LL_ADDRESS", &ipv4ll_address
,
3217 if (r
< 0 && r
!= -ENOENT
)
3218 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3225 suffix
= strrchr(network_file
, '.');
3227 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3228 goto network_file_fail
;
3232 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3234 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3235 goto network_file_fail
;
3238 r
= network_apply(network
, link
);
3240 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3249 _cleanup_free_
char *address_str
= NULL
;
3250 char *prefixlen_str
;
3252 unsigned char prefixlen
;
3254 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3256 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3262 prefixlen_str
= strchr(address_str
, '/');
3263 if (!prefixlen_str
) {
3264 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3268 *prefixlen_str
++ = '\0';
3270 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3272 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3276 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3278 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3282 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3284 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3293 _cleanup_free_
char *route_str
= NULL
;
3294 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3296 char *prefixlen_str
;
3298 unsigned char prefixlen
, tos
, table
;
3301 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3303 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3309 prefixlen_str
= strchr(route_str
, '/');
3310 if (!prefixlen_str
) {
3311 log_link_debug(link
, "Failed to parse route %s", route_str
);
3315 *prefixlen_str
++ = '\0';
3317 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3319 log_link_debug(link
,
3320 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3325 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3327 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3331 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3333 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3335 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3336 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3337 0, route_expire_handler
, route
);
3339 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3342 route
->lifetime
= lifetime
;
3343 sd_event_source_unref(route
->expire
);
3344 route
->expire
= TAKE_PTR(expire
);
3348 if (dhcp4_address
) {
3349 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3351 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3352 goto dhcp4_address_fail
;
3355 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3357 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3359 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3361 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3366 if (ipv4ll_address
) {
3367 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3369 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3370 goto ipv4ll_address_fail
;
3373 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3375 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3377 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3379 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3382 ipv4ll_address_fail
:
3387 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3388 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3389 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3398 r
= link_new(m
, message
, ret
);
3404 log_link_debug(link
, "Link %d added", link
->ifindex
);
3406 r
= link_load(link
);
3410 if (detect_container() <= 0) {
3411 /* not in a container, udev will be around */
3412 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3413 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3415 log_link_warning_errno(link
, r
, "Could not find device: %m");
3419 r
= sd_device_get_is_initialized(device
);
3421 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3426 log_link_debug(link
, "link pending udev initialization...");
3430 r
= link_initialized(link
, device
);
3434 r
= link_initialized_and_synced(link
);
3441 link_enter_failed(link
);
3445 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3450 log_link_info(link
, "Gained IPv6LL");
3452 link
->ipv6ll_address
= *address
;
3453 link_check_ready(link
);
3455 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3456 r
= link_acquire_ipv6_conf(link
);
3458 link_enter_failed(link
);
3466 static int link_carrier_gained(Link
*link
) {
3471 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3472 r
= link_acquire_conf(link
);
3474 link_enter_failed(link
);
3478 r
= link_enter_set_addresses(link
);
3483 r
= link_handle_bound_by_list(link
);
3490 static int link_carrier_lost(Link
*link
) {
3495 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3496 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3497 * configuration and stop the clients as well. */
3498 if (link
->setting_mtu
)
3501 r
= link_stop_clients(link
);
3503 link_enter_failed(link
);
3507 if (link_dhcp4_server_enabled(link
))
3508 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3510 r
= link_drop_config(link
);
3514 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3515 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3516 r
= link_drop_foreign_config(link
);
3521 r
= link_handle_bound_by_list(link
);
3528 int link_carrier_reset(Link
*link
) {
3533 if (link_has_carrier(link
)) {
3534 r
= link_carrier_lost(link
);
3538 r
= link_carrier_gained(link
);
3542 log_link_info(link
, "Reset carrier");
3548 int link_update(Link
*link
, sd_netlink_message
*m
) {
3549 struct ether_addr mac
;
3552 bool had_carrier
, carrier_gained
, carrier_lost
;
3556 assert(link
->ifname
);
3559 if (link
->state
== LINK_STATE_LINGER
) {
3560 log_link_info(link
, "Link readded");
3561 link_set_state(link
, LINK_STATE_ENSLAVING
);
3563 r
= link_new_carrier_maps(link
);
3568 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3569 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3570 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3572 if (link
->state
== LINK_STATE_PENDING
) {
3573 r
= free_and_strdup(&link
->ifname
, ifname
);
3577 Manager
*manager
= link
->manager
;
3580 r
= link_add(manager
, m
, &link
);
3586 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3587 if (r
>= 0 && mtu
> 0) {
3589 if (link
->original_mtu
== 0) {
3590 link
->original_mtu
= mtu
;
3591 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3594 if (link
->dhcp_client
) {
3595 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3598 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3602 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3604 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3608 /* The kernel may broadcast NEWLINK messages without the MAC address
3609 set, simply ignore them. */
3610 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3612 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3615 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3618 log_link_debug(link
, "MAC address: "
3619 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3620 mac
.ether_addr_octet
[0],
3621 mac
.ether_addr_octet
[1],
3622 mac
.ether_addr_octet
[2],
3623 mac
.ether_addr_octet
[3],
3624 mac
.ether_addr_octet
[4],
3625 mac
.ether_addr_octet
[5]);
3628 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3630 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3633 if (link
->dhcp_client
) {
3634 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3635 (const uint8_t *) &link
->mac
,
3639 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3641 r
= dhcp4_set_client_identifier(link
);
3646 if (link
->dhcp6_client
) {
3647 const DUID
* duid
= link_get_duid(link
);
3649 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3650 (const uint8_t *) &link
->mac
,
3654 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3656 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3657 link
->network
->iaid
);
3659 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3661 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3663 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3664 duid
->raw_data_len
);
3666 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3670 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3672 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3676 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3678 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3683 had_carrier
= link_has_carrier(link
);
3685 r
= link_update_flags(link
, m
);
3689 r
= link_update_lldp(link
);
3693 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3694 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3696 if (carrier_gained
) {
3697 log_link_info(link
, "Gained carrier");
3699 r
= link_carrier_gained(link
);
3702 } else if (carrier_lost
) {
3703 log_link_info(link
, "Lost carrier");
3705 r
= link_carrier_lost(link
);
3713 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3721 if (hashmap_isempty(h
))
3725 HASHMAP_FOREACH(link
, h
, i
) {
3729 fprintf(f
, "%i", link
->ifindex
);
3736 int link_save(Link
*link
) {
3737 _cleanup_free_
char *temp_path
= NULL
;
3738 _cleanup_fclose_
FILE *f
= NULL
;
3739 const char *admin_state
, *oper_state
;
3746 assert(link
->state_file
);
3747 assert(link
->lease_file
);
3748 assert(link
->manager
);
3750 if (link
->state
== LINK_STATE_LINGER
) {
3751 unlink(link
->state_file
);
3755 link_lldp_save(link
);
3757 admin_state
= link_state_to_string(link
->state
);
3758 assert(admin_state
);
3760 oper_state
= link_operstate_to_string(link
->operstate
);
3763 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3767 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3768 (void) fchmod(fileno(f
), 0644);
3771 "# This is private data. Do not parse.\n"
3774 admin_state
, oper_state
);
3776 if (link
->network
) {
3778 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3779 const char *dhcp_domainname
= NULL
;
3780 char **dhcp6_domains
= NULL
;
3781 char **dhcp_domains
= NULL
;
3784 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3785 yes_no(link
->network
->required_for_online
));
3787 if (link
->dhcp6_client
) {
3788 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3789 if (r
< 0 && r
!= -ENOMSG
)
3790 log_link_debug(link
, "No DHCPv6 lease");
3793 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3798 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3799 _cleanup_free_
char *b
= NULL
;
3801 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3802 &link
->network
->dns
[j
].address
, &b
);
3804 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3814 if (link
->network
->dhcp_use_dns
&&
3816 const struct in_addr
*addresses
;
3818 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3822 serialize_in_addrs(f
, addresses
, r
);
3827 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3828 struct in6_addr
*in6_addrs
;
3830 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3834 serialize_in6_addrs(f
, in6_addrs
, r
);
3839 /* Make sure to flush out old entries before we use the NDISC data */
3842 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3845 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3849 serialize_in6_addrs(f
, &dd
->address
, 1);
3858 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3860 if (link
->network
->dhcp_use_ntp
&&
3862 const struct in_addr
*addresses
;
3864 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3868 serialize_in_addrs(f
, addresses
, r
);
3873 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3874 struct in6_addr
*in6_addrs
;
3877 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3882 serialize_in6_addrs(f
, in6_addrs
, r
);
3886 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3888 fputstrv(f
, hosts
, NULL
, &space
);
3893 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3894 if (link
->dhcp_lease
) {
3895 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3896 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3899 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3902 fputs("DOMAINS=", f
);
3904 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3906 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3909 if (dhcp_domainname
)
3910 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3912 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3914 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3916 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3917 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3922 fputs("ROUTE_DOMAINS=", f
);
3924 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3926 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3929 if (dhcp_domainname
)
3930 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3932 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3934 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3936 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3937 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3942 fprintf(f
, "LLMNR=%s\n",
3943 resolve_support_to_string(link
->network
->llmnr
));
3944 fprintf(f
, "MDNS=%s\n",
3945 resolve_support_to_string(link
->network
->mdns
));
3947 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3948 fprintf(f
, "DNS_OVER_TLS=%s\n",
3949 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3951 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3952 fprintf(f
, "DNSSEC=%s\n",
3953 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3955 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3958 fputs("DNSSEC_NTA=", f
);
3960 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3961 fputs_with_space(f
, n
, NULL
, &space
);
3965 fputs("ADDRESSES=", f
);
3967 SET_FOREACH(a
, link
->addresses
, i
) {
3968 _cleanup_free_
char *address_str
= NULL
;
3970 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3974 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3979 fputs("ROUTES=", f
);
3981 SET_FOREACH(route
, link
->routes
, i
) {
3982 _cleanup_free_
char *route_str
= NULL
;
3984 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3988 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3989 space
? " " : "", route_str
,
3990 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3997 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3998 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4000 if (link
->dhcp_lease
) {
4001 struct in_addr address
;
4002 const char *tz
= NULL
;
4004 assert(link
->network
);
4006 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4008 fprintf(f
, "TIMEZONE=%s\n", tz
);
4010 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4012 fputs("DHCP4_ADDRESS=", f
);
4013 serialize_in_addrs(f
, &address
, 1);
4017 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4025 unlink(link
->lease_file
);
4028 struct in_addr address
;
4030 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4032 fputs("IPV4LL_ADDRESS=", f
);
4033 serialize_in_addrs(f
, &address
, 1);
4038 r
= fflush_and_check(f
);
4042 if (rename(temp_path
, link
->state_file
) < 0) {
4050 (void) unlink(link
->state_file
);
4052 (void) unlink(temp_path
);
4054 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4057 /* The serialized state in /run is no longer up-to-date. */
4058 void link_dirty(Link
*link
) {
4063 /* mark manager dirty as link is dirty */
4064 manager_dirty(link
->manager
);
4066 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4068 /* allocation errors are ignored */
4071 r
= set_put(link
->manager
->dirty_links
, link
);
4073 /* don't take another ref if the link was already dirty */
4079 /* The serialized state in /run is up-to-date */
4080 void link_clean(Link
*link
) {
4082 assert(link
->manager
);
4084 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4087 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4088 [LINK_STATE_PENDING
] = "pending",
4089 [LINK_STATE_ENSLAVING
] = "configuring",
4090 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4091 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4092 [LINK_STATE_CONFIGURED
] = "configured",
4093 [LINK_STATE_UNMANAGED
] = "unmanaged",
4094 [LINK_STATE_FAILED
] = "failed",
4095 [LINK_STATE_LINGER
] = "linger",
4098 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4100 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4101 [LINK_OPERSTATE_OFF
] = "off",
4102 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4103 [LINK_OPERSTATE_DORMANT
] = "dormant",
4104 [LINK_OPERSTATE_CARRIER
] = "carrier",
4105 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4106 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4109 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);