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 "tmpfile-util.h"
36 uint32_t link_get_vrf_table(Link
*link
) {
37 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
40 uint32_t link_get_dhcp_route_table(Link
*link
) {
41 /* When the interface is part of an VRF use the VRFs routing table, unless
42 * another table is explicitly specified. */
43 if (link
->network
->dhcp_route_table_set
)
44 return link
->network
->dhcp_route_table
;
45 return link_get_vrf_table(link
);
48 uint32_t link_get_ipv6_accept_ra_route_table(Link
*link
) {
49 if (link
->network
->ipv6_accept_ra_route_table_set
)
50 return link
->network
->ipv6_accept_ra_route_table
;
51 return link_get_vrf_table(link
);
54 DUID
* link_get_duid(Link
*link
) {
55 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
56 return &link
->network
->duid
;
58 return &link
->manager
->duid
;
61 static bool link_dhcp6_enabled(Link
*link
) {
64 if (!socket_ipv6_is_supported())
67 if (link
->flags
& IFF_LOOPBACK
)
73 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
76 static bool link_dhcp4_enabled(Link
*link
) {
79 if (link
->flags
& IFF_LOOPBACK
)
85 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
88 static bool link_dhcp4_server_enabled(Link
*link
) {
91 if (link
->flags
& IFF_LOOPBACK
)
97 return link
->network
->dhcp_server
;
100 static bool link_ipv4ll_enabled(Link
*link
) {
103 if (link
->flags
& IFF_LOOPBACK
)
109 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
112 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
115 static bool link_ipv6ll_enabled(Link
*link
) {
118 if (!socket_ipv6_is_supported())
121 if (link
->flags
& IFF_LOOPBACK
)
127 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard"))
130 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
133 static bool link_ipv6_enabled(Link
*link
) {
136 if (!socket_ipv6_is_supported())
139 if (link
->network
->bridge
)
142 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
143 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
146 static bool link_radv_enabled(Link
*link
) {
149 if (!link_ipv6ll_enabled(link
))
152 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
155 static bool link_lldp_rx_enabled(Link
*link
) {
158 if (link
->flags
& IFF_LOOPBACK
)
161 if (link
->iftype
!= ARPHRD_ETHER
)
167 /* LLDP should be handled on bridge slaves as those have a direct
168 * connection to their peers not on the bridge master. Linux doesn't
169 * even (by default) forward lldp packets to the bridge master.*/
170 if (streq_ptr("bridge", link
->kind
))
173 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
176 static bool link_lldp_emit_enabled(Link
*link
) {
179 if (link
->flags
& IFF_LOOPBACK
)
182 if (link
->iftype
!= ARPHRD_ETHER
)
188 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
191 static bool link_ipv4_forward_enabled(Link
*link
) {
194 if (link
->flags
& IFF_LOOPBACK
)
200 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
203 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
206 static bool link_ipv6_forward_enabled(Link
*link
) {
209 if (!socket_ipv6_is_supported())
212 if (link
->flags
& IFF_LOOPBACK
)
218 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
221 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
224 static bool link_proxy_arp_enabled(Link
*link
) {
227 if (link
->flags
& IFF_LOOPBACK
)
233 if (link
->network
->proxy_arp
< 0)
239 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
242 if (!socket_ipv6_is_supported())
245 if (link
->flags
& IFF_LOOPBACK
)
251 if (!link_ipv6ll_enabled(link
))
254 /* If unset use system default (enabled if local forwarding is disabled.
255 * disabled if local forwarding is enabled).
256 * If set, ignore or enforce RA independent of local forwarding state.
258 if (link
->network
->ipv6_accept_ra
< 0)
259 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
260 return !link_ipv6_forward_enabled(link
);
261 else if (link
->network
->ipv6_accept_ra
> 0)
262 /* accept RA even if ip_forward is enabled */
269 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
272 if (!socket_ipv6_is_supported())
273 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
275 if (link
->flags
& IFF_LOOPBACK
)
276 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
279 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
281 return link
->network
->ipv6_privacy_extensions
;
284 static int link_enable_ipv6(Link
*link
) {
285 const char *p
= NULL
;
289 if (link
->flags
& IFF_LOOPBACK
)
292 disabled
= !link_ipv6_enabled(link
);
294 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
296 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
298 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
299 enable_disable(!disabled
), link
->ifname
);
301 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
306 void link_update_operstate(Link
*link
) {
307 LinkOperationalState operstate
;
310 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
311 operstate
= LINK_OPERSTATE_DORMANT
;
312 else if (link_has_carrier(link
)) {
314 uint8_t scope
= RT_SCOPE_NOWHERE
;
317 /* if we have carrier, check what addresses we have */
318 SET_FOREACH(address
, link
->addresses
, i
) {
319 if (!address_is_ready(address
))
322 if (address
->scope
< scope
)
323 scope
= address
->scope
;
326 /* for operstate we also take foreign addresses into account */
327 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
328 if (!address_is_ready(address
))
331 if (address
->scope
< scope
)
332 scope
= address
->scope
;
335 if (scope
< RT_SCOPE_SITE
)
336 /* universally accessible addresses found */
337 operstate
= LINK_OPERSTATE_ROUTABLE
;
338 else if (scope
< RT_SCOPE_HOST
)
339 /* only link or site local addresses found */
340 operstate
= LINK_OPERSTATE_DEGRADED
;
342 /* no useful addresses found */
343 operstate
= LINK_OPERSTATE_CARRIER
;
344 } else if (link
->flags
& IFF_UP
)
345 operstate
= LINK_OPERSTATE_NO_CARRIER
;
347 operstate
= LINK_OPERSTATE_OFF
;
349 if (link
->operstate
!= operstate
) {
350 link
->operstate
= operstate
;
351 link_send_changed(link
, "OperationalState", NULL
);
356 #define FLAG_STRING(string, flag, old, new) \
357 (((old ^ new) & flag) \
358 ? ((old & flag) ? (" -" string) : (" +" string)) \
361 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
362 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
368 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
370 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
372 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
374 /* if we got a message without operstate, take it to mean
375 the state was unchanged */
376 operstate
= link
->kernel_operstate
;
378 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
381 if (link
->flags
!= flags
) {
382 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",
383 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
384 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
385 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
386 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
387 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
388 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
389 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
390 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
391 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
392 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
393 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
394 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
395 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
396 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
397 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
398 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
399 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
400 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
401 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
403 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
404 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
405 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
406 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
407 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
408 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
409 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
410 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
412 /* link flags are currently at most 18 bits, let's align to
414 if (unknown_flags_added
)
416 "Unknown link flags gained: %#.5x (ignoring)",
417 unknown_flags_added
);
419 if (unknown_flags_removed
)
421 "Unknown link flags lost: %#.5x (ignoring)",
422 unknown_flags_removed
);
426 link
->kernel_operstate
= operstate
;
428 link_update_operstate(link
);
433 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
435 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
436 _cleanup_(link_unrefp
) Link
*link
= NULL
;
438 const char *ifname
, *kind
= NULL
;
440 unsigned short iftype
;
446 /* check for link kind */
447 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
449 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
450 r
= sd_netlink_message_exit_container(message
);
455 r
= sd_netlink_message_get_type(message
, &type
);
458 else if (type
!= RTM_NEWLINK
)
461 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
464 else if (ifindex
<= 0)
467 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
471 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
482 .state
= LINK_STATE_PENDING
,
483 .rtnl_extended_attrs
= true,
488 link
->ifname
= strdup(ifname
);
493 link
->kind
= strdup(kind
);
498 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
500 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
502 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
504 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
506 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
509 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
512 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
515 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
519 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
523 r
= link_update_flags(link
, message
);
527 *ret
= TAKE_PTR(link
);
532 static void link_detach_from_manager(Link
*link
) {
533 if (!link
|| !link
->manager
)
536 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
537 set_remove(link
->manager
->links_requesting_uuid
, link
);
541 static Link
*link_free(Link
*link
) {
549 while ((route
= set_first(link
->routes
)))
552 while ((route
= set_first(link
->routes_foreign
)))
555 link
->routes
= set_free(link
->routes
);
556 link
->routes_foreign
= set_free(link
->routes_foreign
);
558 while ((address
= set_first(link
->addresses
)))
559 address_free(address
);
561 while ((address
= set_first(link
->addresses_foreign
)))
562 address_free(address
);
564 link
->addresses
= set_free(link
->addresses
);
565 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
567 while ((address
= link
->pool_addresses
)) {
568 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
569 address_free(address
);
572 sd_dhcp_server_unref(link
->dhcp_server
);
573 sd_dhcp_client_unref(link
->dhcp_client
);
574 sd_dhcp_lease_unref(link
->dhcp_lease
);
576 link_lldp_emit_stop(link
);
578 free(link
->lease_file
);
580 sd_lldp_unref(link
->lldp
);
581 free(link
->lldp_file
);
585 sd_ipv4ll_unref(link
->ipv4ll
);
586 sd_dhcp6_client_unref(link
->dhcp6_client
);
587 sd_ndisc_unref(link
->ndisc
);
588 sd_radv_unref(link
->radv
);
590 link_detach_from_manager(link
);
596 (void) unlink(link
->state_file
);
597 free(link
->state_file
);
599 sd_device_unref(link
->sd_device
);
601 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
602 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
603 hashmap_free(link
->bound_to_links
);
605 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
606 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
607 hashmap_free(link
->bound_by_links
);
612 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
614 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
621 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
630 static void link_set_state(Link
*link
, LinkState state
) {
633 if (link
->state
== state
)
638 link_send_changed(link
, "AdministrativeState", NULL
);
641 static void link_enter_unmanaged(Link
*link
) {
644 log_link_debug(link
, "Unmanaged");
646 link_set_state(link
, LINK_STATE_UNMANAGED
);
651 static int link_stop_clients(Link
*link
) {
655 assert(link
->manager
);
656 assert(link
->manager
->event
);
658 if (link
->dhcp_client
) {
659 k
= sd_dhcp_client_stop(link
->dhcp_client
);
661 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
665 k
= sd_ipv4ll_stop(link
->ipv4ll
);
667 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
670 if (link
->dhcp6_client
) {
671 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
673 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
677 k
= sd_ndisc_stop(link
->ndisc
);
679 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
683 k
= sd_radv_stop(link
->radv
);
685 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
688 link_lldp_emit_stop(link
);
692 void link_enter_failed(Link
*link
) {
695 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
698 log_link_warning(link
, "Failed");
700 link_set_state(link
, LINK_STATE_FAILED
);
702 link_stop_clients(link
);
707 static Address
* link_find_dhcp_server_address(Link
*link
) {
711 assert(link
->network
);
713 /* The first statically configured address if there is any */
714 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
716 if (address
->family
!= AF_INET
)
719 if (in_addr_is_null(address
->family
, &address
->in_addr
))
725 /* If that didn't work, find a suitable address we got from the pool */
726 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
727 if (address
->family
!= AF_INET
)
736 static void link_enter_configured(Link
*link
) {
738 assert(link
->network
);
740 if (link
->state
!= LINK_STATE_CONFIGURING
)
743 log_link_info(link
, "Configured");
745 link_set_state(link
, LINK_STATE_CONFIGURED
);
750 static int link_request_set_routing_policy_rule(Link
*link
) {
751 RoutingPolicyRule
*rule
, *rrule
= NULL
;
755 assert(link
->network
);
757 link_set_state(link
, LINK_STATE_CONFIGURING
);
758 link
->routing_policy_rules_configured
= false;
760 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
761 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
762 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
763 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
765 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
769 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
771 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
772 link_enter_failed(link
);
776 link
->routing_policy_rule_messages
++;
779 routing_policy_rule_purge(link
->manager
, link
);
780 if (link
->routing_policy_rule_messages
== 0) {
781 link
->routing_policy_rules_configured
= true;
782 link_check_ready(link
);
784 log_link_debug(link
, "Setting routing policy rules");
789 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
793 assert(link
->route_messages
> 0);
794 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
795 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
797 link
->route_messages
--;
799 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
802 r
= sd_netlink_message_get_errno(m
);
803 if (r
< 0 && r
!= -EEXIST
)
804 log_link_warning_errno(link
, r
, "Could not set route: %m");
806 if (link
->route_messages
== 0) {
807 log_link_debug(link
, "Routes set");
808 link
->static_routes_configured
= true;
809 link_check_ready(link
);
815 static int link_request_set_routes(Link
*link
) {
817 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
818 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
825 assert(link
->network
);
826 assert(link
->addresses_configured
);
827 assert(link
->address_messages
== 0);
828 assert(link
->state
!= _LINK_STATE_INVALID
);
830 link_set_state(link
, LINK_STATE_CONFIGURING
);
831 link
->static_routes_configured
= false;
833 r
= link_request_set_routing_policy_rule(link
);
837 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
838 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
839 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
841 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
844 r
= route_configure(rt
, link
, route_handler
);
846 log_link_warning_errno(link
, r
, "Could not set routes: %m");
847 link_enter_failed(link
);
851 link
->route_messages
++;
854 if (link
->route_messages
== 0) {
855 link
->static_routes_configured
= true;
856 link_check_ready(link
);
858 log_link_debug(link
, "Setting routes");
863 void link_check_ready(Link
*link
) {
869 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
875 if (!link
->addresses_configured
)
878 if (!link
->neighbors_configured
)
881 SET_FOREACH(a
, link
->addresses
, i
)
882 if (!address_is_ready(a
))
885 if (!link
->addresses_ready
) {
886 link
->addresses_ready
= true;
887 link_request_set_routes(link
);
890 if (!link
->static_routes_configured
)
893 if (!link
->routing_policy_rules_configured
)
896 if (link_ipv4ll_enabled(link
))
897 if (!link
->ipv4ll_address
||
901 if (!link
->network
->bridge
) {
903 if (link_ipv6ll_enabled(link
) &&
904 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
907 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
908 !link
->dhcp4_configured
) ||
909 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
910 !link
->dhcp6_configured
) ||
911 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
912 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
915 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
919 if (link
->state
!= LINK_STATE_CONFIGURED
)
920 link_enter_configured(link
);
925 static int link_request_set_neighbors(Link
*link
) {
930 assert(link
->network
);
931 assert(link
->state
!= _LINK_STATE_INVALID
);
933 link_set_state(link
, LINK_STATE_CONFIGURING
);
934 link
->neighbors_configured
= false;
936 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
937 r
= neighbor_configure(neighbor
, link
, NULL
);
939 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
940 link_enter_failed(link
);
945 if (link
->neighbor_messages
== 0) {
946 link
->neighbors_configured
= true;
947 link_check_ready(link
);
949 log_link_debug(link
, "Setting neighbors");
954 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
960 assert(link
->ifname
);
961 assert(link
->address_messages
> 0);
962 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
963 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
965 link
->address_messages
--;
967 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
970 r
= sd_netlink_message_get_errno(m
);
971 if (r
< 0 && r
!= -EEXIST
)
972 log_link_warning_errno(link
, r
, "could not set address: %m");
974 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
976 if (link
->address_messages
== 0) {
977 log_link_debug(link
, "Addresses set");
978 link
->addresses_configured
= true;
979 link_check_ready(link
);
985 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
986 _cleanup_free_
struct in_addr
*addresses
= NULL
;
987 size_t n_addresses
= 0, n_allocated
= 0;
990 log_debug("Copying DNS server information from %s", link
->ifname
);
995 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
998 /* Only look for IPv4 addresses */
999 if (link
->network
->dns
[i
].family
!= AF_INET
)
1002 ia
= link
->network
->dns
[i
].address
.in
;
1004 /* Never propagate obviously borked data */
1005 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1008 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1011 addresses
[n_addresses
++] = ia
;
1014 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1015 const struct in_addr
*da
= NULL
;
1018 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1021 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1024 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1029 if (n_addresses
<= 0)
1032 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1035 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1036 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1037 size_t n_addresses
= 0, n_allocated
= 0;
1043 log_debug("Copying NTP server information from %s", link
->ifname
);
1045 STRV_FOREACH(a
, link
->network
->ntp
) {
1048 /* Only look for IPv4 addresses */
1049 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1052 /* Never propagate obviously borked data */
1053 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1056 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1059 addresses
[n_addresses
++] = ia
;
1062 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1063 const struct in_addr
*da
= NULL
;
1066 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1069 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1072 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1077 if (n_addresses
<= 0)
1080 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1083 static int link_set_bridge_fdb(Link
*link
) {
1084 FdbEntry
*fdb_entry
;
1087 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1088 r
= fdb_entry_configure(link
, fdb_entry
);
1090 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1096 static int link_request_set_addresses(Link
*link
) {
1097 AddressLabel
*label
;
1102 assert(link
->network
);
1103 assert(link
->state
!= _LINK_STATE_INVALID
);
1105 link_set_state(link
, LINK_STATE_CONFIGURING
);
1107 /* Reset all *_configured flags we are configuring. */
1108 link
->addresses_configured
= false;
1109 link
->addresses_ready
= false;
1110 link
->neighbors_configured
= false;
1111 link
->static_routes_configured
= false;
1112 link
->routing_policy_rules_configured
= false;
1114 r
= link_set_bridge_fdb(link
);
1118 r
= link_request_set_neighbors(link
);
1122 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1125 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1127 r
= address_configure(ad
, link
, address_handler
, update
);
1129 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1130 link_enter_failed(link
);
1134 link
->address_messages
++;
1137 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1138 r
= address_label_configure(label
, link
, NULL
, false);
1140 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1141 link_enter_failed(link
);
1145 link
->address_label_messages
++;
1148 /* now that we can figure out a default address for the dhcp server,
1150 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1152 Link
*uplink
= NULL
;
1153 bool acquired_uplink
= false;
1155 address
= link_find_dhcp_server_address(link
);
1157 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1158 link_enter_failed(link
);
1162 /* use the server address' subnet as the pool */
1163 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1164 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1169 r = sd_dhcp_server_set_router(link->dhcp_server,
1170 &main_address->in_addr.in);
1175 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1176 r
= sd_dhcp_server_set_max_lease_time(
1178 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1183 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1184 r
= sd_dhcp_server_set_default_lease_time(
1186 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1191 if (link
->network
->dhcp_server_emit_dns
) {
1193 if (link
->network
->n_dhcp_server_dns
> 0)
1194 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1196 uplink
= manager_find_uplink(link
->manager
, link
);
1197 acquired_uplink
= true;
1200 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1203 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1206 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1209 if (link
->network
->dhcp_server_emit_ntp
) {
1211 if (link
->network
->n_dhcp_server_ntp
> 0)
1212 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1214 if (!acquired_uplink
)
1215 uplink
= manager_find_uplink(link
->manager
, link
);
1218 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1221 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1225 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1228 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1230 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1232 if (link
->network
->dhcp_server_emit_timezone
) {
1233 _cleanup_free_
char *buffer
= NULL
;
1234 const char *tz
= NULL
;
1236 if (link
->network
->dhcp_server_timezone
)
1237 tz
= link
->network
->dhcp_server_timezone
;
1239 r
= get_timezone(&buffer
);
1241 log_warning_errno(r
, "Failed to determine timezone: %m");
1247 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1252 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1253 r
= sd_dhcp_server_start(link
->dhcp_server
);
1255 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1257 link_enter_failed(link
);
1263 log_link_debug(link
, "Offering DHCPv4 leases");
1266 if (link
->address_messages
== 0) {
1267 link
->addresses_configured
= true;
1268 link_check_ready(link
);
1270 log_link_debug(link
, "Setting addresses");
1275 static int link_set_bridge_vlan(Link
*link
) {
1278 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1280 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1285 static int link_set_proxy_arp(Link
*link
) {
1286 const char *p
= NULL
;
1289 if (!link_proxy_arp_enabled(link
))
1292 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1294 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1296 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1301 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1306 log_link_debug(link
, "Set link");
1308 r
= sd_netlink_message_get_errno(m
);
1309 if (r
< 0 && r
!= -EEXIST
) {
1310 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1311 link_enter_failed(link
);
1317 static int link_configure_after_setting_mtu(Link
*link
);
1319 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1324 assert(link
->ifname
);
1326 link
->setting_mtu
= false;
1328 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1331 r
= sd_netlink_message_get_errno(m
);
1333 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1337 log_link_debug(link
, "Setting MTU done.");
1339 if (link
->state
== LINK_STATE_PENDING
)
1340 (void) link_configure_after_setting_mtu(link
);
1345 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1346 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1350 assert(link
->manager
);
1351 assert(link
->manager
->rtnl
);
1353 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1356 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1358 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1360 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1362 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1363 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1364 (void) link_enable_ipv6(link
);
1366 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1367 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1368 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1370 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1371 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1376 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1378 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1380 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1381 link_netlink_destroy_callback
, link
);
1383 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1386 link
->setting_mtu
= true;
1391 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1396 assert(link
->ifname
);
1398 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1401 r
= sd_netlink_message_get_errno(m
);
1403 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1408 static int link_set_flags(Link
*link
) {
1409 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1410 unsigned ifi_change
= 0;
1411 unsigned ifi_flags
= 0;
1415 assert(link
->manager
);
1416 assert(link
->manager
->rtnl
);
1418 if (link
->flags
& IFF_LOOPBACK
)
1424 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1427 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1429 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1431 if (link
->network
->arp
>= 0) {
1432 ifi_change
|= IFF_NOARP
;
1433 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1436 if (link
->network
->multicast
>= 0) {
1437 ifi_change
|= IFF_MULTICAST
;
1438 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1441 if (link
->network
->allmulticast
>= 0) {
1442 ifi_change
|= IFF_ALLMULTI
;
1443 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1446 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1448 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1450 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1451 link_netlink_destroy_callback
, link
);
1453 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1460 static int link_set_bridge(Link
*link
) {
1461 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1465 assert(link
->network
);
1467 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1469 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1471 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1473 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1475 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1477 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1479 if (link
->network
->use_bpdu
>= 0) {
1480 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1482 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1485 if (link
->network
->hairpin
>= 0) {
1486 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1488 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1491 if (link
->network
->fast_leave
>= 0) {
1492 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1494 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1497 if (link
->network
->allow_port_to_be_root
>= 0) {
1498 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1500 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1504 if (link
->network
->unicast_flood
>= 0) {
1505 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1507 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1510 if (link
->network
->multicast_to_unicast
>= 0) {
1511 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1513 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1516 if (link
->network
->cost
!= 0) {
1517 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1519 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1522 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1523 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1525 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1528 r
= sd_netlink_message_close_container(req
);
1530 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1532 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1533 link_netlink_destroy_callback
, link
);
1535 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1542 static int link_bond_set(Link
*link
) {
1543 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1547 assert(link
->network
);
1549 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1551 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1553 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1555 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1557 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1559 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1561 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1563 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1565 if (link
->network
->active_slave
) {
1566 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1568 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1571 if (link
->network
->primary_slave
) {
1572 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1574 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1577 r
= sd_netlink_message_close_container(req
);
1579 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1581 r
= sd_netlink_message_close_container(req
);
1583 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1585 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1586 link_netlink_destroy_callback
, link
);
1588 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1595 static int link_lldp_save(Link
*link
) {
1596 _cleanup_free_
char *temp_path
= NULL
;
1597 _cleanup_fclose_
FILE *f
= NULL
;
1598 sd_lldp_neighbor
**l
= NULL
;
1602 assert(link
->lldp_file
);
1605 (void) unlink(link
->lldp_file
);
1609 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1613 (void) unlink(link
->lldp_file
);
1619 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1623 fchmod(fileno(f
), 0644);
1625 for (i
= 0; i
< n
; i
++) {
1630 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1635 (void) fwrite(&u
, 1, sizeof(u
), f
);
1636 (void) fwrite(p
, 1, sz
, f
);
1639 r
= fflush_and_check(f
);
1643 if (rename(temp_path
, link
->lldp_file
) < 0) {
1650 (void) unlink(link
->lldp_file
);
1652 (void) unlink(temp_path
);
1654 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1658 for (i
= 0; i
< n
; i
++)
1659 sd_lldp_neighbor_unref(l
[i
]);
1666 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1667 Link
*link
= userdata
;
1672 (void) link_lldp_save(link
);
1674 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1675 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1677 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1679 r
= link_lldp_emit_start(link
);
1681 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1685 static int link_acquire_ipv6_conf(Link
*link
) {
1690 if (link_ipv6_accept_ra_enabled(link
)) {
1691 assert(link
->ndisc
);
1693 log_link_debug(link
, "Discovering IPv6 routers");
1695 r
= sd_ndisc_start(link
->ndisc
);
1696 if (r
< 0 && r
!= -EBUSY
)
1697 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1700 if (link_radv_enabled(link
)) {
1702 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1704 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1706 r
= sd_radv_start(link
->radv
);
1707 if (r
< 0 && r
!= -EBUSY
)
1708 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1711 (void) dhcp6_request_prefix_delegation(link
);
1716 static int link_acquire_ipv4_conf(Link
*link
) {
1720 assert(link
->network
);
1721 assert(link
->manager
);
1722 assert(link
->manager
->event
);
1724 if (link_ipv4ll_enabled(link
)) {
1725 assert(link
->ipv4ll
);
1727 log_link_debug(link
, "Acquiring IPv4 link-local address");
1729 r
= sd_ipv4ll_start(link
->ipv4ll
);
1731 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1734 if (link_dhcp4_enabled(link
)) {
1735 assert(link
->dhcp_client
);
1737 log_link_debug(link
, "Acquiring DHCPv4 lease");
1739 r
= sd_dhcp_client_start(link
->dhcp_client
);
1741 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1747 static int link_acquire_conf(Link
*link
) {
1752 r
= link_acquire_ipv4_conf(link
);
1756 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1757 r
= link_acquire_ipv6_conf(link
);
1762 if (link_lldp_emit_enabled(link
)) {
1763 r
= link_lldp_emit_start(link
);
1765 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1771 bool link_has_carrier(Link
*link
) {
1772 /* see Documentation/networking/operstates.txt in the kernel sources */
1774 if (link
->kernel_operstate
== IF_OPER_UP
)
1777 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1778 /* operstate may not be implemented, so fall back to flags */
1779 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1785 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1790 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1793 r
= sd_netlink_message_get_errno(m
);
1795 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1800 static int link_configure_addrgen_mode(Link
*link
) {
1801 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1802 uint8_t ipv6ll_mode
;
1806 assert(link
->network
);
1807 assert(link
->manager
);
1808 assert(link
->manager
->rtnl
);
1810 log_link_debug(link
, "Setting address genmode for link");
1812 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1814 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1816 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1818 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1820 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1822 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1824 if (!link_ipv6ll_enabled(link
))
1825 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1827 const char *p
= NULL
;
1828 _cleanup_free_
char *stable_secret
= NULL
;
1830 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1832 /* The file may not exist. And event if it exists, when stable_secret is unset,
1833 * then reading the file fails and EIO is returned. */
1834 r
= read_one_line_file(p
, &stable_secret
);
1836 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1838 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1841 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1843 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1845 r
= sd_netlink_message_close_container(req
);
1847 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1849 r
= sd_netlink_message_close_container(req
);
1851 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1853 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1854 link_netlink_destroy_callback
, link
);
1856 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1863 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1868 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1871 r
= sd_netlink_message_get_errno(m
);
1873 /* we warn but don't fail the link, as it may be brought up later */
1874 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1879 static int link_up(Link
*link
) {
1880 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1884 assert(link
->network
);
1885 assert(link
->manager
);
1886 assert(link
->manager
->rtnl
);
1888 log_link_debug(link
, "Bringing link up");
1890 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1892 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1894 /* set it free if not enslaved with networkd */
1895 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1896 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1898 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1901 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1903 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1905 if (link
->network
->mac
) {
1906 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1908 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1911 if (link_ipv6_enabled(link
)) {
1912 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1914 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1916 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1917 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1919 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1921 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1922 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1924 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1927 r
= sd_netlink_message_close_container(req
);
1929 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1931 r
= sd_netlink_message_close_container(req
);
1933 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1936 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1937 link_netlink_destroy_callback
, link
);
1939 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1946 static int link_up_can(Link
*link
) {
1947 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1952 log_link_debug(link
, "Bringing CAN link up");
1954 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1956 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1958 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1960 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1962 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1963 link_netlink_destroy_callback
, link
);
1965 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1972 static int link_set_can(Link
*link
) {
1973 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1977 assert(link
->network
);
1978 assert(link
->manager
);
1979 assert(link
->manager
->rtnl
);
1981 log_link_debug(link
, "link_set_can");
1983 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1985 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1987 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1989 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1991 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1993 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1995 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1997 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1999 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2000 struct can_bittiming bt
= {
2001 .bitrate
= link
->network
->can_bitrate
,
2002 .sample_point
= link
->network
->can_sample_point
,
2005 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2006 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2010 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2011 if (link
->network
->can_sample_point
> 0)
2012 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2014 log_link_debug(link
, "Using default sample point");
2016 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2018 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2021 if (link
->network
->can_restart_us
> 0) {
2022 char time_string
[FORMAT_TIMESPAN_MAX
];
2023 uint64_t restart_ms
;
2025 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2028 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2030 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2032 if (restart_ms
> UINT32_MAX
) {
2033 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2037 log_link_debug(link
, "Setting restart = %s", time_string
);
2039 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2041 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2044 r
= sd_netlink_message_close_container(m
);
2046 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2048 r
= sd_netlink_message_close_container(m
);
2050 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2052 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2053 link_netlink_destroy_callback
, link
);
2055 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2059 if (!(link
->flags
& IFF_UP
)) {
2060 r
= link_up_can(link
);
2062 link_enter_failed(link
);
2067 log_link_debug(link
, "link_set_can done");
2072 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2077 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2080 r
= sd_netlink_message_get_errno(m
);
2082 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2084 if (streq_ptr(link
->kind
, "can"))
2090 int link_down(Link
*link
) {
2091 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2095 assert(link
->manager
);
2096 assert(link
->manager
->rtnl
);
2098 log_link_debug(link
, "Bringing link down");
2100 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2101 RTM_SETLINK
, link
->ifindex
);
2103 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2105 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2107 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2109 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2110 link_netlink_destroy_callback
, link
);
2112 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2119 static int link_handle_bound_to_list(Link
*link
) {
2123 bool required_up
= false;
2124 bool link_is_up
= false;
2128 if (hashmap_isempty(link
->bound_to_links
))
2131 if (link
->flags
& IFF_UP
)
2134 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2135 if (link_has_carrier(l
)) {
2140 if (!required_up
&& link_is_up
) {
2141 r
= link_down(link
);
2144 } else if (required_up
&& !link_is_up
) {
2153 static int link_handle_bound_by_list(Link
*link
) {
2160 if (hashmap_isempty(link
->bound_by_links
))
2163 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2164 r
= link_handle_bound_to_list(l
);
2172 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2178 if (link
== carrier
)
2181 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2184 r
= hashmap_ensure_allocated(h
, NULL
);
2188 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2195 static int link_new_bound_by_list(Link
*link
) {
2200 bool list_updated
= false;
2203 assert(link
->manager
);
2207 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2208 if (!carrier
->network
)
2211 if (strv_isempty(carrier
->network
->bind_carrier
))
2214 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2215 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2219 list_updated
= true;
2226 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2227 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2231 link_dirty(carrier
);
2237 static int link_new_bound_to_list(Link
*link
) {
2242 bool list_updated
= false;
2245 assert(link
->manager
);
2250 if (strv_isempty(link
->network
->bind_carrier
))
2255 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2256 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2257 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2261 list_updated
= true;
2268 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2269 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2273 link_dirty(carrier
);
2279 static int link_new_carrier_maps(Link
*link
) {
2282 r
= link_new_bound_by_list(link
);
2286 r
= link_handle_bound_by_list(link
);
2290 r
= link_new_bound_to_list(link
);
2294 r
= link_handle_bound_to_list(link
);
2301 static void link_free_bound_to_list(Link
*link
) {
2305 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2306 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2308 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2309 link_dirty(bound_to
);
2315 static void link_free_bound_by_list(Link
*link
) {
2319 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2320 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2322 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2323 link_dirty(bound_by
);
2324 link_handle_bound_to_list(bound_by
);
2331 static void link_free_carrier_maps(Link
*link
) {
2332 bool list_updated
= false;
2336 if (!hashmap_isempty(link
->bound_to_links
)) {
2337 link_free_bound_to_list(link
);
2338 list_updated
= true;
2341 if (!hashmap_isempty(link
->bound_by_links
)) {
2342 link_free_bound_by_list(link
);
2343 list_updated
= true;
2352 void link_drop(Link
*link
) {
2353 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2356 link_set_state(link
, LINK_STATE_LINGER
);
2358 link_free_carrier_maps(link
);
2360 log_link_debug(link
, "Link removed");
2362 (void) unlink(link
->state_file
);
2364 link_detach_from_manager(link
);
2371 static int link_joined(Link
*link
) {
2375 assert(link
->network
);
2377 if (!hashmap_isempty(link
->bound_to_links
)) {
2378 r
= link_handle_bound_to_list(link
);
2381 } else if (!(link
->flags
& IFF_UP
)) {
2384 link_enter_failed(link
);
2389 if (link
->network
->bridge
) {
2390 r
= link_set_bridge(link
);
2392 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2395 if (link
->network
->bond
) {
2396 r
= link_bond_set(link
);
2398 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2401 if (link
->network
->use_br_vlan
&&
2402 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2403 r
= link_set_bridge_vlan(link
);
2405 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2408 /* Skip setting up addresses until it gets carrier,
2409 or it would try to set addresses twice,
2410 which is bad for non-idempotent steps. */
2411 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2414 return link_request_set_addresses(link
);
2417 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2421 assert(link
->network
);
2425 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2428 r
= sd_netlink_message_get_errno(m
);
2429 if (r
< 0 && r
!= -EEXIST
) {
2430 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2431 link_enter_failed(link
);
2434 log_link_debug(link
, "Joined netdev");
2436 if (link
->enslaving
<= 0)
2442 static int link_enter_join_netdev(Link
*link
) {
2448 assert(link
->network
);
2449 assert(link
->state
== LINK_STATE_PENDING
);
2451 link_set_state(link
, LINK_STATE_CONFIGURING
);
2455 if (!link
->network
->bridge
&&
2456 !link
->network
->bond
&&
2457 !link
->network
->vrf
&&
2458 hashmap_isempty(link
->network
->stacked_netdevs
))
2459 return link_joined(link
);
2461 if (link
->network
->bond
) {
2462 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2463 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2464 return link_joined(link
);
2466 log_struct(LOG_DEBUG
,
2467 LOG_LINK_INTERFACE(link
),
2468 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2469 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2471 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2473 log_struct_errno(LOG_WARNING
, r
,
2474 LOG_LINK_INTERFACE(link
),
2475 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2476 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2477 link_enter_failed(link
);
2484 if (link
->network
->bridge
) {
2485 log_struct(LOG_DEBUG
,
2486 LOG_LINK_INTERFACE(link
),
2487 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2488 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2490 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2492 log_struct_errno(LOG_WARNING
, r
,
2493 LOG_LINK_INTERFACE(link
),
2494 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2495 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2496 link_enter_failed(link
);
2503 if (link
->network
->vrf
) {
2504 log_struct(LOG_DEBUG
,
2505 LOG_LINK_INTERFACE(link
),
2506 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2507 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2509 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2511 log_struct_errno(LOG_WARNING
, r
,
2512 LOG_LINK_INTERFACE(link
),
2513 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2514 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2515 link_enter_failed(link
);
2522 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2524 if (netdev
->ifindex
> 0) {
2529 log_struct(LOG_DEBUG
,
2530 LOG_LINK_INTERFACE(link
),
2531 LOG_NETDEV_INTERFACE(netdev
),
2532 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2534 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2536 log_struct_errno(LOG_WARNING
, r
,
2537 LOG_LINK_INTERFACE(link
),
2538 LOG_NETDEV_INTERFACE(netdev
),
2539 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2540 link_enter_failed(link
);
2550 static int link_set_ipv4_forward(Link
*link
) {
2553 if (!link_ipv4_forward_enabled(link
))
2556 /* We propagate the forwarding flag from one interface to the
2557 * global setting one way. This means: as long as at least one
2558 * interface was configured at any time that had IP forwarding
2559 * enabled the setting will stay on for good. We do this
2560 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2561 * somewhat in sync (see below). */
2563 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2565 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2570 static int link_set_ipv6_forward(Link
*link
) {
2573 if (!link_ipv6_forward_enabled(link
))
2576 /* On Linux, the IPv6 stack does not know a per-interface
2577 * packet forwarding setting: either packet forwarding is on
2578 * for all, or off for all. We hence don't bother with a
2579 * per-interface setting, but simply propagate the interface
2580 * flag, if it is set, to the global flag, one-way. Note that
2581 * while IPv4 would allow a per-interface flag, we expose the
2582 * same behaviour there and also propagate the setting from
2583 * one to all, to keep things simple (see above). */
2585 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2587 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2592 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2593 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2594 IPv6PrivacyExtensions s
;
2595 const char *p
= NULL
;
2598 s
= link_ipv6_privacy_extensions(link
);
2602 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2603 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2605 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2607 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2612 static int link_set_ipv6_accept_ra(Link
*link
) {
2613 const char *p
= NULL
;
2616 /* Make this a NOP if IPv6 is not available */
2617 if (!socket_ipv6_is_supported())
2620 if (link
->flags
& IFF_LOOPBACK
)
2626 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2628 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2629 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2631 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2636 static int link_set_ipv6_dad_transmits(Link
*link
) {
2637 char buf
[DECIMAL_STR_MAX(int) + 1];
2638 const char *p
= NULL
;
2641 /* Make this a NOP if IPv6 is not available */
2642 if (!socket_ipv6_is_supported())
2645 if (link
->flags
& IFF_LOOPBACK
)
2651 if (link
->network
->ipv6_dad_transmits
< 0)
2654 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2655 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2657 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2659 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2664 static int link_set_ipv6_hop_limit(Link
*link
) {
2665 char buf
[DECIMAL_STR_MAX(int) + 1];
2666 const char *p
= NULL
;
2669 /* Make this a NOP if IPv6 is not available */
2670 if (!socket_ipv6_is_supported())
2673 if (link
->flags
& IFF_LOOPBACK
)
2679 if (link
->network
->ipv6_hop_limit
< 0)
2682 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2683 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2685 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2687 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2692 static int link_set_ipv6_mtu(Link
*link
) {
2693 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2694 const char *p
= NULL
;
2697 /* Make this a NOP if IPv6 is not available */
2698 if (!socket_ipv6_is_supported())
2701 if (link
->flags
& IFF_LOOPBACK
)
2704 if (link
->network
->ipv6_mtu
== 0)
2707 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2709 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2711 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2713 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2718 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2719 Address
*net_address
;
2727 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2728 if (address_equal(net_address
, address
))
2734 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2743 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2744 if (route_equal(net_route
, route
))
2750 static int link_drop_foreign_config(Link
*link
) {
2756 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2757 /* we consider IPv6LL addresses to be managed by the kernel */
2758 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2761 if (link_is_static_address_configured(link
, address
)) {
2762 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2764 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2766 r
= address_remove(address
, link
, NULL
);
2772 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2773 /* do not touch routes managed by the kernel */
2774 if (route
->protocol
== RTPROT_KERNEL
)
2777 if (link_is_static_route_configured(link
, route
)) {
2778 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2782 r
= route_remove(route
, link
, NULL
);
2791 static int link_drop_config(Link
*link
) {
2792 Address
*address
, *pool_address
;
2797 SET_FOREACH(address
, link
->addresses
, i
) {
2798 /* we consider IPv6LL addresses to be managed by the kernel */
2799 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2802 r
= address_remove(address
, link
, NULL
);
2806 /* If this address came from an address pool, clean up the pool */
2807 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2808 if (address_equal(address
, pool_address
)) {
2809 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2810 address_free(pool_address
);
2816 SET_FOREACH(route
, link
->routes
, i
) {
2817 /* do not touch routes managed by the kernel */
2818 if (route
->protocol
== RTPROT_KERNEL
)
2821 r
= route_remove(route
, link
, NULL
);
2831 static int link_update_lldp(Link
*link
) {
2839 if (link
->flags
& IFF_UP
) {
2840 r
= sd_lldp_start(link
->lldp
);
2842 log_link_debug(link
, "Started LLDP.");
2844 r
= sd_lldp_stop(link
->lldp
);
2846 log_link_debug(link
, "Stopped LLDP.");
2852 static int link_configure_can(Link
*link
) {
2855 if (streq_ptr(link
->kind
, "can")) {
2856 /* The CAN interface must be down to configure bitrate, etc... */
2857 if ((link
->flags
& IFF_UP
)) {
2858 r
= link_down(link
);
2860 link_enter_failed(link
);
2867 return link_set_can(link
);
2870 if (!(link
->flags
& IFF_UP
)) {
2871 r
= link_up_can(link
);
2873 link_enter_failed(link
);
2881 static int link_configure(Link
*link
) {
2885 assert(link
->network
);
2886 assert(link
->state
== LINK_STATE_PENDING
);
2888 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2889 return link_configure_can(link
);
2891 /* Drop foreign config, but ignore loopback or critical devices.
2892 * We do not want to remove loopback address or addresses used for root NFS. */
2893 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2894 r
= link_drop_foreign_config(link
);
2899 r
= link_set_proxy_arp(link
);
2903 r
= ipv6_proxy_ndp_addresses_configure(link
);
2907 r
= link_set_ipv4_forward(link
);
2911 r
= link_set_ipv6_forward(link
);
2915 r
= link_set_ipv6_privacy_extensions(link
);
2919 r
= link_set_ipv6_accept_ra(link
);
2923 r
= link_set_ipv6_dad_transmits(link
);
2927 r
= link_set_ipv6_hop_limit(link
);
2931 r
= link_set_flags(link
);
2935 r
= link_set_ipv6_mtu(link
);
2939 if (link_ipv4ll_enabled(link
)) {
2940 r
= ipv4ll_configure(link
);
2945 if (link_dhcp4_enabled(link
)) {
2946 r
= dhcp4_set_promote_secondaries(link
);
2950 r
= dhcp4_configure(link
);
2955 if (link_dhcp4_server_enabled(link
)) {
2956 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2960 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2965 if (link_dhcp6_enabled(link
) ||
2966 link_ipv6_accept_ra_enabled(link
)) {
2967 r
= dhcp6_configure(link
);
2972 if (link_ipv6_accept_ra_enabled(link
)) {
2973 r
= ndisc_configure(link
);
2978 if (link_radv_enabled(link
)) {
2979 r
= radv_configure(link
);
2984 if (link_lldp_rx_enabled(link
)) {
2985 r
= sd_lldp_new(&link
->lldp
);
2989 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2993 r
= sd_lldp_match_capabilities(link
->lldp
,
2994 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2995 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2996 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3000 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3004 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3008 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3012 r
= link_update_lldp(link
);
3017 if (link
->network
->mtu
> 0) {
3018 r
= link_set_mtu(link
, link
->network
->mtu
);
3023 if (socket_ipv6_is_supported()) {
3024 r
= link_configure_addrgen_mode(link
);
3029 return link_configure_after_setting_mtu(link
);
3032 static int link_configure_after_setting_mtu(Link
*link
) {
3036 assert(link
->network
);
3037 assert(link
->state
== LINK_STATE_PENDING
);
3039 if (link
->setting_mtu
)
3042 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3043 r
= link_acquire_conf(link
);
3048 return link_enter_join_netdev(link
);
3051 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3054 if (duid
->raw_data_len
> 0)
3057 if (duid
->type
!= DUID_TYPE_UUID
)
3060 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3061 duid
->raw_data_len
= sizeof(sd_id128_t
);
3066 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3067 Manager
*manager
= userdata
;
3068 const sd_bus_error
*e
;
3078 e
= sd_bus_message_get_error(m
);
3080 log_error_errno(sd_bus_error_get_errno(e
),
3081 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3086 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3090 if (sz
!= sizeof(sd_id128_t
)) {
3091 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3095 memcpy(&manager
->product_uuid
, a
, sz
);
3096 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3097 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3099 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3102 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3103 r
= link_configure(link
);
3105 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3108 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3110 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3111 * even if the method fails. */
3112 manager
->has_product_uuid
= true;
3117 static bool link_requires_uuid(Link
*link
) {
3121 assert(link
->manager
);
3122 assert(link
->network
);
3124 duid
= link_get_duid(link
);
3125 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3128 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3131 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3137 static int link_configure_duid(Link
*link
) {
3143 assert(link
->manager
);
3144 assert(link
->network
);
3147 duid
= link_get_duid(link
);
3149 if (!link_requires_uuid(link
))
3152 if (m
->has_product_uuid
) {
3153 (void) duid_set_uuid(duid
, m
->product_uuid
);
3157 if (!m
->links_requesting_uuid
) {
3158 r
= manager_request_product_uuid(m
, link
);
3163 log_link_warning_errno(link
, r
,
3164 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3168 r
= set_put(m
->links_requesting_uuid
, link
);
3172 r
= set_put(m
->duids_requesting_uuid
, duid
);
3180 static int link_initialized_and_synced(Link
*link
) {
3185 assert(link
->ifname
);
3186 assert(link
->manager
);
3188 if (link
->state
!= LINK_STATE_PENDING
)
3191 log_link_debug(link
, "Link state is up-to-date");
3193 r
= link_new_bound_by_list(link
);
3197 r
= link_handle_bound_by_list(link
);
3201 if (!link
->network
) {
3202 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3203 &link
->mac
, &network
);
3205 link_enter_unmanaged(link
);
3207 } else if (r
== 0 && network
->unmanaged
) {
3208 link_enter_unmanaged(link
);
3213 if (link
->flags
& IFF_LOOPBACK
) {
3214 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3215 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3217 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3218 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3220 if (network
->dhcp_server
)
3221 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3224 r
= network_apply(network
, link
);
3229 r
= link_new_bound_to_list(link
);
3233 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3234 * link_configure() is called later asynchronously. */
3235 r
= link_configure_duid(link
);
3239 r
= link_configure(link
);
3246 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3247 (void) link_initialized_and_synced(link
);
3251 int link_initialized(Link
*link
, sd_device
*device
) {
3252 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3256 assert(link
->manager
);
3257 assert(link
->manager
->rtnl
);
3260 if (link
->state
!= LINK_STATE_PENDING
)
3263 if (link
->sd_device
)
3266 log_link_debug(link
, "udev initialized link");
3268 link
->sd_device
= sd_device_ref(device
);
3270 /* udev has initialized the link, but we don't know if we have yet
3271 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3272 * when it returns we know that the pending NEWLINKs have already been
3273 * processed and that we are up-to-date */
3275 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3280 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3281 link_netlink_destroy_callback
, link
);
3290 static int link_load(Link
*link
) {
3291 _cleanup_free_
char *network_file
= NULL
,
3294 *dhcp4_address
= NULL
,
3295 *ipv4ll_address
= NULL
;
3296 union in_addr_union address
;
3297 union in_addr_union route_dst
;
3303 r
= parse_env_file(NULL
, link
->state_file
,
3304 "NETWORK_FILE", &network_file
,
3305 "ADDRESSES", &addresses
,
3307 "DHCP4_ADDRESS", &dhcp4_address
,
3308 "IPV4LL_ADDRESS", &ipv4ll_address
);
3309 if (r
< 0 && r
!= -ENOENT
)
3310 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3317 suffix
= strrchr(network_file
, '.');
3319 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3320 goto network_file_fail
;
3324 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3326 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3327 goto network_file_fail
;
3330 r
= network_apply(network
, link
);
3332 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3341 _cleanup_free_
char *address_str
= NULL
;
3342 char *prefixlen_str
;
3344 unsigned char prefixlen
;
3346 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3348 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3354 prefixlen_str
= strchr(address_str
, '/');
3355 if (!prefixlen_str
) {
3356 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3360 *prefixlen_str
++ = '\0';
3362 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3364 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3368 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3370 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3374 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3376 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3385 _cleanup_free_
char *route_str
= NULL
;
3386 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3388 char *prefixlen_str
;
3390 unsigned char prefixlen
, tos
, table
;
3393 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3395 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3401 prefixlen_str
= strchr(route_str
, '/');
3402 if (!prefixlen_str
) {
3403 log_link_debug(link
, "Failed to parse route %s", route_str
);
3407 *prefixlen_str
++ = '\0';
3409 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3411 log_link_debug(link
,
3412 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3417 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3419 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3423 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3425 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3427 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3428 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3429 0, route_expire_handler
, route
);
3431 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3434 route
->lifetime
= lifetime
;
3435 sd_event_source_unref(route
->expire
);
3436 route
->expire
= TAKE_PTR(expire
);
3440 if (dhcp4_address
) {
3441 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3443 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3444 goto dhcp4_address_fail
;
3447 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3449 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3451 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3453 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3458 if (ipv4ll_address
) {
3459 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3461 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3462 goto ipv4ll_address_fail
;
3465 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3467 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3469 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3471 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3474 ipv4ll_address_fail
:
3479 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3480 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3481 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3490 r
= link_new(m
, message
, ret
);
3496 log_link_debug(link
, "Link %d added", link
->ifindex
);
3498 r
= link_load(link
);
3502 if (detect_container() <= 0) {
3503 /* not in a container, udev will be around */
3504 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3505 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3507 log_link_warning_errno(link
, r
, "Could not find device: %m");
3511 r
= sd_device_get_is_initialized(device
);
3513 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3518 log_link_debug(link
, "link pending udev initialization...");
3522 r
= link_initialized(link
, device
);
3526 r
= link_initialized_and_synced(link
);
3533 link_enter_failed(link
);
3537 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3542 log_link_info(link
, "Gained IPv6LL");
3544 link
->ipv6ll_address
= *address
;
3545 link_check_ready(link
);
3547 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3548 r
= link_acquire_ipv6_conf(link
);
3550 link_enter_failed(link
);
3558 static int link_carrier_gained(Link
*link
) {
3563 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3564 r
= link_acquire_conf(link
);
3566 link_enter_failed(link
);
3570 r
= link_request_set_addresses(link
);
3575 r
= link_handle_bound_by_list(link
);
3582 static int link_carrier_lost(Link
*link
) {
3587 if (link
->network
&& link
->network
->ignore_carrier_loss
)
3590 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3591 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3592 * configuration and stop the clients as well. */
3593 if (link
->setting_mtu
)
3596 r
= link_stop_clients(link
);
3598 link_enter_failed(link
);
3602 if (link_dhcp4_server_enabled(link
))
3603 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3605 r
= link_drop_config(link
);
3609 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3610 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3611 r
= link_drop_foreign_config(link
);
3616 r
= link_handle_bound_by_list(link
);
3623 int link_carrier_reset(Link
*link
) {
3628 if (link_has_carrier(link
)) {
3629 r
= link_carrier_lost(link
);
3633 r
= link_carrier_gained(link
);
3637 log_link_info(link
, "Reset carrier");
3643 int link_update(Link
*link
, sd_netlink_message
*m
) {
3644 struct ether_addr mac
;
3647 bool had_carrier
, carrier_gained
, carrier_lost
;
3651 assert(link
->ifname
);
3654 if (link
->state
== LINK_STATE_LINGER
) {
3655 log_link_info(link
, "Link readded");
3656 link_set_state(link
, LINK_STATE_CONFIGURING
);
3658 r
= link_new_carrier_maps(link
);
3663 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3664 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3665 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3667 if (link
->state
== LINK_STATE_PENDING
) {
3668 r
= free_and_strdup(&link
->ifname
, ifname
);
3672 Manager
*manager
= link
->manager
;
3675 r
= link_add(manager
, m
, &link
);
3681 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3682 if (r
>= 0 && mtu
> 0) {
3684 if (link
->original_mtu
== 0) {
3685 link
->original_mtu
= mtu
;
3686 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3689 if (link
->dhcp_client
) {
3690 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3693 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3697 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3699 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3703 /* The kernel may broadcast NEWLINK messages without the MAC address
3704 set, simply ignore them. */
3705 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3707 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3710 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3713 log_link_debug(link
, "MAC address: "
3714 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3715 mac
.ether_addr_octet
[0],
3716 mac
.ether_addr_octet
[1],
3717 mac
.ether_addr_octet
[2],
3718 mac
.ether_addr_octet
[3],
3719 mac
.ether_addr_octet
[4],
3720 mac
.ether_addr_octet
[5]);
3723 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3725 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3728 if (link
->dhcp_client
) {
3729 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3730 (const uint8_t *) &link
->mac
,
3734 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3736 r
= dhcp4_set_client_identifier(link
);
3741 if (link
->dhcp6_client
) {
3742 const DUID
* duid
= link_get_duid(link
);
3744 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3745 (const uint8_t *) &link
->mac
,
3749 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3751 if (link
->network
->iaid_set
) {
3752 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3753 link
->network
->iaid
);
3755 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3758 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3760 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3761 duid
->raw_data_len
);
3763 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3767 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3769 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3773 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3775 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3780 had_carrier
= link_has_carrier(link
);
3782 r
= link_update_flags(link
, m
);
3786 r
= link_update_lldp(link
);
3790 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3791 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3793 if (carrier_gained
) {
3794 log_link_info(link
, "Gained carrier");
3796 r
= link_carrier_gained(link
);
3799 } else if (carrier_lost
) {
3800 log_link_info(link
, "Lost carrier");
3802 r
= link_carrier_lost(link
);
3810 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3818 if (hashmap_isempty(h
))
3822 HASHMAP_FOREACH(link
, h
, i
) {
3826 fprintf(f
, "%i", link
->ifindex
);
3833 int link_save(Link
*link
) {
3834 _cleanup_free_
char *temp_path
= NULL
;
3835 _cleanup_fclose_
FILE *f
= NULL
;
3836 const char *admin_state
, *oper_state
;
3843 assert(link
->state_file
);
3844 assert(link
->lease_file
);
3845 assert(link
->manager
);
3847 if (link
->state
== LINK_STATE_LINGER
) {
3848 unlink(link
->state_file
);
3852 link_lldp_save(link
);
3854 admin_state
= link_state_to_string(link
->state
);
3855 assert(admin_state
);
3857 oper_state
= link_operstate_to_string(link
->operstate
);
3860 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3864 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3865 (void) fchmod(fileno(f
), 0644);
3868 "# This is private data. Do not parse.\n"
3871 admin_state
, oper_state
);
3873 if (link
->network
) {
3875 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3876 const char *dhcp_domainname
= NULL
;
3877 char **dhcp6_domains
= NULL
;
3878 char **dhcp_domains
= NULL
;
3881 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3882 yes_no(link
->network
->required_for_online
));
3884 if (link
->dhcp6_client
) {
3885 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3886 if (r
< 0 && r
!= -ENOMSG
)
3887 log_link_debug(link
, "No DHCPv6 lease");
3890 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3895 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3896 _cleanup_free_
char *b
= NULL
;
3898 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3899 &link
->network
->dns
[j
].address
, &b
);
3901 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3911 if (link
->network
->dhcp_use_dns
&&
3913 const struct in_addr
*addresses
;
3915 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3919 serialize_in_addrs(f
, addresses
, r
);
3924 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3925 struct in6_addr
*in6_addrs
;
3927 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3931 serialize_in6_addrs(f
, in6_addrs
, r
);
3936 /* Make sure to flush out old entries before we use the NDISC data */
3939 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3942 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3946 serialize_in6_addrs(f
, &dd
->address
, 1);
3955 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3957 if (link
->network
->dhcp_use_ntp
&&
3959 const struct in_addr
*addresses
;
3961 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3965 serialize_in_addrs(f
, addresses
, r
);
3970 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3971 struct in6_addr
*in6_addrs
;
3974 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3979 serialize_in6_addrs(f
, in6_addrs
, r
);
3983 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3985 fputstrv(f
, hosts
, NULL
, &space
);
3990 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3991 if (link
->dhcp_lease
) {
3992 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3993 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3996 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3999 fputs("DOMAINS=", f
);
4001 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
4003 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4006 if (dhcp_domainname
)
4007 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4009 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4011 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4013 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4014 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4019 fputs("ROUTE_DOMAINS=", f
);
4021 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
4023 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4026 if (dhcp_domainname
)
4027 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4029 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4031 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4033 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4034 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4039 fprintf(f
, "LLMNR=%s\n",
4040 resolve_support_to_string(link
->network
->llmnr
));
4041 fprintf(f
, "MDNS=%s\n",
4042 resolve_support_to_string(link
->network
->mdns
));
4043 if (link
->network
->dns_default_route
>= 0)
4044 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4046 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4047 fprintf(f
, "DNS_OVER_TLS=%s\n",
4048 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4050 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4051 fprintf(f
, "DNSSEC=%s\n",
4052 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4054 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4057 fputs("DNSSEC_NTA=", f
);
4059 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4060 fputs_with_space(f
, n
, NULL
, &space
);
4064 fputs("ADDRESSES=", f
);
4066 SET_FOREACH(a
, link
->addresses
, i
) {
4067 _cleanup_free_
char *address_str
= NULL
;
4069 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4073 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4078 fputs("ROUTES=", f
);
4080 SET_FOREACH(route
, link
->routes
, i
) {
4081 _cleanup_free_
char *route_str
= NULL
;
4083 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4087 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4088 space
? " " : "", route_str
,
4089 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4096 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4097 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4099 if (link
->dhcp_lease
) {
4100 struct in_addr address
;
4101 const char *tz
= NULL
;
4103 assert(link
->network
);
4105 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4107 fprintf(f
, "TIMEZONE=%s\n", tz
);
4109 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4111 fputs("DHCP4_ADDRESS=", f
);
4112 serialize_in_addrs(f
, &address
, 1);
4116 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4124 unlink(link
->lease_file
);
4127 struct in_addr address
;
4129 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4131 fputs("IPV4LL_ADDRESS=", f
);
4132 serialize_in_addrs(f
, &address
, 1);
4137 r
= fflush_and_check(f
);
4141 if (rename(temp_path
, link
->state_file
) < 0) {
4149 (void) unlink(link
->state_file
);
4151 (void) unlink(temp_path
);
4153 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4156 /* The serialized state in /run is no longer up-to-date. */
4157 void link_dirty(Link
*link
) {
4162 /* mark manager dirty as link is dirty */
4163 manager_dirty(link
->manager
);
4165 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4167 /* allocation errors are ignored */
4170 r
= set_put(link
->manager
->dirty_links
, link
);
4172 /* don't take another ref if the link was already dirty */
4178 /* The serialized state in /run is up-to-date */
4179 void link_clean(Link
*link
) {
4181 assert(link
->manager
);
4183 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4186 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4187 [LINK_STATE_PENDING
] = "pending",
4188 [LINK_STATE_CONFIGURING
] = "configuring",
4189 [LINK_STATE_CONFIGURED
] = "configured",
4190 [LINK_STATE_UNMANAGED
] = "unmanaged",
4191 [LINK_STATE_FAILED
] = "failed",
4192 [LINK_STATE_LINGER
] = "linger",
4195 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4197 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4198 [LINK_OPERSTATE_OFF
] = "off",
4199 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4200 [LINK_OPERSTATE_DORMANT
] = "dormant",
4201 [LINK_OPERSTATE_CARRIER
] = "carrier",
4202 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4203 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4206 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);