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-radv.h"
24 #include "networkd-routing-policy-rule.h"
26 #include "socket-util.h"
27 #include "stdio-util.h"
28 #include "string-table.h"
30 #include "tmpfile-util.h"
34 DUID
* link_get_duid(Link
*link
) {
35 if (link
->network
->duid
.type
!= _DUID_TYPE_INVALID
)
36 return &link
->network
->duid
;
38 return &link
->manager
->duid
;
41 static bool link_dhcp6_enabled(Link
*link
) {
44 if (!socket_ipv6_is_supported())
47 if (link
->flags
& IFF_LOOPBACK
)
53 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
56 static bool link_dhcp4_enabled(Link
*link
) {
59 if (link
->flags
& IFF_LOOPBACK
)
65 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
68 static bool link_dhcp4_server_enabled(Link
*link
) {
71 if (link
->flags
& IFF_LOOPBACK
)
77 return link
->network
->dhcp_server
;
80 static bool link_ipv4ll_enabled(Link
*link
) {
83 if (link
->flags
& IFF_LOOPBACK
)
89 if (streq_ptr(link
->kind
, "wireguard"))
92 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
95 static bool link_ipv6ll_enabled(Link
*link
) {
98 if (!socket_ipv6_is_supported())
101 if (link
->flags
& IFF_LOOPBACK
)
107 if (streq_ptr(link
->kind
, "wireguard"))
110 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
113 static bool link_ipv6_enabled(Link
*link
) {
116 if (!socket_ipv6_is_supported())
119 if (link
->network
->bridge
)
122 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
123 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
126 static bool link_radv_enabled(Link
*link
) {
129 if (!link_ipv6ll_enabled(link
))
132 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
135 static bool link_lldp_rx_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
141 if (link
->iftype
!= ARPHRD_ETHER
)
147 /* LLDP should be handled on bridge slaves as those have a direct
148 * connection to their peers not on the bridge master. Linux doesn't
149 * even (by default) forward lldp packets to the bridge master.*/
150 if (streq_ptr("bridge", link
->kind
))
153 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
156 static bool link_lldp_emit_enabled(Link
*link
) {
159 if (link
->flags
& IFF_LOOPBACK
)
162 if (link
->iftype
!= ARPHRD_ETHER
)
168 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
171 static bool link_ipv4_forward_enabled(Link
*link
) {
174 if (link
->flags
& IFF_LOOPBACK
)
180 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
183 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
186 static bool link_ipv6_forward_enabled(Link
*link
) {
189 if (!socket_ipv6_is_supported())
192 if (link
->flags
& IFF_LOOPBACK
)
198 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
201 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
204 static bool link_proxy_arp_enabled(Link
*link
) {
207 if (link
->flags
& IFF_LOOPBACK
)
213 if (link
->network
->proxy_arp
< 0)
219 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
222 if (!socket_ipv6_is_supported())
225 if (link
->flags
& IFF_LOOPBACK
)
231 if (!link_ipv6ll_enabled(link
))
234 /* If unset use system default (enabled if local forwarding is disabled.
235 * disabled if local forwarding is enabled).
236 * If set, ignore or enforce RA independent of local forwarding state.
238 if (link
->network
->ipv6_accept_ra
< 0)
239 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
240 return !link_ipv6_forward_enabled(link
);
241 else if (link
->network
->ipv6_accept_ra
> 0)
242 /* accept RA even if ip_forward is enabled */
249 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
252 if (!socket_ipv6_is_supported())
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
255 if (link
->flags
& IFF_LOOPBACK
)
256 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
259 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
261 return link
->network
->ipv6_privacy_extensions
;
264 static int link_enable_ipv6(Link
*link
) {
265 const char *p
= NULL
;
269 if (link
->flags
& IFF_LOOPBACK
)
272 disabled
= !link_ipv6_enabled(link
);
274 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
276 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
278 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
279 enable_disable(!disabled
), link
->ifname
);
281 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
286 void link_update_operstate(Link
*link
) {
287 LinkOperationalState operstate
;
290 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
291 operstate
= LINK_OPERSTATE_DORMANT
;
292 else if (link_has_carrier(link
)) {
294 uint8_t scope
= RT_SCOPE_NOWHERE
;
297 /* if we have carrier, check what addresses we have */
298 SET_FOREACH(address
, link
->addresses
, i
) {
299 if (!address_is_ready(address
))
302 if (address
->scope
< scope
)
303 scope
= address
->scope
;
306 /* for operstate we also take foreign addresses into account */
307 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
308 if (!address_is_ready(address
))
311 if (address
->scope
< scope
)
312 scope
= address
->scope
;
315 if (scope
< RT_SCOPE_SITE
)
316 /* universally accessible addresses found */
317 operstate
= LINK_OPERSTATE_ROUTABLE
;
318 else if (scope
< RT_SCOPE_HOST
)
319 /* only link or site local addresses found */
320 operstate
= LINK_OPERSTATE_DEGRADED
;
322 /* no useful addresses found */
323 operstate
= LINK_OPERSTATE_CARRIER
;
324 } else if (link
->flags
& IFF_UP
)
325 operstate
= LINK_OPERSTATE_NO_CARRIER
;
327 operstate
= LINK_OPERSTATE_OFF
;
329 if (link
->operstate
!= operstate
) {
330 link
->operstate
= operstate
;
331 link_send_changed(link
, "OperationalState", NULL
);
336 #define FLAG_STRING(string, flag, old, new) \
337 (((old ^ new) & flag) \
338 ? ((old & flag) ? (" -" string) : (" +" string)) \
341 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
342 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
348 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
350 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
352 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
354 /* if we got a message without operstate, take it to mean
355 the state was unchanged */
356 operstate
= link
->kernel_operstate
;
358 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
361 if (link
->flags
!= flags
) {
362 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",
363 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
364 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
365 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
366 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
367 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
368 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
369 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
370 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
371 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
372 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
373 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
374 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
375 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
376 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
377 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
378 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
379 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
380 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
381 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
383 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
384 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
385 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
386 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
387 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
388 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
389 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
390 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
392 /* link flags are currently at most 18 bits, let's align to
394 if (unknown_flags_added
)
396 "Unknown link flags gained: %#.5x (ignoring)",
397 unknown_flags_added
);
399 if (unknown_flags_removed
)
401 "Unknown link flags lost: %#.5x (ignoring)",
402 unknown_flags_removed
);
406 link
->kernel_operstate
= operstate
;
408 link_update_operstate(link
);
413 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_unref
);
415 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
416 _cleanup_(link_unrefp
) Link
*link
= NULL
;
418 const char *ifname
, *kind
= NULL
;
420 unsigned short iftype
;
426 /* check for link kind */
427 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
429 (void) sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
430 r
= sd_netlink_message_exit_container(message
);
435 r
= sd_netlink_message_get_type(message
, &type
);
438 else if (type
!= RTM_NEWLINK
)
441 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
444 else if (ifindex
<= 0)
447 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
451 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
462 .state
= LINK_STATE_PENDING
,
463 .rtnl_extended_attrs
= true,
468 link
->ifname
= strdup(ifname
);
473 link
->kind
= strdup(kind
);
478 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t *)&link
->master_ifindex
);
480 log_link_debug_errno(link
, r
, "New device has no master, continuing without");
482 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
484 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
486 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
489 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
492 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
495 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
499 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
503 r
= link_update_flags(link
, message
);
507 *ret
= TAKE_PTR(link
);
512 static void link_detach_from_manager(Link
*link
) {
513 if (!link
|| !link
->manager
)
516 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
517 set_remove(link
->manager
->links_requesting_uuid
, link
);
520 link
->manager
= NULL
;
523 static Link
*link_free(Link
*link
) {
531 while ((route
= set_first(link
->routes
)))
534 while ((route
= set_first(link
->routes_foreign
)))
537 link
->routes
= set_free(link
->routes
);
538 link
->routes_foreign
= set_free(link
->routes_foreign
);
540 while ((address
= set_first(link
->addresses
)))
541 address_free(address
);
543 while ((address
= set_first(link
->addresses_foreign
)))
544 address_free(address
);
546 link
->addresses
= set_free(link
->addresses
);
547 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
549 while ((address
= link
->pool_addresses
)) {
550 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
551 address_free(address
);
554 sd_dhcp_server_unref(link
->dhcp_server
);
555 sd_dhcp_client_unref(link
->dhcp_client
);
556 sd_dhcp_lease_unref(link
->dhcp_lease
);
558 link_lldp_emit_stop(link
);
560 free(link
->lease_file
);
562 sd_lldp_unref(link
->lldp
);
563 free(link
->lldp_file
);
567 sd_ipv4ll_unref(link
->ipv4ll
);
568 sd_dhcp6_client_unref(link
->dhcp6_client
);
569 sd_ndisc_unref(link
->ndisc
);
570 sd_radv_unref(link
->radv
);
572 link_detach_from_manager(link
);
578 (void) unlink(link
->state_file
);
579 free(link
->state_file
);
581 sd_device_unref(link
->sd_device
);
583 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
584 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
585 hashmap_free(link
->bound_to_links
);
587 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
588 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
589 hashmap_free(link
->bound_by_links
);
594 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
596 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
603 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
612 static void link_set_state(Link
*link
, LinkState state
) {
615 if (link
->state
== state
)
620 link_send_changed(link
, "AdministrativeState", NULL
);
623 static void link_enter_unmanaged(Link
*link
) {
626 log_link_debug(link
, "Unmanaged");
628 link_set_state(link
, LINK_STATE_UNMANAGED
);
633 static int link_stop_clients(Link
*link
) {
637 assert(link
->manager
);
638 assert(link
->manager
->event
);
640 if (link
->dhcp_client
) {
641 k
= sd_dhcp_client_stop(link
->dhcp_client
);
643 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
647 k
= sd_ipv4ll_stop(link
->ipv4ll
);
649 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
652 if (link
->dhcp6_client
) {
653 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
655 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
659 k
= sd_ndisc_stop(link
->ndisc
);
661 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
665 k
= sd_radv_stop(link
->radv
);
667 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
670 link_lldp_emit_stop(link
);
674 void link_enter_failed(Link
*link
) {
677 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
680 log_link_warning(link
, "Failed");
682 link_set_state(link
, LINK_STATE_FAILED
);
684 link_stop_clients(link
);
689 static Address
* link_find_dhcp_server_address(Link
*link
) {
693 assert(link
->network
);
695 /* The first statically configured address if there is any */
696 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
698 if (address
->family
!= AF_INET
)
701 if (in_addr_is_null(address
->family
, &address
->in_addr
))
707 /* If that didn't work, find a suitable address we got from the pool */
708 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
709 if (address
->family
!= AF_INET
)
718 static void link_enter_configured(Link
*link
) {
720 assert(link
->network
);
722 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
725 log_link_info(link
, "Configured");
727 link_set_state(link
, LINK_STATE_CONFIGURED
);
732 void link_check_ready(Link
*link
) {
738 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
744 if (!link
->static_routes_configured
)
747 if (!link
->routing_policy_rules_configured
)
750 if (link_ipv4ll_enabled(link
))
751 if (!link
->ipv4ll_address
||
755 if (!link
->network
->bridge
) {
757 if (link_ipv6ll_enabled(link
))
758 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
761 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
762 !link
->dhcp4_configured
) ||
763 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
764 !link
->dhcp6_configured
) ||
765 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
766 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
769 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
773 SET_FOREACH(a
, link
->addresses
, i
)
774 if (!address_is_ready(a
))
777 if (link
->state
!= LINK_STATE_CONFIGURED
)
778 link_enter_configured(link
);
783 static int link_set_routing_policy_rule(Link
*link
) {
784 RoutingPolicyRule
*rule
, *rrule
= NULL
;
788 assert(link
->network
);
790 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
791 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
792 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
,
793 rule
->protocol
, &rule
->sport
, &rule
->dport
, &rrule
);
795 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
799 r
= routing_policy_rule_configure(rule
, link
, NULL
, false);
801 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
802 link_enter_failed(link
);
806 link
->routing_policy_rule_messages
++;
809 routing_policy_rule_purge(link
->manager
, link
);
810 if (link
->routing_policy_rule_messages
== 0) {
811 link
->routing_policy_rules_configured
= true;
812 link_check_ready(link
);
814 log_link_debug(link
, "Setting routing policy rules");
819 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
823 assert(link
->route_messages
> 0);
824 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
825 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
828 link
->route_messages
--;
830 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
833 r
= sd_netlink_message_get_errno(m
);
834 if (r
< 0 && r
!= -EEXIST
)
835 log_link_warning_errno(link
, r
, "Could not set route: %m");
837 if (link
->route_messages
== 0) {
838 log_link_debug(link
, "Routes set");
839 link
->static_routes_configured
= true;
840 link_check_ready(link
);
846 static int link_enter_set_routes(Link
*link
) {
848 PHASE_NON_GATEWAY
, /* First phase: Routes without a gateway */
849 PHASE_GATEWAY
, /* Second phase: Routes with a gateway */
856 assert(link
->network
);
857 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
859 (void) link_set_routing_policy_rule(link
);
861 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
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 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
895 assert(link
->ifname
);
896 assert(link
->address_messages
> 0);
897 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
898 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
900 link
->address_messages
--;
902 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
905 r
= sd_netlink_message_get_errno(m
);
906 if (r
< 0 && r
!= -EEXIST
)
907 log_link_warning_errno(link
, r
, "could not set address: %m");
909 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
911 if (link
->address_messages
== 0) {
912 log_link_debug(link
, "Addresses set");
913 link_enter_set_routes(link
);
919 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
920 _cleanup_free_
struct in_addr
*addresses
= NULL
;
921 size_t n_addresses
= 0, n_allocated
= 0;
924 log_debug("Copying DNS server information from %s", link
->ifname
);
929 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
932 /* Only look for IPv4 addresses */
933 if (link
->network
->dns
[i
].family
!= AF_INET
)
936 ia
= link
->network
->dns
[i
].address
.in
;
938 /* Never propagate obviously borked data */
939 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
942 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
945 addresses
[n_addresses
++] = ia
;
948 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
949 const struct in_addr
*da
= NULL
;
952 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
955 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
958 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
963 if (n_addresses
<= 0)
966 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
969 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
970 _cleanup_free_
struct in_addr
*addresses
= NULL
;
971 size_t n_addresses
= 0, n_allocated
= 0;
977 log_debug("Copying NTP server information from %s", link
->ifname
);
979 STRV_FOREACH(a
, link
->network
->ntp
) {
982 /* Only look for IPv4 addresses */
983 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
986 /* Never propagate obviously borked data */
987 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
990 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
993 addresses
[n_addresses
++] = ia
;
996 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
997 const struct in_addr
*da
= NULL
;
1000 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1003 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1006 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1011 if (n_addresses
<= 0)
1014 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1017 static int link_set_bridge_fdb(Link
*link
) {
1018 FdbEntry
*fdb_entry
;
1021 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1022 r
= fdb_entry_configure(link
, fdb_entry
);
1024 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1030 static int link_enter_set_addresses(Link
*link
) {
1031 AddressLabel
*label
;
1036 assert(link
->network
);
1037 assert(link
->state
!= _LINK_STATE_INVALID
);
1039 r
= link_set_bridge_fdb(link
);
1043 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1045 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1046 r
= address_configure(ad
, link
, address_handler
, false);
1048 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1049 link_enter_failed(link
);
1053 link
->address_messages
++;
1056 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1057 r
= address_label_configure(label
, link
, NULL
, false);
1059 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1060 link_enter_failed(link
);
1064 link
->address_label_messages
++;
1067 /* now that we can figure out a default address for the dhcp server,
1069 if (link_dhcp4_server_enabled(link
) && (link
->flags
& IFF_UP
)) {
1071 Link
*uplink
= NULL
;
1072 bool acquired_uplink
= false;
1074 address
= link_find_dhcp_server_address(link
);
1076 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1077 link_enter_failed(link
);
1081 /* use the server address' subnet as the pool */
1082 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1083 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1088 r = sd_dhcp_server_set_router(link->dhcp_server,
1089 &main_address->in_addr.in);
1094 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1095 r
= sd_dhcp_server_set_max_lease_time(
1097 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1102 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1103 r
= sd_dhcp_server_set_default_lease_time(
1105 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1110 if (link
->network
->dhcp_server_emit_dns
) {
1112 if (link
->network
->n_dhcp_server_dns
> 0)
1113 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1115 uplink
= manager_find_uplink(link
->manager
, link
);
1116 acquired_uplink
= true;
1119 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1122 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1125 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1128 if (link
->network
->dhcp_server_emit_ntp
) {
1130 if (link
->network
->n_dhcp_server_ntp
> 0)
1131 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1133 if (!acquired_uplink
)
1134 uplink
= manager_find_uplink(link
->manager
, link
);
1137 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1140 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1144 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1147 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1149 return log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1151 if (link
->network
->dhcp_server_emit_timezone
) {
1152 _cleanup_free_
char *buffer
= NULL
;
1153 const char *tz
= NULL
;
1155 if (link
->network
->dhcp_server_timezone
)
1156 tz
= link
->network
->dhcp_server_timezone
;
1158 r
= get_timezone(&buffer
);
1160 log_warning_errno(r
, "Failed to determine timezone: %m");
1166 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1172 r
= sd_dhcp_server_start(link
->dhcp_server
);
1174 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1176 link_enter_failed(link
);
1181 log_link_debug(link
, "Offering DHCPv4 leases");
1184 if (link
->address_messages
== 0)
1185 link_enter_set_routes(link
);
1187 log_link_debug(link
, "Setting addresses");
1192 static int link_set_bridge_vlan(Link
*link
) {
1195 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1197 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1202 static int link_set_proxy_arp(Link
*link
) {
1203 const char *p
= NULL
;
1206 if (!link_proxy_arp_enabled(link
))
1209 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1211 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
1213 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1218 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1223 log_link_debug(link
, "Set link");
1225 r
= sd_netlink_message_get_errno(m
);
1226 if (r
< 0 && r
!= -EEXIST
) {
1227 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1228 link_enter_failed(link
);
1234 static int link_configure_after_setting_mtu(Link
*link
);
1236 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1241 assert(link
->ifname
);
1243 link
->setting_mtu
= false;
1245 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1248 r
= sd_netlink_message_get_errno(m
);
1250 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1254 log_link_debug(link
, "Setting MTU done.");
1256 if (link
->state
== LINK_STATE_PENDING
)
1257 (void) link_configure_after_setting_mtu(link
);
1262 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1263 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1267 assert(link
->manager
);
1268 assert(link
->manager
->rtnl
);
1270 if (link
->mtu
== mtu
|| link
->setting_mtu
)
1273 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1275 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1277 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1279 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1280 * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1281 (void) link_enable_ipv6(link
);
1283 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1284 * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1285 if (link_ipv6_enabled(link
) && mtu
< IPV6_MIN_MTU
) {
1287 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1288 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1293 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1295 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1297 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_mtu_handler
,
1298 link_netlink_destroy_callback
, link
);
1300 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1303 link
->setting_mtu
= true;
1308 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1313 assert(link
->ifname
);
1315 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1318 r
= sd_netlink_message_get_errno(m
);
1320 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1325 static int link_set_flags(Link
*link
) {
1326 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1327 unsigned ifi_change
= 0;
1328 unsigned ifi_flags
= 0;
1332 assert(link
->manager
);
1333 assert(link
->manager
->rtnl
);
1335 if (link
->flags
& IFF_LOOPBACK
)
1341 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1344 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1346 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1348 if (link
->network
->arp
>= 0) {
1349 ifi_change
|= IFF_NOARP
;
1350 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1353 if (link
->network
->multicast
>= 0) {
1354 ifi_change
|= IFF_MULTICAST
;
1355 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1358 if (link
->network
->allmulticast
>= 0) {
1359 ifi_change
|= IFF_ALLMULTI
;
1360 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1363 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1365 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1367 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1368 link_netlink_destroy_callback
, link
);
1370 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1377 static int link_set_bridge(Link
*link
) {
1378 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1382 assert(link
->network
);
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 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1390 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1392 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1394 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1396 if (link
->network
->use_bpdu
>= 0) {
1397 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1399 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1402 if (link
->network
->hairpin
>= 0) {
1403 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1405 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1408 if (link
->network
->fast_leave
>= 0) {
1409 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1411 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1414 if (link
->network
->allow_port_to_be_root
>= 0) {
1415 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1417 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1421 if (link
->network
->unicast_flood
>= 0) {
1422 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1424 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1428 if (link
->network
->cost
!= 0) {
1429 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1431 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1433 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1434 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1436 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1439 r
= sd_netlink_message_close_container(req
);
1441 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1443 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_set_handler
,
1444 link_netlink_destroy_callback
, link
);
1446 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1453 static int link_bond_set(Link
*link
) {
1454 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1458 assert(link
->network
);
1460 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1462 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1464 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1466 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1468 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1470 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1472 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1474 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1476 if (link
->network
->active_slave
) {
1477 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1479 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1482 if (link
->network
->primary_slave
) {
1483 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1485 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1488 r
= sd_netlink_message_close_container(req
);
1490 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1492 r
= sd_netlink_message_close_container(req
);
1494 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1496 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, set_flags_handler
,
1497 link_netlink_destroy_callback
, link
);
1499 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1506 static int link_lldp_save(Link
*link
) {
1507 _cleanup_free_
char *temp_path
= NULL
;
1508 _cleanup_fclose_
FILE *f
= NULL
;
1509 sd_lldp_neighbor
**l
= NULL
;
1513 assert(link
->lldp_file
);
1516 (void) unlink(link
->lldp_file
);
1520 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1524 (void) unlink(link
->lldp_file
);
1530 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1534 fchmod(fileno(f
), 0644);
1536 for (i
= 0; i
< n
; i
++) {
1541 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1546 (void) fwrite(&u
, 1, sizeof(u
), f
);
1547 (void) fwrite(p
, 1, sz
, f
);
1550 r
= fflush_and_check(f
);
1554 if (rename(temp_path
, link
->lldp_file
) < 0) {
1561 (void) unlink(link
->lldp_file
);
1563 (void) unlink(temp_path
);
1565 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1569 for (i
= 0; i
< n
; i
++)
1570 sd_lldp_neighbor_unref(l
[i
]);
1577 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1578 Link
*link
= userdata
;
1583 (void) link_lldp_save(link
);
1585 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1586 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1588 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1590 r
= link_lldp_emit_start(link
);
1592 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1596 static int link_acquire_ipv6_conf(Link
*link
) {
1601 if (link_ipv6_accept_ra_enabled(link
)) {
1602 assert(link
->ndisc
);
1604 log_link_debug(link
, "Discovering IPv6 routers");
1606 r
= sd_ndisc_start(link
->ndisc
);
1607 if (r
< 0 && r
!= -EBUSY
)
1608 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1611 if (link_radv_enabled(link
)) {
1613 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1615 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1617 r
= sd_radv_start(link
->radv
);
1618 if (r
< 0 && r
!= -EBUSY
)
1619 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1622 (void) dhcp6_request_prefix_delegation(link
);
1627 static int link_acquire_ipv4_conf(Link
*link
) {
1631 assert(link
->network
);
1632 assert(link
->manager
);
1633 assert(link
->manager
->event
);
1635 if (link_ipv4ll_enabled(link
)) {
1636 assert(link
->ipv4ll
);
1638 log_link_debug(link
, "Acquiring IPv4 link-local address");
1640 r
= sd_ipv4ll_start(link
->ipv4ll
);
1642 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1645 if (link_dhcp4_enabled(link
)) {
1646 assert(link
->dhcp_client
);
1648 log_link_debug(link
, "Acquiring DHCPv4 lease");
1650 r
= sd_dhcp_client_start(link
->dhcp_client
);
1652 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1658 static int link_acquire_conf(Link
*link
) {
1663 r
= link_acquire_ipv4_conf(link
);
1667 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1668 r
= link_acquire_ipv6_conf(link
);
1673 if (link_lldp_emit_enabled(link
)) {
1674 r
= link_lldp_emit_start(link
);
1676 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1682 bool link_has_carrier(Link
*link
) {
1683 /* see Documentation/networking/operstates.txt in the kernel sources */
1685 if (link
->kernel_operstate
== IF_OPER_UP
)
1688 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1689 /* operstate may not be implemented, so fall back to flags */
1690 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1696 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1701 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1704 r
= sd_netlink_message_get_errno(m
);
1706 /* we warn but don't fail the link, as it may be brought up later */
1707 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1712 int link_up(Link
*link
) {
1713 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1714 uint8_t ipv6ll_mode
;
1718 assert(link
->network
);
1719 assert(link
->manager
);
1720 assert(link
->manager
->rtnl
);
1722 log_link_debug(link
, "Bringing link up");
1724 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1726 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1728 /* set it free if not enslaved with networkd */
1729 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1730 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1732 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1735 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1737 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1739 if (link
->network
->mac
) {
1740 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1742 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1745 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1747 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1749 if (link_ipv6_enabled(link
)) {
1750 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1751 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1753 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1755 if (!link_ipv6ll_enabled(link
))
1756 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1758 const char *p
= NULL
;
1759 _cleanup_free_
char *stable_secret
= NULL
;
1761 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1762 r
= read_one_line_file(p
, &stable_secret
);
1765 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1767 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1769 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1771 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1773 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1774 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1776 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1779 r
= sd_netlink_message_close_container(req
);
1781 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1784 r
= sd_netlink_message_close_container(req
);
1786 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1788 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1789 link_netlink_destroy_callback
, link
);
1791 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1798 static int link_up_can(Link
*link
) {
1799 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1804 log_link_debug(link
, "Bringing CAN link up");
1806 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1808 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1810 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1812 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1814 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
1815 link_netlink_destroy_callback
, link
);
1817 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1824 static int link_set_can(Link
*link
) {
1825 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1829 assert(link
->network
);
1830 assert(link
->manager
);
1831 assert(link
->manager
->rtnl
);
1833 log_link_debug(link
, "link_set_can");
1835 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1837 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1839 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1841 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1843 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1845 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1847 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1849 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1851 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1852 struct can_bittiming bt
= {
1853 .bitrate
= link
->network
->can_bitrate
,
1854 .sample_point
= link
->network
->can_sample_point
,
1857 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1858 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1862 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1863 if (link
->network
->can_sample_point
> 0)
1864 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1866 log_link_debug(link
, "Using default sample point");
1868 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1870 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1873 if (link
->network
->can_restart_us
> 0) {
1874 char time_string
[FORMAT_TIMESPAN_MAX
];
1875 uint64_t restart_ms
;
1877 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1880 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1882 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1884 if (restart_ms
> UINT32_MAX
) {
1885 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1889 log_link_debug(link
, "Setting restart = %s", time_string
);
1891 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1893 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1896 r
= sd_netlink_message_close_container(m
);
1898 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1900 r
= sd_netlink_message_close_container(m
);
1902 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1904 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
1905 link_netlink_destroy_callback
, link
);
1907 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1911 if (!(link
->flags
& IFF_UP
)) {
1912 r
= link_up_can(link
);
1914 link_enter_failed(link
);
1919 log_link_debug(link
, "link_set_can done");
1924 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1929 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1932 r
= sd_netlink_message_get_errno(m
);
1934 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1936 if (streq_ptr(link
->kind
, "can"))
1942 int link_down(Link
*link
) {
1943 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1947 assert(link
->manager
);
1948 assert(link
->manager
->rtnl
);
1950 log_link_debug(link
, "Bringing link down");
1952 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1953 RTM_SETLINK
, link
->ifindex
);
1955 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1957 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1959 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1961 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_handler
,
1962 link_netlink_destroy_callback
, link
);
1964 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1971 static int link_handle_bound_to_list(Link
*link
) {
1975 bool required_up
= false;
1976 bool link_is_up
= false;
1980 if (hashmap_isempty(link
->bound_to_links
))
1983 if (link
->flags
& IFF_UP
)
1986 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1987 if (link_has_carrier(l
)) {
1992 if (!required_up
&& link_is_up
) {
1993 r
= link_down(link
);
1996 } else if (required_up
&& !link_is_up
) {
2005 static int link_handle_bound_by_list(Link
*link
) {
2012 if (hashmap_isempty(link
->bound_by_links
))
2015 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2016 r
= link_handle_bound_to_list(l
);
2024 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2030 if (link
== carrier
)
2033 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2036 r
= hashmap_ensure_allocated(h
, NULL
);
2040 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2047 static int link_new_bound_by_list(Link
*link
) {
2052 bool list_updated
= false;
2055 assert(link
->manager
);
2059 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2060 if (!carrier
->network
)
2063 if (strv_isempty(carrier
->network
->bind_carrier
))
2066 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2067 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2071 list_updated
= true;
2078 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2079 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2083 link_dirty(carrier
);
2089 static int link_new_bound_to_list(Link
*link
) {
2094 bool list_updated
= false;
2097 assert(link
->manager
);
2102 if (strv_isempty(link
->network
->bind_carrier
))
2107 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2108 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2109 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2113 list_updated
= true;
2120 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2121 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2125 link_dirty(carrier
);
2131 static int link_new_carrier_maps(Link
*link
) {
2134 r
= link_new_bound_by_list(link
);
2138 r
= link_handle_bound_by_list(link
);
2142 r
= link_new_bound_to_list(link
);
2146 r
= link_handle_bound_to_list(link
);
2153 static void link_free_bound_to_list(Link
*link
) {
2157 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2158 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2160 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2161 link_dirty(bound_to
);
2167 static void link_free_bound_by_list(Link
*link
) {
2171 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2172 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2174 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2175 link_dirty(bound_by
);
2176 link_handle_bound_to_list(bound_by
);
2183 static void link_free_carrier_maps(Link
*link
) {
2184 bool list_updated
= false;
2188 if (!hashmap_isempty(link
->bound_to_links
)) {
2189 link_free_bound_to_list(link
);
2190 list_updated
= true;
2193 if (!hashmap_isempty(link
->bound_by_links
)) {
2194 link_free_bound_by_list(link
);
2195 list_updated
= true;
2204 void link_drop(Link
*link
) {
2205 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2208 link_set_state(link
, LINK_STATE_LINGER
);
2210 link_free_carrier_maps(link
);
2212 log_link_debug(link
, "Link removed");
2214 (void) unlink(link
->state_file
);
2216 link_detach_from_manager(link
);
2223 static int link_joined(Link
*link
) {
2227 assert(link
->network
);
2229 if (!hashmap_isempty(link
->bound_to_links
)) {
2230 r
= link_handle_bound_to_list(link
);
2233 } else if (!(link
->flags
& IFF_UP
)) {
2236 link_enter_failed(link
);
2241 if (link
->network
->bridge
) {
2242 r
= link_set_bridge(link
);
2244 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2247 if (link
->network
->bond
) {
2248 r
= link_bond_set(link
);
2250 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2253 if (link
->network
->use_br_vlan
&&
2254 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2255 r
= link_set_bridge_vlan(link
);
2257 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2260 /* Skip setting up addresses until it gets carrier,
2261 or it would try to set addresses twice,
2262 which is bad for non-idempotent steps. */
2263 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2266 return link_enter_set_addresses(link
);
2269 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
2273 assert(link
->network
);
2277 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2280 r
= sd_netlink_message_get_errno(m
);
2281 if (r
< 0 && r
!= -EEXIST
) {
2282 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2283 link_enter_failed(link
);
2286 log_link_debug(link
, "Joined netdev");
2288 if (link
->enslaving
<= 0)
2294 static int link_enter_join_netdev(Link
*link
) {
2300 assert(link
->network
);
2301 assert(link
->state
== LINK_STATE_PENDING
);
2303 link_set_state(link
, LINK_STATE_ENSLAVING
);
2307 if (!link
->network
->bridge
&&
2308 !link
->network
->bond
&&
2309 !link
->network
->vrf
&&
2310 hashmap_isempty(link
->network
->stacked_netdevs
))
2311 return link_joined(link
);
2313 if (link
->network
->bond
) {
2314 if (link
->network
->bond
->state
== NETDEV_STATE_READY
&&
2315 link
->network
->bond
->ifindex
== link
->master_ifindex
)
2316 return link_joined(link
);
2318 log_struct(LOG_DEBUG
,
2319 LOG_LINK_INTERFACE(link
),
2320 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2321 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2323 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2325 log_struct_errno(LOG_WARNING
, r
,
2326 LOG_LINK_INTERFACE(link
),
2327 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2328 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2329 link_enter_failed(link
);
2336 if (link
->network
->bridge
) {
2337 log_struct(LOG_DEBUG
,
2338 LOG_LINK_INTERFACE(link
),
2339 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2340 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2342 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2344 log_struct_errno(LOG_WARNING
, r
,
2345 LOG_LINK_INTERFACE(link
),
2346 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2347 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2348 link_enter_failed(link
);
2355 if (link
->network
->vrf
) {
2356 log_struct(LOG_DEBUG
,
2357 LOG_LINK_INTERFACE(link
),
2358 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2359 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2361 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2363 log_struct_errno(LOG_WARNING
, r
,
2364 LOG_LINK_INTERFACE(link
),
2365 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2366 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2367 link_enter_failed(link
);
2374 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2376 if (netdev
->ifindex
> 0) {
2381 log_struct(LOG_DEBUG
,
2382 LOG_LINK_INTERFACE(link
),
2383 LOG_NETDEV_INTERFACE(netdev
),
2384 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2386 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2388 log_struct_errno(LOG_WARNING
, r
,
2389 LOG_LINK_INTERFACE(link
),
2390 LOG_NETDEV_INTERFACE(netdev
),
2391 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2392 link_enter_failed(link
);
2402 static int link_set_ipv4_forward(Link
*link
) {
2405 if (!link_ipv4_forward_enabled(link
))
2408 /* We propagate the forwarding flag from one interface to the
2409 * global setting one way. This means: as long as at least one
2410 * interface was configured at any time that had IP forwarding
2411 * enabled the setting will stay on for good. We do this
2412 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2413 * somewhat in sync (see below). */
2415 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2417 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2422 static int link_set_ipv6_forward(Link
*link
) {
2425 if (!link_ipv6_forward_enabled(link
))
2428 /* On Linux, the IPv6 stack does not know a per-interface
2429 * packet forwarding setting: either packet forwarding is on
2430 * for all, or off for all. We hence don't bother with a
2431 * per-interface setting, but simply propagate the interface
2432 * flag, if it is set, to the global flag, one-way. Note that
2433 * while IPv4 would allow a per-interface flag, we expose the
2434 * same behaviour there and also propagate the setting from
2435 * one to all, to keep things simple (see above). */
2437 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2439 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2444 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2445 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2446 IPv6PrivacyExtensions s
;
2447 const char *p
= NULL
;
2450 s
= link_ipv6_privacy_extensions(link
);
2454 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2455 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2457 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2459 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2464 static int link_set_ipv6_accept_ra(Link
*link
) {
2465 const char *p
= NULL
;
2468 /* Make this a NOP if IPv6 is not available */
2469 if (!socket_ipv6_is_supported())
2472 if (link
->flags
& IFF_LOOPBACK
)
2478 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2480 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2481 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2483 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2488 static int link_set_ipv6_dad_transmits(Link
*link
) {
2489 char buf
[DECIMAL_STR_MAX(int) + 1];
2490 const char *p
= NULL
;
2493 /* Make this a NOP if IPv6 is not available */
2494 if (!socket_ipv6_is_supported())
2497 if (link
->flags
& IFF_LOOPBACK
)
2503 if (link
->network
->ipv6_dad_transmits
< 0)
2506 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2507 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2509 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2511 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2516 static int link_set_ipv6_hop_limit(Link
*link
) {
2517 char buf
[DECIMAL_STR_MAX(int) + 1];
2518 const char *p
= NULL
;
2521 /* Make this a NOP if IPv6 is not available */
2522 if (!socket_ipv6_is_supported())
2525 if (link
->flags
& IFF_LOOPBACK
)
2531 if (link
->network
->ipv6_hop_limit
< 0)
2534 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2535 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2537 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
| WRITE_STRING_FILE_DISABLE_BUFFER
);
2539 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2544 static int link_set_ipv6_mtu(Link
*link
) {
2545 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2546 const char *p
= NULL
;
2549 /* Make this a NOP if IPv6 is not available */
2550 if (!socket_ipv6_is_supported())
2553 if (link
->flags
& IFF_LOOPBACK
)
2556 if (link
->network
->ipv6_mtu
== 0)
2559 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2561 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2563 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
2565 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2570 static bool link_is_static_address_configured(Link
*link
, Address
*address
) {
2571 Address
*net_address
;
2579 LIST_FOREACH(addresses
, net_address
, link
->network
->static_addresses
)
2580 if (address_equal(net_address
, address
))
2586 static bool link_is_static_route_configured(Link
*link
, Route
*route
) {
2595 LIST_FOREACH(routes
, net_route
, link
->network
->static_routes
)
2596 if (route_equal(net_route
, route
))
2602 static int link_drop_foreign_config(Link
*link
) {
2608 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2609 /* we consider IPv6LL addresses to be managed by the kernel */
2610 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2613 if (link_is_static_address_configured(link
, address
)) {
2614 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
2616 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2618 r
= address_remove(address
, link
, NULL
);
2624 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2625 /* do not touch routes managed by the kernel */
2626 if (route
->protocol
== RTPROT_KERNEL
)
2629 if (link_is_static_route_configured(link
, route
)) {
2630 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
);
2634 r
= route_remove(route
, link
, NULL
);
2643 static int link_drop_config(Link
*link
) {
2644 Address
*address
, *pool_address
;
2649 SET_FOREACH(address
, link
->addresses
, i
) {
2650 /* we consider IPv6LL addresses to be managed by the kernel */
2651 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2654 r
= address_remove(address
, link
, NULL
);
2658 /* If this address came from an address pool, clean up the pool */
2659 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2660 if (address_equal(address
, pool_address
)) {
2661 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2662 address_free(pool_address
);
2668 SET_FOREACH(route
, link
->routes
, i
) {
2669 /* do not touch routes managed by the kernel */
2670 if (route
->protocol
== RTPROT_KERNEL
)
2673 r
= route_remove(route
, link
, NULL
);
2683 static int link_update_lldp(Link
*link
) {
2691 if (link
->flags
& IFF_UP
) {
2692 r
= sd_lldp_start(link
->lldp
);
2694 log_link_debug(link
, "Started LLDP.");
2696 r
= sd_lldp_stop(link
->lldp
);
2698 log_link_debug(link
, "Stopped LLDP.");
2704 static int link_configure_can(Link
*link
) {
2707 if (streq_ptr(link
->kind
, "can")) {
2708 /* The CAN interface must be down to configure bitrate, etc... */
2709 if ((link
->flags
& IFF_UP
)) {
2710 r
= link_down(link
);
2712 link_enter_failed(link
);
2719 return link_set_can(link
);
2722 if (!(link
->flags
& IFF_UP
)) {
2723 r
= link_up_can(link
);
2725 link_enter_failed(link
);
2733 static int link_configure(Link
*link
) {
2737 assert(link
->network
);
2738 assert(link
->state
== LINK_STATE_PENDING
);
2740 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2741 return link_configure_can(link
);
2743 /* Drop foreign config, but ignore loopback or critical devices.
2744 * We do not want to remove loopback address or addresses used for root NFS. */
2745 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2746 r
= link_drop_foreign_config(link
);
2751 r
= link_set_proxy_arp(link
);
2755 r
= ipv6_proxy_ndp_addresses_configure(link
);
2759 r
= link_set_ipv4_forward(link
);
2763 r
= link_set_ipv6_forward(link
);
2767 r
= link_set_ipv6_privacy_extensions(link
);
2771 r
= link_set_ipv6_accept_ra(link
);
2775 r
= link_set_ipv6_dad_transmits(link
);
2779 r
= link_set_ipv6_hop_limit(link
);
2783 r
= link_set_flags(link
);
2787 r
= link_set_ipv6_mtu(link
);
2791 if (link_ipv4ll_enabled(link
)) {
2792 r
= ipv4ll_configure(link
);
2797 if (link_dhcp4_enabled(link
)) {
2798 r
= dhcp4_set_promote_secondaries(link
);
2802 r
= dhcp4_configure(link
);
2807 if (link_dhcp4_server_enabled(link
)) {
2808 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2812 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2817 if (link_dhcp6_enabled(link
) ||
2818 link_ipv6_accept_ra_enabled(link
)) {
2819 r
= dhcp6_configure(link
);
2824 if (link_ipv6_accept_ra_enabled(link
)) {
2825 r
= ndisc_configure(link
);
2830 if (link_radv_enabled(link
)) {
2831 r
= radv_configure(link
);
2836 if (link_lldp_rx_enabled(link
)) {
2837 r
= sd_lldp_new(&link
->lldp
);
2841 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2845 r
= sd_lldp_match_capabilities(link
->lldp
,
2846 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2847 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2848 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2852 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2856 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2860 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2864 r
= link_update_lldp(link
);
2869 if (link
->network
->mtu
> 0) {
2870 r
= link_set_mtu(link
, link
->network
->mtu
);
2875 return link_configure_after_setting_mtu(link
);
2878 static int link_configure_after_setting_mtu(Link
*link
) {
2882 assert(link
->network
);
2883 assert(link
->state
== LINK_STATE_PENDING
);
2885 if (link
->setting_mtu
)
2888 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2889 r
= link_acquire_conf(link
);
2894 return link_enter_join_netdev(link
);
2897 static int duid_set_uuid(DUID
*duid
, sd_id128_t uuid
) {
2900 if (duid
->raw_data_len
> 0)
2903 if (duid
->type
!= DUID_TYPE_UUID
)
2906 memcpy(&duid
->raw_data
, &uuid
, sizeof(sd_id128_t
));
2907 duid
->raw_data_len
= sizeof(sd_id128_t
);
2912 int get_product_uuid_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2913 Manager
*manager
= userdata
;
2914 const sd_bus_error
*e
;
2924 e
= sd_bus_message_get_error(m
);
2926 log_error_errno(sd_bus_error_get_errno(e
),
2927 "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
2932 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
2936 if (sz
!= sizeof(sd_id128_t
)) {
2937 log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
2941 memcpy(&manager
->product_uuid
, a
, sz
);
2942 while ((duid
= set_steal_first(manager
->duids_requesting_uuid
)))
2943 (void) duid_set_uuid(duid
, manager
->product_uuid
);
2945 manager
->duids_requesting_uuid
= set_free(manager
->duids_requesting_uuid
);
2948 while ((link
= set_steal_first(manager
->links_requesting_uuid
))) {
2949 r
= link_configure(link
);
2951 log_link_error_errno(link
, r
, "Failed to configure link: %m");
2954 manager
->links_requesting_uuid
= set_free(manager
->links_requesting_uuid
);
2956 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
2957 * even if the method fails. */
2958 manager
->has_product_uuid
= true;
2963 static bool link_requires_uuid(Link
*link
) {
2967 assert(link
->manager
);
2968 assert(link
->network
);
2970 duid
= link_get_duid(link
);
2971 if (duid
->type
!= DUID_TYPE_UUID
|| duid
->raw_data_len
!= 0)
2974 if (link_dhcp4_enabled(link
) && IN_SET(link
->network
->dhcp_client_identifier
, DHCP_CLIENT_ID_DUID
, DHCP_CLIENT_ID_DUID_ONLY
))
2977 if (link_dhcp6_enabled(link
) || link_ipv6_accept_ra_enabled(link
))
2983 static int link_configure_duid(Link
*link
) {
2989 assert(link
->manager
);
2990 assert(link
->network
);
2993 duid
= link_get_duid(link
);
2995 if (!link_requires_uuid(link
))
2998 if (m
->has_product_uuid
) {
2999 (void) duid_set_uuid(duid
, m
->product_uuid
);
3003 if (!m
->links_requesting_uuid
) {
3004 r
= manager_request_product_uuid(m
, link
);
3009 log_link_warning_errno(link
, r
,
3010 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
3014 r
= set_put(m
->links_requesting_uuid
, link
);
3018 r
= set_put(m
->duids_requesting_uuid
, duid
);
3026 static int link_initialized_and_synced(Link
*link
) {
3031 assert(link
->ifname
);
3032 assert(link
->manager
);
3034 if (link
->state
!= LINK_STATE_PENDING
)
3037 log_link_debug(link
, "Link state is up-to-date");
3039 r
= link_new_bound_by_list(link
);
3043 r
= link_handle_bound_by_list(link
);
3047 if (!link
->network
) {
3048 r
= network_get(link
->manager
, link
->sd_device
, link
->ifname
,
3049 &link
->mac
, &network
);
3051 link_enter_unmanaged(link
);
3053 } else if (r
== 0 && network
->unmanaged
) {
3054 link_enter_unmanaged(link
);
3059 if (link
->flags
& IFF_LOOPBACK
) {
3060 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
3061 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
3063 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
3064 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
3066 if (network
->dhcp_server
)
3067 log_link_debug(link
, "Ignoring DHCP server for loopback link");
3070 r
= network_apply(network
, link
);
3075 r
= link_new_bound_to_list(link
);
3079 /* link_configure_duid() returns 0 if it requests product UUID. In that case,
3080 * link_configure() is called later asynchronously. */
3081 r
= link_configure_duid(link
);
3085 r
= link_configure(link
);
3092 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
3093 (void) link_initialized_and_synced(link
);
3097 int link_initialized(Link
*link
, sd_device
*device
) {
3098 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
3102 assert(link
->manager
);
3103 assert(link
->manager
->rtnl
);
3106 if (link
->state
!= LINK_STATE_PENDING
)
3109 if (link
->sd_device
)
3112 log_link_debug(link
, "udev initialized link");
3114 link
->sd_device
= sd_device_ref(device
);
3116 /* udev has initialized the link, but we don't know if we have yet
3117 * processed the NEWLINK messages with the latest state. Do a GETLINK,
3118 * when it returns we know that the pending NEWLINKs have already been
3119 * processed and that we are up-to-date */
3121 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
3126 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_initialized_handler
,
3127 link_netlink_destroy_callback
, link
);
3136 static int link_load(Link
*link
) {
3137 _cleanup_free_
char *network_file
= NULL
,
3140 *dhcp4_address
= NULL
,
3141 *ipv4ll_address
= NULL
;
3142 union in_addr_union address
;
3143 union in_addr_union route_dst
;
3149 r
= parse_env_file(NULL
, link
->state_file
,
3150 "NETWORK_FILE", &network_file
,
3151 "ADDRESSES", &addresses
,
3153 "DHCP4_ADDRESS", &dhcp4_address
,
3154 "IPV4LL_ADDRESS", &ipv4ll_address
);
3155 if (r
< 0 && r
!= -ENOENT
)
3156 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3163 suffix
= strrchr(network_file
, '.');
3165 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3166 goto network_file_fail
;
3170 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3172 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3173 goto network_file_fail
;
3176 r
= network_apply(network
, link
);
3178 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3187 _cleanup_free_
char *address_str
= NULL
;
3188 char *prefixlen_str
;
3190 unsigned char prefixlen
;
3192 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3194 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3200 prefixlen_str
= strchr(address_str
, '/');
3201 if (!prefixlen_str
) {
3202 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3206 *prefixlen_str
++ = '\0';
3208 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3210 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3214 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3216 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3220 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3222 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3231 _cleanup_free_
char *route_str
= NULL
;
3232 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3234 char *prefixlen_str
;
3236 unsigned char prefixlen
, tos
, table
;
3239 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3241 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3247 prefixlen_str
= strchr(route_str
, '/');
3248 if (!prefixlen_str
) {
3249 log_link_debug(link
, "Failed to parse route %s", route_str
);
3253 *prefixlen_str
++ = '\0';
3255 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3257 log_link_debug(link
,
3258 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3263 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3265 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3269 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3271 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3273 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3274 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3275 0, route_expire_handler
, route
);
3277 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3280 route
->lifetime
= lifetime
;
3281 sd_event_source_unref(route
->expire
);
3282 route
->expire
= TAKE_PTR(expire
);
3286 if (dhcp4_address
) {
3287 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3289 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3290 goto dhcp4_address_fail
;
3293 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3295 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3297 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3299 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3304 if (ipv4ll_address
) {
3305 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3307 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3308 goto ipv4ll_address_fail
;
3311 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3313 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3315 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3317 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3320 ipv4ll_address_fail
:
3325 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3326 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
3327 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3336 r
= link_new(m
, message
, ret
);
3342 log_link_debug(link
, "Link %d added", link
->ifindex
);
3344 r
= link_load(link
);
3348 if (detect_container() <= 0) {
3349 /* not in a container, udev will be around */
3350 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3351 r
= sd_device_new_from_device_id(&device
, ifindex_str
);
3353 log_link_warning_errno(link
, r
, "Could not find device: %m");
3357 r
= sd_device_get_is_initialized(device
);
3359 log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized or not: %m");
3364 log_link_debug(link
, "link pending udev initialization...");
3368 r
= link_initialized(link
, device
);
3372 r
= link_initialized_and_synced(link
);
3379 link_enter_failed(link
);
3383 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3388 log_link_info(link
, "Gained IPv6LL");
3390 link
->ipv6ll_address
= *address
;
3391 link_check_ready(link
);
3393 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3394 r
= link_acquire_ipv6_conf(link
);
3396 link_enter_failed(link
);
3404 static int link_carrier_gained(Link
*link
) {
3409 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3410 r
= link_acquire_conf(link
);
3412 link_enter_failed(link
);
3416 r
= link_enter_set_addresses(link
);
3421 r
= link_handle_bound_by_list(link
);
3428 static int link_carrier_lost(Link
*link
) {
3433 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3434 * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3435 * configuration and stop the clients as well. */
3436 if (link
->setting_mtu
)
3439 r
= link_stop_clients(link
);
3441 link_enter_failed(link
);
3445 if (link_dhcp4_server_enabled(link
))
3446 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3448 r
= link_drop_config(link
);
3452 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3453 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3454 r
= link_drop_foreign_config(link
);
3459 r
= link_handle_bound_by_list(link
);
3466 int link_carrier_reset(Link
*link
) {
3471 if (link_has_carrier(link
)) {
3472 r
= link_carrier_lost(link
);
3476 r
= link_carrier_gained(link
);
3480 log_link_info(link
, "Reset carrier");
3486 int link_update(Link
*link
, sd_netlink_message
*m
) {
3487 struct ether_addr mac
;
3490 bool had_carrier
, carrier_gained
, carrier_lost
;
3494 assert(link
->ifname
);
3497 if (link
->state
== LINK_STATE_LINGER
) {
3498 log_link_info(link
, "Link readded");
3499 link_set_state(link
, LINK_STATE_ENSLAVING
);
3501 r
= link_new_carrier_maps(link
);
3506 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3507 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3508 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3510 if (link
->state
== LINK_STATE_PENDING
) {
3511 r
= free_and_strdup(&link
->ifname
, ifname
);
3515 Manager
*manager
= link
->manager
;
3518 r
= link_add(manager
, m
, &link
);
3524 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3525 if (r
>= 0 && mtu
> 0) {
3527 if (link
->original_mtu
== 0) {
3528 link
->original_mtu
= mtu
;
3529 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3532 if (link
->dhcp_client
) {
3533 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3536 return log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3540 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3542 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3546 /* The kernel may broadcast NEWLINK messages without the MAC address
3547 set, simply ignore them. */
3548 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3550 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3553 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3556 log_link_debug(link
, "MAC address: "
3557 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3558 mac
.ether_addr_octet
[0],
3559 mac
.ether_addr_octet
[1],
3560 mac
.ether_addr_octet
[2],
3561 mac
.ether_addr_octet
[3],
3562 mac
.ether_addr_octet
[4],
3563 mac
.ether_addr_octet
[5]);
3566 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3568 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3571 if (link
->dhcp_client
) {
3572 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3573 (const uint8_t *) &link
->mac
,
3577 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3579 r
= dhcp4_set_client_identifier(link
);
3584 if (link
->dhcp6_client
) {
3585 const DUID
* duid
= link_get_duid(link
);
3587 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3588 (const uint8_t *) &link
->mac
,
3592 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3594 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3595 link
->network
->iaid
);
3597 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3599 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3601 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3602 duid
->raw_data_len
);
3604 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3608 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3610 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3614 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3616 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3621 had_carrier
= link_has_carrier(link
);
3623 r
= link_update_flags(link
, m
);
3627 r
= link_update_lldp(link
);
3631 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3632 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3634 if (carrier_gained
) {
3635 log_link_info(link
, "Gained carrier");
3637 r
= link_carrier_gained(link
);
3640 } else if (carrier_lost
) {
3641 log_link_info(link
, "Lost carrier");
3643 r
= link_carrier_lost(link
);
3651 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3659 if (hashmap_isempty(h
))
3663 HASHMAP_FOREACH(link
, h
, i
) {
3667 fprintf(f
, "%i", link
->ifindex
);
3674 int link_save(Link
*link
) {
3675 _cleanup_free_
char *temp_path
= NULL
;
3676 _cleanup_fclose_
FILE *f
= NULL
;
3677 const char *admin_state
, *oper_state
;
3684 assert(link
->state_file
);
3685 assert(link
->lease_file
);
3686 assert(link
->manager
);
3688 if (link
->state
== LINK_STATE_LINGER
) {
3689 unlink(link
->state_file
);
3693 link_lldp_save(link
);
3695 admin_state
= link_state_to_string(link
->state
);
3696 assert(admin_state
);
3698 oper_state
= link_operstate_to_string(link
->operstate
);
3701 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3705 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3706 (void) fchmod(fileno(f
), 0644);
3709 "# This is private data. Do not parse.\n"
3712 admin_state
, oper_state
);
3714 if (link
->network
) {
3716 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3717 const char *dhcp_domainname
= NULL
;
3718 char **dhcp6_domains
= NULL
;
3719 char **dhcp_domains
= NULL
;
3722 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3723 yes_no(link
->network
->required_for_online
));
3725 if (link
->dhcp6_client
) {
3726 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3727 if (r
< 0 && r
!= -ENOMSG
)
3728 log_link_debug(link
, "No DHCPv6 lease");
3731 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3736 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3737 _cleanup_free_
char *b
= NULL
;
3739 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3740 &link
->network
->dns
[j
].address
, &b
);
3742 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3752 if (link
->network
->dhcp_use_dns
&&
3754 const struct in_addr
*addresses
;
3756 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3760 serialize_in_addrs(f
, addresses
, r
);
3765 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3766 struct in6_addr
*in6_addrs
;
3768 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3772 serialize_in6_addrs(f
, in6_addrs
, r
);
3777 /* Make sure to flush out old entries before we use the NDISC data */
3780 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3783 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3787 serialize_in6_addrs(f
, &dd
->address
, 1);
3796 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3798 if (link
->network
->dhcp_use_ntp
&&
3800 const struct in_addr
*addresses
;
3802 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3806 serialize_in_addrs(f
, addresses
, r
);
3811 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3812 struct in6_addr
*in6_addrs
;
3815 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3820 serialize_in6_addrs(f
, in6_addrs
, r
);
3824 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3826 fputstrv(f
, hosts
, NULL
, &space
);
3831 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3832 if (link
->dhcp_lease
) {
3833 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3834 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3837 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3840 fputs("DOMAINS=", f
);
3842 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3844 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3847 if (dhcp_domainname
)
3848 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3850 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3852 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3854 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3855 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3860 fputs("ROUTE_DOMAINS=", f
);
3862 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3864 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3867 if (dhcp_domainname
)
3868 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3870 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3872 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3874 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3875 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3880 fprintf(f
, "LLMNR=%s\n",
3881 resolve_support_to_string(link
->network
->llmnr
));
3882 fprintf(f
, "MDNS=%s\n",
3883 resolve_support_to_string(link
->network
->mdns
));
3885 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3886 fprintf(f
, "DNS_OVER_TLS=%s\n",
3887 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3889 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3890 fprintf(f
, "DNSSEC=%s\n",
3891 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3893 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3896 fputs("DNSSEC_NTA=", f
);
3898 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3899 fputs_with_space(f
, n
, NULL
, &space
);
3903 fputs("ADDRESSES=", f
);
3905 SET_FOREACH(a
, link
->addresses
, i
) {
3906 _cleanup_free_
char *address_str
= NULL
;
3908 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3912 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3917 fputs("ROUTES=", f
);
3919 SET_FOREACH(route
, link
->routes
, i
) {
3920 _cleanup_free_
char *route_str
= NULL
;
3922 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3926 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3927 space
? " " : "", route_str
,
3928 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3935 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3936 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3938 if (link
->dhcp_lease
) {
3939 struct in_addr address
;
3940 const char *tz
= NULL
;
3942 assert(link
->network
);
3944 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3946 fprintf(f
, "TIMEZONE=%s\n", tz
);
3948 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3950 fputs("DHCP4_ADDRESS=", f
);
3951 serialize_in_addrs(f
, &address
, 1);
3955 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3963 unlink(link
->lease_file
);
3966 struct in_addr address
;
3968 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3970 fputs("IPV4LL_ADDRESS=", f
);
3971 serialize_in_addrs(f
, &address
, 1);
3976 r
= fflush_and_check(f
);
3980 if (rename(temp_path
, link
->state_file
) < 0) {
3988 (void) unlink(link
->state_file
);
3990 (void) unlink(temp_path
);
3992 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3995 /* The serialized state in /run is no longer up-to-date. */
3996 void link_dirty(Link
*link
) {
4001 /* mark manager dirty as link is dirty */
4002 manager_dirty(link
->manager
);
4004 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
4006 /* allocation errors are ignored */
4009 r
= set_put(link
->manager
->dirty_links
, link
);
4011 /* don't take another ref if the link was already dirty */
4017 /* The serialized state in /run is up-to-date */
4018 void link_clean(Link
*link
) {
4020 assert(link
->manager
);
4022 link_unref(set_remove(link
->manager
->dirty_links
, link
));
4025 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
4026 [LINK_STATE_PENDING
] = "pending",
4027 [LINK_STATE_ENSLAVING
] = "configuring",
4028 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
4029 [LINK_STATE_SETTING_ROUTES
] = "configuring",
4030 [LINK_STATE_CONFIGURED
] = "configured",
4031 [LINK_STATE_UNMANAGED
] = "unmanaged",
4032 [LINK_STATE_FAILED
] = "failed",
4033 [LINK_STATE_LINGER
] = "linger",
4036 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
4038 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
4039 [LINK_OPERSTATE_OFF
] = "off",
4040 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
4041 [LINK_OPERSTATE_DORMANT
] = "dormant",
4042 [LINK_OPERSTATE_CARRIER
] = "carrier",
4043 [LINK_OPERSTATE_DEGRADED
] = "degraded",
4044 [LINK_OPERSTATE_ROUTABLE
] = "routable",
4047 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);