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
);
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 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
411 _cleanup_(link_unrefp
) Link
*link
= NULL
;
413 const char *ifname
, *kind
= NULL
;
415 unsigned short iftype
;
421 /* check for link kind */
422 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
424 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
425 r
= sd_netlink_message_exit_container(message
);
430 r
= sd_netlink_message_get_type(message
, &type
);
433 else if (type
!= RTM_NEWLINK
)
436 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
439 else if (ifindex
<= 0)
442 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
446 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
450 link
= new0(Link
, 1);
455 link
->manager
= manager
;
456 link
->state
= LINK_STATE_PENDING
;
457 link
->rtnl_extended_attrs
= true;
458 link
->ifindex
= ifindex
;
459 link
->iftype
= iftype
;
460 link
->ifname
= strdup(ifname
);
465 link
->kind
= strdup(kind
);
470 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
472 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
474 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
477 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
480 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
483 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
487 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
491 r
= link_update_flags(link
, message
);
495 *ret
= TAKE_PTR(link
);
500 static Link
*link_free(Link
*link
) {
508 while ((route
= set_first(link
->routes
)))
511 while ((route
= set_first(link
->routes_foreign
)))
514 link
->routes
= set_free(link
->routes
);
515 link
->routes_foreign
= set_free(link
->routes_foreign
);
517 while ((address
= set_first(link
->addresses
)))
518 address_free(address
);
520 while ((address
= set_first(link
->addresses_foreign
)))
521 address_free(address
);
523 link
->addresses
= set_free(link
->addresses
);
524 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
526 while ((address
= link
->pool_addresses
)) {
527 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
528 address_free(address
);
531 sd_dhcp_server_unref(link
->dhcp_server
);
532 sd_dhcp_client_unref(link
->dhcp_client
);
533 sd_dhcp_lease_unref(link
->dhcp_lease
);
535 link_lldp_emit_stop(link
);
537 free(link
->lease_file
);
539 sd_lldp_unref(link
->lldp
);
540 free(link
->lldp_file
);
544 sd_ipv4ll_unref(link
->ipv4ll
);
545 sd_dhcp6_client_unref(link
->dhcp6_client
);
546 sd_ndisc_unref(link
->ndisc
);
547 sd_radv_unref(link
->radv
);
550 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
551 set_remove(link
->manager
->links_requesting_uuid
, link
);
558 (void) unlink(link
->state_file
);
559 free(link
->state_file
);
561 sd_device_unref(link
->sd_device
);
563 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
564 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
565 hashmap_free(link
->bound_to_links
);
567 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
568 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
569 hashmap_free(link
->bound_by_links
);
574 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
576 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
583 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
592 static void link_set_state(Link
*link
, LinkState state
) {
595 if (link
->state
== state
)
600 link_send_changed(link
, "AdministrativeState", NULL
);
603 static void link_enter_unmanaged(Link
*link
) {
606 log_link_debug(link
, "Unmanaged");
608 link_set_state(link
, LINK_STATE_UNMANAGED
);
613 static int link_stop_clients(Link
*link
) {
617 assert(link
->manager
);
618 assert(link
->manager
->event
);
620 if (link
->dhcp_client
) {
621 k
= sd_dhcp_client_stop(link
->dhcp_client
);
623 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
627 k
= sd_ipv4ll_stop(link
->ipv4ll
);
629 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
632 if (link
->dhcp6_client
) {
633 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
635 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
639 k
= sd_ndisc_stop(link
->ndisc
);
641 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
645 k
= sd_radv_stop(link
->radv
);
647 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
650 link_lldp_emit_stop(link
);
654 void link_enter_failed(Link
*link
) {
657 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
660 log_link_warning(link
, "Failed");
662 link_set_state(link
, LINK_STATE_FAILED
);
664 link_stop_clients(link
);
669 static Address
* link_find_dhcp_server_address(Link
*link
) {
673 assert(link
->network
);
675 /* The first statically configured address if there is any */
676 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
678 if (address
->family
!= AF_INET
)
681 if (in_addr_is_null(address
->family
, &address
->in_addr
))
687 /* If that didn't work, find a suitable address we got from the pool */
688 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
689 if (address
->family
!= AF_INET
)
698 static void link_enter_configured(Link
*link
) {
700 assert(link
->network
);
702 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
705 log_link_info(link
, "Configured");
707 link_set_state(link
, LINK_STATE_CONFIGURED
);
712 void link_check_ready(Link
*link
) {
718 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
724 if (!link
->static_routes_configured
)
727 if (!link
->routing_policy_rules_configured
)
730 if (link_ipv4ll_enabled(link
))
731 if (!link
->ipv4ll_address
||
735 if (!link
->network
->bridge
) {
737 if (link_ipv6ll_enabled(link
))
738 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
741 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
742 !link
->dhcp4_configured
) ||
743 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
744 !link
->dhcp6_configured
) ||
745 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
746 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
749 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
753 SET_FOREACH(a
, link
->addresses
, i
)
754 if (!address_is_ready(a
))
757 if (link
->state
!= LINK_STATE_CONFIGURED
)
758 link_enter_configured(link
);
763 static int link_set_routing_policy_rule(Link
*link
) {
764 RoutingPolicyRule
*rule
, *rrule
= NULL
;
768 assert(link
->network
);
770 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
771 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
772 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
774 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
778 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
780 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
781 link_enter_failed(link
);
785 link
->routing_policy_rule_messages
++;
788 routing_policy_rule_purge(link
->manager
, link
);
789 if (link
->routing_policy_rule_messages
== 0) {
790 link
->routing_policy_rules_configured
= true;
791 link_check_ready(link
);
793 log_link_debug(link
, "Setting routing policy rules");
798 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
799 _cleanup_(link_unrefp
) Link
*link
= userdata
;
802 assert(link
->route_messages
> 0);
803 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
804 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
807 link
->route_messages
--;
809 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
812 r
= sd_netlink_message_get_errno(m
);
813 if (r
< 0 && r
!= -EEXIST
)
814 log_link_warning_errno(link
, r
, "Could not set route: %m");
816 if (link
->route_messages
== 0) {
817 log_link_debug(link
, "Routes set");
818 link
->static_routes_configured
= true;
819 link_check_ready(link
);
825 static int link_enter_set_routes(Link
*link
) {
830 assert(link
->network
);
831 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
833 (void) link_set_routing_policy_rule(link
);
835 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
837 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
838 r
= route_configure(rt
, link
, route_handler
);
840 log_link_warning_errno(link
, r
, "Could not set routes: %m");
841 link_enter_failed(link
);
845 link
->route_messages
++;
848 if (link
->route_messages
== 0) {
849 link
->static_routes_configured
= true;
850 link_check_ready(link
);
852 log_link_debug(link
, "Setting routes");
857 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
858 _cleanup_(link_unrefp
) Link
*link
= userdata
;
863 assert(link
->ifname
);
865 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
868 r
= sd_netlink_message_get_errno(m
);
869 if (r
< 0 && r
!= -ESRCH
)
870 log_link_warning_errno(link
, r
, "Could not drop route: %m");
875 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
876 _cleanup_(link_unrefp
) Link
*link
= userdata
;
882 assert(link
->ifname
);
883 assert(link
->address_messages
> 0);
884 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
885 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
887 link
->address_messages
--;
889 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
892 r
= sd_netlink_message_get_errno(m
);
893 if (r
< 0 && r
!= -EEXIST
)
894 log_link_warning_errno(link
, r
, "could not set address: %m");
896 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
898 if (link
->address_messages
== 0) {
899 log_link_debug(link
, "Addresses set");
900 link_enter_set_routes(link
);
906 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
907 _cleanup_(link_unrefp
) Link
*link
= userdata
;
913 assert(link
->ifname
);
914 assert(link
->address_label_messages
> 0);
916 link
->address_label_messages
--;
918 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
921 r
= sd_netlink_message_get_errno(m
);
922 if (r
< 0 && r
!= -EEXIST
)
923 log_link_warning_errno(link
, r
, "could not set address label: %m");
925 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
927 if (link
->address_label_messages
== 0)
928 log_link_debug(link
, "Addresses label set");
933 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
934 _cleanup_free_
struct in_addr
*addresses
= NULL
;
935 size_t n_addresses
= 0, n_allocated
= 0;
938 log_debug("Copying DNS server information from %s", link
->ifname
);
943 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
946 /* Only look for IPv4 addresses */
947 if (link
->network
->dns
[i
].family
!= AF_INET
)
950 ia
= link
->network
->dns
[i
].address
.in
;
952 /* Never propagate obviously borked data */
953 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
956 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
959 addresses
[n_addresses
++] = ia
;
962 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
963 const struct in_addr
*da
= NULL
;
966 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
969 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
972 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
977 if (n_addresses
<= 0)
980 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
983 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
984 _cleanup_free_
struct in_addr
*addresses
= NULL
;
985 size_t n_addresses
= 0, n_allocated
= 0;
991 log_debug("Copying NTP server information from %s", link
->ifname
);
993 STRV_FOREACH(a
, link
->network
->ntp
) {
996 /* Only look for IPv4 addresses */
997 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1000 /* Never propagate obviously borked data */
1001 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1004 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1007 addresses
[n_addresses
++] = ia
;
1010 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1011 const struct in_addr
*da
= NULL
;
1014 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1017 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1020 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1025 if (n_addresses
<= 0)
1028 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1031 static int link_set_bridge_fdb(Link
*link
) {
1032 FdbEntry
*fdb_entry
;
1035 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1036 r
= fdb_entry_configure(link
, fdb_entry
);
1038 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1044 static int link_enter_set_addresses(Link
*link
) {
1045 AddressLabel
*label
;
1050 assert(link
->network
);
1051 assert(link
->state
!= _LINK_STATE_INVALID
);
1053 r
= link_set_bridge_fdb(link
);
1057 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1059 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1060 r
= address_configure(ad
, link
, address_handler
, false);
1062 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1063 link_enter_failed(link
);
1067 link
->address_messages
++;
1070 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1071 r
= address_label_configure(label
, link
, address_label_handler
, false);
1073 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1074 link_enter_failed(link
);
1078 link
->address_label_messages
++;
1081 /* now that we can figure out a default address for the dhcp server,
1083 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1085 Link
*uplink
= NULL
;
1086 bool acquired_uplink
= false;
1088 address
= link_find_dhcp_server_address(link
);
1090 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1091 link_enter_failed(link
);
1095 /* use the server address' subnet as the pool */
1096 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1097 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1102 r = sd_dhcp_server_set_router(link->dhcp_server,
1103 &main_address->in_addr.in);
1108 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1109 r
= sd_dhcp_server_set_max_lease_time(
1111 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1116 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1117 r
= sd_dhcp_server_set_default_lease_time(
1119 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1124 if (link
->network
->dhcp_server_emit_dns
) {
1126 if (link
->network
->n_dhcp_server_dns
> 0)
1127 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1129 uplink
= manager_find_uplink(link
->manager
, link
);
1130 acquired_uplink
= true;
1133 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1136 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1139 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1142 if (link
->network
->dhcp_server_emit_ntp
) {
1144 if (link
->network
->n_dhcp_server_ntp
> 0)
1145 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1147 if (!acquired_uplink
)
1148 uplink
= manager_find_uplink(link
->manager
, link
);
1151 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1154 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1158 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1161 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1163 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1165 if (link
->network
->dhcp_server_emit_timezone
) {
1166 _cleanup_free_
char *buffer
= NULL
;
1167 const char *tz
= NULL
;
1169 if (link
->network
->dhcp_server_timezone
)
1170 tz
= link
->network
->dhcp_server_timezone
;
1172 r
= get_timezone(&buffer
);
1174 log_warning_errno(r
, "Failed to determine timezone: %m");
1180 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1186 r
= sd_dhcp_server_start(link
->dhcp_server
);
1188 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1190 link_enter_failed(link
);
1195 log_link_debug(link
, "Offering DHCPv4 leases");
1198 if (link
->address_messages
== 0)
1199 link_enter_set_routes(link
);
1201 log_link_debug(link
, "Setting addresses");
1206 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1207 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1212 assert(link
->ifname
);
1214 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1217 r
= sd_netlink_message_get_errno(m
);
1218 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1219 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1224 static int link_set_bridge_vlan(Link
*link
) {
1227 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1229 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1234 static int link_set_proxy_arp(Link
*link
) {
1235 const char *p
= NULL
;
1238 if (!link_proxy_arp_enabled(link
))
1241 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1243 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1245 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1250 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1251 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1254 log_link_debug(link
, "Set link");
1256 r
= sd_netlink_message_get_errno(m
);
1257 if (r
< 0 && r
!= -EEXIST
) {
1258 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1259 link_enter_failed(link
);
1266 static int link_configure_after_setting_mtu(Link
*link
);
1268 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1269 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1274 assert(link
->ifname
);
1276 link
->setting_mtu
= false;
1278 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1281 r
= sd_netlink_message_get_errno(m
);
1283 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1287 log_link_debug(link
, "Setting MTU done.");
1289 if (link
->state
== LINK_STATE_PENDING
)
1290 (void) link_configure_after_setting_mtu(link
);
1295 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1296 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1300 assert(link
->manager
);
1301 assert(link
->manager
->rtnl
);
1303 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1306 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1308 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1310 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1312 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1313 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1314 (void) link_enable_ipv6(link
);
1316 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1317 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1318 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1320 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1321 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1326 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1328 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1330 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1332 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1334 link
->setting_mtu
= true;
1341 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1342 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1347 assert(link
->ifname
);
1349 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1352 r
= sd_netlink_message_get_errno(m
);
1354 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1359 static int link_set_flags(Link
*link
) {
1360 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1361 unsigned ifi_change
= 0;
1362 unsigned ifi_flags
= 0;
1366 assert(link
->manager
);
1367 assert(link
->manager
->rtnl
);
1369 if (link
->flags
& IFF_LOOPBACK
)
1375 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1378 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1380 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1382 if (link
->network
->arp
>= 0) {
1383 ifi_change
|= IFF_NOARP
;
1384 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1387 if (link
->network
->multicast
>= 0) {
1388 ifi_change
|= IFF_MULTICAST
;
1389 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1392 if (link
->network
->allmulticast
>= 0) {
1393 ifi_change
|= IFF_ALLMULTI
;
1394 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1397 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1399 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1401 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1403 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1410 static int link_set_bridge(Link
*link
) {
1411 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1415 assert(link
->network
);
1417 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1419 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1421 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1423 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1425 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1427 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1429 if (link
->network
->use_bpdu
>= 0) {
1430 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1432 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1435 if (link
->network
->hairpin
>= 0) {
1436 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1438 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1441 if (link
->network
->fast_leave
>= 0) {
1442 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1444 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1447 if (link
->network
->allow_port_to_be_root
>= 0) {
1448 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1450 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1454 if (link
->network
->unicast_flood
>= 0) {
1455 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1457 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1461 if (link
->network
->cost
!= 0) {
1462 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1464 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1466 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1467 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1469 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1472 r
= sd_netlink_message_close_container(req
);
1474 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1476 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1478 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1485 static int link_bond_set(Link
*link
) {
1486 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1490 assert(link
->network
);
1492 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1494 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1496 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1498 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1500 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1502 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1504 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1506 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1508 if (link
->network
->active_slave
) {
1509 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1511 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1514 if (link
->network
->primary_slave
) {
1515 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1517 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1520 r
= sd_netlink_message_close_container(req
);
1522 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1524 r
= sd_netlink_message_close_container(req
);
1526 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1528 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1530 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1537 static int link_lldp_save(Link
*link
) {
1538 _cleanup_free_
char *temp_path
= NULL
;
1539 _cleanup_fclose_
FILE *f
= NULL
;
1540 sd_lldp_neighbor
**l
= NULL
;
1544 assert(link
->lldp_file
);
1547 (void) unlink(link
->lldp_file
);
1551 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1555 (void) unlink(link
->lldp_file
);
1561 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1565 fchmod(fileno(f
), 0644);
1567 for (i
= 0; i
< n
; i
++) {
1572 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1577 (void) fwrite(&u
, 1, sizeof(u
), f
);
1578 (void) fwrite(p
, 1, sz
, f
);
1581 r
= fflush_and_check(f
);
1585 if (rename(temp_path
, link
->lldp_file
) < 0) {
1592 (void) unlink(link
->lldp_file
);
1594 (void) unlink(temp_path
);
1596 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1600 for (i
= 0; i
< n
; i
++)
1601 sd_lldp_neighbor_unref(l
[i
]);
1608 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1609 Link
*link
= userdata
;
1614 (void) link_lldp_save(link
);
1616 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1617 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1619 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1621 r
= link_lldp_emit_start(link
);
1623 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1627 static int link_acquire_ipv6_conf(Link
*link
) {
1632 if (link_dhcp6_enabled(link
)) {
1633 assert(link
->dhcp6_client
);
1634 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1636 /* start DHCPv6 client in stateless mode */
1637 r
= dhcp6_request_address(link
, true);
1638 if (r
< 0 && r
!= -EBUSY
)
1639 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1641 log_link_debug(link
, "Acquiring DHCPv6 lease");
1644 if (link_ipv6_accept_ra_enabled(link
)) {
1645 assert(link
->ndisc
);
1647 log_link_debug(link
, "Discovering IPv6 routers");
1649 r
= sd_ndisc_start(link
->ndisc
);
1650 if (r
< 0 && r
!= -EBUSY
)
1651 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1654 if (link_radv_enabled(link
)) {
1656 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1658 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1660 r
= sd_radv_start(link
->radv
);
1661 if (r
< 0 && r
!= -EBUSY
)
1662 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1668 static int link_acquire_ipv4_conf(Link
*link
) {
1672 assert(link
->network
);
1673 assert(link
->manager
);
1674 assert(link
->manager
->event
);
1676 if (link_ipv4ll_enabled(link
)) {
1677 assert(link
->ipv4ll
);
1679 log_link_debug(link
, "Acquiring IPv4 link-local address");
1681 r
= sd_ipv4ll_start(link
->ipv4ll
);
1683 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1686 if (link_dhcp4_enabled(link
)) {
1687 assert(link
->dhcp_client
);
1689 log_link_debug(link
, "Acquiring DHCPv4 lease");
1691 r
= sd_dhcp_client_start(link
->dhcp_client
);
1693 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1699 static int link_acquire_conf(Link
*link
) {
1704 r
= link_acquire_ipv4_conf(link
);
1708 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1709 r
= link_acquire_ipv6_conf(link
);
1714 if (link_lldp_emit_enabled(link
)) {
1715 r
= link_lldp_emit_start(link
);
1717 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1723 bool link_has_carrier(Link
*link
) {
1724 /* see Documentation/networking/operstates.txt in the kernel sources */
1726 if (link
->kernel_operstate
== IF_OPER_UP
)
1729 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1730 /* operstate may not be implemented, so fall back to flags */
1731 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1737 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1738 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1743 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1746 r
= sd_netlink_message_get_errno(m
);
1748 /* we warn but don't fail the link, as it may be
1750 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1755 int link_up(Link
*link
) {
1756 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1757 uint8_t ipv6ll_mode
;
1761 assert(link
->network
);
1762 assert(link
->manager
);
1763 assert(link
->manager
->rtnl
);
1765 log_link_debug(link
, "Bringing link up");
1767 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1769 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1771 /* set it free if not enslaved with networkd */
1772 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1773 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1775 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1778 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1780 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1782 if (link
->network
->mac
) {
1783 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1785 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1788 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1790 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1792 if (link_ipv6_enabled(link
)) {
1793 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1794 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1796 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1798 if (!link_ipv6ll_enabled(link
))
1799 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1801 const char *p
= NULL
;
1802 _cleanup_free_
char *stable_secret
= NULL
;
1804 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1805 r
= read_one_line_file(p
, &stable_secret
);
1808 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1810 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1812 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1814 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1816 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1817 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1819 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1822 r
= sd_netlink_message_close_container(req
);
1824 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1827 r
= sd_netlink_message_close_container(req
);
1829 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1831 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1833 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1840 static int link_up_can(Link
*link
) {
1841 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1846 log_link_debug(link
, "Bringing CAN link up");
1848 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1850 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1852 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1854 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1856 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1858 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1865 static int link_set_can(Link
*link
) {
1866 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1870 assert(link
->network
);
1871 assert(link
->manager
);
1872 assert(link
->manager
->rtnl
);
1874 log_link_debug(link
, "link_set_can");
1876 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1878 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1880 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1882 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1884 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1886 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1888 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1890 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1892 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1893 struct can_bittiming bt
= {
1894 .bitrate
= link
->network
->can_bitrate
,
1895 .sample_point
= link
->network
->can_sample_point
,
1898 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1899 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1903 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1904 if (link
->network
->can_sample_point
> 0)
1905 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1907 log_link_debug(link
, "Using default sample point");
1909 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1911 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1914 if (link
->network
->can_restart_us
> 0) {
1915 char time_string
[FORMAT_TIMESPAN_MAX
];
1916 uint64_t restart_ms
;
1918 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1921 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1923 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1925 if (restart_ms
> UINT32_MAX
) {
1926 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1930 log_link_debug(link
, "Setting restart = %s", time_string
);
1932 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1934 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1937 r
= sd_netlink_message_close_container(m
);
1939 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1941 r
= sd_netlink_message_close_container(m
);
1943 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1945 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, link_set_handler
, link
, 0, NULL
);
1947 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1951 if (!(link
->flags
& IFF_UP
)) {
1952 r
= link_up_can(link
);
1954 link_enter_failed(link
);
1959 log_link_debug(link
, "link_set_can done");
1964 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1965 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1970 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1973 r
= sd_netlink_message_get_errno(m
);
1975 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1977 if (streq_ptr(link
->kind
, "can")) {
1985 int link_down(Link
*link
) {
1986 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1990 assert(link
->manager
);
1991 assert(link
->manager
->rtnl
);
1993 log_link_debug(link
, "Bringing link down");
1995 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1996 RTM_SETLINK
, link
->ifindex
);
1998 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2000 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2002 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2004 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
2006 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2013 static int link_handle_bound_to_list(Link
*link
) {
2017 bool required_up
= false;
2018 bool link_is_up
= false;
2022 if (hashmap_isempty(link
->bound_to_links
))
2025 if (link
->flags
& IFF_UP
)
2028 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2029 if (link_has_carrier(l
)) {
2034 if (!required_up
&& link_is_up
) {
2035 r
= link_down(link
);
2038 } else if (required_up
&& !link_is_up
) {
2047 static int link_handle_bound_by_list(Link
*link
) {
2054 if (hashmap_isempty(link
->bound_by_links
))
2057 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2058 r
= link_handle_bound_to_list(l
);
2066 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2072 if (link
== carrier
)
2075 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2078 r
= hashmap_ensure_allocated(h
, NULL
);
2082 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2089 static int link_new_bound_by_list(Link
*link
) {
2094 bool list_updated
= false;
2097 assert(link
->manager
);
2101 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2102 if (!carrier
->network
)
2105 if (strv_isempty(carrier
->network
->bind_carrier
))
2108 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2109 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2113 list_updated
= true;
2120 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2121 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2125 link_dirty(carrier
);
2131 static int link_new_bound_to_list(Link
*link
) {
2136 bool list_updated
= false;
2139 assert(link
->manager
);
2144 if (strv_isempty(link
->network
->bind_carrier
))
2149 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2150 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2151 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2155 list_updated
= true;
2162 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2163 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2167 link_dirty(carrier
);
2173 static int link_new_carrier_maps(Link
*link
) {
2176 r
= link_new_bound_by_list(link
);
2180 r
= link_handle_bound_by_list(link
);
2184 r
= link_new_bound_to_list(link
);
2188 r
= link_handle_bound_to_list(link
);
2195 static void link_free_bound_to_list(Link
*link
) {
2199 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2200 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2202 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2203 link_dirty(bound_to
);
2209 static void link_free_bound_by_list(Link
*link
) {
2213 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2214 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2216 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2217 link_dirty(bound_by
);
2218 link_handle_bound_to_list(bound_by
);
2225 static void link_free_carrier_maps(Link
*link
) {
2226 bool list_updated
= false;
2230 if (!hashmap_isempty(link
->bound_to_links
)) {
2231 link_free_bound_to_list(link
);
2232 list_updated
= true;
2235 if (!hashmap_isempty(link
->bound_by_links
)) {
2236 link_free_bound_by_list(link
);
2237 list_updated
= true;
2246 void link_drop(Link
*link
) {
2247 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2250 link_set_state(link
, LINK_STATE_LINGER
);
2252 link_free_carrier_maps(link
);
2254 log_link_debug(link
, "Link removed");
2256 (void) unlink(link
->state_file
);
2262 static int link_joined(Link
*link
) {
2266 assert(link
->network
);
2268 if (!hashmap_isempty(link
->bound_to_links
)) {
2269 r
= link_handle_bound_to_list(link
);
2272 } else if (!(link
->flags
& IFF_UP
)) {
2275 link_enter_failed(link
);
2280 if (link
->network
->bridge
) {
2281 r
= link_set_bridge(link
);
2283 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2286 if (link
->network
->bond
) {
2287 r
= link_bond_set(link
);
2289 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2292 if (link
->network
->use_br_vlan
&&
2293 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2294 r
= link_set_bridge_vlan(link
);
2296 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2299 /* Skip setting up addresses until it gets carrier,
2300 or it would try to set addresses twice,
2301 which is bad for non-idempotent steps. */
2302 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2305 return link_enter_set_addresses(link
);
2308 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2309 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2313 assert(link
->network
);
2317 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2320 r
= sd_netlink_message_get_errno(m
);
2321 if (r
< 0 && r
!= -EEXIST
) {
2322 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2323 link_enter_failed(link
);
2326 log_link_debug(link
, "Joined netdev");
2328 if (link
->enslaving
<= 0)
2334 static int link_enter_join_netdev(Link
*link
) {
2340 assert(link
->network
);
2341 assert(link
->state
== LINK_STATE_PENDING
);
2343 link_set_state(link
, LINK_STATE_ENSLAVING
);
2347 if (!link
->network
->bridge
&&
2348 !link
->network
->bond
&&
2349 !link
->network
->vrf
&&
2350 hashmap_isempty(link
->network
->stacked_netdevs
))
2351 return link_joined(link
);
2353 if (link
->network
->bond
) {
2354 log_struct(LOG_DEBUG
,
2355 LOG_LINK_INTERFACE(link
),
2356 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2357 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2359 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2361 log_struct_errno(LOG_WARNING
, r
,
2362 LOG_LINK_INTERFACE(link
),
2363 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2364 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2365 link_enter_failed(link
);
2372 if (link
->network
->bridge
) {
2373 log_struct(LOG_DEBUG
,
2374 LOG_LINK_INTERFACE(link
),
2375 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2376 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2378 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2380 log_struct_errno(LOG_WARNING
, r
,
2381 LOG_LINK_INTERFACE(link
),
2382 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2383 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2384 link_enter_failed(link
);
2391 if (link
->network
->vrf
) {
2392 log_struct(LOG_DEBUG
,
2393 LOG_LINK_INTERFACE(link
),
2394 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2395 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2397 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2399 log_struct_errno(LOG_WARNING
, r
,
2400 LOG_LINK_INTERFACE(link
),
2401 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2402 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2403 link_enter_failed(link
);
2410 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2412 if (netdev
->ifindex
> 0) {
2417 log_struct(LOG_DEBUG
,
2418 LOG_LINK_INTERFACE(link
),
2419 LOG_NETDEV_INTERFACE(netdev
),
2420 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2422 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2424 log_struct_errno(LOG_WARNING
, r
,
2425 LOG_LINK_INTERFACE(link
),
2426 LOG_NETDEV_INTERFACE(netdev
),
2427 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2428 link_enter_failed(link
);
2438 static int link_set_ipv4_forward(Link
*link
) {
2441 if (!link_ipv4_forward_enabled(link
))
2444 /* We propagate the forwarding flag from one interface to the
2445 * global setting one way. This means: as long as at least one
2446 * interface was configured at any time that had IP forwarding
2447 * enabled the setting will stay on for good. We do this
2448 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2449 * somewhat in sync (see below). */
2451 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2453 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2458 static int link_set_ipv6_forward(Link
*link
) {
2461 if (!link_ipv6_forward_enabled(link
))
2464 /* On Linux, the IPv6 stack does not know a per-interface
2465 * packet forwarding setting: either packet forwarding is on
2466 * for all, or off for all. We hence don't bother with a
2467 * per-interface setting, but simply propagate the interface
2468 * flag, if it is set, to the global flag, one-way. Note that
2469 * while IPv4 would allow a per-interface flag, we expose the
2470 * same behaviour there and also propagate the setting from
2471 * one to all, to keep things simple (see above). */
2473 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2475 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2480 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2481 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2482 IPv6PrivacyExtensions s
;
2483 const char *p
= NULL
;
2486 s
= link_ipv6_privacy_extensions(link
);
2490 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2491 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2493 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2495 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2500 static int link_set_ipv6_accept_ra(Link
*link
) {
2501 const char *p
= NULL
;
2504 /* Make this a NOP if IPv6 is not available */
2505 if (!socket_ipv6_is_supported())
2508 if (link
->flags
& IFF_LOOPBACK
)
2514 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2516 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2517 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2519 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2524 static int link_set_ipv6_dad_transmits(Link
*link
) {
2525 char buf
[DECIMAL_STR_MAX(int) + 1];
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 if (link
->network
->ipv6_dad_transmits
< 0)
2542 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2543 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2545 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2547 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2552 static int link_set_ipv6_hop_limit(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_hop_limit
< 0)
2570 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2571 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2573 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2575 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2580 static int link_set_ipv6_mtu(Link
*link
) {
2581 char buf
[DECIMAL_STR_MAX(unsigned) + 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
)
2592 if (link
->network
->ipv6_mtu
== 0)
2595 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2597 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2599 r
= write_string_file(p
, buf
, 0);
2601 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2606 static int link_drop_foreign_config(Link
*link
) {
2612 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2613 /* we consider IPv6LL addresses to be managed by the kernel */
2614 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2617 r
= address_remove(address
, link
, link_address_remove_handler
);
2622 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2623 /* do not touch routes managed by the kernel */
2624 if (route
->protocol
== RTPROT_KERNEL
)
2627 r
= route_remove(route
, link
, link_route_remove_handler
);
2635 static int link_drop_config(Link
*link
) {
2636 Address
*address
, *pool_address
;
2641 SET_FOREACH(address
, link
->addresses
, i
) {
2642 /* we consider IPv6LL addresses to be managed by the kernel */
2643 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2646 r
= address_remove(address
, link
, link_address_remove_handler
);
2650 /* If this address came from an address pool, clean up the pool */
2651 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2652 if (address_equal(address
, pool_address
)) {
2653 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2654 address_free(pool_address
);
2660 SET_FOREACH(route
, link
->routes
, i
) {
2661 /* do not touch routes managed by the kernel */
2662 if (route
->protocol
== RTPROT_KERNEL
)
2665 r
= route_remove(route
, link
, link_route_remove_handler
);
2675 static int link_update_lldp(Link
*link
) {
2683 if (link
->flags
& IFF_UP
) {
2684 r
= sd_lldp_start(link
->lldp
);
2686 log_link_debug(link
, "Started LLDP.");
2688 r
= sd_lldp_stop(link
->lldp
);
2690 log_link_debug(link
, "Stopped LLDP.");
2696 static int link_configure_can(Link
*link
) {
2699 if (streq_ptr(link
->kind
, "can")) {
2700 /* The CAN interface must be down to configure bitrate, etc... */
2701 if ((link
->flags
& IFF_UP
)) {
2702 r
= link_down(link
);
2704 link_enter_failed(link
);
2711 return link_set_can(link
);
2714 if (!(link
->flags
& IFF_UP
)) {
2715 r
= link_up_can(link
);
2717 link_enter_failed(link
);
2725 static int link_configure(Link
*link
) {
2729 assert(link
->network
);
2730 assert(link
->state
== LINK_STATE_PENDING
);
2732 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2733 return link_configure_can(link
);
2735 /* Drop foreign config, but ignore loopback or critical devices.
2736 * We do not want to remove loopback address or addresses used for root NFS. */
2737 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2738 r
= link_drop_foreign_config(link
);
2743 r
= link_set_proxy_arp(link
);
2747 r
= ipv6_proxy_ndp_addresses_configure(link
);
2751 r
= link_set_ipv4_forward(link
);
2755 r
= link_set_ipv6_forward(link
);
2759 r
= link_set_ipv6_privacy_extensions(link
);
2763 r
= link_set_ipv6_accept_ra(link
);
2767 r
= link_set_ipv6_dad_transmits(link
);
2771 r
= link_set_ipv6_hop_limit(link
);
2775 r
= link_set_flags(link
);
2779 r
= link_set_ipv6_mtu(link
);
2783 if (link_ipv4ll_enabled(link
)) {
2784 r
= ipv4ll_configure(link
);
2789 if (link_dhcp4_enabled(link
)) {
2790 r
= dhcp4_set_promote_secondaries(link
);
2794 r
= dhcp4_configure(link
);
2799 if (link_dhcp4_server_enabled(link
)) {
2800 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2804 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2809 if (link_dhcp6_enabled(link
) ||
2810 link_ipv6_accept_ra_enabled(link
)) {
2811 r
= dhcp6_configure(link
);
2816 if (link_ipv6_accept_ra_enabled(link
)) {
2817 r
= ndisc_configure(link
);
2822 if (link_radv_enabled(link
)) {
2823 r
= radv_configure(link
);
2828 if (link_lldp_rx_enabled(link
)) {
2829 r
= sd_lldp_new(&link
->lldp
);
2833 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2837 r
= sd_lldp_match_capabilities(link
->lldp
,
2838 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2839 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2840 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2844 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2848 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2852 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2856 r
= link_update_lldp(link
);
2861 if (link
->network
->mtu
> 0) {
2862 r
= link_set_mtu(link
, link
->network
->mtu
);
2867 return link_configure_after_setting_mtu(link
);
2870 static int link_configure_after_setting_mtu(Link
*link
) {
2874 assert(link
->network
);
2875 assert(link
->state
== LINK_STATE_PENDING
);
2877 if (link
->setting_mtu
)
2880 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2881 r
= link_acquire_conf(link
);
2886 return link_enter_join_netdev(link
);
2889 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2892 if (duid
->raw_data_len
> 0)
2895 if (duid
->type
!= DUID_TYPE_UUID
)
2898 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2899 duid
->raw_data_len
= sizeof(sd_id128_t
);
2904 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2905 Manager
*manager
= userdata
;
2906 const sd_bus_error
*e
;
2916 e
= sd_bus_message_get_error(m
);
2918 log_error_errno(sd_bus_error_get_errno(e
),
2919 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2924 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2928 if (sz
!= sizeof(sd_id128_t
)) {
2929 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2933 memcpy(&manager
->product_uuid
, a
, sz
);
2934 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2935 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2937 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2940 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2941 r
= link_configure(link
);
2943 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2946 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2948 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2949 * even if the method fails. */
2950 manager
->has_product_uuid
= true;
2955 static bool link_requires_uuid(Link
*link
) {
2959 assert(link
->manager
);
2960 assert(link
->network
);
2962 duid
= link_get_duid(link
);
2963 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2966 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2969 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2975 static int link_configure_duid(Link
*link
) {
2981 assert(link
->manager
);
2982 assert(link
->network
);
2985 duid
= link_get_duid(link
);
2987 if (!link_requires_uuid(link
))
2990 if (m
->has_product_uuid
) {
2991 (void) duid_set_uuid(duid
, m
->product_uuid
);
2995 if (!m
->links_requesting_uuid
) {
2996 r
= manager_request_product_uuid(m
, link
);
3001 log_link_warning_errno(link
, r
,
3002 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3006 r
= set_put(m
->links_requesting_uuid
, link
);
3010 r
= set_put(m
->duids_requesting_uuid
, duid
);
3018 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
3020 _cleanup_(link_unrefp
) Link
*link
= userdata
;
3025 assert(link
->ifname
);
3026 assert(link
->manager
);
3028 if (link
->state
!= LINK_STATE_PENDING
)
3031 log_link_debug(link
, "Link state is up-to-date");
3033 r
= link_new_bound_by_list(link
);
3037 r
= link_handle_bound_by_list(link
);
3041 if (!link
->network
) {
3042 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3043 &link
->mac
, &network
);
3045 link_enter_unmanaged(link
);
3047 } else if (r
== 0 && network
->unmanaged
) {
3048 link_enter_unmanaged(link
);
3053 if (link
->flags
& IFF_LOOPBACK
) {
3054 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3055 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3057 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3058 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3060 if (network
->dhcp_server
)
3061 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3064 r
= network_apply(network
, link
);
3069 r
= link_new_bound_to_list(link
);
3073 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3074 * link_configure() is called later asynchronously. */
3075 r
= link_configure_duid(link
);
3079 r
= link_configure(link
);
3086 int link_initialized(Link
*link
, sd_device
*device
) {
3087 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3091 assert(link
->manager
);
3092 assert(link
->manager
->rtnl
);
3095 if (link
->state
!= LINK_STATE_PENDING
)
3098 if (link
->sd_device
)
3101 log_link_debug(link
, "udev initialized link");
3103 link
->sd_device
= sd_device_ref(device
);
3105 /* udev has initialized the link, but we don't know if we have yet
3106 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3107 * when it returns we know that the pending NEWLINKs have already been
3108 * processed and that we are up-to-date */
3110 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3115 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
3116 link_initialized_and_synced
, link
, 0, NULL
);
3125 static int link_load(Link
*link
) {
3126 _cleanup_free_
char *network_file
= NULL
,
3129 *dhcp4_address
= NULL
,
3130 *ipv4ll_address
= NULL
;
3131 union in_addr_union address
;
3132 union in_addr_union route_dst
;
3138 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
3139 "NETWORK_FILE", &network_file
,
3140 "ADDRESSES", &addresses
,
3142 "DHCP4_ADDRESS", &dhcp4_address
,
3143 "IPV4LL_ADDRESS", &ipv4ll_address
,
3145 if (r
< 0 && r
!= -ENOENT
)
3146 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3153 suffix
= strrchr(network_file
, '.');
3155 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3156 goto network_file_fail
;
3160 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3162 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3163 goto network_file_fail
;
3166 r
= network_apply(network
, link
);
3168 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3177 _cleanup_free_
char *address_str
= NULL
;
3178 char *prefixlen_str
;
3180 unsigned char prefixlen
;
3182 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3184 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3190 prefixlen_str
= strchr(address_str
, '/');
3191 if (!prefixlen_str
) {
3192 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3196 *prefixlen_str
++ = '\0';
3198 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3200 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3204 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3206 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3210 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3212 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3221 _cleanup_free_
char *route_str
= NULL
;
3222 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3224 char *prefixlen_str
;
3226 unsigned char prefixlen
, tos
, table
;
3229 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3231 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3237 prefixlen_str
= strchr(route_str
, '/');
3238 if (!prefixlen_str
) {
3239 log_link_debug(link
, "Failed to parse route %s", route_str
);
3243 *prefixlen_str
++ = '\0';
3245 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3247 log_link_debug(link
,
3248 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3253 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3255 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3259 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3261 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3263 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3264 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3265 0, route_expire_handler
, route
);
3267 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3270 route
->lifetime
= lifetime
;
3271 sd_event_source_unref(route
->expire
);
3272 route
->expire
= TAKE_PTR(expire
);
3276 if (dhcp4_address
) {
3277 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3279 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3280 goto dhcp4_address_fail
;
3283 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3285 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3287 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3289 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3294 if (ipv4ll_address
) {
3295 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3297 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3298 goto ipv4ll_address_fail
;
3301 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3303 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3305 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3307 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3310 ipv4ll_address_fail
:
3315 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3316 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3317 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3326 r
= link_new(m
, message
, ret
);
3332 log_link_debug(link
, "Link %d added", link
->ifindex
);
3334 r
= link_load(link
);
3338 if (detect_container() <= 0) {
3339 /* not in a container, udev will be around */
3340 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3341 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3343 log_link_warning_errno(link
, r
, "Could not find device: %m");
3347 r
= sd_device_get_is_initialized(device
, &initialized
);
3349 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3354 log_link_debug(link
, "link pending udev initialization...");
3358 r
= link_initialized(link
, device
);
3362 /* we are calling a callback directly, so must take a ref */
3365 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3372 link_enter_failed(link
);
3376 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3381 log_link_info(link
, "Gained IPv6LL");
3383 link
->ipv6ll_address
= *address
;
3384 link_check_ready(link
);
3386 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3387 r
= link_acquire_ipv6_conf(link
);
3389 link_enter_failed(link
);
3397 static int link_carrier_gained(Link
*link
) {
3402 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3403 r
= link_acquire_conf(link
);
3405 link_enter_failed(link
);
3409 r
= link_enter_set_addresses(link
);
3414 r
= link_handle_bound_by_list(link
);
3421 static int link_carrier_lost(Link
*link
) {
3426 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3427 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3428 * configuration and stop the clients as well. */
3429 if (link
->setting_mtu
)
3432 r
= link_stop_clients(link
);
3434 link_enter_failed(link
);
3438 if (link_dhcp4_server_enabled(link
))
3439 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3441 r
= link_drop_config(link
);
3445 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3446 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3447 r
= link_drop_foreign_config(link
);
3452 r
= link_handle_bound_by_list(link
);
3459 int link_carrier_reset(Link
*link
) {
3464 if (link_has_carrier(link
)) {
3465 r
= link_carrier_lost(link
);
3469 r
= link_carrier_gained(link
);
3473 log_link_info(link
, "Reset carrier");
3479 int link_update(Link
*link
, sd_netlink_message
*m
) {
3480 struct ether_addr mac
;
3483 bool had_carrier
, carrier_gained
, carrier_lost
;
3487 assert(link
->ifname
);
3490 if (link
->state
== LINK_STATE_LINGER
) {
3492 log_link_info(link
, "Link readded");
3493 link_set_state(link
, LINK_STATE_ENSLAVING
);
3495 r
= link_new_carrier_maps(link
);
3500 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3501 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3502 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3504 if (link
->state
== LINK_STATE_PENDING
) {
3505 r
= free_and_strdup(&link
->ifname
, ifname
);
3509 Manager
*manager
= link
->manager
;
3512 r
= link_add(manager
, m
, &link
);
3518 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3519 if (r
>= 0 && mtu
> 0) {
3521 if (link
->original_mtu
== 0) {
3522 link
->original_mtu
= mtu
;
3523 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3526 if (link
->dhcp_client
) {
3527 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3530 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3534 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3536 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3540 /* The kernel may broadcast NEWLINK messages without the MAC address
3541 set, simply ignore them. */
3542 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3544 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3547 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3550 log_link_debug(link
, "MAC address: "
3551 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3552 mac
.ether_addr_octet
[0],
3553 mac
.ether_addr_octet
[1],
3554 mac
.ether_addr_octet
[2],
3555 mac
.ether_addr_octet
[3],
3556 mac
.ether_addr_octet
[4],
3557 mac
.ether_addr_octet
[5]);
3560 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3562 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3565 if (link
->dhcp_client
) {
3566 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3567 (const uint8_t *) &link
->mac
,
3571 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3573 r
= dhcp4_set_client_identifier(link
);
3578 if (link
->dhcp6_client
) {
3579 const DUID
* duid
= link_get_duid(link
);
3581 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3582 (const uint8_t *) &link
->mac
,
3586 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3588 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3589 link
->network
->iaid
);
3591 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3593 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3595 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3596 duid
->raw_data_len
);
3598 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3602 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3604 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3608 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3610 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3615 had_carrier
= link_has_carrier(link
);
3617 r
= link_update_flags(link
, m
);
3621 r
= link_update_lldp(link
);
3625 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3626 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3628 if (carrier_gained
) {
3629 log_link_info(link
, "Gained carrier");
3631 r
= link_carrier_gained(link
);
3634 } else if (carrier_lost
) {
3635 log_link_info(link
, "Lost carrier");
3637 r
= link_carrier_lost(link
);
3645 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3653 if (hashmap_isempty(h
))
3657 HASHMAP_FOREACH(link
, h
, i
) {
3661 fprintf(f
, "%i", link
->ifindex
);
3668 int link_save(Link
*link
) {
3669 _cleanup_free_
char *temp_path
= NULL
;
3670 _cleanup_fclose_
FILE *f
= NULL
;
3671 const char *admin_state
, *oper_state
;
3678 assert(link
->state_file
);
3679 assert(link
->lease_file
);
3680 assert(link
->manager
);
3682 if (link
->state
== LINK_STATE_LINGER
) {
3683 unlink(link
->state_file
);
3687 link_lldp_save(link
);
3689 admin_state
= link_state_to_string(link
->state
);
3690 assert(admin_state
);
3692 oper_state
= link_operstate_to_string(link
->operstate
);
3695 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3699 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3700 (void) fchmod(fileno(f
), 0644);
3703 "# This is private data. Do not parse.\n"
3706 admin_state
, oper_state
);
3708 if (link
->network
) {
3710 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3711 const char *dhcp_domainname
= NULL
;
3712 char **dhcp6_domains
= NULL
;
3713 char **dhcp_domains
= NULL
;
3716 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3717 yes_no(link
->network
->required_for_online
));
3719 if (link
->dhcp6_client
) {
3720 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3721 if (r
< 0 && r
!= -ENOMSG
)
3722 log_link_debug(link
, "No DHCPv6 lease");
3725 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3730 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3731 _cleanup_free_
char *b
= NULL
;
3733 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3734 &link
->network
->dns
[j
].address
, &b
);
3736 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3746 if (link
->network
->dhcp_use_dns
&&
3748 const struct in_addr
*addresses
;
3750 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3754 serialize_in_addrs(f
, addresses
, r
);
3759 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3760 struct in6_addr
*in6_addrs
;
3762 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3766 serialize_in6_addrs(f
, in6_addrs
, r
);
3771 /* Make sure to flush out old entries before we use the NDISC data */
3774 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3777 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3781 serialize_in6_addrs(f
, &dd
->address
, 1);
3790 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3792 if (link
->network
->dhcp_use_ntp
&&
3794 const struct in_addr
*addresses
;
3796 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3800 serialize_in_addrs(f
, addresses
, r
);
3805 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3806 struct in6_addr
*in6_addrs
;
3809 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3814 serialize_in6_addrs(f
, in6_addrs
, r
);
3818 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3820 fputstrv(f
, hosts
, NULL
, &space
);
3825 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3826 if (link
->dhcp_lease
) {
3827 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3828 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3831 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3834 fputs("DOMAINS=", f
);
3836 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3838 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3841 if (dhcp_domainname
)
3842 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3844 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3846 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3848 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3849 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3854 fputs("ROUTE_DOMAINS=", f
);
3856 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3858 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3861 if (dhcp_domainname
)
3862 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3864 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3866 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3868 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3869 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3874 fprintf(f
, "LLMNR=%s\n",
3875 resolve_support_to_string(link
->network
->llmnr
));
3876 fprintf(f
, "MDNS=%s\n",
3877 resolve_support_to_string(link
->network
->mdns
));
3879 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3880 fprintf(f
, "DNS_OVER_TLS=%s\n",
3881 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3883 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3884 fprintf(f
, "DNSSEC=%s\n",
3885 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3887 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3890 fputs("DNSSEC_NTA=", f
);
3892 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3893 fputs_with_space(f
, n
, NULL
, &space
);
3897 fputs("ADDRESSES=", f
);
3899 SET_FOREACH(a
, link
->addresses
, i
) {
3900 _cleanup_free_
char *address_str
= NULL
;
3902 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3906 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3911 fputs("ROUTES=", f
);
3913 SET_FOREACH(route
, link
->routes
, i
) {
3914 _cleanup_free_
char *route_str
= NULL
;
3916 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3920 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3921 space
? " " : "", route_str
,
3922 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3929 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3930 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3932 if (link
->dhcp_lease
) {
3933 struct in_addr address
;
3934 const char *tz
= NULL
;
3936 assert(link
->network
);
3938 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3940 fprintf(f
, "TIMEZONE=%s\n", tz
);
3942 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3944 fputs("DHCP4_ADDRESS=", f
);
3945 serialize_in_addrs(f
, &address
, 1);
3949 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3957 unlink(link
->lease_file
);
3960 struct in_addr address
;
3962 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3964 fputs("IPV4LL_ADDRESS=", f
);
3965 serialize_in_addrs(f
, &address
, 1);
3970 r
= fflush_and_check(f
);
3974 if (rename(temp_path
, link
->state_file
) < 0) {
3982 (void) unlink(link
->state_file
);
3984 (void) unlink(temp_path
);
3986 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3989 /* The serialized state in /run is no longer up-to-date. */
3990 void link_dirty(Link
*link
) {
3995 /* mark manager dirty as link is dirty */
3996 manager_dirty(link
->manager
);
3998 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4000 /* allocation errors are ignored */
4003 r
= set_put(link
->manager
->dirty_links
, link
);
4005 /* don't take another ref if the link was already dirty */
4011 /* The serialized state in /run is up-to-date */
4012 void link_clean(Link
*link
) {
4014 assert(link
->manager
);
4016 set_remove(link
->manager
->dirty_links
, link
);
4020 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4021 [LINK_STATE_PENDING
] = "pending",
4022 [LINK_STATE_ENSLAVING
] = "configuring",
4023 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4024 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4025 [LINK_STATE_CONFIGURED
] = "configured",
4026 [LINK_STATE_UNMANAGED
] = "unmanaged",
4027 [LINK_STATE_FAILED
] = "failed",
4028 [LINK_STATE_LINGER
] = "linger",
4031 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4033 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4034 [LINK_OPERSTATE_OFF
] = "off",
4035 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4036 [LINK_OPERSTATE_DORMANT
] = "dormant",
4037 [LINK_OPERSTATE_CARRIER
] = "carrier",
4038 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4039 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4042 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);