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"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-link.h"
32 #include "networkd-lldp-tx.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_rx_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_lldp_tx_enabled(Link
*link
) {
123 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->iftype
!= ARPHRD_ETHER
)
132 return link
->network
->lldp_emit
;
135 static bool link_ipv4_forward_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
147 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
150 static bool link_ipv6_forward_enabled(Link
*link
) {
153 if (!socket_ipv6_is_supported())
156 if (link
->flags
& IFF_LOOPBACK
)
162 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
165 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
168 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 /* If unset use system default (enabled if local forwarding is disabled.
178 * disabled if local forwarding is enabled).
179 * If set, ignore or enforce RA independent of local forwarding state.
181 if (link
->network
->ipv6_accept_ra
< 0)
182 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
183 return !link_ipv6_forward_enabled(link
);
184 else if (link
->network
->ipv6_accept_ra
> 0)
185 /* accept RA even if ip_forward is enabled */
192 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
194 if (!socket_ipv6_is_supported())
195 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
197 if (link
->flags
& IFF_LOOPBACK
)
198 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
201 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
203 return link
->network
->ipv6_privacy_extensions
;
206 void link_update_operstate(Link
*link
) {
207 LinkOperationalState operstate
;
210 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
211 operstate
= LINK_OPERSTATE_DORMANT
;
212 else if (link_has_carrier(link
)) {
214 uint8_t scope
= RT_SCOPE_NOWHERE
;
217 /* if we have carrier, check what addresses we have */
218 SET_FOREACH(address
, link
->addresses
, i
) {
219 if (!address_is_ready(address
))
222 if (address
->scope
< scope
)
223 scope
= address
->scope
;
226 /* for operstate we also take foreign addresses into account */
227 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
228 if (!address_is_ready(address
))
231 if (address
->scope
< scope
)
232 scope
= address
->scope
;
235 if (scope
< RT_SCOPE_SITE
)
236 /* universally accessible addresses found */
237 operstate
= LINK_OPERSTATE_ROUTABLE
;
238 else if (scope
< RT_SCOPE_HOST
)
239 /* only link or site local addresses found */
240 operstate
= LINK_OPERSTATE_DEGRADED
;
242 /* no useful addresses found */
243 operstate
= LINK_OPERSTATE_CARRIER
;
244 } else if (link
->flags
& IFF_UP
)
245 operstate
= LINK_OPERSTATE_NO_CARRIER
;
247 operstate
= LINK_OPERSTATE_OFF
;
249 if (link
->operstate
!= operstate
) {
250 link
->operstate
= operstate
;
251 link_send_changed(link
, "OperationalState", NULL
);
253 manager_dirty(link
->manager
);
257 #define FLAG_STRING(string, flag, old, new) \
258 (((old ^ new) & flag) \
259 ? ((old & flag) ? (" -" string) : (" +" string)) \
262 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
263 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
269 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
271 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
273 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
275 /* if we got a message without operstate, take it to mean
276 the state was unchanged */
277 operstate
= link
->kernel_operstate
;
279 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
282 if (link
->flags
!= flags
) {
283 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",
284 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
285 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
286 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
287 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
288 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
289 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
290 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
291 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
292 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
293 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
294 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
295 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
296 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
297 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
298 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
299 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
300 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
301 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
302 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
304 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
305 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
306 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
307 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
308 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
309 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
310 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
311 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
313 /* link flags are currently at most 18 bits, let's align to
315 if (unknown_flags_added
)
317 "Unknown link flags gained: %#.5x (ignoring)",
318 unknown_flags_added
);
320 if (unknown_flags_removed
)
322 "Unknown link flags lost: %#.5x (ignoring)",
323 unknown_flags_removed
);
327 link
->kernel_operstate
= operstate
;
329 link_update_operstate(link
);
334 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
335 _cleanup_link_unref_ Link
*link
= NULL
;
339 unsigned short iftype
;
345 r
= sd_netlink_message_get_type(message
, &type
);
348 else if (type
!= RTM_NEWLINK
)
351 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
354 else if (ifindex
<= 0)
357 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
361 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
365 link
= new0(Link
, 1);
370 link
->manager
= manager
;
371 link
->state
= LINK_STATE_PENDING
;
372 link
->rtnl_extended_attrs
= true;
373 link
->ifindex
= ifindex
;
374 link
->iftype
= iftype
;
375 link
->ifname
= strdup(ifname
);
379 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
381 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
383 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
386 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
389 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
392 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
396 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
400 r
= link_update_flags(link
, message
);
410 static void link_free(Link
*link
) {
418 while (!set_isempty(link
->addresses
))
419 address_free(set_first(link
->addresses
));
421 while (!set_isempty(link
->addresses_foreign
))
422 address_free(set_first(link
->addresses_foreign
));
424 link
->addresses
= set_free(link
->addresses
);
426 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
428 while ((address
= link
->pool_addresses
)) {
429 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
430 address_free(address
);
433 sd_dhcp_server_unref(link
->dhcp_server
);
434 sd_dhcp_client_unref(link
->dhcp_client
);
435 sd_dhcp_lease_unref(link
->dhcp_lease
);
437 link_lldp_tx_stop(link
);
439 free(link
->lease_file
);
441 sd_lldp_unref(link
->lldp
);
442 free(link
->lldp_file
);
444 sd_ipv4ll_unref(link
->ipv4ll
);
445 sd_dhcp6_client_unref(link
->dhcp6_client
);
446 sd_ndisc_unref(link
->ndisc_router_discovery
);
449 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
453 (void)unlink(link
->state_file
);
454 free(link
->state_file
);
456 udev_device_unref(link
->udev_device
);
458 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
459 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
460 hashmap_free(link
->bound_to_links
);
462 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
463 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
464 hashmap_free(link
->bound_by_links
);
469 Link
*link_unref(Link
*link
) {
473 assert(link
->n_ref
> 0);
485 Link
*link_ref(Link
*link
) {
489 assert(link
->n_ref
> 0);
496 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
503 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
512 static void link_set_state(Link
*link
, LinkState state
) {
515 if (link
->state
== state
)
520 link_send_changed(link
, "AdministrativeState", NULL
);
525 static void link_enter_unmanaged(Link
*link
) {
528 log_link_debug(link
, "Unmanaged");
530 link_set_state(link
, LINK_STATE_UNMANAGED
);
535 static int link_stop_clients(Link
*link
) {
539 assert(link
->manager
);
540 assert(link
->manager
->event
);
542 if (link
->dhcp_client
) {
543 k
= sd_dhcp_client_stop(link
->dhcp_client
);
545 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
549 k
= sd_ipv4ll_stop(link
->ipv4ll
);
551 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
554 if (link
->dhcp6_client
) {
555 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
557 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
560 if (link
->ndisc_router_discovery
) {
561 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
563 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
566 link_lldp_tx_stop(link
);
570 void link_enter_failed(Link
*link
) {
573 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
576 log_link_warning(link
, "Failed");
578 link_set_state(link
, LINK_STATE_FAILED
);
580 link_stop_clients(link
);
585 static Address
* link_find_dhcp_server_address(Link
*link
) {
589 assert(link
->network
);
591 /* The first statically configured address if there is any */
592 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
594 if (address
->family
!= AF_INET
)
597 if (in_addr_is_null(address
->family
, &address
->in_addr
))
603 /* If that didn't work, find a suitable address we got from the pool */
604 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
605 if (address
->family
!= AF_INET
)
614 static int link_enter_configured(Link
*link
) {
616 assert(link
->network
);
617 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
619 log_link_info(link
, "Configured");
621 link_set_state(link
, LINK_STATE_CONFIGURED
);
628 void link_check_ready(Link
*link
) {
637 if (!link
->static_configured
)
640 if (link_ipv4ll_enabled(link
))
641 if (!link
->ipv4ll_address
||
645 if (link_ipv6ll_enabled(link
))
646 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
649 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
650 !link
->dhcp4_configured
) ||
651 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
652 !link
->dhcp6_configured
) ||
653 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
654 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
657 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
660 SET_FOREACH(a
, link
->addresses
, i
)
661 if (!address_is_ready(a
))
664 if (link
->state
!= LINK_STATE_CONFIGURED
)
665 link_enter_configured(link
);
670 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
671 _cleanup_link_unref_ Link
*link
= userdata
;
674 assert(link
->link_messages
> 0);
675 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
676 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
679 link
->link_messages
--;
681 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
684 r
= sd_netlink_message_get_errno(m
);
685 if (r
< 0 && r
!= -EEXIST
)
686 log_link_warning_errno(link
, r
, "Could not set route: %m");
688 if (link
->link_messages
== 0) {
689 log_link_debug(link
, "Routes set");
690 link
->static_configured
= true;
691 link_check_ready(link
);
697 static int link_enter_set_routes(Link
*link
) {
702 assert(link
->network
);
703 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
705 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
707 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
708 r
= route_configure(rt
, link
, &route_handler
);
710 log_link_warning_errno(link
, r
, "Could not set routes: %m");
711 link_enter_failed(link
);
715 link
->link_messages
++;
718 if (link
->link_messages
== 0) {
719 link
->static_configured
= true;
720 link_check_ready(link
);
722 log_link_debug(link
, "Setting routes");
727 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
728 _cleanup_link_unref_ Link
*link
= userdata
;
733 assert(link
->ifname
);
735 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
738 r
= sd_netlink_message_get_errno(m
);
739 if (r
< 0 && r
!= -ESRCH
)
740 log_link_warning_errno(link
, r
, "Could not drop route: %m");
745 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
746 _cleanup_link_unref_ Link
*link
= userdata
;
752 assert(link
->ifname
);
753 assert(link
->link_messages
> 0);
754 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
755 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
757 link
->link_messages
--;
759 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
762 r
= sd_netlink_message_get_errno(m
);
763 if (r
< 0 && r
!= -EEXIST
)
764 log_link_warning_errno(link
, r
, "could not set address: %m");
766 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
768 if (link
->link_messages
== 0) {
769 log_link_debug(link
, "Addresses set");
770 link_enter_set_routes(link
);
776 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
777 _cleanup_free_
struct in_addr
*addresses
= NULL
;
778 size_t n_addresses
= 0, n_allocated
= 0;
781 log_debug("Copying DNS server information from %s", link
->ifname
);
786 STRV_FOREACH(a
, link
->network
->dns
) {
789 /* Only look for IPv4 addresses */
790 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
793 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
796 addresses
[n_addresses
++] = ia
;
799 if (link
->network
->dhcp_use_dns
&&
801 const struct in_addr
*da
= NULL
;
804 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
807 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
810 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
815 if (n_addresses
<= 0)
818 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
821 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
822 _cleanup_free_
struct in_addr
*addresses
= NULL
;
823 size_t n_addresses
= 0, n_allocated
= 0;
829 log_debug("Copying NTP server information from %s", link
->ifname
);
831 STRV_FOREACH(a
, link
->network
->ntp
) {
834 /* Only look for IPv4 addresses */
835 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
838 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
841 addresses
[n_addresses
++] = ia
;
844 if (link
->network
->dhcp_use_ntp
&&
846 const struct in_addr
*da
= NULL
;
849 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
852 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
855 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
860 if (n_addresses
<= 0)
863 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
866 static int link_enter_set_addresses(Link
*link
) {
871 assert(link
->network
);
872 assert(link
->state
!= _LINK_STATE_INVALID
);
874 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
876 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
877 r
= address_configure(ad
, link
, &address_handler
, false);
879 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
880 link_enter_failed(link
);
884 link
->link_messages
++;
887 /* now that we can figure out a default address for the dhcp server,
889 if (link_dhcp4_server_enabled(link
)) {
892 bool acquired_uplink
= false;
894 address
= link_find_dhcp_server_address(link
);
896 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
897 link_enter_failed(link
);
901 /* use the server address' subnet as the pool */
902 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
903 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
908 r = sd_dhcp_server_set_router(link->dhcp_server,
909 &main_address->in_addr.in);
914 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
915 r
= sd_dhcp_server_set_max_lease_time(
917 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
922 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
923 r
= sd_dhcp_server_set_default_lease_time(
925 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
930 if (link
->network
->dhcp_server_emit_dns
) {
932 if (link
->network
->n_dhcp_server_dns
> 0)
933 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
935 uplink
= manager_find_uplink(link
->manager
, link
);
936 acquired_uplink
= true;
939 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
942 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
945 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
949 if (link
->network
->dhcp_server_emit_ntp
) {
951 if (link
->network
->n_dhcp_server_ntp
> 0)
952 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
954 if (!acquired_uplink
)
955 uplink
= manager_find_uplink(link
->manager
, link
);
958 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
961 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
965 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
968 if (link
->network
->dhcp_server_emit_timezone
) {
969 _cleanup_free_
char *buffer
= NULL
;
970 const char *tz
= NULL
;
972 if (link
->network
->dhcp_server_timezone
)
973 tz
= link
->network
->dhcp_server_timezone
;
975 r
= get_timezone(&buffer
);
977 log_warning_errno(r
, "Failed to determine timezone: %m");
983 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
989 r
= sd_dhcp_server_start(link
->dhcp_server
);
991 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
993 link_enter_failed(link
);
998 log_link_debug(link
, "Offering DHCPv4 leases");
1001 if (link
->link_messages
== 0)
1002 link_enter_set_routes(link
);
1004 log_link_debug(link
, "Setting addresses");
1009 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1010 _cleanup_link_unref_ Link
*link
= userdata
;
1015 assert(link
->ifname
);
1017 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1020 r
= sd_netlink_message_get_errno(m
);
1021 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1022 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1027 static int link_set_bridge_fdb(Link
*const link
) {
1028 FdbEntry
*fdb_entry
;
1031 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1032 r
= fdb_entry_configure(link
, fdb_entry
);
1034 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1042 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1043 _cleanup_link_unref_ Link
*link
= userdata
;
1046 log_link_debug(link
, "Set link");
1048 r
= sd_netlink_message_get_errno(m
);
1049 if (r
< 0 && r
!= -EEXIST
) {
1050 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1051 link_enter_failed(link
);
1058 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1059 _cleanup_link_unref_ Link
*link
= userdata
;
1060 const sd_bus_error
*e
;
1065 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1068 e
= sd_bus_message_get_error(m
);
1070 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1075 int link_set_hostname(Link
*link
, const char *hostname
) {
1079 assert(link
->manager
);
1081 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1083 if (!link
->manager
->bus
) {
1084 /* TODO: replace by assert when we can rely on kdbus */
1085 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1089 r
= sd_bus_call_method_async(
1092 "org.freedesktop.hostname1",
1093 "/org/freedesktop/hostname1",
1094 "org.freedesktop.hostname1",
1096 set_hostname_handler
,
1103 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1110 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1111 _cleanup_link_unref_ Link
*link
= userdata
;
1112 const sd_bus_error
*e
;
1117 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1120 e
= sd_bus_message_get_error(m
);
1122 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1127 int link_set_timezone(Link
*link
, const char *tz
) {
1131 assert(link
->manager
);
1134 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1136 if (!link
->manager
->bus
) {
1137 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1141 r
= sd_bus_call_method_async(
1144 "org.freedesktop.timedate1",
1145 "/org/freedesktop/timedate1",
1146 "org.freedesktop.timedate1",
1148 set_timezone_handler
,
1154 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1161 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1162 _cleanup_link_unref_ Link
*link
= userdata
;
1167 assert(link
->ifname
);
1169 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1172 r
= sd_netlink_message_get_errno(m
);
1174 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1179 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1180 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1184 assert(link
->manager
);
1185 assert(link
->manager
->rtnl
);
1187 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1189 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1191 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1193 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1195 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1197 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1199 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1206 static int link_set_bridge(Link
*link
) {
1207 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1211 assert(link
->network
);
1213 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1215 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1217 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1219 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1221 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1223 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1225 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1227 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1229 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1231 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1233 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1235 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1237 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1239 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1241 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1243 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1245 if (link
->network
->cost
!= 0) {
1246 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1248 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1251 r
= sd_netlink_message_close_container(req
);
1253 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1255 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1257 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1264 static int link_lldp_save(Link
*link
) {
1265 _cleanup_free_
char *temp_path
= NULL
;
1266 _cleanup_fclose_
FILE *f
= NULL
;
1267 sd_lldp_neighbor
**l
= NULL
;
1271 assert(link
->lldp_file
);
1274 (void) unlink(link
->lldp_file
);
1278 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1282 (void) unlink(link
->lldp_file
);
1288 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1292 fchmod(fileno(f
), 0644);
1294 for (i
= 0; i
< n
; i
++) {
1299 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1304 (void) fwrite(&u
, 1, sizeof(u
), f
);
1305 (void) fwrite(p
, 1, sz
, f
);
1308 r
= fflush_and_check(f
);
1312 if (rename(temp_path
, link
->lldp_file
) < 0) {
1319 (void) unlink(link
->lldp_file
);
1321 (void) unlink(temp_path
);
1323 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1327 for (i
= 0; i
< n
; i
++)
1328 sd_lldp_neighbor_unref(l
[i
]);
1335 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1336 Link
*link
= userdata
;
1341 (void) link_lldp_save(link
);
1343 if (link_lldp_tx_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1344 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1346 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1348 r
= link_lldp_tx_start(link
);
1350 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1354 static int link_acquire_ipv6_conf(Link
*link
) {
1359 if (link_dhcp6_enabled(link
)) {
1360 assert(link
->dhcp6_client
);
1361 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1363 log_link_debug(link
, "Acquiring DHCPv6 lease");
1365 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1366 if (r
< 0 && r
!= -EBUSY
)
1367 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1369 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1370 if (r
< 0 && r
!= -EBUSY
)
1371 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1374 if (link_ipv6_accept_ra_enabled(link
)) {
1375 assert(link
->ndisc_router_discovery
);
1377 log_link_debug(link
, "Discovering IPv6 routers");
1379 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1380 if (r
< 0 && r
!= -EBUSY
)
1381 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1387 static int link_acquire_conf(Link
*link
) {
1391 assert(link
->network
);
1392 assert(link
->manager
);
1393 assert(link
->manager
->event
);
1395 if (link_ipv4ll_enabled(link
)) {
1396 assert(link
->ipv4ll
);
1398 log_link_debug(link
, "Acquiring IPv4 link-local address");
1400 r
= sd_ipv4ll_start(link
->ipv4ll
);
1402 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1405 if (link_dhcp4_enabled(link
)) {
1406 assert(link
->dhcp_client
);
1408 log_link_debug(link
, "Acquiring DHCPv4 lease");
1410 r
= sd_dhcp_client_start(link
->dhcp_client
);
1412 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1415 if (link_lldp_tx_enabled(link
)) {
1416 r
= link_lldp_tx_start(link
);
1418 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1424 bool link_has_carrier(Link
*link
) {
1425 /* see Documentation/networking/operstates.txt in the kernel sources */
1427 if (link
->kernel_operstate
== IF_OPER_UP
)
1430 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1431 /* operstate may not be implemented, so fall back to flags */
1432 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1438 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1439 _cleanup_link_unref_ Link
*link
= userdata
;
1444 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1447 r
= sd_netlink_message_get_errno(m
);
1449 /* we warn but don't fail the link, as it may be
1451 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1456 static int link_up(Link
*link
) {
1457 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1458 uint8_t ipv6ll_mode
;
1462 assert(link
->network
);
1463 assert(link
->manager
);
1464 assert(link
->manager
->rtnl
);
1466 log_link_debug(link
, "Bringing link up");
1468 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1470 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1472 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1474 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1476 if (link
->network
->mac
) {
1477 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1479 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1482 if (link
->network
->mtu
) {
1483 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1485 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1488 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1490 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1492 if (socket_ipv6_is_supported()) {
1493 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1494 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1496 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1498 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1499 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1501 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1503 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1504 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1506 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1509 r
= sd_netlink_message_close_container(req
);
1511 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1514 r
= sd_netlink_message_close_container(req
);
1516 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1518 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1520 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1527 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1528 _cleanup_link_unref_ Link
*link
= userdata
;
1533 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1536 r
= sd_netlink_message_get_errno(m
);
1538 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1543 static int link_down(Link
*link
) {
1544 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1548 assert(link
->manager
);
1549 assert(link
->manager
->rtnl
);
1551 log_link_debug(link
, "Bringing link down");
1553 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1554 RTM_SETLINK
, link
->ifindex
);
1556 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1558 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1560 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1562 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1564 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1571 static int link_handle_bound_to_list(Link
*link
) {
1575 bool required_up
= false;
1576 bool link_is_up
= false;
1580 if (hashmap_isempty(link
->bound_to_links
))
1583 if (link
->flags
& IFF_UP
)
1586 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1587 if (link_has_carrier(l
)) {
1592 if (!required_up
&& link_is_up
) {
1593 r
= link_down(link
);
1596 } else if (required_up
&& !link_is_up
) {
1605 static int link_handle_bound_by_list(Link
*link
) {
1612 if (hashmap_isempty(link
->bound_by_links
))
1615 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1616 r
= link_handle_bound_to_list(l
);
1624 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1630 if (link
== carrier
)
1633 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1636 r
= hashmap_ensure_allocated(h
, NULL
);
1640 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1647 static int link_new_bound_by_list(Link
*link
) {
1652 bool list_updated
= false;
1655 assert(link
->manager
);
1659 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1660 if (!carrier
->network
)
1663 if (strv_isempty(carrier
->network
->bind_carrier
))
1666 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1667 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1671 list_updated
= true;
1678 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1679 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1683 link_dirty(carrier
);
1689 static int link_new_bound_to_list(Link
*link
) {
1694 bool list_updated
= false;
1697 assert(link
->manager
);
1702 if (strv_isempty(link
->network
->bind_carrier
))
1707 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1708 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1709 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1713 list_updated
= true;
1720 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1721 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1725 link_dirty(carrier
);
1731 static int link_new_carrier_maps(Link
*link
) {
1734 r
= link_new_bound_by_list(link
);
1738 r
= link_handle_bound_by_list(link
);
1742 r
= link_new_bound_to_list(link
);
1746 r
= link_handle_bound_to_list(link
);
1753 static void link_free_bound_to_list(Link
*link
) {
1757 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1758 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1760 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1761 link_dirty(bound_to
);
1767 static void link_free_bound_by_list(Link
*link
) {
1771 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1772 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1774 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1775 link_dirty(bound_by
);
1776 link_handle_bound_to_list(bound_by
);
1783 static void link_free_carrier_maps(Link
*link
) {
1784 bool list_updated
= false;
1788 if (!hashmap_isempty(link
->bound_to_links
)) {
1789 link_free_bound_to_list(link
);
1790 list_updated
= true;
1793 if (!hashmap_isempty(link
->bound_by_links
)) {
1794 link_free_bound_by_list(link
);
1795 list_updated
= true;
1804 void link_drop(Link
*link
) {
1805 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1808 link_set_state(link
, LINK_STATE_LINGER
);
1810 link_free_carrier_maps(link
);
1812 log_link_debug(link
, "Link removed");
1814 (void)unlink(link
->state_file
);
1820 static int link_joined(Link
*link
) {
1824 assert(link
->network
);
1826 if (!hashmap_isempty(link
->bound_to_links
)) {
1827 r
= link_handle_bound_to_list(link
);
1830 } else if (!(link
->flags
& IFF_UP
)) {
1833 link_enter_failed(link
);
1838 if (link
->network
->bridge
) {
1839 r
= link_set_bridge(link
);
1841 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1844 return link_enter_set_addresses(link
);
1847 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1848 _cleanup_link_unref_ Link
*link
= userdata
;
1852 assert(link
->network
);
1856 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1859 r
= sd_netlink_message_get_errno(m
);
1860 if (r
< 0 && r
!= -EEXIST
) {
1861 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1862 link_enter_failed(link
);
1865 log_link_debug(link
, "Joined netdev");
1867 if (link
->enslaving
<= 0)
1873 static int link_enter_join_netdev(Link
*link
) {
1879 assert(link
->network
);
1880 assert(link
->state
== LINK_STATE_PENDING
);
1882 link_set_state(link
, LINK_STATE_ENSLAVING
);
1886 if (!link
->network
->bridge
&&
1887 !link
->network
->bond
&&
1888 hashmap_isempty(link
->network
->stacked_netdevs
))
1889 return link_joined(link
);
1891 if (link
->network
->bond
) {
1892 log_struct(LOG_DEBUG
,
1893 LOG_LINK_INTERFACE(link
),
1894 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1895 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1898 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1900 log_struct_errno(LOG_WARNING
, r
,
1901 LOG_LINK_INTERFACE(link
),
1902 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1903 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1906 link_enter_failed(link
);
1913 if (link
->network
->bridge
) {
1914 log_struct(LOG_DEBUG
,
1915 LOG_LINK_INTERFACE(link
),
1916 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1917 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1920 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1922 log_struct_errno(LOG_WARNING
, r
,
1923 LOG_LINK_INTERFACE(link
),
1924 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1925 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1927 link_enter_failed(link
);
1934 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1936 log_struct(LOG_DEBUG
,
1937 LOG_LINK_INTERFACE(link
),
1938 LOG_NETDEV_INTERFACE(netdev
),
1939 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1942 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1944 log_struct_errno(LOG_WARNING
, r
,
1945 LOG_LINK_INTERFACE(link
),
1946 LOG_NETDEV_INTERFACE(netdev
),
1947 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1949 link_enter_failed(link
);
1959 static int link_set_ipv4_forward(Link
*link
) {
1962 if (!link_ipv4_forward_enabled(link
))
1965 /* We propagate the forwarding flag from one interface to the
1966 * global setting one way. This means: as long as at least one
1967 * interface was configured at any time that had IP forwarding
1968 * enabled the setting will stay on for good. We do this
1969 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1970 * somewhat in sync (see below). */
1972 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1974 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1979 static int link_set_ipv6_forward(Link
*link
) {
1982 if (!link_ipv6_forward_enabled(link
))
1985 /* On Linux, the IPv6 stack does not not know a per-interface
1986 * packet forwarding setting: either packet forwarding is on
1987 * for all, or off for all. We hence don't bother with a
1988 * per-interface setting, but simply propagate the interface
1989 * flag, if it is set, to the global flag, one-way. Note that
1990 * while IPv4 would allow a per-interface flag, we expose the
1991 * same behaviour there and also propagate the setting from
1992 * one to all, to keep things simple (see above). */
1994 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1996 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2001 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2002 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2003 IPv6PrivacyExtensions s
;
2004 const char *p
= NULL
;
2007 s
= link_ipv6_privacy_extensions(link
);
2011 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2012 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2014 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2016 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2021 static int link_set_ipv6_accept_ra(Link
*link
) {
2022 const char *p
= NULL
;
2025 /* Make this a NOP if IPv6 is not available */
2026 if (!socket_ipv6_is_supported())
2029 if (link
->flags
& IFF_LOOPBACK
)
2035 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2037 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2038 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2040 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2045 static int link_set_ipv6_dad_transmits(Link
*link
) {
2046 char buf
[DECIMAL_STR_MAX(int) + 1];
2047 const char *p
= NULL
;
2050 /* Make this a NOP if IPv6 is not available */
2051 if (!socket_ipv6_is_supported())
2054 if (link
->flags
& IFF_LOOPBACK
)
2060 if (link
->network
->ipv6_dad_transmits
< 0)
2063 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2064 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2066 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2068 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2073 static int link_set_ipv6_hop_limit(Link
*link
) {
2074 char buf
[DECIMAL_STR_MAX(int) + 1];
2075 const char *p
= NULL
;
2078 /* Make this a NOP if IPv6 is not available */
2079 if (!socket_ipv6_is_supported())
2082 if (link
->flags
& IFF_LOOPBACK
)
2088 if (link
->network
->ipv6_hop_limit
< 0)
2091 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2092 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2094 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2096 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2101 static int link_drop_foreign_config(Link
*link
) {
2107 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2108 /* we consider IPv6LL addresses to be managed by the kernel */
2109 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2112 r
= address_remove(address
, link
, link_address_remove_handler
);
2117 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2118 /* do not touch routes managed by the kernel */
2119 if (route
->protocol
== RTPROT_KERNEL
)
2122 r
= route_remove(route
, link
, link_address_remove_handler
);
2130 static int link_update_lldp(Link
*link
) {
2138 if (link
->flags
& IFF_UP
) {
2139 r
= sd_lldp_start(link
->lldp
);
2141 log_link_debug(link
, "Started LLDP.");
2143 r
= sd_lldp_stop(link
->lldp
);
2145 log_link_debug(link
, "Stopped LLDP.");
2151 static int link_configure(Link
*link
) {
2155 assert(link
->network
);
2156 assert(link
->state
== LINK_STATE_PENDING
);
2158 /* Drop foreign config, but ignore loopback or critical devices.
2159 * We do not want to remove loopback address or addresses used for root NFS. */
2160 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2161 r
= link_drop_foreign_config(link
);
2166 r
= link_set_bridge_fdb(link
);
2170 r
= link_set_ipv4_forward(link
);
2174 r
= link_set_ipv6_forward(link
);
2178 r
= link_set_ipv6_privacy_extensions(link
);
2182 r
= link_set_ipv6_accept_ra(link
);
2186 r
= link_set_ipv6_dad_transmits(link
);
2190 r
= link_set_ipv6_hop_limit(link
);
2194 if (link_ipv4ll_enabled(link
)) {
2195 r
= ipv4ll_configure(link
);
2200 if (link_dhcp4_enabled(link
)) {
2201 r
= dhcp4_configure(link
);
2206 if (link_dhcp4_server_enabled(link
)) {
2207 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2211 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2216 if (link_dhcp6_enabled(link
) ||
2217 link_ipv6_accept_ra_enabled(link
)) {
2218 r
= dhcp6_configure(link
);
2223 if (link_ipv6_accept_ra_enabled(link
)) {
2224 r
= ndisc_configure(link
);
2229 if (link_lldp_rx_enabled(link
)) {
2230 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2234 r
= sd_lldp_match_capabilities(link
->lldp
,
2235 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2236 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2237 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2241 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2245 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2249 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2253 r
= link_update_lldp(link
);
2258 if (link_has_carrier(link
)) {
2259 r
= link_acquire_conf(link
);
2263 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2264 r
= link_acquire_ipv6_conf(link
);
2270 return link_enter_join_netdev(link
);
2273 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2275 _cleanup_link_unref_ Link
*link
= userdata
;
2280 assert(link
->ifname
);
2281 assert(link
->manager
);
2283 if (link
->state
!= LINK_STATE_PENDING
)
2286 log_link_debug(link
, "Link state is up-to-date");
2288 r
= link_new_bound_by_list(link
);
2292 r
= link_handle_bound_by_list(link
);
2296 if (!link
->network
) {
2297 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2298 &link
->mac
, &network
);
2300 link_enter_unmanaged(link
);
2305 if (link
->flags
& IFF_LOOPBACK
) {
2306 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2307 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2309 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2310 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2312 if (network
->dhcp_server
)
2313 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2316 r
= network_apply(link
->manager
, network
, link
);
2321 r
= link_new_bound_to_list(link
);
2325 r
= link_configure(link
);
2332 int link_initialized(Link
*link
, struct udev_device
*device
) {
2333 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2337 assert(link
->manager
);
2338 assert(link
->manager
->rtnl
);
2341 if (link
->state
!= LINK_STATE_PENDING
)
2344 if (link
->udev_device
)
2347 log_link_debug(link
, "udev initialized link");
2349 link
->udev_device
= udev_device_ref(device
);
2351 /* udev has initialized the link, but we don't know if we have yet
2352 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2353 * when it returns we know that the pending NEWLINKs have already been
2354 * processed and that we are up-to-date */
2356 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2361 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2362 link_initialized_and_synced
, link
, 0, NULL
);
2371 static int link_load(Link
*link
) {
2372 _cleanup_free_
char *network_file
= NULL
,
2375 *dhcp4_address
= NULL
,
2376 *ipv4ll_address
= NULL
;
2377 union in_addr_union address
;
2378 union in_addr_union route_dst
;
2384 r
= parse_env_file(link
->state_file
, NEWLINE
,
2385 "NETWORK_FILE", &network_file
,
2386 "ADDRESSES", &addresses
,
2388 "DHCP4_ADDRESS", &dhcp4_address
,
2389 "IPV4LL_ADDRESS", &ipv4ll_address
,
2391 if (r
< 0 && r
!= -ENOENT
)
2392 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2399 suffix
= strrchr(network_file
, '.');
2401 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2402 goto network_file_fail
;
2406 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2408 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2409 goto network_file_fail
;
2412 r
= network_apply(link
->manager
, network
, link
);
2414 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2423 _cleanup_free_
char *address_str
= NULL
;
2424 char *prefixlen_str
;
2426 unsigned char prefixlen
;
2428 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2430 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2436 prefixlen_str
= strchr(address_str
, '/');
2437 if (!prefixlen_str
) {
2438 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2442 *prefixlen_str
++ = '\0';
2444 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2446 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2450 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2452 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2456 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2458 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2467 _cleanup_free_
char *route_str
= NULL
;
2468 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2470 char *prefixlen_str
;
2472 unsigned char prefixlen
, tos
, table
;
2475 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2477 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2483 prefixlen_str
= strchr(route_str
, '/');
2484 if (!prefixlen_str
) {
2485 log_link_debug(link
, "Failed to parse route %s", route_str
);
2489 *prefixlen_str
++ = '\0';
2491 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2493 log_link_debug(link
,
2494 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2499 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2501 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2505 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2507 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2509 if (lifetime
!= USEC_INFINITY
) {
2510 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2511 0, route_expire_handler
, route
);
2513 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2516 route
->lifetime
= lifetime
;
2517 sd_event_source_unref(route
->expire
);
2518 route
->expire
= expire
;
2523 if (dhcp4_address
) {
2524 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2526 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2527 goto dhcp4_address_fail
;
2530 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2532 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2534 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2536 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2541 if (ipv4ll_address
) {
2542 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2544 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2545 goto ipv4ll_address_fail
;
2548 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2550 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2552 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2554 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2557 ipv4ll_address_fail
:
2562 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2564 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2565 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2573 r
= link_new(m
, message
, ret
);
2579 log_link_debug(link
, "Link %d added", link
->ifindex
);
2581 r
= link_load(link
);
2585 if (detect_container() <= 0) {
2586 /* not in a container, udev will be around */
2587 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2588 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2590 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2594 if (udev_device_get_is_initialized(device
) <= 0) {
2596 log_link_debug(link
, "link pending udev initialization...");
2600 r
= link_initialized(link
, device
);
2604 /* we are calling a callback directly, so must take a ref */
2607 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2614 link_enter_failed(link
);
2618 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2623 log_link_info(link
, "Gained IPv6LL");
2625 link
->ipv6ll_address
= *address
;
2626 link_check_ready(link
);
2628 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2629 r
= link_acquire_ipv6_conf(link
);
2631 link_enter_failed(link
);
2639 static int link_carrier_gained(Link
*link
) {
2644 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2645 r
= link_acquire_conf(link
);
2647 link_enter_failed(link
);
2652 r
= link_handle_bound_by_list(link
);
2659 static int link_carrier_lost(Link
*link
) {
2664 r
= link_stop_clients(link
);
2666 link_enter_failed(link
);
2670 r
= link_handle_bound_by_list(link
);
2677 int link_carrier_reset(Link
*link
) {
2682 if (link_has_carrier(link
)) {
2683 r
= link_carrier_lost(link
);
2687 r
= link_carrier_gained(link
);
2691 log_link_info(link
, "Reset carrier");
2697 int link_update(Link
*link
, sd_netlink_message
*m
) {
2698 struct ether_addr mac
;
2701 bool had_carrier
, carrier_gained
, carrier_lost
;
2705 assert(link
->ifname
);
2708 if (link
->state
== LINK_STATE_LINGER
) {
2710 log_link_info(link
, "Link readded");
2711 link_set_state(link
, LINK_STATE_ENSLAVING
);
2713 r
= link_new_carrier_maps(link
);
2718 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2719 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2720 log_link_info(link
, "Renamed to %s", ifname
);
2722 link_free_carrier_maps(link
);
2724 r
= free_and_strdup(&link
->ifname
, ifname
);
2728 r
= link_new_carrier_maps(link
);
2733 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2734 if (r
>= 0 && mtu
> 0) {
2736 if (!link
->original_mtu
) {
2737 link
->original_mtu
= mtu
;
2738 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2741 if (link
->dhcp_client
) {
2742 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2745 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2751 /* The kernel may broadcast NEWLINK messages without the MAC address
2752 set, simply ignore them. */
2753 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2755 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2758 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2761 log_link_debug(link
, "MAC address: "
2762 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2763 mac
.ether_addr_octet
[0],
2764 mac
.ether_addr_octet
[1],
2765 mac
.ether_addr_octet
[2],
2766 mac
.ether_addr_octet
[3],
2767 mac
.ether_addr_octet
[4],
2768 mac
.ether_addr_octet
[5]);
2771 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2773 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2776 if (link
->dhcp_client
) {
2777 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2778 (const uint8_t *) &link
->mac
,
2782 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2785 if (link
->dhcp6_client
) {
2786 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2787 (const uint8_t *) &link
->mac
,
2791 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2796 had_carrier
= link_has_carrier(link
);
2798 r
= link_update_flags(link
, m
);
2802 r
= link_update_lldp(link
);
2806 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2807 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2809 if (carrier_gained
) {
2810 log_link_info(link
, "Gained carrier");
2812 r
= link_carrier_gained(link
);
2815 } else if (carrier_lost
) {
2816 log_link_info(link
, "Lost carrier");
2818 r
= link_carrier_lost(link
);
2826 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2834 if (hashmap_isempty(h
))
2838 HASHMAP_FOREACH(link
, h
, i
) {
2842 fprintf(f
, "%i", link
->ifindex
);
2849 int link_save(Link
*link
) {
2850 _cleanup_free_
char *temp_path
= NULL
;
2851 _cleanup_fclose_
FILE *f
= NULL
;
2852 const char *admin_state
, *oper_state
;
2859 assert(link
->state_file
);
2860 assert(link
->lease_file
);
2861 assert(link
->manager
);
2863 if (link
->state
== LINK_STATE_LINGER
) {
2864 unlink(link
->state_file
);
2868 link_lldp_save(link
);
2870 admin_state
= link_state_to_string(link
->state
);
2871 assert(admin_state
);
2873 oper_state
= link_operstate_to_string(link
->operstate
);
2876 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2880 fchmod(fileno(f
), 0644);
2883 "# This is private data. Do not parse.\n"
2886 admin_state
, oper_state
);
2888 if (link
->network
) {
2890 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2891 const char *dhcp_domainname
= NULL
;
2892 char **dhcp6_domains
= NULL
;
2894 if (link
->dhcp6_client
) {
2895 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2896 if (r
< 0 && r
!= -ENOMSG
)
2897 log_link_debug(link
, "No DHCPv6 lease");
2900 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2904 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
2906 if (link
->network
->dhcp_use_dns
&&
2908 const struct in_addr
*addresses
;
2910 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2914 serialize_in_addrs(f
, addresses
, r
);
2919 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
2920 struct in6_addr
*in6_addrs
;
2922 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2926 serialize_in6_addrs(f
, in6_addrs
, r
);
2934 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
2936 if (link
->network
->dhcp_use_ntp
&&
2938 const struct in_addr
*addresses
;
2940 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2944 serialize_in_addrs(f
, addresses
, r
);
2949 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
2950 struct in6_addr
*in6_addrs
;
2953 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2958 serialize_in6_addrs(f
, in6_addrs
, r
);
2962 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2964 fputstrv(f
, hosts
, NULL
, &space
);
2969 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
2970 if (link
->dhcp_lease
)
2971 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
2974 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
2977 fputs("DOMAINS=", f
);
2978 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
2980 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
2981 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2983 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
2984 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2988 fputs("ROUTE_DOMAINS=", f
);
2989 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
2991 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
2992 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2994 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
2995 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2999 fprintf(f
, "LLMNR=%s\n",
3000 resolve_support_to_string(link
->network
->llmnr
));
3001 fprintf(f
, "MDNS=%s\n",
3002 resolve_support_to_string(link
->network
->mdns
));
3004 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3005 fprintf(f
, "DNSSEC=%s\n",
3006 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3008 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3011 fputs("DNSSEC_NTA=", f
);
3013 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3014 fputs_with_space(f
, n
, NULL
, &space
);
3018 fputs("ADDRESSES=", f
);
3020 SET_FOREACH(a
, link
->addresses
, i
) {
3021 _cleanup_free_
char *address_str
= NULL
;
3023 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3027 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3032 fputs("ROUTES=", f
);
3034 SET_FOREACH(route
, link
->routes
, i
) {
3035 _cleanup_free_
char *route_str
= NULL
;
3037 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3041 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3042 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3049 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3050 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3052 if (link
->dhcp_lease
) {
3053 struct in_addr address
;
3054 const char *tz
= NULL
;
3056 assert(link
->network
);
3058 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3060 fprintf(f
, "TIMEZONE=%s\n", tz
);
3062 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3064 fputs("DHCP4_ADDRESS=", f
);
3065 serialize_in_addrs(f
, &address
, 1);
3069 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3077 unlink(link
->lease_file
);
3080 struct in_addr address
;
3082 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3084 fputs("IPV4LL_ADDRESS=", f
);
3085 serialize_in_addrs(f
, &address
, 1);
3090 r
= fflush_and_check(f
);
3094 if (rename(temp_path
, link
->state_file
) < 0) {
3102 (void) unlink(link
->state_file
);
3104 (void) unlink(temp_path
);
3106 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3109 /* The serialized state in /run is no longer up-to-date. */
3110 void link_dirty(Link
*link
) {
3115 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3117 /* allocation errors are ignored */
3120 r
= set_put(link
->manager
->dirty_links
, link
);
3122 /* allocation errors are ignored */
3128 /* The serialized state in /run is up-to-date */
3129 void link_clean(Link
*link
) {
3131 assert(link
->manager
);
3133 set_remove(link
->manager
->dirty_links
, link
);
3137 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3138 [LINK_STATE_PENDING
] = "pending",
3139 [LINK_STATE_ENSLAVING
] = "configuring",
3140 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3141 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3142 [LINK_STATE_CONFIGURED
] = "configured",
3143 [LINK_STATE_UNMANAGED
] = "unmanaged",
3144 [LINK_STATE_FAILED
] = "failed",
3145 [LINK_STATE_LINGER
] = "linger",
3148 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3150 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3151 [LINK_OPERSTATE_OFF
] = "off",
3152 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3153 [LINK_OPERSTATE_DORMANT
] = "dormant",
3154 [LINK_OPERSTATE_CARRIER
] = "carrier",
3155 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3156 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3159 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);