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>
24 #include <stdio_ext.h>
26 #include "alloc-util.h"
28 #include "dhcp-lease-internal.h"
31 #include "netlink-util.h"
32 #include "network-internal.h"
33 #include "networkd-ipv6-proxy-ndp.h"
34 #include "networkd-lldp-tx.h"
35 #include "networkd-manager.h"
36 #include "networkd-ndisc.h"
37 #include "networkd-radv.h"
38 #include "networkd-routing-policy-rule.h"
40 #include "socket-util.h"
41 #include "stdio-util.h"
42 #include "string-table.h"
43 #include "udev-util.h"
47 static bool link_dhcp6_enabled(Link
*link
) {
50 if (!socket_ipv6_is_supported())
53 if (link
->flags
& IFF_LOOPBACK
)
59 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
62 static bool link_dhcp4_enabled(Link
*link
) {
65 if (link
->flags
& IFF_LOOPBACK
)
71 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
74 static bool link_dhcp4_server_enabled(Link
*link
) {
77 if (link
->flags
& IFF_LOOPBACK
)
83 return link
->network
->dhcp_server
;
86 static bool link_ipv4ll_enabled(Link
*link
) {
89 if (link
->flags
& IFF_LOOPBACK
)
95 if (streq_ptr(link
->kind
, "wireguard"))
98 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
101 static bool link_ipv6ll_enabled(Link
*link
) {
104 if (!socket_ipv6_is_supported())
107 if (link
->flags
& IFF_LOOPBACK
)
113 if (streq_ptr(link
->kind
, "wireguard"))
116 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
119 static bool link_ipv6_enabled(Link
*link
) {
122 if (!socket_ipv6_is_supported())
125 if (link
->network
->bridge
)
128 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
129 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
132 static bool link_radv_enabled(Link
*link
) {
135 if (!link_ipv6ll_enabled(link
))
138 return link
->network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
;
141 static bool link_lldp_rx_enabled(Link
*link
) {
144 if (link
->flags
& IFF_LOOPBACK
)
147 if (link
->iftype
!= ARPHRD_ETHER
)
153 /* LLDP should be handled on bridge slaves as those have a direct
154 * connection to their peers not on the bridge master. Linux doesn't
155 * even (by default) forward lldp packets to the bridge master.*/
156 if (streq_ptr("bridge", link
->kind
))
159 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
162 static bool link_lldp_emit_enabled(Link
*link
) {
165 if (link
->flags
& IFF_LOOPBACK
)
168 if (link
->iftype
!= ARPHRD_ETHER
)
174 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
177 static bool link_ipv4_forward_enabled(Link
*link
) {
180 if (link
->flags
& IFF_LOOPBACK
)
186 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
189 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
192 static bool link_ipv6_forward_enabled(Link
*link
) {
195 if (!socket_ipv6_is_supported())
198 if (link
->flags
& IFF_LOOPBACK
)
204 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
207 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
210 static bool link_proxy_arp_enabled(Link
*link
) {
213 if (link
->flags
& IFF_LOOPBACK
)
219 if (link
->network
->proxy_arp
< 0)
225 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
228 if (!socket_ipv6_is_supported())
231 if (link
->flags
& IFF_LOOPBACK
)
237 if (!link_ipv6ll_enabled(link
))
240 /* If unset use system default (enabled if local forwarding is disabled.
241 * disabled if local forwarding is enabled).
242 * If set, ignore or enforce RA independent of local forwarding state.
244 if (link
->network
->ipv6_accept_ra
< 0)
245 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
246 return !link_ipv6_forward_enabled(link
);
247 else if (link
->network
->ipv6_accept_ra
> 0)
248 /* accept RA even if ip_forward is enabled */
255 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
258 if (!socket_ipv6_is_supported())
259 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
261 if (link
->flags
& IFF_LOOPBACK
)
262 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
265 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
267 return link
->network
->ipv6_privacy_extensions
;
270 static int link_enable_ipv6(Link
*link
) {
271 const char *p
= NULL
;
275 if (link
->flags
& IFF_LOOPBACK
)
278 disabled
= !link_ipv6_enabled(link
);
280 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
282 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
284 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
285 enable_disable(!disabled
), link
->ifname
);
287 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
292 void link_update_operstate(Link
*link
) {
293 LinkOperationalState operstate
;
296 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
297 operstate
= LINK_OPERSTATE_DORMANT
;
298 else if (link_has_carrier(link
)) {
300 uint8_t scope
= RT_SCOPE_NOWHERE
;
303 /* if we have carrier, check what addresses we have */
304 SET_FOREACH(address
, link
->addresses
, i
) {
305 if (!address_is_ready(address
))
308 if (address
->scope
< scope
)
309 scope
= address
->scope
;
312 /* for operstate we also take foreign addresses into account */
313 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
314 if (!address_is_ready(address
))
317 if (address
->scope
< scope
)
318 scope
= address
->scope
;
321 if (scope
< RT_SCOPE_SITE
)
322 /* universally accessible addresses found */
323 operstate
= LINK_OPERSTATE_ROUTABLE
;
324 else if (scope
< RT_SCOPE_HOST
)
325 /* only link or site local addresses found */
326 operstate
= LINK_OPERSTATE_DEGRADED
;
328 /* no useful addresses found */
329 operstate
= LINK_OPERSTATE_CARRIER
;
330 } else if (link
->flags
& IFF_UP
)
331 operstate
= LINK_OPERSTATE_NO_CARRIER
;
333 operstate
= LINK_OPERSTATE_OFF
;
335 if (link
->operstate
!= operstate
) {
336 link
->operstate
= operstate
;
337 link_send_changed(link
, "OperationalState", NULL
);
342 #define FLAG_STRING(string, flag, old, new) \
343 (((old ^ new) & flag) \
344 ? ((old & flag) ? (" -" string) : (" +" string)) \
347 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
348 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
354 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
356 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
358 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
360 /* if we got a message without operstate, take it to mean
361 the state was unchanged */
362 operstate
= link
->kernel_operstate
;
364 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
367 if (link
->flags
!= flags
) {
368 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",
369 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
370 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
371 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
372 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
373 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
374 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
375 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
376 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
377 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
378 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
379 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
380 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
381 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
382 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
383 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
384 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
385 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
386 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
387 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
389 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
390 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
391 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
392 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
393 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
394 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
395 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
396 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
398 /* link flags are currently at most 18 bits, let's align to
400 if (unknown_flags_added
)
402 "Unknown link flags gained: %#.5x (ignoring)",
403 unknown_flags_added
);
405 if (unknown_flags_removed
)
407 "Unknown link flags lost: %#.5x (ignoring)",
408 unknown_flags_removed
);
412 link
->kernel_operstate
= operstate
;
414 link_update_operstate(link
);
419 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
420 _cleanup_link_unref_ Link
*link
= NULL
;
422 const char *ifname
, *kind
= NULL
;
424 unsigned short iftype
;
430 /* check for link kind */
431 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
433 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
434 r
= sd_netlink_message_exit_container(message
);
439 r
= sd_netlink_message_get_type(message
, &type
);
442 else if (type
!= RTM_NEWLINK
)
445 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
448 else if (ifindex
<= 0)
451 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
455 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
459 link
= new0(Link
, 1);
464 link
->manager
= manager
;
465 link
->state
= LINK_STATE_PENDING
;
466 link
->rtnl_extended_attrs
= true;
467 link
->ifindex
= ifindex
;
468 link
->iftype
= iftype
;
469 link
->ifname
= strdup(ifname
);
474 link
->kind
= strdup(kind
);
479 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
481 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
483 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
486 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
489 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
492 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
496 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
500 r
= link_update_flags(link
, message
);
504 *ret
= TAKE_PTR(link
);
509 static void link_free(Link
*link
) {
517 while (!set_isempty(link
->addresses
))
518 address_free(set_first(link
->addresses
));
520 while (!set_isempty(link
->addresses_foreign
))
521 address_free(set_first(link
->addresses_foreign
));
523 link
->addresses
= set_free(link
->addresses
);
525 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
527 while ((address
= link
->pool_addresses
)) {
528 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
529 address_free(address
);
532 sd_dhcp_server_unref(link
->dhcp_server
);
533 sd_dhcp_client_unref(link
->dhcp_client
);
534 sd_dhcp_lease_unref(link
->dhcp_lease
);
536 link_lldp_emit_stop(link
);
538 free(link
->lease_file
);
540 sd_lldp_unref(link
->lldp
);
541 free(link
->lldp_file
);
545 sd_ipv4ll_unref(link
->ipv4ll
);
546 sd_dhcp6_client_unref(link
->dhcp6_client
);
547 sd_ndisc_unref(link
->ndisc
);
548 sd_radv_unref(link
->radv
);
551 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
557 (void)unlink(link
->state_file
);
558 free(link
->state_file
);
560 udev_device_unref(link
->udev_device
);
562 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
563 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
564 hashmap_free(link
->bound_to_links
);
566 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
567 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
568 hashmap_free(link
->bound_by_links
);
573 Link
*link_unref(Link
*link
) {
577 assert(link
->n_ref
> 0);
589 Link
*link_ref(Link
*link
) {
593 assert(link
->n_ref
> 0);
600 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
607 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
616 static void link_set_state(Link
*link
, LinkState state
) {
619 if (link
->state
== state
)
624 link_send_changed(link
, "AdministrativeState", NULL
);
627 static void link_enter_unmanaged(Link
*link
) {
630 log_link_debug(link
, "Unmanaged");
632 link_set_state(link
, LINK_STATE_UNMANAGED
);
637 static int link_stop_clients(Link
*link
) {
641 assert(link
->manager
);
642 assert(link
->manager
->event
);
644 if (link
->dhcp_client
) {
645 k
= sd_dhcp_client_stop(link
->dhcp_client
);
647 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
651 k
= sd_ipv4ll_stop(link
->ipv4ll
);
653 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
656 if (link
->dhcp6_client
) {
657 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
659 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
663 k
= sd_ndisc_stop(link
->ndisc
);
665 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
669 k
= sd_radv_stop(link
->radv
);
671 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
674 link_lldp_emit_stop(link
);
678 void link_enter_failed(Link
*link
) {
681 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
684 log_link_warning(link
, "Failed");
686 link_set_state(link
, LINK_STATE_FAILED
);
688 link_stop_clients(link
);
693 static Address
* link_find_dhcp_server_address(Link
*link
) {
697 assert(link
->network
);
699 /* The first statically configured address if there is any */
700 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
702 if (address
->family
!= AF_INET
)
705 if (in_addr_is_null(address
->family
, &address
->in_addr
))
711 /* If that didn't work, find a suitable address we got from the pool */
712 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
713 if (address
->family
!= AF_INET
)
722 static void link_enter_configured(Link
*link
) {
724 assert(link
->network
);
726 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
729 log_link_info(link
, "Configured");
731 link_set_state(link
, LINK_STATE_CONFIGURED
);
736 void link_check_ready(Link
*link
) {
742 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
748 if (!link
->static_routes_configured
)
751 if (!link
->routing_policy_rules_configured
)
754 if (link_ipv4ll_enabled(link
))
755 if (!link
->ipv4ll_address
||
759 if (!link
->network
->bridge
) {
761 if (link_ipv6ll_enabled(link
))
762 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
765 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
766 !link
->dhcp4_configured
) ||
767 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
768 !link
->dhcp6_configured
) ||
769 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
770 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
773 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
777 SET_FOREACH(a
, link
->addresses
, i
)
778 if (!address_is_ready(a
))
781 if (link
->state
!= LINK_STATE_CONFIGURED
)
782 link_enter_configured(link
);
787 static int link_set_routing_policy_rule(Link
*link
) {
788 RoutingPolicyRule
*rule
, *rrule
= NULL
;
792 assert(link
->network
);
794 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
795 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
796 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
798 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
802 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
804 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
805 link_enter_failed(link
);
809 link
->routing_policy_rule_messages
++;
812 routing_policy_rule_purge(link
->manager
, link
);
813 if (link
->routing_policy_rule_messages
== 0) {
814 link
->routing_policy_rules_configured
= true;
815 link_check_ready(link
);
817 log_link_debug(link
, "Setting routing policy rules");
822 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
823 _cleanup_link_unref_ Link
*link
= userdata
;
826 assert(link
->route_messages
> 0);
827 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
828 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
831 link
->route_messages
--;
833 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
836 r
= sd_netlink_message_get_errno(m
);
837 if (r
< 0 && r
!= -EEXIST
)
838 log_link_warning_errno(link
, r
, "Could not set route: %m");
840 if (link
->route_messages
== 0) {
841 log_link_debug(link
, "Routes set");
842 link
->static_routes_configured
= true;
843 link_check_ready(link
);
849 static int link_enter_set_routes(Link
*link
) {
854 assert(link
->network
);
855 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
857 (void) link_set_routing_policy_rule(link
);
859 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
861 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
862 r
= route_configure(rt
, link
, route_handler
);
864 log_link_warning_errno(link
, r
, "Could not set routes: %m");
865 link_enter_failed(link
);
869 link
->route_messages
++;
872 if (link
->route_messages
== 0) {
873 link
->static_routes_configured
= true;
874 link_check_ready(link
);
876 log_link_debug(link
, "Setting routes");
881 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
882 _cleanup_link_unref_ Link
*link
= userdata
;
887 assert(link
->ifname
);
889 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
892 r
= sd_netlink_message_get_errno(m
);
893 if (r
< 0 && r
!= -ESRCH
)
894 log_link_warning_errno(link
, r
, "Could not drop route: %m");
899 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
900 _cleanup_link_unref_ Link
*link
= userdata
;
906 assert(link
->ifname
);
907 assert(link
->address_messages
> 0);
908 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
909 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
911 link
->address_messages
--;
913 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
916 r
= sd_netlink_message_get_errno(m
);
917 if (r
< 0 && r
!= -EEXIST
)
918 log_link_warning_errno(link
, r
, "could not set address: %m");
920 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
922 if (link
->address_messages
== 0) {
923 log_link_debug(link
, "Addresses set");
924 link_enter_set_routes(link
);
930 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
931 _cleanup_link_unref_ Link
*link
= userdata
;
937 assert(link
->ifname
);
938 assert(link
->address_label_messages
> 0);
940 link
->address_label_messages
--;
942 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
945 r
= sd_netlink_message_get_errno(m
);
946 if (r
< 0 && r
!= -EEXIST
)
947 log_link_warning_errno(link
, r
, "could not set address label: %m");
949 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
951 if (link
->address_label_messages
== 0)
952 log_link_debug(link
, "Addresses label set");
957 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
958 _cleanup_free_
struct in_addr
*addresses
= NULL
;
959 size_t n_addresses
= 0, n_allocated
= 0;
962 log_debug("Copying DNS server information from %s", link
->ifname
);
967 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
970 /* Only look for IPv4 addresses */
971 if (link
->network
->dns
[i
].family
!= AF_INET
)
974 ia
= link
->network
->dns
[i
].address
.in
;
976 /* Never propagate obviously borked data */
977 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
980 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
983 addresses
[n_addresses
++] = ia
;
986 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
987 const struct in_addr
*da
= NULL
;
990 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
993 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
996 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1001 if (n_addresses
<= 0)
1004 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1007 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1008 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1009 size_t n_addresses
= 0, n_allocated
= 0;
1015 log_debug("Copying NTP server information from %s", link
->ifname
);
1017 STRV_FOREACH(a
, link
->network
->ntp
) {
1020 /* Only look for IPv4 addresses */
1021 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1024 /* Never propagate obviously borked data */
1025 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1028 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1031 addresses
[n_addresses
++] = ia
;
1034 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1035 const struct in_addr
*da
= NULL
;
1038 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1041 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1044 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1049 if (n_addresses
<= 0)
1052 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1055 static int link_set_bridge_fdb(Link
*link
) {
1056 FdbEntry
*fdb_entry
;
1059 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1060 r
= fdb_entry_configure(link
, fdb_entry
);
1062 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1068 static int link_enter_set_addresses(Link
*link
) {
1069 AddressLabel
*label
;
1074 assert(link
->network
);
1075 assert(link
->state
!= _LINK_STATE_INVALID
);
1077 r
= link_set_bridge_fdb(link
);
1081 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1083 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1084 r
= address_configure(ad
, link
, address_handler
, false);
1086 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1087 link_enter_failed(link
);
1091 link
->address_messages
++;
1094 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1095 r
= address_label_configure(label
, link
, address_label_handler
, false);
1097 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1098 link_enter_failed(link
);
1102 link
->address_label_messages
++;
1105 /* now that we can figure out a default address for the dhcp server,
1107 if (link_dhcp4_server_enabled(link
)) {
1109 Link
*uplink
= NULL
;
1110 bool acquired_uplink
= false;
1112 address
= link_find_dhcp_server_address(link
);
1114 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1115 link_enter_failed(link
);
1119 /* use the server address' subnet as the pool */
1120 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1121 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1126 r = sd_dhcp_server_set_router(link->dhcp_server,
1127 &main_address->in_addr.in);
1132 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1133 r
= sd_dhcp_server_set_max_lease_time(
1135 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1140 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1141 r
= sd_dhcp_server_set_default_lease_time(
1143 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1148 if (link
->network
->dhcp_server_emit_dns
) {
1150 if (link
->network
->n_dhcp_server_dns
> 0)
1151 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1153 uplink
= manager_find_uplink(link
->manager
, link
);
1154 acquired_uplink
= true;
1157 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1160 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1163 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1167 if (link
->network
->dhcp_server_emit_ntp
) {
1169 if (link
->network
->n_dhcp_server_ntp
> 0)
1170 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1172 if (!acquired_uplink
)
1173 uplink
= manager_find_uplink(link
->manager
, link
);
1176 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1179 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1183 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1186 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1188 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1192 if (link
->network
->dhcp_server_emit_timezone
) {
1193 _cleanup_free_
char *buffer
= NULL
;
1194 const char *tz
= NULL
;
1196 if (link
->network
->dhcp_server_timezone
)
1197 tz
= link
->network
->dhcp_server_timezone
;
1199 r
= get_timezone(&buffer
);
1201 log_warning_errno(r
, "Failed to determine timezone: %m");
1207 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1213 r
= sd_dhcp_server_start(link
->dhcp_server
);
1215 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1217 link_enter_failed(link
);
1222 log_link_debug(link
, "Offering DHCPv4 leases");
1225 if (link
->address_messages
== 0)
1226 link_enter_set_routes(link
);
1228 log_link_debug(link
, "Setting addresses");
1233 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1234 _cleanup_link_unref_ Link
*link
= userdata
;
1239 assert(link
->ifname
);
1241 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1244 r
= sd_netlink_message_get_errno(m
);
1245 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1246 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1251 static int link_set_bridge_vlan(Link
*link
) {
1254 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1256 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1261 static int link_set_proxy_arp(Link
*link
) {
1262 const char *p
= NULL
;
1265 if (!link_proxy_arp_enabled(link
))
1268 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1270 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1272 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1277 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1278 _cleanup_link_unref_ Link
*link
= userdata
;
1281 log_link_debug(link
, "Set link");
1283 r
= sd_netlink_message_get_errno(m
);
1284 if (r
< 0 && r
!= -EEXIST
) {
1285 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1286 link_enter_failed(link
);
1293 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1294 _cleanup_link_unref_ Link
*link
= userdata
;
1299 assert(link
->ifname
);
1301 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1304 r
= sd_netlink_message_get_errno(m
);
1306 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1311 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1312 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1316 assert(link
->manager
);
1317 assert(link
->manager
->rtnl
);
1319 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1321 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1323 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1325 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1327 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1329 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1331 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1333 link
->setting_mtu
= true;
1340 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1341 _cleanup_link_unref_ Link
*link
= userdata
;
1346 assert(link
->ifname
);
1348 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1351 r
= sd_netlink_message_get_errno(m
);
1353 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1358 static int link_set_flags(Link
*link
) {
1359 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1360 unsigned ifi_change
= 0;
1361 unsigned ifi_flags
= 0;
1365 assert(link
->manager
);
1366 assert(link
->manager
->rtnl
);
1368 if (link
->flags
& IFF_LOOPBACK
)
1374 if (link
->network
->arp
< 0)
1377 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1379 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1381 if (link
->network
->arp
>= 0) {
1382 ifi_change
|= IFF_NOARP
;
1383 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1386 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1388 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1390 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1392 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1399 static int link_set_bridge(Link
*link
) {
1400 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1404 assert(link
->network
);
1406 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1408 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1410 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1412 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1414 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1416 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1418 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1420 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1422 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1424 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1426 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1428 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1430 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1432 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1434 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1436 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1438 if (link
->network
->cost
!= 0) {
1439 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1441 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1443 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1444 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1446 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1449 r
= sd_netlink_message_close_container(req
);
1451 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1453 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1455 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1462 static int link_bond_set(Link
*link
) {
1463 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1467 assert(link
->network
);
1469 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1471 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1473 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1475 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1477 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1479 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1481 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1483 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1485 if (link
->network
->active_slave
) {
1486 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1488 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1491 if (link
->network
->primary_slave
) {
1492 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1494 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1497 r
= sd_netlink_message_close_container(req
);
1499 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1501 r
= sd_netlink_message_close_container(req
);
1503 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1505 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1507 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1514 static int link_lldp_save(Link
*link
) {
1515 _cleanup_free_
char *temp_path
= NULL
;
1516 _cleanup_fclose_
FILE *f
= NULL
;
1517 sd_lldp_neighbor
**l
= NULL
;
1521 assert(link
->lldp_file
);
1524 (void) unlink(link
->lldp_file
);
1528 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1532 (void) unlink(link
->lldp_file
);
1538 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1542 fchmod(fileno(f
), 0644);
1544 for (i
= 0; i
< n
; i
++) {
1549 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1554 (void) fwrite(&u
, 1, sizeof(u
), f
);
1555 (void) fwrite(p
, 1, sz
, f
);
1558 r
= fflush_and_check(f
);
1562 if (rename(temp_path
, link
->lldp_file
) < 0) {
1569 (void) unlink(link
->lldp_file
);
1571 (void) unlink(temp_path
);
1573 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1577 for (i
= 0; i
< n
; i
++)
1578 sd_lldp_neighbor_unref(l
[i
]);
1585 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1586 Link
*link
= userdata
;
1591 (void) link_lldp_save(link
);
1593 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1594 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1596 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1598 r
= link_lldp_emit_start(link
);
1600 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1604 static int link_acquire_ipv6_conf(Link
*link
) {
1609 if (link_dhcp6_enabled(link
)) {
1610 assert(link
->dhcp6_client
);
1611 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1613 /* start DHCPv6 client in stateless mode */
1614 r
= dhcp6_request_address(link
, true);
1615 if (r
< 0 && r
!= -EBUSY
)
1616 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1618 log_link_debug(link
, "Acquiring DHCPv6 lease");
1621 if (link_ipv6_accept_ra_enabled(link
)) {
1622 assert(link
->ndisc
);
1624 log_link_debug(link
, "Discovering IPv6 routers");
1626 r
= sd_ndisc_start(link
->ndisc
);
1627 if (r
< 0 && r
!= -EBUSY
)
1628 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1631 if (link_radv_enabled(link
)) {
1633 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1635 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1637 r
= sd_radv_start(link
->radv
);
1638 if (r
< 0 && r
!= -EBUSY
)
1639 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1645 static int link_acquire_ipv4_conf(Link
*link
) {
1649 assert(link
->network
);
1650 assert(link
->manager
);
1651 assert(link
->manager
->event
);
1653 if (link_ipv4ll_enabled(link
)) {
1654 assert(link
->ipv4ll
);
1656 log_link_debug(link
, "Acquiring IPv4 link-local address");
1658 r
= sd_ipv4ll_start(link
->ipv4ll
);
1660 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1663 if (link_dhcp4_enabled(link
)) {
1664 assert(link
->dhcp_client
);
1666 log_link_debug(link
, "Acquiring DHCPv4 lease");
1668 r
= sd_dhcp_client_start(link
->dhcp_client
);
1670 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1676 static int link_acquire_conf(Link
*link
) {
1681 if (link
->setting_mtu
) {
1682 link
->setting_mtu
= false;
1686 r
= link_acquire_ipv4_conf(link
);
1690 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1691 r
= link_acquire_ipv6_conf(link
);
1696 if (link_lldp_emit_enabled(link
)) {
1697 r
= link_lldp_emit_start(link
);
1699 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1705 bool link_has_carrier(Link
*link
) {
1706 /* see Documentation/networking/operstates.txt in the kernel sources */
1708 if (link
->kernel_operstate
== IF_OPER_UP
)
1711 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1712 /* operstate may not be implemented, so fall back to flags */
1713 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1719 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1720 _cleanup_link_unref_ Link
*link
= userdata
;
1725 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1728 r
= sd_netlink_message_get_errno(m
);
1730 /* we warn but don't fail the link, as it may be
1732 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1737 int link_up(Link
*link
) {
1738 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1739 uint8_t ipv6ll_mode
;
1743 assert(link
->network
);
1744 assert(link
->manager
);
1745 assert(link
->manager
->rtnl
);
1747 log_link_debug(link
, "Bringing link up");
1749 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1751 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1753 /* set it free if not enslaved with networkd */
1754 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1755 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1757 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1760 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1762 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1764 if (link
->network
->mac
) {
1765 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1767 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1770 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1771 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1772 (void) link_enable_ipv6(link
);
1774 if (link
->network
->mtu
) {
1775 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1776 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1777 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1779 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1780 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1782 link
->network
->mtu
= IPV6_MIN_MTU
;
1785 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1787 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1790 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1792 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1794 if (link_ipv6_enabled(link
)) {
1795 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1796 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1798 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1800 if (!link_ipv6ll_enabled(link
))
1801 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1803 const char *p
= NULL
;
1804 _cleanup_free_
char *stable_secret
= NULL
;
1806 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1807 r
= read_one_line_file(p
, &stable_secret
);
1810 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1812 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1814 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1816 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1818 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1819 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1821 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1824 r
= sd_netlink_message_close_container(req
);
1826 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1829 r
= sd_netlink_message_close_container(req
);
1831 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1833 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1835 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1842 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1843 _cleanup_link_unref_ Link
*link
= userdata
;
1848 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1851 r
= sd_netlink_message_get_errno(m
);
1853 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1858 int link_down(Link
*link
) {
1859 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1863 assert(link
->manager
);
1864 assert(link
->manager
->rtnl
);
1866 log_link_debug(link
, "Bringing link down");
1868 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1869 RTM_SETLINK
, link
->ifindex
);
1871 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1873 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1875 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1877 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1879 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1886 static int link_up_can(Link
*link
) {
1887 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1892 log_link_debug(link
, "Bringing CAN link up");
1894 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1896 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1898 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1900 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1902 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1904 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1911 static int link_handle_bound_to_list(Link
*link
) {
1915 bool required_up
= false;
1916 bool link_is_up
= false;
1920 if (hashmap_isempty(link
->bound_to_links
))
1923 if (link
->flags
& IFF_UP
)
1926 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1927 if (link_has_carrier(l
)) {
1932 if (!required_up
&& link_is_up
) {
1933 r
= link_down(link
);
1936 } else if (required_up
&& !link_is_up
) {
1945 static int link_handle_bound_by_list(Link
*link
) {
1952 if (hashmap_isempty(link
->bound_by_links
))
1955 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1956 r
= link_handle_bound_to_list(l
);
1964 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1970 if (link
== carrier
)
1973 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1976 r
= hashmap_ensure_allocated(h
, NULL
);
1980 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1987 static int link_new_bound_by_list(Link
*link
) {
1992 bool list_updated
= false;
1995 assert(link
->manager
);
1999 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
2000 if (!carrier
->network
)
2003 if (strv_isempty(carrier
->network
->bind_carrier
))
2006 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2007 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2011 list_updated
= true;
2018 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2019 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2023 link_dirty(carrier
);
2029 static int link_new_bound_to_list(Link
*link
) {
2034 bool list_updated
= false;
2037 assert(link
->manager
);
2042 if (strv_isempty(link
->network
->bind_carrier
))
2047 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2048 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2049 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2053 list_updated
= true;
2060 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2061 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2065 link_dirty(carrier
);
2071 static int link_new_carrier_maps(Link
*link
) {
2074 r
= link_new_bound_by_list(link
);
2078 r
= link_handle_bound_by_list(link
);
2082 r
= link_new_bound_to_list(link
);
2086 r
= link_handle_bound_to_list(link
);
2093 static void link_free_bound_to_list(Link
*link
) {
2097 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2098 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2100 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2101 link_dirty(bound_to
);
2107 static void link_free_bound_by_list(Link
*link
) {
2111 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2112 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2114 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2115 link_dirty(bound_by
);
2116 link_handle_bound_to_list(bound_by
);
2123 static void link_free_carrier_maps(Link
*link
) {
2124 bool list_updated
= false;
2128 if (!hashmap_isempty(link
->bound_to_links
)) {
2129 link_free_bound_to_list(link
);
2130 list_updated
= true;
2133 if (!hashmap_isempty(link
->bound_by_links
)) {
2134 link_free_bound_by_list(link
);
2135 list_updated
= true;
2144 void link_drop(Link
*link
) {
2145 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2148 link_set_state(link
, LINK_STATE_LINGER
);
2150 link_free_carrier_maps(link
);
2152 log_link_debug(link
, "Link removed");
2154 (void)unlink(link
->state_file
);
2160 static int link_joined(Link
*link
) {
2164 assert(link
->network
);
2166 if (!hashmap_isempty(link
->bound_to_links
)) {
2167 r
= link_handle_bound_to_list(link
);
2170 } else if (!(link
->flags
& IFF_UP
)) {
2173 link_enter_failed(link
);
2178 if (link
->network
->bridge
) {
2179 r
= link_set_bridge(link
);
2181 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2184 if (link
->network
->bond
) {
2185 r
= link_bond_set(link
);
2187 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2190 if (link
->network
->use_br_vlan
&&
2191 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2192 r
= link_set_bridge_vlan(link
);
2194 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2197 /* Skip setting up addresses until it gets carrier,
2198 or it would try to set addresses twice,
2199 which is bad for non-idempotent steps. */
2200 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2203 return link_enter_set_addresses(link
);
2206 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2207 _cleanup_link_unref_ Link
*link
= userdata
;
2211 assert(link
->network
);
2215 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2218 r
= sd_netlink_message_get_errno(m
);
2219 if (r
< 0 && r
!= -EEXIST
) {
2220 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2221 link_enter_failed(link
);
2224 log_link_debug(link
, "Joined netdev");
2226 if (link
->enslaving
<= 0)
2232 static int link_enter_join_netdev(Link
*link
) {
2238 assert(link
->network
);
2239 assert(link
->state
== LINK_STATE_PENDING
);
2241 link_set_state(link
, LINK_STATE_ENSLAVING
);
2245 if (!link
->network
->bridge
&&
2246 !link
->network
->bond
&&
2247 !link
->network
->vrf
&&
2248 hashmap_isempty(link
->network
->stacked_netdevs
))
2249 return link_joined(link
);
2251 if (link
->network
->bond
) {
2252 log_struct(LOG_DEBUG
,
2253 LOG_LINK_INTERFACE(link
),
2254 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2255 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2258 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2260 log_struct_errno(LOG_WARNING
, r
,
2261 LOG_LINK_INTERFACE(link
),
2262 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2263 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2266 link_enter_failed(link
);
2273 if (link
->network
->bridge
) {
2274 log_struct(LOG_DEBUG
,
2275 LOG_LINK_INTERFACE(link
),
2276 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2277 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2280 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2282 log_struct_errno(LOG_WARNING
, r
,
2283 LOG_LINK_INTERFACE(link
),
2284 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2285 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2287 link_enter_failed(link
);
2294 if (link
->network
->vrf
) {
2295 log_struct(LOG_DEBUG
,
2296 LOG_LINK_INTERFACE(link
),
2297 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2298 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2300 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2302 log_struct_errno(LOG_WARNING
, r
,
2303 LOG_LINK_INTERFACE(link
),
2304 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2305 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2307 link_enter_failed(link
);
2314 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2316 if (netdev
->ifindex
> 0) {
2321 log_struct(LOG_DEBUG
,
2322 LOG_LINK_INTERFACE(link
),
2323 LOG_NETDEV_INTERFACE(netdev
),
2324 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2327 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2329 log_struct_errno(LOG_WARNING
, r
,
2330 LOG_LINK_INTERFACE(link
),
2331 LOG_NETDEV_INTERFACE(netdev
),
2332 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2334 link_enter_failed(link
);
2344 static int link_set_ipv4_forward(Link
*link
) {
2347 if (!link_ipv4_forward_enabled(link
))
2350 /* We propagate the forwarding flag from one interface to the
2351 * global setting one way. This means: as long as at least one
2352 * interface was configured at any time that had IP forwarding
2353 * enabled the setting will stay on for good. We do this
2354 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2355 * somewhat in sync (see below). */
2357 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2359 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2364 static int link_set_ipv6_forward(Link
*link
) {
2367 if (!link_ipv6_forward_enabled(link
))
2370 /* On Linux, the IPv6 stack does not know a per-interface
2371 * packet forwarding setting: either packet forwarding is on
2372 * for all, or off for all. We hence don't bother with a
2373 * per-interface setting, but simply propagate the interface
2374 * flag, if it is set, to the global flag, one-way. Note that
2375 * while IPv4 would allow a per-interface flag, we expose the
2376 * same behaviour there and also propagate the setting from
2377 * one to all, to keep things simple (see above). */
2379 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2381 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2386 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2387 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2388 IPv6PrivacyExtensions s
;
2389 const char *p
= NULL
;
2392 s
= link_ipv6_privacy_extensions(link
);
2396 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2397 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2399 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2401 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2406 static int link_set_ipv6_accept_ra(Link
*link
) {
2407 const char *p
= NULL
;
2410 /* Make this a NOP if IPv6 is not available */
2411 if (!socket_ipv6_is_supported())
2414 if (link
->flags
& IFF_LOOPBACK
)
2420 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2422 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2423 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2425 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2430 static int link_set_ipv6_dad_transmits(Link
*link
) {
2431 char buf
[DECIMAL_STR_MAX(int) + 1];
2432 const char *p
= NULL
;
2435 /* Make this a NOP if IPv6 is not available */
2436 if (!socket_ipv6_is_supported())
2439 if (link
->flags
& IFF_LOOPBACK
)
2445 if (link
->network
->ipv6_dad_transmits
< 0)
2448 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2449 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2451 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2453 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2458 static int link_set_ipv6_hop_limit(Link
*link
) {
2459 char buf
[DECIMAL_STR_MAX(int) + 1];
2460 const char *p
= NULL
;
2463 /* Make this a NOP if IPv6 is not available */
2464 if (!socket_ipv6_is_supported())
2467 if (link
->flags
& IFF_LOOPBACK
)
2473 if (link
->network
->ipv6_hop_limit
< 0)
2476 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2477 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2479 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2481 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2486 static int link_drop_foreign_config(Link
*link
) {
2492 SET_FOREACH(address
, link
->addresses_foreign
, 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
);
2502 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2503 /* do not touch routes managed by the kernel */
2504 if (route
->protocol
== RTPROT_KERNEL
)
2507 r
= route_remove(route
, link
, link_route_remove_handler
);
2515 static int link_drop_config(Link
*link
) {
2516 Address
*address
, *pool_address
;
2521 SET_FOREACH(address
, link
->addresses
, i
) {
2522 /* we consider IPv6LL addresses to be managed by the kernel */
2523 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2526 r
= address_remove(address
, link
, link_address_remove_handler
);
2530 /* If this address came from an address pool, clean up the pool */
2531 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2532 if (address_equal(address
, pool_address
)) {
2533 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2534 address_free(pool_address
);
2540 SET_FOREACH(route
, link
->routes
, i
) {
2541 /* do not touch routes managed by the kernel */
2542 if (route
->protocol
== RTPROT_KERNEL
)
2545 r
= route_remove(route
, link
, link_route_remove_handler
);
2555 static int link_update_lldp(Link
*link
) {
2563 if (link
->flags
& IFF_UP
) {
2564 r
= sd_lldp_start(link
->lldp
);
2566 log_link_debug(link
, "Started LLDP.");
2568 r
= sd_lldp_stop(link
->lldp
);
2570 log_link_debug(link
, "Stopped LLDP.");
2576 static int link_configure(Link
*link
) {
2580 assert(link
->network
);
2581 assert(link
->state
== LINK_STATE_PENDING
);
2583 if (streq_ptr(link
->kind
, "vcan")) {
2585 if (!(link
->flags
& IFF_UP
)) {
2586 r
= link_up_can(link
);
2588 link_enter_failed(link
);
2596 /* Drop foreign config, but ignore loopback or critical devices.
2597 * We do not want to remove loopback address or addresses used for root NFS. */
2598 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2599 r
= link_drop_foreign_config(link
);
2604 r
= link_set_proxy_arp(link
);
2608 r
= ipv6_proxy_ndp_addresses_configure(link
);
2612 r
= link_set_ipv4_forward(link
);
2616 r
= link_set_ipv6_forward(link
);
2620 r
= link_set_ipv6_privacy_extensions(link
);
2624 r
= link_set_ipv6_accept_ra(link
);
2628 r
= link_set_ipv6_dad_transmits(link
);
2632 r
= link_set_ipv6_hop_limit(link
);
2636 r
= link_set_flags(link
);
2640 if (link_ipv4ll_enabled(link
)) {
2641 r
= ipv4ll_configure(link
);
2646 if (link_dhcp4_enabled(link
)) {
2647 r
= dhcp4_set_promote_secondaries(link
);
2651 r
= dhcp4_configure(link
);
2656 if (link_dhcp4_server_enabled(link
)) {
2657 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2661 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2666 if (link_dhcp6_enabled(link
) ||
2667 link_ipv6_accept_ra_enabled(link
)) {
2668 r
= dhcp6_configure(link
);
2673 if (link_ipv6_accept_ra_enabled(link
)) {
2674 r
= ndisc_configure(link
);
2679 if (link_radv_enabled(link
)) {
2680 r
= radv_configure(link
);
2685 if (link_lldp_rx_enabled(link
)) {
2686 r
= sd_lldp_new(&link
->lldp
);
2690 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2694 r
= sd_lldp_match_capabilities(link
->lldp
,
2695 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2696 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2697 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2701 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2705 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2709 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2713 r
= link_update_lldp(link
);
2718 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2719 r
= link_acquire_conf(link
);
2724 return link_enter_join_netdev(link
);
2727 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2729 _cleanup_link_unref_ Link
*link
= userdata
;
2734 assert(link
->ifname
);
2735 assert(link
->manager
);
2737 if (link
->state
!= LINK_STATE_PENDING
)
2740 log_link_debug(link
, "Link state is up-to-date");
2742 r
= link_new_bound_by_list(link
);
2746 r
= link_handle_bound_by_list(link
);
2750 if (!link
->network
) {
2751 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2752 &link
->mac
, &network
);
2754 link_enter_unmanaged(link
);
2756 } else if (r
== 0 && network
->unmanaged
) {
2757 link_enter_unmanaged(link
);
2762 if (link
->flags
& IFF_LOOPBACK
) {
2763 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2764 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2766 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2767 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2769 if (network
->dhcp_server
)
2770 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2773 r
= network_apply(network
, link
);
2778 r
= link_new_bound_to_list(link
);
2782 r
= link_configure(link
);
2789 int link_initialized(Link
*link
, struct udev_device
*device
) {
2790 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2794 assert(link
->manager
);
2795 assert(link
->manager
->rtnl
);
2798 if (link
->state
!= LINK_STATE_PENDING
)
2801 if (link
->udev_device
)
2804 log_link_debug(link
, "udev initialized link");
2806 link
->udev_device
= udev_device_ref(device
);
2808 /* udev has initialized the link, but we don't know if we have yet
2809 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2810 * when it returns we know that the pending NEWLINKs have already been
2811 * processed and that we are up-to-date */
2813 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2818 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2819 link_initialized_and_synced
, link
, 0, NULL
);
2828 static int link_load(Link
*link
) {
2829 _cleanup_free_
char *network_file
= NULL
,
2832 *dhcp4_address
= NULL
,
2833 *ipv4ll_address
= NULL
;
2834 union in_addr_union address
;
2835 union in_addr_union route_dst
;
2841 r
= parse_env_file(link
->state_file
, NEWLINE
,
2842 "NETWORK_FILE", &network_file
,
2843 "ADDRESSES", &addresses
,
2845 "DHCP4_ADDRESS", &dhcp4_address
,
2846 "IPV4LL_ADDRESS", &ipv4ll_address
,
2848 if (r
< 0 && r
!= -ENOENT
)
2849 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2856 suffix
= strrchr(network_file
, '.');
2858 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2859 goto network_file_fail
;
2863 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2865 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2866 goto network_file_fail
;
2869 r
= network_apply(network
, link
);
2871 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2880 _cleanup_free_
char *address_str
= NULL
;
2881 char *prefixlen_str
;
2883 unsigned char prefixlen
;
2885 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2887 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2893 prefixlen_str
= strchr(address_str
, '/');
2894 if (!prefixlen_str
) {
2895 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2899 *prefixlen_str
++ = '\0';
2901 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2903 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2907 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2909 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2913 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2915 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2924 _cleanup_free_
char *route_str
= NULL
;
2925 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2927 char *prefixlen_str
;
2929 unsigned char prefixlen
, tos
, table
;
2932 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2934 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2940 prefixlen_str
= strchr(route_str
, '/');
2941 if (!prefixlen_str
) {
2942 log_link_debug(link
, "Failed to parse route %s", route_str
);
2946 *prefixlen_str
++ = '\0';
2948 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2950 log_link_debug(link
,
2951 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2956 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2958 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2962 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2964 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2966 if (lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
2967 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2968 0, route_expire_handler
, route
);
2970 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2973 route
->lifetime
= lifetime
;
2974 sd_event_source_unref(route
->expire
);
2975 route
->expire
= TAKE_PTR(expire
);
2979 if (dhcp4_address
) {
2980 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2982 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2983 goto dhcp4_address_fail
;
2986 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
2988 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2990 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2992 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2997 if (ipv4ll_address
) {
2998 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
3000 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
3001 goto ipv4ll_address_fail
;
3004 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3006 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3008 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3010 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3013 ipv4ll_address_fail
:
3018 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3020 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
3021 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3029 r
= link_new(m
, message
, ret
);
3035 log_link_debug(link
, "Link %d added", link
->ifindex
);
3037 r
= link_load(link
);
3041 if (detect_container() <= 0) {
3042 /* not in a container, udev will be around */
3043 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3044 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3046 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3050 if (udev_device_get_is_initialized(device
) <= 0) {
3052 log_link_debug(link
, "link pending udev initialization...");
3056 r
= link_initialized(link
, device
);
3060 /* we are calling a callback directly, so must take a ref */
3063 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3070 link_enter_failed(link
);
3074 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3079 log_link_info(link
, "Gained IPv6LL");
3081 link
->ipv6ll_address
= *address
;
3082 link_check_ready(link
);
3084 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3085 r
= link_acquire_ipv6_conf(link
);
3087 link_enter_failed(link
);
3095 static int link_carrier_gained(Link
*link
) {
3100 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3101 r
= link_acquire_conf(link
);
3103 link_enter_failed(link
);
3107 r
= link_enter_set_addresses(link
);
3112 r
= link_handle_bound_by_list(link
);
3119 static int link_carrier_lost(Link
*link
) {
3124 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3125 setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3126 configuration and stop the clients as well. */
3127 if (link
->setting_mtu
)
3130 r
= link_stop_clients(link
);
3132 link_enter_failed(link
);
3136 if (link_dhcp4_server_enabled(link
))
3137 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3139 r
= link_drop_config(link
);
3143 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3144 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3145 r
= link_drop_foreign_config(link
);
3150 r
= link_handle_bound_by_list(link
);
3157 int link_carrier_reset(Link
*link
) {
3162 if (link_has_carrier(link
)) {
3163 r
= link_carrier_lost(link
);
3167 r
= link_carrier_gained(link
);
3171 log_link_info(link
, "Reset carrier");
3177 int link_update(Link
*link
, sd_netlink_message
*m
) {
3178 struct ether_addr mac
;
3181 bool had_carrier
, carrier_gained
, carrier_lost
;
3185 assert(link
->ifname
);
3188 if (link
->state
== LINK_STATE_LINGER
) {
3190 log_link_info(link
, "Link readded");
3191 link_set_state(link
, LINK_STATE_ENSLAVING
);
3193 r
= link_new_carrier_maps(link
);
3198 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3199 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3200 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3202 link_free_carrier_maps(link
);
3204 r
= free_and_strdup(&link
->ifname
, ifname
);
3208 r
= link_new_carrier_maps(link
);
3213 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3214 if (r
>= 0 && mtu
> 0) {
3216 if (!link
->original_mtu
) {
3217 link
->original_mtu
= mtu
;
3218 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3221 if (link
->dhcp_client
) {
3222 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3225 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3231 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3233 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3237 /* The kernel may broadcast NEWLINK messages without the MAC address
3238 set, simply ignore them. */
3239 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3241 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3244 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3247 log_link_debug(link
, "MAC address: "
3248 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3249 mac
.ether_addr_octet
[0],
3250 mac
.ether_addr_octet
[1],
3251 mac
.ether_addr_octet
[2],
3252 mac
.ether_addr_octet
[3],
3253 mac
.ether_addr_octet
[4],
3254 mac
.ether_addr_octet
[5]);
3257 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3259 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3262 if (link
->dhcp_client
) {
3263 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3264 (const uint8_t *) &link
->mac
,
3268 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3270 switch (link
->network
->dhcp_client_identifier
) {
3271 case DHCP_CLIENT_ID_DUID
: {
3272 const DUID
*duid
= link_duid(link
);
3274 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3275 link
->network
->iaid
,
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 DUID/IAID in DHCP client: %m");
3283 case DHCP_CLIENT_ID_DUID_ONLY
: {
3284 const DUID
*duid
= link_duid(link
);
3286 r
= sd_dhcp_client_set_duid(link
->dhcp_client
,
3288 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3289 duid
->raw_data_len
);
3291 return log_link_warning_errno(link
, r
, "Could not update DUID in DHCP client: %m");
3294 case DHCP_CLIENT_ID_MAC
:
3295 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3297 (const uint8_t *)&link
->mac
,
3300 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3303 assert_not_reached("Unknown client identifier type.");
3307 if (link
->dhcp6_client
) {
3308 const DUID
* duid
= link_duid(link
);
3310 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3311 (const uint8_t *) &link
->mac
,
3315 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3317 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3318 link
->network
->iaid
);
3320 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3322 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3324 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3325 duid
->raw_data_len
);
3327 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3331 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3333 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3337 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
3339 return log_link_warning_errno(link
, r
, "Could not update MAC for ndisc: %m");
3344 had_carrier
= link_has_carrier(link
);
3346 r
= link_update_flags(link
, m
);
3350 r
= link_update_lldp(link
);
3354 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3355 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3357 if (carrier_gained
) {
3358 log_link_info(link
, "Gained carrier");
3360 r
= link_carrier_gained(link
);
3363 } else if (carrier_lost
) {
3364 log_link_info(link
, "Lost carrier");
3366 r
= link_carrier_lost(link
);
3374 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3382 if (hashmap_isempty(h
))
3386 HASHMAP_FOREACH(link
, h
, i
) {
3390 fprintf(f
, "%i", link
->ifindex
);
3397 int link_save(Link
*link
) {
3398 _cleanup_free_
char *temp_path
= NULL
;
3399 _cleanup_fclose_
FILE *f
= NULL
;
3400 const char *admin_state
, *oper_state
;
3407 assert(link
->state_file
);
3408 assert(link
->lease_file
);
3409 assert(link
->manager
);
3411 if (link
->state
== LINK_STATE_LINGER
) {
3412 unlink(link
->state_file
);
3416 link_lldp_save(link
);
3418 admin_state
= link_state_to_string(link
->state
);
3419 assert(admin_state
);
3421 oper_state
= link_operstate_to_string(link
->operstate
);
3424 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3428 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
3429 (void) fchmod(fileno(f
), 0644);
3432 "# This is private data. Do not parse.\n"
3435 admin_state
, oper_state
);
3437 if (link
->network
) {
3439 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3440 const char *dhcp_domainname
= NULL
;
3441 char **dhcp6_domains
= NULL
;
3442 char **dhcp_domains
= NULL
;
3445 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3446 yes_no(link
->network
->required_for_online
));
3448 if (link
->dhcp6_client
) {
3449 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3450 if (r
< 0 && r
!= -ENOMSG
)
3451 log_link_debug(link
, "No DHCPv6 lease");
3454 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3459 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3460 _cleanup_free_
char *b
= NULL
;
3462 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3463 &link
->network
->dns
[j
].address
, &b
);
3465 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3475 if (link
->network
->dhcp_use_dns
&&
3477 const struct in_addr
*addresses
;
3479 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3483 serialize_in_addrs(f
, addresses
, r
);
3488 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3489 struct in6_addr
*in6_addrs
;
3491 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3495 serialize_in6_addrs(f
, in6_addrs
, r
);
3500 /* Make sure to flush out old entries before we use the NDISC data */
3503 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3506 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3510 serialize_in6_addrs(f
, &dd
->address
, 1);
3519 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3521 if (link
->network
->dhcp_use_ntp
&&
3523 const struct in_addr
*addresses
;
3525 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3529 serialize_in_addrs(f
, addresses
, r
);
3534 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3535 struct in6_addr
*in6_addrs
;
3538 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3543 serialize_in6_addrs(f
, in6_addrs
, r
);
3547 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3549 fputstrv(f
, hosts
, NULL
, &space
);
3554 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3555 if (link
->dhcp_lease
) {
3556 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3557 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3560 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3563 fputs("DOMAINS=", f
);
3565 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3567 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3570 if (dhcp_domainname
)
3571 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3573 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3575 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3577 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3578 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3583 fputs("ROUTE_DOMAINS=", f
);
3585 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3587 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3590 if (dhcp_domainname
)
3591 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3593 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3595 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3597 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3598 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3603 fprintf(f
, "LLMNR=%s\n",
3604 resolve_support_to_string(link
->network
->llmnr
));
3605 fprintf(f
, "MDNS=%s\n",
3606 resolve_support_to_string(link
->network
->mdns
));
3608 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3609 fprintf(f
, "DNSSEC=%s\n",
3610 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3612 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3615 fputs("DNSSEC_NTA=", f
);
3617 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3618 fputs_with_space(f
, n
, NULL
, &space
);
3622 fputs("ADDRESSES=", f
);
3624 SET_FOREACH(a
, link
->addresses
, i
) {
3625 _cleanup_free_
char *address_str
= NULL
;
3627 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3631 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3636 fputs("ROUTES=", f
);
3638 SET_FOREACH(route
, link
->routes
, i
) {
3639 _cleanup_free_
char *route_str
= NULL
;
3641 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3645 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3646 space
? " " : "", route_str
,
3647 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3654 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3655 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3657 if (link
->dhcp_lease
) {
3658 struct in_addr address
;
3659 const char *tz
= NULL
;
3661 assert(link
->network
);
3663 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3665 fprintf(f
, "TIMEZONE=%s\n", tz
);
3667 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3669 fputs("DHCP4_ADDRESS=", f
);
3670 serialize_in_addrs(f
, &address
, 1);
3674 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3682 unlink(link
->lease_file
);
3685 struct in_addr address
;
3687 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3689 fputs("IPV4LL_ADDRESS=", f
);
3690 serialize_in_addrs(f
, &address
, 1);
3695 r
= fflush_and_check(f
);
3699 if (rename(temp_path
, link
->state_file
) < 0) {
3707 (void) unlink(link
->state_file
);
3709 (void) unlink(temp_path
);
3711 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3714 /* The serialized state in /run is no longer up-to-date. */
3715 void link_dirty(Link
*link
) {
3720 /* mark manager dirty as link is dirty */
3721 manager_dirty(link
->manager
);
3723 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3725 /* allocation errors are ignored */
3728 r
= set_put(link
->manager
->dirty_links
, link
);
3730 /* don't take another ref if the link was already dirty */
3736 /* The serialized state in /run is up-to-date */
3737 void link_clean(Link
*link
) {
3739 assert(link
->manager
);
3741 set_remove(link
->manager
->dirty_links
, link
);
3745 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3746 [LINK_STATE_PENDING
] = "pending",
3747 [LINK_STATE_ENSLAVING
] = "configuring",
3748 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3749 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3750 [LINK_STATE_CONFIGURED
] = "configured",
3751 [LINK_STATE_UNMANAGED
] = "unmanaged",
3752 [LINK_STATE_FAILED
] = "failed",
3753 [LINK_STATE_LINGER
] = "linger",
3756 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3758 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3759 [LINK_OPERSTATE_OFF
] = "off",
3760 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3761 [LINK_OPERSTATE_DORMANT
] = "dormant",
3762 [LINK_OPERSTATE_CARRIER
] = "carrier",
3763 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3764 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3767 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);