1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <netinet/ether.h>
25 #include "alloc-util.h"
27 #include "dhcp-lease-internal.h"
30 #include "netlink-util.h"
31 #include "network-internal.h"
32 #include "networkd-ipv6-proxy-ndp.h"
33 #include "networkd-lldp-tx.h"
34 #include "networkd-manager.h"
35 #include "networkd-ndisc.h"
36 #include "networkd-radv.h"
37 #include "networkd-routing-policy-rule.h"
39 #include "socket-util.h"
40 #include "stdio-util.h"
41 #include "string-table.h"
42 #include "udev-util.h"
46 static bool link_dhcp6_enabled(Link
*link
) {
49 if (!socket_ipv6_is_supported())
52 if (link
->flags
& IFF_LOOPBACK
)
58 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
61 static bool link_dhcp4_enabled(Link
*link
) {
64 if (link
->flags
& IFF_LOOPBACK
)
70 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
73 static bool link_dhcp4_server_enabled(Link
*link
) {
76 if (link
->flags
& IFF_LOOPBACK
)
82 return link
->network
->dhcp_server
;
85 static bool link_ipv4ll_enabled(Link
*link
) {
88 if (link
->flags
& IFF_LOOPBACK
)
94 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
97 static bool link_ipv6ll_enabled(Link
*link
) {
100 if (!socket_ipv6_is_supported())
103 if (link
->flags
& IFF_LOOPBACK
)
109 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
112 static bool link_ipv6_enabled(Link
*link
) {
115 if (!socket_ipv6_is_supported())
118 if (link
->network
->bridge
)
121 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
122 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
125 static bool link_radv_enabled(Link
*link
) {
128 if (!link_ipv6ll_enabled(link
))
131 return link
->network
->router_prefix_delegation
;
134 static bool link_lldp_rx_enabled(Link
*link
) {
137 if (link
->flags
& IFF_LOOPBACK
)
140 if (link
->iftype
!= ARPHRD_ETHER
)
146 /* LLDP should be handled on bridge slaves as those have a direct
147 * connection to their peers not on the bridge master. Linux doesn't
148 * even (by default) forward lldp packets to the bridge master.*/
149 if (streq_ptr("bridge", link
->kind
))
152 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
155 static bool link_lldp_emit_enabled(Link
*link
) {
158 if (link
->flags
& IFF_LOOPBACK
)
161 if (link
->iftype
!= ARPHRD_ETHER
)
167 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
170 static bool link_ipv4_forward_enabled(Link
*link
) {
173 if (link
->flags
& IFF_LOOPBACK
)
179 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
182 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
185 static bool link_ipv6_forward_enabled(Link
*link
) {
188 if (!socket_ipv6_is_supported())
191 if (link
->flags
& IFF_LOOPBACK
)
197 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
200 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
203 static bool link_proxy_arp_enabled(Link
*link
) {
206 if (link
->flags
& IFF_LOOPBACK
)
212 if (link
->network
->proxy_arp
< 0)
218 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
221 if (!socket_ipv6_is_supported())
224 if (link
->flags
& IFF_LOOPBACK
)
230 if (!link_ipv6ll_enabled(link
))
233 /* If unset use system default (enabled if local forwarding is disabled.
234 * disabled if local forwarding is enabled).
235 * If set, ignore or enforce RA independent of local forwarding state.
237 if (link
->network
->ipv6_accept_ra
< 0)
238 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
239 return !link_ipv6_forward_enabled(link
);
240 else if (link
->network
->ipv6_accept_ra
> 0)
241 /* accept RA even if ip_forward is enabled */
248 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
251 if (!socket_ipv6_is_supported())
252 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
254 if (link
->flags
& IFF_LOOPBACK
)
255 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
258 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
260 return link
->network
->ipv6_privacy_extensions
;
263 static int link_enable_ipv6(Link
*link
) {
264 const char *p
= NULL
;
268 if (link
->flags
& IFF_LOOPBACK
)
271 disabled
= !link_ipv6_enabled(link
);
273 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
275 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
277 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
278 enable_disable(!disabled
), link
->ifname
);
280 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
285 void link_update_operstate(Link
*link
) {
286 LinkOperationalState operstate
;
289 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
290 operstate
= LINK_OPERSTATE_DORMANT
;
291 else if (link_has_carrier(link
)) {
293 uint8_t scope
= RT_SCOPE_NOWHERE
;
296 /* if we have carrier, check what addresses we have */
297 SET_FOREACH(address
, link
->addresses
, i
) {
298 if (!address_is_ready(address
))
301 if (address
->scope
< scope
)
302 scope
= address
->scope
;
305 /* for operstate we also take foreign addresses into account */
306 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
307 if (!address_is_ready(address
))
310 if (address
->scope
< scope
)
311 scope
= address
->scope
;
314 if (scope
< RT_SCOPE_SITE
)
315 /* universally accessible addresses found */
316 operstate
= LINK_OPERSTATE_ROUTABLE
;
317 else if (scope
< RT_SCOPE_HOST
)
318 /* only link or site local addresses found */
319 operstate
= LINK_OPERSTATE_DEGRADED
;
321 /* no useful addresses found */
322 operstate
= LINK_OPERSTATE_CARRIER
;
323 } else if (link
->flags
& IFF_UP
)
324 operstate
= LINK_OPERSTATE_NO_CARRIER
;
326 operstate
= LINK_OPERSTATE_OFF
;
328 if (link
->operstate
!= operstate
) {
329 link
->operstate
= operstate
;
330 link_send_changed(link
, "OperationalState", NULL
);
335 #define FLAG_STRING(string, flag, old, new) \
336 (((old ^ new) & flag) \
337 ? ((old & flag) ? (" -" string) : (" +" string)) \
340 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
341 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
347 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
349 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
351 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
353 /* if we got a message without operstate, take it to mean
354 the state was unchanged */
355 operstate
= link
->kernel_operstate
;
357 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
360 if (link
->flags
!= flags
) {
361 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",
362 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
363 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
364 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
365 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
366 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
367 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
368 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
369 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
370 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
371 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
372 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
373 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
374 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
375 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
376 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
377 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
378 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
379 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
380 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
382 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
383 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
384 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
385 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
386 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
387 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
388 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
389 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
391 /* link flags are currently at most 18 bits, let's align to
393 if (unknown_flags_added
)
395 "Unknown link flags gained: %#.5x (ignoring)",
396 unknown_flags_added
);
398 if (unknown_flags_removed
)
400 "Unknown link flags lost: %#.5x (ignoring)",
401 unknown_flags_removed
);
405 link
->kernel_operstate
= operstate
;
407 link_update_operstate(link
);
412 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
413 _cleanup_link_unref_ Link
*link
= NULL
;
415 const char *ifname
, *kind
= NULL
;
417 unsigned short iftype
;
423 /* check for link kind */
424 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
426 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
427 r
= sd_netlink_message_exit_container(message
);
432 r
= sd_netlink_message_get_type(message
, &type
);
435 else if (type
!= RTM_NEWLINK
)
438 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
441 else if (ifindex
<= 0)
444 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
448 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
452 link
= new0(Link
, 1);
457 link
->manager
= manager
;
458 link
->state
= LINK_STATE_PENDING
;
459 link
->rtnl_extended_attrs
= true;
460 link
->ifindex
= ifindex
;
461 link
->iftype
= iftype
;
462 link
->ifname
= strdup(ifname
);
467 link
->kind
= strdup(kind
);
472 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
474 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
476 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
479 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
482 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
485 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
489 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
493 r
= link_update_flags(link
, message
);
503 static void link_free(Link
*link
) {
511 while (!set_isempty(link
->addresses
))
512 address_free(set_first(link
->addresses
));
514 while (!set_isempty(link
->addresses_foreign
))
515 address_free(set_first(link
->addresses_foreign
));
517 link
->addresses
= set_free(link
->addresses
);
519 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
521 while ((address
= link
->pool_addresses
)) {
522 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
523 address_free(address
);
526 sd_dhcp_server_unref(link
->dhcp_server
);
527 sd_dhcp_client_unref(link
->dhcp_client
);
528 sd_dhcp_lease_unref(link
->dhcp_lease
);
530 link_lldp_emit_stop(link
);
532 free(link
->lease_file
);
534 sd_lldp_unref(link
->lldp
);
535 free(link
->lldp_file
);
539 sd_ipv4ll_unref(link
->ipv4ll
);
540 sd_dhcp6_client_unref(link
->dhcp6_client
);
541 sd_ndisc_unref(link
->ndisc
);
542 sd_radv_unref(link
->radv
);
545 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
551 (void)unlink(link
->state_file
);
552 free(link
->state_file
);
554 udev_device_unref(link
->udev_device
);
556 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
557 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
558 hashmap_free(link
->bound_to_links
);
560 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
561 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
562 hashmap_free(link
->bound_by_links
);
567 Link
*link_unref(Link
*link
) {
571 assert(link
->n_ref
> 0);
583 Link
*link_ref(Link
*link
) {
587 assert(link
->n_ref
> 0);
594 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
601 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
610 static void link_set_state(Link
*link
, LinkState state
) {
613 if (link
->state
== state
)
618 link_send_changed(link
, "AdministrativeState", NULL
);
621 static void link_enter_unmanaged(Link
*link
) {
624 log_link_debug(link
, "Unmanaged");
626 link_set_state(link
, LINK_STATE_UNMANAGED
);
631 static int link_stop_clients(Link
*link
) {
635 assert(link
->manager
);
636 assert(link
->manager
->event
);
638 if (link
->dhcp_client
) {
639 k
= sd_dhcp_client_stop(link
->dhcp_client
);
641 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
645 k
= sd_ipv4ll_stop(link
->ipv4ll
);
647 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
650 if (link
->dhcp6_client
) {
651 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
653 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
657 k
= sd_ndisc_stop(link
->ndisc
);
659 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
663 k
= sd_radv_stop(link
->radv
);
665 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
668 link_lldp_emit_stop(link
);
672 void link_enter_failed(Link
*link
) {
675 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
678 log_link_warning(link
, "Failed");
680 link_set_state(link
, LINK_STATE_FAILED
);
682 link_stop_clients(link
);
687 static Address
* link_find_dhcp_server_address(Link
*link
) {
691 assert(link
->network
);
693 /* The first statically configured address if there is any */
694 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
696 if (address
->family
!= AF_INET
)
699 if (in_addr_is_null(address
->family
, &address
->in_addr
))
705 /* If that didn't work, find a suitable address we got from the pool */
706 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
707 if (address
->family
!= AF_INET
)
716 static void link_enter_configured(Link
*link
) {
718 assert(link
->network
);
720 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
723 log_link_info(link
, "Configured");
725 link_set_state(link
, LINK_STATE_CONFIGURED
);
730 void link_check_ready(Link
*link
) {
736 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
742 if (!link
->static_configured
)
745 if (link_ipv4ll_enabled(link
))
746 if (!link
->ipv4ll_address
||
750 if (!link
->network
->bridge
) {
752 if (link_ipv6ll_enabled(link
))
753 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
756 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
757 !link
->dhcp4_configured
) ||
758 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
759 !link
->dhcp6_configured
) ||
760 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
761 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
764 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
768 SET_FOREACH(a
, link
->addresses
, i
)
769 if (!address_is_ready(a
))
772 if (link
->state
!= LINK_STATE_CONFIGURED
)
773 link_enter_configured(link
);
778 static int link_set_routing_policy_rule(Link
*link
) {
779 RoutingPolicyRule
*rule
, *rrule
= NULL
;
783 assert(link
->network
);
785 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
786 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
787 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, &rrule
);
789 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
793 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
795 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
796 link_enter_failed(link
);
800 link
->link_messages
++;
803 routing_policy_rule_purge(link
->manager
, link
);
808 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
809 _cleanup_link_unref_ Link
*link
= userdata
;
812 assert(link
->link_messages
> 0);
813 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
814 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
817 link
->link_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
->link_messages
== 0) {
827 log_link_debug(link
, "Routes set");
828 link
->static_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 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
845 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
846 r
= route_configure(rt
, link
, route_handler
);
848 log_link_warning_errno(link
, r
, "Could not set routes: %m");
849 link_enter_failed(link
);
853 link
->link_messages
++;
856 if (link
->link_messages
== 0) {
857 link
->static_configured
= true;
858 link_check_ready(link
);
860 log_link_debug(link
, "Setting routes");
865 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
866 _cleanup_link_unref_ Link
*link
= userdata
;
871 assert(link
->ifname
);
873 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
876 r
= sd_netlink_message_get_errno(m
);
877 if (r
< 0 && r
!= -ESRCH
)
878 log_link_warning_errno(link
, r
, "Could not drop route: %m");
883 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
884 _cleanup_link_unref_ Link
*link
= userdata
;
890 assert(link
->ifname
);
891 assert(link
->link_messages
> 0);
892 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
893 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
895 link
->link_messages
--;
897 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
900 r
= sd_netlink_message_get_errno(m
);
901 if (r
< 0 && r
!= -EEXIST
)
902 log_link_warning_errno(link
, r
, "could not set address: %m");
904 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
906 if (link
->link_messages
== 0) {
907 log_link_debug(link
, "Addresses set");
908 link_enter_set_routes(link
);
914 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
915 _cleanup_link_unref_ Link
*link
= userdata
;
921 assert(link
->ifname
);
922 assert(link
->link_messages
> 0);
924 link
->link_messages
--;
926 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
929 r
= sd_netlink_message_get_errno(m
);
930 if (r
< 0 && r
!= -EEXIST
)
931 log_link_warning_errno(link
, r
, "could not set address label: %m");
933 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
935 if (link
->link_messages
== 0) {
936 log_link_debug(link
, "Addresses label set");
937 link_enter_set_routes(link
);
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
->link_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
->link_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");
1153 if (link
->network
->dhcp_server_emit_ntp
) {
1155 if (link
->network
->n_dhcp_server_ntp
> 0)
1156 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1158 if (!acquired_uplink
)
1159 uplink
= manager_find_uplink(link
->manager
, link
);
1162 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1165 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1169 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1172 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1174 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1178 if (link
->network
->dhcp_server_emit_timezone
) {
1179 _cleanup_free_
char *buffer
= NULL
;
1180 const char *tz
= NULL
;
1182 if (link
->network
->dhcp_server_timezone
)
1183 tz
= link
->network
->dhcp_server_timezone
;
1185 r
= get_timezone(&buffer
);
1187 log_warning_errno(r
, "Failed to determine timezone: %m");
1193 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1199 r
= sd_dhcp_server_start(link
->dhcp_server
);
1201 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1203 link_enter_failed(link
);
1208 log_link_debug(link
, "Offering DHCPv4 leases");
1211 if (link
->link_messages
== 0)
1212 link_enter_set_routes(link
);
1214 log_link_debug(link
, "Setting addresses");
1219 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1220 _cleanup_link_unref_ Link
*link
= userdata
;
1225 assert(link
->ifname
);
1227 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1230 r
= sd_netlink_message_get_errno(m
);
1231 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1232 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1237 static int link_set_bridge_vlan(Link
*link
) {
1240 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1242 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1247 static int link_set_proxy_arp(Link
*link
) {
1248 const char *p
= NULL
;
1251 if (!link_proxy_arp_enabled(link
))
1254 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1256 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1258 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1263 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1264 _cleanup_link_unref_ Link
*link
= userdata
;
1267 log_link_debug(link
, "Set link");
1269 r
= sd_netlink_message_get_errno(m
);
1270 if (r
< 0 && r
!= -EEXIST
) {
1271 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1272 link_enter_failed(link
);
1279 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1280 _cleanup_link_unref_ Link
*link
= userdata
;
1285 assert(link
->ifname
);
1287 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1290 r
= sd_netlink_message_get_errno(m
);
1292 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1297 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1298 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1302 assert(link
->manager
);
1303 assert(link
->manager
->rtnl
);
1305 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1307 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1309 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1311 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1313 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1315 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1317 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1324 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1325 _cleanup_link_unref_ Link
*link
= userdata
;
1330 assert(link
->ifname
);
1332 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1335 r
= sd_netlink_message_get_errno(m
);
1337 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1342 static int link_set_flags(Link
*link
) {
1343 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1344 unsigned ifi_change
= 0;
1345 unsigned ifi_flags
= 0;
1349 assert(link
->manager
);
1350 assert(link
->manager
->rtnl
);
1352 if (link
->flags
& IFF_LOOPBACK
)
1358 if (link
->network
->arp
< 0)
1361 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1363 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1365 if (link
->network
->arp
>= 0) {
1366 ifi_change
|= IFF_NOARP
;
1367 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1370 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1372 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1374 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1376 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1383 static int link_set_bridge(Link
*link
) {
1384 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1388 assert(link
->network
);
1390 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1392 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1394 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1396 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1398 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1400 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1402 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1404 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1406 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1408 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1410 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1412 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1414 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1416 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1418 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1420 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1422 if (link
->network
->cost
!= 0) {
1423 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1425 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1427 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1428 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1430 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1433 r
= sd_netlink_message_close_container(req
);
1435 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1437 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1439 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1446 static int link_bond_set(Link
*link
) {
1447 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1451 assert(link
->network
);
1453 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1455 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1457 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1459 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1461 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1463 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1465 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1467 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1469 if (link
->network
->active_slave
) {
1470 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1472 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1475 if (link
->network
->primary_slave
) {
1476 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1478 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1481 r
= sd_netlink_message_close_container(req
);
1483 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1485 r
= sd_netlink_message_close_container(req
);
1487 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1489 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1491 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1498 static int link_lldp_save(Link
*link
) {
1499 _cleanup_free_
char *temp_path
= NULL
;
1500 _cleanup_fclose_
FILE *f
= NULL
;
1501 sd_lldp_neighbor
**l
= NULL
;
1505 assert(link
->lldp_file
);
1508 (void) unlink(link
->lldp_file
);
1512 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1516 (void) unlink(link
->lldp_file
);
1522 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1526 fchmod(fileno(f
), 0644);
1528 for (i
= 0; i
< n
; i
++) {
1533 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1538 (void) fwrite(&u
, 1, sizeof(u
), f
);
1539 (void) fwrite(p
, 1, sz
, f
);
1542 r
= fflush_and_check(f
);
1546 if (rename(temp_path
, link
->lldp_file
) < 0) {
1553 (void) unlink(link
->lldp_file
);
1555 (void) unlink(temp_path
);
1557 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1561 for (i
= 0; i
< n
; i
++)
1562 sd_lldp_neighbor_unref(l
[i
]);
1569 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1570 Link
*link
= userdata
;
1575 (void) link_lldp_save(link
);
1577 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1578 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1580 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1582 r
= link_lldp_emit_start(link
);
1584 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1588 static int link_acquire_ipv6_conf(Link
*link
) {
1593 if (link_dhcp6_enabled(link
)) {
1594 assert(link
->dhcp6_client
);
1595 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1597 /* start DHCPv6 client in stateless mode */
1598 r
= dhcp6_request_address(link
, true);
1599 if (r
< 0 && r
!= -EBUSY
)
1600 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1602 log_link_debug(link
, "Acquiring DHCPv6 lease");
1605 if (link_ipv6_accept_ra_enabled(link
)) {
1606 assert(link
->ndisc
);
1608 log_link_debug(link
, "Discovering IPv6 routers");
1610 r
= sd_ndisc_start(link
->ndisc
);
1611 if (r
< 0 && r
!= -EBUSY
)
1612 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1615 if (link_radv_enabled(link
)) {
1617 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1619 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1621 r
= sd_radv_start(link
->radv
);
1622 if (r
< 0 && r
!= -EBUSY
)
1623 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1629 static int link_acquire_ipv4_conf(Link
*link
) {
1633 assert(link
->network
);
1634 assert(link
->manager
);
1635 assert(link
->manager
->event
);
1637 if (link_ipv4ll_enabled(link
)) {
1638 assert(link
->ipv4ll
);
1640 log_link_debug(link
, "Acquiring IPv4 link-local address");
1642 r
= sd_ipv4ll_start(link
->ipv4ll
);
1644 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1647 if (link_dhcp4_enabled(link
)) {
1648 assert(link
->dhcp_client
);
1650 log_link_debug(link
, "Acquiring DHCPv4 lease");
1652 r
= sd_dhcp_client_start(link
->dhcp_client
);
1654 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1660 static int link_acquire_conf(Link
*link
) {
1665 r
= link_acquire_ipv4_conf(link
);
1669 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1670 r
= link_acquire_ipv6_conf(link
);
1675 if (link_lldp_emit_enabled(link
)) {
1676 r
= link_lldp_emit_start(link
);
1678 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1684 bool link_has_carrier(Link
*link
) {
1685 /* see Documentation/networking/operstates.txt in the kernel sources */
1687 if (link
->kernel_operstate
== IF_OPER_UP
)
1690 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1691 /* operstate may not be implemented, so fall back to flags */
1692 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1698 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1699 _cleanup_link_unref_ Link
*link
= userdata
;
1704 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1707 r
= sd_netlink_message_get_errno(m
);
1709 /* we warn but don't fail the link, as it may be
1711 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1716 int link_up(Link
*link
) {
1717 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1718 uint8_t ipv6ll_mode
;
1722 assert(link
->network
);
1723 assert(link
->manager
);
1724 assert(link
->manager
->rtnl
);
1726 log_link_debug(link
, "Bringing link up");
1728 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1730 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1732 /* set it free if not enslaved with networkd */
1733 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1734 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1736 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1739 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1741 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1743 if (link
->network
->mac
) {
1744 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1746 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1749 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1750 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1751 (void) link_enable_ipv6(link
);
1753 if (link
->network
->mtu
) {
1754 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1755 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1756 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1758 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1759 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1761 link
->network
->mtu
= IPV6_MIN_MTU
;
1764 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1766 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1769 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1771 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1773 if (link_ipv6_enabled(link
)) {
1774 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1775 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1777 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1779 if (!link_ipv6ll_enabled(link
))
1780 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1782 const char *p
= NULL
;
1783 _cleanup_free_
char *stable_secret
= NULL
;
1785 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1786 r
= read_one_line_file(p
, &stable_secret
);
1789 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1791 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1793 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1795 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1797 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1798 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1800 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1803 r
= sd_netlink_message_close_container(req
);
1805 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1808 r
= sd_netlink_message_close_container(req
);
1810 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1812 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1814 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1821 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1822 _cleanup_link_unref_ Link
*link
= userdata
;
1827 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1830 r
= sd_netlink_message_get_errno(m
);
1832 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1837 int link_down(Link
*link
) {
1838 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1842 assert(link
->manager
);
1843 assert(link
->manager
->rtnl
);
1845 log_link_debug(link
, "Bringing link down");
1847 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1848 RTM_SETLINK
, link
->ifindex
);
1850 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1852 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1854 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1856 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1858 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1865 static int link_up_can(Link
*link
) {
1866 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1871 log_link_debug(link
, "Bringing CAN link up");
1873 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1875 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1877 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1879 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1881 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1883 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1890 static int link_handle_bound_to_list(Link
*link
) {
1894 bool required_up
= false;
1895 bool link_is_up
= false;
1899 if (hashmap_isempty(link
->bound_to_links
))
1902 if (link
->flags
& IFF_UP
)
1905 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1906 if (link_has_carrier(l
)) {
1911 if (!required_up
&& link_is_up
) {
1912 r
= link_down(link
);
1915 } else if (required_up
&& !link_is_up
) {
1924 static int link_handle_bound_by_list(Link
*link
) {
1931 if (hashmap_isempty(link
->bound_by_links
))
1934 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1935 r
= link_handle_bound_to_list(l
);
1943 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1949 if (link
== carrier
)
1952 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1955 r
= hashmap_ensure_allocated(h
, NULL
);
1959 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1966 static int link_new_bound_by_list(Link
*link
) {
1971 bool list_updated
= false;
1974 assert(link
->manager
);
1978 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1979 if (!carrier
->network
)
1982 if (strv_isempty(carrier
->network
->bind_carrier
))
1985 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1986 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1990 list_updated
= true;
1997 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1998 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2002 link_dirty(carrier
);
2008 static int link_new_bound_to_list(Link
*link
) {
2013 bool list_updated
= false;
2016 assert(link
->manager
);
2021 if (strv_isempty(link
->network
->bind_carrier
))
2026 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2027 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2028 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2032 list_updated
= true;
2039 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2040 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2044 link_dirty(carrier
);
2050 static int link_new_carrier_maps(Link
*link
) {
2053 r
= link_new_bound_by_list(link
);
2057 r
= link_handle_bound_by_list(link
);
2061 r
= link_new_bound_to_list(link
);
2065 r
= link_handle_bound_to_list(link
);
2072 static void link_free_bound_to_list(Link
*link
) {
2076 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2077 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2079 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2080 link_dirty(bound_to
);
2086 static void link_free_bound_by_list(Link
*link
) {
2090 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2091 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2093 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2094 link_dirty(bound_by
);
2095 link_handle_bound_to_list(bound_by
);
2102 static void link_free_carrier_maps(Link
*link
) {
2103 bool list_updated
= false;
2107 if (!hashmap_isempty(link
->bound_to_links
)) {
2108 link_free_bound_to_list(link
);
2109 list_updated
= true;
2112 if (!hashmap_isempty(link
->bound_by_links
)) {
2113 link_free_bound_by_list(link
);
2114 list_updated
= true;
2123 void link_drop(Link
*link
) {
2124 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2127 link_set_state(link
, LINK_STATE_LINGER
);
2129 link_free_carrier_maps(link
);
2131 log_link_debug(link
, "Link removed");
2133 (void)unlink(link
->state_file
);
2139 static int link_joined(Link
*link
) {
2143 assert(link
->network
);
2145 if (!hashmap_isempty(link
->bound_to_links
)) {
2146 r
= link_handle_bound_to_list(link
);
2149 } else if (!(link
->flags
& IFF_UP
)) {
2152 link_enter_failed(link
);
2157 if (link
->network
->bridge
) {
2158 r
= link_set_bridge(link
);
2160 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2163 if (link
->network
->bond
) {
2164 r
= link_bond_set(link
);
2166 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2169 if (link
->network
->use_br_vlan
&&
2170 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2171 r
= link_set_bridge_vlan(link
);
2173 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2176 /* Skip setting up addresses until it gets carrier,
2177 or it would try to set addresses twice,
2178 which is bad for non-idempotent steps. */
2179 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2182 return link_enter_set_addresses(link
);
2185 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2186 _cleanup_link_unref_ Link
*link
= userdata
;
2190 assert(link
->network
);
2194 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2197 r
= sd_netlink_message_get_errno(m
);
2198 if (r
< 0 && r
!= -EEXIST
) {
2199 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2200 link_enter_failed(link
);
2203 log_link_debug(link
, "Joined netdev");
2205 if (link
->enslaving
<= 0)
2211 static int link_enter_join_netdev(Link
*link
) {
2217 assert(link
->network
);
2218 assert(link
->state
== LINK_STATE_PENDING
);
2220 link_set_state(link
, LINK_STATE_ENSLAVING
);
2224 if (!link
->network
->bridge
&&
2225 !link
->network
->bond
&&
2226 !link
->network
->vrf
&&
2227 hashmap_isempty(link
->network
->stacked_netdevs
))
2228 return link_joined(link
);
2230 if (link
->network
->bond
) {
2231 log_struct(LOG_DEBUG
,
2232 LOG_LINK_INTERFACE(link
),
2233 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2234 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2237 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2239 log_struct_errno(LOG_WARNING
, r
,
2240 LOG_LINK_INTERFACE(link
),
2241 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2242 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2245 link_enter_failed(link
);
2252 if (link
->network
->bridge
) {
2253 log_struct(LOG_DEBUG
,
2254 LOG_LINK_INTERFACE(link
),
2255 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2256 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2259 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2261 log_struct_errno(LOG_WARNING
, r
,
2262 LOG_LINK_INTERFACE(link
),
2263 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2264 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2266 link_enter_failed(link
);
2273 if (link
->network
->vrf
) {
2274 log_struct(LOG_DEBUG
,
2275 LOG_LINK_INTERFACE(link
),
2276 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2277 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2279 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2281 log_struct_errno(LOG_WARNING
, r
,
2282 LOG_LINK_INTERFACE(link
),
2283 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2284 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2286 link_enter_failed(link
);
2293 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2295 if (netdev
->ifindex
> 0) {
2300 log_struct(LOG_DEBUG
,
2301 LOG_LINK_INTERFACE(link
),
2302 LOG_NETDEV_INTERFACE(netdev
),
2303 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2306 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2308 log_struct_errno(LOG_WARNING
, r
,
2309 LOG_LINK_INTERFACE(link
),
2310 LOG_NETDEV_INTERFACE(netdev
),
2311 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2313 link_enter_failed(link
);
2323 static int link_set_ipv4_forward(Link
*link
) {
2326 if (!link_ipv4_forward_enabled(link
))
2329 /* We propagate the forwarding flag from one interface to the
2330 * global setting one way. This means: as long as at least one
2331 * interface was configured at any time that had IP forwarding
2332 * enabled the setting will stay on for good. We do this
2333 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2334 * somewhat in sync (see below). */
2336 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2338 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2343 static int link_set_ipv6_forward(Link
*link
) {
2346 if (!link_ipv6_forward_enabled(link
))
2349 /* On Linux, the IPv6 stack does not know a per-interface
2350 * packet forwarding setting: either packet forwarding is on
2351 * for all, or off for all. We hence don't bother with a
2352 * per-interface setting, but simply propagate the interface
2353 * flag, if it is set, to the global flag, one-way. Note that
2354 * while IPv4 would allow a per-interface flag, we expose the
2355 * same behaviour there and also propagate the setting from
2356 * one to all, to keep things simple (see above). */
2358 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2360 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2365 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2366 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2367 IPv6PrivacyExtensions s
;
2368 const char *p
= NULL
;
2371 s
= link_ipv6_privacy_extensions(link
);
2375 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2376 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2378 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2380 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2385 static int link_set_ipv6_accept_ra(Link
*link
) {
2386 const char *p
= NULL
;
2389 /* Make this a NOP if IPv6 is not available */
2390 if (!socket_ipv6_is_supported())
2393 if (link
->flags
& IFF_LOOPBACK
)
2399 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2401 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2402 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2404 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2409 static int link_set_ipv6_dad_transmits(Link
*link
) {
2410 char buf
[DECIMAL_STR_MAX(int) + 1];
2411 const char *p
= NULL
;
2414 /* Make this a NOP if IPv6 is not available */
2415 if (!socket_ipv6_is_supported())
2418 if (link
->flags
& IFF_LOOPBACK
)
2424 if (link
->network
->ipv6_dad_transmits
< 0)
2427 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2428 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2430 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2432 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2437 static int link_set_ipv6_hop_limit(Link
*link
) {
2438 char buf
[DECIMAL_STR_MAX(int) + 1];
2439 const char *p
= NULL
;
2442 /* Make this a NOP if IPv6 is not available */
2443 if (!socket_ipv6_is_supported())
2446 if (link
->flags
& IFF_LOOPBACK
)
2452 if (link
->network
->ipv6_hop_limit
< 0)
2455 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2456 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2458 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2460 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2465 static int link_drop_foreign_config(Link
*link
) {
2471 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2472 /* we consider IPv6LL addresses to be managed by the kernel */
2473 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2476 r
= address_remove(address
, link
, link_address_remove_handler
);
2481 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2482 /* do not touch routes managed by the kernel */
2483 if (route
->protocol
== RTPROT_KERNEL
)
2486 r
= route_remove(route
, link
, link_route_remove_handler
);
2494 static int link_drop_config(Link
*link
) {
2495 Address
*address
, *pool_address
;
2500 SET_FOREACH(address
, link
->addresses
, i
) {
2501 /* we consider IPv6LL addresses to be managed by the kernel */
2502 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2505 r
= address_remove(address
, link
, link_address_remove_handler
);
2509 /* If this address came from an address pool, clean up the pool */
2510 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2511 if (address_equal(address
, pool_address
)) {
2512 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2513 address_free(pool_address
);
2519 SET_FOREACH(route
, link
->routes
, i
) {
2520 /* do not touch routes managed by the kernel */
2521 if (route
->protocol
== RTPROT_KERNEL
)
2524 r
= route_remove(route
, link
, link_route_remove_handler
);
2534 static int link_update_lldp(Link
*link
) {
2542 if (link
->flags
& IFF_UP
) {
2543 r
= sd_lldp_start(link
->lldp
);
2545 log_link_debug(link
, "Started LLDP.");
2547 r
= sd_lldp_stop(link
->lldp
);
2549 log_link_debug(link
, "Stopped LLDP.");
2555 static int link_configure(Link
*link
) {
2559 assert(link
->network
);
2560 assert(link
->state
== LINK_STATE_PENDING
);
2562 if (streq_ptr(link
->kind
, "vcan")) {
2564 if (!(link
->flags
& IFF_UP
)) {
2565 r
= link_up_can(link
);
2567 link_enter_failed(link
);
2575 /* Drop foreign config, but ignore loopback or critical devices.
2576 * We do not want to remove loopback address or addresses used for root NFS. */
2577 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2578 r
= link_drop_foreign_config(link
);
2583 r
= link_set_proxy_arp(link
);
2587 r
= ipv6_proxy_ndp_addresses_configure(link
);
2591 r
= link_set_ipv4_forward(link
);
2595 r
= link_set_ipv6_forward(link
);
2599 r
= link_set_ipv6_privacy_extensions(link
);
2603 r
= link_set_ipv6_accept_ra(link
);
2607 r
= link_set_ipv6_dad_transmits(link
);
2611 r
= link_set_ipv6_hop_limit(link
);
2615 r
= link_set_flags(link
);
2619 if (link_ipv4ll_enabled(link
)) {
2620 r
= ipv4ll_configure(link
);
2625 if (link_dhcp4_enabled(link
)) {
2626 r
= dhcp4_configure(link
);
2631 if (link_dhcp4_server_enabled(link
)) {
2632 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2636 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2641 if (link_dhcp6_enabled(link
) ||
2642 link_ipv6_accept_ra_enabled(link
)) {
2643 r
= dhcp6_configure(link
);
2648 if (link_ipv6_accept_ra_enabled(link
)) {
2649 r
= ndisc_configure(link
);
2654 if (link_radv_enabled(link
)) {
2655 r
= radv_configure(link
);
2660 if (link_lldp_rx_enabled(link
)) {
2661 r
= sd_lldp_new(&link
->lldp
);
2665 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2669 r
= sd_lldp_match_capabilities(link
->lldp
,
2670 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2671 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2672 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2676 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2680 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2684 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2688 r
= link_update_lldp(link
);
2693 (void) link_set_routing_policy_rule(link
);
2695 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2696 r
= link_acquire_conf(link
);
2701 return link_enter_join_netdev(link
);
2704 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2706 _cleanup_link_unref_ Link
*link
= userdata
;
2711 assert(link
->ifname
);
2712 assert(link
->manager
);
2714 if (link
->state
!= LINK_STATE_PENDING
)
2717 log_link_debug(link
, "Link state is up-to-date");
2719 r
= link_new_bound_by_list(link
);
2723 r
= link_handle_bound_by_list(link
);
2727 if (!link
->network
) {
2728 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2729 &link
->mac
, &network
);
2731 link_enter_unmanaged(link
);
2733 } else if (r
== 0 && network
->unmanaged
) {
2734 link_enter_unmanaged(link
);
2739 if (link
->flags
& IFF_LOOPBACK
) {
2740 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2741 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2743 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2744 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2746 if (network
->dhcp_server
)
2747 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2750 r
= network_apply(network
, link
);
2755 r
= link_new_bound_to_list(link
);
2759 r
= link_configure(link
);
2766 int link_initialized(Link
*link
, struct udev_device
*device
) {
2767 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2771 assert(link
->manager
);
2772 assert(link
->manager
->rtnl
);
2775 if (link
->state
!= LINK_STATE_PENDING
)
2778 if (link
->udev_device
)
2781 log_link_debug(link
, "udev initialized link");
2783 link
->udev_device
= udev_device_ref(device
);
2785 /* udev has initialized the link, but we don't know if we have yet
2786 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2787 * when it returns we know that the pending NEWLINKs have already been
2788 * processed and that we are up-to-date */
2790 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2795 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2796 link_initialized_and_synced
, link
, 0, NULL
);
2805 static int link_load(Link
*link
) {
2806 _cleanup_free_
char *network_file
= NULL
,
2809 *dhcp4_address
= NULL
,
2810 *ipv4ll_address
= NULL
;
2811 union in_addr_union address
;
2812 union in_addr_union route_dst
;
2818 r
= parse_env_file(link
->state_file
, NEWLINE
,
2819 "NETWORK_FILE", &network_file
,
2820 "ADDRESSES", &addresses
,
2822 "DHCP4_ADDRESS", &dhcp4_address
,
2823 "IPV4LL_ADDRESS", &ipv4ll_address
,
2825 if (r
< 0 && r
!= -ENOENT
)
2826 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2833 suffix
= strrchr(network_file
, '.');
2835 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2836 goto network_file_fail
;
2840 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2842 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2843 goto network_file_fail
;
2846 r
= network_apply(network
, link
);
2848 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2857 _cleanup_free_
char *address_str
= NULL
;
2858 char *prefixlen_str
;
2860 unsigned char prefixlen
;
2862 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2864 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2870 prefixlen_str
= strchr(address_str
, '/');
2871 if (!prefixlen_str
) {
2872 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2876 *prefixlen_str
++ = '\0';
2878 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2880 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2884 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2886 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2890 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2892 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2901 _cleanup_free_
char *route_str
= NULL
;
2902 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2904 char *prefixlen_str
;
2906 unsigned char prefixlen
, tos
, table
;
2909 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2911 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2917 prefixlen_str
= strchr(route_str
, '/');
2918 if (!prefixlen_str
) {
2919 log_link_debug(link
, "Failed to parse route %s", route_str
);
2923 *prefixlen_str
++ = '\0';
2925 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2927 log_link_debug(link
,
2928 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2933 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2935 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2939 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2941 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2943 if (lifetime
!= USEC_INFINITY
) {
2944 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2945 0, route_expire_handler
, route
);
2947 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2950 route
->lifetime
= lifetime
;
2951 sd_event_source_unref(route
->expire
);
2952 route
->expire
= expire
;
2957 if (dhcp4_address
) {
2958 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2960 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2961 goto dhcp4_address_fail
;
2964 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
2966 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2968 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2970 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2975 if (ipv4ll_address
) {
2976 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2978 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2979 goto ipv4ll_address_fail
;
2982 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2984 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2986 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2988 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2991 ipv4ll_address_fail
:
2996 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2998 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2999 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3007 r
= link_new(m
, message
, ret
);
3013 log_link_debug(link
, "Link %d added", link
->ifindex
);
3015 r
= link_load(link
);
3019 if (detect_container() <= 0) {
3020 /* not in a container, udev will be around */
3021 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3022 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3024 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3028 if (udev_device_get_is_initialized(device
) <= 0) {
3030 log_link_debug(link
, "link pending udev initialization...");
3034 r
= link_initialized(link
, device
);
3038 /* we are calling a callback directly, so must take a ref */
3041 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3048 link_enter_failed(link
);
3052 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3057 log_link_info(link
, "Gained IPv6LL");
3059 link
->ipv6ll_address
= *address
;
3060 link_check_ready(link
);
3062 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3063 r
= link_acquire_ipv6_conf(link
);
3065 link_enter_failed(link
);
3073 static int link_carrier_gained(Link
*link
) {
3078 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3079 r
= link_acquire_conf(link
);
3081 link_enter_failed(link
);
3085 r
= link_enter_set_addresses(link
);
3090 r
= link_handle_bound_by_list(link
);
3097 static int link_carrier_lost(Link
*link
) {
3102 r
= link_stop_clients(link
);
3104 link_enter_failed(link
);
3108 if (link_dhcp4_server_enabled(link
))
3109 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3111 r
= link_drop_config(link
);
3115 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3116 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3117 r
= link_drop_foreign_config(link
);
3122 r
= link_handle_bound_by_list(link
);
3129 int link_carrier_reset(Link
*link
) {
3134 if (link_has_carrier(link
)) {
3135 r
= link_carrier_lost(link
);
3139 r
= link_carrier_gained(link
);
3143 log_link_info(link
, "Reset carrier");
3149 int link_update(Link
*link
, sd_netlink_message
*m
) {
3150 struct ether_addr mac
;
3153 bool had_carrier
, carrier_gained
, carrier_lost
;
3157 assert(link
->ifname
);
3160 if (link
->state
== LINK_STATE_LINGER
) {
3162 log_link_info(link
, "Link readded");
3163 link_set_state(link
, LINK_STATE_ENSLAVING
);
3165 r
= link_new_carrier_maps(link
);
3170 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3171 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3172 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3174 link_free_carrier_maps(link
);
3176 r
= free_and_strdup(&link
->ifname
, ifname
);
3180 r
= link_new_carrier_maps(link
);
3185 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3186 if (r
>= 0 && mtu
> 0) {
3188 if (!link
->original_mtu
) {
3189 link
->original_mtu
= mtu
;
3190 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3193 if (link
->dhcp_client
) {
3194 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3197 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3203 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3205 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3209 /* The kernel may broadcast NEWLINK messages without the MAC address
3210 set, simply ignore them. */
3211 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3213 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3216 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3219 log_link_debug(link
, "MAC address: "
3220 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3221 mac
.ether_addr_octet
[0],
3222 mac
.ether_addr_octet
[1],
3223 mac
.ether_addr_octet
[2],
3224 mac
.ether_addr_octet
[3],
3225 mac
.ether_addr_octet
[4],
3226 mac
.ether_addr_octet
[5]);
3229 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3231 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3234 if (link
->dhcp_client
) {
3235 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3236 (const uint8_t *) &link
->mac
,
3240 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3242 switch (link
->network
->dhcp_client_identifier
) {
3243 case DHCP_CLIENT_ID_DUID
: {
3244 const DUID
*duid
= link_duid(link
);
3246 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3247 link
->network
->iaid
,
3249 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3250 duid
->raw_data_len
);
3252 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3255 case DHCP_CLIENT_ID_MAC
:
3256 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3258 (const uint8_t *)&link
->mac
,
3261 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3264 assert_not_reached("Unknown client identifier type.");
3268 if (link
->dhcp6_client
) {
3269 const DUID
* duid
= link_duid(link
);
3271 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3272 (const uint8_t *) &link
->mac
,
3276 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3278 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3279 link
->network
->iaid
);
3281 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3283 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3285 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3286 duid
->raw_data_len
);
3288 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3292 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3294 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3299 had_carrier
= link_has_carrier(link
);
3301 r
= link_update_flags(link
, m
);
3305 r
= link_update_lldp(link
);
3309 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3310 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3312 if (carrier_gained
) {
3313 log_link_info(link
, "Gained carrier");
3315 r
= link_carrier_gained(link
);
3318 } else if (carrier_lost
) {
3319 log_link_info(link
, "Lost carrier");
3321 r
= link_carrier_lost(link
);
3329 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3337 if (hashmap_isempty(h
))
3340 fputs_unlocked(prefix
, f
);
3341 HASHMAP_FOREACH(link
, h
, i
) {
3343 fputc_unlocked(' ', f
);
3345 fprintf(f
, "%i", link
->ifindex
);
3349 fputc_unlocked('\n', f
);
3352 int link_save(Link
*link
) {
3353 _cleanup_free_
char *temp_path
= NULL
;
3354 _cleanup_fclose_
FILE *f
= NULL
;
3355 const char *admin_state
, *oper_state
;
3362 assert(link
->state_file
);
3363 assert(link
->lease_file
);
3364 assert(link
->manager
);
3366 if (link
->state
== LINK_STATE_LINGER
) {
3367 unlink(link
->state_file
);
3371 link_lldp_save(link
);
3373 admin_state
= link_state_to_string(link
->state
);
3374 assert(admin_state
);
3376 oper_state
= link_operstate_to_string(link
->operstate
);
3379 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3383 (void) fchmod(fileno(f
), 0644);
3386 "# This is private data. Do not parse.\n"
3389 admin_state
, oper_state
);
3391 if (link
->network
) {
3393 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3394 const char *dhcp_domainname
= NULL
;
3395 char **dhcp6_domains
= NULL
;
3396 char **dhcp_domains
= NULL
;
3399 if (link
->dhcp6_client
) {
3400 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3401 if (r
< 0 && r
!= -ENOMSG
)
3402 log_link_debug(link
, "No DHCPv6 lease");
3405 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3407 fputs_unlocked("DNS=", f
);
3410 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3411 _cleanup_free_
char *b
= NULL
;
3413 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3414 &link
->network
->dns
[j
].address
, &b
);
3416 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3421 fputc_unlocked(' ', f
);
3422 fputs_unlocked(b
, f
);
3426 if (link
->network
->dhcp_use_dns
&&
3428 const struct in_addr
*addresses
;
3430 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3433 fputc_unlocked(' ', f
);
3434 serialize_in_addrs(f
, addresses
, r
);
3439 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3440 struct in6_addr
*in6_addrs
;
3442 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3445 fputc_unlocked(' ', f
);
3446 serialize_in6_addrs(f
, in6_addrs
, r
);
3451 /* Make sure to flush out old entries before we use the NDISC data */
3454 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3457 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3459 fputc_unlocked(' ', f
);
3461 serialize_in6_addrs(f
, &dd
->address
, 1);
3466 fputc_unlocked('\n', f
);
3468 fputs_unlocked("NTP=", f
);
3470 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3472 if (link
->network
->dhcp_use_ntp
&&
3474 const struct in_addr
*addresses
;
3476 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3479 fputc_unlocked(' ', f
);
3480 serialize_in_addrs(f
, addresses
, r
);
3485 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3486 struct in6_addr
*in6_addrs
;
3489 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3493 fputc_unlocked(' ', f
);
3494 serialize_in6_addrs(f
, in6_addrs
, r
);
3498 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3500 fputstrv(f
, hosts
, NULL
, &space
);
3503 fputc_unlocked('\n', f
);
3505 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3506 if (link
->dhcp_lease
) {
3507 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3508 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3511 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3514 fputs_unlocked("DOMAINS=", f
);
3516 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3518 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3521 if (dhcp_domainname
)
3522 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3524 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3526 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3528 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3529 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3532 fputc_unlocked('\n', f
);
3534 fputs_unlocked("ROUTE_DOMAINS=", f
);
3536 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3538 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3541 if (dhcp_domainname
)
3542 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3544 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3546 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3548 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3549 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3552 fputc_unlocked('\n', f
);
3554 fprintf(f
, "LLMNR=%s\n",
3555 resolve_support_to_string(link
->network
->llmnr
));
3556 fprintf(f
, "MDNS=%s\n",
3557 resolve_support_to_string(link
->network
->mdns
));
3559 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3560 fprintf(f
, "DNSSEC=%s\n",
3561 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3563 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3566 fputs_unlocked("DNSSEC_NTA=", f
);
3568 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3569 fputs_with_space(f
, n
, NULL
, &space
);
3570 fputc_unlocked('\n', f
);
3573 fputs_unlocked("ADDRESSES=", f
);
3575 SET_FOREACH(a
, link
->addresses
, i
) {
3576 _cleanup_free_
char *address_str
= NULL
;
3578 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3582 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3585 fputc_unlocked('\n', f
);
3587 fputs_unlocked("ROUTES=", f
);
3589 SET_FOREACH(route
, link
->routes
, i
) {
3590 _cleanup_free_
char *route_str
= NULL
;
3592 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3596 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3597 space
? " " : "", route_str
,
3598 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3602 fputc_unlocked('\n', f
);
3605 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3606 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3608 if (link
->dhcp_lease
) {
3609 struct in_addr address
;
3610 const char *tz
= NULL
;
3612 assert(link
->network
);
3614 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3616 fprintf(f
, "TIMEZONE=%s\n", tz
);
3618 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3620 fputs_unlocked("DHCP4_ADDRESS=", f
);
3621 serialize_in_addrs(f
, &address
, 1);
3622 fputc_unlocked('\n', f
);
3625 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3633 unlink(link
->lease_file
);
3636 struct in_addr address
;
3638 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3640 fputs_unlocked("IPV4LL_ADDRESS=", f
);
3641 serialize_in_addrs(f
, &address
, 1);
3642 fputc_unlocked('\n', f
);
3646 r
= fflush_and_check(f
);
3650 if (rename(temp_path
, link
->state_file
) < 0) {
3658 (void) unlink(link
->state_file
);
3660 (void) unlink(temp_path
);
3662 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3665 /* The serialized state in /run is no longer up-to-date. */
3666 void link_dirty(Link
*link
) {
3671 /* mark manager dirty as link is dirty */
3672 manager_dirty(link
->manager
);
3674 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3676 /* allocation errors are ignored */
3679 r
= set_put(link
->manager
->dirty_links
, link
);
3681 /* don't take another ref if the link was already dirty */
3687 /* The serialized state in /run is up-to-date */
3688 void link_clean(Link
*link
) {
3690 assert(link
->manager
);
3692 set_remove(link
->manager
->dirty_links
, link
);
3696 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3697 [LINK_STATE_PENDING
] = "pending",
3698 [LINK_STATE_ENSLAVING
] = "configuring",
3699 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3700 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3701 [LINK_STATE_CONFIGURED
] = "configured",
3702 [LINK_STATE_UNMANAGED
] = "unmanaged",
3703 [LINK_STATE_FAILED
] = "failed",
3704 [LINK_STATE_LINGER
] = "linger",
3707 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3709 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3710 [LINK_OPERSTATE_OFF
] = "off",
3711 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3712 [LINK_OPERSTATE_DORMANT
] = "dormant",
3713 [LINK_OPERSTATE_CARRIER
] = "carrier",
3714 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3715 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3718 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);