1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Tom Gundersen <teg@jklm.no>
6 #include <netinet/ether.h>
8 #include <linux/can/netlink.h>
10 #include <stdio_ext.h>
12 #include "alloc-util.h"
14 #include "dhcp-lease-internal.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"
29 #include "udev-util.h"
33 static bool link_dhcp6_enabled(Link
*link
) {
36 if (!socket_ipv6_is_supported())
39 if (link
->flags
& IFF_LOOPBACK
)
45 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
48 static bool link_dhcp4_enabled(Link
*link
) {
51 if (link
->flags
& IFF_LOOPBACK
)
57 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
60 static bool link_dhcp4_server_enabled(Link
*link
) {
63 if (link
->flags
& IFF_LOOPBACK
)
69 return link
->network
->dhcp_server
;
72 static bool link_ipv4ll_enabled(Link
*link
) {
75 if (link
->flags
& IFF_LOOPBACK
)
81 if (streq_ptr(link
->kind
, "wireguard"))
84 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
87 static bool link_ipv6ll_enabled(Link
*link
) {
90 if (!socket_ipv6_is_supported())
93 if (link
->flags
& IFF_LOOPBACK
)
99 if (streq_ptr(link
->kind
, "wireguard"))
102 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
105 static bool link_ipv6_enabled(Link
*link
) {
108 if (!socket_ipv6_is_supported())
111 if (link
->network
->bridge
)
114 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
115 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
118 static bool link_radv_enabled(Link
*link
) {
121 if (!link_ipv6ll_enabled(link
))
124 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
127 static bool link_lldp_rx_enabled(Link
*link
) {
130 if (link
->flags
& IFF_LOOPBACK
)
133 if (link
->iftype
!= ARPHRD_ETHER
)
139 /* LLDP should be handled on bridge slaves as those have a direct
140 * connection to their peers not on the bridge master. Linux doesn't
141 * even (by default) forward lldp packets to the bridge master.*/
142 if (streq_ptr("bridge", link
->kind
))
145 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
148 static bool link_lldp_emit_enabled(Link
*link
) {
151 if (link
->flags
& IFF_LOOPBACK
)
154 if (link
->iftype
!= ARPHRD_ETHER
)
160 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
163 static bool link_ipv4_forward_enabled(Link
*link
) {
166 if (link
->flags
& IFF_LOOPBACK
)
172 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
175 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
178 static bool link_ipv6_forward_enabled(Link
*link
) {
181 if (!socket_ipv6_is_supported())
184 if (link
->flags
& IFF_LOOPBACK
)
190 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
193 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
196 static bool link_proxy_arp_enabled(Link
*link
) {
199 if (link
->flags
& IFF_LOOPBACK
)
205 if (link
->network
->proxy_arp
< 0)
211 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
214 if (!socket_ipv6_is_supported())
217 if (link
->flags
& IFF_LOOPBACK
)
223 if (!link_ipv6ll_enabled(link
))
226 /* If unset use system default (enabled if local forwarding is disabled.
227 * disabled if local forwarding is enabled).
228 * If set, ignore or enforce RA independent of local forwarding state.
230 if (link
->network
->ipv6_accept_ra
< 0)
231 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
232 return !link_ipv6_forward_enabled(link
);
233 else if (link
->network
->ipv6_accept_ra
> 0)
234 /* accept RA even if ip_forward is enabled */
241 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
244 if (!socket_ipv6_is_supported())
245 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
247 if (link
->flags
& IFF_LOOPBACK
)
248 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
251 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
253 return link
->network
->ipv6_privacy_extensions
;
256 static int link_enable_ipv6(Link
*link
) {
257 const char *p
= NULL
;
261 if (link
->flags
& IFF_LOOPBACK
)
264 disabled
= !link_ipv6_enabled(link
);
266 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
268 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
270 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
271 enable_disable(!disabled
), link
->ifname
);
273 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
278 void link_update_operstate(Link
*link
) {
279 LinkOperationalState operstate
;
282 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
283 operstate
= LINK_OPERSTATE_DORMANT
;
284 else if (link_has_carrier(link
)) {
286 uint8_t scope
= RT_SCOPE_NOWHERE
;
289 /* if we have carrier, check what addresses we have */
290 SET_FOREACH(address
, link
->addresses
, i
) {
291 if (!address_is_ready(address
))
294 if (address
->scope
< scope
)
295 scope
= address
->scope
;
298 /* for operstate we also take foreign addresses into account */
299 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
300 if (!address_is_ready(address
))
303 if (address
->scope
< scope
)
304 scope
= address
->scope
;
307 if (scope
< RT_SCOPE_SITE
)
308 /* universally accessible addresses found */
309 operstate
= LINK_OPERSTATE_ROUTABLE
;
310 else if (scope
< RT_SCOPE_HOST
)
311 /* only link or site local addresses found */
312 operstate
= LINK_OPERSTATE_DEGRADED
;
314 /* no useful addresses found */
315 operstate
= LINK_OPERSTATE_CARRIER
;
316 } else if (link
->flags
& IFF_UP
)
317 operstate
= LINK_OPERSTATE_NO_CARRIER
;
319 operstate
= LINK_OPERSTATE_OFF
;
321 if (link
->operstate
!= operstate
) {
322 link
->operstate
= operstate
;
323 link_send_changed(link
, "OperationalState", NULL
);
328 #define FLAG_STRING(string, flag, old, new) \
329 (((old ^ new) & flag) \
330 ? ((old & flag) ? (" -" string) : (" +" string)) \
333 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
334 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
340 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
342 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
344 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
346 /* if we got a message without operstate, take it to mean
347 the state was unchanged */
348 operstate
= link
->kernel_operstate
;
350 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
353 if (link
->flags
!= flags
) {
354 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",
355 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
356 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
357 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
358 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
359 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
360 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
361 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
362 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
363 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
364 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
365 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
366 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
367 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
368 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
369 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
370 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
371 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
372 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
373 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
375 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
376 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
377 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
378 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
379 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
380 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
381 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
382 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
384 /* link flags are currently at most 18 bits, let's align to
386 if (unknown_flags_added
)
388 "Unknown link flags gained: %#.5x (ignoring)",
389 unknown_flags_added
);
391 if (unknown_flags_removed
)
393 "Unknown link flags lost: %#.5x (ignoring)",
394 unknown_flags_removed
);
398 link
->kernel_operstate
= operstate
;
400 link_update_operstate(link
);
405 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
406 _cleanup_(link_unrefp
) Link
*link
= NULL
;
408 const char *ifname
, *kind
= NULL
;
410 unsigned short iftype
;
416 /* check for link kind */
417 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
419 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
420 r
= sd_netlink_message_exit_container(message
);
425 r
= sd_netlink_message_get_type(message
, &type
);
428 else if (type
!= RTM_NEWLINK
)
431 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
434 else if (ifindex
<= 0)
437 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
441 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
445 link
= new0(Link
, 1);
450 link
->manager
= manager
;
451 link
->state
= LINK_STATE_PENDING
;
452 link
->rtnl_extended_attrs
= true;
453 link
->ifindex
= ifindex
;
454 link
->iftype
= iftype
;
455 link
->ifname
= strdup(ifname
);
460 link
->kind
= strdup(kind
);
465 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
467 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
469 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
472 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
475 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
478 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
482 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
486 r
= link_update_flags(link
, message
);
490 *ret
= TAKE_PTR(link
);
495 static void link_free(Link
*link
) {
503 while (!set_isempty(link
->addresses
))
504 address_free(set_first(link
->addresses
));
506 while (!set_isempty(link
->addresses_foreign
))
507 address_free(set_first(link
->addresses_foreign
));
509 link
->addresses
= set_free(link
->addresses
);
511 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
513 while ((address
= link
->pool_addresses
)) {
514 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
515 address_free(address
);
518 sd_dhcp_server_unref(link
->dhcp_server
);
519 sd_dhcp_client_unref(link
->dhcp_client
);
520 sd_dhcp_lease_unref(link
->dhcp_lease
);
522 link_lldp_emit_stop(link
);
524 free(link
->lease_file
);
526 sd_lldp_unref(link
->lldp
);
527 free(link
->lldp_file
);
531 sd_ipv4ll_unref(link
->ipv4ll
);
532 sd_dhcp6_client_unref(link
->dhcp6_client
);
533 sd_ndisc_unref(link
->ndisc
);
534 sd_radv_unref(link
->radv
);
537 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
543 (void)unlink(link
->state_file
);
544 free(link
->state_file
);
546 udev_device_unref(link
->udev_device
);
548 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
549 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
550 hashmap_free(link
->bound_to_links
);
552 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
553 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
554 hashmap_free(link
->bound_by_links
);
559 Link
*link_unref(Link
*link
) {
563 assert(link
->n_ref
> 0);
575 Link
*link_ref(Link
*link
) {
579 assert(link
->n_ref
> 0);
586 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
593 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
602 static void link_set_state(Link
*link
, LinkState state
) {
605 if (link
->state
== state
)
610 link_send_changed(link
, "AdministrativeState", NULL
);
613 static void link_enter_unmanaged(Link
*link
) {
616 log_link_debug(link
, "Unmanaged");
618 link_set_state(link
, LINK_STATE_UNMANAGED
);
623 static int link_stop_clients(Link
*link
) {
627 assert(link
->manager
);
628 assert(link
->manager
->event
);
630 if (link
->dhcp_client
) {
631 k
= sd_dhcp_client_stop(link
->dhcp_client
);
633 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
637 k
= sd_ipv4ll_stop(link
->ipv4ll
);
639 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
642 if (link
->dhcp6_client
) {
643 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
645 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
649 k
= sd_ndisc_stop(link
->ndisc
);
651 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
655 k
= sd_radv_stop(link
->radv
);
657 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
660 link_lldp_emit_stop(link
);
664 void link_enter_failed(Link
*link
) {
667 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
670 log_link_warning(link
, "Failed");
672 link_set_state(link
, LINK_STATE_FAILED
);
674 link_stop_clients(link
);
679 static Address
* link_find_dhcp_server_address(Link
*link
) {
683 assert(link
->network
);
685 /* The first statically configured address if there is any */
686 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
688 if (address
->family
!= AF_INET
)
691 if (in_addr_is_null(address
->family
, &address
->in_addr
))
697 /* If that didn't work, find a suitable address we got from the pool */
698 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
699 if (address
->family
!= AF_INET
)
708 static void link_enter_configured(Link
*link
) {
710 assert(link
->network
);
712 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
715 log_link_info(link
, "Configured");
717 link_set_state(link
, LINK_STATE_CONFIGURED
);
722 void link_check_ready(Link
*link
) {
728 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
734 if (!link
->static_routes_configured
)
737 if (!link
->routing_policy_rules_configured
)
740 if (link_ipv4ll_enabled(link
))
741 if (!link
->ipv4ll_address
||
745 if (!link
->network
->bridge
) {
747 if (link_ipv6ll_enabled(link
))
748 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
751 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
752 !link
->dhcp4_configured
) ||
753 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
754 !link
->dhcp6_configured
) ||
755 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
756 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
759 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
763 SET_FOREACH(a
, link
->addresses
, i
)
764 if (!address_is_ready(a
))
767 if (link
->state
!= LINK_STATE_CONFIGURED
)
768 link_enter_configured(link
);
773 static int link_set_routing_policy_rule(Link
*link
) {
774 RoutingPolicyRule
*rule
, *rrule
= NULL
;
778 assert(link
->network
);
780 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
781 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
782 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
784 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
788 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
790 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
791 link_enter_failed(link
);
795 link
->routing_policy_rule_messages
++;
798 routing_policy_rule_purge(link
->manager
, link
);
799 if (link
->routing_policy_rule_messages
== 0) {
800 link
->routing_policy_rules_configured
= true;
801 link_check_ready(link
);
803 log_link_debug(link
, "Setting routing policy rules");
808 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
809 _cleanup_(link_unrefp
) Link
*link
= userdata
;
812 assert(link
->route_messages
> 0);
813 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
814 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
817 link
->route_messages
--;
819 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
822 r
= sd_netlink_message_get_errno(m
);
823 if (r
< 0 && r
!= -EEXIST
)
824 log_link_warning_errno(link
, r
, "Could not set route: %m");
826 if (link
->route_messages
== 0) {
827 log_link_debug(link
, "Routes set");
828 link
->static_routes_configured
= true;
829 link_check_ready(link
);
835 static int link_enter_set_routes(Link
*link
) {
840 assert(link
->network
);
841 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
843 (void) link_set_routing_policy_rule(link
);
845 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
847 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
848 r
= route_configure(rt
, link
, route_handler
);
850 log_link_warning_errno(link
, r
, "Could not set routes: %m");
851 link_enter_failed(link
);
855 link
->route_messages
++;
858 if (link
->route_messages
== 0) {
859 link
->static_routes_configured
= true;
860 link_check_ready(link
);
862 log_link_debug(link
, "Setting routes");
867 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
868 _cleanup_(link_unrefp
) Link
*link
= userdata
;
873 assert(link
->ifname
);
875 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
878 r
= sd_netlink_message_get_errno(m
);
879 if (r
< 0 && r
!= -ESRCH
)
880 log_link_warning_errno(link
, r
, "Could not drop route: %m");
885 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
886 _cleanup_(link_unrefp
) Link
*link
= userdata
;
892 assert(link
->ifname
);
893 assert(link
->address_messages
> 0);
894 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
895 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
897 link
->address_messages
--;
899 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
902 r
= sd_netlink_message_get_errno(m
);
903 if (r
< 0 && r
!= -EEXIST
)
904 log_link_warning_errno(link
, r
, "could not set address: %m");
906 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
908 if (link
->address_messages
== 0) {
909 log_link_debug(link
, "Addresses set");
910 link_enter_set_routes(link
);
916 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
917 _cleanup_(link_unrefp
) Link
*link
= userdata
;
923 assert(link
->ifname
);
924 assert(link
->address_label_messages
> 0);
926 link
->address_label_messages
--;
928 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
931 r
= sd_netlink_message_get_errno(m
);
932 if (r
< 0 && r
!= -EEXIST
)
933 log_link_warning_errno(link
, r
, "could not set address label: %m");
935 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
937 if (link
->address_label_messages
== 0)
938 log_link_debug(link
, "Addresses label set");
943 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
944 _cleanup_free_
struct in_addr
*addresses
= NULL
;
945 size_t n_addresses
= 0, n_allocated
= 0;
948 log_debug("Copying DNS server information from %s", link
->ifname
);
953 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
956 /* Only look for IPv4 addresses */
957 if (link
->network
->dns
[i
].family
!= AF_INET
)
960 ia
= link
->network
->dns
[i
].address
.in
;
962 /* Never propagate obviously borked data */
963 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
966 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
969 addresses
[n_addresses
++] = ia
;
972 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
973 const struct in_addr
*da
= NULL
;
976 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
979 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
982 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
987 if (n_addresses
<= 0)
990 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
993 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
994 _cleanup_free_
struct in_addr
*addresses
= NULL
;
995 size_t n_addresses
= 0, n_allocated
= 0;
1001 log_debug("Copying NTP server information from %s", link
->ifname
);
1003 STRV_FOREACH(a
, link
->network
->ntp
) {
1006 /* Only look for IPv4 addresses */
1007 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1010 /* Never propagate obviously borked data */
1011 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1014 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1017 addresses
[n_addresses
++] = ia
;
1020 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1021 const struct in_addr
*da
= NULL
;
1024 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1027 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1030 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1035 if (n_addresses
<= 0)
1038 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1041 static int link_set_bridge_fdb(Link
*link
) {
1042 FdbEntry
*fdb_entry
;
1045 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1046 r
= fdb_entry_configure(link
, fdb_entry
);
1048 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1054 static int link_enter_set_addresses(Link
*link
) {
1055 AddressLabel
*label
;
1060 assert(link
->network
);
1061 assert(link
->state
!= _LINK_STATE_INVALID
);
1063 r
= link_set_bridge_fdb(link
);
1067 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1069 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1070 r
= address_configure(ad
, link
, address_handler
, false);
1072 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1073 link_enter_failed(link
);
1077 link
->address_messages
++;
1080 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1081 r
= address_label_configure(label
, link
, address_label_handler
, false);
1083 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1084 link_enter_failed(link
);
1088 link
->address_label_messages
++;
1091 /* now that we can figure out a default address for the dhcp server,
1093 if (link_dhcp4_server_enabled(link
)) {
1095 Link
*uplink
= NULL
;
1096 bool acquired_uplink
= false;
1098 address
= link_find_dhcp_server_address(link
);
1100 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1101 link_enter_failed(link
);
1105 /* use the server address' subnet as the pool */
1106 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1107 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1112 r = sd_dhcp_server_set_router(link->dhcp_server,
1113 &main_address->in_addr.in);
1118 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1119 r
= sd_dhcp_server_set_max_lease_time(
1121 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1126 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1127 r
= sd_dhcp_server_set_default_lease_time(
1129 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1134 if (link
->network
->dhcp_server_emit_dns
) {
1136 if (link
->network
->n_dhcp_server_dns
> 0)
1137 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1139 uplink
= manager_find_uplink(link
->manager
, link
);
1140 acquired_uplink
= true;
1143 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1146 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1149 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1152 if (link
->network
->dhcp_server_emit_ntp
) {
1154 if (link
->network
->n_dhcp_server_ntp
> 0)
1155 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1157 if (!acquired_uplink
)
1158 uplink
= manager_find_uplink(link
->manager
, link
);
1161 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1164 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1168 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1171 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1173 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1177 if (link
->network
->dhcp_server_emit_timezone
) {
1178 _cleanup_free_
char *buffer
= NULL
;
1179 const char *tz
= NULL
;
1181 if (link
->network
->dhcp_server_timezone
)
1182 tz
= link
->network
->dhcp_server_timezone
;
1184 r
= get_timezone(&buffer
);
1186 log_warning_errno(r
, "Failed to determine timezone: %m");
1192 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1198 r
= sd_dhcp_server_start(link
->dhcp_server
);
1200 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1202 link_enter_failed(link
);
1207 log_link_debug(link
, "Offering DHCPv4 leases");
1210 if (link
->address_messages
== 0)
1211 link_enter_set_routes(link
);
1213 log_link_debug(link
, "Setting addresses");
1218 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1219 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1224 assert(link
->ifname
);
1226 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1229 r
= sd_netlink_message_get_errno(m
);
1230 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1231 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1236 static int link_set_bridge_vlan(Link
*link
) {
1239 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1241 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1246 static int link_set_proxy_arp(Link
*link
) {
1247 const char *p
= NULL
;
1250 if (!link_proxy_arp_enabled(link
))
1253 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1255 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1257 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1262 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1263 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1266 log_link_debug(link
, "Set link");
1268 r
= sd_netlink_message_get_errno(m
);
1269 if (r
< 0 && r
!= -EEXIST
) {
1270 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1271 link_enter_failed(link
);
1278 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1279 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1284 assert(link
->ifname
);
1286 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1289 r
= sd_netlink_message_get_errno(m
);
1291 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1296 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1297 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1301 assert(link
->manager
);
1302 assert(link
->manager
->rtnl
);
1304 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1306 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1308 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1310 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1312 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1314 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1316 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1318 link
->setting_mtu
= true;
1325 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1326 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1331 assert(link
->ifname
);
1333 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1336 r
= sd_netlink_message_get_errno(m
);
1338 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1343 static int link_set_flags(Link
*link
) {
1344 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1345 unsigned ifi_change
= 0;
1346 unsigned ifi_flags
= 0;
1350 assert(link
->manager
);
1351 assert(link
->manager
->rtnl
);
1353 if (link
->flags
& IFF_LOOPBACK
)
1359 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1362 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1364 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1366 if (link
->network
->arp
>= 0) {
1367 ifi_change
|= IFF_NOARP
;
1368 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1371 if (link
->network
->multicast
>= 0) {
1372 ifi_change
|= IFF_MULTICAST
;
1373 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1376 if (link
->network
->allmulticast
>= 0) {
1377 ifi_change
|= IFF_ALLMULTI
;
1378 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1381 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1383 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1385 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1387 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1394 static int link_set_bridge(Link
*link
) {
1395 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1399 assert(link
->network
);
1401 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1403 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1405 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1407 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1409 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1411 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1413 if (link
->network
->use_bpdu
>= 0) {
1414 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1416 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1419 if (link
->network
->hairpin
>= 0) {
1420 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1422 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1425 if (link
->network
->fast_leave
>= 0) {
1426 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1428 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1431 if (link
->network
->allow_port_to_be_root
>= 0) {
1432 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1434 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1438 if (link
->network
->unicast_flood
>= 0) {
1439 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1441 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1445 if (link
->network
->cost
!= 0) {
1446 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1448 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1450 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1451 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1453 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1456 r
= sd_netlink_message_close_container(req
);
1458 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1460 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1462 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1469 static int link_bond_set(Link
*link
) {
1470 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1474 assert(link
->network
);
1476 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1478 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1480 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1482 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1484 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1486 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1488 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1490 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1492 if (link
->network
->active_slave
) {
1493 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1495 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1498 if (link
->network
->primary_slave
) {
1499 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1501 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1504 r
= sd_netlink_message_close_container(req
);
1506 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1508 r
= sd_netlink_message_close_container(req
);
1510 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1512 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1514 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1521 static int link_lldp_save(Link
*link
) {
1522 _cleanup_free_
char *temp_path
= NULL
;
1523 _cleanup_fclose_
FILE *f
= NULL
;
1524 sd_lldp_neighbor
**l
= NULL
;
1528 assert(link
->lldp_file
);
1531 (void) unlink(link
->lldp_file
);
1535 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1539 (void) unlink(link
->lldp_file
);
1545 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1549 fchmod(fileno(f
), 0644);
1551 for (i
= 0; i
< n
; i
++) {
1556 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1561 (void) fwrite(&u
, 1, sizeof(u
), f
);
1562 (void) fwrite(p
, 1, sz
, f
);
1565 r
= fflush_and_check(f
);
1569 if (rename(temp_path
, link
->lldp_file
) < 0) {
1576 (void) unlink(link
->lldp_file
);
1578 (void) unlink(temp_path
);
1580 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1584 for (i
= 0; i
< n
; i
++)
1585 sd_lldp_neighbor_unref(l
[i
]);
1592 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1593 Link
*link
= userdata
;
1598 (void) link_lldp_save(link
);
1600 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1601 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1603 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1605 r
= link_lldp_emit_start(link
);
1607 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1611 static int link_acquire_ipv6_conf(Link
*link
) {
1616 if (link_dhcp6_enabled(link
)) {
1617 assert(link
->dhcp6_client
);
1618 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1620 /* start DHCPv6 client in stateless mode */
1621 r
= dhcp6_request_address(link
, true);
1622 if (r
< 0 && r
!= -EBUSY
)
1623 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1625 log_link_debug(link
, "Acquiring DHCPv6 lease");
1628 if (link_ipv6_accept_ra_enabled(link
)) {
1629 assert(link
->ndisc
);
1631 log_link_debug(link
, "Discovering IPv6 routers");
1633 r
= sd_ndisc_start(link
->ndisc
);
1634 if (r
< 0 && r
!= -EBUSY
)
1635 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1638 if (link_radv_enabled(link
)) {
1640 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1642 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1644 r
= sd_radv_start(link
->radv
);
1645 if (r
< 0 && r
!= -EBUSY
)
1646 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1652 static int link_acquire_ipv4_conf(Link
*link
) {
1656 assert(link
->network
);
1657 assert(link
->manager
);
1658 assert(link
->manager
->event
);
1660 if (link_ipv4ll_enabled(link
)) {
1661 assert(link
->ipv4ll
);
1663 log_link_debug(link
, "Acquiring IPv4 link-local address");
1665 r
= sd_ipv4ll_start(link
->ipv4ll
);
1667 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1670 if (link_dhcp4_enabled(link
)) {
1671 assert(link
->dhcp_client
);
1673 log_link_debug(link
, "Acquiring DHCPv4 lease");
1675 r
= sd_dhcp_client_start(link
->dhcp_client
);
1677 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1683 static int link_acquire_conf(Link
*link
) {
1688 if (link
->setting_mtu
) {
1689 link
->setting_mtu
= false;
1693 r
= link_acquire_ipv4_conf(link
);
1697 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1698 r
= link_acquire_ipv6_conf(link
);
1703 if (link_lldp_emit_enabled(link
)) {
1704 r
= link_lldp_emit_start(link
);
1706 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1712 bool link_has_carrier(Link
*link
) {
1713 /* see Documentation/networking/operstates.txt in the kernel sources */
1715 if (link
->kernel_operstate
== IF_OPER_UP
)
1718 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1719 /* operstate may not be implemented, so fall back to flags */
1720 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1726 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1727 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1732 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1735 r
= sd_netlink_message_get_errno(m
);
1737 /* we warn but don't fail the link, as it may be
1739 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1744 int link_up(Link
*link
) {
1745 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1746 uint8_t ipv6ll_mode
;
1750 assert(link
->network
);
1751 assert(link
->manager
);
1752 assert(link
->manager
->rtnl
);
1754 log_link_debug(link
, "Bringing link up");
1756 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1758 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1760 /* set it free if not enslaved with networkd */
1761 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1762 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1764 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1767 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1769 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1771 if (link
->network
->mac
) {
1772 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1774 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1777 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1778 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1779 (void) link_enable_ipv6(link
);
1781 if (link
->network
->mtu
!= 0) {
1782 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1783 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1784 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1786 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1787 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1789 link
->network
->mtu
= IPV6_MIN_MTU
;
1792 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1794 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1797 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1799 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1801 if (link_ipv6_enabled(link
)) {
1802 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1803 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1805 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1807 if (!link_ipv6ll_enabled(link
))
1808 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1810 const char *p
= NULL
;
1811 _cleanup_free_
char *stable_secret
= NULL
;
1813 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1814 r
= read_one_line_file(p
, &stable_secret
);
1817 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1819 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1821 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1823 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1825 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1826 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1828 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1831 r
= sd_netlink_message_close_container(req
);
1833 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1836 r
= sd_netlink_message_close_container(req
);
1838 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1840 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1842 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1849 static int link_up_can(Link
*link
) {
1850 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1855 log_link_debug(link
, "Bringing CAN link up");
1857 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1859 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1861 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1863 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1865 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1867 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1874 static int link_set_can(Link
*link
) {
1875 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1879 assert(link
->network
);
1880 assert(link
->manager
);
1881 assert(link
->manager
->rtnl
);
1883 log_link_debug(link
, "link_set_can");
1885 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1887 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1889 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1891 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1893 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1895 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1897 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1899 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1901 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1902 struct can_bittiming bt
= {
1903 .bitrate
= link
->network
->can_bitrate
,
1904 .sample_point
= link
->network
->can_sample_point
,
1907 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1908 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1912 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1913 if (link
->network
->can_sample_point
> 0)
1914 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1916 log_link_debug(link
, "Using default sample point");
1918 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1920 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1923 if (link
->network
->can_restart_us
> 0) {
1924 char time_string
[FORMAT_TIMESPAN_MAX
];
1925 uint64_t restart_ms
;
1927 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1930 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1932 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1934 if (restart_ms
> UINT32_MAX
) {
1935 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1939 log_link_debug(link
, "Setting restart = %s", time_string
);
1941 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1943 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1946 r
= sd_netlink_message_close_container(m
);
1948 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1950 r
= sd_netlink_message_close_container(m
);
1952 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1954 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, link_set_handler
, link
, 0, NULL
);
1956 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1960 if (!(link
->flags
& IFF_UP
)) {
1961 r
= link_up_can(link
);
1963 link_enter_failed(link
);
1968 log_link_debug(link
, "link_set_can done");
1973 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1974 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1979 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1982 r
= sd_netlink_message_get_errno(m
);
1984 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1986 if (streq_ptr(link
->kind
, "can")) {
1994 int link_down(Link
*link
) {
1995 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1999 assert(link
->manager
);
2000 assert(link
->manager
->rtnl
);
2002 log_link_debug(link
, "Bringing link down");
2004 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2005 RTM_SETLINK
, link
->ifindex
);
2007 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2009 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2011 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2013 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
2015 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2022 static int link_handle_bound_to_list(Link
*link
) {
2026 bool required_up
= false;
2027 bool link_is_up
= false;
2031 if (hashmap_isempty(link
->bound_to_links
))
2034 if (link
->flags
& IFF_UP
)
2037 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2038 if (link_has_carrier(l
)) {
2043 if (!required_up
&& link_is_up
) {
2044 r
= link_down(link
);
2047 } else if (required_up
&& !link_is_up
) {
2056 static int link_handle_bound_by_list(Link
*link
) {
2063 if (hashmap_isempty(link
->bound_by_links
))
2066 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2067 r
= link_handle_bound_to_list(l
);
2075 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2081 if (link
== carrier
)
2084 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2087 r
= hashmap_ensure_allocated(h
, NULL
);
2091 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2098 static int link_new_bound_by_list(Link
*link
) {
2103 bool list_updated
= false;
2106 assert(link
->manager
);
2110 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2111 if (!carrier
->network
)
2114 if (strv_isempty(carrier
->network
->bind_carrier
))
2117 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2118 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2122 list_updated
= true;
2129 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2130 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2134 link_dirty(carrier
);
2140 static int link_new_bound_to_list(Link
*link
) {
2145 bool list_updated
= false;
2148 assert(link
->manager
);
2153 if (strv_isempty(link
->network
->bind_carrier
))
2158 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2159 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2160 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2164 list_updated
= true;
2171 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2172 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2176 link_dirty(carrier
);
2182 static int link_new_carrier_maps(Link
*link
) {
2185 r
= link_new_bound_by_list(link
);
2189 r
= link_handle_bound_by_list(link
);
2193 r
= link_new_bound_to_list(link
);
2197 r
= link_handle_bound_to_list(link
);
2204 static void link_free_bound_to_list(Link
*link
) {
2208 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2209 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2211 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2212 link_dirty(bound_to
);
2218 static void link_free_bound_by_list(Link
*link
) {
2222 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2223 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2225 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2226 link_dirty(bound_by
);
2227 link_handle_bound_to_list(bound_by
);
2234 static void link_free_carrier_maps(Link
*link
) {
2235 bool list_updated
= false;
2239 if (!hashmap_isempty(link
->bound_to_links
)) {
2240 link_free_bound_to_list(link
);
2241 list_updated
= true;
2244 if (!hashmap_isempty(link
->bound_by_links
)) {
2245 link_free_bound_by_list(link
);
2246 list_updated
= true;
2255 void link_drop(Link
*link
) {
2256 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2259 link_set_state(link
, LINK_STATE_LINGER
);
2261 link_free_carrier_maps(link
);
2263 log_link_debug(link
, "Link removed");
2265 (void)unlink(link
->state_file
);
2271 static int link_joined(Link
*link
) {
2275 assert(link
->network
);
2277 if (!hashmap_isempty(link
->bound_to_links
)) {
2278 r
= link_handle_bound_to_list(link
);
2281 } else if (!(link
->flags
& IFF_UP
)) {
2284 link_enter_failed(link
);
2289 if (link
->network
->bridge
) {
2290 r
= link_set_bridge(link
);
2292 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2295 if (link
->network
->bond
) {
2296 r
= link_bond_set(link
);
2298 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2301 if (link
->network
->use_br_vlan
&&
2302 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2303 r
= link_set_bridge_vlan(link
);
2305 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2308 /* Skip setting up addresses until it gets carrier,
2309 or it would try to set addresses twice,
2310 which is bad for non-idempotent steps. */
2311 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2314 return link_enter_set_addresses(link
);
2317 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2318 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2322 assert(link
->network
);
2326 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2329 r
= sd_netlink_message_get_errno(m
);
2330 if (r
< 0 && r
!= -EEXIST
) {
2331 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2332 link_enter_failed(link
);
2335 log_link_debug(link
, "Joined netdev");
2337 if (link
->enslaving
<= 0)
2343 static int link_enter_join_netdev(Link
*link
) {
2349 assert(link
->network
);
2350 assert(link
->state
== LINK_STATE_PENDING
);
2352 link_set_state(link
, LINK_STATE_ENSLAVING
);
2356 if (!link
->network
->bridge
&&
2357 !link
->network
->bond
&&
2358 !link
->network
->vrf
&&
2359 hashmap_isempty(link
->network
->stacked_netdevs
))
2360 return link_joined(link
);
2362 if (link
->network
->bond
) {
2363 log_struct(LOG_DEBUG
,
2364 LOG_LINK_INTERFACE(link
),
2365 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2366 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2368 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2370 log_struct_errno(LOG_WARNING
, r
,
2371 LOG_LINK_INTERFACE(link
),
2372 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2373 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2374 link_enter_failed(link
);
2381 if (link
->network
->bridge
) {
2382 log_struct(LOG_DEBUG
,
2383 LOG_LINK_INTERFACE(link
),
2384 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2385 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2387 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2389 log_struct_errno(LOG_WARNING
, r
,
2390 LOG_LINK_INTERFACE(link
),
2391 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2392 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2393 link_enter_failed(link
);
2400 if (link
->network
->vrf
) {
2401 log_struct(LOG_DEBUG
,
2402 LOG_LINK_INTERFACE(link
),
2403 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2404 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2406 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2408 log_struct_errno(LOG_WARNING
, r
,
2409 LOG_LINK_INTERFACE(link
),
2410 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2411 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2412 link_enter_failed(link
);
2419 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2421 if (netdev
->ifindex
> 0) {
2426 log_struct(LOG_DEBUG
,
2427 LOG_LINK_INTERFACE(link
),
2428 LOG_NETDEV_INTERFACE(netdev
),
2429 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2431 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2433 log_struct_errno(LOG_WARNING
, r
,
2434 LOG_LINK_INTERFACE(link
),
2435 LOG_NETDEV_INTERFACE(netdev
),
2436 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2437 link_enter_failed(link
);
2447 static int link_set_ipv4_forward(Link
*link
) {
2450 if (!link_ipv4_forward_enabled(link
))
2453 /* We propagate the forwarding flag from one interface to the
2454 * global setting one way. This means: as long as at least one
2455 * interface was configured at any time that had IP forwarding
2456 * enabled the setting will stay on for good. We do this
2457 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2458 * somewhat in sync (see below). */
2460 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2462 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2467 static int link_set_ipv6_forward(Link
*link
) {
2470 if (!link_ipv6_forward_enabled(link
))
2473 /* On Linux, the IPv6 stack does not know a per-interface
2474 * packet forwarding setting: either packet forwarding is on
2475 * for all, or off for all. We hence don't bother with a
2476 * per-interface setting, but simply propagate the interface
2477 * flag, if it is set, to the global flag, one-way. Note that
2478 * while IPv4 would allow a per-interface flag, we expose the
2479 * same behaviour there and also propagate the setting from
2480 * one to all, to keep things simple (see above). */
2482 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2484 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2489 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2490 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2491 IPv6PrivacyExtensions s
;
2492 const char *p
= NULL
;
2495 s
= link_ipv6_privacy_extensions(link
);
2499 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2500 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2502 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2504 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2509 static int link_set_ipv6_accept_ra(Link
*link
) {
2510 const char *p
= NULL
;
2513 /* Make this a NOP if IPv6 is not available */
2514 if (!socket_ipv6_is_supported())
2517 if (link
->flags
& IFF_LOOPBACK
)
2523 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2525 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2526 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2528 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2533 static int link_set_ipv6_dad_transmits(Link
*link
) {
2534 char buf
[DECIMAL_STR_MAX(int) + 1];
2535 const char *p
= NULL
;
2538 /* Make this a NOP if IPv6 is not available */
2539 if (!socket_ipv6_is_supported())
2542 if (link
->flags
& IFF_LOOPBACK
)
2548 if (link
->network
->ipv6_dad_transmits
< 0)
2551 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2552 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2554 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2556 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2561 static int link_set_ipv6_hop_limit(Link
*link
) {
2562 char buf
[DECIMAL_STR_MAX(int) + 1];
2563 const char *p
= NULL
;
2566 /* Make this a NOP if IPv6 is not available */
2567 if (!socket_ipv6_is_supported())
2570 if (link
->flags
& IFF_LOOPBACK
)
2576 if (link
->network
->ipv6_hop_limit
< 0)
2579 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2580 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2582 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2584 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2589 static int link_set_ipv6_mtu(Link
*link
) {
2590 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2591 const char *p
= NULL
;
2594 /* Make this a NOP if IPv6 is not available */
2595 if (!socket_ipv6_is_supported())
2598 if (link
->flags
& IFF_LOOPBACK
)
2601 if (link
->network
->ipv6_mtu
== 0)
2604 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2606 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2608 r
= write_string_file(p
, buf
, 0);
2610 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2615 static int link_drop_foreign_config(Link
*link
) {
2621 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2622 /* we consider IPv6LL addresses to be managed by the kernel */
2623 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2626 r
= address_remove(address
, link
, link_address_remove_handler
);
2631 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2632 /* do not touch routes managed by the kernel */
2633 if (route
->protocol
== RTPROT_KERNEL
)
2636 r
= route_remove(route
, link
, link_route_remove_handler
);
2644 static int link_drop_config(Link
*link
) {
2645 Address
*address
, *pool_address
;
2650 SET_FOREACH(address
, link
->addresses
, i
) {
2651 /* we consider IPv6LL addresses to be managed by the kernel */
2652 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2655 r
= address_remove(address
, link
, link_address_remove_handler
);
2659 /* If this address came from an address pool, clean up the pool */
2660 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2661 if (address_equal(address
, pool_address
)) {
2662 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2663 address_free(pool_address
);
2669 SET_FOREACH(route
, link
->routes
, i
) {
2670 /* do not touch routes managed by the kernel */
2671 if (route
->protocol
== RTPROT_KERNEL
)
2674 r
= route_remove(route
, link
, link_route_remove_handler
);
2684 static int link_update_lldp(Link
*link
) {
2692 if (link
->flags
& IFF_UP
) {
2693 r
= sd_lldp_start(link
->lldp
);
2695 log_link_debug(link
, "Started LLDP.");
2697 r
= sd_lldp_stop(link
->lldp
);
2699 log_link_debug(link
, "Stopped LLDP.");
2705 static int link_configure_can(Link
*link
) {
2708 if (streq_ptr(link
->kind
, "can")) {
2709 /* The CAN interface must be down to configure bitrate, etc... */
2710 if ((link
->flags
& IFF_UP
)) {
2711 r
= link_down(link
);
2713 link_enter_failed(link
);
2720 return link_set_can(link
);
2723 if (!(link
->flags
& IFF_UP
)) {
2724 r
= link_up_can(link
);
2726 link_enter_failed(link
);
2734 static int link_configure(Link
*link
) {
2738 assert(link
->network
);
2739 assert(link
->state
== LINK_STATE_PENDING
);
2741 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2742 return link_configure_can(link
);
2744 /* Drop foreign config, but ignore loopback or critical devices.
2745 * We do not want to remove loopback address or addresses used for root NFS. */
2746 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2747 r
= link_drop_foreign_config(link
);
2752 r
= link_set_proxy_arp(link
);
2756 r
= ipv6_proxy_ndp_addresses_configure(link
);
2760 r
= link_set_ipv4_forward(link
);
2764 r
= link_set_ipv6_forward(link
);
2768 r
= link_set_ipv6_privacy_extensions(link
);
2772 r
= link_set_ipv6_accept_ra(link
);
2776 r
= link_set_ipv6_dad_transmits(link
);
2780 r
= link_set_ipv6_hop_limit(link
);
2784 r
= link_set_flags(link
);
2788 r
= link_set_ipv6_mtu(link
);
2792 if (link_ipv4ll_enabled(link
)) {
2793 r
= ipv4ll_configure(link
);
2798 if (link_dhcp4_enabled(link
)) {
2799 r
= dhcp4_set_promote_secondaries(link
);
2803 r
= dhcp4_configure(link
);
2808 if (link_dhcp4_server_enabled(link
)) {
2809 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2813 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2818 if (link_dhcp6_enabled(link
) ||
2819 link_ipv6_accept_ra_enabled(link
)) {
2820 r
= dhcp6_configure(link
);
2825 if (link_ipv6_accept_ra_enabled(link
)) {
2826 r
= ndisc_configure(link
);
2831 if (link_radv_enabled(link
)) {
2832 r
= radv_configure(link
);
2837 if (link_lldp_rx_enabled(link
)) {
2838 r
= sd_lldp_new(&link
->lldp
);
2842 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2846 r
= sd_lldp_match_capabilities(link
->lldp
,
2847 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2848 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2849 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2853 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2857 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2861 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2865 r
= link_update_lldp(link
);
2870 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2871 r
= link_acquire_conf(link
);
2876 return link_enter_join_netdev(link
);
2879 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2881 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2886 assert(link
->ifname
);
2887 assert(link
->manager
);
2889 if (link
->state
!= LINK_STATE_PENDING
)
2892 log_link_debug(link
, "Link state is up-to-date");
2894 r
= link_new_bound_by_list(link
);
2898 r
= link_handle_bound_by_list(link
);
2902 if (!link
->network
) {
2903 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2904 &link
->mac
, &network
);
2906 link_enter_unmanaged(link
);
2908 } else if (r
== 0 && network
->unmanaged
) {
2909 link_enter_unmanaged(link
);
2914 if (link
->flags
& IFF_LOOPBACK
) {
2915 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2916 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2918 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2919 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2921 if (network
->dhcp_server
)
2922 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2925 r
= network_apply(network
, link
);
2930 r
= link_new_bound_to_list(link
);
2934 r
= link_configure(link
);
2941 int link_initialized(Link
*link
, struct udev_device
*device
) {
2942 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2946 assert(link
->manager
);
2947 assert(link
->manager
->rtnl
);
2950 if (link
->state
!= LINK_STATE_PENDING
)
2953 if (link
->udev_device
)
2956 log_link_debug(link
, "udev initialized link");
2958 link
->udev_device
= udev_device_ref(device
);
2960 /* udev has initialized the link, but we don't know if we have yet
2961 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2962 * when it returns we know that the pending NEWLINKs have already been
2963 * processed and that we are up-to-date */
2965 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2970 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2971 link_initialized_and_synced
, link
, 0, NULL
);
2980 static int link_load(Link
*link
) {
2981 _cleanup_free_
char *network_file
= NULL
,
2984 *dhcp4_address
= NULL
,
2985 *ipv4ll_address
= NULL
;
2986 union in_addr_union address
;
2987 union in_addr_union route_dst
;
2993 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
2994 "NETWORK_FILE", &network_file
,
2995 "ADDRESSES", &addresses
,
2997 "DHCP4_ADDRESS", &dhcp4_address
,
2998 "IPV4LL_ADDRESS", &ipv4ll_address
,
3000 if (r
< 0 && r
!= -ENOENT
)
3001 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3008 suffix
= strrchr(network_file
, '.');
3010 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3011 goto network_file_fail
;
3015 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3017 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3018 goto network_file_fail
;
3021 r
= network_apply(network
, link
);
3023 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3032 _cleanup_free_
char *address_str
= NULL
;
3033 char *prefixlen_str
;
3035 unsigned char prefixlen
;
3037 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3039 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3045 prefixlen_str
= strchr(address_str
, '/');
3046 if (!prefixlen_str
) {
3047 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3051 *prefixlen_str
++ = '\0';
3053 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3055 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3059 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3061 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3065 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3067 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3076 _cleanup_free_
char *route_str
= NULL
;
3077 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3079 char *prefixlen_str
;
3081 unsigned char prefixlen
, tos
, table
;
3084 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3086 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3092 prefixlen_str
= strchr(route_str
, '/');
3093 if (!prefixlen_str
) {
3094 log_link_debug(link
, "Failed to parse route %s", route_str
);
3098 *prefixlen_str
++ = '\0';
3100 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3102 log_link_debug(link
,
3103 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3108 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3110 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3114 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3116 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3118 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3119 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3120 0, route_expire_handler
, route
);
3122 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3125 route
->lifetime
= lifetime
;
3126 sd_event_source_unref(route
->expire
);
3127 route
->expire
= TAKE_PTR(expire
);
3131 if (dhcp4_address
) {
3132 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3134 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3135 goto dhcp4_address_fail
;
3138 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3140 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3142 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3144 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3149 if (ipv4ll_address
) {
3150 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3152 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3153 goto ipv4ll_address_fail
;
3156 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3158 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3160 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3162 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3165 ipv4ll_address_fail
:
3170 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3172 _cleanup_(udev_device_unrefp
) struct udev_device
*device
= NULL
;
3173 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3181 r
= link_new(m
, message
, ret
);
3187 log_link_debug(link
, "Link %d added", link
->ifindex
);
3189 r
= link_load(link
);
3193 if (detect_container() <= 0) {
3194 /* not in a container, udev will be around */
3195 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3196 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3198 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3202 if (udev_device_get_is_initialized(device
) <= 0) {
3204 log_link_debug(link
, "link pending udev initialization...");
3208 r
= link_initialized(link
, device
);
3212 /* we are calling a callback directly, so must take a ref */
3215 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3222 link_enter_failed(link
);
3226 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3231 log_link_info(link
, "Gained IPv6LL");
3233 link
->ipv6ll_address
= *address
;
3234 link_check_ready(link
);
3236 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3237 r
= link_acquire_ipv6_conf(link
);
3239 link_enter_failed(link
);
3247 static int link_carrier_gained(Link
*link
) {
3252 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3253 r
= link_acquire_conf(link
);
3255 link_enter_failed(link
);
3259 r
= link_enter_set_addresses(link
);
3264 r
= link_handle_bound_by_list(link
);
3271 static int link_carrier_lost(Link
*link
) {
3276 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3277 setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3278 configuration and stop the clients as well. */
3279 if (link
->setting_mtu
)
3282 r
= link_stop_clients(link
);
3284 link_enter_failed(link
);
3288 if (link_dhcp4_server_enabled(link
))
3289 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3291 r
= link_drop_config(link
);
3295 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3296 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3297 r
= link_drop_foreign_config(link
);
3302 r
= link_handle_bound_by_list(link
);
3309 int link_carrier_reset(Link
*link
) {
3314 if (link_has_carrier(link
)) {
3315 r
= link_carrier_lost(link
);
3319 r
= link_carrier_gained(link
);
3323 log_link_info(link
, "Reset carrier");
3329 int link_update(Link
*link
, sd_netlink_message
*m
) {
3330 struct ether_addr mac
;
3333 bool had_carrier
, carrier_gained
, carrier_lost
;
3337 assert(link
->ifname
);
3340 if (link
->state
== LINK_STATE_LINGER
) {
3342 log_link_info(link
, "Link readded");
3343 link_set_state(link
, LINK_STATE_ENSLAVING
);
3345 r
= link_new_carrier_maps(link
);
3350 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3351 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3352 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3354 if (link
->state
== LINK_STATE_PENDING
) {
3355 r
= free_and_strdup(&link
->ifname
, ifname
);
3359 Manager
*manager
= link
->manager
;
3362 r
= link_add(manager
, m
, &link
);
3368 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3369 if (r
>= 0 && mtu
> 0) {
3371 if (link
->original_mtu
== 0) {
3372 link
->original_mtu
= mtu
;
3373 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3376 if (link
->dhcp_client
) {
3377 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3380 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3386 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3388 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3392 /* The kernel may broadcast NEWLINK messages without the MAC address
3393 set, simply ignore them. */
3394 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3396 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3399 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3402 log_link_debug(link
, "MAC address: "
3403 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3404 mac
.ether_addr_octet
[0],
3405 mac
.ether_addr_octet
[1],
3406 mac
.ether_addr_octet
[2],
3407 mac
.ether_addr_octet
[3],
3408 mac
.ether_addr_octet
[4],
3409 mac
.ether_addr_octet
[5]);
3412 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3414 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3417 if (link
->dhcp_client
) {
3418 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3419 (const uint8_t *) &link
->mac
,
3423 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3425 switch (link
->network
->dhcp_client_identifier
) {
3426 case DHCP_CLIENT_ID_DUID
: {
3427 const DUID
*duid
= link_duid(link
);
3429 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3430 link
->network
->iaid
,
3432 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3433 duid
->raw_data_len
);
3435 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3438 case DHCP_CLIENT_ID_DUID_ONLY
: {
3439 const DUID
*duid
= link_duid(link
);
3441 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
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 in DHCP client: %m");
3449 case DHCP_CLIENT_ID_MAC
:
3450 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3452 (const uint8_t *)&link
->mac
,
3455 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3458 assert_not_reached("Unknown client identifier type.");
3462 if (link
->dhcp6_client
) {
3463 const DUID
* duid
= link_duid(link
);
3465 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3466 (const uint8_t *) &link
->mac
,
3470 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3472 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3473 link
->network
->iaid
);
3475 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3477 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3479 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3480 duid
->raw_data_len
);
3482 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3486 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3488 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3492 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3494 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3499 had_carrier
= link_has_carrier(link
);
3501 r
= link_update_flags(link
, m
);
3505 r
= link_update_lldp(link
);
3509 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3510 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3512 if (carrier_gained
) {
3513 log_link_info(link
, "Gained carrier");
3515 r
= link_carrier_gained(link
);
3518 } else if (carrier_lost
) {
3519 log_link_info(link
, "Lost carrier");
3521 r
= link_carrier_lost(link
);
3529 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3537 if (hashmap_isempty(h
))
3541 HASHMAP_FOREACH(link
, h
, i
) {
3545 fprintf(f
, "%i", link
->ifindex
);
3552 int link_save(Link
*link
) {
3553 _cleanup_free_
char *temp_path
= NULL
;
3554 _cleanup_fclose_
FILE *f
= NULL
;
3555 const char *admin_state
, *oper_state
;
3562 assert(link
->state_file
);
3563 assert(link
->lease_file
);
3564 assert(link
->manager
);
3566 if (link
->state
== LINK_STATE_LINGER
) {
3567 unlink(link
->state_file
);
3571 link_lldp_save(link
);
3573 admin_state
= link_state_to_string(link
->state
);
3574 assert(admin_state
);
3576 oper_state
= link_operstate_to_string(link
->operstate
);
3579 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3583 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3584 (void) fchmod(fileno(f
), 0644);
3587 "# This is private data. Do not parse.\n"
3590 admin_state
, oper_state
);
3592 if (link
->network
) {
3594 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3595 const char *dhcp_domainname
= NULL
;
3596 char **dhcp6_domains
= NULL
;
3597 char **dhcp_domains
= NULL
;
3600 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3601 yes_no(link
->network
->required_for_online
));
3603 if (link
->dhcp6_client
) {
3604 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3605 if (r
< 0 && r
!= -ENOMSG
)
3606 log_link_debug(link
, "No DHCPv6 lease");
3609 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3614 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3615 _cleanup_free_
char *b
= NULL
;
3617 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3618 &link
->network
->dns
[j
].address
, &b
);
3620 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3630 if (link
->network
->dhcp_use_dns
&&
3632 const struct in_addr
*addresses
;
3634 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3638 serialize_in_addrs(f
, addresses
, r
);
3643 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3644 struct in6_addr
*in6_addrs
;
3646 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3650 serialize_in6_addrs(f
, in6_addrs
, r
);
3655 /* Make sure to flush out old entries before we use the NDISC data */
3658 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3661 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3665 serialize_in6_addrs(f
, &dd
->address
, 1);
3674 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3676 if (link
->network
->dhcp_use_ntp
&&
3678 const struct in_addr
*addresses
;
3680 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3684 serialize_in_addrs(f
, addresses
, r
);
3689 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3690 struct in6_addr
*in6_addrs
;
3693 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3698 serialize_in6_addrs(f
, in6_addrs
, r
);
3702 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3704 fputstrv(f
, hosts
, NULL
, &space
);
3709 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3710 if (link
->dhcp_lease
) {
3711 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3712 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3715 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3718 fputs("DOMAINS=", f
);
3720 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3722 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3725 if (dhcp_domainname
)
3726 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3728 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3730 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3732 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3733 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3738 fputs("ROUTE_DOMAINS=", f
);
3740 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3742 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3745 if (dhcp_domainname
)
3746 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3748 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3750 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3752 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3753 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3758 fprintf(f
, "LLMNR=%s\n",
3759 resolve_support_to_string(link
->network
->llmnr
));
3760 fprintf(f
, "MDNS=%s\n",
3761 resolve_support_to_string(link
->network
->mdns
));
3763 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3764 fprintf(f
, "DNS_OVER_TLS=%s\n",
3765 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3767 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3768 fprintf(f
, "DNSSEC=%s\n",
3769 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3771 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3774 fputs("DNSSEC_NTA=", f
);
3776 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3777 fputs_with_space(f
, n
, NULL
, &space
);
3781 fputs("ADDRESSES=", f
);
3783 SET_FOREACH(a
, link
->addresses
, i
) {
3784 _cleanup_free_
char *address_str
= NULL
;
3786 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3790 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3795 fputs("ROUTES=", f
);
3797 SET_FOREACH(route
, link
->routes
, i
) {
3798 _cleanup_free_
char *route_str
= NULL
;
3800 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3804 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3805 space
? " " : "", route_str
,
3806 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3813 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3814 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3816 if (link
->dhcp_lease
) {
3817 struct in_addr address
;
3818 const char *tz
= NULL
;
3820 assert(link
->network
);
3822 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3824 fprintf(f
, "TIMEZONE=%s\n", tz
);
3826 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3828 fputs("DHCP4_ADDRESS=", f
);
3829 serialize_in_addrs(f
, &address
, 1);
3833 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3841 unlink(link
->lease_file
);
3844 struct in_addr address
;
3846 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3848 fputs("IPV4LL_ADDRESS=", f
);
3849 serialize_in_addrs(f
, &address
, 1);
3854 r
= fflush_and_check(f
);
3858 if (rename(temp_path
, link
->state_file
) < 0) {
3866 (void) unlink(link
->state_file
);
3868 (void) unlink(temp_path
);
3870 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3873 /* The serialized state in /run is no longer up-to-date. */
3874 void link_dirty(Link
*link
) {
3879 /* mark manager dirty as link is dirty */
3880 manager_dirty(link
->manager
);
3882 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3884 /* allocation errors are ignored */
3887 r
= set_put(link
->manager
->dirty_links
, link
);
3889 /* don't take another ref if the link was already dirty */
3895 /* The serialized state in /run is up-to-date */
3896 void link_clean(Link
*link
) {
3898 assert(link
->manager
);
3900 set_remove(link
->manager
->dirty_links
, link
);
3904 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3905 [LINK_STATE_PENDING
] = "pending",
3906 [LINK_STATE_ENSLAVING
] = "configuring",
3907 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3908 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3909 [LINK_STATE_CONFIGURED
] = "configured",
3910 [LINK_STATE_UNMANAGED
] = "unmanaged",
3911 [LINK_STATE_FAILED
] = "failed",
3912 [LINK_STATE_LINGER
] = "linger",
3915 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3917 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3918 [LINK_OPERSTATE_OFF
] = "off",
3919 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3920 [LINK_OPERSTATE_DORMANT
] = "dormant",
3921 [LINK_OPERSTATE_CARRIER
] = "carrier",
3922 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3923 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3926 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);