1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
5 #include <linux/can/netlink.h>
9 #include "alloc-util.h"
11 #include "dhcp-identifier.h"
12 #include "dhcp-lease-internal.h"
16 #include "missing_network.h"
17 #include "netdev/vrf.h"
18 #include "netlink-util.h"
19 #include "network-internal.h"
20 #include "networkd-ipv6-proxy-ndp.h"
21 #include "networkd-lldp-tx.h"
22 #include "networkd-manager.h"
23 #include "networkd-ndisc.h"
24 #include "networkd-neighbor.h"
25 #include "networkd-radv.h"
26 #include "networkd-routing-policy-rule.h"
28 #include "socket-util.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
32 #include "sysctl-util.h"
33 #include "tmpfile-util.h"
37 uint32_t link_get_vrf_table(Link
*link
) {
38 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
41 uint32_t link_get_dhcp_route_table(Link
*link
) {
42 /* When the interface is part of an VRF use the VRFs routing table, unless
43 * another table is explicitly specified. */
44 if (link
->network
->dhcp_route_table_set
)
45 return link
->network
->dhcp_route_table
;
46 return link_get_vrf_table(link
);
49 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
50 if (link
->network
->ipv6_accept_ra_route_table_set
)
51 return link
->network
->ipv6_accept_ra_route_table
;
52 return link_get_vrf_table(link
);
55 DUID
* link_get_duid(Link
*link
) {
56 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
57 return &link
->network
->duid
;
59 return &link
->manager
->duid
;
62 static bool link_dhcp6_enabled(Link
*link
) {
65 if (!socket_ipv6_is_supported())
68 if (link
->flags
& IFF_LOOPBACK
)
74 if (link
->network
->bond
)
77 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
80 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
83 static bool link_dhcp4_enabled(Link
*link
) {
86 if (link
->flags
& IFF_LOOPBACK
)
92 if (link
->network
->bond
)
95 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
98 static bool link_dhcp4_server_enabled(Link
*link
) {
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (link
->network
->bond
)
110 return link
->network
->dhcp_server
;
113 static bool link_ipv4ll_enabled(Link
*link
) {
116 if (link
->flags
& IFF_LOOPBACK
)
122 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
125 if (link
->network
->bond
)
128 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
131 static bool link_ipv6ll_enabled(Link
*link
) {
134 if (!socket_ipv6_is_supported())
137 if (link
->flags
& IFF_LOOPBACK
)
143 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
146 if (link
->network
->bond
)
149 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
152 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
155 static bool link_ipv6_enabled(Link
*link
) {
158 if (!socket_ipv6_is_supported())
161 if (link
->network
->bond
)
164 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
167 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
168 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
171 static bool link_radv_enabled(Link
*link
) {
174 if (!link_ipv6ll_enabled(link
))
177 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
180 static bool link_lldp_rx_enabled(Link
*link
) {
183 if (link
->flags
& IFF_LOOPBACK
)
186 if (link
->iftype
!= ARPHRD_ETHER
)
192 /* LLDP should be handled on bridge slaves as those have a direct
193 * connection to their peers not on the bridge master. Linux doesn't
194 * even (by default) forward lldp packets to the bridge master.*/
195 if (streq_ptr("bridge", link
->kind
))
198 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
201 static bool link_lldp_emit_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
207 if (link
->iftype
!= ARPHRD_ETHER
)
213 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
216 static bool link_ipv4_forward_enabled(Link
*link
) {
219 if (link
->flags
& IFF_LOOPBACK
)
225 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
228 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
231 static bool link_ipv6_forward_enabled(Link
*link
) {
234 if (!socket_ipv6_is_supported())
237 if (link
->flags
& IFF_LOOPBACK
)
243 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
246 if (manager_sysctl_ipv6_enabled(link
->manager
) == 0)
249 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
252 static bool link_proxy_arp_enabled(Link
*link
) {
255 if (link
->flags
& IFF_LOOPBACK
)
261 if (link
->network
->proxy_arp
< 0)
267 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
270 if (!socket_ipv6_is_supported())
273 if (link
->flags
& IFF_LOOPBACK
)
279 if (!link_ipv6ll_enabled(link
))
282 /* If unset use system default (enabled if local forwarding is disabled.
283 * disabled if local forwarding is enabled).
284 * If set, ignore or enforce RA independent of local forwarding state.
286 if (link
->network
->ipv6_accept_ra
< 0)
287 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
288 return !link_ipv6_forward_enabled(link
);
289 else if (link
->network
->ipv6_accept_ra
> 0)
290 /* accept RA even if ip_forward is enabled */
297 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
300 if (!socket_ipv6_is_supported())
301 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
303 if (link
->flags
& IFF_LOOPBACK
)
304 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
307 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
309 return link
->network
->ipv6_privacy_extensions
;
312 static int link_enable_ipv6(Link
*link
) {
316 if (link
->flags
& IFF_LOOPBACK
)
319 disabled
= !link_ipv6_enabled(link
);
321 r
= sysctl_write_ip_property_boolean(AF_INET6
, link
->ifname
, "disable_ipv6", disabled
);
323 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
324 enable_disable(!disabled
), link
->ifname
);
326 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
331 static bool link_is_enslaved(Link
*link
) {
332 if (link
->flags
& IFF_SLAVE
)
333 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
336 if (!link
->enslaved_raw
)
342 if (link
->network
->bridge
)
343 /* TODO: support the case when link is not managed by networkd. */
349 static void link_update_master_operstate(Link
*link
, NetDev
*netdev
) {
355 if (link_get(link
->manager
, netdev
->ifindex
, &master
) < 0)
358 link_update_operstate(master
, true);
361 void link_update_operstate(Link
*link
, bool also_update_master
) {
362 LinkOperationalState operstate
;
367 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
368 operstate
= LINK_OPERSTATE_DORMANT
;
369 else if (link_has_carrier(link
)) {
371 uint8_t scope
= RT_SCOPE_NOWHERE
;
373 /* if we have carrier, check what addresses we have */
374 SET_FOREACH(address
, link
->addresses
, i
) {
375 if (!address_is_ready(address
))
378 if (address
->scope
< scope
)
379 scope
= address
->scope
;
382 /* for operstate we also take foreign addresses into account */
383 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
384 if (!address_is_ready(address
))
387 if (address
->scope
< scope
)
388 scope
= address
->scope
;
391 if (scope
< RT_SCOPE_SITE
)
392 /* universally accessible addresses found */
393 operstate
= LINK_OPERSTATE_ROUTABLE
;
394 else if (scope
< RT_SCOPE_HOST
)
395 /* only link or site local addresses found */
396 operstate
= LINK_OPERSTATE_DEGRADED
;
398 /* no useful addresses found */
399 operstate
= LINK_OPERSTATE_CARRIER
;
400 } else if (link
->flags
& IFF_UP
)
401 operstate
= LINK_OPERSTATE_NO_CARRIER
;
403 operstate
= LINK_OPERSTATE_OFF
;
405 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
406 link_is_enslaved(link
))
407 operstate
= LINK_OPERSTATE_ENSLAVED
;
409 if (IN_SET(operstate
, LINK_OPERSTATE_CARRIER
, LINK_OPERSTATE_ENSLAVED
, LINK_OPERSTATE_ROUTABLE
)) {
412 HASHMAP_FOREACH(slave
, link
->slaves
, i
) {
413 link_update_operstate(slave
, false);
415 if (IN_SET(slave
->operstate
,
416 LINK_OPERSTATE_OFF
, LINK_OPERSTATE_NO_CARRIER
, LINK_OPERSTATE_DORMANT
))
417 operstate
= LINK_OPERSTATE_DEGRADED
;
421 if (link
->operstate
!= operstate
) {
422 link
->operstate
= operstate
;
423 link_send_changed(link
, "OperationalState", NULL
);
427 if (also_update_master
&& link
->network
) {
428 link_update_master_operstate(link
, link
->network
->bond
);
429 link_update_master_operstate(link
, link
->network
->bridge
);
433 #define FLAG_STRING(string, flag, old, new) \
434 (((old ^ new) & flag) \
435 ? ((old & flag) ? (" -" string) : (" +" string)) \
438 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
439 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
445 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
447 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
449 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
451 /* if we got a message without operstate, take it to mean
452 the state was unchanged */
453 operstate
= link
->kernel_operstate
;
455 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
458 if (link
->flags
!= flags
) {
459 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",
460 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
461 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
462 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
463 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
464 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
465 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
466 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
467 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
468 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
469 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
470 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
471 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
472 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
473 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
474 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
475 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
476 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
477 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
478 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
480 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
481 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
482 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
483 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
484 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
485 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
486 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
487 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
489 /* link flags are currently at most 18 bits, let's align to
491 if (unknown_flags_added
)
493 "Unknown link flags gained: %#.5x (ignoring)",
494 unknown_flags_added
);
496 if (unknown_flags_removed
)
498 "Unknown link flags lost: %#.5x (ignoring)",
499 unknown_flags_removed
);
503 link
->kernel_operstate
= operstate
;
505 link_update_operstate(link
, true);
510 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
512 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
513 _cleanup_(link_unrefp
) Link
*link
= NULL
;
515 const char *ifname
, *kind
= NULL
;
517 unsigned short iftype
;
523 /* check for link kind */
524 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
526 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
527 r
= sd_netlink_message_exit_container(message
);
532 r
= sd_netlink_message_get_type(message
, &type
);
535 else if (type
!= RTM_NEWLINK
)
538 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
541 else if (ifindex
<= 0)
544 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
548 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
559 .state
= LINK_STATE_PENDING
,
560 .rtnl_extended_attrs
= true,
565 link
->ifname
= strdup(ifname
);
570 link
->kind
= strdup(kind
);
575 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
577 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
579 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
581 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
583 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
586 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
589 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
592 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
596 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
600 r
= link_update_flags(link
, message
);
604 *ret
= TAKE_PTR(link
);
609 static void link_detach_from_manager(Link
*link
) {
610 if (!link
|| !link
->manager
)
613 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
614 set_remove(link
->manager
->links_requesting_uuid
, link
);
618 static Link
*link_free(Link
*link
) {
619 Link
*carrier
, *master
;
626 while ((route
= set_first(link
->routes
)))
629 while ((route
= set_first(link
->routes_foreign
)))
632 link
->routes
= set_free(link
->routes
);
633 link
->routes_foreign
= set_free(link
->routes_foreign
);
635 while ((address
= set_first(link
->addresses
)))
636 address_free(address
);
638 while ((address
= set_first(link
->addresses_foreign
)))
639 address_free(address
);
641 link
->addresses
= set_free(link
->addresses
);
642 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
644 while ((address
= link
->pool_addresses
)) {
645 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
646 address_free(address
);
649 sd_dhcp_server_unref(link
->dhcp_server
);
650 sd_dhcp_client_unref(link
->dhcp_client
);
651 sd_dhcp_lease_unref(link
->dhcp_lease
);
653 link_lldp_emit_stop(link
);
655 free(link
->lease_file
);
657 sd_lldp_unref(link
->lldp
);
658 free(link
->lldp_file
);
662 sd_ipv4ll_unref(link
->ipv4ll
);
663 sd_dhcp6_client_unref(link
->dhcp6_client
);
664 sd_ndisc_unref(link
->ndisc
);
665 sd_radv_unref(link
->radv
);
667 link_detach_from_manager(link
);
673 (void) unlink(link
->state_file
);
674 free(link
->state_file
);
676 sd_device_unref(link
->sd_device
);
678 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
679 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
680 hashmap_free(link
->bound_to_links
);
682 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
683 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
684 hashmap_free(link
->bound_by_links
);
686 hashmap_free(link
->slaves
);
689 if (link
->network
->bond
&&
690 link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
) >= 0)
691 (void) hashmap_remove(master
->slaves
, INT_TO_PTR(link
->ifindex
));
693 if (link
->network
->bridge
&&
694 link_get(link
->manager
, link
->network
->bridge
->ifindex
, &master
) >= 0)
695 (void) hashmap_remove(master
->slaves
, INT_TO_PTR(link
->ifindex
));
701 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
703 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
710 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
719 static void link_set_state(Link
*link
, LinkState state
) {
722 if (link
->state
== state
)
727 link_send_changed(link
, "AdministrativeState", NULL
);
730 static void link_enter_unmanaged(Link
*link
) {
733 log_link_debug(link
, "Unmanaged");
735 link_set_state(link
, LINK_STATE_UNMANAGED
);
740 static int link_stop_clients(Link
*link
) {
744 assert(link
->manager
);
745 assert(link
->manager
->event
);
747 if (link
->dhcp_client
) {
748 k
= sd_dhcp_client_stop(link
->dhcp_client
);
750 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
754 k
= sd_ipv4ll_stop(link
->ipv4ll
);
756 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
759 if (link
->dhcp6_client
) {
760 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
762 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
766 k
= sd_ndisc_stop(link
->ndisc
);
768 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
772 k
= sd_radv_stop(link
->radv
);
774 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
777 link_lldp_emit_stop(link
);
781 void link_enter_failed(Link
*link
) {
784 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
787 log_link_warning(link
, "Failed");
789 link_set_state(link
, LINK_STATE_FAILED
);
791 link_stop_clients(link
);
796 static Address
* link_find_dhcp_server_address(Link
*link
) {
800 assert(link
->network
);
802 /* The first statically configured address if there is any */
803 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
805 if (address
->family
!= AF_INET
)
808 if (in_addr_is_null(address
->family
, &address
->in_addr
))
814 /* If that didn't work, find a suitable address we got from the pool */
815 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
816 if (address
->family
!= AF_INET
)
825 static void link_enter_configured(Link
*link
) {
827 assert(link
->network
);
829 if (link
->state
!= LINK_STATE_CONFIGURING
)
832 log_link_info(link
, "Configured");
834 link_set_state(link
, LINK_STATE_CONFIGURED
);
839 static int link_request_set_routing_policy_rule(Link
*link
) {
840 RoutingPolicyRule
*rule
, *rrule
= NULL
;
844 assert(link
->network
);
846 link_set_state(link
, LINK_STATE_CONFIGURING
);
847 link
->routing_policy_rules_configured
= false;
849 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
850 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
851 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
852 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
854 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
858 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
860 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
861 link_enter_failed(link
);
865 link
->routing_policy_rule_messages
++;
868 routing_policy_rule_purge(link
->manager
, link
);
869 if (link
->routing_policy_rule_messages
== 0) {
870 link
->routing_policy_rules_configured
= true;
871 link_check_ready(link
);
873 log_link_debug(link
, "Setting routing policy rules");
878 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
882 assert(link
->route_messages
> 0);
883 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
884 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
886 link
->route_messages
--;
888 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
891 r
= sd_netlink_message_get_errno(m
);
892 if (r
< 0 && r
!= -EEXIST
)
893 log_link_warning_errno(link
, r
, "Could not set route: %m");
895 if (link
->route_messages
== 0) {
896 log_link_debug(link
, "Routes set");
897 link
->static_routes_configured
= true;
898 link_check_ready(link
);
904 static int link_request_set_routes(Link
*link
) {
906 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
907 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
914 assert(link
->network
);
915 assert(link
->addresses_configured
);
916 assert(link
->address_messages
== 0);
917 assert(link
->state
!= _LINK_STATE_INVALID
);
919 link_set_state(link
, LINK_STATE_CONFIGURING
);
920 link
->static_routes_configured
= false;
922 r
= link_request_set_routing_policy_rule(link
);
926 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
927 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
928 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
930 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
933 r
= route_configure(rt
, link
, route_handler
);
935 log_link_warning_errno(link
, r
, "Could not set routes: %m");
936 link_enter_failed(link
);
940 link
->route_messages
++;
943 if (link
->route_messages
== 0) {
944 link
->static_routes_configured
= true;
945 link_check_ready(link
);
947 log_link_debug(link
, "Setting routes");
952 void link_check_ready(Link
*link
) {
958 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
964 if (!link
->addresses_configured
)
967 if (!link
->neighbors_configured
)
970 SET_FOREACH(a
, link
->addresses
, i
)
971 if (!address_is_ready(a
))
974 if (!link
->addresses_ready
) {
975 link
->addresses_ready
= true;
976 link_request_set_routes(link
);
979 if (!link
->static_routes_configured
)
982 if (!link
->routing_policy_rules_configured
)
985 if (link_ipv4ll_enabled(link
))
986 if (!link
->ipv4ll_address
||
990 if (link_ipv6ll_enabled(link
) &&
991 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
994 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
995 !link
->dhcp4_configured
) ||
996 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
997 !link
->dhcp6_configured
) ||
998 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
999 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
1002 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
1005 if (link
->state
!= LINK_STATE_CONFIGURED
)
1006 link_enter_configured(link
);
1011 static int link_request_set_neighbors(Link
*link
) {
1016 assert(link
->network
);
1017 assert(link
->state
!= _LINK_STATE_INVALID
);
1019 link_set_state(link
, LINK_STATE_CONFIGURING
);
1020 link
->neighbors_configured
= false;
1022 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
1023 r
= neighbor_configure(neighbor
, link
, NULL
);
1025 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
1026 link_enter_failed(link
);
1031 if (link
->neighbor_messages
== 0) {
1032 link
->neighbors_configured
= true;
1033 link_check_ready(link
);
1035 log_link_debug(link
, "Setting neighbors");
1040 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1046 assert(link
->ifname
);
1047 assert(link
->address_messages
> 0);
1048 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
1049 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
1051 link
->address_messages
--;
1053 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1056 r
= sd_netlink_message_get_errno(m
);
1057 if (r
< 0 && r
!= -EEXIST
)
1058 log_link_warning_errno(link
, r
, "could not set address: %m");
1060 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1062 if (link
->address_messages
== 0) {
1063 log_link_debug(link
, "Addresses set");
1064 link
->addresses_configured
= true;
1065 link_check_ready(link
);
1071 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1072 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1073 size_t n_addresses
= 0, n_allocated
= 0;
1076 log_debug("Copying DNS server information from %s", link
->ifname
);
1081 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1084 /* Only look for IPv4 addresses */
1085 if (link
->network
->dns
[i
].family
!= AF_INET
)
1088 ia
= link
->network
->dns
[i
].address
.in
;
1090 /* Never propagate obviously borked data */
1091 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1094 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1097 addresses
[n_addresses
++] = ia
;
1100 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1101 const struct in_addr
*da
= NULL
;
1104 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1107 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1110 for (j
= 0; j
< n
; j
++)
1111 if (in4_addr_is_non_local(&da
[j
]))
1112 addresses
[n_addresses
++] = da
[j
];
1116 if (n_addresses
<= 0)
1119 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1122 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1123 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1124 size_t n_addresses
= 0, n_allocated
= 0;
1130 log_debug("Copying NTP server information from %s", link
->ifname
);
1132 STRV_FOREACH(a
, link
->network
->ntp
) {
1135 /* Only look for IPv4 addresses */
1136 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1139 /* Never propagate obviously borked data */
1140 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1143 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1146 addresses
[n_addresses
++] = ia
;
1149 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1150 const struct in_addr
*da
= NULL
;
1153 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1156 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1159 for (j
= 0; j
< n
; j
++)
1160 if (in4_addr_is_non_local(&da
[j
]))
1161 addresses
[n_addresses
++] = da
[j
];
1165 if (n_addresses
<= 0)
1168 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1171 static int link_set_bridge_fdb(Link
*link
) {
1172 FdbEntry
*fdb_entry
;
1175 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1176 r
= fdb_entry_configure(link
, fdb_entry
);
1178 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1184 static int link_request_set_addresses(Link
*link
) {
1185 AddressLabel
*label
;
1190 assert(link
->network
);
1191 assert(link
->state
!= _LINK_STATE_INVALID
);
1193 link_set_state(link
, LINK_STATE_CONFIGURING
);
1195 /* Reset all *_configured flags we are configuring. */
1196 link
->addresses_configured
= false;
1197 link
->addresses_ready
= false;
1198 link
->neighbors_configured
= false;
1199 link
->static_routes_configured
= false;
1200 link
->routing_policy_rules_configured
= false;
1202 r
= link_set_bridge_fdb(link
);
1206 r
= link_request_set_neighbors(link
);
1210 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1213 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1215 r
= address_configure(ad
, link
, address_handler
, update
);
1217 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1218 link_enter_failed(link
);
1222 link
->address_messages
++;
1225 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1226 r
= address_label_configure(label
, link
, NULL
, false);
1228 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1229 link_enter_failed(link
);
1233 link
->address_label_messages
++;
1236 /* now that we can figure out a default address for the dhcp server,
1238 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1240 Link
*uplink
= NULL
;
1241 bool acquired_uplink
= false;
1243 address
= link_find_dhcp_server_address(link
);
1245 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1246 link_enter_failed(link
);
1250 /* use the server address' subnet as the pool */
1251 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1252 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1257 r = sd_dhcp_server_set_router(link->dhcp_server,
1258 &main_address->in_addr.in);
1263 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1264 r
= sd_dhcp_server_set_max_lease_time(
1266 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1271 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1272 r
= sd_dhcp_server_set_default_lease_time(
1274 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1279 if (link
->network
->dhcp_server_emit_dns
) {
1281 if (link
->network
->n_dhcp_server_dns
> 0)
1282 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1284 uplink
= manager_find_uplink(link
->manager
, link
);
1285 acquired_uplink
= true;
1288 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1291 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1294 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1297 if (link
->network
->dhcp_server_emit_ntp
) {
1299 if (link
->network
->n_dhcp_server_ntp
> 0)
1300 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1302 if (!acquired_uplink
)
1303 uplink
= manager_find_uplink(link
->manager
, link
);
1306 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1309 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1313 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1316 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1318 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1320 if (link
->network
->dhcp_server_emit_timezone
) {
1321 _cleanup_free_
char *buffer
= NULL
;
1322 const char *tz
= NULL
;
1324 if (link
->network
->dhcp_server_timezone
)
1325 tz
= link
->network
->dhcp_server_timezone
;
1327 r
= get_timezone(&buffer
);
1329 log_warning_errno(r
, "Failed to determine timezone: %m");
1335 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1340 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1341 r
= sd_dhcp_server_start(link
->dhcp_server
);
1343 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1345 link_enter_failed(link
);
1351 log_link_debug(link
, "Offering DHCPv4 leases");
1354 if (link
->address_messages
== 0) {
1355 link
->addresses_configured
= true;
1356 link_check_ready(link
);
1358 log_link_debug(link
, "Setting addresses");
1363 static int link_set_bridge_vlan(Link
*link
) {
1366 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1368 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1373 static int link_set_proxy_arp(Link
*link
) {
1376 if (!link_proxy_arp_enabled(link
))
1379 r
= sysctl_write_ip_property_boolean(AF_INET
, link
->ifname
, "proxy_arp", link
->network
->proxy_arp
> 0);
1381 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1386 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1391 log_link_debug(link
, "Set link");
1393 r
= sd_netlink_message_get_errno(m
);
1394 if (r
< 0 && r
!= -EEXIST
) {
1395 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1396 link_enter_failed(link
);
1402 static int link_configure_after_setting_mtu(Link
*link
);
1404 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1409 assert(link
->ifname
);
1411 link
->setting_mtu
= false;
1413 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1416 r
= sd_netlink_message_get_errno(m
);
1418 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1422 log_link_debug(link
, "Setting MTU done.");
1424 if (link
->state
== LINK_STATE_PENDING
)
1425 (void) link_configure_after_setting_mtu(link
);
1430 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1431 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1435 assert(link
->manager
);
1436 assert(link
->manager
->rtnl
);
1438 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1441 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1443 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1445 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1447 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1448 * for this interface, then disable IPv6 else enable it. */
1449 (void) link_enable_ipv6(link
);
1451 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1452 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1453 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1455 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1456 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1461 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1463 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1465 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1466 link_netlink_destroy_callback
, link
);
1468 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1471 link
->setting_mtu
= true;
1476 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1481 assert(link
->ifname
);
1483 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1486 r
= sd_netlink_message_get_errno(m
);
1488 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1493 static int link_set_flags(Link
*link
) {
1494 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1495 unsigned ifi_change
= 0;
1496 unsigned ifi_flags
= 0;
1500 assert(link
->manager
);
1501 assert(link
->manager
->rtnl
);
1503 if (link
->flags
& IFF_LOOPBACK
)
1509 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1512 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1514 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1516 if (link
->network
->arp
>= 0) {
1517 ifi_change
|= IFF_NOARP
;
1518 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1521 if (link
->network
->multicast
>= 0) {
1522 ifi_change
|= IFF_MULTICAST
;
1523 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1526 if (link
->network
->allmulticast
>= 0) {
1527 ifi_change
|= IFF_ALLMULTI
;
1528 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1531 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1533 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1535 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1536 link_netlink_destroy_callback
, link
);
1538 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1545 static int link_set_bridge(Link
*link
) {
1546 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1550 assert(link
->network
);
1552 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1554 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1556 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1558 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1560 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1562 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1564 if (link
->network
->use_bpdu
>= 0) {
1565 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1567 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1570 if (link
->network
->hairpin
>= 0) {
1571 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1573 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1576 if (link
->network
->fast_leave
>= 0) {
1577 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1579 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1582 if (link
->network
->allow_port_to_be_root
>= 0) {
1583 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1585 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1589 if (link
->network
->unicast_flood
>= 0) {
1590 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1592 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1595 if (link
->network
->multicast_to_unicast
>= 0) {
1596 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1598 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1601 if (link
->network
->cost
!= 0) {
1602 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1604 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1607 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1608 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1610 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1613 r
= sd_netlink_message_close_container(req
);
1615 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1617 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1618 link_netlink_destroy_callback
, link
);
1620 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1627 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1632 assert(link
->ifname
);
1634 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1637 r
= sd_netlink_message_get_errno(m
);
1639 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1646 static int link_set_bond(Link
*link
) {
1647 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1651 assert(link
->network
);
1653 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1655 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1657 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1659 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1661 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1663 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1665 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1667 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1669 if (link
->network
->active_slave
) {
1670 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1672 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1675 if (link
->network
->primary_slave
) {
1676 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1678 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1681 r
= sd_netlink_message_close_container(req
);
1683 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1685 r
= sd_netlink_message_close_container(req
);
1687 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1689 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1690 link_netlink_destroy_callback
, link
);
1692 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1699 static int link_append_to_master(Link
*link
, NetDev
*netdev
) {
1706 r
= link_get(link
->manager
, netdev
->ifindex
, &master
);
1710 r
= hashmap_ensure_allocated(&master
->slaves
, NULL
);
1714 r
= hashmap_put(master
->slaves
, INT_TO_PTR(link
->ifindex
), link
);
1721 static int link_lldp_save(Link
*link
) {
1722 _cleanup_free_
char *temp_path
= NULL
;
1723 _cleanup_fclose_
FILE *f
= NULL
;
1724 sd_lldp_neighbor
**l
= NULL
;
1728 assert(link
->lldp_file
);
1731 (void) unlink(link
->lldp_file
);
1735 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1739 (void) unlink(link
->lldp_file
);
1745 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1749 fchmod(fileno(f
), 0644);
1751 for (i
= 0; i
< n
; i
++) {
1756 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1761 (void) fwrite(&u
, 1, sizeof(u
), f
);
1762 (void) fwrite(p
, 1, sz
, f
);
1765 r
= fflush_and_check(f
);
1769 if (rename(temp_path
, link
->lldp_file
) < 0) {
1776 (void) unlink(link
->lldp_file
);
1778 (void) unlink(temp_path
);
1780 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1784 for (i
= 0; i
< n
; i
++)
1785 sd_lldp_neighbor_unref(l
[i
]);
1792 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1793 Link
*link
= userdata
;
1798 (void) link_lldp_save(link
);
1800 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1801 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1803 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1805 r
= link_lldp_emit_start(link
);
1807 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1811 static int link_acquire_ipv6_conf(Link
*link
) {
1816 if (link_ipv6_accept_ra_enabled(link
)) {
1817 assert(link
->ndisc
);
1819 log_link_debug(link
, "Discovering IPv6 routers");
1821 r
= sd_ndisc_start(link
->ndisc
);
1822 if (r
< 0 && r
!= -EBUSY
)
1823 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1826 if (link_radv_enabled(link
)) {
1828 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1830 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1832 r
= sd_radv_start(link
->radv
);
1833 if (r
< 0 && r
!= -EBUSY
)
1834 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1837 (void) dhcp6_request_prefix_delegation(link
);
1842 static int link_acquire_ipv4_conf(Link
*link
) {
1846 assert(link
->network
);
1847 assert(link
->manager
);
1848 assert(link
->manager
->event
);
1850 if (link_ipv4ll_enabled(link
)) {
1851 assert(link
->ipv4ll
);
1853 log_link_debug(link
, "Acquiring IPv4 link-local address");
1855 r
= sd_ipv4ll_start(link
->ipv4ll
);
1857 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1860 if (link_dhcp4_enabled(link
)) {
1861 assert(link
->dhcp_client
);
1863 log_link_debug(link
, "Acquiring DHCPv4 lease");
1865 r
= sd_dhcp_client_start(link
->dhcp_client
);
1867 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1873 static int link_acquire_conf(Link
*link
) {
1878 r
= link_acquire_ipv4_conf(link
);
1882 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1883 r
= link_acquire_ipv6_conf(link
);
1888 if (link_lldp_emit_enabled(link
)) {
1889 r
= link_lldp_emit_start(link
);
1891 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1897 bool link_has_carrier(Link
*link
) {
1898 /* see Documentation/networking/operstates.txt in the kernel sources */
1900 if (link
->kernel_operstate
== IF_OPER_UP
)
1903 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1904 /* operstate may not be implemented, so fall back to flags */
1905 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1911 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1916 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1919 r
= sd_netlink_message_get_errno(m
);
1921 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1926 static int link_configure_addrgen_mode(Link
*link
) {
1927 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1928 uint8_t ipv6ll_mode
;
1932 assert(link
->network
);
1933 assert(link
->manager
);
1934 assert(link
->manager
->rtnl
);
1936 log_link_debug(link
, "Setting address genmode for link");
1938 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1940 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1942 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1944 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1946 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1948 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1950 if (!link_ipv6ll_enabled(link
))
1951 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1952 else if (sysctl_read_ip_property(AF_INET6
, link
->ifname
, "stable_secret", NULL
) < 0)
1953 /* The file may not exist. And event if it exists, when stable_secret is unset,
1954 * reading the file fails with EIO. */
1955 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1957 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1959 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1961 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1963 r
= sd_netlink_message_close_container(req
);
1965 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1967 r
= sd_netlink_message_close_container(req
);
1969 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1971 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1972 link_netlink_destroy_callback
, link
);
1974 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1981 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1986 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1989 r
= sd_netlink_message_get_errno(m
);
1991 /* we warn but don't fail the link, as it may be brought up later */
1992 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1997 static int link_up(Link
*link
) {
1998 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2002 assert(link
->network
);
2003 assert(link
->manager
);
2004 assert(link
->manager
->rtnl
);
2006 log_link_debug(link
, "Bringing link up");
2008 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2010 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2012 /* set it free if not enslaved with networkd */
2013 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
2014 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
2016 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
2019 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2021 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2023 if (link
->network
->mac
) {
2024 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
2026 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
2029 if (link_ipv6_enabled(link
)) {
2030 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
2032 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
2034 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
2035 r
= sd_netlink_message_open_container(req
, AF_INET6
);
2037 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
2039 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
2040 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
2042 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
2045 r
= sd_netlink_message_close_container(req
);
2047 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
2049 r
= sd_netlink_message_close_container(req
);
2051 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2054 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2055 link_netlink_destroy_callback
, link
);
2057 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2064 static int link_up_can(Link
*link
) {
2065 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2070 log_link_debug(link
, "Bringing CAN link up");
2072 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2074 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2076 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2078 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2080 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2081 link_netlink_destroy_callback
, link
);
2083 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2090 static int link_set_can(Link
*link
) {
2091 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2095 assert(link
->network
);
2096 assert(link
->manager
);
2097 assert(link
->manager
->rtnl
);
2099 log_link_debug(link
, "link_set_can");
2101 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2103 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2105 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2107 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2109 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2111 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2113 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2115 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2117 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2118 struct can_bittiming bt
= {
2119 .bitrate
= link
->network
->can_bitrate
,
2120 .sample_point
= link
->network
->can_sample_point
,
2123 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2124 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2128 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2129 if (link
->network
->can_sample_point
> 0)
2130 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2132 log_link_debug(link
, "Using default sample point");
2134 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2136 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2139 if (link
->network
->can_restart_us
> 0) {
2140 char time_string
[FORMAT_TIMESPAN_MAX
];
2141 uint64_t restart_ms
;
2143 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2146 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2148 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2150 if (restart_ms
> UINT32_MAX
) {
2151 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2155 log_link_debug(link
, "Setting restart = %s", time_string
);
2157 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2159 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2162 r
= sd_netlink_message_close_container(m
);
2164 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2166 r
= sd_netlink_message_close_container(m
);
2168 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2170 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2171 link_netlink_destroy_callback
, link
);
2173 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2177 if (!(link
->flags
& IFF_UP
)) {
2178 r
= link_up_can(link
);
2180 link_enter_failed(link
);
2185 log_link_debug(link
, "link_set_can done");
2190 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2195 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2198 r
= sd_netlink_message_get_errno(m
);
2200 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2202 if (streq_ptr(link
->kind
, "can"))
2208 int link_down(Link
*link
) {
2209 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2213 assert(link
->manager
);
2214 assert(link
->manager
->rtnl
);
2216 log_link_debug(link
, "Bringing link down");
2218 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2219 RTM_SETLINK
, link
->ifindex
);
2221 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2223 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2225 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2227 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2228 link_netlink_destroy_callback
, link
);
2230 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2237 static int link_handle_bound_to_list(Link
*link
) {
2241 bool required_up
= false;
2242 bool link_is_up
= false;
2246 if (hashmap_isempty(link
->bound_to_links
))
2249 if (link
->flags
& IFF_UP
)
2252 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2253 if (link_has_carrier(l
)) {
2258 if (!required_up
&& link_is_up
) {
2259 r
= link_down(link
);
2262 } else if (required_up
&& !link_is_up
) {
2271 static int link_handle_bound_by_list(Link
*link
) {
2278 if (hashmap_isempty(link
->bound_by_links
))
2281 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2282 r
= link_handle_bound_to_list(l
);
2290 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2296 if (link
== carrier
)
2299 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2302 r
= hashmap_ensure_allocated(h
, NULL
);
2306 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2313 static int link_new_bound_by_list(Link
*link
) {
2318 bool list_updated
= false;
2321 assert(link
->manager
);
2325 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2326 if (!carrier
->network
)
2329 if (strv_isempty(carrier
->network
->bind_carrier
))
2332 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2333 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2337 list_updated
= true;
2344 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2345 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2349 link_dirty(carrier
);
2355 static int link_new_bound_to_list(Link
*link
) {
2360 bool list_updated
= false;
2363 assert(link
->manager
);
2368 if (strv_isempty(link
->network
->bind_carrier
))
2373 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2374 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2375 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2379 list_updated
= true;
2386 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2387 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2391 link_dirty(carrier
);
2397 static int link_new_carrier_maps(Link
*link
) {
2400 r
= link_new_bound_by_list(link
);
2404 r
= link_handle_bound_by_list(link
);
2408 r
= link_new_bound_to_list(link
);
2412 r
= link_handle_bound_to_list(link
);
2419 static void link_free_bound_to_list(Link
*link
) {
2423 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2424 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2426 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2427 link_dirty(bound_to
);
2433 static void link_free_bound_by_list(Link
*link
) {
2437 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2438 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2440 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2441 link_dirty(bound_by
);
2442 link_handle_bound_to_list(bound_by
);
2449 static void link_free_carrier_maps(Link
*link
) {
2450 bool list_updated
= false;
2454 if (!hashmap_isempty(link
->bound_to_links
)) {
2455 link_free_bound_to_list(link
);
2456 list_updated
= true;
2459 if (!hashmap_isempty(link
->bound_by_links
)) {
2460 link_free_bound_by_list(link
);
2461 list_updated
= true;
2470 void link_drop(Link
*link
) {
2471 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2474 link_set_state(link
, LINK_STATE_LINGER
);
2476 link_free_carrier_maps(link
);
2478 log_link_debug(link
, "Link removed");
2480 (void) unlink(link
->state_file
);
2482 link_detach_from_manager(link
);
2489 static int link_joined(Link
*link
) {
2493 assert(link
->network
);
2495 if (!hashmap_isempty(link
->bound_to_links
)) {
2496 r
= link_handle_bound_to_list(link
);
2499 } else if (!(link
->flags
& IFF_UP
)) {
2502 link_enter_failed(link
);
2507 if (link
->network
->bridge
) {
2508 r
= link_set_bridge(link
);
2510 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2512 r
= link_append_to_master(link
, link
->network
->bridge
);
2514 log_link_error_errno(link
, r
, "Failed to add to bridge master's slave list: %m");
2517 if (link
->network
->bond
) {
2518 r
= link_set_bond(link
);
2520 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2522 r
= link_append_to_master(link
, link
->network
->bond
);
2524 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2527 if (link
->network
->use_br_vlan
&&
2528 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2529 r
= link_set_bridge_vlan(link
);
2531 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2534 /* Skip setting up addresses until it gets carrier,
2535 or it would try to set addresses twice,
2536 which is bad for non-idempotent steps. */
2537 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2540 return link_request_set_addresses(link
);
2543 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2547 assert(link
->network
);
2548 assert(link
->enslaving
> 0);
2549 assert(!link
->enslaved_raw
);
2553 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2556 r
= sd_netlink_message_get_errno(m
);
2557 if (r
< 0 && r
!= -EEXIST
) {
2558 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2559 link_enter_failed(link
);
2562 log_link_debug(link
, "Joined netdev");
2564 if (link
->enslaving
== 0) {
2565 link
->enslaved_raw
= true;
2572 static int link_enter_join_netdev(Link
*link
) {
2578 assert(link
->network
);
2579 assert(link
->state
== LINK_STATE_PENDING
);
2581 link_set_state(link
, LINK_STATE_CONFIGURING
);
2584 link
->enslaving
= 0;
2585 link
->enslaved_raw
= false;
2587 if (link
->network
->bond
) {
2588 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2589 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2590 return link_joined(link
);
2592 log_struct(LOG_DEBUG
,
2593 LOG_LINK_INTERFACE(link
),
2594 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2595 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2599 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2601 log_struct_errno(LOG_WARNING
, r
,
2602 LOG_LINK_INTERFACE(link
),
2603 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2604 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2605 link_enter_failed(link
);
2610 if (link
->network
->bridge
) {
2611 log_struct(LOG_DEBUG
,
2612 LOG_LINK_INTERFACE(link
),
2613 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2614 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2618 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2620 log_struct_errno(LOG_WARNING
, r
,
2621 LOG_LINK_INTERFACE(link
),
2622 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2623 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2624 link_enter_failed(link
);
2629 if (link
->network
->vrf
) {
2630 log_struct(LOG_DEBUG
,
2631 LOG_LINK_INTERFACE(link
),
2632 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2633 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2637 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2639 log_struct_errno(LOG_WARNING
, r
,
2640 LOG_LINK_INTERFACE(link
),
2641 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2642 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2643 link_enter_failed(link
);
2648 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2650 if (netdev
->ifindex
> 0) {
2655 log_struct(LOG_DEBUG
,
2656 LOG_LINK_INTERFACE(link
),
2657 LOG_NETDEV_INTERFACE(netdev
),
2658 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2662 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2664 log_struct_errno(LOG_WARNING
, r
,
2665 LOG_LINK_INTERFACE(link
),
2666 LOG_NETDEV_INTERFACE(netdev
),
2667 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2668 link_enter_failed(link
);
2673 if (link
->enslaving
== 0)
2674 return link_joined(link
);
2679 static int link_set_ipv4_forward(Link
*link
) {
2682 if (!link_ipv4_forward_enabled(link
))
2685 /* We propagate the forwarding flag from one interface to the
2686 * global setting one way. This means: as long as at least one
2687 * interface was configured at any time that had IP forwarding
2688 * enabled the setting will stay on for good. We do this
2689 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2690 * somewhat in sync (see below). */
2692 r
= sysctl_write_ip_property(AF_INET
, NULL
, "ip_forward", "1");
2694 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2699 static int link_set_ipv6_forward(Link
*link
) {
2702 if (!link_ipv6_forward_enabled(link
))
2705 /* On Linux, the IPv6 stack does not know a per-interface
2706 * packet forwarding setting: either packet forwarding is on
2707 * for all, or off for all. We hence don't bother with a
2708 * per-interface setting, but simply propagate the interface
2709 * flag, if it is set, to the global flag, one-way. Note that
2710 * while IPv4 would allow a per-interface flag, we expose the
2711 * same behaviour there and also propagate the setting from
2712 * one to all, to keep things simple (see above). */
2714 r
= sysctl_write_ip_property(AF_INET6
, "all", "forwarding", "1");
2716 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2721 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2722 IPv6PrivacyExtensions s
;
2725 s
= link_ipv6_privacy_extensions(link
);
2729 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "use_tempaddr", (int) link
->network
->ipv6_privacy_extensions
);
2731 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2736 static int link_set_ipv6_accept_ra(Link
*link
) {
2739 /* Make this a NOP if IPv6 is not available */
2740 if (!socket_ipv6_is_supported())
2743 if (link
->flags
& IFF_LOOPBACK
)
2749 r
= sysctl_write_ip_property(AF_INET6
, link
->ifname
, "accept_ra", "0");
2751 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2756 static int link_set_ipv6_dad_transmits(Link
*link
) {
2759 /* Make this a NOP if IPv6 is not available */
2760 if (!socket_ipv6_is_supported())
2763 if (link
->flags
& IFF_LOOPBACK
)
2769 if (link
->network
->ipv6_dad_transmits
< 0)
2772 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "dad_transmits", link
->network
->ipv6_dad_transmits
);
2774 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2779 static int link_set_ipv6_hop_limit(Link
*link
) {
2782 /* Make this a NOP if IPv6 is not available */
2783 if (!socket_ipv6_is_supported())
2786 if (link
->flags
& IFF_LOOPBACK
)
2792 if (link
->network
->ipv6_hop_limit
< 0)
2795 r
= sysctl_write_ip_property_int(AF_INET6
, link
->ifname
, "hop_limit", link
->network
->ipv6_hop_limit
);
2797 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2802 static int link_set_ipv6_mtu(Link
*link
) {
2805 /* Make this a NOP if IPv6 is not available */
2806 if (!socket_ipv6_is_supported())
2809 if (link
->flags
& IFF_LOOPBACK
)
2812 if (link
->network
->ipv6_mtu
== 0)
2815 r
= sysctl_write_ip_property_uint32(AF_INET6
, link
->ifname
, "mtu", link
->network
->ipv6_mtu
);
2817 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2822 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2823 Address
*net_address
;
2831 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2832 if (address_equal(net_address
, address
))
2838 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2847 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2848 if (route_equal(net_route
, route
))
2854 static int link_drop_foreign_config(Link
*link
) {
2860 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2861 /* we consider IPv6LL addresses to be managed by the kernel */
2862 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2865 if (link_is_static_address_configured(link
, address
)) {
2866 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2868 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2870 r
= address_remove(address
, link
, NULL
);
2876 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2877 /* do not touch routes managed by the kernel */
2878 if (route
->protocol
== RTPROT_KERNEL
)
2881 if (link_is_static_route_configured(link
, route
)) {
2882 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2886 r
= route_remove(route
, link
, NULL
);
2895 static int link_drop_config(Link
*link
) {
2896 Address
*address
, *pool_address
;
2901 SET_FOREACH(address
, link
->addresses
, i
) {
2902 /* we consider IPv6LL addresses to be managed by the kernel */
2903 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2906 r
= address_remove(address
, link
, NULL
);
2910 /* If this address came from an address pool, clean up the pool */
2911 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2912 if (address_equal(address
, pool_address
)) {
2913 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2914 address_free(pool_address
);
2920 SET_FOREACH(route
, link
->routes
, i
) {
2921 /* do not touch routes managed by the kernel */
2922 if (route
->protocol
== RTPROT_KERNEL
)
2925 r
= route_remove(route
, link
, NULL
);
2935 static int link_update_lldp(Link
*link
) {
2943 if (link
->flags
& IFF_UP
) {
2944 r
= sd_lldp_start(link
->lldp
);
2946 log_link_debug(link
, "Started LLDP.");
2948 r
= sd_lldp_stop(link
->lldp
);
2950 log_link_debug(link
, "Stopped LLDP.");
2956 static int link_configure_can(Link
*link
) {
2959 if (streq_ptr(link
->kind
, "can")) {
2960 /* The CAN interface must be down to configure bitrate, etc... */
2961 if ((link
->flags
& IFF_UP
)) {
2962 r
= link_down(link
);
2964 link_enter_failed(link
);
2971 return link_set_can(link
);
2974 if (!(link
->flags
& IFF_UP
)) {
2975 r
= link_up_can(link
);
2977 link_enter_failed(link
);
2985 static int link_configure(Link
*link
) {
2989 assert(link
->network
);
2990 assert(link
->state
== LINK_STATE_PENDING
);
2992 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2993 return link_configure_can(link
);
2995 /* Drop foreign config, but ignore loopback or critical devices.
2996 * We do not want to remove loopback address or addresses used for root NFS. */
2997 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2998 r
= link_drop_foreign_config(link
);
3003 r
= link_set_proxy_arp(link
);
3007 r
= ipv6_proxy_ndp_addresses_configure(link
);
3011 r
= link_set_ipv4_forward(link
);
3015 r
= link_set_ipv6_forward(link
);
3019 r
= link_set_ipv6_privacy_extensions(link
);
3023 r
= link_set_ipv6_accept_ra(link
);
3027 r
= link_set_ipv6_dad_transmits(link
);
3031 r
= link_set_ipv6_hop_limit(link
);
3035 r
= link_set_flags(link
);
3039 r
= link_set_ipv6_mtu(link
);
3043 if (link_ipv4ll_enabled(link
)) {
3044 r
= ipv4ll_configure(link
);
3049 if (link_dhcp4_enabled(link
)) {
3050 r
= dhcp4_set_promote_secondaries(link
);
3054 r
= dhcp4_configure(link
);
3059 if (link_dhcp4_server_enabled(link
)) {
3060 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3064 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3069 if (link_dhcp6_enabled(link
) ||
3070 link_ipv6_accept_ra_enabled(link
)) {
3071 r
= dhcp6_configure(link
);
3076 if (link_ipv6_accept_ra_enabled(link
)) {
3077 r
= ndisc_configure(link
);
3082 if (link_radv_enabled(link
)) {
3083 r
= radv_configure(link
);
3088 if (link_lldp_rx_enabled(link
)) {
3089 r
= sd_lldp_new(&link
->lldp
);
3093 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3097 r
= sd_lldp_match_capabilities(link
->lldp
,
3098 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3099 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3100 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3104 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3108 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3112 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3116 r
= link_update_lldp(link
);
3121 if (link
->network
->mtu
> 0) {
3122 r
= link_set_mtu(link
, link
->network
->mtu
);
3127 if (socket_ipv6_is_supported()) {
3128 r
= link_configure_addrgen_mode(link
);
3133 return link_configure_after_setting_mtu(link
);
3136 static int link_configure_after_setting_mtu(Link
*link
) {
3140 assert(link
->network
);
3141 assert(link
->state
== LINK_STATE_PENDING
);
3143 if (link
->setting_mtu
)
3146 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3147 r
= link_acquire_conf(link
);
3152 return link_enter_join_netdev(link
);
3155 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3158 if (duid
->raw_data_len
> 0)
3161 if (duid
->type
!= DUID_TYPE_UUID
)
3164 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3165 duid
->raw_data_len
= sizeof(sd_id128_t
);
3170 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3171 Manager
*manager
= userdata
;
3172 const sd_bus_error
*e
;
3182 e
= sd_bus_message_get_error(m
);
3184 log_error_errno(sd_bus_error_get_errno(e
),
3185 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3190 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3194 if (sz
!= sizeof(sd_id128_t
)) {
3195 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3199 memcpy(&manager
->product_uuid
, a
, sz
);
3200 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3201 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3203 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3206 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3207 r
= link_configure(link
);
3209 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3212 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3214 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3215 * even if the method fails. */
3216 manager
->has_product_uuid
= true;
3221 static bool link_requires_uuid(Link
*link
) {
3225 assert(link
->manager
);
3226 assert(link
->network
);
3228 duid
= link_get_duid(link
);
3229 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3232 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3235 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3241 static int link_configure_duid(Link
*link
) {
3247 assert(link
->manager
);
3248 assert(link
->network
);
3251 duid
= link_get_duid(link
);
3253 if (!link_requires_uuid(link
))
3256 if (m
->has_product_uuid
) {
3257 (void) duid_set_uuid(duid
, m
->product_uuid
);
3261 if (!m
->links_requesting_uuid
) {
3262 r
= manager_request_product_uuid(m
, link
);
3267 log_link_warning_errno(link
, r
,
3268 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3272 r
= set_put(m
->links_requesting_uuid
, link
);
3276 r
= set_put(m
->duids_requesting_uuid
, duid
);
3284 static int link_initialized_and_synced(Link
*link
) {
3289 assert(link
->ifname
);
3290 assert(link
->manager
);
3292 if (link
->state
!= LINK_STATE_PENDING
)
3295 log_link_debug(link
, "Link state is up-to-date");
3297 r
= link_new_bound_by_list(link
);
3301 r
= link_handle_bound_by_list(link
);
3305 if (!link
->network
) {
3306 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3307 &link
->mac
, &network
);
3309 link_enter_unmanaged(link
);
3311 } else if (r
== 0 && network
->unmanaged
) {
3312 link_enter_unmanaged(link
);
3317 if (link
->flags
& IFF_LOOPBACK
) {
3318 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3319 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3321 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3322 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3324 if (network
->dhcp_server
)
3325 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3328 r
= network_apply(network
, link
);
3333 r
= link_new_bound_to_list(link
);
3337 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3338 * link_configure() is called later asynchronously. */
3339 r
= link_configure_duid(link
);
3343 r
= link_configure(link
);
3350 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3351 (void) link_initialized_and_synced(link
);
3355 int link_initialized(Link
*link
, sd_device
*device
) {
3356 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3360 assert(link
->manager
);
3361 assert(link
->manager
->rtnl
);
3364 if (link
->state
!= LINK_STATE_PENDING
)
3367 if (link
->sd_device
)
3370 log_link_debug(link
, "udev initialized link");
3372 link
->sd_device
= sd_device_ref(device
);
3374 /* udev has initialized the link, but we don't know if we have yet
3375 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3376 * when it returns we know that the pending NEWLINKs have already been
3377 * processed and that we are up-to-date */
3379 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3384 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3385 link_netlink_destroy_callback
, link
);
3394 static int link_load(Link
*link
) {
3395 _cleanup_free_
char *network_file
= NULL
,
3398 *dhcp4_address
= NULL
,
3399 *ipv4ll_address
= NULL
;
3400 union in_addr_union address
;
3401 union in_addr_union route_dst
;
3407 r
= parse_env_file(NULL
, link
->state_file
,
3408 "NETWORK_FILE", &network_file
,
3409 "ADDRESSES", &addresses
,
3411 "DHCP4_ADDRESS", &dhcp4_address
,
3412 "IPV4LL_ADDRESS", &ipv4ll_address
);
3413 if (r
< 0 && r
!= -ENOENT
)
3414 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3421 suffix
= strrchr(network_file
, '.');
3423 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3424 goto network_file_fail
;
3428 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3430 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3431 goto network_file_fail
;
3434 r
= network_apply(network
, link
);
3436 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3445 _cleanup_free_
char *address_str
= NULL
;
3446 char *prefixlen_str
;
3448 unsigned char prefixlen
;
3450 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3452 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3458 prefixlen_str
= strchr(address_str
, '/');
3459 if (!prefixlen_str
) {
3460 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3464 *prefixlen_str
++ = '\0';
3466 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3468 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3472 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3474 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3478 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3480 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3489 _cleanup_free_
char *route_str
= NULL
;
3490 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3492 char *prefixlen_str
;
3494 unsigned char prefixlen
, tos
, table
;
3497 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3499 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3505 prefixlen_str
= strchr(route_str
, '/');
3506 if (!prefixlen_str
) {
3507 log_link_debug(link
, "Failed to parse route %s", route_str
);
3511 *prefixlen_str
++ = '\0';
3513 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3515 log_link_debug(link
,
3516 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3521 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3523 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3527 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3529 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3531 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3532 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3533 0, route_expire_handler
, route
);
3535 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3538 route
->lifetime
= lifetime
;
3539 sd_event_source_unref(route
->expire
);
3540 route
->expire
= TAKE_PTR(expire
);
3544 if (dhcp4_address
) {
3545 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3547 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3548 goto dhcp4_address_fail
;
3551 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3553 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3555 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3557 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3562 if (ipv4ll_address
) {
3563 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3565 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3566 goto ipv4ll_address_fail
;
3569 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3571 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3573 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3575 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3578 ipv4ll_address_fail
:
3583 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3584 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3585 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3594 r
= link_new(m
, message
, ret
);
3600 log_link_debug(link
, "Link %d added", link
->ifindex
);
3602 r
= link_load(link
);
3606 if (detect_container() <= 0) {
3607 /* not in a container, udev will be around */
3608 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3609 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3611 log_link_warning_errno(link
, r
, "Could not find device: %m");
3615 r
= sd_device_get_is_initialized(device
);
3617 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3622 log_link_debug(link
, "link pending udev initialization...");
3626 r
= link_initialized(link
, device
);
3630 r
= link_initialized_and_synced(link
);
3637 link_enter_failed(link
);
3641 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3646 log_link_info(link
, "Gained IPv6LL");
3648 link
->ipv6ll_address
= *address
;
3649 link_check_ready(link
);
3651 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3652 r
= link_acquire_ipv6_conf(link
);
3654 link_enter_failed(link
);
3662 static int link_carrier_gained(Link
*link
) {
3667 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3668 r
= link_acquire_conf(link
);
3670 link_enter_failed(link
);
3674 r
= link_request_set_addresses(link
);
3679 r
= link_handle_bound_by_list(link
);
3686 static int link_carrier_lost(Link
*link
) {
3691 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3694 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3695 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3696 * configuration and stop the clients as well. */
3697 if (link
->setting_mtu
)
3700 r
= link_stop_clients(link
);
3702 link_enter_failed(link
);
3706 if (link_dhcp4_server_enabled(link
))
3707 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3709 r
= link_drop_config(link
);
3713 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3714 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3715 r
= link_drop_foreign_config(link
);
3720 r
= link_handle_bound_by_list(link
);
3727 int link_carrier_reset(Link
*link
) {
3732 if (link_has_carrier(link
)) {
3733 r
= link_carrier_lost(link
);
3737 r
= link_carrier_gained(link
);
3741 log_link_info(link
, "Reset carrier");
3747 int link_update(Link
*link
, sd_netlink_message
*m
) {
3748 struct ether_addr mac
;
3751 bool had_carrier
, carrier_gained
, carrier_lost
;
3755 assert(link
->ifname
);
3758 if (link
->state
== LINK_STATE_LINGER
) {
3759 log_link_info(link
, "Link readded");
3760 link_set_state(link
, LINK_STATE_CONFIGURING
);
3762 r
= link_new_carrier_maps(link
);
3767 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3768 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3769 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3771 if (link
->state
== LINK_STATE_PENDING
) {
3772 r
= free_and_strdup(&link
->ifname
, ifname
);
3776 Manager
*manager
= link
->manager
;
3779 r
= link_add(manager
, m
, &link
);
3785 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3786 if (r
>= 0 && mtu
> 0) {
3788 if (link
->original_mtu
== 0) {
3789 link
->original_mtu
= mtu
;
3790 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3793 if (link
->dhcp_client
) {
3794 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3797 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3801 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3803 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3807 /* The kernel may broadcast NEWLINK messages without the MAC address
3808 set, simply ignore them. */
3809 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3811 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3814 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3817 log_link_debug(link
, "MAC address: "
3818 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3819 mac
.ether_addr_octet
[0],
3820 mac
.ether_addr_octet
[1],
3821 mac
.ether_addr_octet
[2],
3822 mac
.ether_addr_octet
[3],
3823 mac
.ether_addr_octet
[4],
3824 mac
.ether_addr_octet
[5]);
3827 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3829 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3832 if (link
->dhcp_client
) {
3833 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3834 (const uint8_t *) &link
->mac
,
3838 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3840 r
= dhcp4_set_client_identifier(link
);
3845 if (link
->dhcp6_client
) {
3846 const DUID
* duid
= link_get_duid(link
);
3848 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3849 (const uint8_t *) &link
->mac
,
3853 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3855 if (link
->network
->iaid_set
) {
3856 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3857 link
->network
->iaid
);
3859 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3862 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3864 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3865 duid
->raw_data_len
);
3867 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3871 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3873 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3877 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3879 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3884 had_carrier
= link_has_carrier(link
);
3886 r
= link_update_flags(link
, m
);
3890 r
= link_update_lldp(link
);
3894 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3895 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3897 if (carrier_gained
) {
3898 log_link_info(link
, "Gained carrier");
3900 r
= link_carrier_gained(link
);
3903 } else if (carrier_lost
) {
3904 log_link_info(link
, "Lost carrier");
3906 r
= link_carrier_lost(link
);
3914 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3922 if (hashmap_isempty(h
))
3926 HASHMAP_FOREACH(link
, h
, i
) {
3930 fprintf(f
, "%i", link
->ifindex
);
3937 int link_save(Link
*link
) {
3938 _cleanup_free_
char *temp_path
= NULL
;
3939 _cleanup_fclose_
FILE *f
= NULL
;
3940 const char *admin_state
, *oper_state
;
3947 assert(link
->state_file
);
3948 assert(link
->lease_file
);
3949 assert(link
->manager
);
3951 if (link
->state
== LINK_STATE_LINGER
) {
3952 unlink(link
->state_file
);
3956 link_lldp_save(link
);
3958 admin_state
= link_state_to_string(link
->state
);
3959 assert(admin_state
);
3961 oper_state
= link_operstate_to_string(link
->operstate
);
3964 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3968 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3969 (void) fchmod(fileno(f
), 0644);
3972 "# This is private data. Do not parse.\n"
3975 admin_state
, oper_state
);
3977 if (link
->network
) {
3979 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3980 const char *dhcp_domainname
= NULL
;
3981 char **dhcp6_domains
= NULL
;
3982 char **dhcp_domains
= NULL
;
3985 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3986 yes_no(link
->network
->required_for_online
));
3988 if (link
->dhcp6_client
) {
3989 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3990 if (r
< 0 && r
!= -ENOMSG
)
3991 log_link_debug(link
, "No DHCPv6 lease");
3994 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3999 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
4000 _cleanup_free_
char *b
= NULL
;
4002 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
4003 &link
->network
->dns
[j
].address
, &b
);
4005 log_debug_errno(r
, "Failed to format address, ignoring: %m");
4015 if (link
->network
->dhcp_use_dns
&&
4017 const struct in_addr
*addresses
;
4019 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
4021 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4025 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
4026 struct in6_addr
*in6_addrs
;
4028 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4032 serialize_in6_addrs(f
, in6_addrs
, r
);
4037 /* Make sure to flush out old entries before we use the NDISC data */
4040 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4043 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4047 serialize_in6_addrs(f
, &dd
->address
, 1);
4056 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4058 if (link
->network
->dhcp_use_ntp
&&
4060 const struct in_addr
*addresses
;
4062 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4064 if (serialize_in_addrs(f
, addresses
, r
, space
, in4_addr_is_non_local
) > 0)
4068 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4069 struct in6_addr
*in6_addrs
;
4072 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4077 serialize_in6_addrs(f
, in6_addrs
, r
);
4081 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4083 fputstrv(f
, hosts
, NULL
, &space
);
4088 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4089 if (link
->dhcp_lease
) {
4090 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4091 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4094 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4097 ordered_set_print(f
, "DOMAINS=", link
->network
->search_domains
);
4099 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4102 if (dhcp_domainname
)
4103 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4105 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4107 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4109 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4110 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4115 ordered_set_print(f
, "ROUTE_DOMAINS=", link
->network
->route_domains
);
4117 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4120 if (dhcp_domainname
)
4121 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4123 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4125 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4127 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4128 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4133 fprintf(f
, "LLMNR=%s\n",
4134 resolve_support_to_string(link
->network
->llmnr
));
4135 fprintf(f
, "MDNS=%s\n",
4136 resolve_support_to_string(link
->network
->mdns
));
4137 if (link
->network
->dns_default_route
>= 0)
4138 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4140 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4141 fprintf(f
, "DNS_OVER_TLS=%s\n",
4142 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4144 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4145 fprintf(f
, "DNSSEC=%s\n",
4146 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4148 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4151 fputs("DNSSEC_NTA=", f
);
4153 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4154 fputs_with_space(f
, n
, NULL
, &space
);
4158 fputs("ADDRESSES=", f
);
4160 SET_FOREACH(a
, link
->addresses
, i
) {
4161 _cleanup_free_
char *address_str
= NULL
;
4163 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4167 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4172 fputs("ROUTES=", f
);
4174 SET_FOREACH(route
, link
->routes
, i
) {
4175 _cleanup_free_
char *route_str
= NULL
;
4177 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4181 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4182 space
? " " : "", route_str
,
4183 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4190 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4191 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4193 if (link
->dhcp_lease
) {
4194 struct in_addr address
;
4195 const char *tz
= NULL
;
4197 assert(link
->network
);
4199 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4201 fprintf(f
, "TIMEZONE=%s\n", tz
);
4203 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4205 fputs("DHCP4_ADDRESS=", f
);
4206 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4210 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4218 unlink(link
->lease_file
);
4221 struct in_addr address
;
4223 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4225 fputs("IPV4LL_ADDRESS=", f
);
4226 serialize_in_addrs(f
, &address
, 1, false, NULL
);
4231 r
= fflush_and_check(f
);
4235 if (rename(temp_path
, link
->state_file
) < 0) {
4243 (void) unlink(link
->state_file
);
4245 (void) unlink(temp_path
);
4247 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4250 /* The serialized state in /run is no longer up-to-date. */
4251 void link_dirty(Link
*link
) {
4256 /* mark manager dirty as link is dirty */
4257 manager_dirty(link
->manager
);
4259 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4261 /* allocation errors are ignored */
4264 r
= set_put(link
->manager
->dirty_links
, link
);
4266 /* don't take another ref if the link was already dirty */
4272 /* The serialized state in /run is up-to-date */
4273 void link_clean(Link
*link
) {
4275 assert(link
->manager
);
4277 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4280 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4281 [LINK_STATE_PENDING
] = "pending",
4282 [LINK_STATE_CONFIGURING
] = "configuring",
4283 [LINK_STATE_CONFIGURED
] = "configured",
4284 [LINK_STATE_UNMANAGED
] = "unmanaged",
4285 [LINK_STATE_FAILED
] = "failed",
4286 [LINK_STATE_LINGER
] = "linger",
4289 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4291 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4292 [LINK_OPERSTATE_OFF
] = "off",
4293 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4294 [LINK_OPERSTATE_DORMANT
] = "dormant",
4295 [LINK_OPERSTATE_CARRIER
] = "carrier",
4296 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4297 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
4298 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4301 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);