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_routes_configured
)
745 if (!link
->routing_policy_rules_configured
)
748 if (link_ipv4ll_enabled(link
))
749 if (!link
->ipv4ll_address
||
753 if (!link
->network
->bridge
) {
755 if (link_ipv6ll_enabled(link
))
756 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
759 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
760 !link
->dhcp4_configured
) ||
761 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
762 !link
->dhcp6_configured
) ||
763 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
764 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
767 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
771 SET_FOREACH(a
, link
->addresses
, i
)
772 if (!address_is_ready(a
))
775 if (link
->state
!= LINK_STATE_CONFIGURED
)
776 link_enter_configured(link
);
781 static int link_set_routing_policy_rule(Link
*link
) {
782 RoutingPolicyRule
*rule
, *rrule
= NULL
;
786 assert(link
->network
);
788 LIST_FOREACH(rules
, rule
, link
->network
->rules
) {
789 r
= routing_policy_rule_get(link
->manager
, rule
->family
, &rule
->from
, rule
->from_prefixlen
, &rule
->to
,
790 rule
->to_prefixlen
, rule
->tos
, rule
->fwmark
, rule
->table
, rule
->iif
, rule
->oif
, &rrule
);
792 (void) routing_policy_rule_make_local(link
->manager
, rrule
);
796 r
= routing_policy_rule_configure(rule
, link
, link_routing_policy_rule_handler
, false);
798 log_link_warning_errno(link
, r
, "Could not set routing policy rules: %m");
799 link_enter_failed(link
);
803 link
->routing_policy_rule_messages
++;
806 routing_policy_rule_purge(link
->manager
, link
);
807 if (link
->routing_policy_rule_messages
== 0) {
808 link
->routing_policy_rules_configured
= true;
809 link_check_ready(link
);
811 log_link_debug(link
, "Setting routing policy rules");
816 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
817 _cleanup_link_unref_ Link
*link
= userdata
;
820 assert(link
->route_messages
> 0);
821 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
822 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
825 link
->route_messages
--;
827 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
830 r
= sd_netlink_message_get_errno(m
);
831 if (r
< 0 && r
!= -EEXIST
)
832 log_link_warning_errno(link
, r
, "Could not set route: %m");
834 if (link
->route_messages
== 0) {
835 log_link_debug(link
, "Routes set");
836 link
->static_routes_configured
= true;
837 link_check_ready(link
);
843 static int link_enter_set_routes(Link
*link
) {
848 assert(link
->network
);
849 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
851 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
853 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
854 r
= route_configure(rt
, link
, route_handler
);
856 log_link_warning_errno(link
, r
, "Could not set routes: %m");
857 link_enter_failed(link
);
861 link
->route_messages
++;
864 (void) link_set_routing_policy_rule(link
);
866 if (link
->route_messages
== 0) {
867 link
->static_routes_configured
= true;
868 link_check_ready(link
);
870 log_link_debug(link
, "Setting routes");
875 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
876 _cleanup_link_unref_ Link
*link
= userdata
;
881 assert(link
->ifname
);
883 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
886 r
= sd_netlink_message_get_errno(m
);
887 if (r
< 0 && r
!= -ESRCH
)
888 log_link_warning_errno(link
, r
, "Could not drop route: %m");
893 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
894 _cleanup_link_unref_ Link
*link
= userdata
;
900 assert(link
->ifname
);
901 assert(link
->address_messages
> 0);
902 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
903 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
905 link
->address_messages
--;
907 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
910 r
= sd_netlink_message_get_errno(m
);
911 if (r
< 0 && r
!= -EEXIST
)
912 log_link_warning_errno(link
, r
, "could not set address: %m");
914 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
916 if (link
->address_messages
== 0) {
917 log_link_debug(link
, "Addresses set");
918 link_enter_set_routes(link
);
924 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
925 _cleanup_link_unref_ Link
*link
= userdata
;
931 assert(link
->ifname
);
932 assert(link
->address_label_messages
> 0);
934 link
->address_label_messages
--;
936 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
939 r
= sd_netlink_message_get_errno(m
);
940 if (r
< 0 && r
!= -EEXIST
)
941 log_link_warning_errno(link
, r
, "could not set address label: %m");
943 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
945 if (link
->address_label_messages
== 0)
946 log_link_debug(link
, "Addresses label set");
951 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
952 _cleanup_free_
struct in_addr
*addresses
= NULL
;
953 size_t n_addresses
= 0, n_allocated
= 0;
956 log_debug("Copying DNS server information from %s", link
->ifname
);
961 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
964 /* Only look for IPv4 addresses */
965 if (link
->network
->dns
[i
].family
!= AF_INET
)
968 ia
= link
->network
->dns
[i
].address
.in
;
970 /* Never propagate obviously borked data */
971 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
974 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
977 addresses
[n_addresses
++] = ia
;
980 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
981 const struct in_addr
*da
= NULL
;
984 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
987 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
990 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
995 if (n_addresses
<= 0)
998 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
1001 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
1002 _cleanup_free_
struct in_addr
*addresses
= NULL
;
1003 size_t n_addresses
= 0, n_allocated
= 0;
1009 log_debug("Copying NTP server information from %s", link
->ifname
);
1011 STRV_FOREACH(a
, link
->network
->ntp
) {
1014 /* Only look for IPv4 addresses */
1015 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
1018 /* Never propagate obviously borked data */
1019 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
1022 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
1025 addresses
[n_addresses
++] = ia
;
1028 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
1029 const struct in_addr
*da
= NULL
;
1032 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
1035 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
1038 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
1043 if (n_addresses
<= 0)
1046 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1049 static int link_set_bridge_fdb(Link
*link
) {
1050 FdbEntry
*fdb_entry
;
1053 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1054 r
= fdb_entry_configure(link
, fdb_entry
);
1056 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1062 static int link_enter_set_addresses(Link
*link
) {
1063 AddressLabel
*label
;
1068 assert(link
->network
);
1069 assert(link
->state
!= _LINK_STATE_INVALID
);
1071 r
= link_set_bridge_fdb(link
);
1075 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1077 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1078 r
= address_configure(ad
, link
, address_handler
, false);
1080 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1081 link_enter_failed(link
);
1085 link
->address_messages
++;
1088 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1089 r
= address_label_configure(label
, link
, address_label_handler
, false);
1091 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1092 link_enter_failed(link
);
1096 link
->address_label_messages
++;
1099 /* now that we can figure out a default address for the dhcp server,
1101 if (link_dhcp4_server_enabled(link
)) {
1103 Link
*uplink
= NULL
;
1104 bool acquired_uplink
= false;
1106 address
= link_find_dhcp_server_address(link
);
1108 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1109 link_enter_failed(link
);
1113 /* use the server address' subnet as the pool */
1114 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1115 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1120 r = sd_dhcp_server_set_router(link->dhcp_server,
1121 &main_address->in_addr.in);
1126 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1127 r
= sd_dhcp_server_set_max_lease_time(
1129 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1134 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1135 r
= sd_dhcp_server_set_default_lease_time(
1137 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1142 if (link
->network
->dhcp_server_emit_dns
) {
1144 if (link
->network
->n_dhcp_server_dns
> 0)
1145 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1147 uplink
= manager_find_uplink(link
->manager
, link
);
1148 acquired_uplink
= true;
1151 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1154 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1157 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1161 if (link
->network
->dhcp_server_emit_ntp
) {
1163 if (link
->network
->n_dhcp_server_ntp
> 0)
1164 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1166 if (!acquired_uplink
)
1167 uplink
= manager_find_uplink(link
->manager
, link
);
1170 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1173 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1177 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1180 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1182 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1186 if (link
->network
->dhcp_server_emit_timezone
) {
1187 _cleanup_free_
char *buffer
= NULL
;
1188 const char *tz
= NULL
;
1190 if (link
->network
->dhcp_server_timezone
)
1191 tz
= link
->network
->dhcp_server_timezone
;
1193 r
= get_timezone(&buffer
);
1195 log_warning_errno(r
, "Failed to determine timezone: %m");
1201 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1207 r
= sd_dhcp_server_start(link
->dhcp_server
);
1209 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1211 link_enter_failed(link
);
1216 log_link_debug(link
, "Offering DHCPv4 leases");
1219 if (link
->address_messages
== 0)
1220 link_enter_set_routes(link
);
1222 log_link_debug(link
, "Setting addresses");
1227 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1228 _cleanup_link_unref_ Link
*link
= userdata
;
1233 assert(link
->ifname
);
1235 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1238 r
= sd_netlink_message_get_errno(m
);
1239 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1240 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1245 static int link_set_bridge_vlan(Link
*link
) {
1248 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1250 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1255 static int link_set_proxy_arp(Link
*link
) {
1256 const char *p
= NULL
;
1259 if (!link_proxy_arp_enabled(link
))
1262 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1264 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1266 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1271 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1272 _cleanup_link_unref_ Link
*link
= userdata
;
1275 log_link_debug(link
, "Set link");
1277 r
= sd_netlink_message_get_errno(m
);
1278 if (r
< 0 && r
!= -EEXIST
) {
1279 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1280 link_enter_failed(link
);
1287 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1288 _cleanup_link_unref_ Link
*link
= userdata
;
1293 assert(link
->ifname
);
1295 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1298 r
= sd_netlink_message_get_errno(m
);
1300 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1305 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1306 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1310 assert(link
->manager
);
1311 assert(link
->manager
->rtnl
);
1313 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1315 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1317 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1319 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1321 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1323 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1325 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1327 link
->setting_mtu
= true;
1334 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1335 _cleanup_link_unref_ Link
*link
= userdata
;
1340 assert(link
->ifname
);
1342 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1345 r
= sd_netlink_message_get_errno(m
);
1347 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1352 static int link_set_flags(Link
*link
) {
1353 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1354 unsigned ifi_change
= 0;
1355 unsigned ifi_flags
= 0;
1359 assert(link
->manager
);
1360 assert(link
->manager
->rtnl
);
1362 if (link
->flags
& IFF_LOOPBACK
)
1368 if (link
->network
->arp
< 0)
1371 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1373 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1375 if (link
->network
->arp
>= 0) {
1376 ifi_change
|= IFF_NOARP
;
1377 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1380 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1382 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1384 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1386 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1393 static int link_set_bridge(Link
*link
) {
1394 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1398 assert(link
->network
);
1400 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1402 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1404 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1406 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1408 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1410 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1412 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1414 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1416 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1418 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1420 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1422 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1424 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1426 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1428 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1430 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1432 if (link
->network
->cost
!= 0) {
1433 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1435 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1437 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1438 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1440 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1443 r
= sd_netlink_message_close_container(req
);
1445 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1447 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1449 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1456 static int link_bond_set(Link
*link
) {
1457 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1461 assert(link
->network
);
1463 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1465 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1467 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1469 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1471 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1473 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1475 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1477 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1479 if (link
->network
->active_slave
) {
1480 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1482 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1485 if (link
->network
->primary_slave
) {
1486 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1488 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1491 r
= sd_netlink_message_close_container(req
);
1493 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1495 r
= sd_netlink_message_close_container(req
);
1497 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1499 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1501 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1508 static int link_lldp_save(Link
*link
) {
1509 _cleanup_free_
char *temp_path
= NULL
;
1510 _cleanup_fclose_
FILE *f
= NULL
;
1511 sd_lldp_neighbor
**l
= NULL
;
1515 assert(link
->lldp_file
);
1518 (void) unlink(link
->lldp_file
);
1522 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1526 (void) unlink(link
->lldp_file
);
1532 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1536 fchmod(fileno(f
), 0644);
1538 for (i
= 0; i
< n
; i
++) {
1543 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1548 (void) fwrite(&u
, 1, sizeof(u
), f
);
1549 (void) fwrite(p
, 1, sz
, f
);
1552 r
= fflush_and_check(f
);
1556 if (rename(temp_path
, link
->lldp_file
) < 0) {
1563 (void) unlink(link
->lldp_file
);
1565 (void) unlink(temp_path
);
1567 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1571 for (i
= 0; i
< n
; i
++)
1572 sd_lldp_neighbor_unref(l
[i
]);
1579 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1580 Link
*link
= userdata
;
1585 (void) link_lldp_save(link
);
1587 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1588 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1590 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1592 r
= link_lldp_emit_start(link
);
1594 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1598 static int link_acquire_ipv6_conf(Link
*link
) {
1603 if (link_dhcp6_enabled(link
)) {
1604 assert(link
->dhcp6_client
);
1605 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1607 /* start DHCPv6 client in stateless mode */
1608 r
= dhcp6_request_address(link
, true);
1609 if (r
< 0 && r
!= -EBUSY
)
1610 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1612 log_link_debug(link
, "Acquiring DHCPv6 lease");
1615 if (link_ipv6_accept_ra_enabled(link
)) {
1616 assert(link
->ndisc
);
1618 log_link_debug(link
, "Discovering IPv6 routers");
1620 r
= sd_ndisc_start(link
->ndisc
);
1621 if (r
< 0 && r
!= -EBUSY
)
1622 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1625 if (link_radv_enabled(link
)) {
1627 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1629 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1631 r
= sd_radv_start(link
->radv
);
1632 if (r
< 0 && r
!= -EBUSY
)
1633 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1639 static int link_acquire_ipv4_conf(Link
*link
) {
1643 assert(link
->network
);
1644 assert(link
->manager
);
1645 assert(link
->manager
->event
);
1647 if (link_ipv4ll_enabled(link
)) {
1648 assert(link
->ipv4ll
);
1650 log_link_debug(link
, "Acquiring IPv4 link-local address");
1652 r
= sd_ipv4ll_start(link
->ipv4ll
);
1654 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1657 if (link_dhcp4_enabled(link
)) {
1658 assert(link
->dhcp_client
);
1660 log_link_debug(link
, "Acquiring DHCPv4 lease");
1662 r
= sd_dhcp_client_start(link
->dhcp_client
);
1664 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1670 static int link_acquire_conf(Link
*link
) {
1675 if (link
->setting_mtu
) {
1676 link
->setting_mtu
= false;
1680 r
= link_acquire_ipv4_conf(link
);
1684 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1685 r
= link_acquire_ipv6_conf(link
);
1690 if (link_lldp_emit_enabled(link
)) {
1691 r
= link_lldp_emit_start(link
);
1693 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1699 bool link_has_carrier(Link
*link
) {
1700 /* see Documentation/networking/operstates.txt in the kernel sources */
1702 if (link
->kernel_operstate
== IF_OPER_UP
)
1705 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1706 /* operstate may not be implemented, so fall back to flags */
1707 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1713 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1714 _cleanup_link_unref_ Link
*link
= userdata
;
1719 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1722 r
= sd_netlink_message_get_errno(m
);
1724 /* we warn but don't fail the link, as it may be
1726 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1731 int link_up(Link
*link
) {
1732 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1733 uint8_t ipv6ll_mode
;
1737 assert(link
->network
);
1738 assert(link
->manager
);
1739 assert(link
->manager
->rtnl
);
1741 log_link_debug(link
, "Bringing link up");
1743 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1745 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1747 /* set it free if not enslaved with networkd */
1748 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1749 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1751 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1754 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1756 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1758 if (link
->network
->mac
) {
1759 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1761 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1764 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1765 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1766 (void) link_enable_ipv6(link
);
1768 if (link
->network
->mtu
) {
1769 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1770 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1771 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1773 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1774 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1776 link
->network
->mtu
= IPV6_MIN_MTU
;
1779 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1781 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1784 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1786 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1788 if (link_ipv6_enabled(link
)) {
1789 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1790 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1792 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1794 if (!link_ipv6ll_enabled(link
))
1795 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1797 const char *p
= NULL
;
1798 _cleanup_free_
char *stable_secret
= NULL
;
1800 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1801 r
= read_one_line_file(p
, &stable_secret
);
1804 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1806 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1808 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1810 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1812 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1813 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1815 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1818 r
= sd_netlink_message_close_container(req
);
1820 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1823 r
= sd_netlink_message_close_container(req
);
1825 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1827 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1829 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1836 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1837 _cleanup_link_unref_ Link
*link
= userdata
;
1842 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1845 r
= sd_netlink_message_get_errno(m
);
1847 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1852 int link_down(Link
*link
) {
1853 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1857 assert(link
->manager
);
1858 assert(link
->manager
->rtnl
);
1860 log_link_debug(link
, "Bringing link down");
1862 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1863 RTM_SETLINK
, link
->ifindex
);
1865 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1867 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1869 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1871 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1873 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1880 static int link_up_can(Link
*link
) {
1881 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1886 log_link_debug(link
, "Bringing CAN link up");
1888 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1890 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1892 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1894 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1896 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1898 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1905 static int link_handle_bound_to_list(Link
*link
) {
1909 bool required_up
= false;
1910 bool link_is_up
= false;
1914 if (hashmap_isempty(link
->bound_to_links
))
1917 if (link
->flags
& IFF_UP
)
1920 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1921 if (link_has_carrier(l
)) {
1926 if (!required_up
&& link_is_up
) {
1927 r
= link_down(link
);
1930 } else if (required_up
&& !link_is_up
) {
1939 static int link_handle_bound_by_list(Link
*link
) {
1946 if (hashmap_isempty(link
->bound_by_links
))
1949 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1950 r
= link_handle_bound_to_list(l
);
1958 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1964 if (link
== carrier
)
1967 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1970 r
= hashmap_ensure_allocated(h
, NULL
);
1974 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1981 static int link_new_bound_by_list(Link
*link
) {
1986 bool list_updated
= false;
1989 assert(link
->manager
);
1993 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1994 if (!carrier
->network
)
1997 if (strv_isempty(carrier
->network
->bind_carrier
))
2000 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
2001 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
2005 list_updated
= true;
2012 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2013 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
2017 link_dirty(carrier
);
2023 static int link_new_bound_to_list(Link
*link
) {
2028 bool list_updated
= false;
2031 assert(link
->manager
);
2036 if (strv_isempty(link
->network
->bind_carrier
))
2041 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
2042 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
2043 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
2047 list_updated
= true;
2054 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2055 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2059 link_dirty(carrier
);
2065 static int link_new_carrier_maps(Link
*link
) {
2068 r
= link_new_bound_by_list(link
);
2072 r
= link_handle_bound_by_list(link
);
2076 r
= link_new_bound_to_list(link
);
2080 r
= link_handle_bound_to_list(link
);
2087 static void link_free_bound_to_list(Link
*link
) {
2091 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2092 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2094 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2095 link_dirty(bound_to
);
2101 static void link_free_bound_by_list(Link
*link
) {
2105 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2106 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2108 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2109 link_dirty(bound_by
);
2110 link_handle_bound_to_list(bound_by
);
2117 static void link_free_carrier_maps(Link
*link
) {
2118 bool list_updated
= false;
2122 if (!hashmap_isempty(link
->bound_to_links
)) {
2123 link_free_bound_to_list(link
);
2124 list_updated
= true;
2127 if (!hashmap_isempty(link
->bound_by_links
)) {
2128 link_free_bound_by_list(link
);
2129 list_updated
= true;
2138 void link_drop(Link
*link
) {
2139 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2142 link_set_state(link
, LINK_STATE_LINGER
);
2144 link_free_carrier_maps(link
);
2146 log_link_debug(link
, "Link removed");
2148 (void)unlink(link
->state_file
);
2154 static int link_joined(Link
*link
) {
2158 assert(link
->network
);
2160 if (!hashmap_isempty(link
->bound_to_links
)) {
2161 r
= link_handle_bound_to_list(link
);
2164 } else if (!(link
->flags
& IFF_UP
)) {
2167 link_enter_failed(link
);
2172 if (link
->network
->bridge
) {
2173 r
= link_set_bridge(link
);
2175 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2178 if (link
->network
->bond
) {
2179 r
= link_bond_set(link
);
2181 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2184 if (link
->network
->use_br_vlan
&&
2185 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2186 r
= link_set_bridge_vlan(link
);
2188 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2191 /* Skip setting up addresses until it gets carrier,
2192 or it would try to set addresses twice,
2193 which is bad for non-idempotent steps. */
2194 if (!link_has_carrier(link
) && !link
->network
->configure_without_carrier
)
2197 return link_enter_set_addresses(link
);
2200 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2201 _cleanup_link_unref_ Link
*link
= userdata
;
2205 assert(link
->network
);
2209 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2212 r
= sd_netlink_message_get_errno(m
);
2213 if (r
< 0 && r
!= -EEXIST
) {
2214 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2215 link_enter_failed(link
);
2218 log_link_debug(link
, "Joined netdev");
2220 if (link
->enslaving
<= 0)
2226 static int link_enter_join_netdev(Link
*link
) {
2232 assert(link
->network
);
2233 assert(link
->state
== LINK_STATE_PENDING
);
2235 link_set_state(link
, LINK_STATE_ENSLAVING
);
2239 if (!link
->network
->bridge
&&
2240 !link
->network
->bond
&&
2241 !link
->network
->vrf
&&
2242 hashmap_isempty(link
->network
->stacked_netdevs
))
2243 return link_joined(link
);
2245 if (link
->network
->bond
) {
2246 log_struct(LOG_DEBUG
,
2247 LOG_LINK_INTERFACE(link
),
2248 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2249 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2252 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2254 log_struct_errno(LOG_WARNING
, r
,
2255 LOG_LINK_INTERFACE(link
),
2256 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2257 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2260 link_enter_failed(link
);
2267 if (link
->network
->bridge
) {
2268 log_struct(LOG_DEBUG
,
2269 LOG_LINK_INTERFACE(link
),
2270 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2271 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2274 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2276 log_struct_errno(LOG_WARNING
, r
,
2277 LOG_LINK_INTERFACE(link
),
2278 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2279 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2281 link_enter_failed(link
);
2288 if (link
->network
->vrf
) {
2289 log_struct(LOG_DEBUG
,
2290 LOG_LINK_INTERFACE(link
),
2291 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2292 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2294 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2296 log_struct_errno(LOG_WARNING
, r
,
2297 LOG_LINK_INTERFACE(link
),
2298 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2299 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2301 link_enter_failed(link
);
2308 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2310 if (netdev
->ifindex
> 0) {
2315 log_struct(LOG_DEBUG
,
2316 LOG_LINK_INTERFACE(link
),
2317 LOG_NETDEV_INTERFACE(netdev
),
2318 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2321 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2323 log_struct_errno(LOG_WARNING
, r
,
2324 LOG_LINK_INTERFACE(link
),
2325 LOG_NETDEV_INTERFACE(netdev
),
2326 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2328 link_enter_failed(link
);
2338 static int link_set_ipv4_forward(Link
*link
) {
2341 if (!link_ipv4_forward_enabled(link
))
2344 /* We propagate the forwarding flag from one interface to the
2345 * global setting one way. This means: as long as at least one
2346 * interface was configured at any time that had IP forwarding
2347 * enabled the setting will stay on for good. We do this
2348 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2349 * somewhat in sync (see below). */
2351 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2353 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2358 static int link_set_ipv6_forward(Link
*link
) {
2361 if (!link_ipv6_forward_enabled(link
))
2364 /* On Linux, the IPv6 stack does not know a per-interface
2365 * packet forwarding setting: either packet forwarding is on
2366 * for all, or off for all. We hence don't bother with a
2367 * per-interface setting, but simply propagate the interface
2368 * flag, if it is set, to the global flag, one-way. Note that
2369 * while IPv4 would allow a per-interface flag, we expose the
2370 * same behaviour there and also propagate the setting from
2371 * one to all, to keep things simple (see above). */
2373 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2375 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2380 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2381 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2382 IPv6PrivacyExtensions s
;
2383 const char *p
= NULL
;
2386 s
= link_ipv6_privacy_extensions(link
);
2390 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2391 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2393 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2395 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2400 static int link_set_ipv6_accept_ra(Link
*link
) {
2401 const char *p
= NULL
;
2404 /* Make this a NOP if IPv6 is not available */
2405 if (!socket_ipv6_is_supported())
2408 if (link
->flags
& IFF_LOOPBACK
)
2414 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2416 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2417 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2419 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2424 static int link_set_ipv6_dad_transmits(Link
*link
) {
2425 char buf
[DECIMAL_STR_MAX(int) + 1];
2426 const char *p
= NULL
;
2429 /* Make this a NOP if IPv6 is not available */
2430 if (!socket_ipv6_is_supported())
2433 if (link
->flags
& IFF_LOOPBACK
)
2439 if (link
->network
->ipv6_dad_transmits
< 0)
2442 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2443 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2445 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2447 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2452 static int link_set_ipv6_hop_limit(Link
*link
) {
2453 char buf
[DECIMAL_STR_MAX(int) + 1];
2454 const char *p
= NULL
;
2457 /* Make this a NOP if IPv6 is not available */
2458 if (!socket_ipv6_is_supported())
2461 if (link
->flags
& IFF_LOOPBACK
)
2467 if (link
->network
->ipv6_hop_limit
< 0)
2470 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2471 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2473 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2475 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2480 static int link_drop_foreign_config(Link
*link
) {
2486 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2487 /* we consider IPv6LL addresses to be managed by the kernel */
2488 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2491 r
= address_remove(address
, link
, link_address_remove_handler
);
2496 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2497 /* do not touch routes managed by the kernel */
2498 if (route
->protocol
== RTPROT_KERNEL
)
2501 r
= route_remove(route
, link
, link_route_remove_handler
);
2509 static int link_drop_config(Link
*link
) {
2510 Address
*address
, *pool_address
;
2515 SET_FOREACH(address
, link
->addresses
, i
) {
2516 /* we consider IPv6LL addresses to be managed by the kernel */
2517 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2520 r
= address_remove(address
, link
, link_address_remove_handler
);
2524 /* If this address came from an address pool, clean up the pool */
2525 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2526 if (address_equal(address
, pool_address
)) {
2527 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2528 address_free(pool_address
);
2534 SET_FOREACH(route
, link
->routes
, i
) {
2535 /* do not touch routes managed by the kernel */
2536 if (route
->protocol
== RTPROT_KERNEL
)
2539 r
= route_remove(route
, link
, link_route_remove_handler
);
2549 static int link_update_lldp(Link
*link
) {
2557 if (link
->flags
& IFF_UP
) {
2558 r
= sd_lldp_start(link
->lldp
);
2560 log_link_debug(link
, "Started LLDP.");
2562 r
= sd_lldp_stop(link
->lldp
);
2564 log_link_debug(link
, "Stopped LLDP.");
2570 static int link_configure(Link
*link
) {
2574 assert(link
->network
);
2575 assert(link
->state
== LINK_STATE_PENDING
);
2577 if (streq_ptr(link
->kind
, "vcan")) {
2579 if (!(link
->flags
& IFF_UP
)) {
2580 r
= link_up_can(link
);
2582 link_enter_failed(link
);
2590 /* Drop foreign config, but ignore loopback or critical devices.
2591 * We do not want to remove loopback address or addresses used for root NFS. */
2592 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2593 r
= link_drop_foreign_config(link
);
2598 r
= link_set_proxy_arp(link
);
2602 r
= ipv6_proxy_ndp_addresses_configure(link
);
2606 r
= link_set_ipv4_forward(link
);
2610 r
= link_set_ipv6_forward(link
);
2614 r
= link_set_ipv6_privacy_extensions(link
);
2618 r
= link_set_ipv6_accept_ra(link
);
2622 r
= link_set_ipv6_dad_transmits(link
);
2626 r
= link_set_ipv6_hop_limit(link
);
2630 r
= link_set_flags(link
);
2634 if (link_ipv4ll_enabled(link
)) {
2635 r
= ipv4ll_configure(link
);
2640 if (link_dhcp4_enabled(link
)) {
2641 r
= dhcp4_set_promote_secondaries(link
);
2645 r
= dhcp4_configure(link
);
2650 if (link_dhcp4_server_enabled(link
)) {
2651 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2655 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2660 if (link_dhcp6_enabled(link
) ||
2661 link_ipv6_accept_ra_enabled(link
)) {
2662 r
= dhcp6_configure(link
);
2667 if (link_ipv6_accept_ra_enabled(link
)) {
2668 r
= ndisc_configure(link
);
2673 if (link_radv_enabled(link
)) {
2674 r
= radv_configure(link
);
2679 if (link_lldp_rx_enabled(link
)) {
2680 r
= sd_lldp_new(&link
->lldp
);
2684 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2688 r
= sd_lldp_match_capabilities(link
->lldp
,
2689 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2690 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2691 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2695 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2699 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2703 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2707 r
= link_update_lldp(link
);
2712 if (link_has_carrier(link
) || link
->network
->configure_without_carrier
) {
2713 r
= link_acquire_conf(link
);
2718 return link_enter_join_netdev(link
);
2721 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2723 _cleanup_link_unref_ Link
*link
= userdata
;
2728 assert(link
->ifname
);
2729 assert(link
->manager
);
2731 if (link
->state
!= LINK_STATE_PENDING
)
2734 log_link_debug(link
, "Link state is up-to-date");
2736 r
= link_new_bound_by_list(link
);
2740 r
= link_handle_bound_by_list(link
);
2744 if (!link
->network
) {
2745 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2746 &link
->mac
, &network
);
2748 link_enter_unmanaged(link
);
2750 } else if (r
== 0 && network
->unmanaged
) {
2751 link_enter_unmanaged(link
);
2756 if (link
->flags
& IFF_LOOPBACK
) {
2757 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2758 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2760 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2761 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2763 if (network
->dhcp_server
)
2764 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2767 r
= network_apply(network
, link
);
2772 r
= link_new_bound_to_list(link
);
2776 r
= link_configure(link
);
2783 int link_initialized(Link
*link
, struct udev_device
*device
) {
2784 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2788 assert(link
->manager
);
2789 assert(link
->manager
->rtnl
);
2792 if (link
->state
!= LINK_STATE_PENDING
)
2795 if (link
->udev_device
)
2798 log_link_debug(link
, "udev initialized link");
2800 link
->udev_device
= udev_device_ref(device
);
2802 /* udev has initialized the link, but we don't know if we have yet
2803 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2804 * when it returns we know that the pending NEWLINKs have already been
2805 * processed and that we are up-to-date */
2807 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2812 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2813 link_initialized_and_synced
, link
, 0, NULL
);
2822 static int link_load(Link
*link
) {
2823 _cleanup_free_
char *network_file
= NULL
,
2826 *dhcp4_address
= NULL
,
2827 *ipv4ll_address
= NULL
;
2828 union in_addr_union address
;
2829 union in_addr_union route_dst
;
2835 r
= parse_env_file(link
->state_file
, NEWLINE
,
2836 "NETWORK_FILE", &network_file
,
2837 "ADDRESSES", &addresses
,
2839 "DHCP4_ADDRESS", &dhcp4_address
,
2840 "IPV4LL_ADDRESS", &ipv4ll_address
,
2842 if (r
< 0 && r
!= -ENOENT
)
2843 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2850 suffix
= strrchr(network_file
, '.');
2852 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2853 goto network_file_fail
;
2857 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2859 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2860 goto network_file_fail
;
2863 r
= network_apply(network
, link
);
2865 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2874 _cleanup_free_
char *address_str
= NULL
;
2875 char *prefixlen_str
;
2877 unsigned char prefixlen
;
2879 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2881 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2887 prefixlen_str
= strchr(address_str
, '/');
2888 if (!prefixlen_str
) {
2889 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2893 *prefixlen_str
++ = '\0';
2895 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2897 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2901 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2903 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2907 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2909 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2918 _cleanup_free_
char *route_str
= NULL
;
2919 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2921 char *prefixlen_str
;
2923 unsigned char prefixlen
, tos
, table
;
2926 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2928 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2934 prefixlen_str
= strchr(route_str
, '/');
2935 if (!prefixlen_str
) {
2936 log_link_debug(link
, "Failed to parse route %s", route_str
);
2940 *prefixlen_str
++ = '\0';
2942 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2944 log_link_debug(link
,
2945 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2950 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2952 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2956 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2958 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2960 if (lifetime
!= USEC_INFINITY
) {
2961 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2962 0, route_expire_handler
, route
);
2964 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2967 route
->lifetime
= lifetime
;
2968 sd_event_source_unref(route
->expire
);
2969 route
->expire
= expire
;
2974 if (dhcp4_address
) {
2975 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2977 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2978 goto dhcp4_address_fail
;
2981 r
= sd_dhcp_client_new(&link
->dhcp_client
, link
->network
->dhcp_anonymize
);
2983 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2985 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2987 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2992 if (ipv4ll_address
) {
2993 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2995 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2996 goto ipv4ll_address_fail
;
2999 r
= sd_ipv4ll_new(&link
->ipv4ll
);
3001 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
3003 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
3005 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
3008 ipv4ll_address_fail
:
3013 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
3015 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
3016 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
3024 r
= link_new(m
, message
, ret
);
3030 log_link_debug(link
, "Link %d added", link
->ifindex
);
3032 r
= link_load(link
);
3036 if (detect_container() <= 0) {
3037 /* not in a container, udev will be around */
3038 sprintf(ifindex_str
, "n%d", link
->ifindex
);
3039 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
3041 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
3045 if (udev_device_get_is_initialized(device
) <= 0) {
3047 log_link_debug(link
, "link pending udev initialization...");
3051 r
= link_initialized(link
, device
);
3055 /* we are calling a callback directly, so must take a ref */
3058 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3065 link_enter_failed(link
);
3069 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3074 log_link_info(link
, "Gained IPv6LL");
3076 link
->ipv6ll_address
= *address
;
3077 link_check_ready(link
);
3079 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3080 r
= link_acquire_ipv6_conf(link
);
3082 link_enter_failed(link
);
3090 static int link_carrier_gained(Link
*link
) {
3095 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3096 r
= link_acquire_conf(link
);
3098 link_enter_failed(link
);
3102 r
= link_enter_set_addresses(link
);
3107 r
= link_handle_bound_by_list(link
);
3114 static int link_carrier_lost(Link
*link
) {
3119 /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
3120 setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
3121 configuration and stop the clients as well. */
3122 if (link
->setting_mtu
)
3125 r
= link_stop_clients(link
);
3127 link_enter_failed(link
);
3131 if (link_dhcp4_server_enabled(link
))
3132 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3134 r
= link_drop_config(link
);
3138 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3139 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3140 r
= link_drop_foreign_config(link
);
3145 r
= link_handle_bound_by_list(link
);
3152 int link_carrier_reset(Link
*link
) {
3157 if (link_has_carrier(link
)) {
3158 r
= link_carrier_lost(link
);
3162 r
= link_carrier_gained(link
);
3166 log_link_info(link
, "Reset carrier");
3172 int link_update(Link
*link
, sd_netlink_message
*m
) {
3173 struct ether_addr mac
;
3176 bool had_carrier
, carrier_gained
, carrier_lost
;
3180 assert(link
->ifname
);
3183 if (link
->state
== LINK_STATE_LINGER
) {
3185 log_link_info(link
, "Link readded");
3186 link_set_state(link
, LINK_STATE_ENSLAVING
);
3188 r
= link_new_carrier_maps(link
);
3193 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3194 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3195 log_link_info(link
, "Interface name change detected, %s has been renamed to %s.", link
->ifname
, ifname
);
3197 link_free_carrier_maps(link
);
3199 r
= free_and_strdup(&link
->ifname
, ifname
);
3203 r
= link_new_carrier_maps(link
);
3208 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3209 if (r
>= 0 && mtu
> 0) {
3211 if (!link
->original_mtu
) {
3212 link
->original_mtu
= mtu
;
3213 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3216 if (link
->dhcp_client
) {
3217 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3220 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3226 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3228 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3232 /* The kernel may broadcast NEWLINK messages without the MAC address
3233 set, simply ignore them. */
3234 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3236 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3239 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3242 log_link_debug(link
, "MAC address: "
3243 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3244 mac
.ether_addr_octet
[0],
3245 mac
.ether_addr_octet
[1],
3246 mac
.ether_addr_octet
[2],
3247 mac
.ether_addr_octet
[3],
3248 mac
.ether_addr_octet
[4],
3249 mac
.ether_addr_octet
[5]);
3252 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3254 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3257 if (link
->dhcp_client
) {
3258 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3259 (const uint8_t *) &link
->mac
,
3263 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3265 switch (link
->network
->dhcp_client_identifier
) {
3266 case DHCP_CLIENT_ID_DUID
: {
3267 const DUID
*duid
= link_duid(link
);
3269 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3270 link
->network
->iaid
,
3272 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3273 duid
->raw_data_len
);
3275 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3278 case DHCP_CLIENT_ID_MAC
:
3279 r
= sd_dhcp_client_set_client_id(link
->dhcp_client
,
3281 (const uint8_t *)&link
->mac
,
3284 return log_link_warning_errno(link
, r
, "Could not update MAC client id in DHCP client: %m");
3287 assert_not_reached("Unknown client identifier type.");
3291 if (link
->dhcp6_client
) {
3292 const DUID
* duid
= link_duid(link
);
3294 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3295 (const uint8_t *) &link
->mac
,
3299 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3301 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3302 link
->network
->iaid
);
3304 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3306 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3308 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3309 duid
->raw_data_len
);
3311 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3315 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3317 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3322 had_carrier
= link_has_carrier(link
);
3324 r
= link_update_flags(link
, m
);
3328 r
= link_update_lldp(link
);
3332 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3333 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3335 if (carrier_gained
) {
3336 log_link_info(link
, "Gained carrier");
3338 r
= link_carrier_gained(link
);
3341 } else if (carrier_lost
) {
3342 log_link_info(link
, "Lost carrier");
3344 r
= link_carrier_lost(link
);
3352 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3360 if (hashmap_isempty(h
))
3363 fputs_unlocked(prefix
, f
);
3364 HASHMAP_FOREACH(link
, h
, i
) {
3366 fputc_unlocked(' ', f
);
3368 fprintf(f
, "%i", link
->ifindex
);
3372 fputc_unlocked('\n', f
);
3375 int link_save(Link
*link
) {
3376 _cleanup_free_
char *temp_path
= NULL
;
3377 _cleanup_fclose_
FILE *f
= NULL
;
3378 const char *admin_state
, *oper_state
;
3385 assert(link
->state_file
);
3386 assert(link
->lease_file
);
3387 assert(link
->manager
);
3389 if (link
->state
== LINK_STATE_LINGER
) {
3390 unlink(link
->state_file
);
3394 link_lldp_save(link
);
3396 admin_state
= link_state_to_string(link
->state
);
3397 assert(admin_state
);
3399 oper_state
= link_operstate_to_string(link
->operstate
);
3402 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3406 (void) fchmod(fileno(f
), 0644);
3409 "# This is private data. Do not parse.\n"
3412 admin_state
, oper_state
);
3414 if (link
->network
) {
3416 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3417 const char *dhcp_domainname
= NULL
;
3418 char **dhcp6_domains
= NULL
;
3419 char **dhcp_domains
= NULL
;
3422 fprintf(f
, "REQUIRED_FOR_ONLINE=%s\n",
3423 yes_no(link
->network
->required_for_online
));
3425 if (link
->dhcp6_client
) {
3426 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3427 if (r
< 0 && r
!= -ENOMSG
)
3428 log_link_debug(link
, "No DHCPv6 lease");
3431 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3433 fputs_unlocked("DNS=", f
);
3436 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3437 _cleanup_free_
char *b
= NULL
;
3439 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3440 &link
->network
->dns
[j
].address
, &b
);
3442 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3447 fputc_unlocked(' ', f
);
3448 fputs_unlocked(b
, f
);
3452 if (link
->network
->dhcp_use_dns
&&
3454 const struct in_addr
*addresses
;
3456 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3459 fputc_unlocked(' ', f
);
3460 serialize_in_addrs(f
, addresses
, r
);
3465 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3466 struct in6_addr
*in6_addrs
;
3468 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3471 fputc_unlocked(' ', f
);
3472 serialize_in6_addrs(f
, in6_addrs
, r
);
3477 /* Make sure to flush out old entries before we use the NDISC data */
3480 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3483 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3485 fputc_unlocked(' ', f
);
3487 serialize_in6_addrs(f
, &dd
->address
, 1);
3492 fputc_unlocked('\n', f
);
3494 fputs_unlocked("NTP=", f
);
3496 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3498 if (link
->network
->dhcp_use_ntp
&&
3500 const struct in_addr
*addresses
;
3502 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3505 fputc_unlocked(' ', f
);
3506 serialize_in_addrs(f
, addresses
, r
);
3511 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3512 struct in6_addr
*in6_addrs
;
3515 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3519 fputc_unlocked(' ', f
);
3520 serialize_in6_addrs(f
, in6_addrs
, r
);
3524 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3526 fputstrv(f
, hosts
, NULL
, &space
);
3529 fputc_unlocked('\n', f
);
3531 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3532 if (link
->dhcp_lease
) {
3533 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3534 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3537 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3540 fputs_unlocked("DOMAINS=", f
);
3542 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3544 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3547 if (dhcp_domainname
)
3548 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3550 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3552 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3554 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3555 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3558 fputc_unlocked('\n', f
);
3560 fputs_unlocked("ROUTE_DOMAINS=", f
);
3562 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3564 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3567 if (dhcp_domainname
)
3568 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3570 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3572 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3574 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3575 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3578 fputc_unlocked('\n', f
);
3580 fprintf(f
, "LLMNR=%s\n",
3581 resolve_support_to_string(link
->network
->llmnr
));
3582 fprintf(f
, "MDNS=%s\n",
3583 resolve_support_to_string(link
->network
->mdns
));
3585 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3586 fprintf(f
, "DNSSEC=%s\n",
3587 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3589 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3592 fputs_unlocked("DNSSEC_NTA=", f
);
3594 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3595 fputs_with_space(f
, n
, NULL
, &space
);
3596 fputc_unlocked('\n', f
);
3599 fputs_unlocked("ADDRESSES=", f
);
3601 SET_FOREACH(a
, link
->addresses
, i
) {
3602 _cleanup_free_
char *address_str
= NULL
;
3604 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3608 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3611 fputc_unlocked('\n', f
);
3613 fputs_unlocked("ROUTES=", f
);
3615 SET_FOREACH(route
, link
->routes
, i
) {
3616 _cleanup_free_
char *route_str
= NULL
;
3618 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3622 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%"PRIu32
"/"USEC_FMT
,
3623 space
? " " : "", route_str
,
3624 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3628 fputc_unlocked('\n', f
);
3631 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3632 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3634 if (link
->dhcp_lease
) {
3635 struct in_addr address
;
3636 const char *tz
= NULL
;
3638 assert(link
->network
);
3640 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3642 fprintf(f
, "TIMEZONE=%s\n", tz
);
3644 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3646 fputs_unlocked("DHCP4_ADDRESS=", f
);
3647 serialize_in_addrs(f
, &address
, 1);
3648 fputc_unlocked('\n', f
);
3651 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3659 unlink(link
->lease_file
);
3662 struct in_addr address
;
3664 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3666 fputs_unlocked("IPV4LL_ADDRESS=", f
);
3667 serialize_in_addrs(f
, &address
, 1);
3668 fputc_unlocked('\n', f
);
3672 r
= fflush_and_check(f
);
3676 if (rename(temp_path
, link
->state_file
) < 0) {
3684 (void) unlink(link
->state_file
);
3686 (void) unlink(temp_path
);
3688 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3691 /* The serialized state in /run is no longer up-to-date. */
3692 void link_dirty(Link
*link
) {
3697 /* mark manager dirty as link is dirty */
3698 manager_dirty(link
->manager
);
3700 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3702 /* allocation errors are ignored */
3705 r
= set_put(link
->manager
->dirty_links
, link
);
3707 /* don't take another ref if the link was already dirty */
3713 /* The serialized state in /run is up-to-date */
3714 void link_clean(Link
*link
) {
3716 assert(link
->manager
);
3718 set_remove(link
->manager
->dirty_links
, link
);
3722 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3723 [LINK_STATE_PENDING
] = "pending",
3724 [LINK_STATE_ENSLAVING
] = "configuring",
3725 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3726 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3727 [LINK_STATE_CONFIGURED
] = "configured",
3728 [LINK_STATE_UNMANAGED
] = "unmanaged",
3729 [LINK_STATE_FAILED
] = "failed",
3730 [LINK_STATE_LINGER
] = "linger",
3733 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3735 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3736 [LINK_OPERSTATE_OFF
] = "off",
3737 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3738 [LINK_OPERSTATE_DORMANT
] = "dormant",
3739 [LINK_OPERSTATE_CARRIER
] = "carrier",
3740 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3741 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3744 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);