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 "netlink-util.h"
18 #include "network-internal.h"
19 #include "networkd-ipv6-proxy-ndp.h"
20 #include "networkd-lldp-tx.h"
21 #include "networkd-manager.h"
22 #include "networkd-ndisc.h"
23 #include "networkd-neighbor.h"
24 #include "networkd-radv.h"
25 #include "networkd-routing-policy-rule.h"
27 #include "socket-util.h"
28 #include "stdio-util.h"
29 #include "string-table.h"
31 #include "tmpfile-util.h"
35 DUID
* link_get_duid(Link
*link
) {
36 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
37 return &link
->network
->duid
;
39 return &link
->manager
->duid
;
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (!socket_ipv6_is_supported())
48 if (link
->flags
& IFF_LOOPBACK
)
54 if (link
->network
->bond
)
57 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
60 static bool link_dhcp4_enabled(Link
*link
) {
63 if (link
->flags
& IFF_LOOPBACK
)
69 if (link
->network
->bond
)
72 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
75 static bool link_dhcp4_server_enabled(Link
*link
) {
78 if (link
->flags
& IFF_LOOPBACK
)
84 if (link
->network
->bond
)
87 return link
->network
->dhcp_server
;
90 static bool link_ipv4ll_enabled(Link
*link
) {
93 if (link
->flags
& IFF_LOOPBACK
)
99 if (streq_ptr(link
->kind
, "wireguard"))
102 if (link
->network
->bond
)
105 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
108 static bool link_ipv6ll_enabled(Link
*link
) {
111 if (!socket_ipv6_is_supported())
114 if (link
->flags
& IFF_LOOPBACK
)
120 if (streq_ptr(link
->kind
, "wireguard"))
123 if (link
->network
->bond
)
126 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
129 static bool link_ipv6_enabled(Link
*link
) {
132 if (!socket_ipv6_is_supported())
135 if (link
->network
->bridge
|| link
->network
->bond
)
138 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
139 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
142 static bool link_radv_enabled(Link
*link
) {
145 if (!link_ipv6ll_enabled(link
))
148 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
151 static bool link_lldp_rx_enabled(Link
*link
) {
154 if (link
->flags
& IFF_LOOPBACK
)
157 if (link
->iftype
!= ARPHRD_ETHER
)
163 /* LLDP should be handled on bridge slaves as those have a direct
164 * connection to their peers not on the bridge master. Linux doesn't
165 * even (by default) forward lldp packets to the bridge master.*/
166 if (streq_ptr("bridge", link
->kind
))
169 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
172 static bool link_lldp_emit_enabled(Link
*link
) {
175 if (link
->flags
& IFF_LOOPBACK
)
178 if (link
->iftype
!= ARPHRD_ETHER
)
184 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
187 static bool link_ipv4_forward_enabled(Link
*link
) {
190 if (link
->flags
& IFF_LOOPBACK
)
196 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
199 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
202 static bool link_ipv6_forward_enabled(Link
*link
) {
205 if (!socket_ipv6_is_supported())
208 if (link
->flags
& IFF_LOOPBACK
)
214 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
217 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
220 static bool link_proxy_arp_enabled(Link
*link
) {
223 if (link
->flags
& IFF_LOOPBACK
)
229 if (link
->network
->proxy_arp
< 0)
235 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
238 if (!socket_ipv6_is_supported())
241 if (link
->flags
& IFF_LOOPBACK
)
247 if (!link_ipv6ll_enabled(link
))
250 /* If unset use system default (enabled if local forwarding is disabled.
251 * disabled if local forwarding is enabled).
252 * If set, ignore or enforce RA independent of local forwarding state.
254 if (link
->network
->ipv6_accept_ra
< 0)
255 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
256 return !link_ipv6_forward_enabled(link
);
257 else if (link
->network
->ipv6_accept_ra
> 0)
258 /* accept RA even if ip_forward is enabled */
265 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
268 if (!socket_ipv6_is_supported())
269 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
271 if (link
->flags
& IFF_LOOPBACK
)
272 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
275 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
277 return link
->network
->ipv6_privacy_extensions
;
280 static int link_enable_ipv6(Link
*link
) {
281 const char *p
= NULL
;
285 if (link
->flags
& IFF_LOOPBACK
)
288 disabled
= !link_ipv6_enabled(link
);
290 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
292 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
294 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
295 enable_disable(!disabled
), link
->ifname
);
297 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
302 void link_update_operstate(Link
*link
, bool also_update_bond_master
) {
303 LinkOperationalState operstate
;
307 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
308 operstate
= LINK_OPERSTATE_DORMANT
;
309 else if (link_has_carrier(link
)) {
311 uint8_t scope
= RT_SCOPE_NOWHERE
;
314 /* if we have carrier, check what addresses we have */
315 SET_FOREACH(address
, link
->addresses
, i
) {
316 if (!address_is_ready(address
))
319 if (address
->scope
< scope
)
320 scope
= address
->scope
;
323 /* for operstate we also take foreign addresses into account */
324 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
325 if (!address_is_ready(address
))
328 if (address
->scope
< scope
)
329 scope
= address
->scope
;
332 if (scope
< RT_SCOPE_SITE
)
333 /* universally accessible addresses found */
334 operstate
= LINK_OPERSTATE_ROUTABLE
;
335 else if (scope
< RT_SCOPE_HOST
)
336 /* only link or site local addresses found */
337 operstate
= LINK_OPERSTATE_DEGRADED
;
339 /* no useful addresses found */
340 operstate
= LINK_OPERSTATE_CARRIER
;
341 } else if (link
->flags
& IFF_UP
)
342 operstate
= LINK_OPERSTATE_NO_CARRIER
;
344 operstate
= LINK_OPERSTATE_OFF
;
346 if (IN_SET(operstate
, LINK_OPERSTATE_DEGRADED
, LINK_OPERSTATE_CARRIER
) &&
347 link
->flags
& IFF_SLAVE
)
348 operstate
= LINK_OPERSTATE_ENSLAVED
;
350 if (IN_SET(operstate
, LINK_OPERSTATE_CARRIER
, LINK_OPERSTATE_ENSLAVED
, LINK_OPERSTATE_ROUTABLE
) &&
351 !hashmap_isempty(link
->bond_slaves
)) {
355 HASHMAP_FOREACH(slave
, link
->bond_slaves
, i
) {
356 link_update_operstate(slave
, false);
358 if (IN_SET(slave
->operstate
,
359 LINK_OPERSTATE_OFF
, LINK_OPERSTATE_NO_CARRIER
, LINK_OPERSTATE_DORMANT
))
360 operstate
= LINK_OPERSTATE_DEGRADED
;
364 if (link
->operstate
!= operstate
) {
365 link
->operstate
= operstate
;
366 link_send_changed(link
, "OperationalState", NULL
);
370 if (also_update_bond_master
&& link
->network
&& link
->network
->bond
) {
373 if (link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
) < 0)
376 link_update_operstate(master
, true);
380 #define FLAG_STRING(string, flag, old, new) \
381 (((old ^ new) & flag) \
382 ? ((old & flag) ? (" -" string) : (" +" string)) \
385 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
386 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
392 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
394 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
396 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
398 /* if we got a message without operstate, take it to mean
399 the state was unchanged */
400 operstate
= link
->kernel_operstate
;
402 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
405 if (link
->flags
!= flags
) {
406 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",
407 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
408 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
409 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
410 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
411 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
412 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
413 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
414 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
415 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
416 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
417 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
418 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
419 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
420 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
421 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
422 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
423 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
424 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
425 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
427 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
428 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
429 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
430 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
431 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
432 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
433 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
434 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
436 /* link flags are currently at most 18 bits, let's align to
438 if (unknown_flags_added
)
440 "Unknown link flags gained: %#.5x (ignoring)",
441 unknown_flags_added
);
443 if (unknown_flags_removed
)
445 "Unknown link flags lost: %#.5x (ignoring)",
446 unknown_flags_removed
);
450 link
->kernel_operstate
= operstate
;
452 link_update_operstate(link
, true);
457 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
459 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
460 _cleanup_(link_unrefp
) Link
*link
= NULL
;
462 const char *ifname
, *kind
= NULL
;
464 unsigned short iftype
;
470 /* check for link kind */
471 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
473 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
474 r
= sd_netlink_message_exit_container(message
);
479 r
= sd_netlink_message_get_type(message
, &type
);
482 else if (type
!= RTM_NEWLINK
)
485 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
488 else if (ifindex
<= 0)
491 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
495 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
506 .state
= LINK_STATE_PENDING
,
507 .rtnl_extended_attrs
= true,
512 link
->ifname
= strdup(ifname
);
517 link
->kind
= strdup(kind
);
522 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
524 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
526 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
528 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
530 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
533 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
536 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
539 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
543 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
547 r
= link_update_flags(link
, message
);
551 *ret
= TAKE_PTR(link
);
556 static void link_detach_from_manager(Link
*link
) {
557 if (!link
|| !link
->manager
)
560 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
561 set_remove(link
->manager
->links_requesting_uuid
, link
);
565 static Link
*link_free(Link
*link
) {
573 while ((route
= set_first(link
->routes
)))
576 while ((route
= set_first(link
->routes_foreign
)))
579 link
->routes
= set_free(link
->routes
);
580 link
->routes_foreign
= set_free(link
->routes_foreign
);
582 while ((address
= set_first(link
->addresses
)))
583 address_free(address
);
585 while ((address
= set_first(link
->addresses_foreign
)))
586 address_free(address
);
588 link
->addresses
= set_free(link
->addresses
);
589 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
591 while ((address
= link
->pool_addresses
)) {
592 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
593 address_free(address
);
596 sd_dhcp_server_unref(link
->dhcp_server
);
597 sd_dhcp_client_unref(link
->dhcp_client
);
598 sd_dhcp_lease_unref(link
->dhcp_lease
);
600 link_lldp_emit_stop(link
);
602 free(link
->lease_file
);
604 sd_lldp_unref(link
->lldp
);
605 free(link
->lldp_file
);
609 sd_ipv4ll_unref(link
->ipv4ll
);
610 sd_dhcp6_client_unref(link
->dhcp6_client
);
611 sd_ndisc_unref(link
->ndisc
);
612 sd_radv_unref(link
->radv
);
614 link_detach_from_manager(link
);
620 (void) unlink(link
->state_file
);
621 free(link
->state_file
);
623 sd_device_unref(link
->sd_device
);
625 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
626 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
627 hashmap_free(link
->bound_to_links
);
629 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
630 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
631 hashmap_free(link
->bound_by_links
);
633 hashmap_free(link
->bond_slaves
);
638 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
640 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
647 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
656 static void link_set_state(Link
*link
, LinkState state
) {
659 if (link
->state
== state
)
664 link_send_changed(link
, "AdministrativeState", NULL
);
667 static void link_enter_unmanaged(Link
*link
) {
670 log_link_debug(link
, "Unmanaged");
672 link_set_state(link
, LINK_STATE_UNMANAGED
);
677 static int link_stop_clients(Link
*link
) {
681 assert(link
->manager
);
682 assert(link
->manager
->event
);
684 if (link
->dhcp_client
) {
685 k
= sd_dhcp_client_stop(link
->dhcp_client
);
687 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
691 k
= sd_ipv4ll_stop(link
->ipv4ll
);
693 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
696 if (link
->dhcp6_client
) {
697 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
699 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
703 k
= sd_ndisc_stop(link
->ndisc
);
705 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
709 k
= sd_radv_stop(link
->radv
);
711 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
714 link_lldp_emit_stop(link
);
718 void link_enter_failed(Link
*link
) {
721 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
724 log_link_warning(link
, "Failed");
726 link_set_state(link
, LINK_STATE_FAILED
);
728 link_stop_clients(link
);
733 static Address
* link_find_dhcp_server_address(Link
*link
) {
737 assert(link
->network
);
739 /* The first statically configured address if there is any */
740 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
742 if (address
->family
!= AF_INET
)
745 if (in_addr_is_null(address
->family
, &address
->in_addr
))
751 /* If that didn't work, find a suitable address we got from the pool */
752 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
753 if (address
->family
!= AF_INET
)
762 static void link_enter_configured(Link
*link
) {
764 assert(link
->network
);
766 if (link
->state
!= LINK_STATE_CONFIGURING
)
769 log_link_info(link
, "Configured");
771 link_set_state(link
, LINK_STATE_CONFIGURED
);
776 static int link_request_set_routing_policy_rule(Link
*link
) {
777 RoutingPolicyRule
*rule
, *rrule
= NULL
;
781 assert(link
->network
);
783 link_set_state(link
, LINK_STATE_CONFIGURING
);
784 link
->routing_policy_rules_configured
= false;
786 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
787 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
788 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
789 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
791 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
795 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
797 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
798 link_enter_failed(link
);
802 link
->routing_policy_rule_messages
++;
805 routing_policy_rule_purge(link
->manager
, link
);
806 if (link
->routing_policy_rule_messages
== 0) {
807 link
->routing_policy_rules_configured
= true;
808 link_check_ready(link
);
810 log_link_debug(link
, "Setting routing policy rules");
815 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
819 assert(link
->route_messages
> 0);
820 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
821 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
823 link
->route_messages
--;
825 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
828 r
= sd_netlink_message_get_errno(m
);
829 if (r
< 0 && r
!= -EEXIST
)
830 log_link_warning_errno(link
, r
, "Could not set route: %m");
832 if (link
->route_messages
== 0) {
833 log_link_debug(link
, "Routes set");
834 link
->static_routes_configured
= true;
835 link_check_ready(link
);
841 static int link_request_set_routes(Link
*link
) {
843 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
844 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
851 assert(link
->network
);
852 assert(link
->addresses_configured
);
853 assert(link
->address_messages
== 0);
854 assert(link
->state
!= _LINK_STATE_INVALID
);
856 link_set_state(link
, LINK_STATE_CONFIGURING
);
857 link
->static_routes_configured
= false;
859 r
= link_request_set_routing_policy_rule(link
);
863 /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
864 for (phase
= 0; phase
< _PHASE_MAX
; phase
++)
865 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
867 if (in_addr_is_null(rt
->family
, &rt
->gw
) != (phase
== PHASE_NON_GATEWAY
))
870 r
= route_configure(rt
, link
, route_handler
);
872 log_link_warning_errno(link
, r
, "Could not set routes: %m");
873 link_enter_failed(link
);
877 link
->route_messages
++;
880 if (link
->route_messages
== 0) {
881 link
->static_routes_configured
= true;
882 link_check_ready(link
);
884 log_link_debug(link
, "Setting routes");
889 void link_check_ready(Link
*link
) {
895 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
901 if (!link
->addresses_configured
)
904 if (!link
->neighbors_configured
)
907 SET_FOREACH(a
, link
->addresses
, i
)
908 if (!address_is_ready(a
))
911 if (!link
->addresses_ready
) {
912 link
->addresses_ready
= true;
913 link_request_set_routes(link
);
916 if (!link
->static_routes_configured
)
919 if (!link
->routing_policy_rules_configured
)
922 if (link_ipv4ll_enabled(link
))
923 if (!link
->ipv4ll_address
||
927 if (!link
->network
->bridge
) {
929 if (link_ipv6ll_enabled(link
) &&
930 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
))
933 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
934 !link
->dhcp4_configured
) ||
935 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
936 !link
->dhcp6_configured
) ||
937 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
938 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
941 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
945 if (link
->state
!= LINK_STATE_CONFIGURED
)
946 link_enter_configured(link
);
951 static int link_request_set_neighbors(Link
*link
) {
956 assert(link
->network
);
957 assert(link
->state
!= _LINK_STATE_INVALID
);
959 link_set_state(link
, LINK_STATE_CONFIGURING
);
960 link
->neighbors_configured
= false;
962 LIST_FOREACH(neighbors
, neighbor
, link
->network
->neighbors
) {
963 r
= neighbor_configure(neighbor
, link
, NULL
);
965 log_link_warning_errno(link
, r
, "Could not set neighbor: %m");
966 link_enter_failed(link
);
971 if (link
->neighbor_messages
== 0) {
972 link
->neighbors_configured
= true;
973 link_check_ready(link
);
975 log_link_debug(link
, "Setting neighbors");
980 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
986 assert(link
->ifname
);
987 assert(link
->address_messages
> 0);
988 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
989 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
991 link
->address_messages
--;
993 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
996 r
= sd_netlink_message_get_errno(m
);
997 if (r
< 0 && r
!= -EEXIST
)
998 log_link_warning_errno(link
, r
, "could not set address: %m");
1000 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
1002 if (link
->address_messages
== 0) {
1003 log_link_debug(link
, "Addresses set");
1004 link
->addresses_configured
= true;
1005 link_check_ready(link
);
1011 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1012 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1013 size_t n_addresses
= 0, n_allocated
= 0;
1016 log_debug("Copying DNS server information from %s", link
->ifname
);
1021 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
1024 /* Only look for IPv4 addresses */
1025 if (link
->network
->dns
[i
].family
!= AF_INET
)
1028 ia
= link
->network
->dns
[i
].address
.in
;
1030 /* Never propagate obviously borked data */
1031 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1034 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1037 addresses
[n_addresses
++] = ia
;
1040 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
1041 const struct in_addr
*da
= NULL
;
1044 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
1047 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1050 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1055 if (n_addresses
<= 0)
1058 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1061 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1062 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1063 size_t n_addresses
= 0, n_allocated
= 0;
1069 log_debug("Copying NTP server information from %s", link
->ifname
);
1071 STRV_FOREACH(a
, link
->network
->ntp
) {
1074 /* Only look for IPv4 addresses */
1075 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1078 /* Never propagate obviously borked data */
1079 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1082 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1085 addresses
[n_addresses
++] = ia
;
1088 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1089 const struct in_addr
*da
= NULL
;
1092 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1095 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1098 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1103 if (n_addresses
<= 0)
1106 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1109 static int link_set_bridge_fdb(Link
*link
) {
1110 FdbEntry
*fdb_entry
;
1113 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1114 r
= fdb_entry_configure(link
, fdb_entry
);
1116 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1122 static int link_request_set_addresses(Link
*link
) {
1123 AddressLabel
*label
;
1128 assert(link
->network
);
1129 assert(link
->state
!= _LINK_STATE_INVALID
);
1131 link_set_state(link
, LINK_STATE_CONFIGURING
);
1133 /* Reset all *_configured flags we are configuring. */
1134 link
->addresses_configured
= false;
1135 link
->addresses_ready
= false;
1136 link
->neighbors_configured
= false;
1137 link
->static_routes_configured
= false;
1138 link
->routing_policy_rules_configured
= false;
1140 r
= link_set_bridge_fdb(link
);
1144 r
= link_request_set_neighbors(link
);
1148 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1151 update
= address_get(link
, ad
->family
, &ad
->in_addr
, ad
->prefixlen
, NULL
) > 0;
1153 r
= address_configure(ad
, link
, address_handler
, update
);
1155 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1156 link_enter_failed(link
);
1160 link
->address_messages
++;
1163 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1164 r
= address_label_configure(label
, link
, NULL
, false);
1166 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1167 link_enter_failed(link
);
1171 link
->address_label_messages
++;
1174 /* now that we can figure out a default address for the dhcp server,
1176 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1178 Link
*uplink
= NULL
;
1179 bool acquired_uplink
= false;
1181 address
= link_find_dhcp_server_address(link
);
1183 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1184 link_enter_failed(link
);
1188 /* use the server address' subnet as the pool */
1189 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1190 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1195 r = sd_dhcp_server_set_router(link->dhcp_server,
1196 &main_address->in_addr.in);
1201 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1202 r
= sd_dhcp_server_set_max_lease_time(
1204 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1209 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1210 r
= sd_dhcp_server_set_default_lease_time(
1212 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1217 if (link
->network
->dhcp_server_emit_dns
) {
1219 if (link
->network
->n_dhcp_server_dns
> 0)
1220 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1222 uplink
= manager_find_uplink(link
->manager
, link
);
1223 acquired_uplink
= true;
1226 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1229 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1232 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1235 if (link
->network
->dhcp_server_emit_ntp
) {
1237 if (link
->network
->n_dhcp_server_ntp
> 0)
1238 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1240 if (!acquired_uplink
)
1241 uplink
= manager_find_uplink(link
->manager
, link
);
1244 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1247 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1251 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1254 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1256 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1258 if (link
->network
->dhcp_server_emit_timezone
) {
1259 _cleanup_free_
char *buffer
= NULL
;
1260 const char *tz
= NULL
;
1262 if (link
->network
->dhcp_server_timezone
)
1263 tz
= link
->network
->dhcp_server_timezone
;
1265 r
= get_timezone(&buffer
);
1267 log_warning_errno(r
, "Failed to determine timezone: %m");
1273 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1278 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
1279 r
= sd_dhcp_server_start(link
->dhcp_server
);
1281 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1283 link_enter_failed(link
);
1289 log_link_debug(link
, "Offering DHCPv4 leases");
1292 if (link
->address_messages
== 0) {
1293 link
->addresses_configured
= true;
1294 link_check_ready(link
);
1296 log_link_debug(link
, "Setting addresses");
1301 static int link_set_bridge_vlan(Link
*link
) {
1304 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1306 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1311 static int link_set_proxy_arp(Link
*link
) {
1312 const char *p
= NULL
;
1315 if (!link_proxy_arp_enabled(link
))
1318 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1320 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1322 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1327 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1332 log_link_debug(link
, "Set link");
1334 r
= sd_netlink_message_get_errno(m
);
1335 if (r
< 0 && r
!= -EEXIST
) {
1336 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1337 link_enter_failed(link
);
1343 static int link_configure_after_setting_mtu(Link
*link
);
1345 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1350 assert(link
->ifname
);
1352 link
->setting_mtu
= false;
1354 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1357 r
= sd_netlink_message_get_errno(m
);
1359 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1363 log_link_debug(link
, "Setting MTU done.");
1365 if (link
->state
== LINK_STATE_PENDING
)
1366 (void) link_configure_after_setting_mtu(link
);
1371 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1372 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1376 assert(link
->manager
);
1377 assert(link
->manager
->rtnl
);
1379 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1382 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1384 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1386 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1388 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1389 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1390 (void) link_enable_ipv6(link
);
1392 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1393 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1394 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1396 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1397 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1402 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1404 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1406 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1407 link_netlink_destroy_callback
, link
);
1409 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1412 link
->setting_mtu
= true;
1417 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1422 assert(link
->ifname
);
1424 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1427 r
= sd_netlink_message_get_errno(m
);
1429 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1434 static int link_set_flags(Link
*link
) {
1435 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1436 unsigned ifi_change
= 0;
1437 unsigned ifi_flags
= 0;
1441 assert(link
->manager
);
1442 assert(link
->manager
->rtnl
);
1444 if (link
->flags
& IFF_LOOPBACK
)
1450 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1453 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1455 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1457 if (link
->network
->arp
>= 0) {
1458 ifi_change
|= IFF_NOARP
;
1459 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1462 if (link
->network
->multicast
>= 0) {
1463 ifi_change
|= IFF_MULTICAST
;
1464 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1467 if (link
->network
->allmulticast
>= 0) {
1468 ifi_change
|= IFF_ALLMULTI
;
1469 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1472 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1474 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1476 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1477 link_netlink_destroy_callback
, link
);
1479 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1486 static int link_set_bridge(Link
*link
) {
1487 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1491 assert(link
->network
);
1493 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1495 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1497 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1499 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1501 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1503 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1505 if (link
->network
->use_bpdu
>= 0) {
1506 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1508 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1511 if (link
->network
->hairpin
>= 0) {
1512 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1514 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1517 if (link
->network
->fast_leave
>= 0) {
1518 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1520 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1523 if (link
->network
->allow_port_to_be_root
>= 0) {
1524 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1526 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1530 if (link
->network
->unicast_flood
>= 0) {
1531 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1533 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1536 if (link
->network
->multicast_to_unicast
>= 0) {
1537 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
1539 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
1542 if (link
->network
->cost
!= 0) {
1543 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1545 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1548 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1549 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1551 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1554 r
= sd_netlink_message_close_container(req
);
1556 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1558 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1559 link_netlink_destroy_callback
, link
);
1561 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1568 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1573 assert(link
->ifname
);
1575 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1578 r
= sd_netlink_message_get_errno(m
);
1580 log_link_warning_errno(link
, r
, "Could not set bonding interface: %m");
1587 static int link_set_bond(Link
*link
) {
1588 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1592 assert(link
->network
);
1594 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1596 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1598 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1600 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1602 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1604 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1606 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1608 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1610 if (link
->network
->active_slave
) {
1611 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1613 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1616 if (link
->network
->primary_slave
) {
1617 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1619 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1622 r
= sd_netlink_message_close_container(req
);
1624 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1626 r
= sd_netlink_message_close_container(req
);
1628 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1630 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_bond_handler
,
1631 link_netlink_destroy_callback
, link
);
1633 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1640 static int link_append_bond_slave(Link
*link
) {
1645 assert(link
->network
);
1646 assert(link
->network
->bond
);
1648 r
= link_get(link
->manager
, link
->network
->bond
->ifindex
, &master
);
1652 r
= hashmap_ensure_allocated(&master
->bond_slaves
, NULL
);
1656 r
= hashmap_put(master
->bond_slaves
, INT_TO_PTR(link
->ifindex
), link
);
1663 static int link_lldp_save(Link
*link
) {
1664 _cleanup_free_
char *temp_path
= NULL
;
1665 _cleanup_fclose_
FILE *f
= NULL
;
1666 sd_lldp_neighbor
**l
= NULL
;
1670 assert(link
->lldp_file
);
1673 (void) unlink(link
->lldp_file
);
1677 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1681 (void) unlink(link
->lldp_file
);
1687 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1691 fchmod(fileno(f
), 0644);
1693 for (i
= 0; i
< n
; i
++) {
1698 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1703 (void) fwrite(&u
, 1, sizeof(u
), f
);
1704 (void) fwrite(p
, 1, sz
, f
);
1707 r
= fflush_and_check(f
);
1711 if (rename(temp_path
, link
->lldp_file
) < 0) {
1718 (void) unlink(link
->lldp_file
);
1720 (void) unlink(temp_path
);
1722 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1726 for (i
= 0; i
< n
; i
++)
1727 sd_lldp_neighbor_unref(l
[i
]);
1734 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1735 Link
*link
= userdata
;
1740 (void) link_lldp_save(link
);
1742 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1743 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1745 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1747 r
= link_lldp_emit_start(link
);
1749 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1753 static int link_acquire_ipv6_conf(Link
*link
) {
1758 if (link_ipv6_accept_ra_enabled(link
)) {
1759 assert(link
->ndisc
);
1761 log_link_debug(link
, "Discovering IPv6 routers");
1763 r
= sd_ndisc_start(link
->ndisc
);
1764 if (r
< 0 && r
!= -EBUSY
)
1765 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1768 if (link_radv_enabled(link
)) {
1770 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1772 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1774 r
= sd_radv_start(link
->radv
);
1775 if (r
< 0 && r
!= -EBUSY
)
1776 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1779 (void) dhcp6_request_prefix_delegation(link
);
1784 static int link_acquire_ipv4_conf(Link
*link
) {
1788 assert(link
->network
);
1789 assert(link
->manager
);
1790 assert(link
->manager
->event
);
1792 if (link_ipv4ll_enabled(link
)) {
1793 assert(link
->ipv4ll
);
1795 log_link_debug(link
, "Acquiring IPv4 link-local address");
1797 r
= sd_ipv4ll_start(link
->ipv4ll
);
1799 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1802 if (link_dhcp4_enabled(link
)) {
1803 assert(link
->dhcp_client
);
1805 log_link_debug(link
, "Acquiring DHCPv4 lease");
1807 r
= sd_dhcp_client_start(link
->dhcp_client
);
1809 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1815 static int link_acquire_conf(Link
*link
) {
1820 r
= link_acquire_ipv4_conf(link
);
1824 if (!in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
)) {
1825 r
= link_acquire_ipv6_conf(link
);
1830 if (link_lldp_emit_enabled(link
)) {
1831 r
= link_lldp_emit_start(link
);
1833 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1839 bool link_has_carrier(Link
*link
) {
1840 /* see Documentation/networking/operstates.txt in the kernel sources */
1842 if (link
->kernel_operstate
== IF_OPER_UP
)
1845 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1846 /* operstate may not be implemented, so fall back to flags */
1847 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1853 static int link_address_genmode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1858 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1861 r
= sd_netlink_message_get_errno(m
);
1863 log_link_warning_errno(link
, r
, "Could not set address genmode for interface: %m");
1868 static int link_configure_addrgen_mode(Link
*link
) {
1869 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1870 uint8_t ipv6ll_mode
;
1874 assert(link
->network
);
1875 assert(link
->manager
);
1876 assert(link
->manager
->rtnl
);
1878 log_link_debug(link
, "Setting address genmode for link");
1880 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1882 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1884 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1886 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1888 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1890 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1892 if (!link_ipv6ll_enabled(link
))
1893 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1895 const char *p
= NULL
;
1896 _cleanup_free_
char *stable_secret
= NULL
;
1898 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1900 /* The file may not exist. And event if it exists, when stable_secret is unset,
1901 * then reading the file fails and EIO is returned. */
1902 r
= read_one_line_file(p
, &stable_secret
);
1904 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1906 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1909 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1911 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1913 r
= sd_netlink_message_close_container(req
);
1915 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1917 r
= sd_netlink_message_close_container(req
);
1919 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1921 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_address_genmode_handler
,
1922 link_netlink_destroy_callback
, link
);
1924 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1931 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1936 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1939 r
= sd_netlink_message_get_errno(m
);
1941 /* we warn but don't fail the link, as it may be brought up later */
1942 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1947 static int link_up(Link
*link
) {
1948 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1952 assert(link
->network
);
1953 assert(link
->manager
);
1954 assert(link
->manager
->rtnl
);
1956 log_link_debug(link
, "Bringing link up");
1958 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1960 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1962 /* set it free if not enslaved with networkd */
1963 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1964 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1966 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1969 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1971 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1973 if (link
->network
->mac
) {
1974 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1976 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1979 if (link_ipv6_enabled(link
)) {
1980 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1982 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1984 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1985 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1987 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1989 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1990 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1992 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1995 r
= sd_netlink_message_close_container(req
);
1997 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1999 r
= sd_netlink_message_close_container(req
);
2001 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
2004 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2005 link_netlink_destroy_callback
, link
);
2007 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2014 static int link_up_can(Link
*link
) {
2015 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2020 log_link_debug(link
, "Bringing CAN link up");
2022 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
2024 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2026 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
2028 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2030 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
2031 link_netlink_destroy_callback
, link
);
2033 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2040 static int link_set_can(Link
*link
) {
2041 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
2045 assert(link
->network
);
2046 assert(link
->manager
);
2047 assert(link
->manager
->rtnl
);
2049 log_link_debug(link
, "link_set_can");
2051 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
2053 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
2055 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
2057 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
2059 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
2061 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
2063 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
2065 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
2067 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
2068 struct can_bittiming bt
= {
2069 .bitrate
= link
->network
->can_bitrate
,
2070 .sample_point
= link
->network
->can_sample_point
,
2073 if (link
->network
->can_bitrate
> UINT32_MAX
) {
2074 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
2078 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
2079 if (link
->network
->can_sample_point
> 0)
2080 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
2082 log_link_debug(link
, "Using default sample point");
2084 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
2086 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
2089 if (link
->network
->can_restart_us
> 0) {
2090 char time_string
[FORMAT_TIMESPAN_MAX
];
2091 uint64_t restart_ms
;
2093 if (link
->network
->can_restart_us
== USEC_INFINITY
)
2096 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
2098 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
2100 if (restart_ms
> UINT32_MAX
) {
2101 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
2105 log_link_debug(link
, "Setting restart = %s", time_string
);
2107 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
2109 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
2112 r
= sd_netlink_message_close_container(m
);
2114 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2116 r
= sd_netlink_message_close_container(m
);
2118 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
2120 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
2121 link_netlink_destroy_callback
, link
);
2123 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2127 if (!(link
->flags
& IFF_UP
)) {
2128 r
= link_up_can(link
);
2130 link_enter_failed(link
);
2135 log_link_debug(link
, "link_set_can done");
2140 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2145 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2148 r
= sd_netlink_message_get_errno(m
);
2150 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
2152 if (streq_ptr(link
->kind
, "can"))
2158 int link_down(Link
*link
) {
2159 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2163 assert(link
->manager
);
2164 assert(link
->manager
->rtnl
);
2166 log_link_debug(link
, "Bringing link down");
2168 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2169 RTM_SETLINK
, link
->ifindex
);
2171 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2173 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2175 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2177 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
2178 link_netlink_destroy_callback
, link
);
2180 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2187 static int link_handle_bound_to_list(Link
*link
) {
2191 bool required_up
= false;
2192 bool link_is_up
= false;
2196 if (hashmap_isempty(link
->bound_to_links
))
2199 if (link
->flags
& IFF_UP
)
2202 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2203 if (link_has_carrier(l
)) {
2208 if (!required_up
&& link_is_up
) {
2209 r
= link_down(link
);
2212 } else if (required_up
&& !link_is_up
) {
2221 static int link_handle_bound_by_list(Link
*link
) {
2228 if (hashmap_isempty(link
->bound_by_links
))
2231 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2232 r
= link_handle_bound_to_list(l
);
2240 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2246 if (link
== carrier
)
2249 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2252 r
= hashmap_ensure_allocated(h
, NULL
);
2256 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2263 static int link_new_bound_by_list(Link
*link
) {
2268 bool list_updated
= false;
2271 assert(link
->manager
);
2275 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2276 if (!carrier
->network
)
2279 if (strv_isempty(carrier
->network
->bind_carrier
))
2282 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2283 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2287 list_updated
= true;
2294 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2295 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2299 link_dirty(carrier
);
2305 static int link_new_bound_to_list(Link
*link
) {
2310 bool list_updated
= false;
2313 assert(link
->manager
);
2318 if (strv_isempty(link
->network
->bind_carrier
))
2323 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2324 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2325 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2329 list_updated
= true;
2336 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2337 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2341 link_dirty(carrier
);
2347 static int link_new_carrier_maps(Link
*link
) {
2350 r
= link_new_bound_by_list(link
);
2354 r
= link_handle_bound_by_list(link
);
2358 r
= link_new_bound_to_list(link
);
2362 r
= link_handle_bound_to_list(link
);
2369 static void link_free_bound_to_list(Link
*link
) {
2373 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2374 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2376 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2377 link_dirty(bound_to
);
2383 static void link_free_bound_by_list(Link
*link
) {
2387 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2388 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2390 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2391 link_dirty(bound_by
);
2392 link_handle_bound_to_list(bound_by
);
2399 static void link_free_carrier_maps(Link
*link
) {
2400 bool list_updated
= false;
2404 if (!hashmap_isempty(link
->bound_to_links
)) {
2405 link_free_bound_to_list(link
);
2406 list_updated
= true;
2409 if (!hashmap_isempty(link
->bound_by_links
)) {
2410 link_free_bound_by_list(link
);
2411 list_updated
= true;
2420 void link_drop(Link
*link
) {
2421 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2424 link_set_state(link
, LINK_STATE_LINGER
);
2426 link_free_carrier_maps(link
);
2428 log_link_debug(link
, "Link removed");
2430 (void) unlink(link
->state_file
);
2432 link_detach_from_manager(link
);
2439 static int link_joined(Link
*link
) {
2443 assert(link
->network
);
2445 if (!hashmap_isempty(link
->bound_to_links
)) {
2446 r
= link_handle_bound_to_list(link
);
2449 } else if (!(link
->flags
& IFF_UP
)) {
2452 link_enter_failed(link
);
2457 if (link
->network
->bridge
) {
2458 r
= link_set_bridge(link
);
2460 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2463 if (link
->network
->bond
) {
2464 r
= link_set_bond(link
);
2466 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2468 r
= link_append_bond_slave(link
);
2470 log_link_error_errno(link
, r
, "Failed to add to bond master's slave list: %m");
2473 if (link
->network
->use_br_vlan
&&
2474 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2475 r
= link_set_bridge_vlan(link
);
2477 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2480 /* Skip setting up addresses until it gets carrier,
2481 or it would try to set addresses twice,
2482 which is bad for non-idempotent steps. */
2483 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2486 return link_request_set_addresses(link
);
2489 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2493 assert(link
->network
);
2497 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2500 r
= sd_netlink_message_get_errno(m
);
2501 if (r
< 0 && r
!= -EEXIST
) {
2502 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2503 link_enter_failed(link
);
2506 log_link_debug(link
, "Joined netdev");
2508 if (link
->enslaving
<= 0)
2514 static int link_enter_join_netdev(Link
*link
) {
2520 assert(link
->network
);
2521 assert(link
->state
== LINK_STATE_PENDING
);
2523 link_set_state(link
, LINK_STATE_CONFIGURING
);
2527 if (!link
->network
->bridge
&&
2528 !link
->network
->bond
&&
2529 !link
->network
->vrf
&&
2530 hashmap_isempty(link
->network
->stacked_netdevs
))
2531 return link_joined(link
);
2533 if (link
->network
->bond
) {
2534 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2535 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2536 return link_joined(link
);
2538 log_struct(LOG_DEBUG
,
2539 LOG_LINK_INTERFACE(link
),
2540 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2541 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2543 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2545 log_struct_errno(LOG_WARNING
, r
,
2546 LOG_LINK_INTERFACE(link
),
2547 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2548 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2549 link_enter_failed(link
);
2556 if (link
->network
->bridge
) {
2557 log_struct(LOG_DEBUG
,
2558 LOG_LINK_INTERFACE(link
),
2559 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2560 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2562 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2564 log_struct_errno(LOG_WARNING
, r
,
2565 LOG_LINK_INTERFACE(link
),
2566 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2567 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2568 link_enter_failed(link
);
2575 if (link
->network
->vrf
) {
2576 log_struct(LOG_DEBUG
,
2577 LOG_LINK_INTERFACE(link
),
2578 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2579 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2581 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2583 log_struct_errno(LOG_WARNING
, r
,
2584 LOG_LINK_INTERFACE(link
),
2585 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2586 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2587 link_enter_failed(link
);
2594 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2596 if (netdev
->ifindex
> 0) {
2601 log_struct(LOG_DEBUG
,
2602 LOG_LINK_INTERFACE(link
),
2603 LOG_NETDEV_INTERFACE(netdev
),
2604 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2606 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2608 log_struct_errno(LOG_WARNING
, r
,
2609 LOG_LINK_INTERFACE(link
),
2610 LOG_NETDEV_INTERFACE(netdev
),
2611 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2612 link_enter_failed(link
);
2622 static int link_set_ipv4_forward(Link
*link
) {
2625 if (!link_ipv4_forward_enabled(link
))
2628 /* We propagate the forwarding flag from one interface to the
2629 * global setting one way. This means: as long as at least one
2630 * interface was configured at any time that had IP forwarding
2631 * enabled the setting will stay on for good. We do this
2632 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2633 * somewhat in sync (see below). */
2635 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2637 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2642 static int link_set_ipv6_forward(Link
*link
) {
2645 if (!link_ipv6_forward_enabled(link
))
2648 /* On Linux, the IPv6 stack does not know a per-interface
2649 * packet forwarding setting: either packet forwarding is on
2650 * for all, or off for all. We hence don't bother with a
2651 * per-interface setting, but simply propagate the interface
2652 * flag, if it is set, to the global flag, one-way. Note that
2653 * while IPv4 would allow a per-interface flag, we expose the
2654 * same behaviour there and also propagate the setting from
2655 * one to all, to keep things simple (see above). */
2657 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2659 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2664 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2665 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2666 IPv6PrivacyExtensions s
;
2667 const char *p
= NULL
;
2670 s
= link_ipv6_privacy_extensions(link
);
2674 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2675 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2677 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2679 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2684 static int link_set_ipv6_accept_ra(Link
*link
) {
2685 const char *p
= NULL
;
2688 /* Make this a NOP if IPv6 is not available */
2689 if (!socket_ipv6_is_supported())
2692 if (link
->flags
& IFF_LOOPBACK
)
2698 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2700 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2701 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2703 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2708 static int link_set_ipv6_dad_transmits(Link
*link
) {
2709 char buf
[DECIMAL_STR_MAX(int) + 1];
2710 const char *p
= NULL
;
2713 /* Make this a NOP if IPv6 is not available */
2714 if (!socket_ipv6_is_supported())
2717 if (link
->flags
& IFF_LOOPBACK
)
2723 if (link
->network
->ipv6_dad_transmits
< 0)
2726 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2727 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2729 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2731 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2736 static int link_set_ipv6_hop_limit(Link
*link
) {
2737 char buf
[DECIMAL_STR_MAX(int) + 1];
2738 const char *p
= NULL
;
2741 /* Make this a NOP if IPv6 is not available */
2742 if (!socket_ipv6_is_supported())
2745 if (link
->flags
& IFF_LOOPBACK
)
2751 if (link
->network
->ipv6_hop_limit
< 0)
2754 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2755 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2757 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2759 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2764 static int link_set_ipv6_mtu(Link
*link
) {
2765 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2766 const char *p
= NULL
;
2769 /* Make this a NOP if IPv6 is not available */
2770 if (!socket_ipv6_is_supported())
2773 if (link
->flags
& IFF_LOOPBACK
)
2776 if (link
->network
->ipv6_mtu
== 0)
2779 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2781 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2783 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2785 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2790 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2791 Address
*net_address
;
2799 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2800 if (address_equal(net_address
, address
))
2806 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2815 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2816 if (route_equal(net_route
, route
))
2822 static int link_drop_foreign_config(Link
*link
) {
2828 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2829 /* we consider IPv6LL addresses to be managed by the kernel */
2830 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2833 if (link_is_static_address_configured(link
, address
)) {
2834 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2836 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2838 r
= address_remove(address
, link
, NULL
);
2844 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2845 /* do not touch routes managed by the kernel */
2846 if (route
->protocol
== RTPROT_KERNEL
)
2849 if (link_is_static_route_configured(link
, route
)) {
2850 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2854 r
= route_remove(route
, link
, NULL
);
2863 static int link_drop_config(Link
*link
) {
2864 Address
*address
, *pool_address
;
2869 SET_FOREACH(address
, link
->addresses
, i
) {
2870 /* we consider IPv6LL addresses to be managed by the kernel */
2871 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2874 r
= address_remove(address
, link
, NULL
);
2878 /* If this address came from an address pool, clean up the pool */
2879 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2880 if (address_equal(address
, pool_address
)) {
2881 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2882 address_free(pool_address
);
2888 SET_FOREACH(route
, link
->routes
, i
) {
2889 /* do not touch routes managed by the kernel */
2890 if (route
->protocol
== RTPROT_KERNEL
)
2893 r
= route_remove(route
, link
, NULL
);
2903 static int link_update_lldp(Link
*link
) {
2911 if (link
->flags
& IFF_UP
) {
2912 r
= sd_lldp_start(link
->lldp
);
2914 log_link_debug(link
, "Started LLDP.");
2916 r
= sd_lldp_stop(link
->lldp
);
2918 log_link_debug(link
, "Stopped LLDP.");
2924 static int link_configure_can(Link
*link
) {
2927 if (streq_ptr(link
->kind
, "can")) {
2928 /* The CAN interface must be down to configure bitrate, etc... */
2929 if ((link
->flags
& IFF_UP
)) {
2930 r
= link_down(link
);
2932 link_enter_failed(link
);
2939 return link_set_can(link
);
2942 if (!(link
->flags
& IFF_UP
)) {
2943 r
= link_up_can(link
);
2945 link_enter_failed(link
);
2953 static int link_configure(Link
*link
) {
2957 assert(link
->network
);
2958 assert(link
->state
== LINK_STATE_PENDING
);
2960 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2961 return link_configure_can(link
);
2963 /* Drop foreign config, but ignore loopback or critical devices.
2964 * We do not want to remove loopback address or addresses used for root NFS. */
2965 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2966 r
= link_drop_foreign_config(link
);
2971 r
= link_set_proxy_arp(link
);
2975 r
= ipv6_proxy_ndp_addresses_configure(link
);
2979 r
= link_set_ipv4_forward(link
);
2983 r
= link_set_ipv6_forward(link
);
2987 r
= link_set_ipv6_privacy_extensions(link
);
2991 r
= link_set_ipv6_accept_ra(link
);
2995 r
= link_set_ipv6_dad_transmits(link
);
2999 r
= link_set_ipv6_hop_limit(link
);
3003 r
= link_set_flags(link
);
3007 r
= link_set_ipv6_mtu(link
);
3011 if (link_ipv4ll_enabled(link
)) {
3012 r
= ipv4ll_configure(link
);
3017 if (link_dhcp4_enabled(link
)) {
3018 r
= dhcp4_set_promote_secondaries(link
);
3022 r
= dhcp4_configure(link
);
3027 if (link_dhcp4_server_enabled(link
)) {
3028 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
3032 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
3037 if (link_dhcp6_enabled(link
) ||
3038 link_ipv6_accept_ra_enabled(link
)) {
3039 r
= dhcp6_configure(link
);
3044 if (link_ipv6_accept_ra_enabled(link
)) {
3045 r
= ndisc_configure(link
);
3050 if (link_radv_enabled(link
)) {
3051 r
= radv_configure(link
);
3056 if (link_lldp_rx_enabled(link
)) {
3057 r
= sd_lldp_new(&link
->lldp
);
3061 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
3065 r
= sd_lldp_match_capabilities(link
->lldp
,
3066 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
3067 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
3068 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
3072 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
3076 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
3080 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
3084 r
= link_update_lldp(link
);
3089 if (link
->network
->mtu
> 0) {
3090 r
= link_set_mtu(link
, link
->network
->mtu
);
3095 if (socket_ipv6_is_supported()) {
3096 r
= link_configure_addrgen_mode(link
);
3101 return link_configure_after_setting_mtu(link
);
3104 static int link_configure_after_setting_mtu(Link
*link
) {
3108 assert(link
->network
);
3109 assert(link
->state
== LINK_STATE_PENDING
);
3111 if (link
->setting_mtu
)
3114 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
3115 r
= link_acquire_conf(link
);
3120 return link_enter_join_netdev(link
);
3123 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
3126 if (duid
->raw_data_len
> 0)
3129 if (duid
->type
!= DUID_TYPE_UUID
)
3132 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
3133 duid
->raw_data_len
= sizeof(sd_id128_t
);
3138 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
3139 Manager
*manager
= userdata
;
3140 const sd_bus_error
*e
;
3150 e
= sd_bus_message_get_error(m
);
3152 log_error_errno(sd_bus_error_get_errno(e
),
3153 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
3158 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
3162 if (sz
!= sizeof(sd_id128_t
)) {
3163 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
3167 memcpy(&manager
->product_uuid
, a
, sz
);
3168 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
3169 (void) duid_set_uuid(duid
, manager
->product_uuid
);
3171 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
3174 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
3175 r
= link_configure(link
);
3177 log_link_error_errno(link
, r
, "Failed to configure link: %m");
3180 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
3182 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
3183 * even if the method fails. */
3184 manager
->has_product_uuid
= true;
3189 static bool link_requires_uuid(Link
*link
) {
3193 assert(link
->manager
);
3194 assert(link
->network
);
3196 duid
= link_get_duid(link
);
3197 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
3200 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
3203 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
3209 static int link_configure_duid(Link
*link
) {
3215 assert(link
->manager
);
3216 assert(link
->network
);
3219 duid
= link_get_duid(link
);
3221 if (!link_requires_uuid(link
))
3224 if (m
->has_product_uuid
) {
3225 (void) duid_set_uuid(duid
, m
->product_uuid
);
3229 if (!m
->links_requesting_uuid
) {
3230 r
= manager_request_product_uuid(m
, link
);
3235 log_link_warning_errno(link
, r
,
3236 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3240 r
= set_put(m
->links_requesting_uuid
, link
);
3244 r
= set_put(m
->duids_requesting_uuid
, duid
);
3252 static int link_initialized_and_synced(Link
*link
) {
3257 assert(link
->ifname
);
3258 assert(link
->manager
);
3260 if (link
->state
!= LINK_STATE_PENDING
)
3263 log_link_debug(link
, "Link state is up-to-date");
3265 r
= link_new_bound_by_list(link
);
3269 r
= link_handle_bound_by_list(link
);
3273 if (!link
->network
) {
3274 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3275 &link
->mac
, &network
);
3277 link_enter_unmanaged(link
);
3279 } else if (r
== 0 && network
->unmanaged
) {
3280 link_enter_unmanaged(link
);
3285 if (link
->flags
& IFF_LOOPBACK
) {
3286 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3287 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3289 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3290 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3292 if (network
->dhcp_server
)
3293 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3296 r
= network_apply(network
, link
);
3301 r
= link_new_bound_to_list(link
);
3305 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3306 * link_configure() is called later asynchronously. */
3307 r
= link_configure_duid(link
);
3311 r
= link_configure(link
);
3318 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3319 (void) link_initialized_and_synced(link
);
3323 int link_initialized(Link
*link
, sd_device
*device
) {
3324 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3328 assert(link
->manager
);
3329 assert(link
->manager
->rtnl
);
3332 if (link
->state
!= LINK_STATE_PENDING
)
3335 if (link
->sd_device
)
3338 log_link_debug(link
, "udev initialized link");
3340 link
->sd_device
= sd_device_ref(device
);
3342 /* udev has initialized the link, but we don't know if we have yet
3343 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3344 * when it returns we know that the pending NEWLINKs have already been
3345 * processed and that we are up-to-date */
3347 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3352 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3353 link_netlink_destroy_callback
, link
);
3362 static int link_load(Link
*link
) {
3363 _cleanup_free_
char *network_file
= NULL
,
3366 *dhcp4_address
= NULL
,
3367 *ipv4ll_address
= NULL
;
3368 union in_addr_union address
;
3369 union in_addr_union route_dst
;
3375 r
= parse_env_file(NULL
, link
->state_file
,
3376 "NETWORK_FILE", &network_file
,
3377 "ADDRESSES", &addresses
,
3379 "DHCP4_ADDRESS", &dhcp4_address
,
3380 "IPV4LL_ADDRESS", &ipv4ll_address
);
3381 if (r
< 0 && r
!= -ENOENT
)
3382 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3389 suffix
= strrchr(network_file
, '.');
3391 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3392 goto network_file_fail
;
3396 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3398 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3399 goto network_file_fail
;
3402 r
= network_apply(network
, link
);
3404 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3413 _cleanup_free_
char *address_str
= NULL
;
3414 char *prefixlen_str
;
3416 unsigned char prefixlen
;
3418 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3420 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3426 prefixlen_str
= strchr(address_str
, '/');
3427 if (!prefixlen_str
) {
3428 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3432 *prefixlen_str
++ = '\0';
3434 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3436 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3440 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3442 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3446 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3448 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3457 _cleanup_free_
char *route_str
= NULL
;
3458 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3460 char *prefixlen_str
;
3462 unsigned char prefixlen
, tos
, table
;
3465 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3467 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3473 prefixlen_str
= strchr(route_str
, '/');
3474 if (!prefixlen_str
) {
3475 log_link_debug(link
, "Failed to parse route %s", route_str
);
3479 *prefixlen_str
++ = '\0';
3481 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3483 log_link_debug(link
,
3484 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3489 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3491 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3495 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3497 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3499 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3500 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3501 0, route_expire_handler
, route
);
3503 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3506 route
->lifetime
= lifetime
;
3507 sd_event_source_unref(route
->expire
);
3508 route
->expire
= TAKE_PTR(expire
);
3512 if (dhcp4_address
) {
3513 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3515 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3516 goto dhcp4_address_fail
;
3519 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3521 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3523 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3525 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3530 if (ipv4ll_address
) {
3531 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3533 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3534 goto ipv4ll_address_fail
;
3537 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3539 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3541 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3543 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3546 ipv4ll_address_fail
:
3551 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3552 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3553 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3562 r
= link_new(m
, message
, ret
);
3568 log_link_debug(link
, "Link %d added", link
->ifindex
);
3570 r
= link_load(link
);
3574 if (detect_container() <= 0) {
3575 /* not in a container, udev will be around */
3576 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3577 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3579 log_link_warning_errno(link
, r
, "Could not find device: %m");
3583 r
= sd_device_get_is_initialized(device
);
3585 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3590 log_link_debug(link
, "link pending udev initialization...");
3594 r
= link_initialized(link
, device
);
3598 r
= link_initialized_and_synced(link
);
3605 link_enter_failed(link
);
3609 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3614 log_link_info(link
, "Gained IPv6LL");
3616 link
->ipv6ll_address
= *address
;
3617 link_check_ready(link
);
3619 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3620 r
= link_acquire_ipv6_conf(link
);
3622 link_enter_failed(link
);
3630 static int link_carrier_gained(Link
*link
) {
3635 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3636 r
= link_acquire_conf(link
);
3638 link_enter_failed(link
);
3642 r
= link_request_set_addresses(link
);
3647 r
= link_handle_bound_by_list(link
);
3654 static int link_carrier_lost(Link
*link
) {
3659 if (link
->network
->ignore_carrier_loss
)
3662 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3663 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3664 * configuration and stop the clients as well. */
3665 if (link
->setting_mtu
)
3668 r
= link_stop_clients(link
);
3670 link_enter_failed(link
);
3674 if (link_dhcp4_server_enabled(link
))
3675 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3677 r
= link_drop_config(link
);
3681 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3682 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3683 r
= link_drop_foreign_config(link
);
3688 r
= link_handle_bound_by_list(link
);
3695 int link_carrier_reset(Link
*link
) {
3700 if (link_has_carrier(link
)) {
3701 r
= link_carrier_lost(link
);
3705 r
= link_carrier_gained(link
);
3709 log_link_info(link
, "Reset carrier");
3715 int link_update(Link
*link
, sd_netlink_message
*m
) {
3716 struct ether_addr mac
;
3719 bool had_carrier
, carrier_gained
, carrier_lost
;
3723 assert(link
->ifname
);
3726 if (link
->state
== LINK_STATE_LINGER
) {
3727 log_link_info(link
, "Link readded");
3728 link_set_state(link
, LINK_STATE_CONFIGURING
);
3730 r
= link_new_carrier_maps(link
);
3735 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3736 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3737 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3739 if (link
->state
== LINK_STATE_PENDING
) {
3740 r
= free_and_strdup(&link
->ifname
, ifname
);
3744 Manager
*manager
= link
->manager
;
3747 r
= link_add(manager
, m
, &link
);
3753 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3754 if (r
>= 0 && mtu
> 0) {
3756 if (link
->original_mtu
== 0) {
3757 link
->original_mtu
= mtu
;
3758 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3761 if (link
->dhcp_client
) {
3762 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3765 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3769 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3771 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3775 /* The kernel may broadcast NEWLINK messages without the MAC address
3776 set, simply ignore them. */
3777 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3779 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3782 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3785 log_link_debug(link
, "MAC address: "
3786 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3787 mac
.ether_addr_octet
[0],
3788 mac
.ether_addr_octet
[1],
3789 mac
.ether_addr_octet
[2],
3790 mac
.ether_addr_octet
[3],
3791 mac
.ether_addr_octet
[4],
3792 mac
.ether_addr_octet
[5]);
3795 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3797 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3800 if (link
->dhcp_client
) {
3801 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3802 (const uint8_t *) &link
->mac
,
3806 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3808 r
= dhcp4_set_client_identifier(link
);
3813 if (link
->dhcp6_client
) {
3814 const DUID
* duid
= link_get_duid(link
);
3816 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3817 (const uint8_t *) &link
->mac
,
3821 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3823 if (link
->network
->iaid_set
) {
3824 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3825 link
->network
->iaid
);
3827 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3830 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3832 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3833 duid
->raw_data_len
);
3835 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3839 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3841 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3845 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3847 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3852 had_carrier
= link_has_carrier(link
);
3854 r
= link_update_flags(link
, m
);
3858 r
= link_update_lldp(link
);
3862 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3863 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3865 if (carrier_gained
) {
3866 log_link_info(link
, "Gained carrier");
3868 r
= link_carrier_gained(link
);
3871 } else if (carrier_lost
) {
3872 log_link_info(link
, "Lost carrier");
3874 r
= link_carrier_lost(link
);
3882 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3890 if (hashmap_isempty(h
))
3894 HASHMAP_FOREACH(link
, h
, i
) {
3898 fprintf(f
, "%i", link
->ifindex
);
3905 int link_save(Link
*link
) {
3906 _cleanup_free_
char *temp_path
= NULL
;
3907 _cleanup_fclose_
FILE *f
= NULL
;
3908 const char *admin_state
, *oper_state
;
3915 assert(link
->state_file
);
3916 assert(link
->lease_file
);
3917 assert(link
->manager
);
3919 if (link
->state
== LINK_STATE_LINGER
) {
3920 unlink(link
->state_file
);
3924 link_lldp_save(link
);
3926 admin_state
= link_state_to_string(link
->state
);
3927 assert(admin_state
);
3929 oper_state
= link_operstate_to_string(link
->operstate
);
3932 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3936 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3937 (void) fchmod(fileno(f
), 0644);
3940 "# This is private data. Do not parse.\n"
3943 admin_state
, oper_state
);
3945 if (link
->network
) {
3947 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3948 const char *dhcp_domainname
= NULL
;
3949 char **dhcp6_domains
= NULL
;
3950 char **dhcp_domains
= NULL
;
3953 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3954 yes_no(link
->network
->required_for_online
));
3956 if (link
->dhcp6_client
) {
3957 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3958 if (r
< 0 && r
!= -ENOMSG
)
3959 log_link_debug(link
, "No DHCPv6 lease");
3962 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3967 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3968 _cleanup_free_
char *b
= NULL
;
3970 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3971 &link
->network
->dns
[j
].address
, &b
);
3973 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3983 if (link
->network
->dhcp_use_dns
&&
3985 const struct in_addr
*addresses
;
3987 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3991 serialize_in_addrs(f
, addresses
, r
);
3996 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3997 struct in6_addr
*in6_addrs
;
3999 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
4003 serialize_in6_addrs(f
, in6_addrs
, r
);
4008 /* Make sure to flush out old entries before we use the NDISC data */
4011 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
4014 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
4018 serialize_in6_addrs(f
, &dd
->address
, 1);
4027 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
4029 if (link
->network
->dhcp_use_ntp
&&
4031 const struct in_addr
*addresses
;
4033 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
4037 serialize_in_addrs(f
, addresses
, r
);
4042 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
4043 struct in6_addr
*in6_addrs
;
4046 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
4051 serialize_in6_addrs(f
, in6_addrs
, r
);
4055 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
4057 fputstrv(f
, hosts
, NULL
, &space
);
4062 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
4063 if (link
->dhcp_lease
) {
4064 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
4065 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
4068 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
4071 fputs("DOMAINS=", f
);
4073 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
4075 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
4078 if (dhcp_domainname
)
4079 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4081 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4083 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4085 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4086 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4091 fputs("ROUTE_DOMAINS=", f
);
4093 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
4095 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
4098 if (dhcp_domainname
)
4099 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
4101 fputstrv(f
, dhcp_domains
, NULL
, &space
);
4103 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
4105 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
4106 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
4111 fprintf(f
, "LLMNR=%s\n",
4112 resolve_support_to_string(link
->network
->llmnr
));
4113 fprintf(f
, "MDNS=%s\n",
4114 resolve_support_to_string(link
->network
->mdns
));
4115 if (link
->network
->dns_default_route
>= 0)
4116 fprintf(f
, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link
->network
->dns_default_route
));
4118 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
4119 fprintf(f
, "DNS_OVER_TLS=%s\n",
4120 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
4122 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
4123 fprintf(f
, "DNSSEC=%s\n",
4124 dnssec_mode_to_string(link
->network
->dnssec_mode
));
4126 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
4129 fputs("DNSSEC_NTA=", f
);
4131 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
4132 fputs_with_space(f
, n
, NULL
, &space
);
4136 fputs("ADDRESSES=", f
);
4138 SET_FOREACH(a
, link
->addresses
, i
) {
4139 _cleanup_free_
char *address_str
= NULL
;
4141 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
4145 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
4150 fputs("ROUTES=", f
);
4152 SET_FOREACH(route
, link
->routes
, i
) {
4153 _cleanup_free_
char *route_str
= NULL
;
4155 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
4159 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
4160 space
? " " : "", route_str
,
4161 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
4168 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
4169 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
4171 if (link
->dhcp_lease
) {
4172 struct in_addr address
;
4173 const char *tz
= NULL
;
4175 assert(link
->network
);
4177 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
4179 fprintf(f
, "TIMEZONE=%s\n", tz
);
4181 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
4183 fputs("DHCP4_ADDRESS=", f
);
4184 serialize_in_addrs(f
, &address
, 1);
4188 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
4196 unlink(link
->lease_file
);
4199 struct in_addr address
;
4201 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
4203 fputs("IPV4LL_ADDRESS=", f
);
4204 serialize_in_addrs(f
, &address
, 1);
4209 r
= fflush_and_check(f
);
4213 if (rename(temp_path
, link
->state_file
) < 0) {
4221 (void) unlink(link
->state_file
);
4223 (void) unlink(temp_path
);
4225 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
4228 /* The serialized state in /run is no longer up-to-date. */
4229 void link_dirty(Link
*link
) {
4234 /* mark manager dirty as link is dirty */
4235 manager_dirty(link
->manager
);
4237 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4239 /* allocation errors are ignored */
4242 r
= set_put(link
->manager
->dirty_links
, link
);
4244 /* don't take another ref if the link was already dirty */
4250 /* The serialized state in /run is up-to-date */
4251 void link_clean(Link
*link
) {
4253 assert(link
->manager
);
4255 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4258 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4259 [LINK_STATE_PENDING
] = "pending",
4260 [LINK_STATE_CONFIGURING
] = "configuring",
4261 [LINK_STATE_CONFIGURED
] = "configured",
4262 [LINK_STATE_UNMANAGED
] = "unmanaged",
4263 [LINK_STATE_FAILED
] = "failed",
4264 [LINK_STATE_LINGER
] = "linger",
4267 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4269 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4270 [LINK_OPERSTATE_OFF
] = "off",
4271 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4272 [LINK_OPERSTATE_DORMANT
] = "dormant",
4273 [LINK_OPERSTATE_CARRIER
] = "carrier",
4274 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4275 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
4276 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4279 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);