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_ipv6ll_enabled(link
))
751 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
754 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
755 !link
->dhcp4_configured
) ||
756 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
757 !link
->dhcp6_configured
) ||
758 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
759 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
762 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
, &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
->link_messages
++;
800 routing_policy_rule_purge(link
->manager
, link
);
805 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
806 _cleanup_link_unref_ Link
*link
= userdata
;
809 assert(link
->link_messages
> 0);
810 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
811 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
814 link
->link_messages
--;
816 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
819 r
= sd_netlink_message_get_errno(m
);
820 if (r
< 0 && r
!= -EEXIST
)
821 log_link_warning_errno(link
, r
, "Could not set route: %m");
823 if (link
->link_messages
== 0) {
824 log_link_debug(link
, "Routes set");
825 link
->static_configured
= true;
826 link_check_ready(link
);
832 static int link_enter_set_routes(Link
*link
) {
837 assert(link
->network
);
838 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
840 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
842 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
843 r
= route_configure(rt
, link
, route_handler
);
845 log_link_warning_errno(link
, r
, "Could not set routes: %m");
846 link_enter_failed(link
);
850 link
->link_messages
++;
853 if (link
->link_messages
== 0) {
854 link
->static_configured
= true;
855 link_check_ready(link
);
857 log_link_debug(link
, "Setting routes");
862 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
863 _cleanup_link_unref_ Link
*link
= userdata
;
868 assert(link
->ifname
);
870 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
873 r
= sd_netlink_message_get_errno(m
);
874 if (r
< 0 && r
!= -ESRCH
)
875 log_link_warning_errno(link
, r
, "Could not drop route: %m");
880 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
881 _cleanup_link_unref_ Link
*link
= userdata
;
887 assert(link
->ifname
);
888 assert(link
->link_messages
> 0);
889 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
890 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
892 link
->link_messages
--;
894 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
897 r
= sd_netlink_message_get_errno(m
);
898 if (r
< 0 && r
!= -EEXIST
)
899 log_link_warning_errno(link
, r
, "could not set address: %m");
901 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
903 if (link
->link_messages
== 0) {
904 log_link_debug(link
, "Addresses set");
905 link_enter_set_routes(link
);
911 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
912 _cleanup_link_unref_ Link
*link
= userdata
;
918 assert(link
->ifname
);
919 assert(link
->link_messages
> 0);
921 link
->link_messages
--;
923 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
926 r
= sd_netlink_message_get_errno(m
);
927 if (r
< 0 && r
!= -EEXIST
)
928 log_link_warning_errno(link
, r
, "could not set address label: %m");
930 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
932 if (link
->link_messages
== 0) {
933 log_link_debug(link
, "Addresses label set");
934 link_enter_set_routes(link
);
940 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
941 _cleanup_free_
struct in_addr
*addresses
= NULL
;
942 size_t n_addresses
= 0, n_allocated
= 0;
945 log_debug("Copying DNS server information from %s", link
->ifname
);
950 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
953 /* Only look for IPv4 addresses */
954 if (link
->network
->dns
[i
].family
!= AF_INET
)
957 ia
= link
->network
->dns
[i
].address
.in
;
959 /* Never propagate obviously borked data */
960 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
963 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
966 addresses
[n_addresses
++] = ia
;
969 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
970 const struct in_addr
*da
= NULL
;
973 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
976 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
979 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
984 if (n_addresses
<= 0)
987 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
990 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
991 _cleanup_free_
struct in_addr
*addresses
= NULL
;
992 size_t n_addresses
= 0, n_allocated
= 0;
998 log_debug("Copying NTP server information from %s", link
->ifname
);
1000 STRV_FOREACH(a
, link
->network
->ntp
) {
1003 /* Only look for IPv4 addresses */
1004 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1007 /* Never propagate obviously borked data */
1008 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1011 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1014 addresses
[n_addresses
++] = ia
;
1017 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1018 const struct in_addr
*da
= NULL
;
1021 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1024 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1027 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1032 if (n_addresses
<= 0)
1035 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1038 static int link_set_bridge_fdb(Link
*link
) {
1039 FdbEntry
*fdb_entry
;
1042 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1043 r
= fdb_entry_configure(link
, fdb_entry
);
1045 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1051 static int link_enter_set_addresses(Link
*link
) {
1052 AddressLabel
*label
;
1057 assert(link
->network
);
1058 assert(link
->state
!= _LINK_STATE_INVALID
);
1060 r
= link_set_bridge_fdb(link
);
1064 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1066 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1067 r
= address_configure(ad
, link
, address_handler
, false);
1069 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1070 link_enter_failed(link
);
1074 link
->link_messages
++;
1077 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1078 r
= address_label_configure(label
, link
, address_label_handler
, false);
1080 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1081 link_enter_failed(link
);
1085 link
->link_messages
++;
1088 /* now that we can figure out a default address for the dhcp server,
1090 if (link_dhcp4_server_enabled(link
)) {
1092 Link
*uplink
= NULL
;
1093 bool acquired_uplink
= false;
1095 address
= link_find_dhcp_server_address(link
);
1097 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1098 link_enter_failed(link
);
1102 /* use the server address' subnet as the pool */
1103 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1104 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1109 r = sd_dhcp_server_set_router(link->dhcp_server,
1110 &main_address->in_addr.in);
1115 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1116 r
= sd_dhcp_server_set_max_lease_time(
1118 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1123 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1124 r
= sd_dhcp_server_set_default_lease_time(
1126 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1131 if (link
->network
->dhcp_server_emit_dns
) {
1133 if (link
->network
->n_dhcp_server_dns
> 0)
1134 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1136 uplink
= manager_find_uplink(link
->manager
, link
);
1137 acquired_uplink
= true;
1140 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1143 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1146 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1150 if (link
->network
->dhcp_server_emit_ntp
) {
1152 if (link
->network
->n_dhcp_server_ntp
> 0)
1153 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1155 if (!acquired_uplink
)
1156 uplink
= manager_find_uplink(link
->manager
, link
);
1159 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1162 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1166 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1169 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1171 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1175 if (link
->network
->dhcp_server_emit_timezone
) {
1176 _cleanup_free_
char *buffer
= NULL
;
1177 const char *tz
= NULL
;
1179 if (link
->network
->dhcp_server_timezone
)
1180 tz
= link
->network
->dhcp_server_timezone
;
1182 r
= get_timezone(&buffer
);
1184 log_warning_errno(r
, "Failed to determine timezone: %m");
1190 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1196 r
= sd_dhcp_server_start(link
->dhcp_server
);
1198 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1200 link_enter_failed(link
);
1205 log_link_debug(link
, "Offering DHCPv4 leases");
1208 if (link
->link_messages
== 0)
1209 link_enter_set_routes(link
);
1211 log_link_debug(link
, "Setting addresses");
1216 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1217 _cleanup_link_unref_ Link
*link
= userdata
;
1222 assert(link
->ifname
);
1224 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1227 r
= sd_netlink_message_get_errno(m
);
1228 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1229 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1234 static int link_set_bridge_vlan(Link
*link
) {
1237 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1239 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1244 static int link_set_proxy_arp(Link
*link
) {
1245 const char *p
= NULL
;
1248 if (!link_proxy_arp_enabled(link
))
1251 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1253 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1255 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1260 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1261 _cleanup_link_unref_ Link
*link
= userdata
;
1264 log_link_debug(link
, "Set link");
1266 r
= sd_netlink_message_get_errno(m
);
1267 if (r
< 0 && r
!= -EEXIST
) {
1268 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1269 link_enter_failed(link
);
1276 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1277 _cleanup_link_unref_ Link
*link
= userdata
;
1282 assert(link
->ifname
);
1284 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1287 r
= sd_netlink_message_get_errno(m
);
1289 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1294 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1295 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1299 assert(link
->manager
);
1300 assert(link
->manager
->rtnl
);
1302 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1304 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1306 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1308 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1310 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1312 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1314 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1321 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1322 _cleanup_link_unref_ Link
*link
= userdata
;
1327 assert(link
->ifname
);
1329 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1332 r
= sd_netlink_message_get_errno(m
);
1334 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1339 static int link_set_flags(Link
*link
) {
1340 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1341 unsigned ifi_change
= 0;
1342 unsigned ifi_flags
= 0;
1346 assert(link
->manager
);
1347 assert(link
->manager
->rtnl
);
1349 if (link
->flags
& IFF_LOOPBACK
)
1355 if (link
->network
->arp
< 0)
1358 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1360 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1362 if (link
->network
->arp
>= 0) {
1363 ifi_change
|= IFF_NOARP
;
1364 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1367 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1369 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1371 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1373 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1380 static int link_set_bridge(Link
*link
) {
1381 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1385 assert(link
->network
);
1387 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1389 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1391 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1393 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1395 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1397 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1399 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1401 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1403 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1405 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1407 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1409 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1411 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1413 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1415 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1417 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1419 if (link
->network
->cost
!= 0) {
1420 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1422 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1424 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1425 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1427 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1430 r
= sd_netlink_message_close_container(req
);
1432 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1434 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1436 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1443 static int link_bond_set(Link
*link
) {
1444 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1448 assert(link
->network
);
1450 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1452 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1454 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1456 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1458 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1460 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1462 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1464 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1466 if (link
->network
->active_slave
) {
1467 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1469 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1472 if (link
->network
->primary_slave
) {
1473 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1475 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1478 r
= sd_netlink_message_close_container(req
);
1480 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1482 r
= sd_netlink_message_close_container(req
);
1484 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1486 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1488 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1495 static int link_lldp_save(Link
*link
) {
1496 _cleanup_free_
char *temp_path
= NULL
;
1497 _cleanup_fclose_
FILE *f
= NULL
;
1498 sd_lldp_neighbor
**l
= NULL
;
1502 assert(link
->lldp_file
);
1505 (void) unlink(link
->lldp_file
);
1509 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1513 (void) unlink(link
->lldp_file
);
1519 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1523 fchmod(fileno(f
), 0644);
1525 for (i
= 0; i
< n
; i
++) {
1530 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1535 (void) fwrite(&u
, 1, sizeof(u
), f
);
1536 (void) fwrite(p
, 1, sz
, f
);
1539 r
= fflush_and_check(f
);
1543 if (rename(temp_path
, link
->lldp_file
) < 0) {
1550 (void) unlink(link
->lldp_file
);
1552 (void) unlink(temp_path
);
1554 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1558 for (i
= 0; i
< n
; i
++)
1559 sd_lldp_neighbor_unref(l
[i
]);
1566 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1567 Link
*link
= userdata
;
1572 (void) link_lldp_save(link
);
1574 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1575 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1577 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1579 r
= link_lldp_emit_start(link
);
1581 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1585 static int link_acquire_ipv6_conf(Link
*link
) {
1590 if (link_dhcp6_enabled(link
)) {
1591 assert(link
->dhcp6_client
);
1592 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1594 /* start DHCPv6 client in stateless mode */
1595 r
= dhcp6_request_address(link
, true);
1596 if (r
< 0 && r
!= -EBUSY
)
1597 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1599 log_link_debug(link
, "Acquiring DHCPv6 lease");
1602 if (link_ipv6_accept_ra_enabled(link
)) {
1603 assert(link
->ndisc
);
1605 log_link_debug(link
, "Discovering IPv6 routers");
1607 r
= sd_ndisc_start(link
->ndisc
);
1608 if (r
< 0 && r
!= -EBUSY
)
1609 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1612 if (link_radv_enabled(link
)) {
1614 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1616 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1618 r
= sd_radv_start(link
->radv
);
1619 if (r
< 0 && r
!= -EBUSY
)
1620 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1626 static int link_acquire_ipv4_conf(Link
*link
) {
1630 assert(link
->network
);
1631 assert(link
->manager
);
1632 assert(link
->manager
->event
);
1634 if (link_ipv4ll_enabled(link
)) {
1635 assert(link
->ipv4ll
);
1637 log_link_debug(link
, "Acquiring IPv4 link-local address");
1639 r
= sd_ipv4ll_start(link
->ipv4ll
);
1641 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1644 if (link_dhcp4_enabled(link
)) {
1645 assert(link
->dhcp_client
);
1647 log_link_debug(link
, "Acquiring DHCPv4 lease");
1649 r
= sd_dhcp_client_start(link
->dhcp_client
);
1651 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1657 static int link_acquire_conf(Link
*link
) {
1662 r
= link_acquire_ipv4_conf(link
);
1666 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1667 r
= link_acquire_ipv6_conf(link
);
1672 if (link_lldp_emit_enabled(link
)) {
1673 r
= link_lldp_emit_start(link
);
1675 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1681 bool link_has_carrier(Link
*link
) {
1682 /* see Documentation/networking/operstates.txt in the kernel sources */
1684 if (link
->kernel_operstate
== IF_OPER_UP
)
1687 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1688 /* operstate may not be implemented, so fall back to flags */
1689 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1695 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1696 _cleanup_link_unref_ Link
*link
= userdata
;
1701 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1704 r
= sd_netlink_message_get_errno(m
);
1706 /* we warn but don't fail the link, as it may be
1708 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1713 int link_up(Link
*link
) {
1714 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1715 uint8_t ipv6ll_mode
;
1719 assert(link
->network
);
1720 assert(link
->manager
);
1721 assert(link
->manager
->rtnl
);
1723 log_link_debug(link
, "Bringing link up");
1725 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1727 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1729 /* set it free if not enslaved with networkd */
1730 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1731 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1733 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1736 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1738 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1740 if (link
->network
->mac
) {
1741 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1743 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1746 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1747 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1748 (void) link_enable_ipv6(link
);
1750 if (link
->network
->mtu
) {
1751 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1752 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1753 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1755 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1756 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1758 link
->network
->mtu
= IPV6_MIN_MTU
;
1761 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1763 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1766 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1768 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1770 if (link_ipv6_enabled(link
)) {
1771 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1772 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1774 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1776 if (!link_ipv6ll_enabled(link
))
1777 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1779 const char *p
= NULL
;
1780 _cleanup_free_
char *stable_secret
= NULL
;
1782 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1783 r
= read_one_line_file(p
, &stable_secret
);
1786 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1788 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1790 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1792 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1794 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1795 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1797 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1800 r
= sd_netlink_message_close_container(req
);
1802 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1805 r
= sd_netlink_message_close_container(req
);
1807 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1809 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1811 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1818 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1819 _cleanup_link_unref_ Link
*link
= userdata
;
1824 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1827 r
= sd_netlink_message_get_errno(m
);
1829 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1834 int link_down(Link
*link
) {
1835 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1839 assert(link
->manager
);
1840 assert(link
->manager
->rtnl
);
1842 log_link_debug(link
, "Bringing link down");
1844 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1845 RTM_SETLINK
, link
->ifindex
);
1847 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1849 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1851 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1853 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1855 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1862 static int link_up_can(Link
*link
) {
1863 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1868 log_link_debug(link
, "Bringing CAN link up");
1870 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1872 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1874 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1876 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1878 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1880 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1887 static int link_handle_bound_to_list(Link
*link
) {
1891 bool required_up
= false;
1892 bool link_is_up
= false;
1896 if (hashmap_isempty(link
->bound_to_links
))
1899 if (link
->flags
& IFF_UP
)
1902 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1903 if (link_has_carrier(l
)) {
1908 if (!required_up
&& link_is_up
) {
1909 r
= link_down(link
);
1912 } else if (required_up
&& !link_is_up
) {
1921 static int link_handle_bound_by_list(Link
*link
) {
1928 if (hashmap_isempty(link
->bound_by_links
))
1931 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1932 r
= link_handle_bound_to_list(l
);
1940 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1946 if (link
== carrier
)
1949 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1952 r
= hashmap_ensure_allocated(h
, NULL
);
1956 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1963 static int link_new_bound_by_list(Link
*link
) {
1968 bool list_updated
= false;
1971 assert(link
->manager
);
1975 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1976 if (!carrier
->network
)
1979 if (strv_isempty(carrier
->network
->bind_carrier
))
1982 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1983 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1987 list_updated
= true;
1994 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1995 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1999 link_dirty(carrier
);
2005 static int link_new_bound_to_list(Link
*link
) {
2010 bool list_updated
= false;
2013 assert(link
->manager
);
2018 if (strv_isempty(link
->network
->bind_carrier
))
2023 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2024 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2025 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2029 list_updated
= true;
2036 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2037 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2041 link_dirty(carrier
);
2047 static int link_new_carrier_maps(Link
*link
) {
2050 r
= link_new_bound_by_list(link
);
2054 r
= link_handle_bound_by_list(link
);
2058 r
= link_new_bound_to_list(link
);
2062 r
= link_handle_bound_to_list(link
);
2069 static void link_free_bound_to_list(Link
*link
) {
2073 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2074 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2076 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2077 link_dirty(bound_to
);
2083 static void link_free_bound_by_list(Link
*link
) {
2087 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2088 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2090 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2091 link_dirty(bound_by
);
2092 link_handle_bound_to_list(bound_by
);
2099 static void link_free_carrier_maps(Link
*link
) {
2100 bool list_updated
= false;
2104 if (!hashmap_isempty(link
->bound_to_links
)) {
2105 link_free_bound_to_list(link
);
2106 list_updated
= true;
2109 if (!hashmap_isempty(link
->bound_by_links
)) {
2110 link_free_bound_by_list(link
);
2111 list_updated
= true;
2120 void link_drop(Link
*link
) {
2121 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2124 link_set_state(link
, LINK_STATE_LINGER
);
2126 link_free_carrier_maps(link
);
2128 log_link_debug(link
, "Link removed");
2130 (void)unlink(link
->state_file
);
2136 static int link_joined(Link
*link
) {
2140 assert(link
->network
);
2142 if (!hashmap_isempty(link
->bound_to_links
)) {
2143 r
= link_handle_bound_to_list(link
);
2146 } else if (!(link
->flags
& IFF_UP
)) {
2149 link_enter_failed(link
);
2154 if (link
->network
->bridge
) {
2155 r
= link_set_bridge(link
);
2157 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2160 if (link
->network
->bond
) {
2161 r
= link_bond_set(link
);
2163 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2166 if (link
->network
->use_br_vlan
&&
2167 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2168 r
= link_set_bridge_vlan(link
);
2170 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2173 /* Skip setting up addresses until it gets carrier,
2174 or it would try to set addresses twice,
2175 which is bad for non-idempotent steps. */
2176 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2179 return link_enter_set_addresses(link
);
2182 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2183 _cleanup_link_unref_ Link
*link
= userdata
;
2187 assert(link
->network
);
2191 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2194 r
= sd_netlink_message_get_errno(m
);
2195 if (r
< 0 && r
!= -EEXIST
) {
2196 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2197 link_enter_failed(link
);
2200 log_link_debug(link
, "Joined netdev");
2202 if (link
->enslaving
<= 0)
2208 static int link_enter_join_netdev(Link
*link
) {
2214 assert(link
->network
);
2215 assert(link
->state
== LINK_STATE_PENDING
);
2217 link_set_state(link
, LINK_STATE_ENSLAVING
);
2221 if (!link
->network
->bridge
&&
2222 !link
->network
->bond
&&
2223 !link
->network
->vrf
&&
2224 hashmap_isempty(link
->network
->stacked_netdevs
))
2225 return link_joined(link
);
2227 if (link
->network
->bond
) {
2228 log_struct(LOG_DEBUG
,
2229 LOG_LINK_INTERFACE(link
),
2230 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2231 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2234 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2236 log_struct_errno(LOG_WARNING
, r
,
2237 LOG_LINK_INTERFACE(link
),
2238 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2239 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2242 link_enter_failed(link
);
2249 if (link
->network
->bridge
) {
2250 log_struct(LOG_DEBUG
,
2251 LOG_LINK_INTERFACE(link
),
2252 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2253 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2256 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2258 log_struct_errno(LOG_WARNING
, r
,
2259 LOG_LINK_INTERFACE(link
),
2260 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2261 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2263 link_enter_failed(link
);
2270 if (link
->network
->vrf
) {
2271 log_struct(LOG_DEBUG
,
2272 LOG_LINK_INTERFACE(link
),
2273 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2274 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2276 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2278 log_struct_errno(LOG_WARNING
, r
,
2279 LOG_LINK_INTERFACE(link
),
2280 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2281 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2283 link_enter_failed(link
);
2290 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2292 log_struct(LOG_DEBUG
,
2293 LOG_LINK_INTERFACE(link
),
2294 LOG_NETDEV_INTERFACE(netdev
),
2295 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2298 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2300 log_struct_errno(LOG_WARNING
, r
,
2301 LOG_LINK_INTERFACE(link
),
2302 LOG_NETDEV_INTERFACE(netdev
),
2303 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2305 link_enter_failed(link
);
2315 static int link_set_ipv4_forward(Link
*link
) {
2318 if (!link_ipv4_forward_enabled(link
))
2321 /* We propagate the forwarding flag from one interface to the
2322 * global setting one way. This means: as long as at least one
2323 * interface was configured at any time that had IP forwarding
2324 * enabled the setting will stay on for good. We do this
2325 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2326 * somewhat in sync (see below). */
2328 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2330 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2335 static int link_set_ipv6_forward(Link
*link
) {
2338 if (!link_ipv6_forward_enabled(link
))
2341 /* On Linux, the IPv6 stack does not know a per-interface
2342 * packet forwarding setting: either packet forwarding is on
2343 * for all, or off for all. We hence don't bother with a
2344 * per-interface setting, but simply propagate the interface
2345 * flag, if it is set, to the global flag, one-way. Note that
2346 * while IPv4 would allow a per-interface flag, we expose the
2347 * same behaviour there and also propagate the setting from
2348 * one to all, to keep things simple (see above). */
2350 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2352 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2357 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2358 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2359 IPv6PrivacyExtensions s
;
2360 const char *p
= NULL
;
2363 s
= link_ipv6_privacy_extensions(link
);
2367 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2368 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2370 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2372 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2377 static int link_set_ipv6_accept_ra(Link
*link
) {
2378 const char *p
= NULL
;
2381 /* Make this a NOP if IPv6 is not available */
2382 if (!socket_ipv6_is_supported())
2385 if (link
->flags
& IFF_LOOPBACK
)
2391 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2393 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2394 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2396 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2401 static int link_set_ipv6_dad_transmits(Link
*link
) {
2402 char buf
[DECIMAL_STR_MAX(int) + 1];
2403 const char *p
= NULL
;
2406 /* Make this a NOP if IPv6 is not available */
2407 if (!socket_ipv6_is_supported())
2410 if (link
->flags
& IFF_LOOPBACK
)
2416 if (link
->network
->ipv6_dad_transmits
< 0)
2419 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2420 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2422 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2424 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2429 static int link_set_ipv6_hop_limit(Link
*link
) {
2430 char buf
[DECIMAL_STR_MAX(int) + 1];
2431 const char *p
= NULL
;
2434 /* Make this a NOP if IPv6 is not available */
2435 if (!socket_ipv6_is_supported())
2438 if (link
->flags
& IFF_LOOPBACK
)
2444 if (link
->network
->ipv6_hop_limit
< 0)
2447 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2448 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2450 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2452 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2457 static int link_drop_foreign_config(Link
*link
) {
2463 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2464 /* we consider IPv6LL addresses to be managed by the kernel */
2465 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2468 r
= address_remove(address
, link
, link_address_remove_handler
);
2473 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2474 /* do not touch routes managed by the kernel */
2475 if (route
->protocol
== RTPROT_KERNEL
)
2478 r
= route_remove(route
, link
, link_route_remove_handler
);
2486 static int link_drop_config(Link
*link
) {
2487 Address
*address
, *pool_address
;
2492 SET_FOREACH(address
, link
->addresses
, i
) {
2493 /* we consider IPv6LL addresses to be managed by the kernel */
2494 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2497 r
= address_remove(address
, link
, link_address_remove_handler
);
2501 /* If this address came from an address pool, clean up the pool */
2502 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2503 if (address_equal(address
, pool_address
)) {
2504 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2505 address_free(pool_address
);
2511 SET_FOREACH(route
, link
->routes
, i
) {
2512 /* do not touch routes managed by the kernel */
2513 if (route
->protocol
== RTPROT_KERNEL
)
2516 r
= route_remove(route
, link
, link_route_remove_handler
);
2526 static int link_update_lldp(Link
*link
) {
2534 if (link
->flags
& IFF_UP
) {
2535 r
= sd_lldp_start(link
->lldp
);
2537 log_link_debug(link
, "Started LLDP.");
2539 r
= sd_lldp_stop(link
->lldp
);
2541 log_link_debug(link
, "Stopped LLDP.");
2547 static int link_configure(Link
*link
) {
2551 assert(link
->network
);
2552 assert(link
->state
== LINK_STATE_PENDING
);
2554 if (streq_ptr(link
->kind
, "vcan")) {
2556 if (!(link
->flags
& IFF_UP
)) {
2557 r
= link_up_can(link
);
2559 link_enter_failed(link
);
2567 /* Drop foreign config, but ignore loopback or critical devices.
2568 * We do not want to remove loopback address or addresses used for root NFS. */
2569 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2570 r
= link_drop_foreign_config(link
);
2575 r
= link_set_proxy_arp(link
);
2579 r
= ipv6_proxy_ndp_addresses_configure(link
);
2583 r
= link_set_ipv4_forward(link
);
2587 r
= link_set_ipv6_forward(link
);
2591 r
= link_set_ipv6_privacy_extensions(link
);
2595 r
= link_set_ipv6_accept_ra(link
);
2599 r
= link_set_ipv6_dad_transmits(link
);
2603 r
= link_set_ipv6_hop_limit(link
);
2607 r
= link_set_flags(link
);
2611 if (link_ipv4ll_enabled(link
)) {
2612 r
= ipv4ll_configure(link
);
2617 if (link_dhcp4_enabled(link
)) {
2618 r
= dhcp4_configure(link
);
2623 if (link_dhcp4_server_enabled(link
)) {
2624 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2628 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2633 if (link_dhcp6_enabled(link
) ||
2634 link_ipv6_accept_ra_enabled(link
)) {
2635 r
= dhcp6_configure(link
);
2640 if (link_ipv6_accept_ra_enabled(link
)) {
2641 r
= ndisc_configure(link
);
2646 if (link_radv_enabled(link
)) {
2647 r
= radv_configure(link
);
2652 if (link_lldp_rx_enabled(link
)) {
2653 r
= sd_lldp_new(&link
->lldp
);
2657 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2661 r
= sd_lldp_match_capabilities(link
->lldp
,
2662 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2663 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2664 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2668 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2672 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2676 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2680 r
= link_update_lldp(link
);
2685 (void) link_set_routing_policy_rule(link
);
2687 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2688 r
= link_acquire_conf(link
);
2693 return link_enter_join_netdev(link
);
2696 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2698 _cleanup_link_unref_ Link
*link
= userdata
;
2703 assert(link
->ifname
);
2704 assert(link
->manager
);
2706 if (link
->state
!= LINK_STATE_PENDING
)
2709 log_link_debug(link
, "Link state is up-to-date");
2711 r
= link_new_bound_by_list(link
);
2715 r
= link_handle_bound_by_list(link
);
2719 if (!link
->network
) {
2720 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2721 &link
->mac
, &network
);
2723 link_enter_unmanaged(link
);
2725 } else if (r
== 0 && network
->unmanaged
) {
2726 link_enter_unmanaged(link
);
2731 if (link
->flags
& IFF_LOOPBACK
) {
2732 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2733 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2735 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2736 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2738 if (network
->dhcp_server
)
2739 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2742 r
= network_apply(network
, link
);
2747 r
= link_new_bound_to_list(link
);
2751 r
= link_configure(link
);
2758 int link_initialized(Link
*link
, struct udev_device
*device
) {
2759 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2763 assert(link
->manager
);
2764 assert(link
->manager
->rtnl
);
2767 if (link
->state
!= LINK_STATE_PENDING
)
2770 if (link
->udev_device
)
2773 log_link_debug(link
, "udev initialized link");
2775 link
->udev_device
= udev_device_ref(device
);
2777 /* udev has initialized the link, but we don't know if we have yet
2778 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2779 * when it returns we know that the pending NEWLINKs have already been
2780 * processed and that we are up-to-date */
2782 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2787 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2788 link_initialized_and_synced
, link
, 0, NULL
);
2797 static int link_load(Link
*link
) {
2798 _cleanup_free_
char *network_file
= NULL
,
2801 *dhcp4_address
= NULL
,
2802 *ipv4ll_address
= NULL
;
2803 union in_addr_union address
;
2804 union in_addr_union route_dst
;
2810 r
= parse_env_file(link
->state_file
, NEWLINE
,
2811 "NETWORK_FILE", &network_file
,
2812 "ADDRESSES", &addresses
,
2814 "DHCP4_ADDRESS", &dhcp4_address
,
2815 "IPV4LL_ADDRESS", &ipv4ll_address
,
2817 if (r
< 0 && r
!= -ENOENT
)
2818 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2825 suffix
= strrchr(network_file
, '.');
2827 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2828 goto network_file_fail
;
2832 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2834 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2835 goto network_file_fail
;
2838 r
= network_apply(network
, link
);
2840 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2849 _cleanup_free_
char *address_str
= NULL
;
2850 char *prefixlen_str
;
2852 unsigned char prefixlen
;
2854 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2856 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2862 prefixlen_str
= strchr(address_str
, '/');
2863 if (!prefixlen_str
) {
2864 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2868 *prefixlen_str
++ = '\0';
2870 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2872 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2876 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2878 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2882 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2884 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2893 _cleanup_free_
char *route_str
= NULL
;
2894 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2896 char *prefixlen_str
;
2898 unsigned char prefixlen
, tos
, table
;
2901 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2903 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2909 prefixlen_str
= strchr(route_str
, '/');
2910 if (!prefixlen_str
) {
2911 log_link_debug(link
, "Failed to parse route %s", route_str
);
2915 *prefixlen_str
++ = '\0';
2917 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2919 log_link_debug(link
,
2920 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2925 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2927 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2931 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2933 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2935 if (lifetime
!= USEC_INFINITY
) {
2936 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2937 0, route_expire_handler
, route
);
2939 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2942 route
->lifetime
= lifetime
;
2943 sd_event_source_unref(route
->expire
);
2944 route
->expire
= expire
;
2949 if (dhcp4_address
) {
2950 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2952 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2953 goto dhcp4_address_fail
;
2956 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
2958 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2960 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2962 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2967 if (ipv4ll_address
) {
2968 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2970 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2971 goto ipv4ll_address_fail
;
2974 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2976 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2978 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2980 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2983 ipv4ll_address_fail
:
2988 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2990 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2991 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2999 r
= link_new(m
, message
, ret
);
3005 log_link_debug(link
, "Link %d added", link
->ifindex
);
3007 r
= link_load(link
);
3011 if (detect_container() <= 0) {
3012 /* not in a container, udev will be around */
3013 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3014 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3016 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3020 if (udev_device_get_is_initialized(device
) <= 0) {
3022 log_link_debug(link
, "link pending udev initialization...");
3026 r
= link_initialized(link
, device
);
3030 /* we are calling a callback directly, so must take a ref */
3033 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3040 link_enter_failed(link
);
3044 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3049 log_link_info(link
, "Gained IPv6LL");
3051 link
->ipv6ll_address
= *address
;
3052 link_check_ready(link
);
3054 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3055 r
= link_acquire_ipv6_conf(link
);
3057 link_enter_failed(link
);
3065 static int link_carrier_gained(Link
*link
) {
3070 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3071 r
= link_acquire_conf(link
);
3073 link_enter_failed(link
);
3077 r
= link_enter_set_addresses(link
);
3082 r
= link_handle_bound_by_list(link
);
3089 static int link_carrier_lost(Link
*link
) {
3094 r
= link_stop_clients(link
);
3096 link_enter_failed(link
);
3100 if (link_dhcp4_server_enabled(link
))
3101 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3103 r
= link_drop_config(link
);
3107 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3108 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3109 r
= link_drop_foreign_config(link
);
3114 r
= link_handle_bound_by_list(link
);
3121 int link_carrier_reset(Link
*link
) {
3126 if (link_has_carrier(link
)) {
3127 r
= link_carrier_lost(link
);
3131 r
= link_carrier_gained(link
);
3135 log_link_info(link
, "Reset carrier");
3141 int link_update(Link
*link
, sd_netlink_message
*m
) {
3142 struct ether_addr mac
;
3145 bool had_carrier
, carrier_gained
, carrier_lost
;
3149 assert(link
->ifname
);
3152 if (link
->state
== LINK_STATE_LINGER
) {
3154 log_link_info(link
, "Link readded");
3155 link_set_state(link
, LINK_STATE_ENSLAVING
);
3157 r
= link_new_carrier_maps(link
);
3162 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3163 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3164 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3166 link_free_carrier_maps(link
);
3168 r
= free_and_strdup(&link
->ifname
, ifname
);
3172 r
= link_new_carrier_maps(link
);
3177 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3178 if (r
>= 0 && mtu
> 0) {
3180 if (!link
->original_mtu
) {
3181 link
->original_mtu
= mtu
;
3182 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3185 if (link
->dhcp_client
) {
3186 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3189 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3195 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3197 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3201 /* The kernel may broadcast NEWLINK messages without the MAC address
3202 set, simply ignore them. */
3203 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3205 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3208 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3211 log_link_debug(link
, "MAC address: "
3212 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3213 mac
.ether_addr_octet
[0],
3214 mac
.ether_addr_octet
[1],
3215 mac
.ether_addr_octet
[2],
3216 mac
.ether_addr_octet
[3],
3217 mac
.ether_addr_octet
[4],
3218 mac
.ether_addr_octet
[5]);
3221 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3223 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3226 if (link
->dhcp_client
) {
3227 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3228 (const uint8_t *) &link
->mac
,
3232 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3234 switch (link
->network
->dhcp_client_identifier
) {
3235 case DHCP_CLIENT_ID_DUID
: {
3236 const DUID
*duid
= link_duid(link
);
3238 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3239 link
->network
->iaid
,
3241 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3242 duid
->raw_data_len
);
3244 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3247 case DHCP_CLIENT_ID_MAC
:
3248 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3250 (const uint8_t *)&link
->mac
,
3253 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3256 assert_not_reached("Unknown client identifier type.");
3260 if (link
->dhcp6_client
) {
3261 const DUID
* duid
= link_duid(link
);
3263 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3264 (const uint8_t *) &link
->mac
,
3268 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3270 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3271 link
->network
->iaid
);
3273 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3275 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3277 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3278 duid
->raw_data_len
);
3280 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3284 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3286 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3291 had_carrier
= link_has_carrier(link
);
3293 r
= link_update_flags(link
, m
);
3297 r
= link_update_lldp(link
);
3301 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3302 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3304 if (carrier_gained
) {
3305 log_link_info(link
, "Gained carrier");
3307 r
= link_carrier_gained(link
);
3310 } else if (carrier_lost
) {
3311 log_link_info(link
, "Lost carrier");
3313 r
= link_carrier_lost(link
);
3321 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3329 if (hashmap_isempty(h
))
3332 fputs_unlocked(prefix
, f
);
3333 HASHMAP_FOREACH(link
, h
, i
) {
3335 fputc_unlocked(' ', f
);
3337 fprintf(f
, "%i", link
->ifindex
);
3341 fputc_unlocked('\n', f
);
3344 int link_save(Link
*link
) {
3345 _cleanup_free_
char *temp_path
= NULL
;
3346 _cleanup_fclose_
FILE *f
= NULL
;
3347 const char *admin_state
, *oper_state
;
3354 assert(link
->state_file
);
3355 assert(link
->lease_file
);
3356 assert(link
->manager
);
3358 if (link
->state
== LINK_STATE_LINGER
) {
3359 unlink(link
->state_file
);
3363 link_lldp_save(link
);
3365 admin_state
= link_state_to_string(link
->state
);
3366 assert(admin_state
);
3368 oper_state
= link_operstate_to_string(link
->operstate
);
3371 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3375 (void) fchmod(fileno(f
), 0644);
3378 "# This is private data. Do not parse.\n"
3381 admin_state
, oper_state
);
3383 if (link
->network
) {
3385 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3386 const char *dhcp_domainname
= NULL
;
3387 char **dhcp6_domains
= NULL
;
3388 char **dhcp_domains
= NULL
;
3391 if (link
->dhcp6_client
) {
3392 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3393 if (r
< 0 && r
!= -ENOMSG
)
3394 log_link_debug(link
, "No DHCPv6 lease");
3397 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3399 fputs_unlocked("DNS=", f
);
3402 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3403 _cleanup_free_
char *b
= NULL
;
3405 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3406 &link
->network
->dns
[j
].address
, &b
);
3408 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3413 fputc_unlocked(' ', f
);
3414 fputs_unlocked(b
, f
);
3418 if (link
->network
->dhcp_use_dns
&&
3420 const struct in_addr
*addresses
;
3422 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3425 fputc_unlocked(' ', f
);
3426 serialize_in_addrs(f
, addresses
, r
);
3431 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3432 struct in6_addr
*in6_addrs
;
3434 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3437 fputc_unlocked(' ', f
);
3438 serialize_in6_addrs(f
, in6_addrs
, r
);
3443 /* Make sure to flush out old entries before we use the NDISC data */
3446 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3449 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3451 fputc_unlocked(' ', f
);
3453 serialize_in6_addrs(f
, &dd
->address
, 1);
3458 fputc_unlocked('\n', f
);
3460 fputs_unlocked("NTP=", f
);
3462 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3464 if (link
->network
->dhcp_use_ntp
&&
3466 const struct in_addr
*addresses
;
3468 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3471 fputc_unlocked(' ', f
);
3472 serialize_in_addrs(f
, addresses
, r
);
3477 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3478 struct in6_addr
*in6_addrs
;
3481 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3485 fputc_unlocked(' ', f
);
3486 serialize_in6_addrs(f
, in6_addrs
, r
);
3490 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3492 fputstrv(f
, hosts
, NULL
, &space
);
3495 fputc_unlocked('\n', f
);
3497 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3498 if (link
->dhcp_lease
) {
3499 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3500 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3503 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3506 fputs_unlocked("DOMAINS=", f
);
3508 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3510 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3513 if (dhcp_domainname
)
3514 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3516 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3518 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3520 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3521 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3524 fputc_unlocked('\n', f
);
3526 fputs_unlocked("ROUTE_DOMAINS=", f
);
3528 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3530 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3533 if (dhcp_domainname
)
3534 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3536 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3538 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3540 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3541 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3544 fputc_unlocked('\n', f
);
3546 fprintf(f
, "LLMNR=%s\n",
3547 resolve_support_to_string(link
->network
->llmnr
));
3548 fprintf(f
, "MDNS=%s\n",
3549 resolve_support_to_string(link
->network
->mdns
));
3551 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3552 fprintf(f
, "DNSSEC=%s\n",
3553 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3555 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3558 fputs_unlocked("DNSSEC_NTA=", f
);
3560 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3561 fputs_with_space(f
, n
, NULL
, &space
);
3562 fputc_unlocked('\n', f
);
3565 fputs_unlocked("ADDRESSES=", f
);
3567 SET_FOREACH(a
, link
->addresses
, i
) {
3568 _cleanup_free_
char *address_str
= NULL
;
3570 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3574 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3577 fputc_unlocked('\n', f
);
3579 fputs_unlocked("ROUTES=", f
);
3581 SET_FOREACH(route
, link
->routes
, i
) {
3582 _cleanup_free_
char *route_str
= NULL
;
3584 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3588 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3589 space
? " " : "", route_str
,
3590 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3594 fputc_unlocked('\n', f
);
3597 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3598 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3600 if (link
->dhcp_lease
) {
3601 struct in_addr address
;
3602 const char *tz
= NULL
;
3604 assert(link
->network
);
3606 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3608 fprintf(f
, "TIMEZONE=%s\n", tz
);
3610 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3612 fputs_unlocked("DHCP4_ADDRESS=", f
);
3613 serialize_in_addrs(f
, &address
, 1);
3614 fputc_unlocked('\n', f
);
3617 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3625 unlink(link
->lease_file
);
3628 struct in_addr address
;
3630 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3632 fputs_unlocked("IPV4LL_ADDRESS=", f
);
3633 serialize_in_addrs(f
, &address
, 1);
3634 fputc_unlocked('\n', f
);
3638 r
= fflush_and_check(f
);
3642 if (rename(temp_path
, link
->state_file
) < 0) {
3650 (void) unlink(link
->state_file
);
3652 (void) unlink(temp_path
);
3654 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3657 /* The serialized state in /run is no longer up-to-date. */
3658 void link_dirty(Link
*link
) {
3663 /* mark manager dirty as link is dirty */
3664 manager_dirty(link
->manager
);
3666 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3668 /* allocation errors are ignored */
3671 r
= set_put(link
->manager
->dirty_links
, link
);
3673 /* don't take another ref if the link was already dirty */
3679 /* The serialized state in /run is up-to-date */
3680 void link_clean(Link
*link
) {
3682 assert(link
->manager
);
3684 set_remove(link
->manager
->dirty_links
, link
);
3688 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3689 [LINK_STATE_PENDING
] = "pending",
3690 [LINK_STATE_ENSLAVING
] = "configuring",
3691 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3692 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3693 [LINK_STATE_CONFIGURED
] = "configured",
3694 [LINK_STATE_UNMANAGED
] = "unmanaged",
3695 [LINK_STATE_FAILED
] = "failed",
3696 [LINK_STATE_LINGER
] = "linger",
3699 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3701 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3702 [LINK_OPERSTATE_OFF
] = "off",
3703 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3704 [LINK_OPERSTATE_DORMANT
] = "dormant",
3705 [LINK_OPERSTATE_CARRIER
] = "carrier",
3706 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3707 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3710 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);