2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
30 #include "netlink-util.h"
31 #include "network-internal.h"
32 #include "networkd-link.h"
33 #include "networkd-netdev.h"
35 #include "socket-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (link
->flags
& IFF_LOOPBACK
)
51 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
54 static bool link_dhcp4_enabled(Link
*link
) {
57 if (link
->flags
& IFF_LOOPBACK
)
63 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
66 static bool link_dhcp4_server_enabled(Link
*link
) {
69 if (link
->flags
& IFF_LOOPBACK
)
75 return link
->network
->dhcp_server
;
78 static bool link_ipv4ll_enabled(Link
*link
) {
81 if (link
->flags
& IFF_LOOPBACK
)
87 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
90 static bool link_ipv6ll_enabled(Link
*link
) {
93 if (link
->flags
& IFF_LOOPBACK
)
99 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
102 static bool link_lldp_enabled(Link
*link
) {
105 if (link
->flags
& IFF_LOOPBACK
)
108 if (link
->iftype
!= ARPHRD_ETHER
)
114 if (link
->network
->bridge
)
117 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
120 static bool link_ipv4_forward_enabled(Link
*link
) {
123 if (link
->flags
& IFF_LOOPBACK
)
129 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
132 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
135 static bool link_ipv6_forward_enabled(Link
*link
) {
138 if (!socket_ipv6_is_supported())
141 if (link
->flags
& IFF_LOOPBACK
)
147 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
150 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
153 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
162 /* If unset use system default (enabled if local forwarding is disabled.
163 * disabled if local forwarding is enabled).
164 * If set, ignore or enforce RA independent of local forwarding state.
166 if (link
->network
->ipv6_accept_ra
< 0)
167 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
168 return !link_ipv6_forward_enabled(link
);
169 else if (link
->network
->ipv6_accept_ra
> 0)
170 /* accept RA even if ip_forward is enabled */
177 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
179 if (!socket_ipv6_is_supported())
180 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
182 if (link
->flags
& IFF_LOOPBACK
)
183 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
186 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
188 return link
->network
->ipv6_privacy_extensions
;
191 void link_update_operstate(Link
*link
) {
192 LinkOperationalState operstate
;
195 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
196 operstate
= LINK_OPERSTATE_DORMANT
;
197 else if (link_has_carrier(link
)) {
199 uint8_t scope
= RT_SCOPE_NOWHERE
;
202 /* if we have carrier, check what addresses we have */
203 SET_FOREACH(address
, link
->addresses
, i
) {
204 if (!address_is_ready(address
))
207 if (address
->scope
< scope
)
208 scope
= address
->scope
;
211 /* for operstate we also take foreign addresses into account */
212 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
213 if (!address_is_ready(address
))
216 if (address
->scope
< scope
)
217 scope
= address
->scope
;
220 if (scope
< RT_SCOPE_SITE
)
221 /* universally accessible addresses found */
222 operstate
= LINK_OPERSTATE_ROUTABLE
;
223 else if (scope
< RT_SCOPE_HOST
)
224 /* only link or site local addresses found */
225 operstate
= LINK_OPERSTATE_DEGRADED
;
227 /* no useful addresses found */
228 operstate
= LINK_OPERSTATE_CARRIER
;
229 } else if (link
->flags
& IFF_UP
)
230 operstate
= LINK_OPERSTATE_NO_CARRIER
;
232 operstate
= LINK_OPERSTATE_OFF
;
234 if (link
->operstate
!= operstate
) {
235 link
->operstate
= operstate
;
236 link_send_changed(link
, "OperationalState", NULL
);
238 manager_dirty(link
->manager
);
242 #define FLAG_STRING(string, flag, old, new) \
243 (((old ^ new) & flag) \
244 ? ((old & flag) ? (" -" string) : (" +" string)) \
247 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
248 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
254 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
256 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
258 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
260 /* if we got a message without operstate, take it to mean
261 the state was unchanged */
262 operstate
= link
->kernel_operstate
;
264 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
267 if (link
->flags
!= flags
) {
268 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",
269 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
270 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
271 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
272 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
273 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
274 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
275 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
276 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
277 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
278 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
279 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
280 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
281 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
282 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
283 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
284 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
285 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
286 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
287 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
289 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
290 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
291 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
292 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
293 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
294 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
295 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
296 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
298 /* link flags are currently at most 18 bits, let's align to
300 if (unknown_flags_added
)
302 "Unknown link flags gained: %#.5x (ignoring)",
303 unknown_flags_added
);
305 if (unknown_flags_removed
)
307 "Unknown link flags lost: %#.5x (ignoring)",
308 unknown_flags_removed
);
312 link
->kernel_operstate
= operstate
;
314 link_update_operstate(link
);
319 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
320 _cleanup_link_unref_ Link
*link
= NULL
;
324 unsigned short iftype
;
330 r
= sd_netlink_message_get_type(message
, &type
);
333 else if (type
!= RTM_NEWLINK
)
336 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
339 else if (ifindex
<= 0)
342 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
346 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
350 link
= new0(Link
, 1);
355 link
->manager
= manager
;
356 link
->state
= LINK_STATE_PENDING
;
357 link
->rtnl_extended_attrs
= true;
358 link
->ifindex
= ifindex
;
359 link
->iftype
= iftype
;
360 link
->ifname
= strdup(ifname
);
364 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
366 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
368 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
371 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
374 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
377 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
381 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
385 r
= link_update_flags(link
, message
);
395 static void link_free(Link
*link
) {
403 while (!set_isempty(link
->addresses
))
404 address_free(set_first(link
->addresses
));
406 while (!set_isempty(link
->addresses_foreign
))
407 address_free(set_first(link
->addresses_foreign
));
409 link
->addresses
= set_free(link
->addresses
);
411 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
413 while ((address
= link
->pool_addresses
)) {
414 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
415 address_free(address
);
418 sd_dhcp_server_unref(link
->dhcp_server
);
419 sd_dhcp_client_unref(link
->dhcp_client
);
420 sd_dhcp_lease_unref(link
->dhcp_lease
);
422 free(link
->lease_file
);
424 sd_lldp_unref(link
->lldp
);
425 free(link
->lldp_file
);
427 sd_ipv4ll_unref(link
->ipv4ll
);
428 sd_dhcp6_client_unref(link
->dhcp6_client
);
429 sd_ndisc_unref(link
->ndisc_router_discovery
);
432 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
436 (void)unlink(link
->state_file
);
437 free(link
->state_file
);
439 udev_device_unref(link
->udev_device
);
441 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
442 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
443 hashmap_free(link
->bound_to_links
);
445 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
446 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
447 hashmap_free(link
->bound_by_links
);
452 Link
*link_unref(Link
*link
) {
456 assert(link
->n_ref
> 0);
468 Link
*link_ref(Link
*link
) {
472 assert(link
->n_ref
> 0);
479 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
486 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
495 static void link_set_state(Link
*link
, LinkState state
) {
498 if (link
->state
== state
)
503 link_send_changed(link
, "AdministrativeState", NULL
);
508 static void link_enter_unmanaged(Link
*link
) {
511 log_link_debug(link
, "Unmanaged");
513 link_set_state(link
, LINK_STATE_UNMANAGED
);
518 static int link_stop_clients(Link
*link
) {
522 assert(link
->manager
);
523 assert(link
->manager
->event
);
525 if (link
->dhcp_client
) {
526 k
= sd_dhcp_client_stop(link
->dhcp_client
);
528 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
532 k
= sd_ipv4ll_stop(link
->ipv4ll
);
534 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
537 if (link
->dhcp6_client
) {
538 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
540 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
543 if (link
->ndisc_router_discovery
) {
544 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
546 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
552 void link_enter_failed(Link
*link
) {
555 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
558 log_link_warning(link
, "Failed");
560 link_set_state(link
, LINK_STATE_FAILED
);
562 link_stop_clients(link
);
567 static Address
* link_find_dhcp_server_address(Link
*link
) {
571 assert(link
->network
);
573 /* The first statically configured address if there is any */
574 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
576 if (address
->family
!= AF_INET
)
579 if (in_addr_is_null(address
->family
, &address
->in_addr
))
585 /* If that didn't work, find a suitable address we got from the pool */
586 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
587 if (address
->family
!= AF_INET
)
596 static int link_enter_configured(Link
*link
) {
598 assert(link
->network
);
599 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
601 log_link_info(link
, "Configured");
603 link_set_state(link
, LINK_STATE_CONFIGURED
);
610 void link_check_ready(Link
*link
) {
619 if (!link
->static_configured
)
622 if (link_ipv4ll_enabled(link
))
623 if (!link
->ipv4ll_address
||
627 if (link_ipv6ll_enabled(link
))
628 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
631 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
632 !link
->dhcp4_configured
) ||
633 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
634 !link
->dhcp6_configured
) ||
635 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
636 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
639 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
642 SET_FOREACH(a
, link
->addresses
, i
)
643 if (!address_is_ready(a
))
646 if (link
->state
!= LINK_STATE_CONFIGURED
)
647 link_enter_configured(link
);
652 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
653 _cleanup_link_unref_ Link
*link
= userdata
;
656 assert(link
->link_messages
> 0);
657 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
658 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
661 link
->link_messages
--;
663 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
666 r
= sd_netlink_message_get_errno(m
);
667 if (r
< 0 && r
!= -EEXIST
)
668 log_link_warning_errno(link
, r
, "Could not set route: %m");
670 if (link
->link_messages
== 0) {
671 log_link_debug(link
, "Routes set");
672 link
->static_configured
= true;
673 link_check_ready(link
);
679 static int link_enter_set_routes(Link
*link
) {
684 assert(link
->network
);
685 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
687 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
689 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
690 r
= route_configure(rt
, link
, &route_handler
);
692 log_link_warning_errno(link
, r
, "Could not set routes: %m");
693 link_enter_failed(link
);
697 link
->link_messages
++;
700 if (link
->link_messages
== 0) {
701 link
->static_configured
= true;
702 link_check_ready(link
);
704 log_link_debug(link
, "Setting routes");
709 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
710 _cleanup_link_unref_ Link
*link
= userdata
;
715 assert(link
->ifname
);
717 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
720 r
= sd_netlink_message_get_errno(m
);
721 if (r
< 0 && r
!= -ESRCH
)
722 log_link_warning_errno(link
, r
, "Could not drop route: %m");
727 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
728 _cleanup_link_unref_ Link
*link
= userdata
;
734 assert(link
->ifname
);
735 assert(link
->link_messages
> 0);
736 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
737 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
739 link
->link_messages
--;
741 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
744 r
= sd_netlink_message_get_errno(m
);
745 if (r
< 0 && r
!= -EEXIST
)
746 log_link_warning_errno(link
, r
, "could not set address: %m");
748 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
750 if (link
->link_messages
== 0) {
751 log_link_debug(link
, "Addresses set");
752 link_enter_set_routes(link
);
758 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
759 _cleanup_free_
struct in_addr
*addresses
= NULL
;
760 size_t n_addresses
= 0, n_allocated
= 0;
763 log_debug("Copying DNS server information from %s", link
->ifname
);
768 STRV_FOREACH(a
, link
->network
->dns
) {
771 /* Only look for IPv4 addresses */
772 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
775 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
778 addresses
[n_addresses
++] = ia
;
781 if (link
->network
->dhcp_use_dns
&&
783 const struct in_addr
*da
= NULL
;
786 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
789 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
792 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
797 if (n_addresses
<= 0)
800 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
803 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
804 _cleanup_free_
struct in_addr
*addresses
= NULL
;
805 size_t n_addresses
= 0, n_allocated
= 0;
811 log_debug("Copying NTP server information from %s", link
->ifname
);
813 STRV_FOREACH(a
, link
->network
->ntp
) {
816 /* Only look for IPv4 addresses */
817 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
820 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
823 addresses
[n_addresses
++] = ia
;
826 if (link
->network
->dhcp_use_ntp
&&
828 const struct in_addr
*da
= NULL
;
831 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
834 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
837 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
842 if (n_addresses
<= 0)
845 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
848 static int link_enter_set_addresses(Link
*link
) {
853 assert(link
->network
);
854 assert(link
->state
!= _LINK_STATE_INVALID
);
856 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
858 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
859 r
= address_configure(ad
, link
, &address_handler
, false);
861 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
862 link_enter_failed(link
);
866 link
->link_messages
++;
869 /* now that we can figure out a default address for the dhcp server,
871 if (link_dhcp4_server_enabled(link
)) {
874 bool acquired_uplink
= false;
876 address
= link_find_dhcp_server_address(link
);
878 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
879 link_enter_failed(link
);
883 /* use the server address' subnet as the pool */
884 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
885 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
890 r = sd_dhcp_server_set_router(link->dhcp_server,
891 &main_address->in_addr.in);
896 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
897 r
= sd_dhcp_server_set_max_lease_time(
899 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
904 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
905 r
= sd_dhcp_server_set_default_lease_time(
907 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
912 if (link
->network
->dhcp_server_emit_dns
) {
914 if (link
->network
->n_dhcp_server_dns
> 0)
915 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
917 uplink
= manager_find_uplink(link
->manager
, link
);
918 acquired_uplink
= true;
921 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
924 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
927 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
931 if (link
->network
->dhcp_server_emit_ntp
) {
933 if (link
->network
->n_dhcp_server_ntp
> 0)
934 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
936 if (!acquired_uplink
)
937 uplink
= manager_find_uplink(link
->manager
, link
);
940 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
943 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
947 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
950 if (link
->network
->dhcp_server_emit_timezone
) {
951 _cleanup_free_
char *buffer
= NULL
;
952 const char *tz
= NULL
;
954 if (link
->network
->dhcp_server_timezone
)
955 tz
= link
->network
->dhcp_server_timezone
;
957 r
= get_timezone(&buffer
);
959 log_warning_errno(r
, "Failed to determine timezone: %m");
965 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
971 r
= sd_dhcp_server_start(link
->dhcp_server
);
973 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
975 link_enter_failed(link
);
980 log_link_debug(link
, "Offering DHCPv4 leases");
983 if (link
->link_messages
== 0)
984 link_enter_set_routes(link
);
986 log_link_debug(link
, "Setting addresses");
991 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
992 _cleanup_link_unref_ Link
*link
= userdata
;
997 assert(link
->ifname
);
999 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1002 r
= sd_netlink_message_get_errno(m
);
1003 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1004 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1009 static int link_set_bridge_fdb(Link
*const link
) {
1010 FdbEntry
*fdb_entry
;
1013 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1014 r
= fdb_entry_configure(link
, fdb_entry
);
1016 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1024 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1025 _cleanup_link_unref_ Link
*link
= userdata
;
1028 log_link_debug(link
, "Set link");
1030 r
= sd_netlink_message_get_errno(m
);
1031 if (r
< 0 && r
!= -EEXIST
) {
1032 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1033 link_enter_failed(link
);
1040 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1041 _cleanup_link_unref_ Link
*link
= userdata
;
1042 const sd_bus_error
*e
;
1047 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1050 e
= sd_bus_message_get_error(m
);
1052 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1057 int link_set_hostname(Link
*link
, const char *hostname
) {
1061 assert(link
->manager
);
1063 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1065 if (!link
->manager
->bus
) {
1066 /* TODO: replace by assert when we can rely on kdbus */
1067 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1071 r
= sd_bus_call_method_async(
1074 "org.freedesktop.hostname1",
1075 "/org/freedesktop/hostname1",
1076 "org.freedesktop.hostname1",
1078 set_hostname_handler
,
1085 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1092 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1093 _cleanup_link_unref_ Link
*link
= userdata
;
1094 const sd_bus_error
*e
;
1099 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1102 e
= sd_bus_message_get_error(m
);
1104 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1109 int link_set_timezone(Link
*link
, const char *tz
) {
1113 assert(link
->manager
);
1116 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1118 if (!link
->manager
->bus
) {
1119 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1123 r
= sd_bus_call_method_async(
1126 "org.freedesktop.timedate1",
1127 "/org/freedesktop/timedate1",
1128 "org.freedesktop.timedate1",
1130 set_timezone_handler
,
1136 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1143 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1144 _cleanup_link_unref_ Link
*link
= userdata
;
1149 assert(link
->ifname
);
1151 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1154 r
= sd_netlink_message_get_errno(m
);
1156 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1161 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1162 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1166 assert(link
->manager
);
1167 assert(link
->manager
->rtnl
);
1169 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1171 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1173 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1175 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1177 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1179 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1181 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1188 static int link_set_bridge(Link
*link
) {
1189 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1193 assert(link
->network
);
1195 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1197 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1199 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1201 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1203 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1205 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1207 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1209 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1211 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1213 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1215 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1217 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1219 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1221 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1223 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1225 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1227 if (link
->network
->cost
!= 0) {
1228 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1230 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1233 r
= sd_netlink_message_close_container(req
);
1235 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1237 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1239 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1246 static int link_lldp_save(Link
*link
) {
1247 _cleanup_free_
char *temp_path
= NULL
;
1248 _cleanup_fclose_
FILE *f
= NULL
;
1249 sd_lldp_neighbor
**l
= NULL
;
1253 assert(link
->lldp_file
);
1256 (void) unlink(link
->lldp_file
);
1260 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1264 (void) unlink(link
->lldp_file
);
1270 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1274 fchmod(fileno(f
), 0644);
1276 for (i
= 0; i
< n
; i
++) {
1281 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1286 (void) fwrite(&u
, 1, sizeof(u
), f
);
1287 (void) fwrite(p
, 1, sz
, f
);
1290 r
= fflush_and_check(f
);
1294 if (rename(temp_path
, link
->lldp_file
) < 0) {
1301 (void) unlink(link
->lldp_file
);
1303 (void) unlink(temp_path
);
1305 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1309 for (i
= 0; i
< n
; i
++)
1310 sd_lldp_neighbor_unref(l
[i
]);
1317 static void lldp_handler(sd_lldp
*lldp
, void *userdata
) {
1318 Link
*link
= userdata
;
1322 (void) link_lldp_save(link
);
1325 static int link_acquire_ipv6_conf(Link
*link
) {
1330 if (link_dhcp6_enabled(link
)) {
1331 assert(link
->dhcp6_client
);
1332 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1334 log_link_debug(link
, "Acquiring DHCPv6 lease");
1336 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1337 if (r
< 0 && r
!= -EBUSY
)
1338 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1340 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1341 if (r
< 0 && r
!= -EBUSY
)
1342 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1345 if (link_ipv6_accept_ra_enabled(link
)) {
1346 assert(link
->ndisc_router_discovery
);
1348 log_link_debug(link
, "Discovering IPv6 routers");
1350 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1351 if (r
< 0 && r
!= -EBUSY
)
1352 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1358 static int link_acquire_conf(Link
*link
) {
1362 assert(link
->network
);
1363 assert(link
->manager
);
1364 assert(link
->manager
->event
);
1366 if (link_ipv4ll_enabled(link
)) {
1367 assert(link
->ipv4ll
);
1369 log_link_debug(link
, "Acquiring IPv4 link-local address");
1371 r
= sd_ipv4ll_start(link
->ipv4ll
);
1373 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1376 if (link_dhcp4_enabled(link
)) {
1377 assert(link
->dhcp_client
);
1379 log_link_debug(link
, "Acquiring DHCPv4 lease");
1381 r
= sd_dhcp_client_start(link
->dhcp_client
);
1383 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1389 bool link_has_carrier(Link
*link
) {
1390 /* see Documentation/networking/operstates.txt in the kernel sources */
1392 if (link
->kernel_operstate
== IF_OPER_UP
)
1395 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1396 /* operstate may not be implemented, so fall back to flags */
1397 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1403 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1404 _cleanup_link_unref_ Link
*link
= userdata
;
1409 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1412 r
= sd_netlink_message_get_errno(m
);
1414 /* we warn but don't fail the link, as it may be
1416 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1421 static int link_up(Link
*link
) {
1422 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1423 uint8_t ipv6ll_mode
;
1427 assert(link
->network
);
1428 assert(link
->manager
);
1429 assert(link
->manager
->rtnl
);
1431 log_link_debug(link
, "Bringing link up");
1433 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1435 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1437 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1439 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1441 if (link
->network
->mac
) {
1442 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1444 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1447 if (link
->network
->mtu
) {
1448 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1450 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1453 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1455 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1457 if (socket_ipv6_is_supported()) {
1458 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1459 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1461 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1463 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1464 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1466 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1468 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1469 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1471 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1474 r
= sd_netlink_message_close_container(req
);
1476 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1479 r
= sd_netlink_message_close_container(req
);
1481 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1483 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1485 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1492 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1493 _cleanup_link_unref_ Link
*link
= userdata
;
1498 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1501 r
= sd_netlink_message_get_errno(m
);
1503 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1508 static int link_down(Link
*link
) {
1509 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1513 assert(link
->manager
);
1514 assert(link
->manager
->rtnl
);
1516 log_link_debug(link
, "Bringing link down");
1518 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1519 RTM_SETLINK
, link
->ifindex
);
1521 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1523 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1525 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1527 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1529 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1536 static int link_handle_bound_to_list(Link
*link
) {
1540 bool required_up
= false;
1541 bool link_is_up
= false;
1545 if (hashmap_isempty(link
->bound_to_links
))
1548 if (link
->flags
& IFF_UP
)
1551 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1552 if (link_has_carrier(l
)) {
1557 if (!required_up
&& link_is_up
) {
1558 r
= link_down(link
);
1561 } else if (required_up
&& !link_is_up
) {
1570 static int link_handle_bound_by_list(Link
*link
) {
1577 if (hashmap_isempty(link
->bound_by_links
))
1580 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1581 r
= link_handle_bound_to_list(l
);
1589 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1595 if (link
== carrier
)
1598 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1601 r
= hashmap_ensure_allocated(h
, NULL
);
1605 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1612 static int link_new_bound_by_list(Link
*link
) {
1617 bool list_updated
= false;
1620 assert(link
->manager
);
1624 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1625 if (!carrier
->network
)
1628 if (strv_isempty(carrier
->network
->bind_carrier
))
1631 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1632 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1636 list_updated
= true;
1643 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1644 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1648 link_dirty(carrier
);
1654 static int link_new_bound_to_list(Link
*link
) {
1659 bool list_updated
= false;
1662 assert(link
->manager
);
1667 if (strv_isempty(link
->network
->bind_carrier
))
1672 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1673 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1674 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1678 list_updated
= true;
1685 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1686 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1690 link_dirty(carrier
);
1696 static int link_new_carrier_maps(Link
*link
) {
1699 r
= link_new_bound_by_list(link
);
1703 r
= link_handle_bound_by_list(link
);
1707 r
= link_new_bound_to_list(link
);
1711 r
= link_handle_bound_to_list(link
);
1718 static void link_free_bound_to_list(Link
*link
) {
1722 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1723 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1725 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1726 link_dirty(bound_to
);
1732 static void link_free_bound_by_list(Link
*link
) {
1736 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1737 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1739 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1740 link_dirty(bound_by
);
1741 link_handle_bound_to_list(bound_by
);
1748 static void link_free_carrier_maps(Link
*link
) {
1749 bool list_updated
= false;
1753 if (!hashmap_isempty(link
->bound_to_links
)) {
1754 link_free_bound_to_list(link
);
1755 list_updated
= true;
1758 if (!hashmap_isempty(link
->bound_by_links
)) {
1759 link_free_bound_by_list(link
);
1760 list_updated
= true;
1769 void link_drop(Link
*link
) {
1770 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1773 link_set_state(link
, LINK_STATE_LINGER
);
1775 link_free_carrier_maps(link
);
1777 log_link_debug(link
, "Link removed");
1779 (void)unlink(link
->state_file
);
1785 static int link_joined(Link
*link
) {
1789 assert(link
->network
);
1791 if (!hashmap_isempty(link
->bound_to_links
)) {
1792 r
= link_handle_bound_to_list(link
);
1795 } else if (!(link
->flags
& IFF_UP
)) {
1798 link_enter_failed(link
);
1803 if(link
->network
->bridge
) {
1804 r
= link_set_bridge(link
);
1806 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1809 return link_enter_set_addresses(link
);
1812 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1813 _cleanup_link_unref_ Link
*link
= userdata
;
1817 assert(link
->network
);
1821 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1824 r
= sd_netlink_message_get_errno(m
);
1825 if (r
< 0 && r
!= -EEXIST
) {
1826 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1827 link_enter_failed(link
);
1830 log_link_debug(link
, "Joined netdev");
1832 if (link
->enslaving
<= 0)
1838 static int link_enter_join_netdev(Link
*link
) {
1844 assert(link
->network
);
1845 assert(link
->state
== LINK_STATE_PENDING
);
1847 link_set_state(link
, LINK_STATE_ENSLAVING
);
1851 if (!link
->network
->bridge
&&
1852 !link
->network
->bond
&&
1853 hashmap_isempty(link
->network
->stacked_netdevs
))
1854 return link_joined(link
);
1856 if (link
->network
->bond
) {
1857 log_struct(LOG_DEBUG
,
1858 LOG_LINK_INTERFACE(link
),
1859 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1860 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1863 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1865 log_struct_errno(LOG_WARNING
, r
,
1866 LOG_LINK_INTERFACE(link
),
1867 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1868 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1871 link_enter_failed(link
);
1878 if (link
->network
->bridge
) {
1879 log_struct(LOG_DEBUG
,
1880 LOG_LINK_INTERFACE(link
),
1881 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1882 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1885 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1887 log_struct_errno(LOG_WARNING
, r
,
1888 LOG_LINK_INTERFACE(link
),
1889 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1890 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1892 link_enter_failed(link
);
1899 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1901 log_struct(LOG_DEBUG
,
1902 LOG_LINK_INTERFACE(link
),
1903 LOG_NETDEV_INTERFACE(netdev
),
1904 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1907 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1909 log_struct_errno(LOG_WARNING
, r
,
1910 LOG_LINK_INTERFACE(link
),
1911 LOG_NETDEV_INTERFACE(netdev
),
1912 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1914 link_enter_failed(link
);
1924 static int link_set_ipv4_forward(Link
*link
) {
1927 if (!link_ipv4_forward_enabled(link
))
1930 /* We propagate the forwarding flag from one interface to the
1931 * global setting one way. This means: as long as at least one
1932 * interface was configured at any time that had IP forwarding
1933 * enabled the setting will stay on for good. We do this
1934 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1935 * somewhat in sync (see below). */
1937 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1939 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1944 static int link_set_ipv6_forward(Link
*link
) {
1947 if (!link_ipv6_forward_enabled(link
))
1950 /* On Linux, the IPv6 stack does not not know a per-interface
1951 * packet forwarding setting: either packet forwarding is on
1952 * for all, or off for all. We hence don't bother with a
1953 * per-interface setting, but simply propagate the interface
1954 * flag, if it is set, to the global flag, one-way. Note that
1955 * while IPv4 would allow a per-interface flag, we expose the
1956 * same behaviour there and also propagate the setting from
1957 * one to all, to keep things simple (see above). */
1959 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1961 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1966 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1967 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1968 IPv6PrivacyExtensions s
;
1969 const char *p
= NULL
;
1972 s
= link_ipv6_privacy_extensions(link
);
1976 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1977 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1979 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1981 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1986 static int link_set_ipv6_accept_ra(Link
*link
) {
1987 const char *p
= NULL
;
1990 /* Make this a NOP if IPv6 is not available */
1991 if (!socket_ipv6_is_supported())
1994 if (link
->flags
& IFF_LOOPBACK
)
2000 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2002 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2003 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2005 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2010 static int link_set_ipv6_dad_transmits(Link
*link
) {
2011 char buf
[DECIMAL_STR_MAX(int) + 1];
2012 const char *p
= NULL
;
2015 /* Make this a NOP if IPv6 is not available */
2016 if (!socket_ipv6_is_supported())
2019 if (link
->flags
& IFF_LOOPBACK
)
2025 if (link
->network
->ipv6_dad_transmits
< 0)
2028 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2029 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2031 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2033 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2038 static int link_set_ipv6_hop_limit(Link
*link
) {
2039 char buf
[DECIMAL_STR_MAX(int) + 1];
2040 const char *p
= NULL
;
2043 /* Make this a NOP if IPv6 is not available */
2044 if (!socket_ipv6_is_supported())
2047 if (link
->flags
& IFF_LOOPBACK
)
2053 if (link
->network
->ipv6_hop_limit
< 0)
2056 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2057 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2059 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2061 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2066 static int link_drop_foreign_config(Link
*link
) {
2072 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2073 /* we consider IPv6LL addresses to be managed by the kernel */
2074 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2077 r
= address_remove(address
, link
, link_address_remove_handler
);
2082 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2083 /* do not touch routes managed by the kernel */
2084 if (route
->protocol
== RTPROT_KERNEL
)
2087 r
= route_remove(route
, link
, link_address_remove_handler
);
2095 static int link_update_lldp(Link
*link
) {
2103 if (link
->flags
& IFF_UP
) {
2104 r
= sd_lldp_start(link
->lldp
);
2106 log_link_debug(link
, "Started LLDP.");
2108 r
= sd_lldp_stop(link
->lldp
);
2110 log_link_debug(link
, "Stopped LLDP.");
2116 static int link_configure(Link
*link
) {
2120 assert(link
->network
);
2121 assert(link
->state
== LINK_STATE_PENDING
);
2123 /* Drop foreign config, but ignore loopback or critical devices.
2124 * We do not want to remove loopback address or addresses used for root NFS. */
2125 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2126 r
= link_drop_foreign_config(link
);
2131 r
= link_set_bridge_fdb(link
);
2135 r
= link_set_ipv4_forward(link
);
2139 r
= link_set_ipv6_forward(link
);
2143 r
= link_set_ipv6_privacy_extensions(link
);
2147 r
= link_set_ipv6_accept_ra(link
);
2151 r
= link_set_ipv6_dad_transmits(link
);
2155 r
= link_set_ipv6_hop_limit(link
);
2159 if (link_ipv4ll_enabled(link
)) {
2160 r
= ipv4ll_configure(link
);
2165 if (link_dhcp4_enabled(link
)) {
2166 r
= dhcp4_configure(link
);
2171 if (link_dhcp4_server_enabled(link
)) {
2172 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2176 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2181 if (link_dhcp6_enabled(link
) ||
2182 link_ipv6_accept_ra_enabled(link
)) {
2183 r
= dhcp6_configure(link
);
2188 if (link_ipv6_accept_ra_enabled(link
)) {
2189 r
= ndisc_configure(link
);
2194 if (link_lldp_enabled(link
)) {
2195 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2199 r
= sd_lldp_match_capabilities(link
->lldp
,
2200 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2201 _LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2202 _LLDP_SYSTEM_CAPABILITIES_ALL
);
2206 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2210 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2214 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2218 r
= link_update_lldp(link
);
2223 if (link_has_carrier(link
)) {
2224 r
= link_acquire_conf(link
);
2228 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2229 r
= link_acquire_ipv6_conf(link
);
2235 return link_enter_join_netdev(link
);
2238 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2240 _cleanup_link_unref_ Link
*link
= userdata
;
2245 assert(link
->ifname
);
2246 assert(link
->manager
);
2248 if (link
->state
!= LINK_STATE_PENDING
)
2251 log_link_debug(link
, "Link state is up-to-date");
2253 r
= link_new_bound_by_list(link
);
2257 r
= link_handle_bound_by_list(link
);
2261 if (!link
->network
) {
2262 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2263 &link
->mac
, &network
);
2265 link_enter_unmanaged(link
);
2270 if (link
->flags
& IFF_LOOPBACK
) {
2271 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2272 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2274 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2275 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2277 if (network
->dhcp_server
)
2278 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2281 r
= network_apply(link
->manager
, network
, link
);
2286 r
= link_new_bound_to_list(link
);
2290 r
= link_configure(link
);
2297 int link_initialized(Link
*link
, struct udev_device
*device
) {
2298 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2302 assert(link
->manager
);
2303 assert(link
->manager
->rtnl
);
2306 if (link
->state
!= LINK_STATE_PENDING
)
2309 if (link
->udev_device
)
2312 log_link_debug(link
, "udev initialized link");
2314 link
->udev_device
= udev_device_ref(device
);
2316 /* udev has initialized the link, but we don't know if we have yet
2317 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2318 * when it returns we know that the pending NEWLINKs have already been
2319 * processed and that we are up-to-date */
2321 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2326 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2327 link_initialized_and_synced
, link
, 0, NULL
);
2336 static int link_load(Link
*link
) {
2337 _cleanup_free_
char *network_file
= NULL
,
2340 *dhcp4_address
= NULL
,
2341 *ipv4ll_address
= NULL
;
2342 union in_addr_union address
;
2343 union in_addr_union route_dst
;
2349 r
= parse_env_file(link
->state_file
, NEWLINE
,
2350 "NETWORK_FILE", &network_file
,
2351 "ADDRESSES", &addresses
,
2353 "DHCP4_ADDRESS", &dhcp4_address
,
2354 "IPV4LL_ADDRESS", &ipv4ll_address
,
2356 if (r
< 0 && r
!= -ENOENT
)
2357 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2364 suffix
= strrchr(network_file
, '.');
2366 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2367 goto network_file_fail
;
2371 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2373 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2374 goto network_file_fail
;
2377 r
= network_apply(link
->manager
, network
, link
);
2379 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2388 _cleanup_free_
char *address_str
= NULL
;
2389 char *prefixlen_str
;
2391 unsigned char prefixlen
;
2393 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2395 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2401 prefixlen_str
= strchr(address_str
, '/');
2402 if (!prefixlen_str
) {
2403 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2407 *prefixlen_str
++ = '\0';
2409 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2411 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2415 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2417 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2421 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2423 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2432 _cleanup_free_
char *route_str
= NULL
;
2433 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2435 char *prefixlen_str
;
2437 unsigned char prefixlen
, tos
, table
;
2440 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2442 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2448 prefixlen_str
= strchr(route_str
, '/');
2449 if (!prefixlen_str
) {
2450 log_link_debug(link
, "Failed to parse route %s", route_str
);
2454 *prefixlen_str
++ = '\0';
2456 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2458 log_link_debug(link
,
2459 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2464 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2466 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2470 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2472 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2474 if (lifetime
!= USEC_INFINITY
) {
2475 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2476 0, route_expire_handler
, route
);
2478 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2481 route
->lifetime
= lifetime
;
2482 sd_event_source_unref(route
->expire
);
2483 route
->expire
= expire
;
2488 if (dhcp4_address
) {
2489 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2491 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2492 goto dhcp4_address_fail
;
2495 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2497 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2499 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2501 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2506 if (ipv4ll_address
) {
2507 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2509 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2510 goto ipv4ll_address_fail
;
2513 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2515 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2517 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2519 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2522 ipv4ll_address_fail
:
2527 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2529 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2530 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2538 r
= link_new(m
, message
, ret
);
2544 log_link_debug(link
, "Link %d added", link
->ifindex
);
2546 r
= link_load(link
);
2550 if (detect_container() <= 0) {
2551 /* not in a container, udev will be around */
2552 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2553 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2555 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2559 if (udev_device_get_is_initialized(device
) <= 0) {
2561 log_link_debug(link
, "link pending udev initialization...");
2565 r
= link_initialized(link
, device
);
2569 /* we are calling a callback directly, so must take a ref */
2572 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2579 link_enter_failed(link
);
2583 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2588 log_link_info(link
, "Gained IPv6LL");
2590 link
->ipv6ll_address
= *address
;
2591 link_check_ready(link
);
2593 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2594 r
= link_acquire_ipv6_conf(link
);
2596 link_enter_failed(link
);
2604 static int link_carrier_gained(Link
*link
) {
2609 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2610 r
= link_acquire_conf(link
);
2612 link_enter_failed(link
);
2617 r
= link_handle_bound_by_list(link
);
2624 static int link_carrier_lost(Link
*link
) {
2629 r
= link_stop_clients(link
);
2631 link_enter_failed(link
);
2635 r
= link_handle_bound_by_list(link
);
2642 int link_carrier_reset(Link
*link
) {
2647 if (link_has_carrier(link
)) {
2648 r
= link_carrier_lost(link
);
2652 r
= link_carrier_gained(link
);
2656 log_link_info(link
, "Reset carrier");
2662 int link_update(Link
*link
, sd_netlink_message
*m
) {
2663 struct ether_addr mac
;
2666 bool had_carrier
, carrier_gained
, carrier_lost
;
2670 assert(link
->ifname
);
2673 if (link
->state
== LINK_STATE_LINGER
) {
2675 log_link_info(link
, "Link readded");
2676 link_set_state(link
, LINK_STATE_ENSLAVING
);
2678 r
= link_new_carrier_maps(link
);
2683 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2684 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2685 log_link_info(link
, "Renamed to %s", ifname
);
2687 link_free_carrier_maps(link
);
2689 r
= free_and_strdup(&link
->ifname
, ifname
);
2693 r
= link_new_carrier_maps(link
);
2698 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2699 if (r
>= 0 && mtu
> 0) {
2701 if (!link
->original_mtu
) {
2702 link
->original_mtu
= mtu
;
2703 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2706 if (link
->dhcp_client
) {
2707 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2710 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2716 /* The kernel may broadcast NEWLINK messages without the MAC address
2717 set, simply ignore them. */
2718 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2720 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2723 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2726 log_link_debug(link
, "MAC address: "
2727 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2728 mac
.ether_addr_octet
[0],
2729 mac
.ether_addr_octet
[1],
2730 mac
.ether_addr_octet
[2],
2731 mac
.ether_addr_octet
[3],
2732 mac
.ether_addr_octet
[4],
2733 mac
.ether_addr_octet
[5]);
2736 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2738 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2741 if (link
->dhcp_client
) {
2742 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2743 (const uint8_t *) &link
->mac
,
2747 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2750 if (link
->dhcp6_client
) {
2751 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2752 (const uint8_t *) &link
->mac
,
2756 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2761 had_carrier
= link_has_carrier(link
);
2763 r
= link_update_flags(link
, m
);
2767 r
= link_update_lldp(link
);
2771 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2772 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2774 if (carrier_gained
) {
2775 log_link_info(link
, "Gained carrier");
2777 r
= link_carrier_gained(link
);
2780 } else if (carrier_lost
) {
2781 log_link_info(link
, "Lost carrier");
2783 r
= link_carrier_lost(link
);
2791 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2799 if (hashmap_isempty(h
))
2803 HASHMAP_FOREACH(link
, h
, i
) {
2807 fprintf(f
, "%i", link
->ifindex
);
2814 int link_save(Link
*link
) {
2815 _cleanup_free_
char *temp_path
= NULL
;
2816 _cleanup_fclose_
FILE *f
= NULL
;
2817 const char *admin_state
, *oper_state
;
2824 assert(link
->state_file
);
2825 assert(link
->lease_file
);
2826 assert(link
->manager
);
2828 if (link
->state
== LINK_STATE_LINGER
) {
2829 unlink(link
->state_file
);
2833 link_lldp_save(link
);
2835 admin_state
= link_state_to_string(link
->state
);
2836 assert(admin_state
);
2838 oper_state
= link_operstate_to_string(link
->operstate
);
2841 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2845 fchmod(fileno(f
), 0644);
2848 "# This is private data. Do not parse.\n"
2851 admin_state
, oper_state
);
2853 if (link
->network
) {
2855 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2856 const char *dhcp_domainname
= NULL
;
2857 char **dhcp6_domains
= NULL
;
2859 if (link
->dhcp6_client
) {
2860 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2861 if (r
< 0 && r
!= -ENOMSG
)
2862 log_link_debug(link
, "No DHCPv6 lease");
2865 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2869 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
2871 if (link
->network
->dhcp_use_dns
&&
2873 const struct in_addr
*addresses
;
2875 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2879 serialize_in_addrs(f
, addresses
, r
);
2884 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
2885 struct in6_addr
*in6_addrs
;
2887 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2891 serialize_in6_addrs(f
, in6_addrs
, r
);
2899 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
2901 if (link
->network
->dhcp_use_ntp
&&
2903 const struct in_addr
*addresses
;
2905 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2909 serialize_in_addrs(f
, addresses
, r
);
2914 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
2915 struct in6_addr
*in6_addrs
;
2918 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2923 serialize_in6_addrs(f
, in6_addrs
, r
);
2927 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2929 fputstrv(f
, hosts
, NULL
, &space
);
2934 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
2935 if (link
->dhcp_lease
)
2936 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
2939 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
2942 fputs("DOMAINS=", f
);
2943 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
2945 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
2946 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2948 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
2949 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2953 fputs("ROUTE_DOMAINS=", f
);
2954 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
2956 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
2957 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2959 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
2960 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2964 fprintf(f
, "LLMNR=%s\n",
2965 resolve_support_to_string(link
->network
->llmnr
));
2966 fprintf(f
, "MDNS=%s\n",
2967 resolve_support_to_string(link
->network
->mdns
));
2969 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
2970 fprintf(f
, "DNSSEC=%s\n",
2971 dnssec_mode_to_string(link
->network
->dnssec_mode
));
2973 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
2976 fputs("DNSSEC_NTA=", f
);
2978 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
2979 fputs_with_space(f
, n
, NULL
, &space
);
2983 fputs("ADDRESSES=", f
);
2985 SET_FOREACH(a
, link
->addresses
, i
) {
2986 _cleanup_free_
char *address_str
= NULL
;
2988 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2992 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2997 fputs("ROUTES=", f
);
2999 SET_FOREACH(route
, link
->routes
, i
) {
3000 _cleanup_free_
char *route_str
= NULL
;
3002 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3006 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3007 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3014 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3015 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3017 if (link
->dhcp_lease
) {
3018 struct in_addr address
;
3019 const char *tz
= NULL
;
3021 assert(link
->network
);
3023 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3025 fprintf(f
, "TIMEZONE=%s\n", tz
);
3027 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3029 fputs("DHCP4_ADDRESS=", f
);
3030 serialize_in_addrs(f
, &address
, 1);
3034 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3042 unlink(link
->lease_file
);
3045 struct in_addr address
;
3047 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3049 fputs("IPV4LL_ADDRESS=", f
);
3050 serialize_in_addrs(f
, &address
, 1);
3055 r
= fflush_and_check(f
);
3059 if (rename(temp_path
, link
->state_file
) < 0) {
3067 (void) unlink(link
->state_file
);
3069 (void) unlink(temp_path
);
3071 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3074 /* The serialized state in /run is no longer up-to-date. */
3075 void link_dirty(Link
*link
) {
3080 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3082 /* allocation errors are ignored */
3085 r
= set_put(link
->manager
->dirty_links
, link
);
3087 /* allocation errors are ignored */
3093 /* The serialized state in /run is up-to-date */
3094 void link_clean(Link
*link
) {
3096 assert(link
->manager
);
3098 set_remove(link
->manager
->dirty_links
, link
);
3102 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3103 [LINK_STATE_PENDING
] = "pending",
3104 [LINK_STATE_ENSLAVING
] = "configuring",
3105 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3106 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3107 [LINK_STATE_CONFIGURED
] = "configured",
3108 [LINK_STATE_UNMANAGED
] = "unmanaged",
3109 [LINK_STATE_FAILED
] = "failed",
3110 [LINK_STATE_LINGER
] = "linger",
3113 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3115 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3116 [LINK_OPERSTATE_OFF
] = "off",
3117 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3118 [LINK_OPERSTATE_DORMANT
] = "dormant",
3119 [LINK_OPERSTATE_CARRIER
] = "carrier",
3120 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3121 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3124 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);