1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
15 #include "netlink-util.h"
16 #include "network-internal.h"
17 #include "networkd-ipv6-proxy-ndp.h"
18 #include "networkd-lldp-tx.h"
19 #include "networkd-manager.h"
20 #include "networkd-ndisc.h"
21 #include "networkd-radv.h"
22 #include "networkd-routing-policy-rule.h"
24 #include "socket-util.h"
25 #include "stdio-util.h"
26 #include "string-table.h"
31 DUID
* link_get_duid(Link
*link
) {
32 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
33 return &link
->network
->duid
;
35 return &link
->manager
->duid
;
38 static bool link_dhcp6_enabled(Link
*link
) {
41 if (!socket_ipv6_is_supported())
44 if (link
->flags
& IFF_LOOPBACK
)
50 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
53 static bool link_dhcp4_enabled(Link
*link
) {
56 if (link
->flags
& IFF_LOOPBACK
)
62 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
65 static bool link_dhcp4_server_enabled(Link
*link
) {
68 if (link
->flags
& IFF_LOOPBACK
)
74 return link
->network
->dhcp_server
;
77 static bool link_ipv4ll_enabled(Link
*link
) {
80 if (link
->flags
& IFF_LOOPBACK
)
86 if (streq_ptr(link
->kind
, "wireguard"))
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 static bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 if (streq_ptr(link
->kind
, "wireguard"))
107 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
110 static bool link_ipv6_enabled(Link
*link
) {
113 if (!socket_ipv6_is_supported())
116 if (link
->network
->bridge
)
119 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
120 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
123 static bool link_radv_enabled(Link
*link
) {
126 if (!link_ipv6ll_enabled(link
))
129 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
132 static bool link_lldp_rx_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
138 if (link
->iftype
!= ARPHRD_ETHER
)
144 /* LLDP should be handled on bridge slaves as those have a direct
145 * connection to their peers not on the bridge master. Linux doesn't
146 * even (by default) forward lldp packets to the bridge master.*/
147 if (streq_ptr("bridge", link
->kind
))
150 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
153 static bool link_lldp_emit_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
159 if (link
->iftype
!= ARPHRD_ETHER
)
165 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
168 static bool link_ipv4_forward_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
180 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
183 static bool link_ipv6_forward_enabled(Link
*link
) {
186 if (!socket_ipv6_is_supported())
189 if (link
->flags
& IFF_LOOPBACK
)
195 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
198 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
201 static bool link_proxy_arp_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
210 if (link
->network
->proxy_arp
< 0)
216 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
219 if (!socket_ipv6_is_supported())
222 if (link
->flags
& IFF_LOOPBACK
)
228 if (!link_ipv6ll_enabled(link
))
231 /* If unset use system default (enabled if local forwarding is disabled.
232 * disabled if local forwarding is enabled).
233 * If set, ignore or enforce RA independent of local forwarding state.
235 if (link
->network
->ipv6_accept_ra
< 0)
236 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
237 return !link_ipv6_forward_enabled(link
);
238 else if (link
->network
->ipv6_accept_ra
> 0)
239 /* accept RA even if ip_forward is enabled */
246 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
249 if (!socket_ipv6_is_supported())
250 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
252 if (link
->flags
& IFF_LOOPBACK
)
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
256 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
258 return link
->network
->ipv6_privacy_extensions
;
261 static int link_enable_ipv6(Link
*link
) {
262 const char *p
= NULL
;
266 if (link
->flags
& IFF_LOOPBACK
)
269 disabled
= !link_ipv6_enabled(link
);
271 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
273 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
275 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
276 enable_disable(!disabled
), link
->ifname
);
278 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
283 void link_update_operstate(Link
*link
) {
284 LinkOperationalState operstate
;
287 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
288 operstate
= LINK_OPERSTATE_DORMANT
;
289 else if (link_has_carrier(link
)) {
291 uint8_t scope
= RT_SCOPE_NOWHERE
;
294 /* if we have carrier, check what addresses we have */
295 SET_FOREACH(address
, link
->addresses
, i
) {
296 if (!address_is_ready(address
))
299 if (address
->scope
< scope
)
300 scope
= address
->scope
;
303 /* for operstate we also take foreign addresses into account */
304 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
305 if (!address_is_ready(address
))
308 if (address
->scope
< scope
)
309 scope
= address
->scope
;
312 if (scope
< RT_SCOPE_SITE
)
313 /* universally accessible addresses found */
314 operstate
= LINK_OPERSTATE_ROUTABLE
;
315 else if (scope
< RT_SCOPE_HOST
)
316 /* only link or site local addresses found */
317 operstate
= LINK_OPERSTATE_DEGRADED
;
319 /* no useful addresses found */
320 operstate
= LINK_OPERSTATE_CARRIER
;
321 } else if (link
->flags
& IFF_UP
)
322 operstate
= LINK_OPERSTATE_NO_CARRIER
;
324 operstate
= LINK_OPERSTATE_OFF
;
326 if (link
->operstate
!= operstate
) {
327 link
->operstate
= operstate
;
328 link_send_changed(link
, "OperationalState", NULL
);
333 #define FLAG_STRING(string, flag, old, new) \
334 (((old ^ new) & flag) \
335 ? ((old & flag) ? (" -" string) : (" +" string)) \
338 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
339 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
345 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
347 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
349 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
351 /* if we got a message without operstate, take it to mean
352 the state was unchanged */
353 operstate
= link
->kernel_operstate
;
355 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
358 if (link
->flags
!= flags
) {
359 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
360 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
361 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
362 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
363 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
364 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
365 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
366 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
367 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
368 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
369 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
370 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
371 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
372 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
373 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
374 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
375 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
376 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
377 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
378 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
380 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
381 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
382 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
383 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
384 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
385 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
386 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
387 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
389 /* link flags are currently at most 18 bits, let's align to
391 if (unknown_flags_added
)
393 "Unknown link flags gained: %#.5x (ignoring)",
394 unknown_flags_added
);
396 if (unknown_flags_removed
)
398 "Unknown link flags lost: %#.5x (ignoring)",
399 unknown_flags_removed
);
403 link
->kernel_operstate
= operstate
;
405 link_update_operstate(link
);
410 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
412 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
413 _cleanup_(link_unrefp
) Link
*link
= NULL
;
415 const char *ifname
, *kind
= NULL
;
417 unsigned short iftype
;
423 /* check for link kind */
424 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
426 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
427 r
= sd_netlink_message_exit_container(message
);
432 r
= sd_netlink_message_get_type(message
, &type
);
435 else if (type
!= RTM_NEWLINK
)
438 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
441 else if (ifindex
<= 0)
444 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
448 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
459 .state
= LINK_STATE_PENDING
,
460 .rtnl_extended_attrs
= true,
465 link
->ifname
= strdup(ifname
);
470 link
->kind
= strdup(kind
);
475 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
477 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
479 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
481 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
483 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
486 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
489 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
492 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
496 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
500 r
= link_update_flags(link
, message
);
504 *ret
= TAKE_PTR(link
);
509 static void link_detach_from_manager(Link
*link
) {
510 if (!link
|| !link
->manager
)
513 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
514 set_remove(link
->manager
->links_requesting_uuid
, link
);
517 link
->manager
= NULL
;
520 static Link
*link_free(Link
*link
) {
528 while ((route
= set_first(link
->routes
)))
531 while ((route
= set_first(link
->routes_foreign
)))
534 link
->routes
= set_free(link
->routes
);
535 link
->routes_foreign
= set_free(link
->routes_foreign
);
537 while ((address
= set_first(link
->addresses
)))
538 address_free(address
);
540 while ((address
= set_first(link
->addresses_foreign
)))
541 address_free(address
);
543 link
->addresses
= set_free(link
->addresses
);
544 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
546 while ((address
= link
->pool_addresses
)) {
547 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
548 address_free(address
);
551 sd_dhcp_server_unref(link
->dhcp_server
);
552 sd_dhcp_client_unref(link
->dhcp_client
);
553 sd_dhcp_lease_unref(link
->dhcp_lease
);
555 link_lldp_emit_stop(link
);
557 free(link
->lease_file
);
559 sd_lldp_unref(link
->lldp
);
560 free(link
->lldp_file
);
564 sd_ipv4ll_unref(link
->ipv4ll
);
565 sd_dhcp6_client_unref(link
->dhcp6_client
);
566 sd_ndisc_unref(link
->ndisc
);
567 sd_radv_unref(link
->radv
);
569 link_detach_from_manager(link
);
575 (void) unlink(link
->state_file
);
576 free(link
->state_file
);
578 sd_device_unref(link
->sd_device
);
580 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
581 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
582 hashmap_free(link
->bound_to_links
);
584 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
585 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
586 hashmap_free(link
->bound_by_links
);
591 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
593 void link_netlink_destroy_callback(void *userdata
) {
594 Link
*link
= userdata
;
601 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
608 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
617 static void link_set_state(Link
*link
, LinkState state
) {
620 if (link
->state
== state
)
625 link_send_changed(link
, "AdministrativeState", NULL
);
628 static void link_enter_unmanaged(Link
*link
) {
631 log_link_debug(link
, "Unmanaged");
633 link_set_state(link
, LINK_STATE_UNMANAGED
);
638 static int link_stop_clients(Link
*link
) {
642 assert(link
->manager
);
643 assert(link
->manager
->event
);
645 if (link
->dhcp_client
) {
646 k
= sd_dhcp_client_stop(link
->dhcp_client
);
648 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
652 k
= sd_ipv4ll_stop(link
->ipv4ll
);
654 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
657 if (link
->dhcp6_client
) {
658 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
660 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
664 k
= sd_ndisc_stop(link
->ndisc
);
666 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
670 k
= sd_radv_stop(link
->radv
);
672 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
675 link_lldp_emit_stop(link
);
679 void link_enter_failed(Link
*link
) {
682 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
685 log_link_warning(link
, "Failed");
687 link_set_state(link
, LINK_STATE_FAILED
);
689 link_stop_clients(link
);
694 static Address
* link_find_dhcp_server_address(Link
*link
) {
698 assert(link
->network
);
700 /* The first statically configured address if there is any */
701 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
703 if (address
->family
!= AF_INET
)
706 if (in_addr_is_null(address
->family
, &address
->in_addr
))
712 /* If that didn't work, find a suitable address we got from the pool */
713 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
714 if (address
->family
!= AF_INET
)
723 static void link_enter_configured(Link
*link
) {
725 assert(link
->network
);
727 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
730 log_link_info(link
, "Configured");
732 link_set_state(link
, LINK_STATE_CONFIGURED
);
737 void link_check_ready(Link
*link
) {
743 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
749 if (!link
->static_routes_configured
)
752 if (!link
->routing_policy_rules_configured
)
755 if (link_ipv4ll_enabled(link
))
756 if (!link
->ipv4ll_address
||
760 if (!link
->network
->bridge
) {
762 if (link_ipv6ll_enabled(link
))
763 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
766 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
767 !link
->dhcp4_configured
) ||
768 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
769 !link
->dhcp6_configured
) ||
770 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
771 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
774 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
778 SET_FOREACH(a
, link
->addresses
, i
)
779 if (!address_is_ready(a
))
782 if (link
->state
!= LINK_STATE_CONFIGURED
)
783 link_enter_configured(link
);
788 static int link_set_routing_policy_rule(Link
*link
) {
789 RoutingPolicyRule
*rule
, *rrule
= NULL
;
793 assert(link
->network
);
795 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
796 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
797 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
798 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
800 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
804 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
806 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
807 link_enter_failed(link
);
811 link
->routing_policy_rule_messages
++;
814 routing_policy_rule_purge(link
->manager
, link
);
815 if (link
->routing_policy_rule_messages
== 0) {
816 link
->routing_policy_rules_configured
= true;
817 link_check_ready(link
);
819 log_link_debug(link
, "Setting routing policy rules");
824 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
825 Link
*link
= userdata
;
829 assert(link
->route_messages
> 0);
830 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
831 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
834 link
->route_messages
--;
836 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
839 r
= sd_netlink_message_get_errno(m
);
840 if (r
< 0 && r
!= -EEXIST
)
841 log_link_warning_errno(link
, r
, "Could not set route: %m");
843 if (link
->route_messages
== 0) {
844 log_link_debug(link
, "Routes set");
845 link
->static_routes_configured
= true;
846 link_check_ready(link
);
852 static int link_enter_set_routes(Link
*link
) {
857 assert(link
->network
);
858 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
860 (void) link_set_routing_policy_rule(link
);
862 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
864 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
865 r
= route_configure(rt
, link
, route_handler
);
867 log_link_warning_errno(link
, r
, "Could not set routes: %m");
868 link_enter_failed(link
);
872 link
->route_messages
++;
875 if (link
->route_messages
== 0) {
876 link
->static_routes_configured
= true;
877 link_check_ready(link
);
879 log_link_debug(link
, "Setting routes");
884 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
885 Link
*link
= userdata
;
890 assert(link
->ifname
);
892 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
895 r
= sd_netlink_message_get_errno(m
);
896 if (r
< 0 && r
!= -ESRCH
)
897 log_link_warning_errno(link
, r
, "Could not drop route: %m");
902 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
903 Link
*link
= userdata
;
909 assert(link
->ifname
);
910 assert(link
->address_messages
> 0);
911 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
912 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
914 link
->address_messages
--;
916 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
919 r
= sd_netlink_message_get_errno(m
);
920 if (r
< 0 && r
!= -EEXIST
)
921 log_link_warning_errno(link
, r
, "could not set address: %m");
923 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
925 if (link
->address_messages
== 0) {
926 log_link_debug(link
, "Addresses set");
927 link_enter_set_routes(link
);
933 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
934 Link
*link
= userdata
;
940 assert(link
->ifname
);
941 assert(link
->address_label_messages
> 0);
943 link
->address_label_messages
--;
945 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
948 r
= sd_netlink_message_get_errno(m
);
949 if (r
< 0 && r
!= -EEXIST
)
950 log_link_warning_errno(link
, r
, "could not set address label: %m");
952 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
954 if (link
->address_label_messages
== 0)
955 log_link_debug(link
, "Addresses label set");
960 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
961 _cleanup_free_
struct in_addr
*addresses
= NULL
;
962 size_t n_addresses
= 0, n_allocated
= 0;
965 log_debug("Copying DNS server information from %s", link
->ifname
);
970 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
973 /* Only look for IPv4 addresses */
974 if (link
->network
->dns
[i
].family
!= AF_INET
)
977 ia
= link
->network
->dns
[i
].address
.in
;
979 /* Never propagate obviously borked data */
980 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
983 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
986 addresses
[n_addresses
++] = ia
;
989 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
990 const struct in_addr
*da
= NULL
;
993 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
996 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
999 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1004 if (n_addresses
<= 0)
1007 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1010 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1011 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1012 size_t n_addresses
= 0, n_allocated
= 0;
1018 log_debug("Copying NTP server information from %s", link
->ifname
);
1020 STRV_FOREACH(a
, link
->network
->ntp
) {
1023 /* Only look for IPv4 addresses */
1024 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1027 /* Never propagate obviously borked data */
1028 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1031 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1034 addresses
[n_addresses
++] = ia
;
1037 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1038 const struct in_addr
*da
= NULL
;
1041 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1044 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1047 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1052 if (n_addresses
<= 0)
1055 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1058 static int link_set_bridge_fdb(Link
*link
) {
1059 FdbEntry
*fdb_entry
;
1062 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1063 r
= fdb_entry_configure(link
, fdb_entry
);
1065 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1071 static int link_enter_set_addresses(Link
*link
) {
1072 AddressLabel
*label
;
1077 assert(link
->network
);
1078 assert(link
->state
!= _LINK_STATE_INVALID
);
1080 r
= link_set_bridge_fdb(link
);
1084 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1086 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1087 r
= address_configure(ad
, link
, address_handler
, false);
1089 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1090 link_enter_failed(link
);
1094 link
->address_messages
++;
1097 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1098 r
= address_label_configure(label
, link
, address_label_handler
, false);
1100 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1101 link_enter_failed(link
);
1105 link
->address_label_messages
++;
1108 /* now that we can figure out a default address for the dhcp server,
1110 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1112 Link
*uplink
= NULL
;
1113 bool acquired_uplink
= false;
1115 address
= link_find_dhcp_server_address(link
);
1117 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1118 link_enter_failed(link
);
1122 /* use the server address' subnet as the pool */
1123 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1124 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1129 r = sd_dhcp_server_set_router(link->dhcp_server,
1130 &main_address->in_addr.in);
1135 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1136 r
= sd_dhcp_server_set_max_lease_time(
1138 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1143 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1144 r
= sd_dhcp_server_set_default_lease_time(
1146 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1151 if (link
->network
->dhcp_server_emit_dns
) {
1153 if (link
->network
->n_dhcp_server_dns
> 0)
1154 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1156 uplink
= manager_find_uplink(link
->manager
, link
);
1157 acquired_uplink
= true;
1160 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1163 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1166 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1169 if (link
->network
->dhcp_server_emit_ntp
) {
1171 if (link
->network
->n_dhcp_server_ntp
> 0)
1172 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1174 if (!acquired_uplink
)
1175 uplink
= manager_find_uplink(link
->manager
, link
);
1178 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1181 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1185 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1188 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1190 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1192 if (link
->network
->dhcp_server_emit_timezone
) {
1193 _cleanup_free_
char *buffer
= NULL
;
1194 const char *tz
= NULL
;
1196 if (link
->network
->dhcp_server_timezone
)
1197 tz
= link
->network
->dhcp_server_timezone
;
1199 r
= get_timezone(&buffer
);
1201 log_warning_errno(r
, "Failed to determine timezone: %m");
1207 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1213 r
= sd_dhcp_server_start(link
->dhcp_server
);
1215 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1217 link_enter_failed(link
);
1222 log_link_debug(link
, "Offering DHCPv4 leases");
1225 if (link
->address_messages
== 0)
1226 link_enter_set_routes(link
);
1228 log_link_debug(link
, "Setting addresses");
1233 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1234 Link
*link
= userdata
;
1239 assert(link
->ifname
);
1241 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1244 r
= sd_netlink_message_get_errno(m
);
1245 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1246 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1251 static int link_set_bridge_vlan(Link
*link
) {
1254 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1256 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1261 static int link_set_proxy_arp(Link
*link
) {
1262 const char *p
= NULL
;
1265 if (!link_proxy_arp_enabled(link
))
1268 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1270 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1272 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1277 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1278 Link
*link
= userdata
;
1283 log_link_debug(link
, "Set link");
1285 r
= sd_netlink_message_get_errno(m
);
1286 if (r
< 0 && r
!= -EEXIST
) {
1287 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1288 link_enter_failed(link
);
1294 static int link_configure_after_setting_mtu(Link
*link
);
1296 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1297 Link
*link
= userdata
;
1302 assert(link
->ifname
);
1304 link
->setting_mtu
= false;
1306 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1309 r
= sd_netlink_message_get_errno(m
);
1311 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1315 log_link_debug(link
, "Setting MTU done.");
1317 if (link
->state
== LINK_STATE_PENDING
)
1318 (void) link_configure_after_setting_mtu(link
);
1323 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1324 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1328 assert(link
->manager
);
1329 assert(link
->manager
->rtnl
);
1331 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1334 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1336 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1338 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1340 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1341 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1342 (void) link_enable_ipv6(link
);
1344 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1345 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1346 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1348 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1349 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1354 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1356 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1358 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1359 link_netlink_destroy_callback
, link
, 0, __func__
);
1361 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1364 link
->setting_mtu
= true;
1369 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1370 Link
*link
= userdata
;
1375 assert(link
->ifname
);
1377 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1380 r
= sd_netlink_message_get_errno(m
);
1382 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1387 static int link_set_flags(Link
*link
) {
1388 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1389 unsigned ifi_change
= 0;
1390 unsigned ifi_flags
= 0;
1394 assert(link
->manager
);
1395 assert(link
->manager
->rtnl
);
1397 if (link
->flags
& IFF_LOOPBACK
)
1403 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1406 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1408 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1410 if (link
->network
->arp
>= 0) {
1411 ifi_change
|= IFF_NOARP
;
1412 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1415 if (link
->network
->multicast
>= 0) {
1416 ifi_change
|= IFF_MULTICAST
;
1417 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1420 if (link
->network
->allmulticast
>= 0) {
1421 ifi_change
|= IFF_ALLMULTI
;
1422 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1425 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1427 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1429 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1430 link_netlink_destroy_callback
, link
, 0, __func__
);
1432 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1439 static int link_set_bridge(Link
*link
) {
1440 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1444 assert(link
->network
);
1446 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1448 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1450 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1452 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1454 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1456 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1458 if (link
->network
->use_bpdu
>= 0) {
1459 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1461 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1464 if (link
->network
->hairpin
>= 0) {
1465 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1467 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1470 if (link
->network
->fast_leave
>= 0) {
1471 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1473 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1476 if (link
->network
->allow_port_to_be_root
>= 0) {
1477 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1479 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1483 if (link
->network
->unicast_flood
>= 0) {
1484 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1486 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1490 if (link
->network
->cost
!= 0) {
1491 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1493 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1495 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1496 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1498 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1501 r
= sd_netlink_message_close_container(req
);
1503 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1505 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1506 link_netlink_destroy_callback
, link
, 0, __func__
);
1508 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1515 static int link_bond_set(Link
*link
) {
1516 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1520 assert(link
->network
);
1522 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1524 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1526 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1528 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1530 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1532 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1534 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1536 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1538 if (link
->network
->active_slave
) {
1539 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1541 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1544 if (link
->network
->primary_slave
) {
1545 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1547 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1550 r
= sd_netlink_message_close_container(req
);
1552 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1554 r
= sd_netlink_message_close_container(req
);
1556 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1558 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1559 link_netlink_destroy_callback
, link
, 0, __func__
);
1561 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1568 static int link_lldp_save(Link
*link
) {
1569 _cleanup_free_
char *temp_path
= NULL
;
1570 _cleanup_fclose_
FILE *f
= NULL
;
1571 sd_lldp_neighbor
**l
= NULL
;
1575 assert(link
->lldp_file
);
1578 (void) unlink(link
->lldp_file
);
1582 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1586 (void) unlink(link
->lldp_file
);
1592 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1596 fchmod(fileno(f
), 0644);
1598 for (i
= 0; i
< n
; i
++) {
1603 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1608 (void) fwrite(&u
, 1, sizeof(u
), f
);
1609 (void) fwrite(p
, 1, sz
, f
);
1612 r
= fflush_and_check(f
);
1616 if (rename(temp_path
, link
->lldp_file
) < 0) {
1623 (void) unlink(link
->lldp_file
);
1625 (void) unlink(temp_path
);
1627 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1631 for (i
= 0; i
< n
; i
++)
1632 sd_lldp_neighbor_unref(l
[i
]);
1639 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1640 Link
*link
= userdata
;
1645 (void) link_lldp_save(link
);
1647 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1648 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1650 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1652 r
= link_lldp_emit_start(link
);
1654 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1658 static int link_acquire_ipv6_conf(Link
*link
) {
1663 if (link_ipv6_accept_ra_enabled(link
)) {
1664 assert(link
->ndisc
);
1666 log_link_debug(link
, "Discovering IPv6 routers");
1668 r
= sd_ndisc_start(link
->ndisc
);
1669 if (r
< 0 && r
!= -EBUSY
)
1670 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1673 if (link_radv_enabled(link
)) {
1675 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1677 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1679 r
= sd_radv_start(link
->radv
);
1680 if (r
< 0 && r
!= -EBUSY
)
1681 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1684 (void) dhcp6_request_prefix_delegation(link
);
1689 static int link_acquire_ipv4_conf(Link
*link
) {
1693 assert(link
->network
);
1694 assert(link
->manager
);
1695 assert(link
->manager
->event
);
1697 if (link_ipv4ll_enabled(link
)) {
1698 assert(link
->ipv4ll
);
1700 log_link_debug(link
, "Acquiring IPv4 link-local address");
1702 r
= sd_ipv4ll_start(link
->ipv4ll
);
1704 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1707 if (link_dhcp4_enabled(link
)) {
1708 assert(link
->dhcp_client
);
1710 log_link_debug(link
, "Acquiring DHCPv4 lease");
1712 r
= sd_dhcp_client_start(link
->dhcp_client
);
1714 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1720 static int link_acquire_conf(Link
*link
) {
1725 r
= link_acquire_ipv4_conf(link
);
1729 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1730 r
= link_acquire_ipv6_conf(link
);
1735 if (link_lldp_emit_enabled(link
)) {
1736 r
= link_lldp_emit_start(link
);
1738 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1744 bool link_has_carrier(Link
*link
) {
1745 /* see Documentation/networking/operstates.txt in the kernel sources */
1747 if (link
->kernel_operstate
== IF_OPER_UP
)
1750 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1751 /* operstate may not be implemented, so fall back to flags */
1752 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1758 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1759 Link
*link
= userdata
;
1764 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1767 r
= sd_netlink_message_get_errno(m
);
1769 /* we warn but don't fail the link, as it may be brought up later */
1770 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1775 int link_up(Link
*link
) {
1776 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1777 uint8_t ipv6ll_mode
;
1781 assert(link
->network
);
1782 assert(link
->manager
);
1783 assert(link
->manager
->rtnl
);
1785 log_link_debug(link
, "Bringing link up");
1787 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1789 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1791 /* set it free if not enslaved with networkd */
1792 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1793 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1795 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1798 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1800 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1802 if (link
->network
->mac
) {
1803 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1805 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1808 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1810 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1812 if (link_ipv6_enabled(link
)) {
1813 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1814 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1816 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1818 if (!link_ipv6ll_enabled(link
))
1819 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1821 const char *p
= NULL
;
1822 _cleanup_free_
char *stable_secret
= NULL
;
1824 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1825 r
= read_one_line_file(p
, &stable_secret
);
1828 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1830 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1832 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1834 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1836 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1837 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1839 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1842 r
= sd_netlink_message_close_container(req
);
1844 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1847 r
= sd_netlink_message_close_container(req
);
1849 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1851 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1852 link_netlink_destroy_callback
, link
, 0, __func__
);
1854 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1861 static int link_up_can(Link
*link
) {
1862 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1867 log_link_debug(link
, "Bringing CAN link up");
1869 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1871 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1873 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1875 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1877 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1878 link_netlink_destroy_callback
, link
, 0, __func__
);
1880 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1887 static int link_set_can(Link
*link
) {
1888 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1892 assert(link
->network
);
1893 assert(link
->manager
);
1894 assert(link
->manager
->rtnl
);
1896 log_link_debug(link
, "link_set_can");
1898 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1900 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1902 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1904 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1906 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1908 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1910 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1912 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1914 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1915 struct can_bittiming bt
= {
1916 .bitrate
= link
->network
->can_bitrate
,
1917 .sample_point
= link
->network
->can_sample_point
,
1920 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1921 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1925 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1926 if (link
->network
->can_sample_point
> 0)
1927 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1929 log_link_debug(link
, "Using default sample point");
1931 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1933 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1936 if (link
->network
->can_restart_us
> 0) {
1937 char time_string
[FORMAT_TIMESPAN_MAX
];
1938 uint64_t restart_ms
;
1940 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1943 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1945 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1947 if (restart_ms
> UINT32_MAX
) {
1948 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1952 log_link_debug(link
, "Setting restart = %s", time_string
);
1954 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1956 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %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_message_close_container(m
);
1965 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1967 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1968 link_netlink_destroy_callback
, link
, 0, __func__
);
1970 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1974 if (!(link
->flags
& IFF_UP
)) {
1975 r
= link_up_can(link
);
1977 link_enter_failed(link
);
1982 log_link_debug(link
, "link_set_can done");
1987 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1988 Link
*link
= userdata
;
1993 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1996 r
= sd_netlink_message_get_errno(m
);
1998 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2000 if (streq_ptr(link
->kind
, "can"))
2006 int link_down(Link
*link
) {
2007 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2011 assert(link
->manager
);
2012 assert(link
->manager
->rtnl
);
2014 log_link_debug(link
, "Bringing link down");
2016 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2017 RTM_SETLINK
, link
->ifindex
);
2019 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2021 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2023 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2025 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2026 link_netlink_destroy_callback
, link
, 0, __func__
);
2028 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2035 static int link_handle_bound_to_list(Link
*link
) {
2039 bool required_up
= false;
2040 bool link_is_up
= false;
2044 if (hashmap_isempty(link
->bound_to_links
))
2047 if (link
->flags
& IFF_UP
)
2050 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2051 if (link_has_carrier(l
)) {
2056 if (!required_up
&& link_is_up
) {
2057 r
= link_down(link
);
2060 } else if (required_up
&& !link_is_up
) {
2069 static int link_handle_bound_by_list(Link
*link
) {
2076 if (hashmap_isempty(link
->bound_by_links
))
2079 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2080 r
= link_handle_bound_to_list(l
);
2088 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2094 if (link
== carrier
)
2097 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2100 r
= hashmap_ensure_allocated(h
, NULL
);
2104 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2111 static int link_new_bound_by_list(Link
*link
) {
2116 bool list_updated
= false;
2119 assert(link
->manager
);
2123 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2124 if (!carrier
->network
)
2127 if (strv_isempty(carrier
->network
->bind_carrier
))
2130 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2131 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2135 list_updated
= true;
2142 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2143 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2147 link_dirty(carrier
);
2153 static int link_new_bound_to_list(Link
*link
) {
2158 bool list_updated
= false;
2161 assert(link
->manager
);
2166 if (strv_isempty(link
->network
->bind_carrier
))
2171 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2172 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2173 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2177 list_updated
= true;
2184 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2185 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2189 link_dirty(carrier
);
2195 static int link_new_carrier_maps(Link
*link
) {
2198 r
= link_new_bound_by_list(link
);
2202 r
= link_handle_bound_by_list(link
);
2206 r
= link_new_bound_to_list(link
);
2210 r
= link_handle_bound_to_list(link
);
2217 static void link_free_bound_to_list(Link
*link
) {
2221 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2222 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2224 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2225 link_dirty(bound_to
);
2231 static void link_free_bound_by_list(Link
*link
) {
2235 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2236 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2238 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2239 link_dirty(bound_by
);
2240 link_handle_bound_to_list(bound_by
);
2247 static void link_free_carrier_maps(Link
*link
) {
2248 bool list_updated
= false;
2252 if (!hashmap_isempty(link
->bound_to_links
)) {
2253 link_free_bound_to_list(link
);
2254 list_updated
= true;
2257 if (!hashmap_isempty(link
->bound_by_links
)) {
2258 link_free_bound_by_list(link
);
2259 list_updated
= true;
2268 void link_drop(Link
*link
) {
2269 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2272 link_set_state(link
, LINK_STATE_LINGER
);
2274 link_free_carrier_maps(link
);
2276 log_link_debug(link
, "Link removed");
2278 (void) unlink(link
->state_file
);
2280 link_detach_from_manager(link
);
2287 static int link_joined(Link
*link
) {
2291 assert(link
->network
);
2293 if (!hashmap_isempty(link
->bound_to_links
)) {
2294 r
= link_handle_bound_to_list(link
);
2297 } else if (!(link
->flags
& IFF_UP
)) {
2300 link_enter_failed(link
);
2305 if (link
->network
->bridge
) {
2306 r
= link_set_bridge(link
);
2308 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2311 if (link
->network
->bond
) {
2312 r
= link_bond_set(link
);
2314 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2317 if (link
->network
->use_br_vlan
&&
2318 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2319 r
= link_set_bridge_vlan(link
);
2321 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2324 /* Skip setting up addresses until it gets carrier,
2325 or it would try to set addresses twice,
2326 which is bad for non-idempotent steps. */
2327 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2330 return link_enter_set_addresses(link
);
2333 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2334 Link
*link
= userdata
;
2338 assert(link
->network
);
2342 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2345 r
= sd_netlink_message_get_errno(m
);
2346 if (r
< 0 && r
!= -EEXIST
) {
2347 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2348 link_enter_failed(link
);
2351 log_link_debug(link
, "Joined netdev");
2353 if (link
->enslaving
<= 0)
2359 static int link_enter_join_netdev(Link
*link
) {
2365 assert(link
->network
);
2366 assert(link
->state
== LINK_STATE_PENDING
);
2368 link_set_state(link
, LINK_STATE_ENSLAVING
);
2372 if (!link
->network
->bridge
&&
2373 !link
->network
->bond
&&
2374 !link
->network
->vrf
&&
2375 hashmap_isempty(link
->network
->stacked_netdevs
))
2376 return link_joined(link
);
2378 if (link
->network
->bond
) {
2379 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2380 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2381 return link_joined(link
);
2383 log_struct(LOG_DEBUG
,
2384 LOG_LINK_INTERFACE(link
),
2385 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2386 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2388 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2390 log_struct_errno(LOG_WARNING
, r
,
2391 LOG_LINK_INTERFACE(link
),
2392 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2393 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2394 link_enter_failed(link
);
2401 if (link
->network
->bridge
) {
2402 log_struct(LOG_DEBUG
,
2403 LOG_LINK_INTERFACE(link
),
2404 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2405 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2407 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2409 log_struct_errno(LOG_WARNING
, r
,
2410 LOG_LINK_INTERFACE(link
),
2411 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2412 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2413 link_enter_failed(link
);
2420 if (link
->network
->vrf
) {
2421 log_struct(LOG_DEBUG
,
2422 LOG_LINK_INTERFACE(link
),
2423 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2424 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2426 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2428 log_struct_errno(LOG_WARNING
, r
,
2429 LOG_LINK_INTERFACE(link
),
2430 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2431 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2432 link_enter_failed(link
);
2439 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2441 if (netdev
->ifindex
> 0) {
2446 log_struct(LOG_DEBUG
,
2447 LOG_LINK_INTERFACE(link
),
2448 LOG_NETDEV_INTERFACE(netdev
),
2449 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2451 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2453 log_struct_errno(LOG_WARNING
, r
,
2454 LOG_LINK_INTERFACE(link
),
2455 LOG_NETDEV_INTERFACE(netdev
),
2456 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2457 link_enter_failed(link
);
2467 static int link_set_ipv4_forward(Link
*link
) {
2470 if (!link_ipv4_forward_enabled(link
))
2473 /* We propagate the forwarding flag from one interface to the
2474 * global setting one way. This means: as long as at least one
2475 * interface was configured at any time that had IP forwarding
2476 * enabled the setting will stay on for good. We do this
2477 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2478 * somewhat in sync (see below). */
2480 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2482 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2487 static int link_set_ipv6_forward(Link
*link
) {
2490 if (!link_ipv6_forward_enabled(link
))
2493 /* On Linux, the IPv6 stack does not know a per-interface
2494 * packet forwarding setting: either packet forwarding is on
2495 * for all, or off for all. We hence don't bother with a
2496 * per-interface setting, but simply propagate the interface
2497 * flag, if it is set, to the global flag, one-way. Note that
2498 * while IPv4 would allow a per-interface flag, we expose the
2499 * same behaviour there and also propagate the setting from
2500 * one to all, to keep things simple (see above). */
2502 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2504 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2509 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2510 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2511 IPv6PrivacyExtensions s
;
2512 const char *p
= NULL
;
2515 s
= link_ipv6_privacy_extensions(link
);
2519 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2520 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2522 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2524 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2529 static int link_set_ipv6_accept_ra(Link
*link
) {
2530 const char *p
= NULL
;
2533 /* Make this a NOP if IPv6 is not available */
2534 if (!socket_ipv6_is_supported())
2537 if (link
->flags
& IFF_LOOPBACK
)
2543 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2545 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2546 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2548 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2553 static int link_set_ipv6_dad_transmits(Link
*link
) {
2554 char buf
[DECIMAL_STR_MAX(int) + 1];
2555 const char *p
= NULL
;
2558 /* Make this a NOP if IPv6 is not available */
2559 if (!socket_ipv6_is_supported())
2562 if (link
->flags
& IFF_LOOPBACK
)
2568 if (link
->network
->ipv6_dad_transmits
< 0)
2571 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2572 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2574 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2576 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2581 static int link_set_ipv6_hop_limit(Link
*link
) {
2582 char buf
[DECIMAL_STR_MAX(int) + 1];
2583 const char *p
= NULL
;
2586 /* Make this a NOP if IPv6 is not available */
2587 if (!socket_ipv6_is_supported())
2590 if (link
->flags
& IFF_LOOPBACK
)
2596 if (link
->network
->ipv6_hop_limit
< 0)
2599 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2600 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2602 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2604 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2609 static int link_set_ipv6_mtu(Link
*link
) {
2610 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2611 const char *p
= NULL
;
2614 /* Make this a NOP if IPv6 is not available */
2615 if (!socket_ipv6_is_supported())
2618 if (link
->flags
& IFF_LOOPBACK
)
2621 if (link
->network
->ipv6_mtu
== 0)
2624 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2626 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2628 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2630 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2635 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2636 Address
*net_address
;
2644 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2645 if (address_equal(net_address
, address
))
2651 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2660 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2661 if (route_equal(net_route
, route
))
2667 static int link_drop_foreign_config(Link
*link
) {
2673 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2674 /* we consider IPv6LL addresses to be managed by the kernel */
2675 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2678 if (link_is_static_address_configured(link
, address
)) {
2679 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2681 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2683 r
= address_remove(address
, link
, link_address_remove_handler
);
2689 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2690 /* do not touch routes managed by the kernel */
2691 if (route
->protocol
== RTPROT_KERNEL
)
2694 if (link_is_static_route_configured(link
, route
)) {
2695 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2699 r
= route_remove(route
, link
, link_route_remove_handler
);
2708 static int link_drop_config(Link
*link
) {
2709 Address
*address
, *pool_address
;
2714 SET_FOREACH(address
, link
->addresses
, i
) {
2715 /* we consider IPv6LL addresses to be managed by the kernel */
2716 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2719 r
= address_remove(address
, link
, link_address_remove_handler
);
2723 /* If this address came from an address pool, clean up the pool */
2724 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2725 if (address_equal(address
, pool_address
)) {
2726 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2727 address_free(pool_address
);
2733 SET_FOREACH(route
, link
->routes
, i
) {
2734 /* do not touch routes managed by the kernel */
2735 if (route
->protocol
== RTPROT_KERNEL
)
2738 r
= route_remove(route
, link
, link_route_remove_handler
);
2748 static int link_update_lldp(Link
*link
) {
2756 if (link
->flags
& IFF_UP
) {
2757 r
= sd_lldp_start(link
->lldp
);
2759 log_link_debug(link
, "Started LLDP.");
2761 r
= sd_lldp_stop(link
->lldp
);
2763 log_link_debug(link
, "Stopped LLDP.");
2769 static int link_configure_can(Link
*link
) {
2772 if (streq_ptr(link
->kind
, "can")) {
2773 /* The CAN interface must be down to configure bitrate, etc... */
2774 if ((link
->flags
& IFF_UP
)) {
2775 r
= link_down(link
);
2777 link_enter_failed(link
);
2784 return link_set_can(link
);
2787 if (!(link
->flags
& IFF_UP
)) {
2788 r
= link_up_can(link
);
2790 link_enter_failed(link
);
2798 static int link_configure(Link
*link
) {
2802 assert(link
->network
);
2803 assert(link
->state
== LINK_STATE_PENDING
);
2805 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2806 return link_configure_can(link
);
2808 /* Drop foreign config, but ignore loopback or critical devices.
2809 * We do not want to remove loopback address or addresses used for root NFS. */
2810 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2811 r
= link_drop_foreign_config(link
);
2816 r
= link_set_proxy_arp(link
);
2820 r
= ipv6_proxy_ndp_addresses_configure(link
);
2824 r
= link_set_ipv4_forward(link
);
2828 r
= link_set_ipv6_forward(link
);
2832 r
= link_set_ipv6_privacy_extensions(link
);
2836 r
= link_set_ipv6_accept_ra(link
);
2840 r
= link_set_ipv6_dad_transmits(link
);
2844 r
= link_set_ipv6_hop_limit(link
);
2848 r
= link_set_flags(link
);
2852 r
= link_set_ipv6_mtu(link
);
2856 if (link_ipv4ll_enabled(link
)) {
2857 r
= ipv4ll_configure(link
);
2862 if (link_dhcp4_enabled(link
)) {
2863 r
= dhcp4_set_promote_secondaries(link
);
2867 r
= dhcp4_configure(link
);
2872 if (link_dhcp4_server_enabled(link
)) {
2873 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2877 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2882 if (link_dhcp6_enabled(link
) ||
2883 link_ipv6_accept_ra_enabled(link
)) {
2884 r
= dhcp6_configure(link
);
2889 if (link_ipv6_accept_ra_enabled(link
)) {
2890 r
= ndisc_configure(link
);
2895 if (link_radv_enabled(link
)) {
2896 r
= radv_configure(link
);
2901 if (link_lldp_rx_enabled(link
)) {
2902 r
= sd_lldp_new(&link
->lldp
);
2906 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2910 r
= sd_lldp_match_capabilities(link
->lldp
,
2911 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2912 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2913 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2917 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2921 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2925 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2929 r
= link_update_lldp(link
);
2934 if (link
->network
->mtu
> 0) {
2935 r
= link_set_mtu(link
, link
->network
->mtu
);
2940 return link_configure_after_setting_mtu(link
);
2943 static int link_configure_after_setting_mtu(Link
*link
) {
2947 assert(link
->network
);
2948 assert(link
->state
== LINK_STATE_PENDING
);
2950 if (link
->setting_mtu
)
2953 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2954 r
= link_acquire_conf(link
);
2959 return link_enter_join_netdev(link
);
2962 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2965 if (duid
->raw_data_len
> 0)
2968 if (duid
->type
!= DUID_TYPE_UUID
)
2971 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2972 duid
->raw_data_len
= sizeof(sd_id128_t
);
2977 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2978 Manager
*manager
= userdata
;
2979 const sd_bus_error
*e
;
2989 e
= sd_bus_message_get_error(m
);
2991 log_error_errno(sd_bus_error_get_errno(e
),
2992 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2997 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3001 if (sz
!= sizeof(sd_id128_t
)) {
3002 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3006 memcpy(&manager
->product_uuid
, a
, sz
);
3007 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3008 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3010 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3013 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3014 r
= link_configure(link
);
3016 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3019 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3021 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3022 * even if the method fails. */
3023 manager
->has_product_uuid
= true;
3028 static bool link_requires_uuid(Link
*link
) {
3032 assert(link
->manager
);
3033 assert(link
->network
);
3035 duid
= link_get_duid(link
);
3036 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3039 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3042 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3048 static int link_configure_duid(Link
*link
) {
3054 assert(link
->manager
);
3055 assert(link
->network
);
3058 duid
= link_get_duid(link
);
3060 if (!link_requires_uuid(link
))
3063 if (m
->has_product_uuid
) {
3064 (void) duid_set_uuid(duid
, m
->product_uuid
);
3068 if (!m
->links_requesting_uuid
) {
3069 r
= manager_request_product_uuid(m
, link
);
3074 log_link_warning_errno(link
, r
,
3075 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3079 r
= set_put(m
->links_requesting_uuid
, link
);
3083 r
= set_put(m
->duids_requesting_uuid
, duid
);
3091 static int link_initialized_and_synced(Link
*link
) {
3096 assert(link
->ifname
);
3097 assert(link
->manager
);
3099 if (link
->state
!= LINK_STATE_PENDING
)
3102 log_link_debug(link
, "Link state is up-to-date");
3104 r
= link_new_bound_by_list(link
);
3108 r
= link_handle_bound_by_list(link
);
3112 if (!link
->network
) {
3113 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3114 &link
->mac
, &network
);
3116 link_enter_unmanaged(link
);
3118 } else if (r
== 0 && network
->unmanaged
) {
3119 link_enter_unmanaged(link
);
3124 if (link
->flags
& IFF_LOOPBACK
) {
3125 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3126 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3128 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3129 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3131 if (network
->dhcp_server
)
3132 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3135 r
= network_apply(network
, link
);
3140 r
= link_new_bound_to_list(link
);
3144 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3145 * link_configure() is called later asynchronously. */
3146 r
= link_configure_duid(link
);
3150 r
= link_configure(link
);
3157 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
3158 (void) link_initialized_and_synced(userdata
);
3162 int link_initialized(Link
*link
, sd_device
*device
) {
3163 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3167 assert(link
->manager
);
3168 assert(link
->manager
->rtnl
);
3171 if (link
->state
!= LINK_STATE_PENDING
)
3174 if (link
->sd_device
)
3177 log_link_debug(link
, "udev initialized link");
3179 link
->sd_device
= sd_device_ref(device
);
3181 /* udev has initialized the link, but we don't know if we have yet
3182 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3183 * when it returns we know that the pending NEWLINKs have already been
3184 * processed and that we are up-to-date */
3186 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3191 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3192 link_netlink_destroy_callback
, link
, 0, __func__
);
3201 static int link_load(Link
*link
) {
3202 _cleanup_free_
char *network_file
= NULL
,
3205 *dhcp4_address
= NULL
,
3206 *ipv4ll_address
= NULL
;
3207 union in_addr_union address
;
3208 union in_addr_union route_dst
;
3214 r
= parse_env_file(NULL
, link
->state_file
,
3215 "NETWORK_FILE", &network_file
,
3216 "ADDRESSES", &addresses
,
3218 "DHCP4_ADDRESS", &dhcp4_address
,
3219 "IPV4LL_ADDRESS", &ipv4ll_address
);
3220 if (r
< 0 && r
!= -ENOENT
)
3221 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3228 suffix
= strrchr(network_file
, '.');
3230 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3231 goto network_file_fail
;
3235 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3237 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3238 goto network_file_fail
;
3241 r
= network_apply(network
, link
);
3243 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3252 _cleanup_free_
char *address_str
= NULL
;
3253 char *prefixlen_str
;
3255 unsigned char prefixlen
;
3257 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3259 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3265 prefixlen_str
= strchr(address_str
, '/');
3266 if (!prefixlen_str
) {
3267 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3271 *prefixlen_str
++ = '\0';
3273 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3275 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3279 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3281 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3285 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3287 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3296 _cleanup_free_
char *route_str
= NULL
;
3297 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3299 char *prefixlen_str
;
3301 unsigned char prefixlen
, tos
, table
;
3304 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3306 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3312 prefixlen_str
= strchr(route_str
, '/');
3313 if (!prefixlen_str
) {
3314 log_link_debug(link
, "Failed to parse route %s", route_str
);
3318 *prefixlen_str
++ = '\0';
3320 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3322 log_link_debug(link
,
3323 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3328 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3330 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3334 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3336 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3338 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3339 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3340 0, route_expire_handler
, route
);
3342 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3345 route
->lifetime
= lifetime
;
3346 sd_event_source_unref(route
->expire
);
3347 route
->expire
= TAKE_PTR(expire
);
3351 if (dhcp4_address
) {
3352 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3354 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3355 goto dhcp4_address_fail
;
3358 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3360 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3362 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3364 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3369 if (ipv4ll_address
) {
3370 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3372 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3373 goto ipv4ll_address_fail
;
3376 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3378 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3380 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3382 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3385 ipv4ll_address_fail
:
3390 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3391 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3392 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3401 r
= link_new(m
, message
, ret
);
3407 log_link_debug(link
, "Link %d added", link
->ifindex
);
3409 r
= link_load(link
);
3413 if (detect_container() <= 0) {
3414 /* not in a container, udev will be around */
3415 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3416 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3418 log_link_warning_errno(link
, r
, "Could not find device: %m");
3422 r
= sd_device_get_is_initialized(device
);
3424 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3429 log_link_debug(link
, "link pending udev initialization...");
3433 r
= link_initialized(link
, device
);
3437 r
= link_initialized_and_synced(link
);
3444 link_enter_failed(link
);
3448 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3453 log_link_info(link
, "Gained IPv6LL");
3455 link
->ipv6ll_address
= *address
;
3456 link_check_ready(link
);
3458 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3459 r
= link_acquire_ipv6_conf(link
);
3461 link_enter_failed(link
);
3469 static int link_carrier_gained(Link
*link
) {
3474 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3475 r
= link_acquire_conf(link
);
3477 link_enter_failed(link
);
3481 r
= link_enter_set_addresses(link
);
3486 r
= link_handle_bound_by_list(link
);
3493 static int link_carrier_lost(Link
*link
) {
3498 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3499 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3500 * configuration and stop the clients as well. */
3501 if (link
->setting_mtu
)
3504 r
= link_stop_clients(link
);
3506 link_enter_failed(link
);
3510 if (link_dhcp4_server_enabled(link
))
3511 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3513 r
= link_drop_config(link
);
3517 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3518 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3519 r
= link_drop_foreign_config(link
);
3524 r
= link_handle_bound_by_list(link
);
3531 int link_carrier_reset(Link
*link
) {
3536 if (link_has_carrier(link
)) {
3537 r
= link_carrier_lost(link
);
3541 r
= link_carrier_gained(link
);
3545 log_link_info(link
, "Reset carrier");
3551 int link_update(Link
*link
, sd_netlink_message
*m
) {
3552 struct ether_addr mac
;
3555 bool had_carrier
, carrier_gained
, carrier_lost
;
3559 assert(link
->ifname
);
3562 if (link
->state
== LINK_STATE_LINGER
) {
3563 log_link_info(link
, "Link readded");
3564 link_set_state(link
, LINK_STATE_ENSLAVING
);
3566 r
= link_new_carrier_maps(link
);
3571 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3572 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3573 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3575 if (link
->state
== LINK_STATE_PENDING
) {
3576 r
= free_and_strdup(&link
->ifname
, ifname
);
3580 Manager
*manager
= link
->manager
;
3583 r
= link_add(manager
, m
, &link
);
3589 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3590 if (r
>= 0 && mtu
> 0) {
3592 if (link
->original_mtu
== 0) {
3593 link
->original_mtu
= mtu
;
3594 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3597 if (link
->dhcp_client
) {
3598 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3601 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3605 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3607 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3611 /* The kernel may broadcast NEWLINK messages without the MAC address
3612 set, simply ignore them. */
3613 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3615 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3618 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3621 log_link_debug(link
, "MAC address: "
3622 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3623 mac
.ether_addr_octet
[0],
3624 mac
.ether_addr_octet
[1],
3625 mac
.ether_addr_octet
[2],
3626 mac
.ether_addr_octet
[3],
3627 mac
.ether_addr_octet
[4],
3628 mac
.ether_addr_octet
[5]);
3631 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3633 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3636 if (link
->dhcp_client
) {
3637 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3638 (const uint8_t *) &link
->mac
,
3642 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3644 r
= dhcp4_set_client_identifier(link
);
3649 if (link
->dhcp6_client
) {
3650 const DUID
* duid
= link_get_duid(link
);
3652 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3653 (const uint8_t *) &link
->mac
,
3657 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3659 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3660 link
->network
->iaid
);
3662 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3664 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3666 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3667 duid
->raw_data_len
);
3669 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3673 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3675 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3679 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3681 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3686 had_carrier
= link_has_carrier(link
);
3688 r
= link_update_flags(link
, m
);
3692 r
= link_update_lldp(link
);
3696 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3697 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3699 if (carrier_gained
) {
3700 log_link_info(link
, "Gained carrier");
3702 r
= link_carrier_gained(link
);
3705 } else if (carrier_lost
) {
3706 log_link_info(link
, "Lost carrier");
3708 r
= link_carrier_lost(link
);
3716 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3724 if (hashmap_isempty(h
))
3728 HASHMAP_FOREACH(link
, h
, i
) {
3732 fprintf(f
, "%i", link
->ifindex
);
3739 int link_save(Link
*link
) {
3740 _cleanup_free_
char *temp_path
= NULL
;
3741 _cleanup_fclose_
FILE *f
= NULL
;
3742 const char *admin_state
, *oper_state
;
3749 assert(link
->state_file
);
3750 assert(link
->lease_file
);
3751 assert(link
->manager
);
3753 if (link
->state
== LINK_STATE_LINGER
) {
3754 unlink(link
->state_file
);
3758 link_lldp_save(link
);
3760 admin_state
= link_state_to_string(link
->state
);
3761 assert(admin_state
);
3763 oper_state
= link_operstate_to_string(link
->operstate
);
3766 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3770 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3771 (void) fchmod(fileno(f
), 0644);
3774 "# This is private data. Do not parse.\n"
3777 admin_state
, oper_state
);
3779 if (link
->network
) {
3781 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3782 const char *dhcp_domainname
= NULL
;
3783 char **dhcp6_domains
= NULL
;
3784 char **dhcp_domains
= NULL
;
3787 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3788 yes_no(link
->network
->required_for_online
));
3790 if (link
->dhcp6_client
) {
3791 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3792 if (r
< 0 && r
!= -ENOMSG
)
3793 log_link_debug(link
, "No DHCPv6 lease");
3796 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3801 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3802 _cleanup_free_
char *b
= NULL
;
3804 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3805 &link
->network
->dns
[j
].address
, &b
);
3807 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3817 if (link
->network
->dhcp_use_dns
&&
3819 const struct in_addr
*addresses
;
3821 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3825 serialize_in_addrs(f
, addresses
, r
);
3830 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3831 struct in6_addr
*in6_addrs
;
3833 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3837 serialize_in6_addrs(f
, in6_addrs
, r
);
3842 /* Make sure to flush out old entries before we use the NDISC data */
3845 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3848 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3852 serialize_in6_addrs(f
, &dd
->address
, 1);
3861 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3863 if (link
->network
->dhcp_use_ntp
&&
3865 const struct in_addr
*addresses
;
3867 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3871 serialize_in_addrs(f
, addresses
, r
);
3876 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3877 struct in6_addr
*in6_addrs
;
3880 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3885 serialize_in6_addrs(f
, in6_addrs
, r
);
3889 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3891 fputstrv(f
, hosts
, NULL
, &space
);
3896 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3897 if (link
->dhcp_lease
) {
3898 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3899 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3902 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3905 fputs("DOMAINS=", f
);
3907 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3909 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3912 if (dhcp_domainname
)
3913 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3915 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3917 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3919 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3920 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3925 fputs("ROUTE_DOMAINS=", f
);
3927 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3929 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3932 if (dhcp_domainname
)
3933 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3935 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3937 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3939 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3940 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3945 fprintf(f
, "LLMNR=%s\n",
3946 resolve_support_to_string(link
->network
->llmnr
));
3947 fprintf(f
, "MDNS=%s\n",
3948 resolve_support_to_string(link
->network
->mdns
));
3950 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3951 fprintf(f
, "DNS_OVER_TLS=%s\n",
3952 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3954 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3955 fprintf(f
, "DNSSEC=%s\n",
3956 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3958 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3961 fputs("DNSSEC_NTA=", f
);
3963 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3964 fputs_with_space(f
, n
, NULL
, &space
);
3968 fputs("ADDRESSES=", f
);
3970 SET_FOREACH(a
, link
->addresses
, i
) {
3971 _cleanup_free_
char *address_str
= NULL
;
3973 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3977 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3982 fputs("ROUTES=", f
);
3984 SET_FOREACH(route
, link
->routes
, i
) {
3985 _cleanup_free_
char *route_str
= NULL
;
3987 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3991 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3992 space
? " " : "", route_str
,
3993 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4000 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4001 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4003 if (link
->dhcp_lease
) {
4004 struct in_addr address
;
4005 const char *tz
= NULL
;
4007 assert(link
->network
);
4009 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4011 fprintf(f
, "TIMEZONE=%s\n", tz
);
4013 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4015 fputs("DHCP4_ADDRESS=", f
);
4016 serialize_in_addrs(f
, &address
, 1);
4020 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4028 unlink(link
->lease_file
);
4031 struct in_addr address
;
4033 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4035 fputs("IPV4LL_ADDRESS=", f
);
4036 serialize_in_addrs(f
, &address
, 1);
4041 r
= fflush_and_check(f
);
4045 if (rename(temp_path
, link
->state_file
) < 0) {
4053 (void) unlink(link
->state_file
);
4055 (void) unlink(temp_path
);
4057 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4060 /* The serialized state in /run is no longer up-to-date. */
4061 void link_dirty(Link
*link
) {
4066 /* mark manager dirty as link is dirty */
4067 manager_dirty(link
->manager
);
4069 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4071 /* allocation errors are ignored */
4074 r
= set_put(link
->manager
->dirty_links
, link
);
4076 /* don't take another ref if the link was already dirty */
4082 /* The serialized state in /run is up-to-date */
4083 void link_clean(Link
*link
) {
4085 assert(link
->manager
);
4087 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4090 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4091 [LINK_STATE_PENDING
] = "pending",
4092 [LINK_STATE_ENSLAVING
] = "configuring",
4093 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4094 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4095 [LINK_STATE_CONFIGURED
] = "configured",
4096 [LINK_STATE_UNMANAGED
] = "unmanaged",
4097 [LINK_STATE_FAILED
] = "failed",
4098 [LINK_STATE_LINGER
] = "linger",
4101 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4103 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4104 [LINK_OPERSTATE_OFF
] = "off",
4105 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4106 [LINK_OPERSTATE_DORMANT
] = "dormant",
4107 [LINK_OPERSTATE_CARRIER
] = "carrier",
4108 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4109 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4112 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);