1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 #include <netinet/ether.h>
10 #include <linux/can/netlink.h>
12 #include <stdio_ext.h>
14 #include "alloc-util.h"
16 #include "dhcp-lease-internal.h"
19 #include "netlink-util.h"
20 #include "network-internal.h"
21 #include "networkd-ipv6-proxy-ndp.h"
22 #include "networkd-lldp-tx.h"
23 #include "networkd-manager.h"
24 #include "networkd-ndisc.h"
25 #include "networkd-radv.h"
26 #include "networkd-routing-policy-rule.h"
28 #include "socket-util.h"
29 #include "stdio-util.h"
30 #include "string-table.h"
31 #include "udev-util.h"
35 static bool link_dhcp6_enabled(Link
*link
) {
38 if (!socket_ipv6_is_supported())
41 if (link
->flags
& IFF_LOOPBACK
)
47 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
50 static bool link_dhcp4_enabled(Link
*link
) {
53 if (link
->flags
& IFF_LOOPBACK
)
59 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
62 static bool link_dhcp4_server_enabled(Link
*link
) {
65 if (link
->flags
& IFF_LOOPBACK
)
71 return link
->network
->dhcp_server
;
74 static bool link_ipv4ll_enabled(Link
*link
) {
77 if (link
->flags
& IFF_LOOPBACK
)
83 if (streq_ptr(link
->kind
, "wireguard"))
86 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
89 static bool link_ipv6ll_enabled(Link
*link
) {
92 if (!socket_ipv6_is_supported())
95 if (link
->flags
& IFF_LOOPBACK
)
101 if (streq_ptr(link
->kind
, "wireguard"))
104 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
107 static bool link_ipv6_enabled(Link
*link
) {
110 if (!socket_ipv6_is_supported())
113 if (link
->network
->bridge
)
116 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
117 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
120 static bool link_radv_enabled(Link
*link
) {
123 if (!link_ipv6ll_enabled(link
))
126 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
129 static bool link_lldp_rx_enabled(Link
*link
) {
132 if (link
->flags
& IFF_LOOPBACK
)
135 if (link
->iftype
!= ARPHRD_ETHER
)
141 /* LLDP should be handled on bridge slaves as those have a direct
142 * connection to their peers not on the bridge master. Linux doesn't
143 * even (by default) forward lldp packets to the bridge master.*/
144 if (streq_ptr("bridge", link
->kind
))
147 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
150 static bool link_lldp_emit_enabled(Link
*link
) {
153 if (link
->flags
& IFF_LOOPBACK
)
156 if (link
->iftype
!= ARPHRD_ETHER
)
162 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
165 static bool link_ipv4_forward_enabled(Link
*link
) {
168 if (link
->flags
& IFF_LOOPBACK
)
174 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
177 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
180 static bool link_ipv6_forward_enabled(Link
*link
) {
183 if (!socket_ipv6_is_supported())
186 if (link
->flags
& IFF_LOOPBACK
)
192 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
195 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
198 static bool link_proxy_arp_enabled(Link
*link
) {
201 if (link
->flags
& IFF_LOOPBACK
)
207 if (link
->network
->proxy_arp
< 0)
213 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
216 if (!socket_ipv6_is_supported())
219 if (link
->flags
& IFF_LOOPBACK
)
225 if (!link_ipv6ll_enabled(link
))
228 /* If unset use system default (enabled if local forwarding is disabled.
229 * disabled if local forwarding is enabled).
230 * If set, ignore or enforce RA independent of local forwarding state.
232 if (link
->network
->ipv6_accept_ra
< 0)
233 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
234 return !link_ipv6_forward_enabled(link
);
235 else if (link
->network
->ipv6_accept_ra
> 0)
236 /* accept RA even if ip_forward is enabled */
243 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
246 if (!socket_ipv6_is_supported())
247 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
249 if (link
->flags
& IFF_LOOPBACK
)
250 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
255 return link
->network
->ipv6_privacy_extensions
;
258 static int link_enable_ipv6(Link
*link
) {
259 const char *p
= NULL
;
263 if (link
->flags
& IFF_LOOPBACK
)
266 disabled
= !link_ipv6_enabled(link
);
268 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
270 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
272 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
273 enable_disable(!disabled
), link
->ifname
);
275 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
280 void link_update_operstate(Link
*link
) {
281 LinkOperationalState operstate
;
284 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
285 operstate
= LINK_OPERSTATE_DORMANT
;
286 else if (link_has_carrier(link
)) {
288 uint8_t scope
= RT_SCOPE_NOWHERE
;
291 /* if we have carrier, check what addresses we have */
292 SET_FOREACH(address
, link
->addresses
, i
) {
293 if (!address_is_ready(address
))
296 if (address
->scope
< scope
)
297 scope
= address
->scope
;
300 /* for operstate we also take foreign addresses into account */
301 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
302 if (!address_is_ready(address
))
305 if (address
->scope
< scope
)
306 scope
= address
->scope
;
309 if (scope
< RT_SCOPE_SITE
)
310 /* universally accessible addresses found */
311 operstate
= LINK_OPERSTATE_ROUTABLE
;
312 else if (scope
< RT_SCOPE_HOST
)
313 /* only link or site local addresses found */
314 operstate
= LINK_OPERSTATE_DEGRADED
;
316 /* no useful addresses found */
317 operstate
= LINK_OPERSTATE_CARRIER
;
318 } else if (link
->flags
& IFF_UP
)
319 operstate
= LINK_OPERSTATE_NO_CARRIER
;
321 operstate
= LINK_OPERSTATE_OFF
;
323 if (link
->operstate
!= operstate
) {
324 link
->operstate
= operstate
;
325 link_send_changed(link
, "OperationalState", NULL
);
330 #define FLAG_STRING(string, flag, old, new) \
331 (((old ^ new) & flag) \
332 ? ((old & flag) ? (" -" string) : (" +" string)) \
335 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
336 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
342 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
344 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
346 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
348 /* if we got a message without operstate, take it to mean
349 the state was unchanged */
350 operstate
= link
->kernel_operstate
;
352 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
355 if (link
->flags
!= flags
) {
356 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",
357 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
358 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
359 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
360 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
361 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
362 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
363 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
364 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
365 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
366 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
367 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
368 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
369 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
370 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
371 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
372 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
373 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
374 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
375 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
377 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
378 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
379 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
380 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
381 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
382 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
383 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
384 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
386 /* link flags are currently at most 18 bits, let's align to
388 if (unknown_flags_added
)
390 "Unknown link flags gained: %#.5x (ignoring)",
391 unknown_flags_added
);
393 if (unknown_flags_removed
)
395 "Unknown link flags lost: %#.5x (ignoring)",
396 unknown_flags_removed
);
400 link
->kernel_operstate
= operstate
;
402 link_update_operstate(link
);
407 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
408 _cleanup_(link_unrefp
) Link
*link
= NULL
;
410 const char *ifname
, *kind
= NULL
;
412 unsigned short iftype
;
418 /* check for link kind */
419 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
421 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
422 r
= sd_netlink_message_exit_container(message
);
427 r
= sd_netlink_message_get_type(message
, &type
);
430 else if (type
!= RTM_NEWLINK
)
433 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
436 else if (ifindex
<= 0)
439 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
443 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
447 link
= new0(Link
, 1);
452 link
->manager
= manager
;
453 link
->state
= LINK_STATE_PENDING
;
454 link
->rtnl_extended_attrs
= true;
455 link
->ifindex
= ifindex
;
456 link
->iftype
= iftype
;
457 link
->ifname
= strdup(ifname
);
462 link
->kind
= strdup(kind
);
467 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
469 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
471 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
474 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
477 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
480 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
484 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
488 r
= link_update_flags(link
, message
);
492 *ret
= TAKE_PTR(link
);
497 static void link_free(Link
*link
) {
505 while (!set_isempty(link
->addresses
))
506 address_free(set_first(link
->addresses
));
508 while (!set_isempty(link
->addresses_foreign
))
509 address_free(set_first(link
->addresses_foreign
));
511 link
->addresses
= set_free(link
->addresses
);
513 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
515 while ((address
= link
->pool_addresses
)) {
516 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
517 address_free(address
);
520 sd_dhcp_server_unref(link
->dhcp_server
);
521 sd_dhcp_client_unref(link
->dhcp_client
);
522 sd_dhcp_lease_unref(link
->dhcp_lease
);
524 link_lldp_emit_stop(link
);
526 free(link
->lease_file
);
528 sd_lldp_unref(link
->lldp
);
529 free(link
->lldp_file
);
533 sd_ipv4ll_unref(link
->ipv4ll
);
534 sd_dhcp6_client_unref(link
->dhcp6_client
);
535 sd_ndisc_unref(link
->ndisc
);
536 sd_radv_unref(link
->radv
);
539 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
545 (void)unlink(link
->state_file
);
546 free(link
->state_file
);
548 udev_device_unref(link
->udev_device
);
550 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
551 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
552 hashmap_free(link
->bound_to_links
);
554 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
555 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
556 hashmap_free(link
->bound_by_links
);
561 Link
*link_unref(Link
*link
) {
565 assert(link
->n_ref
> 0);
577 Link
*link_ref(Link
*link
) {
581 assert(link
->n_ref
> 0);
588 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
595 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
604 static void link_set_state(Link
*link
, LinkState state
) {
607 if (link
->state
== state
)
612 link_send_changed(link
, "AdministrativeState", NULL
);
615 static void link_enter_unmanaged(Link
*link
) {
618 log_link_debug(link
, "Unmanaged");
620 link_set_state(link
, LINK_STATE_UNMANAGED
);
625 static int link_stop_clients(Link
*link
) {
629 assert(link
->manager
);
630 assert(link
->manager
->event
);
632 if (link
->dhcp_client
) {
633 k
= sd_dhcp_client_stop(link
->dhcp_client
);
635 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
639 k
= sd_ipv4ll_stop(link
->ipv4ll
);
641 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
644 if (link
->dhcp6_client
) {
645 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
647 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
651 k
= sd_ndisc_stop(link
->ndisc
);
653 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
657 k
= sd_radv_stop(link
->radv
);
659 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
662 link_lldp_emit_stop(link
);
666 void link_enter_failed(Link
*link
) {
669 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
672 log_link_warning(link
, "Failed");
674 link_set_state(link
, LINK_STATE_FAILED
);
676 link_stop_clients(link
);
681 static Address
* link_find_dhcp_server_address(Link
*link
) {
685 assert(link
->network
);
687 /* The first statically configured address if there is any */
688 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
690 if (address
->family
!= AF_INET
)
693 if (in_addr_is_null(address
->family
, &address
->in_addr
))
699 /* If that didn't work, find a suitable address we got from the pool */
700 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
701 if (address
->family
!= AF_INET
)
710 static void link_enter_configured(Link
*link
) {
712 assert(link
->network
);
714 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
717 log_link_info(link
, "Configured");
719 link_set_state(link
, LINK_STATE_CONFIGURED
);
724 void link_check_ready(Link
*link
) {
730 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
736 if (!link
->static_routes_configured
)
739 if (!link
->routing_policy_rules_configured
)
742 if (link_ipv4ll_enabled(link
))
743 if (!link
->ipv4ll_address
||
747 if (!link
->network
->bridge
) {
749 if (link_ipv6ll_enabled(link
))
750 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
753 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
754 !link
->dhcp4_configured
) ||
755 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
756 !link
->dhcp6_configured
) ||
757 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
758 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
761 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
765 SET_FOREACH(a
, link
->addresses
, i
)
766 if (!address_is_ready(a
))
769 if (link
->state
!= LINK_STATE_CONFIGURED
)
770 link_enter_configured(link
);
775 static int link_set_routing_policy_rule(Link
*link
) {
776 RoutingPolicyRule
*rule
, *rrule
= NULL
;
780 assert(link
->network
);
782 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
783 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
784 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
786 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
790 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
792 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
793 link_enter_failed(link
);
797 link
->routing_policy_rule_messages
++;
800 routing_policy_rule_purge(link
->manager
, link
);
801 if (link
->routing_policy_rule_messages
== 0) {
802 link
->routing_policy_rules_configured
= true;
803 link_check_ready(link
);
805 log_link_debug(link
, "Setting routing policy rules");
810 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
811 _cleanup_(link_unrefp
) Link
*link
= userdata
;
814 assert(link
->route_messages
> 0);
815 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
816 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
819 link
->route_messages
--;
821 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
824 r
= sd_netlink_message_get_errno(m
);
825 if (r
< 0 && r
!= -EEXIST
)
826 log_link_warning_errno(link
, r
, "Could not set route: %m");
828 if (link
->route_messages
== 0) {
829 log_link_debug(link
, "Routes set");
830 link
->static_routes_configured
= true;
831 link_check_ready(link
);
837 static int link_enter_set_routes(Link
*link
) {
842 assert(link
->network
);
843 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
845 (void) link_set_routing_policy_rule(link
);
847 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
849 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
850 r
= route_configure(rt
, link
, route_handler
);
852 log_link_warning_errno(link
, r
, "Could not set routes: %m");
853 link_enter_failed(link
);
857 link
->route_messages
++;
860 if (link
->route_messages
== 0) {
861 link
->static_routes_configured
= true;
862 link_check_ready(link
);
864 log_link_debug(link
, "Setting routes");
869 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
870 _cleanup_(link_unrefp
) Link
*link
= userdata
;
875 assert(link
->ifname
);
877 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
880 r
= sd_netlink_message_get_errno(m
);
881 if (r
< 0 && r
!= -ESRCH
)
882 log_link_warning_errno(link
, r
, "Could not drop route: %m");
887 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
888 _cleanup_(link_unrefp
) Link
*link
= userdata
;
894 assert(link
->ifname
);
895 assert(link
->address_messages
> 0);
896 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
897 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
899 link
->address_messages
--;
901 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
904 r
= sd_netlink_message_get_errno(m
);
905 if (r
< 0 && r
!= -EEXIST
)
906 log_link_warning_errno(link
, r
, "could not set address: %m");
908 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
910 if (link
->address_messages
== 0) {
911 log_link_debug(link
, "Addresses set");
912 link_enter_set_routes(link
);
918 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
919 _cleanup_(link_unrefp
) Link
*link
= userdata
;
925 assert(link
->ifname
);
926 assert(link
->address_label_messages
> 0);
928 link
->address_label_messages
--;
930 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
933 r
= sd_netlink_message_get_errno(m
);
934 if (r
< 0 && r
!= -EEXIST
)
935 log_link_warning_errno(link
, r
, "could not set address label: %m");
937 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
939 if (link
->address_label_messages
== 0)
940 log_link_debug(link
, "Addresses label set");
945 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
946 _cleanup_free_
struct in_addr
*addresses
= NULL
;
947 size_t n_addresses
= 0, n_allocated
= 0;
950 log_debug("Copying DNS server information from %s", link
->ifname
);
955 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
958 /* Only look for IPv4 addresses */
959 if (link
->network
->dns
[i
].family
!= AF_INET
)
962 ia
= link
->network
->dns
[i
].address
.in
;
964 /* Never propagate obviously borked data */
965 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
968 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
971 addresses
[n_addresses
++] = ia
;
974 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
975 const struct in_addr
*da
= NULL
;
978 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
981 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
984 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
989 if (n_addresses
<= 0)
992 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
995 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
996 _cleanup_free_
struct in_addr
*addresses
= NULL
;
997 size_t n_addresses
= 0, n_allocated
= 0;
1003 log_debug("Copying NTP server information from %s", link
->ifname
);
1005 STRV_FOREACH(a
, link
->network
->ntp
) {
1008 /* Only look for IPv4 addresses */
1009 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1012 /* Never propagate obviously borked data */
1013 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1016 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1019 addresses
[n_addresses
++] = ia
;
1022 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1023 const struct in_addr
*da
= NULL
;
1026 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1029 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1032 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1037 if (n_addresses
<= 0)
1040 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1043 static int link_set_bridge_fdb(Link
*link
) {
1044 FdbEntry
*fdb_entry
;
1047 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1048 r
= fdb_entry_configure(link
, fdb_entry
);
1050 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1056 static int link_enter_set_addresses(Link
*link
) {
1057 AddressLabel
*label
;
1062 assert(link
->network
);
1063 assert(link
->state
!= _LINK_STATE_INVALID
);
1065 r
= link_set_bridge_fdb(link
);
1069 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1071 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1072 r
= address_configure(ad
, link
, address_handler
, false);
1074 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1075 link_enter_failed(link
);
1079 link
->address_messages
++;
1082 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1083 r
= address_label_configure(label
, link
, address_label_handler
, false);
1085 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1086 link_enter_failed(link
);
1090 link
->address_label_messages
++;
1093 /* now that we can figure out a default address for the dhcp server,
1095 if (link_dhcp4_server_enabled(link
)) {
1097 Link
*uplink
= NULL
;
1098 bool acquired_uplink
= false;
1100 address
= link_find_dhcp_server_address(link
);
1102 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1103 link_enter_failed(link
);
1107 /* use the server address' subnet as the pool */
1108 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1109 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1114 r = sd_dhcp_server_set_router(link->dhcp_server,
1115 &main_address->in_addr.in);
1120 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1121 r
= sd_dhcp_server_set_max_lease_time(
1123 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1128 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1129 r
= sd_dhcp_server_set_default_lease_time(
1131 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1136 if (link
->network
->dhcp_server_emit_dns
) {
1138 if (link
->network
->n_dhcp_server_dns
> 0)
1139 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1141 uplink
= manager_find_uplink(link
->manager
, link
);
1142 acquired_uplink
= true;
1145 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1148 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1151 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1154 if (link
->network
->dhcp_server_emit_ntp
) {
1156 if (link
->network
->n_dhcp_server_ntp
> 0)
1157 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1159 if (!acquired_uplink
)
1160 uplink
= manager_find_uplink(link
->manager
, link
);
1163 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1166 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1170 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1173 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1175 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1179 if (link
->network
->dhcp_server_emit_timezone
) {
1180 _cleanup_free_
char *buffer
= NULL
;
1181 const char *tz
= NULL
;
1183 if (link
->network
->dhcp_server_timezone
)
1184 tz
= link
->network
->dhcp_server_timezone
;
1186 r
= get_timezone(&buffer
);
1188 log_warning_errno(r
, "Failed to determine timezone: %m");
1194 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1200 r
= sd_dhcp_server_start(link
->dhcp_server
);
1202 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1204 link_enter_failed(link
);
1209 log_link_debug(link
, "Offering DHCPv4 leases");
1212 if (link
->address_messages
== 0)
1213 link_enter_set_routes(link
);
1215 log_link_debug(link
, "Setting addresses");
1220 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1221 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1226 assert(link
->ifname
);
1228 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1231 r
= sd_netlink_message_get_errno(m
);
1232 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1233 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1238 static int link_set_bridge_vlan(Link
*link
) {
1241 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1243 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1248 static int link_set_proxy_arp(Link
*link
) {
1249 const char *p
= NULL
;
1252 if (!link_proxy_arp_enabled(link
))
1255 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1257 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1259 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1264 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1265 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1268 log_link_debug(link
, "Set link");
1270 r
= sd_netlink_message_get_errno(m
);
1271 if (r
< 0 && r
!= -EEXIST
) {
1272 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1273 link_enter_failed(link
);
1280 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1281 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1286 assert(link
->ifname
);
1288 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1291 r
= sd_netlink_message_get_errno(m
);
1293 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1298 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1299 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1303 assert(link
->manager
);
1304 assert(link
->manager
->rtnl
);
1306 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1308 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1310 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1312 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1314 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1316 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1318 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1320 link
->setting_mtu
= true;
1327 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1328 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1333 assert(link
->ifname
);
1335 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1338 r
= sd_netlink_message_get_errno(m
);
1340 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1345 static int link_set_flags(Link
*link
) {
1346 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1347 unsigned ifi_change
= 0;
1348 unsigned ifi_flags
= 0;
1352 assert(link
->manager
);
1353 assert(link
->manager
->rtnl
);
1355 if (link
->flags
& IFF_LOOPBACK
)
1361 if (link
->network
->arp
< 0 && link
->network
->multicast
< 0 && link
->network
->allmulticast
< 0)
1364 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1366 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1368 if (link
->network
->arp
>= 0) {
1369 ifi_change
|= IFF_NOARP
;
1370 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
1373 if (link
->network
->multicast
>= 0) {
1374 ifi_change
|= IFF_MULTICAST
;
1375 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
1378 if (link
->network
->allmulticast
>= 0) {
1379 ifi_change
|= IFF_ALLMULTI
;
1380 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
1383 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1385 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1387 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1389 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1396 static int link_set_bridge(Link
*link
) {
1397 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1401 assert(link
->network
);
1403 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1405 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1407 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1409 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1411 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1413 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1415 if (link
->network
->use_bpdu
>= 0) {
1416 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
1418 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1421 if (link
->network
->hairpin
>= 0) {
1422 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1424 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1427 if (link
->network
->fast_leave
>= 0) {
1428 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1430 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1433 if (link
->network
->allow_port_to_be_root
>= 0) {
1434 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
1436 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1440 if (link
->network
->unicast_flood
>= 0) {
1441 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1443 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1447 if (link
->network
->cost
!= 0) {
1448 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1450 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1452 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1453 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1455 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1458 r
= sd_netlink_message_close_container(req
);
1460 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1462 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1464 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1471 static int link_bond_set(Link
*link
) {
1472 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1476 assert(link
->network
);
1478 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1480 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1482 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1484 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1486 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1488 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1490 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1492 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1494 if (link
->network
->active_slave
) {
1495 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1497 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1500 if (link
->network
->primary_slave
) {
1501 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1503 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1506 r
= sd_netlink_message_close_container(req
);
1508 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1510 r
= sd_netlink_message_close_container(req
);
1512 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1514 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1516 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1523 static int link_lldp_save(Link
*link
) {
1524 _cleanup_free_
char *temp_path
= NULL
;
1525 _cleanup_fclose_
FILE *f
= NULL
;
1526 sd_lldp_neighbor
**l
= NULL
;
1530 assert(link
->lldp_file
);
1533 (void) unlink(link
->lldp_file
);
1537 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1541 (void) unlink(link
->lldp_file
);
1547 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1551 fchmod(fileno(f
), 0644);
1553 for (i
= 0; i
< n
; i
++) {
1558 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1563 (void) fwrite(&u
, 1, sizeof(u
), f
);
1564 (void) fwrite(p
, 1, sz
, f
);
1567 r
= fflush_and_check(f
);
1571 if (rename(temp_path
, link
->lldp_file
) < 0) {
1578 (void) unlink(link
->lldp_file
);
1580 (void) unlink(temp_path
);
1582 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1586 for (i
= 0; i
< n
; i
++)
1587 sd_lldp_neighbor_unref(l
[i
]);
1594 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1595 Link
*link
= userdata
;
1600 (void) link_lldp_save(link
);
1602 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1603 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1605 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1607 r
= link_lldp_emit_start(link
);
1609 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1613 static int link_acquire_ipv6_conf(Link
*link
) {
1618 if (link_dhcp6_enabled(link
)) {
1619 assert(link
->dhcp6_client
);
1620 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1622 /* start DHCPv6 client in stateless mode */
1623 r
= dhcp6_request_address(link
, true);
1624 if (r
< 0 && r
!= -EBUSY
)
1625 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1627 log_link_debug(link
, "Acquiring DHCPv6 lease");
1630 if (link_ipv6_accept_ra_enabled(link
)) {
1631 assert(link
->ndisc
);
1633 log_link_debug(link
, "Discovering IPv6 routers");
1635 r
= sd_ndisc_start(link
->ndisc
);
1636 if (r
< 0 && r
!= -EBUSY
)
1637 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1640 if (link_radv_enabled(link
)) {
1642 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1644 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1646 r
= sd_radv_start(link
->radv
);
1647 if (r
< 0 && r
!= -EBUSY
)
1648 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1654 static int link_acquire_ipv4_conf(Link
*link
) {
1658 assert(link
->network
);
1659 assert(link
->manager
);
1660 assert(link
->manager
->event
);
1662 if (link_ipv4ll_enabled(link
)) {
1663 assert(link
->ipv4ll
);
1665 log_link_debug(link
, "Acquiring IPv4 link-local address");
1667 r
= sd_ipv4ll_start(link
->ipv4ll
);
1669 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1672 if (link_dhcp4_enabled(link
)) {
1673 assert(link
->dhcp_client
);
1675 log_link_debug(link
, "Acquiring DHCPv4 lease");
1677 r
= sd_dhcp_client_start(link
->dhcp_client
);
1679 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1685 static int link_acquire_conf(Link
*link
) {
1690 if (link
->setting_mtu
) {
1691 link
->setting_mtu
= false;
1695 r
= link_acquire_ipv4_conf(link
);
1699 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1700 r
= link_acquire_ipv6_conf(link
);
1705 if (link_lldp_emit_enabled(link
)) {
1706 r
= link_lldp_emit_start(link
);
1708 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1714 bool link_has_carrier(Link
*link
) {
1715 /* see Documentation/networking/operstates.txt in the kernel sources */
1717 if (link
->kernel_operstate
== IF_OPER_UP
)
1720 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1721 /* operstate may not be implemented, so fall back to flags */
1722 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1728 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1729 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1734 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1737 r
= sd_netlink_message_get_errno(m
);
1739 /* we warn but don't fail the link, as it may be
1741 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1746 int link_up(Link
*link
) {
1747 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1748 uint8_t ipv6ll_mode
;
1752 assert(link
->network
);
1753 assert(link
->manager
);
1754 assert(link
->manager
->rtnl
);
1756 log_link_debug(link
, "Bringing link up");
1758 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1760 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1762 /* set it free if not enslaved with networkd */
1763 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1764 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1766 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1769 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1771 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1773 if (link
->network
->mac
) {
1774 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1776 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1779 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1780 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1781 (void) link_enable_ipv6(link
);
1783 if (link
->network
->mtu
!= 0) {
1784 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1785 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1786 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1788 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1789 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1791 link
->network
->mtu
= IPV6_MIN_MTU
;
1794 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1796 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1799 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1801 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1803 if (link_ipv6_enabled(link
)) {
1804 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1805 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1807 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1809 if (!link_ipv6ll_enabled(link
))
1810 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1812 const char *p
= NULL
;
1813 _cleanup_free_
char *stable_secret
= NULL
;
1815 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1816 r
= read_one_line_file(p
, &stable_secret
);
1819 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1821 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1823 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1825 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1827 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1828 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1830 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1833 r
= sd_netlink_message_close_container(req
);
1835 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1838 r
= sd_netlink_message_close_container(req
);
1840 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1842 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1844 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1851 static int link_up_can(Link
*link
) {
1852 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1857 log_link_debug(link
, "Bringing CAN link up");
1859 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1861 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1863 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1865 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1867 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1869 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1876 static int link_set_can(Link
*link
) {
1877 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1881 assert(link
->network
);
1882 assert(link
->manager
);
1883 assert(link
->manager
->rtnl
);
1885 log_link_debug(link
, "link_set_can");
1887 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
1889 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
1891 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
1893 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1895 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
1897 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
1899 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
1901 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1903 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
1904 struct can_bittiming bt
= {
1905 .bitrate
= link
->network
->can_bitrate
,
1906 .sample_point
= link
->network
->can_sample_point
,
1909 if (link
->network
->can_bitrate
> UINT32_MAX
) {
1910 log_link_error(link
, "bitrate (%zu) too big.", link
->network
->can_bitrate
);
1914 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
1915 if (link
->network
->can_sample_point
> 0)
1916 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
1918 log_link_debug(link
, "Using default sample point");
1920 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
1922 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
1925 if (link
->network
->can_restart_us
> 0) {
1926 char time_string
[FORMAT_TIMESPAN_MAX
];
1927 uint64_t restart_ms
;
1929 if (link
->network
->can_restart_us
== USEC_INFINITY
)
1932 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
1934 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
1936 if (restart_ms
> UINT32_MAX
) {
1937 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
1941 log_link_debug(link
, "Setting restart = %s", time_string
);
1943 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
1945 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
1948 r
= sd_netlink_message_close_container(m
);
1950 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1952 r
= sd_netlink_message_close_container(m
);
1954 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
1956 r
= sd_netlink_call_async(link
->manager
->rtnl
, m
, link_set_handler
, link
, 0, NULL
);
1958 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1962 if (!(link
->flags
& IFF_UP
)) {
1963 r
= link_up_can(link
);
1965 link_enter_failed(link
);
1970 log_link_debug(link
, "link_set_can done");
1975 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1976 _cleanup_(link_unrefp
) Link
*link
= userdata
;
1981 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1984 r
= sd_netlink_message_get_errno(m
);
1986 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1988 if (streq_ptr(link
->kind
, "can")) {
1996 int link_down(Link
*link
) {
1997 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2001 assert(link
->manager
);
2002 assert(link
->manager
->rtnl
);
2004 log_link_debug(link
, "Bringing link down");
2006 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
2007 RTM_SETLINK
, link
->ifindex
);
2009 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
2011 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
2013 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
2015 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
2017 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
2024 static int link_handle_bound_to_list(Link
*link
) {
2028 bool required_up
= false;
2029 bool link_is_up
= false;
2033 if (hashmap_isempty(link
->bound_to_links
))
2036 if (link
->flags
& IFF_UP
)
2039 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
2040 if (link_has_carrier(l
)) {
2045 if (!required_up
&& link_is_up
) {
2046 r
= link_down(link
);
2049 } else if (required_up
&& !link_is_up
) {
2058 static int link_handle_bound_by_list(Link
*link
) {
2065 if (hashmap_isempty(link
->bound_by_links
))
2068 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
2069 r
= link_handle_bound_to_list(l
);
2077 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
2083 if (link
== carrier
)
2086 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
2089 r
= hashmap_ensure_allocated(h
, NULL
);
2093 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
2100 static int link_new_bound_by_list(Link
*link
) {
2105 bool list_updated
= false;
2108 assert(link
->manager
);
2112 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2113 if (!carrier
->network
)
2116 if (strv_isempty(carrier
->network
->bind_carrier
))
2119 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2120 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2124 list_updated
= true;
2131 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2132 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2136 link_dirty(carrier
);
2142 static int link_new_bound_to_list(Link
*link
) {
2147 bool list_updated
= false;
2150 assert(link
->manager
);
2155 if (strv_isempty(link
->network
->bind_carrier
))
2160 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2161 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2162 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2166 list_updated
= true;
2173 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2174 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2178 link_dirty(carrier
);
2184 static int link_new_carrier_maps(Link
*link
) {
2187 r
= link_new_bound_by_list(link
);
2191 r
= link_handle_bound_by_list(link
);
2195 r
= link_new_bound_to_list(link
);
2199 r
= link_handle_bound_to_list(link
);
2206 static void link_free_bound_to_list(Link
*link
) {
2210 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2211 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2213 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2214 link_dirty(bound_to
);
2220 static void link_free_bound_by_list(Link
*link
) {
2224 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2225 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2227 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2228 link_dirty(bound_by
);
2229 link_handle_bound_to_list(bound_by
);
2236 static void link_free_carrier_maps(Link
*link
) {
2237 bool list_updated
= false;
2241 if (!hashmap_isempty(link
->bound_to_links
)) {
2242 link_free_bound_to_list(link
);
2243 list_updated
= true;
2246 if (!hashmap_isempty(link
->bound_by_links
)) {
2247 link_free_bound_by_list(link
);
2248 list_updated
= true;
2257 void link_drop(Link
*link
) {
2258 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2261 link_set_state(link
, LINK_STATE_LINGER
);
2263 link_free_carrier_maps(link
);
2265 log_link_debug(link
, "Link removed");
2267 (void)unlink(link
->state_file
);
2273 static int link_joined(Link
*link
) {
2277 assert(link
->network
);
2279 if (!hashmap_isempty(link
->bound_to_links
)) {
2280 r
= link_handle_bound_to_list(link
);
2283 } else if (!(link
->flags
& IFF_UP
)) {
2286 link_enter_failed(link
);
2291 if (link
->network
->bridge
) {
2292 r
= link_set_bridge(link
);
2294 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2297 if (link
->network
->bond
) {
2298 r
= link_bond_set(link
);
2300 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2303 if (link
->network
->use_br_vlan
&&
2304 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2305 r
= link_set_bridge_vlan(link
);
2307 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2310 /* Skip setting up addresses until it gets carrier,
2311 or it would try to set addresses twice,
2312 which is bad for non-idempotent steps. */
2313 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2316 return link_enter_set_addresses(link
);
2319 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2320 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2324 assert(link
->network
);
2328 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2331 r
= sd_netlink_message_get_errno(m
);
2332 if (r
< 0 && r
!= -EEXIST
) {
2333 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2334 link_enter_failed(link
);
2337 log_link_debug(link
, "Joined netdev");
2339 if (link
->enslaving
<= 0)
2345 static int link_enter_join_netdev(Link
*link
) {
2351 assert(link
->network
);
2352 assert(link
->state
== LINK_STATE_PENDING
);
2354 link_set_state(link
, LINK_STATE_ENSLAVING
);
2358 if (!link
->network
->bridge
&&
2359 !link
->network
->bond
&&
2360 !link
->network
->vrf
&&
2361 hashmap_isempty(link
->network
->stacked_netdevs
))
2362 return link_joined(link
);
2364 if (link
->network
->bond
) {
2365 log_struct(LOG_DEBUG
,
2366 LOG_LINK_INTERFACE(link
),
2367 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2368 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
));
2370 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2372 log_struct_errno(LOG_WARNING
, r
,
2373 LOG_LINK_INTERFACE(link
),
2374 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2375 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
));
2376 link_enter_failed(link
);
2383 if (link
->network
->bridge
) {
2384 log_struct(LOG_DEBUG
,
2385 LOG_LINK_INTERFACE(link
),
2386 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2387 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
));
2389 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2391 log_struct_errno(LOG_WARNING
, r
,
2392 LOG_LINK_INTERFACE(link
),
2393 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2394 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
));
2395 link_enter_failed(link
);
2402 if (link
->network
->vrf
) {
2403 log_struct(LOG_DEBUG
,
2404 LOG_LINK_INTERFACE(link
),
2405 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2406 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
));
2408 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2410 log_struct_errno(LOG_WARNING
, r
,
2411 LOG_LINK_INTERFACE(link
),
2412 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2413 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
));
2414 link_enter_failed(link
);
2421 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2423 if (netdev
->ifindex
> 0) {
2428 log_struct(LOG_DEBUG
,
2429 LOG_LINK_INTERFACE(link
),
2430 LOG_NETDEV_INTERFACE(netdev
),
2431 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
));
2433 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2435 log_struct_errno(LOG_WARNING
, r
,
2436 LOG_LINK_INTERFACE(link
),
2437 LOG_NETDEV_INTERFACE(netdev
),
2438 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
));
2439 link_enter_failed(link
);
2449 static int link_set_ipv4_forward(Link
*link
) {
2452 if (!link_ipv4_forward_enabled(link
))
2455 /* We propagate the forwarding flag from one interface to the
2456 * global setting one way. This means: as long as at least one
2457 * interface was configured at any time that had IP forwarding
2458 * enabled the setting will stay on for good. We do this
2459 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2460 * somewhat in sync (see below). */
2462 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2464 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2469 static int link_set_ipv6_forward(Link
*link
) {
2472 if (!link_ipv6_forward_enabled(link
))
2475 /* On Linux, the IPv6 stack does not know a per-interface
2476 * packet forwarding setting: either packet forwarding is on
2477 * for all, or off for all. We hence don't bother with a
2478 * per-interface setting, but simply propagate the interface
2479 * flag, if it is set, to the global flag, one-way. Note that
2480 * while IPv4 would allow a per-interface flag, we expose the
2481 * same behaviour there and also propagate the setting from
2482 * one to all, to keep things simple (see above). */
2484 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2486 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2491 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2492 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2493 IPv6PrivacyExtensions s
;
2494 const char *p
= NULL
;
2497 s
= link_ipv6_privacy_extensions(link
);
2501 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2502 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2504 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2506 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2511 static int link_set_ipv6_accept_ra(Link
*link
) {
2512 const char *p
= NULL
;
2515 /* Make this a NOP if IPv6 is not available */
2516 if (!socket_ipv6_is_supported())
2519 if (link
->flags
& IFF_LOOPBACK
)
2525 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2527 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2528 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2530 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2535 static int link_set_ipv6_dad_transmits(Link
*link
) {
2536 char buf
[DECIMAL_STR_MAX(int) + 1];
2537 const char *p
= NULL
;
2540 /* Make this a NOP if IPv6 is not available */
2541 if (!socket_ipv6_is_supported())
2544 if (link
->flags
& IFF_LOOPBACK
)
2550 if (link
->network
->ipv6_dad_transmits
< 0)
2553 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2554 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2556 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2558 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2563 static int link_set_ipv6_hop_limit(Link
*link
) {
2564 char buf
[DECIMAL_STR_MAX(int) + 1];
2565 const char *p
= NULL
;
2568 /* Make this a NOP if IPv6 is not available */
2569 if (!socket_ipv6_is_supported())
2572 if (link
->flags
& IFF_LOOPBACK
)
2578 if (link
->network
->ipv6_hop_limit
< 0)
2581 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2582 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2584 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2586 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2591 static int link_set_ipv6_mtu(Link
*link
) {
2592 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2593 const char *p
= NULL
;
2596 /* Make this a NOP if IPv6 is not available */
2597 if (!socket_ipv6_is_supported())
2600 if (link
->flags
& IFF_LOOPBACK
)
2603 if (link
->network
->ipv6_mtu
== 0)
2606 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/mtu");
2608 xsprintf(buf
, "%" PRIu32
, link
->network
->ipv6_mtu
);
2610 r
= write_string_file(p
, buf
, 0);
2612 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU for interface: %m");
2617 static int link_drop_foreign_config(Link
*link
) {
2623 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2624 /* we consider IPv6LL addresses to be managed by the kernel */
2625 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2628 r
= address_remove(address
, link
, link_address_remove_handler
);
2633 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2634 /* do not touch routes managed by the kernel */
2635 if (route
->protocol
== RTPROT_KERNEL
)
2638 r
= route_remove(route
, link
, link_route_remove_handler
);
2646 static int link_drop_config(Link
*link
) {
2647 Address
*address
, *pool_address
;
2652 SET_FOREACH(address
, link
->addresses
, i
) {
2653 /* we consider IPv6LL addresses to be managed by the kernel */
2654 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2657 r
= address_remove(address
, link
, link_address_remove_handler
);
2661 /* If this address came from an address pool, clean up the pool */
2662 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2663 if (address_equal(address
, pool_address
)) {
2664 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2665 address_free(pool_address
);
2671 SET_FOREACH(route
, link
->routes
, i
) {
2672 /* do not touch routes managed by the kernel */
2673 if (route
->protocol
== RTPROT_KERNEL
)
2676 r
= route_remove(route
, link
, link_route_remove_handler
);
2686 static int link_update_lldp(Link
*link
) {
2694 if (link
->flags
& IFF_UP
) {
2695 r
= sd_lldp_start(link
->lldp
);
2697 log_link_debug(link
, "Started LLDP.");
2699 r
= sd_lldp_stop(link
->lldp
);
2701 log_link_debug(link
, "Stopped LLDP.");
2707 static int link_configure_can(Link
*link
) {
2710 if (streq_ptr(link
->kind
, "can")) {
2711 /* The CAN interface must be down to configure bitrate, etc... */
2712 if ((link
->flags
& IFF_UP
)) {
2713 r
= link_down(link
);
2715 link_enter_failed(link
);
2722 return link_set_can(link
);
2725 if (!(link
->flags
& IFF_UP
)) {
2726 r
= link_up_can(link
);
2728 link_enter_failed(link
);
2736 static int link_configure(Link
*link
) {
2740 assert(link
->network
);
2741 assert(link
->state
== LINK_STATE_PENDING
);
2743 if (STRPTR_IN_SET(link
->kind
, "can", "vcan"))
2744 return link_configure_can(link
);
2746 /* Drop foreign config, but ignore loopback or critical devices.
2747 * We do not want to remove loopback address or addresses used for root NFS. */
2748 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2749 r
= link_drop_foreign_config(link
);
2754 r
= link_set_proxy_arp(link
);
2758 r
= ipv6_proxy_ndp_addresses_configure(link
);
2762 r
= link_set_ipv4_forward(link
);
2766 r
= link_set_ipv6_forward(link
);
2770 r
= link_set_ipv6_privacy_extensions(link
);
2774 r
= link_set_ipv6_accept_ra(link
);
2778 r
= link_set_ipv6_dad_transmits(link
);
2782 r
= link_set_ipv6_hop_limit(link
);
2786 r
= link_set_flags(link
);
2790 r
= link_set_ipv6_mtu(link
);
2794 if (link_ipv4ll_enabled(link
)) {
2795 r
= ipv4ll_configure(link
);
2800 if (link_dhcp4_enabled(link
)) {
2801 r
= dhcp4_set_promote_secondaries(link
);
2805 r
= dhcp4_configure(link
);
2810 if (link_dhcp4_server_enabled(link
)) {
2811 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2815 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2820 if (link_dhcp6_enabled(link
) ||
2821 link_ipv6_accept_ra_enabled(link
)) {
2822 r
= dhcp6_configure(link
);
2827 if (link_ipv6_accept_ra_enabled(link
)) {
2828 r
= ndisc_configure(link
);
2833 if (link_radv_enabled(link
)) {
2834 r
= radv_configure(link
);
2839 if (link_lldp_rx_enabled(link
)) {
2840 r
= sd_lldp_new(&link
->lldp
);
2844 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2848 r
= sd_lldp_match_capabilities(link
->lldp
,
2849 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2850 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2851 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2855 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2859 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2863 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2867 r
= link_update_lldp(link
);
2872 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2873 r
= link_acquire_conf(link
);
2878 return link_enter_join_netdev(link
);
2881 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2883 _cleanup_(link_unrefp
) Link
*link
= userdata
;
2888 assert(link
->ifname
);
2889 assert(link
->manager
);
2891 if (link
->state
!= LINK_STATE_PENDING
)
2894 log_link_debug(link
, "Link state is up-to-date");
2896 r
= link_new_bound_by_list(link
);
2900 r
= link_handle_bound_by_list(link
);
2904 if (!link
->network
) {
2905 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2906 &link
->mac
, &network
);
2908 link_enter_unmanaged(link
);
2910 } else if (r
== 0 && network
->unmanaged
) {
2911 link_enter_unmanaged(link
);
2916 if (link
->flags
& IFF_LOOPBACK
) {
2917 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2918 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2920 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2921 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2923 if (network
->dhcp_server
)
2924 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2927 r
= network_apply(network
, link
);
2932 r
= link_new_bound_to_list(link
);
2936 r
= link_configure(link
);
2943 int link_initialized(Link
*link
, struct udev_device
*device
) {
2944 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2948 assert(link
->manager
);
2949 assert(link
->manager
->rtnl
);
2952 if (link
->state
!= LINK_STATE_PENDING
)
2955 if (link
->udev_device
)
2958 log_link_debug(link
, "udev initialized link");
2960 link
->udev_device
= udev_device_ref(device
);
2962 /* udev has initialized the link, but we don't know if we have yet
2963 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2964 * when it returns we know that the pending NEWLINKs have already been
2965 * processed and that we are up-to-date */
2967 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2972 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2973 link_initialized_and_synced
, link
, 0, NULL
);
2982 static int link_load(Link
*link
) {
2983 _cleanup_free_
char *network_file
= NULL
,
2986 *dhcp4_address
= NULL
,
2987 *ipv4ll_address
= NULL
;
2988 union in_addr_union address
;
2989 union in_addr_union route_dst
;
2995 r
= parse_env_file(NULL
, link
->state_file
, NEWLINE
,
2996 "NETWORK_FILE", &network_file
,
2997 "ADDRESSES", &addresses
,
2999 "DHCP4_ADDRESS", &dhcp4_address
,
3000 "IPV4LL_ADDRESS", &ipv4ll_address
,
3002 if (r
< 0 && r
!= -ENOENT
)
3003 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
3010 suffix
= strrchr(network_file
, '.');
3012 log_link_debug(link
, "Failed to get network name from %s", network_file
);
3013 goto network_file_fail
;
3017 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
3019 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
3020 goto network_file_fail
;
3023 r
= network_apply(network
, link
);
3025 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
3034 _cleanup_free_
char *address_str
= NULL
;
3035 char *prefixlen_str
;
3037 unsigned char prefixlen
;
3039 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
3041 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
3047 prefixlen_str
= strchr(address_str
, '/');
3048 if (!prefixlen_str
) {
3049 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
3053 *prefixlen_str
++ = '\0';
3055 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
3057 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
3061 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
3063 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
3067 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
3069 return log_link_error_errno(link
, r
, "Failed to add address: %m");
3078 _cleanup_free_
char *route_str
= NULL
;
3079 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
3081 char *prefixlen_str
;
3083 unsigned char prefixlen
, tos
, table
;
3086 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
3088 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
3094 prefixlen_str
= strchr(route_str
, '/');
3095 if (!prefixlen_str
) {
3096 log_link_debug(link
, "Failed to parse route %s", route_str
);
3100 *prefixlen_str
++ = '\0';
3102 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
3104 log_link_debug(link
,
3105 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
3110 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
3112 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
3116 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
3118 return log_link_error_errno(link
, r
, "Failed to add route: %m");
3120 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
3121 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
3122 0, route_expire_handler
, route
);
3124 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
3127 route
->lifetime
= lifetime
;
3128 sd_event_source_unref(route
->expire
);
3129 route
->expire
= TAKE_PTR(expire
);
3133 if (dhcp4_address
) {
3134 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
3136 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
3137 goto dhcp4_address_fail
;
3140 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
? link
->network
->dhcp_anonymize
: 0);
3142 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
3144 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
3146 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
3151 if (ipv4ll_address
) {
3152 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3154 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3155 goto ipv4ll_address_fail
;
3158 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3160 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3162 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3164 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3167 ipv4ll_address_fail
:
3172 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3174 _cleanup_(udev_device_unrefp
) struct udev_device
*device
= NULL
;
3175 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3183 r
= link_new(m
, message
, ret
);
3189 log_link_debug(link
, "Link %d added", link
->ifindex
);
3191 r
= link_load(link
);
3195 if (detect_container() <= 0) {
3196 /* not in a container, udev will be around */
3197 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3198 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3200 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3204 if (udev_device_get_is_initialized(device
) <= 0) {
3206 log_link_debug(link
, "link pending udev initialization...");
3210 r
= link_initialized(link
, device
);
3214 /* we are calling a callback directly, so must take a ref */
3217 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3224 link_enter_failed(link
);
3228 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3233 log_link_info(link
, "Gained IPv6LL");
3235 link
->ipv6ll_address
= *address
;
3236 link_check_ready(link
);
3238 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3239 r
= link_acquire_ipv6_conf(link
);
3241 link_enter_failed(link
);
3249 static int link_carrier_gained(Link
*link
) {
3254 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3255 r
= link_acquire_conf(link
);
3257 link_enter_failed(link
);
3261 r
= link_enter_set_addresses(link
);
3266 r
= link_handle_bound_by_list(link
);
3273 static int link_carrier_lost(Link
*link
) {
3278 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3279 setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3280 configuration and stop the clients as well. */
3281 if (link
->setting_mtu
)
3284 r
= link_stop_clients(link
);
3286 link_enter_failed(link
);
3290 if (link_dhcp4_server_enabled(link
))
3291 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3293 r
= link_drop_config(link
);
3297 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3298 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3299 r
= link_drop_foreign_config(link
);
3304 r
= link_handle_bound_by_list(link
);
3311 int link_carrier_reset(Link
*link
) {
3316 if (link_has_carrier(link
)) {
3317 r
= link_carrier_lost(link
);
3321 r
= link_carrier_gained(link
);
3325 log_link_info(link
, "Reset carrier");
3331 int link_update(Link
*link
, sd_netlink_message
*m
) {
3332 struct ether_addr mac
;
3335 bool had_carrier
, carrier_gained
, carrier_lost
;
3339 assert(link
->ifname
);
3342 if (link
->state
== LINK_STATE_LINGER
) {
3344 log_link_info(link
, "Link readded");
3345 link_set_state(link
, LINK_STATE_ENSLAVING
);
3347 r
= link_new_carrier_maps(link
);
3352 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3353 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3354 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3356 if (link
->state
== LINK_STATE_PENDING
) {
3357 r
= free_and_strdup(&link
->ifname
, ifname
);
3361 Manager
*manager
= link
->manager
;
3364 r
= link_add(manager
, m
, &link
);
3370 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3371 if (r
>= 0 && mtu
> 0) {
3373 if (link
->original_mtu
== 0) {
3374 link
->original_mtu
= mtu
;
3375 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3378 if (link
->dhcp_client
) {
3379 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3382 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3388 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3390 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3394 /* The kernel may broadcast NEWLINK messages without the MAC address
3395 set, simply ignore them. */
3396 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3398 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3401 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3404 log_link_debug(link
, "MAC address: "
3405 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3406 mac
.ether_addr_octet
[0],
3407 mac
.ether_addr_octet
[1],
3408 mac
.ether_addr_octet
[2],
3409 mac
.ether_addr_octet
[3],
3410 mac
.ether_addr_octet
[4],
3411 mac
.ether_addr_octet
[5]);
3414 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3416 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3419 if (link
->dhcp_client
) {
3420 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3421 (const uint8_t *) &link
->mac
,
3425 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3427 switch (link
->network
->dhcp_client_identifier
) {
3428 case DHCP_CLIENT_ID_DUID
: {
3429 const DUID
*duid
= link_duid(link
);
3431 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3432 link
->network
->iaid
,
3434 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3435 duid
->raw_data_len
);
3437 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3440 case DHCP_CLIENT_ID_DUID_ONLY
: {
3441 const DUID
*duid
= link_duid(link
);
3443 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
3445 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3446 duid
->raw_data_len
);
3448 return log_link_warning_errno(link
, r
, "Could not update DUID in DHCP client: %m");
3451 case DHCP_CLIENT_ID_MAC
:
3452 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3454 (const uint8_t *)&link
->mac
,
3457 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3460 assert_not_reached("Unknown client identifier type.");
3464 if (link
->dhcp6_client
) {
3465 const DUID
* duid
= link_duid(link
);
3467 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3468 (const uint8_t *) &link
->mac
,
3472 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3474 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3475 link
->network
->iaid
);
3477 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3479 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3481 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3482 duid
->raw_data_len
);
3484 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3488 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3490 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3494 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3496 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3501 had_carrier
= link_has_carrier(link
);
3503 r
= link_update_flags(link
, m
);
3507 r
= link_update_lldp(link
);
3511 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3512 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3514 if (carrier_gained
) {
3515 log_link_info(link
, "Gained carrier");
3517 r
= link_carrier_gained(link
);
3520 } else if (carrier_lost
) {
3521 log_link_info(link
, "Lost carrier");
3523 r
= link_carrier_lost(link
);
3531 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3539 if (hashmap_isempty(h
))
3543 HASHMAP_FOREACH(link
, h
, i
) {
3547 fprintf(f
, "%i", link
->ifindex
);
3554 int link_save(Link
*link
) {
3555 _cleanup_free_
char *temp_path
= NULL
;
3556 _cleanup_fclose_
FILE *f
= NULL
;
3557 const char *admin_state
, *oper_state
;
3564 assert(link
->state_file
);
3565 assert(link
->lease_file
);
3566 assert(link
->manager
);
3568 if (link
->state
== LINK_STATE_LINGER
) {
3569 unlink(link
->state_file
);
3573 link_lldp_save(link
);
3575 admin_state
= link_state_to_string(link
->state
);
3576 assert(admin_state
);
3578 oper_state
= link_operstate_to_string(link
->operstate
);
3581 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3585 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3586 (void) fchmod(fileno(f
), 0644);
3589 "# This is private data. Do not parse.\n"
3592 admin_state
, oper_state
);
3594 if (link
->network
) {
3596 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3597 const char *dhcp_domainname
= NULL
;
3598 char **dhcp6_domains
= NULL
;
3599 char **dhcp_domains
= NULL
;
3602 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3603 yes_no(link
->network
->required_for_online
));
3605 if (link
->dhcp6_client
) {
3606 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3607 if (r
< 0 && r
!= -ENOMSG
)
3608 log_link_debug(link
, "No DHCPv6 lease");
3611 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3616 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3617 _cleanup_free_
char *b
= NULL
;
3619 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3620 &link
->network
->dns
[j
].address
, &b
);
3622 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3632 if (link
->network
->dhcp_use_dns
&&
3634 const struct in_addr
*addresses
;
3636 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3640 serialize_in_addrs(f
, addresses
, r
);
3645 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3646 struct in6_addr
*in6_addrs
;
3648 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3652 serialize_in6_addrs(f
, in6_addrs
, r
);
3657 /* Make sure to flush out old entries before we use the NDISC data */
3660 if (link
->network
->ipv6_accept_ra_use_dns
&& link
->ndisc_rdnss
) {
3663 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3667 serialize_in6_addrs(f
, &dd
->address
, 1);
3676 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3678 if (link
->network
->dhcp_use_ntp
&&
3680 const struct in_addr
*addresses
;
3682 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3686 serialize_in_addrs(f
, addresses
, r
);
3691 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3692 struct in6_addr
*in6_addrs
;
3695 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3700 serialize_in6_addrs(f
, in6_addrs
, r
);
3704 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3706 fputstrv(f
, hosts
, NULL
, &space
);
3711 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3712 if (link
->dhcp_lease
) {
3713 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3714 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3717 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3720 fputs("DOMAINS=", f
);
3722 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3724 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3727 if (dhcp_domainname
)
3728 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3730 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3732 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3734 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3735 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3740 fputs("ROUTE_DOMAINS=", f
);
3742 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3744 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3747 if (dhcp_domainname
)
3748 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3750 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3752 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3754 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3755 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3760 fprintf(f
, "LLMNR=%s\n",
3761 resolve_support_to_string(link
->network
->llmnr
));
3762 fprintf(f
, "MDNS=%s\n",
3763 resolve_support_to_string(link
->network
->mdns
));
3765 if (link
->network
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
3766 fprintf(f
, "DNS_OVER_TLS=%s\n",
3767 dns_over_tls_mode_to_string(link
->network
->dns_over_tls_mode
));
3769 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3770 fprintf(f
, "DNSSEC=%s\n",
3771 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3773 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3776 fputs("DNSSEC_NTA=", f
);
3778 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3779 fputs_with_space(f
, n
, NULL
, &space
);
3783 fputs("ADDRESSES=", f
);
3785 SET_FOREACH(a
, link
->addresses
, i
) {
3786 _cleanup_free_
char *address_str
= NULL
;
3788 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3792 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3797 fputs("ROUTES=", f
);
3799 SET_FOREACH(route
, link
->routes
, i
) {
3800 _cleanup_free_
char *route_str
= NULL
;
3802 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3806 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3807 space
? " " : "", route_str
,
3808 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3815 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3816 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3818 if (link
->dhcp_lease
) {
3819 struct in_addr address
;
3820 const char *tz
= NULL
;
3822 assert(link
->network
);
3824 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3826 fprintf(f
, "TIMEZONE=%s\n", tz
);
3828 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3830 fputs("DHCP4_ADDRESS=", f
);
3831 serialize_in_addrs(f
, &address
, 1);
3835 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3843 unlink(link
->lease_file
);
3846 struct in_addr address
;
3848 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3850 fputs("IPV4LL_ADDRESS=", f
);
3851 serialize_in_addrs(f
, &address
, 1);
3856 r
= fflush_and_check(f
);
3860 if (rename(temp_path
, link
->state_file
) < 0) {
3868 (void) unlink(link
->state_file
);
3870 (void) unlink(temp_path
);
3872 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3875 /* The serialized state in /run is no longer up-to-date. */
3876 void link_dirty(Link
*link
) {
3881 /* mark manager dirty as link is dirty */
3882 manager_dirty(link
->manager
);
3884 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3886 /* allocation errors are ignored */
3889 r
= set_put(link
->manager
->dirty_links
, link
);
3891 /* don't take another ref if the link was already dirty */
3897 /* The serialized state in /run is up-to-date */
3898 void link_clean(Link
*link
) {
3900 assert(link
->manager
);
3902 set_remove(link
->manager
->dirty_links
, link
);
3906 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3907 [LINK_STATE_PENDING
] = "pending",
3908 [LINK_STATE_ENSLAVING
] = "configuring",
3909 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3910 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3911 [LINK_STATE_CONFIGURED
] = "configured",
3912 [LINK_STATE_UNMANAGED
] = "unmanaged",
3913 [LINK_STATE_FAILED
] = "failed",
3914 [LINK_STATE_LINGER
] = "linger",
3917 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3919 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3920 [LINK_OPERSTATE_OFF
] = "off",
3921 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3922 [LINK_OPERSTATE_DORMANT
] = "dormant",
3923 [LINK_OPERSTATE_CARRIER
] = "carrier",
3924 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3925 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3928 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);