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-lease-internal.h"
14 #include "netlink-util.h"
15 #include "network-internal.h"
16 #include "networkd-ipv6-proxy-ndp.h"
17 #include "networkd-lldp-tx.h"
18 #include "networkd-manager.h"
19 #include "networkd-ndisc.h"
20 #include "networkd-radv.h"
21 #include "networkd-routing-policy-rule.h"
23 #include "socket-util.h"
24 #include "stdio-util.h"
25 #include "string-table.h"
30 static bool link_dhcp6_enabled(Link
*link
) {
33 if (!socket_ipv6_is_supported())
36 if (link
->flags
& IFF_LOOPBACK
)
42 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
45 static bool link_dhcp4_enabled(Link
*link
) {
48 if (link
->flags
& IFF_LOOPBACK
)
54 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
57 static bool link_dhcp4_server_enabled(Link
*link
) {
60 if (link
->flags
& IFF_LOOPBACK
)
66 return link
->network
->dhcp_server
;
69 static bool link_ipv4ll_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 if (streq_ptr(link
->kind
, "wireguard"))
81 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
84 static bool link_ipv6ll_enabled(Link
*link
) {
87 if (!socket_ipv6_is_supported())
90 if (link
->flags
& IFF_LOOPBACK
)
96 if (streq_ptr(link
->kind
, "wireguard"))
99 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
102 static bool link_ipv6_enabled(Link
*link
) {
105 if (!socket_ipv6_is_supported())
108 if (link
->network
->bridge
)
111 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
112 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
115 static bool link_radv_enabled(Link
*link
) {
118 if (!link_ipv6ll_enabled(link
))
121 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
124 static bool link_lldp_rx_enabled(Link
*link
) {
127 if (link
->flags
& IFF_LOOPBACK
)
130 if (link
->iftype
!= ARPHRD_ETHER
)
136 /* LLDP should be handled on bridge slaves as those have a direct
137 * connection to their peers not on the bridge master. Linux doesn't
138 * even (by default) forward lldp packets to the bridge master.*/
139 if (streq_ptr("bridge", link
->kind
))
142 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
145 static bool link_lldp_emit_enabled(Link
*link
) {
148 if (link
->flags
& IFF_LOOPBACK
)
151 if (link
->iftype
!= ARPHRD_ETHER
)
157 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
160 static bool link_ipv4_forward_enabled(Link
*link
) {
163 if (link
->flags
& IFF_LOOPBACK
)
169 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
172 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
175 static bool link_ipv6_forward_enabled(Link
*link
) {
178 if (!socket_ipv6_is_supported())
181 if (link
->flags
& IFF_LOOPBACK
)
187 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
190 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
193 static bool link_proxy_arp_enabled(Link
*link
) {
196 if (link
->flags
& IFF_LOOPBACK
)
202 if (link
->network
->proxy_arp
< 0)
208 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
211 if (!socket_ipv6_is_supported())
214 if (link
->flags
& IFF_LOOPBACK
)
220 if (!link_ipv6ll_enabled(link
))
223 /* If unset use system default (enabled if local forwarding is disabled.
224 * disabled if local forwarding is enabled).
225 * If set, ignore or enforce RA independent of local forwarding state.
227 if (link
->network
->ipv6_accept_ra
< 0)
228 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
229 return !link_ipv6_forward_enabled(link
);
230 else if (link
->network
->ipv6_accept_ra
> 0)
231 /* accept RA even if ip_forward is enabled */
238 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
241 if (!socket_ipv6_is_supported())
242 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
244 if (link
->flags
& IFF_LOOPBACK
)
245 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
248 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
250 return link
->network
->ipv6_privacy_extensions
;
253 static int link_enable_ipv6(Link
*link
) {
254 const char *p
= NULL
;
258 if (link
->flags
& IFF_LOOPBACK
)
261 disabled
= !link_ipv6_enabled(link
);
263 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
265 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
267 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
268 enable_disable(!disabled
), link
->ifname
);
270 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
275 void link_update_operstate(Link
*link
) {
276 LinkOperationalState operstate
;
279 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
280 operstate
= LINK_OPERSTATE_DORMANT
;
281 else if (link_has_carrier(link
)) {
283 uint8_t scope
= RT_SCOPE_NOWHERE
;
286 /* if we have carrier, check what addresses we have */
287 SET_FOREACH(address
, link
->addresses
, i
) {
288 if (!address_is_ready(address
))
291 if (address
->scope
< scope
)
292 scope
= address
->scope
;
295 /* for operstate we also take foreign addresses into account */
296 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
297 if (!address_is_ready(address
))
300 if (address
->scope
< scope
)
301 scope
= address
->scope
;
304 if (scope
< RT_SCOPE_SITE
)
305 /* universally accessible addresses found */
306 operstate
= LINK_OPERSTATE_ROUTABLE
;
307 else if (scope
< RT_SCOPE_HOST
)
308 /* only link or site local addresses found */
309 operstate
= LINK_OPERSTATE_DEGRADED
;
311 /* no useful addresses found */
312 operstate
= LINK_OPERSTATE_CARRIER
;
313 } else if (link
->flags
& IFF_UP
)
314 operstate
= LINK_OPERSTATE_NO_CARRIER
;
316 operstate
= LINK_OPERSTATE_OFF
;
318 if (link
->operstate
!= operstate
) {
319 link
->operstate
= operstate
;
320 link_send_changed(link
, "OperationalState", NULL
);
325 #define FLAG_STRING(string, flag, old, new) \
326 (((old ^ new) & flag) \
327 ? ((old & flag) ? (" -" string) : (" +" string)) \
330 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
331 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
337 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
339 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
341 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
343 /* if we got a message without operstate, take it to mean
344 the state was unchanged */
345 operstate
= link
->kernel_operstate
;
347 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
350 if (link
->flags
!= flags
) {
351 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",
352 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
353 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
354 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
355 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
356 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
357 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
358 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
359 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
360 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
361 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
362 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
363 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
364 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
365 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
366 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
367 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
368 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
369 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
370 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
372 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
373 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
374 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
375 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
376 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
377 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
378 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
379 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
381 /* link flags are currently at most 18 bits, let's align to
383 if (unknown_flags_added
)
385 "Unknown link flags gained: %#.5x (ignoring)",
386 unknown_flags_added
);
388 if (unknown_flags_removed
)
390 "Unknown link flags lost: %#.5x (ignoring)",
391 unknown_flags_removed
);
395 link
->kernel_operstate
= operstate
;
397 link_update_operstate(link
);
402 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
403 _cleanup_(link_unrefp
) Link
*link
= NULL
;
405 const char *ifname
, *kind
= NULL
;
407 unsigned short iftype
;
413 /* check for link kind */
414 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
416 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
417 r
= sd_netlink_message_exit_container(message
);
422 r
= sd_netlink_message_get_type(message
, &type
);
425 else if (type
!= RTM_NEWLINK
)
428 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
431 else if (ifindex
<= 0)
434 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
438 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
442 link
= new0(Link
, 1);
447 link
->manager
= manager
;
448 link
->state
= LINK_STATE_PENDING
;
449 link
->rtnl_extended_attrs
= true;
450 link
->ifindex
= ifindex
;
451 link
->iftype
= iftype
;
452 link
->ifname
= strdup(ifname
);
457 link
->kind
= strdup(kind
);
462 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
464 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
466 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
469 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
472 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
475 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
479 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
483 r
= link_update_flags(link
, message
);
487 *ret
= TAKE_PTR(link
);
492 static void link_free(Link
*link
) {
501 while ((route
= set_first(link
->routes
)))
504 while ((route
= set_first(link
->routes_foreign
)))
507 link
->routes
= set_free(link
->routes
);
508 link
->routes_foreign
= set_free(link
->routes_foreign
);
510 while ((address
= set_first(link
->addresses
)))
511 address_free(address
);
513 while ((address
= set_first(link
->addresses_foreign
)))
514 address_free(address
);
516 link
->addresses
= set_free(link
->addresses
);
517 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
519 while ((address
= link
->pool_addresses
)) {
520 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
521 address_free(address
);
524 sd_dhcp_server_unref(link
->dhcp_server
);
525 sd_dhcp_client_unref(link
->dhcp_client
);
526 sd_dhcp_lease_unref(link
->dhcp_lease
);
528 link_lldp_emit_stop(link
);
530 free(link
->lease_file
);
532 sd_lldp_unref(link
->lldp
);
533 free(link
->lldp_file
);
537 sd_ipv4ll_unref(link
->ipv4ll
);
538 sd_dhcp6_client_unref(link
->dhcp6_client
);
539 sd_ndisc_unref(link
->ndisc
);
540 sd_radv_unref(link
->radv
);
543 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
549 (void) unlink(link
->state_file
);
550 free(link
->state_file
);
552 sd_device_unref(link
->sd_device
);
554 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
555 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
556 hashmap_free(link
->bound_to_links
);
558 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
559 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
560 hashmap_free(link
->bound_by_links
);
565 Link
*link_unref(Link
*link
) {
569 assert(link
->n_ref
> 0);
581 Link
*link_ref(Link
*link
) {
585 assert(link
->n_ref
> 0);
592 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
599 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
608 static void link_set_state(Link
*link
, LinkState state
) {
611 if (link
->state
== state
)
616 link_send_changed(link
, "AdministrativeState", NULL
);
619 static void link_enter_unmanaged(Link
*link
) {
622 log_link_debug(link
, "Unmanaged");
624 link_set_state(link
, LINK_STATE_UNMANAGED
);
629 static int link_stop_clients(Link
*link
) {
633 assert(link
->manager
);
634 assert(link
->manager
->event
);
636 if (link
->dhcp_client
) {
637 k
= sd_dhcp_client_stop(link
->dhcp_client
);
639 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
643 k
= sd_ipv4ll_stop(link
->ipv4ll
);
645 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
648 if (link
->dhcp6_client
) {
649 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
651 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
655 k
= sd_ndisc_stop(link
->ndisc
);
657 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
661 k
= sd_radv_stop(link
->radv
);
663 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
666 link_lldp_emit_stop(link
);
670 void link_enter_failed(Link
*link
) {
673 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
676 log_link_warning(link
, "Failed");
678 link_set_state(link
, LINK_STATE_FAILED
);
680 link_stop_clients(link
);
685 static Address
* link_find_dhcp_server_address(Link
*link
) {
689 assert(link
->network
);
691 /* The first statically configured address if there is any */
692 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
694 if (address
->family
!= AF_INET
)
697 if (in_addr_is_null(address
->family
, &address
->in_addr
))
703 /* If that didn't work, find a suitable address we got from the pool */
704 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
705 if (address
->family
!= AF_INET
)
714 static void link_enter_configured(Link
*link
) {
716 assert(link
->network
);
718 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
721 log_link_info(link
, "Configured");
723 link_set_state(link
, LINK_STATE_CONFIGURED
);
728 void link_check_ready(Link
*link
) {
734 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
740 if (!link
->static_routes_configured
)
743 if (!link
->routing_policy_rules_configured
)
746 if (link_ipv4ll_enabled(link
))
747 if (!link
->ipv4ll_address
||
751 if (!link
->network
->bridge
) {
753 if (link_ipv6ll_enabled(link
))
754 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
757 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
758 !link
->dhcp4_configured
) ||
759 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
760 !link
->dhcp6_configured
) ||
761 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
762 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
765 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
769 SET_FOREACH(a
, link
->addresses
, i
)
770 if (!address_is_ready(a
))
773 if (link
->state
!= LINK_STATE_CONFIGURED
)
774 link_enter_configured(link
);
779 static int link_set_routing_policy_rule(Link
*link
) {
780 RoutingPolicyRule
*rule
, *rrule
= NULL
;
784 assert(link
->network
);
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
, &rrule
);
790 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
794 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
796 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
797 link_enter_failed(link
);
801 link
->routing_policy_rule_messages
++;
804 routing_policy_rule_purge(link
->manager
, link
);
805 if (link
->routing_policy_rule_messages
== 0) {
806 link
->routing_policy_rules_configured
= true;
807 link_check_ready(link
);
809 log_link_debug(link
, "Setting routing policy rules");
814 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
815 _cleanup_(link_unrefp
) Link
*link
= userdata
;
818 assert(link
->route_messages
> 0);
819 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
820 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
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_enter_set_routes(Link
*link
) {
846 assert(link
->network
);
847 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
849 (void) link_set_routing_policy_rule(link
);
851 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
853 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
854 r
= route_configure(rt
, link
, route_handler
);
856 log_link_warning_errno(link
, r
, "Could not set routes: %m");
857 link_enter_failed(link
);
861 link
->route_messages
++;
864 if (link
->route_messages
== 0) {
865 link
->static_routes_configured
= true;
866 link_check_ready(link
);
868 log_link_debug(link
, "Setting routes");
873 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
874 _cleanup_(link_unrefp
) Link
*link
= userdata
;
879 assert(link
->ifname
);
881 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
884 r
= sd_netlink_message_get_errno(m
);
885 if (r
< 0 && r
!= -ESRCH
)
886 log_link_warning_errno(link
, r
, "Could not drop route: %m");
891 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
892 _cleanup_(link_unrefp
) Link
*link
= userdata
;
898 assert(link
->ifname
);
899 assert(link
->address_messages
> 0);
900 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
901 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
903 link
->address_messages
--;
905 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
908 r
= sd_netlink_message_get_errno(m
);
909 if (r
< 0 && r
!= -EEXIST
)
910 log_link_warning_errno(link
, r
, "could not set address: %m");
912 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
914 if (link
->address_messages
== 0) {
915 log_link_debug(link
, "Addresses set");
916 link_enter_set_routes(link
);
922 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
923 _cleanup_(link_unrefp
) Link
*link
= userdata
;
929 assert(link
->ifname
);
930 assert(link
->address_label_messages
> 0);
932 link
->address_label_messages
--;
934 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
937 r
= sd_netlink_message_get_errno(m
);
938 if (r
< 0 && r
!= -EEXIST
)
939 log_link_warning_errno(link
, r
, "could not set address label: %m");
941 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
943 if (link
->address_label_messages
== 0)
944 log_link_debug(link
, "Addresses label set");
949 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
950 _cleanup_free_
struct in_addr
*addresses
= NULL
;
951 size_t n_addresses
= 0, n_allocated
= 0;
954 log_debug("Copying DNS server information from %s", link
->ifname
);
959 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
962 /* Only look for IPv4 addresses */
963 if (link
->network
->dns
[i
].family
!= AF_INET
)
966 ia
= link
->network
->dns
[i
].address
.in
;
968 /* Never propagate obviously borked data */
969 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
972 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
975 addresses
[n_addresses
++] = ia
;
978 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
979 const struct in_addr
*da
= NULL
;
982 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
985 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
988 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
993 if (n_addresses
<= 0)
996 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
999 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1000 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1001 size_t n_addresses
= 0, n_allocated
= 0;
1007 log_debug("Copying NTP server information from %s", link
->ifname
);
1009 STRV_FOREACH(a
, link
->network
->ntp
) {
1012 /* Only look for IPv4 addresses */
1013 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1016 /* Never propagate obviously borked data */
1017 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1020 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1023 addresses
[n_addresses
++] = ia
;
1026 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1027 const struct in_addr
*da
= NULL
;
1030 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1033 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1036 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1041 if (n_addresses
<= 0)
1044 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1047 static int link_set_bridge_fdb(Link
*link
) {
1048 FdbEntry
*fdb_entry
;
1051 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1052 r
= fdb_entry_configure(link
, fdb_entry
);
1054 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1060 static int link_enter_set_addresses(Link
*link
) {
1061 AddressLabel
*label
;
1066 assert(link
->network
);
1067 assert(link
->state
!= _LINK_STATE_INVALID
);
1069 r
= link_set_bridge_fdb(link
);
1073 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1075 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1076 r
= address_configure(ad
, link
, address_handler
, false);
1078 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1079 link_enter_failed(link
);
1083 link
->address_messages
++;
1086 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1087 r
= address_label_configure(label
, link
, address_label_handler
, false);
1089 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1090 link_enter_failed(link
);
1094 link
->address_label_messages
++;
1097 /* now that we can figure out a default address for the dhcp server,
1099 if (link_dhcp4_server_enabled(link
)) {
1101 Link
*uplink
= NULL
;
1102 bool acquired_uplink
= false;
1104 address
= link_find_dhcp_server_address(link
);
1106 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1107 link_enter_failed(link
);
1111 /* use the server address' subnet as the pool */
1112 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1113 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1118 r = sd_dhcp_server_set_router(link->dhcp_server,
1119 &main_address->in_addr.in);
1124 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1125 r
= sd_dhcp_server_set_max_lease_time(
1127 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1132 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1133 r
= sd_dhcp_server_set_default_lease_time(
1135 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1140 if (link
->network
->dhcp_server_emit_dns
) {
1142 if (link
->network
->n_dhcp_server_dns
> 0)
1143 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1145 uplink
= manager_find_uplink(link
->manager
, link
);
1146 acquired_uplink
= true;
1149 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1152 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1155 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1158 if (link
->network
->dhcp_server_emit_ntp
) {
1160 if (link
->network
->n_dhcp_server_ntp
> 0)
1161 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1163 if (!acquired_uplink
)
1164 uplink
= manager_find_uplink(link
->manager
, link
);
1167 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1170 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1174 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1177 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1179 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1181 if (link
->network
->dhcp_server_emit_timezone
) {
1182 _cleanup_free_
char *buffer
= NULL
;
1183 const char *tz
= NULL
;
1185 if (link
->network
->dhcp_server_timezone
)
1186 tz
= link
->network
->dhcp_server_timezone
;
1188 r
= get_timezone(&buffer
);
1190 log_warning_errno(r
, "Failed to determine timezone: %m");
1196 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1202 r
= sd_dhcp_server_start(link
->dhcp_server
);
1204 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1206 link_enter_failed(link
);
1211 log_link_debug(link
, "Offering DHCPv4 leases");
1214 if (link
->address_messages
== 0)
1215 link_enter_set_routes(link
);
1217 log_link_debug(link
, "Setting addresses");
1222 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1223 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1228 assert(link
->ifname
);
1230 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1233 r
= sd_netlink_message_get_errno(m
);
1234 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1235 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1240 static int link_set_bridge_vlan(Link
*link
) {
1243 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1245 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1250 static int link_set_proxy_arp(Link
*link
) {
1251 const char *p
= NULL
;
1254 if (!link_proxy_arp_enabled(link
))
1257 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1259 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1261 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1266 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1267 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1270 log_link_debug(link
, "Set link");
1272 r
= sd_netlink_message_get_errno(m
);
1273 if (r
< 0 && r
!= -EEXIST
) {
1274 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1275 link_enter_failed(link
);
1282 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1283 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1288 assert(link
->ifname
);
1290 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1293 r
= sd_netlink_message_get_errno(m
);
1295 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1300 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1301 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1305 assert(link
->manager
);
1306 assert(link
->manager
->rtnl
);
1308 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1310 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1312 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1314 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1315 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1316 (void) link_enable_ipv6(link
);
1318 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1319 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1320 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1322 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1323 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1325 link
->network
->mtu
= IPV6_MIN_MTU
;
1328 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1330 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1332 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1334 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1336 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1338 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1340 link
->setting_mtu
= true;
1347 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1348 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1353 assert(link
->ifname
);
1355 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1358 r
= sd_netlink_message_get_errno(m
);
1360 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1365 static int link_set_flags(Link
*link
) {
1366 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1367 unsigned ifi_change
= 0;
1368 unsigned ifi_flags
= 0;
1372 assert(link
->manager
);
1373 assert(link
->manager
->rtnl
);
1375 if (link
->flags
& IFF_LOOPBACK
)
1381 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
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 (link
->network
->arp
>= 0) {
1389 ifi_change
|= IFF_NOARP
;
1390 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1393 if (link
->network
->multicast
>= 0) {
1394 ifi_change
|= IFF_MULTICAST
;
1395 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1398 if (link
->network
->allmulticast
>= 0) {
1399 ifi_change
|= IFF_ALLMULTI
;
1400 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1403 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1405 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1407 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1409 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1416 static int link_set_bridge(Link
*link
) {
1417 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1421 assert(link
->network
);
1423 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1425 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1427 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1429 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1431 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1433 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1435 if (link
->network
->use_bpdu
>= 0) {
1436 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1438 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1441 if (link
->network
->hairpin
>= 0) {
1442 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1444 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1447 if (link
->network
->fast_leave
>= 0) {
1448 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1450 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1453 if (link
->network
->allow_port_to_be_root
>= 0) {
1454 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1456 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1460 if (link
->network
->unicast_flood
>= 0) {
1461 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1463 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1467 if (link
->network
->cost
!= 0) {
1468 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1470 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1472 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1473 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1475 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1478 r
= sd_netlink_message_close_container(req
);
1480 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1482 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1484 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1491 static int link_bond_set(Link
*link
) {
1492 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1496 assert(link
->network
);
1498 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1500 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1502 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1504 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1506 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1508 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1510 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1512 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1514 if (link
->network
->active_slave
) {
1515 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1517 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1520 if (link
->network
->primary_slave
) {
1521 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1523 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1526 r
= sd_netlink_message_close_container(req
);
1528 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1530 r
= sd_netlink_message_close_container(req
);
1532 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1534 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1536 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1543 static int link_lldp_save(Link
*link
) {
1544 _cleanup_free_
char *temp_path
= NULL
;
1545 _cleanup_fclose_
FILE *f
= NULL
;
1546 sd_lldp_neighbor
**l
= NULL
;
1550 assert(link
->lldp_file
);
1553 (void) unlink(link
->lldp_file
);
1557 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1561 (void) unlink(link
->lldp_file
);
1567 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1571 fchmod(fileno(f
), 0644);
1573 for (i
= 0; i
< n
; i
++) {
1578 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1583 (void) fwrite(&u
, 1, sizeof(u
), f
);
1584 (void) fwrite(p
, 1, sz
, f
);
1587 r
= fflush_and_check(f
);
1591 if (rename(temp_path
, link
->lldp_file
) < 0) {
1598 (void) unlink(link
->lldp_file
);
1600 (void) unlink(temp_path
);
1602 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1606 for (i
= 0; i
< n
; i
++)
1607 sd_lldp_neighbor_unref(l
[i
]);
1614 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1615 Link
*link
= userdata
;
1620 (void) link_lldp_save(link
);
1622 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1623 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1625 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1627 r
= link_lldp_emit_start(link
);
1629 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1633 static int link_acquire_ipv6_conf(Link
*link
) {
1638 if (link_dhcp6_enabled(link
)) {
1639 assert(link
->dhcp6_client
);
1640 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1642 /* start DHCPv6 client in stateless mode */
1643 r
= dhcp6_request_address(link
, true);
1644 if (r
< 0 && r
!= -EBUSY
)
1645 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1647 log_link_debug(link
, "Acquiring DHCPv6 lease");
1650 if (link_ipv6_accept_ra_enabled(link
)) {
1651 assert(link
->ndisc
);
1653 log_link_debug(link
, "Discovering IPv6 routers");
1655 r
= sd_ndisc_start(link
->ndisc
);
1656 if (r
< 0 && r
!= -EBUSY
)
1657 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1660 if (link_radv_enabled(link
)) {
1662 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1664 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1666 r
= sd_radv_start(link
->radv
);
1667 if (r
< 0 && r
!= -EBUSY
)
1668 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1674 static int link_acquire_ipv4_conf(Link
*link
) {
1678 assert(link
->network
);
1679 assert(link
->manager
);
1680 assert(link
->manager
->event
);
1682 if (link_ipv4ll_enabled(link
)) {
1683 assert(link
->ipv4ll
);
1685 log_link_debug(link
, "Acquiring IPv4 link-local address");
1687 r
= sd_ipv4ll_start(link
->ipv4ll
);
1689 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1692 if (link_dhcp4_enabled(link
)) {
1693 assert(link
->dhcp_client
);
1695 log_link_debug(link
, "Acquiring DHCPv4 lease");
1697 r
= sd_dhcp_client_start(link
->dhcp_client
);
1699 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1705 static int link_acquire_conf(Link
*link
) {
1710 if (link
->setting_mtu
) {
1711 link
->setting_mtu
= false;
1715 r
= link_acquire_ipv4_conf(link
);
1719 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1720 r
= link_acquire_ipv6_conf(link
);
1725 if (link_lldp_emit_enabled(link
)) {
1726 r
= link_lldp_emit_start(link
);
1728 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1734 bool link_has_carrier(Link
*link
) {
1735 /* see Documentation/networking/operstates.txt in the kernel sources */
1737 if (link
->kernel_operstate
== IF_OPER_UP
)
1740 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1741 /* operstate may not be implemented, so fall back to flags */
1742 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1748 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1749 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1754 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1757 r
= sd_netlink_message_get_errno(m
);
1759 /* we warn but don't fail the link, as it may be
1761 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1766 int link_up(Link
*link
) {
1767 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1768 uint8_t ipv6ll_mode
;
1772 assert(link
->network
);
1773 assert(link
->manager
);
1774 assert(link
->manager
->rtnl
);
1776 log_link_debug(link
, "Bringing link up");
1778 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1780 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1782 /* set it free if not enslaved with networkd */
1783 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1784 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1786 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1789 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1791 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1793 if (link
->network
->mac
) {
1794 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1796 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1799 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1801 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1803 if (link_ipv6_enabled(link
)) {
1804 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1805 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1807 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1809 if (!link_ipv6ll_enabled(link
))
1810 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1812 const char *p
= NULL
;
1813 _cleanup_free_
char *stable_secret
= NULL
;
1815 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1816 r
= read_one_line_file(p
, &stable_secret
);
1819 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1821 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1823 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1825 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1827 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1828 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1830 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1833 r
= sd_netlink_message_close_container(req
);
1835 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1838 r
= sd_netlink_message_close_container(req
);
1840 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1842 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1844 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1851 static int link_up_can(Link
*link
) {
1852 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1857 log_link_debug(link
, "Bringing CAN link up");
1859 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1861 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1863 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1865 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1867 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1869 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1876 static int link_set_can(Link
*link
) {
1877 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1881 assert(link
->network
);
1882 assert(link
->manager
);
1883 assert(link
->manager
->rtnl
);
1885 log_link_debug(link
, "link_set_can");
1887 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1889 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1891 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1893 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1895 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1897 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1899 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1901 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1903 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1904 struct can_bittiming bt
= {
1905 .bitrate
= link
->network
->can_bitrate
,
1906 .sample_point
= link
->network
->can_sample_point
,
1909 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1910 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1914 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1915 if (link
->network
->can_sample_point
> 0)
1916 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1918 log_link_debug(link
, "Using default sample point");
1920 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1922 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1925 if (link
->network
->can_restart_us
> 0) {
1926 char time_string
[FORMAT_TIMESPAN_MAX
];
1927 uint64_t restart_ms
;
1929 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1932 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1934 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1936 if (restart_ms
> UINT32_MAX
) {
1937 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1941 log_link_debug(link
, "Setting restart = %s", time_string
);
1943 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1945 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1948 r
= sd_netlink_message_close_container(m
);
1950 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1952 r
= sd_netlink_message_close_container(m
);
1954 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1956 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, link_set_handler
, link
, 0, NULL
);
1958 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1962 if (!(link
->flags
& IFF_UP
)) {
1963 r
= link_up_can(link
);
1965 link_enter_failed(link
);
1970 log_link_debug(link
, "link_set_can done");
1975 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1976 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1981 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1984 r
= sd_netlink_message_get_errno(m
);
1986 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1988 if (streq_ptr(link
->kind
, "can")) {
1996 int link_down(Link
*link
) {
1997 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2001 assert(link
->manager
);
2002 assert(link
->manager
->rtnl
);
2004 log_link_debug(link
, "Bringing link down");
2006 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2007 RTM_SETLINK
, link
->ifindex
);
2009 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2011 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2013 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2015 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
2017 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2024 static int link_handle_bound_to_list(Link
*link
) {
2028 bool required_up
= false;
2029 bool link_is_up
= false;
2033 if (hashmap_isempty(link
->bound_to_links
))
2036 if (link
->flags
& IFF_UP
)
2039 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2040 if (link_has_carrier(l
)) {
2045 if (!required_up
&& link_is_up
) {
2046 r
= link_down(link
);
2049 } else if (required_up
&& !link_is_up
) {
2058 static int link_handle_bound_by_list(Link
*link
) {
2065 if (hashmap_isempty(link
->bound_by_links
))
2068 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2069 r
= link_handle_bound_to_list(l
);
2077 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2083 if (link
== carrier
)
2086 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2089 r
= hashmap_ensure_allocated(h
, NULL
);
2093 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2100 static int link_new_bound_by_list(Link
*link
) {
2105 bool list_updated
= false;
2108 assert(link
->manager
);
2112 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2113 if (!carrier
->network
)
2116 if (strv_isempty(carrier
->network
->bind_carrier
))
2119 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2120 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2124 list_updated
= true;
2131 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2132 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2136 link_dirty(carrier
);
2142 static int link_new_bound_to_list(Link
*link
) {
2147 bool list_updated
= false;
2150 assert(link
->manager
);
2155 if (strv_isempty(link
->network
->bind_carrier
))
2160 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2161 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2162 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2166 list_updated
= true;
2173 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2174 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2178 link_dirty(carrier
);
2184 static int link_new_carrier_maps(Link
*link
) {
2187 r
= link_new_bound_by_list(link
);
2191 r
= link_handle_bound_by_list(link
);
2195 r
= link_new_bound_to_list(link
);
2199 r
= link_handle_bound_to_list(link
);
2206 static void link_free_bound_to_list(Link
*link
) {
2210 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2211 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2213 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2214 link_dirty(bound_to
);
2220 static void link_free_bound_by_list(Link
*link
) {
2224 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2225 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2227 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2228 link_dirty(bound_by
);
2229 link_handle_bound_to_list(bound_by
);
2236 static void link_free_carrier_maps(Link
*link
) {
2237 bool list_updated
= false;
2241 if (!hashmap_isempty(link
->bound_to_links
)) {
2242 link_free_bound_to_list(link
);
2243 list_updated
= true;
2246 if (!hashmap_isempty(link
->bound_by_links
)) {
2247 link_free_bound_by_list(link
);
2248 list_updated
= true;
2257 void link_drop(Link
*link
) {
2258 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2261 link_set_state(link
, LINK_STATE_LINGER
);
2263 link_free_carrier_maps(link
);
2265 log_link_debug(link
, "Link removed");
2267 (void) unlink(link
->state_file
);
2273 static int link_joined(Link
*link
) {
2277 assert(link
->network
);
2279 if (!hashmap_isempty(link
->bound_to_links
)) {
2280 r
= link_handle_bound_to_list(link
);
2283 } else if (!(link
->flags
& IFF_UP
)) {
2286 link_enter_failed(link
);
2291 if (link
->network
->bridge
) {
2292 r
= link_set_bridge(link
);
2294 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2297 if (link
->network
->bond
) {
2298 r
= link_bond_set(link
);
2300 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2303 if (link
->network
->use_br_vlan
&&
2304 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2305 r
= link_set_bridge_vlan(link
);
2307 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2310 /* Skip setting up addresses until it gets carrier,
2311 or it would try to set addresses twice,
2312 which is bad for non-idempotent steps. */
2313 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2316 return link_enter_set_addresses(link
);
2319 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2320 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2324 assert(link
->network
);
2328 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2331 r
= sd_netlink_message_get_errno(m
);
2332 if (r
< 0 && r
!= -EEXIST
) {
2333 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2334 link_enter_failed(link
);
2337 log_link_debug(link
, "Joined netdev");
2339 if (link
->enslaving
<= 0)
2345 static int link_enter_join_netdev(Link
*link
) {
2351 assert(link
->network
);
2352 assert(link
->state
== LINK_STATE_PENDING
);
2354 link_set_state(link
, LINK_STATE_ENSLAVING
);
2358 if (!link
->network
->bridge
&&
2359 !link
->network
->bond
&&
2360 !link
->network
->vrf
&&
2361 hashmap_isempty(link
->network
->stacked_netdevs
))
2362 return link_joined(link
);
2364 if (link
->network
->bond
) {
2365 log_struct(LOG_DEBUG
,
2366 LOG_LINK_INTERFACE(link
),
2367 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2368 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2370 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2372 log_struct_errno(LOG_WARNING
, r
,
2373 LOG_LINK_INTERFACE(link
),
2374 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2375 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2376 link_enter_failed(link
);
2383 if (link
->network
->bridge
) {
2384 log_struct(LOG_DEBUG
,
2385 LOG_LINK_INTERFACE(link
),
2386 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2387 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2389 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2391 log_struct_errno(LOG_WARNING
, r
,
2392 LOG_LINK_INTERFACE(link
),
2393 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2394 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2395 link_enter_failed(link
);
2402 if (link
->network
->vrf
) {
2403 log_struct(LOG_DEBUG
,
2404 LOG_LINK_INTERFACE(link
),
2405 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2406 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2408 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2410 log_struct_errno(LOG_WARNING
, r
,
2411 LOG_LINK_INTERFACE(link
),
2412 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2413 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2414 link_enter_failed(link
);
2421 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2423 if (netdev
->ifindex
> 0) {
2428 log_struct(LOG_DEBUG
,
2429 LOG_LINK_INTERFACE(link
),
2430 LOG_NETDEV_INTERFACE(netdev
),
2431 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2433 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2435 log_struct_errno(LOG_WARNING
, r
,
2436 LOG_LINK_INTERFACE(link
),
2437 LOG_NETDEV_INTERFACE(netdev
),
2438 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2439 link_enter_failed(link
);
2449 static int link_set_ipv4_forward(Link
*link
) {
2452 if (!link_ipv4_forward_enabled(link
))
2455 /* We propagate the forwarding flag from one interface to the
2456 * global setting one way. This means: as long as at least one
2457 * interface was configured at any time that had IP forwarding
2458 * enabled the setting will stay on for good. We do this
2459 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2460 * somewhat in sync (see below). */
2462 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2464 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2469 static int link_set_ipv6_forward(Link
*link
) {
2472 if (!link_ipv6_forward_enabled(link
))
2475 /* On Linux, the IPv6 stack does not know a per-interface
2476 * packet forwarding setting: either packet forwarding is on
2477 * for all, or off for all. We hence don't bother with a
2478 * per-interface setting, but simply propagate the interface
2479 * flag, if it is set, to the global flag, one-way. Note that
2480 * while IPv4 would allow a per-interface flag, we expose the
2481 * same behaviour there and also propagate the setting from
2482 * one to all, to keep things simple (see above). */
2484 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2486 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2491 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2492 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2493 IPv6PrivacyExtensions s
;
2494 const char *p
= NULL
;
2497 s
= link_ipv6_privacy_extensions(link
);
2501 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2502 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2504 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2506 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2511 static int link_set_ipv6_accept_ra(Link
*link
) {
2512 const char *p
= NULL
;
2515 /* Make this a NOP if IPv6 is not available */
2516 if (!socket_ipv6_is_supported())
2519 if (link
->flags
& IFF_LOOPBACK
)
2525 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2527 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2528 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2530 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2535 static int link_set_ipv6_dad_transmits(Link
*link
) {
2536 char buf
[DECIMAL_STR_MAX(int) + 1];
2537 const char *p
= NULL
;
2540 /* Make this a NOP if IPv6 is not available */
2541 if (!socket_ipv6_is_supported())
2544 if (link
->flags
& IFF_LOOPBACK
)
2550 if (link
->network
->ipv6_dad_transmits
< 0)
2553 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2554 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2556 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2558 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2563 static int link_set_ipv6_hop_limit(Link
*link
) {
2564 char buf
[DECIMAL_STR_MAX(int) + 1];
2565 const char *p
= NULL
;
2568 /* Make this a NOP if IPv6 is not available */
2569 if (!socket_ipv6_is_supported())
2572 if (link
->flags
& IFF_LOOPBACK
)
2578 if (link
->network
->ipv6_hop_limit
< 0)
2581 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2582 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2584 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2586 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2591 static int link_set_ipv6_mtu(Link
*link
) {
2592 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2593 const char *p
= NULL
;
2596 /* Make this a NOP if IPv6 is not available */
2597 if (!socket_ipv6_is_supported())
2600 if (link
->flags
& IFF_LOOPBACK
)
2603 if (link
->network
->ipv6_mtu
== 0)
2606 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2608 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2610 r
= write_string_file(p
, buf
, 0);
2612 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2617 static int link_drop_foreign_config(Link
*link
) {
2623 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2624 /* we consider IPv6LL addresses to be managed by the kernel */
2625 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2628 r
= address_remove(address
, link
, link_address_remove_handler
);
2633 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2634 /* do not touch routes managed by the kernel */
2635 if (route
->protocol
== RTPROT_KERNEL
)
2638 r
= route_remove(route
, link
, link_route_remove_handler
);
2646 static int link_drop_config(Link
*link
) {
2647 Address
*address
, *pool_address
;
2652 SET_FOREACH(address
, link
->addresses
, i
) {
2653 /* we consider IPv6LL addresses to be managed by the kernel */
2654 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2657 r
= address_remove(address
, link
, link_address_remove_handler
);
2661 /* If this address came from an address pool, clean up the pool */
2662 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2663 if (address_equal(address
, pool_address
)) {
2664 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2665 address_free(pool_address
);
2671 SET_FOREACH(route
, link
->routes
, i
) {
2672 /* do not touch routes managed by the kernel */
2673 if (route
->protocol
== RTPROT_KERNEL
)
2676 r
= route_remove(route
, link
, link_route_remove_handler
);
2686 static int link_update_lldp(Link
*link
) {
2694 if (link
->flags
& IFF_UP
) {
2695 r
= sd_lldp_start(link
->lldp
);
2697 log_link_debug(link
, "Started LLDP.");
2699 r
= sd_lldp_stop(link
->lldp
);
2701 log_link_debug(link
, "Stopped LLDP.");
2707 static int link_configure_can(Link
*link
) {
2710 if (streq_ptr(link
->kind
, "can")) {
2711 /* The CAN interface must be down to configure bitrate, etc... */
2712 if ((link
->flags
& IFF_UP
)) {
2713 r
= link_down(link
);
2715 link_enter_failed(link
);
2722 return link_set_can(link
);
2725 if (!(link
->flags
& IFF_UP
)) {
2726 r
= link_up_can(link
);
2728 link_enter_failed(link
);
2736 static int link_configure(Link
*link
) {
2740 assert(link
->network
);
2741 assert(link
->state
== LINK_STATE_PENDING
);
2743 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2744 return link_configure_can(link
);
2746 /* Drop foreign config, but ignore loopback or critical devices.
2747 * We do not want to remove loopback address or addresses used for root NFS. */
2748 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2749 r
= link_drop_foreign_config(link
);
2754 r
= link_set_proxy_arp(link
);
2758 r
= ipv6_proxy_ndp_addresses_configure(link
);
2762 r
= link_set_ipv4_forward(link
);
2766 r
= link_set_ipv6_forward(link
);
2770 r
= link_set_ipv6_privacy_extensions(link
);
2774 r
= link_set_ipv6_accept_ra(link
);
2778 r
= link_set_ipv6_dad_transmits(link
);
2782 r
= link_set_ipv6_hop_limit(link
);
2786 r
= link_set_flags(link
);
2790 r
= link_set_ipv6_mtu(link
);
2794 if (link_ipv4ll_enabled(link
)) {
2795 r
= ipv4ll_configure(link
);
2800 if (link_dhcp4_enabled(link
)) {
2801 r
= dhcp4_set_promote_secondaries(link
);
2805 r
= dhcp4_configure(link
);
2810 if (link_dhcp4_server_enabled(link
)) {
2811 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2815 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2820 if (link_dhcp6_enabled(link
) ||
2821 link_ipv6_accept_ra_enabled(link
)) {
2822 r
= dhcp6_configure(link
);
2827 if (link_ipv6_accept_ra_enabled(link
)) {
2828 r
= ndisc_configure(link
);
2833 if (link_radv_enabled(link
)) {
2834 r
= radv_configure(link
);
2839 if (link_lldp_rx_enabled(link
)) {
2840 r
= sd_lldp_new(&link
->lldp
);
2844 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2848 r
= sd_lldp_match_capabilities(link
->lldp
,
2849 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2850 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2851 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2855 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2859 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2863 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2867 r
= link_update_lldp(link
);
2872 if (link
->network
->mtu
> 0) {
2873 r
= link_set_mtu(link
, link
->network
->mtu
);
2878 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2879 r
= link_acquire_conf(link
);
2884 return link_enter_join_netdev(link
);
2887 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2889 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2894 assert(link
->ifname
);
2895 assert(link
->manager
);
2897 if (link
->state
!= LINK_STATE_PENDING
)
2900 log_link_debug(link
, "Link state is up-to-date");
2902 r
= link_new_bound_by_list(link
);
2906 r
= link_handle_bound_by_list(link
);
2910 if (!link
->network
) {
2911 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
2912 &link
->mac
, &network
);
2914 link_enter_unmanaged(link
);
2916 } else if (r
== 0 && network
->unmanaged
) {
2917 link_enter_unmanaged(link
);
2922 if (link
->flags
& IFF_LOOPBACK
) {
2923 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2924 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2926 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2927 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2929 if (network
->dhcp_server
)
2930 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2933 r
= network_apply(network
, link
);
2938 r
= link_new_bound_to_list(link
);
2942 r
= link_configure(link
);
2949 int link_initialized(Link
*link
, sd_device
*device
) {
2950 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2954 assert(link
->manager
);
2955 assert(link
->manager
->rtnl
);
2958 if (link
->state
!= LINK_STATE_PENDING
)
2961 if (link
->sd_device
)
2964 log_link_debug(link
, "udev initialized link");
2966 link
->sd_device
= sd_device_ref(device
);
2968 /* udev has initialized the link, but we don't know if we have yet
2969 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2970 * when it returns we know that the pending NEWLINKs have already been
2971 * processed and that we are up-to-date */
2973 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2978 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2979 link_initialized_and_synced
, link
, 0, NULL
);
2988 static int link_load(Link
*link
) {
2989 _cleanup_free_
char *network_file
= NULL
,
2992 *dhcp4_address
= NULL
,
2993 *ipv4ll_address
= NULL
;
2994 union in_addr_union address
;
2995 union in_addr_union route_dst
;
3001 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
3002 "NETWORK_FILE", &network_file
,
3003 "ADDRESSES", &addresses
,
3005 "DHCP4_ADDRESS", &dhcp4_address
,
3006 "IPV4LL_ADDRESS", &ipv4ll_address
,
3008 if (r
< 0 && r
!= -ENOENT
)
3009 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3016 suffix
= strrchr(network_file
, '.');
3018 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3019 goto network_file_fail
;
3023 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3025 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3026 goto network_file_fail
;
3029 r
= network_apply(network
, link
);
3031 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3040 _cleanup_free_
char *address_str
= NULL
;
3041 char *prefixlen_str
;
3043 unsigned char prefixlen
;
3045 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3047 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3053 prefixlen_str
= strchr(address_str
, '/');
3054 if (!prefixlen_str
) {
3055 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3059 *prefixlen_str
++ = '\0';
3061 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3063 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3067 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3069 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3073 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3075 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3084 _cleanup_free_
char *route_str
= NULL
;
3085 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3087 char *prefixlen_str
;
3089 unsigned char prefixlen
, tos
, table
;
3092 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3094 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3100 prefixlen_str
= strchr(route_str
, '/');
3101 if (!prefixlen_str
) {
3102 log_link_debug(link
, "Failed to parse route %s", route_str
);
3106 *prefixlen_str
++ = '\0';
3108 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3110 log_link_debug(link
,
3111 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3116 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3118 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3122 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3124 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3126 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3127 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3128 0, route_expire_handler
, route
);
3130 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3133 route
->lifetime
= lifetime
;
3134 sd_event_source_unref(route
->expire
);
3135 route
->expire
= TAKE_PTR(expire
);
3139 if (dhcp4_address
) {
3140 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3142 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3143 goto dhcp4_address_fail
;
3146 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3148 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3150 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3152 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3157 if (ipv4ll_address
) {
3158 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3160 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3161 goto ipv4ll_address_fail
;
3164 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3166 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3168 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3170 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3173 ipv4ll_address_fail
:
3178 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3179 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3180 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3189 r
= link_new(m
, message
, ret
);
3195 log_link_debug(link
, "Link %d added", link
->ifindex
);
3197 r
= link_load(link
);
3201 if (detect_container() <= 0) {
3202 /* not in a container, udev will be around */
3203 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3204 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3206 log_link_warning_errno(link
, r
, "Could not find device: %m");
3210 r
= sd_device_get_is_initialized(device
, &initialized
);
3212 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3217 log_link_debug(link
, "link pending udev initialization...");
3221 r
= link_initialized(link
, device
);
3225 /* we are calling a callback directly, so must take a ref */
3228 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3235 link_enter_failed(link
);
3239 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3244 log_link_info(link
, "Gained IPv6LL");
3246 link
->ipv6ll_address
= *address
;
3247 link_check_ready(link
);
3249 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3250 r
= link_acquire_ipv6_conf(link
);
3252 link_enter_failed(link
);
3260 static int link_carrier_gained(Link
*link
) {
3265 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3266 r
= link_acquire_conf(link
);
3268 link_enter_failed(link
);
3272 r
= link_enter_set_addresses(link
);
3277 r
= link_handle_bound_by_list(link
);
3284 static int link_carrier_lost(Link
*link
) {
3289 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3290 setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3291 configuration and stop the clients as well. */
3292 if (link
->setting_mtu
)
3295 r
= link_stop_clients(link
);
3297 link_enter_failed(link
);
3301 if (link_dhcp4_server_enabled(link
))
3302 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3304 r
= link_drop_config(link
);
3308 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3309 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3310 r
= link_drop_foreign_config(link
);
3315 r
= link_handle_bound_by_list(link
);
3322 int link_carrier_reset(Link
*link
) {
3327 if (link_has_carrier(link
)) {
3328 r
= link_carrier_lost(link
);
3332 r
= link_carrier_gained(link
);
3336 log_link_info(link
, "Reset carrier");
3342 int link_update(Link
*link
, sd_netlink_message
*m
) {
3343 struct ether_addr mac
;
3346 bool had_carrier
, carrier_gained
, carrier_lost
;
3350 assert(link
->ifname
);
3353 if (link
->state
== LINK_STATE_LINGER
) {
3355 log_link_info(link
, "Link readded");
3356 link_set_state(link
, LINK_STATE_ENSLAVING
);
3358 r
= link_new_carrier_maps(link
);
3363 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3364 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3365 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3367 if (link
->state
== LINK_STATE_PENDING
) {
3368 r
= free_and_strdup(&link
->ifname
, ifname
);
3372 Manager
*manager
= link
->manager
;
3375 r
= link_add(manager
, m
, &link
);
3381 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3382 if (r
>= 0 && mtu
> 0) {
3384 if (link
->original_mtu
== 0) {
3385 link
->original_mtu
= mtu
;
3386 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3389 if (link
->dhcp_client
) {
3390 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3393 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3397 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3399 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3403 /* The kernel may broadcast NEWLINK messages without the MAC address
3404 set, simply ignore them. */
3405 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3407 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3410 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3413 log_link_debug(link
, "MAC address: "
3414 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3415 mac
.ether_addr_octet
[0],
3416 mac
.ether_addr_octet
[1],
3417 mac
.ether_addr_octet
[2],
3418 mac
.ether_addr_octet
[3],
3419 mac
.ether_addr_octet
[4],
3420 mac
.ether_addr_octet
[5]);
3423 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3425 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3428 if (link
->dhcp_client
) {
3429 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3430 (const uint8_t *) &link
->mac
,
3434 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3436 switch (link
->network
->dhcp_client_identifier
) {
3437 case DHCP_CLIENT_ID_DUID
: {
3438 const DUID
*duid
= link_duid(link
);
3440 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3441 link
->network
->iaid
,
3443 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3444 duid
->raw_data_len
);
3446 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3449 case DHCP_CLIENT_ID_DUID_ONLY
: {
3450 const DUID
*duid
= link_duid(link
);
3452 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
3454 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3455 duid
->raw_data_len
);
3457 return log_link_warning_errno(link
, r
, "Could not update DUID in DHCP client: %m");
3460 case DHCP_CLIENT_ID_MAC
:
3461 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3463 (const uint8_t *)&link
->mac
,
3466 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3469 assert_not_reached("Unknown client identifier type.");
3473 if (link
->dhcp6_client
) {
3474 const DUID
* duid
= link_duid(link
);
3476 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3477 (const uint8_t *) &link
->mac
,
3481 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3483 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3484 link
->network
->iaid
);
3486 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3488 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3490 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3491 duid
->raw_data_len
);
3493 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3497 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3499 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3503 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3505 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3510 had_carrier
= link_has_carrier(link
);
3512 r
= link_update_flags(link
, m
);
3516 r
= link_update_lldp(link
);
3520 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3521 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3523 if (carrier_gained
) {
3524 log_link_info(link
, "Gained carrier");
3526 r
= link_carrier_gained(link
);
3529 } else if (carrier_lost
) {
3530 log_link_info(link
, "Lost carrier");
3532 r
= link_carrier_lost(link
);
3540 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3548 if (hashmap_isempty(h
))
3552 HASHMAP_FOREACH(link
, h
, i
) {
3556 fprintf(f
, "%i", link
->ifindex
);
3563 int link_save(Link
*link
) {
3564 _cleanup_free_
char *temp_path
= NULL
;
3565 _cleanup_fclose_
FILE *f
= NULL
;
3566 const char *admin_state
, *oper_state
;
3573 assert(link
->state_file
);
3574 assert(link
->lease_file
);
3575 assert(link
->manager
);
3577 if (link
->state
== LINK_STATE_LINGER
) {
3578 unlink(link
->state_file
);
3582 link_lldp_save(link
);
3584 admin_state
= link_state_to_string(link
->state
);
3585 assert(admin_state
);
3587 oper_state
= link_operstate_to_string(link
->operstate
);
3590 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3594 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3595 (void) fchmod(fileno(f
), 0644);
3598 "# This is private data. Do not parse.\n"
3601 admin_state
, oper_state
);
3603 if (link
->network
) {
3605 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3606 const char *dhcp_domainname
= NULL
;
3607 char **dhcp6_domains
= NULL
;
3608 char **dhcp_domains
= NULL
;
3611 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3612 yes_no(link
->network
->required_for_online
));
3614 if (link
->dhcp6_client
) {
3615 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3616 if (r
< 0 && r
!= -ENOMSG
)
3617 log_link_debug(link
, "No DHCPv6 lease");
3620 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3625 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3626 _cleanup_free_
char *b
= NULL
;
3628 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3629 &link
->network
->dns
[j
].address
, &b
);
3631 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3641 if (link
->network
->dhcp_use_dns
&&
3643 const struct in_addr
*addresses
;
3645 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3649 serialize_in_addrs(f
, addresses
, r
);
3654 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3655 struct in6_addr
*in6_addrs
;
3657 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3661 serialize_in6_addrs(f
, in6_addrs
, r
);
3666 /* Make sure to flush out old entries before we use the NDISC data */
3669 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3672 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3676 serialize_in6_addrs(f
, &dd
->address
, 1);
3685 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3687 if (link
->network
->dhcp_use_ntp
&&
3689 const struct in_addr
*addresses
;
3691 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3695 serialize_in_addrs(f
, addresses
, r
);
3700 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3701 struct in6_addr
*in6_addrs
;
3704 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3709 serialize_in6_addrs(f
, in6_addrs
, r
);
3713 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3715 fputstrv(f
, hosts
, NULL
, &space
);
3720 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3721 if (link
->dhcp_lease
) {
3722 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3723 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3726 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3729 fputs("DOMAINS=", f
);
3731 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3733 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3736 if (dhcp_domainname
)
3737 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3739 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3741 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3743 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3744 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3749 fputs("ROUTE_DOMAINS=", f
);
3751 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3753 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3756 if (dhcp_domainname
)
3757 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3759 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3761 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3763 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3764 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3769 fprintf(f
, "LLMNR=%s\n",
3770 resolve_support_to_string(link
->network
->llmnr
));
3771 fprintf(f
, "MDNS=%s\n",
3772 resolve_support_to_string(link
->network
->mdns
));
3774 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3775 fprintf(f
, "DNS_OVER_TLS=%s\n",
3776 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3778 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3779 fprintf(f
, "DNSSEC=%s\n",
3780 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3782 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3785 fputs("DNSSEC_NTA=", f
);
3787 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3788 fputs_with_space(f
, n
, NULL
, &space
);
3792 fputs("ADDRESSES=", f
);
3794 SET_FOREACH(a
, link
->addresses
, i
) {
3795 _cleanup_free_
char *address_str
= NULL
;
3797 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3801 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3806 fputs("ROUTES=", f
);
3808 SET_FOREACH(route
, link
->routes
, i
) {
3809 _cleanup_free_
char *route_str
= NULL
;
3811 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3815 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3816 space
? " " : "", route_str
,
3817 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3824 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3825 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3827 if (link
->dhcp_lease
) {
3828 struct in_addr address
;
3829 const char *tz
= NULL
;
3831 assert(link
->network
);
3833 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3835 fprintf(f
, "TIMEZONE=%s\n", tz
);
3837 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3839 fputs("DHCP4_ADDRESS=", f
);
3840 serialize_in_addrs(f
, &address
, 1);
3844 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3852 unlink(link
->lease_file
);
3855 struct in_addr address
;
3857 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3859 fputs("IPV4LL_ADDRESS=", f
);
3860 serialize_in_addrs(f
, &address
, 1);
3865 r
= fflush_and_check(f
);
3869 if (rename(temp_path
, link
->state_file
) < 0) {
3877 (void) unlink(link
->state_file
);
3879 (void) unlink(temp_path
);
3881 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3884 /* The serialized state in /run is no longer up-to-date. */
3885 void link_dirty(Link
*link
) {
3890 /* mark manager dirty as link is dirty */
3891 manager_dirty(link
->manager
);
3893 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3895 /* allocation errors are ignored */
3898 r
= set_put(link
->manager
->dirty_links
, link
);
3900 /* don't take another ref if the link was already dirty */
3906 /* The serialized state in /run is up-to-date */
3907 void link_clean(Link
*link
) {
3909 assert(link
->manager
);
3911 set_remove(link
->manager
->dirty_links
, link
);
3915 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3916 [LINK_STATE_PENDING
] = "pending",
3917 [LINK_STATE_ENSLAVING
] = "configuring",
3918 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3919 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3920 [LINK_STATE_CONFIGURED
] = "configured",
3921 [LINK_STATE_UNMANAGED
] = "unmanaged",
3922 [LINK_STATE_FAILED
] = "failed",
3923 [LINK_STATE_LINGER
] = "linger",
3926 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3928 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3929 [LINK_OPERSTATE_OFF
] = "off",
3930 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3931 [LINK_OPERSTATE_DORMANT
] = "dormant",
3932 [LINK_OPERSTATE_CARRIER
] = "carrier",
3933 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3934 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3937 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);