1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
4 #include <sys/socket.h>
7 #include <linux/fib_rules.h>
10 #include "sd-netlink.h"
12 #include "alloc-util.h"
14 #include "conf-parser.h"
16 #include "device-private.h"
17 #include "device-util.h"
18 #include "dns-domain.h"
21 #include "local-addresses.h"
22 #include "netlink-util.h"
23 #include "network-internal.h"
24 #include "networkd-manager.h"
25 #include "networkd-speed-meter.h"
26 #include "ordered-set.h"
27 #include "path-util.h"
30 #include "sysctl-util.h"
31 #include "tmpfile-util.h"
32 #include "udev-util.h"
35 /* use 8 MB for receive socket kernel queue. */
36 #define RCVBUF_SIZE (8*1024*1024)
38 static int setup_default_address_pool(Manager
*m
) {
44 /* Add in the well-known private address ranges. */
46 r
= address_pool_new_from_string(m
, &p
, AF_INET6
, "fd00::", 8);
50 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "10.0.0.0", 8);
54 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "172.16.0.0", 12);
58 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "192.168.0.0", 16);
65 static int manager_reset_all(Manager
*m
) {
72 HASHMAP_FOREACH(link
, m
->links
, i
) {
73 r
= link_carrier_reset(link
);
75 log_link_warning_errno(link
, r
, "Could not reset carrier: %m");
81 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
82 Manager
*m
= userdata
;
88 r
= sd_bus_message_read(message
, "b", &b
);
90 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
97 log_debug("Coming back from suspend, resetting all connections...");
99 (void) manager_reset_all(m
);
104 static int on_connected(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
105 Manager
*m
= userdata
;
110 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
111 if (m
->dynamic_hostname
)
112 (void) manager_set_hostname(m
, m
->dynamic_hostname
);
113 if (m
->dynamic_timezone
)
114 (void) manager_set_timezone(m
, m
->dynamic_timezone
);
115 if (m
->links_requesting_uuid
)
116 (void) manager_request_product_uuid(m
, NULL
);
121 int manager_connect_bus(Manager
*m
) {
129 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-network");
131 return log_error_errno(r
, "Failed to connect to bus: %m");
133 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable
, m
);
135 return log_error_errno(r
, "Failed to add manager object vtable: %m");
137 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable
, link_object_find
, m
);
139 return log_error_errno(r
, "Failed to add link object vtable: %m");
141 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/link", link_node_enumerator
, m
);
143 return log_error_errno(r
, "Failed to add link enumerator: %m");
145 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable
, network_object_find
, m
);
147 return log_error_errno(r
, "Failed to add network object vtable: %m");
149 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/network", network_node_enumerator
, m
);
151 return log_error_errno(r
, "Failed to add network enumerator: %m");
153 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.network1", 0, NULL
, NULL
);
155 return log_error_errno(r
, "Failed to request name: %m");
157 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
159 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
161 r
= sd_bus_match_signal_async(
164 "org.freedesktop.DBus.Local",
166 "org.freedesktop.DBus.Local",
168 on_connected
, NULL
, m
);
170 return log_error_errno(r
, "Failed to request match on Connected signal: %m");
172 r
= sd_bus_match_signal_async(
175 "org.freedesktop.login1",
176 "/org/freedesktop/login1",
177 "org.freedesktop.login1.Manager",
179 match_prepare_for_sleep
, NULL
, m
);
181 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
186 static int manager_udev_process_link(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
187 Manager
*m
= userdata
;
195 r
= device_get_action(device
, &action
);
197 log_device_debug_errno(device
, r
, "Failed to get udev action, ignoring device: %m");
201 if (!IN_SET(action
, DEVICE_ACTION_ADD
, DEVICE_ACTION_CHANGE
, DEVICE_ACTION_MOVE
)) {
202 log_device_debug(device
, "Ignoring udev %s event for device.", device_action_to_string(action
));
206 r
= sd_device_get_ifindex(device
, &ifindex
);
208 log_device_debug_errno(device
, r
, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m");
212 r
= device_is_renaming(device
);
214 log_device_error_errno(device
, r
, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
215 device_action_to_string(action
));
219 log_device_debug(device
, "Interface is under renaming, wait for the interface to be renamed: %m");
223 r
= link_get(m
, ifindex
, &link
);
226 log_debug_errno(r
, "Failed to get link from ifindex %i, ignoring: %m", ifindex
);
230 (void) link_initialized(link
, device
);
235 static int manager_connect_udev(Manager
*m
) {
238 /* udev does not initialize devices inside containers,
239 * so we rely on them being already initialized before
240 * entering the container */
241 if (detect_container() > 0)
244 r
= sd_device_monitor_new(&m
->device_monitor
);
246 return log_error_errno(r
, "Failed to initialize device monitor: %m");
248 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "net", NULL
);
250 return log_error_errno(r
, "Could not add device monitor filter: %m");
252 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
254 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
256 r
= sd_device_monitor_start(m
->device_monitor
, manager_udev_process_link
, m
);
258 return log_error_errno(r
, "Failed to start device monitor: %m");
263 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
264 Manager
*m
= userdata
;
267 uint32_t ifindex
, priority
= 0;
268 unsigned char protocol
, scope
, tos
, table
, rt_type
;
270 unsigned char dst_prefixlen
, src_prefixlen
;
271 union in_addr_union dst
= IN_ADDR_NULL
, gw
= IN_ADDR_NULL
, src
= IN_ADDR_NULL
, prefsrc
= IN_ADDR_NULL
;
279 if (sd_netlink_message_is_error(message
)) {
280 r
= sd_netlink_message_get_errno(message
);
282 log_warning_errno(r
, "rtnl: failed to receive route, ignoring: %m");
287 r
= sd_netlink_message_get_type(message
, &type
);
289 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
291 } else if (!IN_SET(type
, RTM_NEWROUTE
, RTM_DELROUTE
)) {
292 log_warning("rtnl: received unexpected message type when processing route, ignoring");
296 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
298 log_debug("rtnl: received route without ifindex, ignoring");
301 log_warning_errno(r
, "rtnl: could not get ifindex from route, ignoring: %m");
303 } else if (ifindex
<= 0) {
304 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex
);
307 r
= link_get(m
, ifindex
, &link
);
308 if (r
< 0 || !link
) {
309 /* when enumerating we might be out of sync, but we will
310 * get the route again, so just ignore it */
312 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex
);
317 r
= sd_rtnl_message_route_get_family(message
, &family
);
318 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
319 log_link_warning(link
, "rtnl: received address with invalid family, ignoring");
323 r
= sd_rtnl_message_route_get_protocol(message
, &protocol
);
325 log_warning_errno(r
, "rtnl: could not get route protocol: %m");
331 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &dst
.in
);
332 if (r
< 0 && r
!= -ENODATA
) {
333 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
337 r
= sd_netlink_message_read_in_addr(message
, RTA_GATEWAY
, &gw
.in
);
338 if (r
< 0 && r
!= -ENODATA
) {
339 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
343 r
= sd_netlink_message_read_in_addr(message
, RTA_SRC
, &src
.in
);
344 if (r
< 0 && r
!= -ENODATA
) {
345 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
349 r
= sd_netlink_message_read_in_addr(message
, RTA_PREFSRC
, &prefsrc
.in
);
350 if (r
< 0 && r
!= -ENODATA
) {
351 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
358 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &dst
.in6
);
359 if (r
< 0 && r
!= -ENODATA
) {
360 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
364 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &gw
.in6
);
365 if (r
< 0 && r
!= -ENODATA
) {
366 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
370 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &src
.in6
);
371 if (r
< 0 && r
!= -ENODATA
) {
372 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
376 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &prefsrc
.in6
);
377 if (r
< 0 && r
!= -ENODATA
) {
378 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
385 assert_not_reached("Received unsupported address family");
389 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &dst_prefixlen
);
391 log_link_warning_errno(link
, r
, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
395 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &src_prefixlen
);
397 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source prefixlen, ignoring: %m");
401 r
= sd_rtnl_message_route_get_scope(message
, &scope
);
403 log_link_warning_errno(link
, r
, "rtnl: received route with invalid scope, ignoring: %m");
407 r
= sd_rtnl_message_route_get_tos(message
, &tos
);
409 log_link_warning_errno(link
, r
, "rtnl: received route with invalid tos, ignoring: %m");
413 r
= sd_rtnl_message_route_get_type(message
, &rt_type
);
415 log_link_warning_errno(link
, r
, "rtnl: received route with invalid type, ignoring: %m");
419 r
= sd_rtnl_message_route_get_table(message
, &table
);
421 log_link_warning_errno(link
, r
, "rtnl: received route with invalid table, ignoring: %m");
425 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &priority
);
426 if (r
< 0 && r
!= -ENODATA
) {
427 log_link_warning_errno(link
, r
, "rtnl: received route with invalid priority, ignoring: %m");
431 (void) route_get(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
434 _cleanup_free_
char *buf_dst
= NULL
, *buf_dst_prefixlen
= NULL
,
435 *buf_src
= NULL
, *buf_gw
= NULL
, *buf_prefsrc
= NULL
;
437 if (!in_addr_is_null(family
, &dst
)) {
438 (void) in_addr_to_string(family
, &dst
, &buf_dst
);
439 (void) asprintf(&buf_dst_prefixlen
, "/%u", dst_prefixlen
);
441 if (!in_addr_is_null(family
, &src
))
442 (void) in_addr_to_string(family
, &src
, &buf_src
);
443 if (!in_addr_is_null(family
, &gw
))
444 (void) in_addr_to_string(family
, &gw
, &buf_gw
);
445 if (!in_addr_is_null(family
, &prefsrc
))
446 (void) in_addr_to_string(family
, &prefsrc
, &buf_prefsrc
);
449 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s",
450 type
== RTM_DELROUTE
? "Removing" : route
? "Updating" : "Adding",
451 strna(buf_dst
), strempty(buf_dst_prefixlen
),
452 strna(buf_src
), strna(buf_gw
), strna(buf_prefsrc
));
458 /* A route appeared that we did not request */
459 r
= route_add_foreign(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
461 log_link_warning_errno(link
, r
, "Failed to add route, ignoring: %m");
466 route_update(route
, &src
, src_prefixlen
, &gw
, &prefsrc
, scope
, protocol
, rt_type
);
475 assert_not_reached("Received invalid RTNL message type");
481 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
482 _cleanup_free_
char *buf
= NULL
;
483 Manager
*m
= userdata
;
486 unsigned char flags
, prefixlen
, scope
;
487 union in_addr_union in_addr
= IN_ADDR_NULL
;
488 struct ifa_cacheinfo cinfo
;
489 Address
*address
= NULL
;
490 char valid_buf
[FORMAT_TIMESPAN_MAX
];
491 const char *valid_str
= NULL
;
492 int ifindex
, family
, r
;
498 if (sd_netlink_message_is_error(message
)) {
499 r
= sd_netlink_message_get_errno(message
);
501 log_warning_errno(r
, "rtnl: failed to receive address, ignoring: %m");
506 r
= sd_netlink_message_get_type(message
, &type
);
508 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
510 } else if (!IN_SET(type
, RTM_NEWADDR
, RTM_DELADDR
)) {
511 log_warning("rtnl: received unexpected message type when processing address, ignoring");
515 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
517 log_warning_errno(r
, "rtnl: could not get ifindex from address, ignoring: %m");
519 } else if (ifindex
<= 0) {
520 log_warning("rtnl: received address message with invalid ifindex, ignoring: %d", ifindex
);
523 r
= link_get(m
, ifindex
, &link
);
524 if (r
< 0 || !link
) {
525 /* when enumerating we might be out of sync, but we will
526 * get the address again, so just ignore it */
528 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex
);
533 r
= sd_rtnl_message_addr_get_family(message
, &family
);
534 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
535 log_link_warning(link
, "rtnl: received address with invalid family, ignoring");
539 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
541 log_link_warning_errno(link
, r
, "rtnl: received address with invalid prefixlen, ignoring: %m");
545 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
547 log_link_warning_errno(link
, r
, "rtnl: received address with invalid scope, ignoring: %m");
551 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
553 log_link_warning_errno(link
, r
, "rtnl: received address with invalid flags, ignoring: %m");
559 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
561 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
568 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
570 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
577 assert_not_reached("Received unsupported address family");
580 r
= in_addr_to_string(family
, &in_addr
, &buf
);
582 log_link_warning_errno(link
, r
, "Could not print address, ignoring: %m");
586 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
587 if (r
< 0 && r
!= -ENODATA
) {
588 log_link_warning_errno(link
, r
, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
590 } else if (r
>= 0 && cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
591 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
592 cinfo
.ifa_valid
* USEC_PER_SEC
,
595 (void) address_get(link
, family
, &in_addr
, prefixlen
, &address
);
600 log_link_debug(link
, "Updating address: %s/%u (valid %s%s)", buf
, prefixlen
,
601 valid_str
? "for " : "forever", strempty(valid_str
));
603 /* An address appeared that we did not request */
604 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
606 log_link_warning_errno(link
, r
, "Failed to add address %s/%u, ignoring: %m", buf
, prefixlen
);
609 log_link_debug(link
, "Adding address: %s/%u (valid %s%s)", buf
, prefixlen
,
610 valid_str
? "for " : "forever", strempty(valid_str
));
613 r
= address_update(address
, flags
, scope
, &cinfo
);
615 log_link_warning_errno(link
, r
, "Failed to update address %s/%u, ignoring: %m", buf
, prefixlen
);
624 log_link_debug(link
, "Removing address: %s/%u (valid %s%s)", buf
, prefixlen
,
625 valid_str
? "for " : "forever", strempty(valid_str
));
626 (void) address_drop(address
);
628 log_link_warning(link
, "Removing non-existent address: %s/%u (valid %s%s), ignoring", buf
, prefixlen
,
629 valid_str
? "for " : "forever", strempty(valid_str
));
633 assert_not_reached("Received invalid RTNL message type");
639 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
640 Manager
*m
= userdata
;
642 NetDev
*netdev
= NULL
;
651 if (sd_netlink_message_is_error(message
)) {
652 r
= sd_netlink_message_get_errno(message
);
654 log_warning_errno(r
, "rtnl: Could not receive link, ignoring: %m");
659 r
= sd_netlink_message_get_type(message
, &type
);
661 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
663 } else if (!IN_SET(type
, RTM_NEWLINK
, RTM_DELLINK
)) {
664 log_warning("rtnl: Received unexpected message type when processing link, ignoring");
668 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
670 log_warning_errno(r
, "rtnl: Could not get ifindex from link, ignoring: %m");
672 } else if (ifindex
<= 0) {
673 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex
);
677 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
679 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
683 (void) link_get(m
, ifindex
, &link
);
684 (void) netdev_get(m
, name
, &netdev
);
689 /* link is new, so add it */
690 r
= link_add(m
, message
, &link
);
692 log_warning_errno(r
, "Could not add new link, ignoring: %m");
698 /* netdev exists, so make sure the ifindex matches */
699 r
= netdev_set_ifindex(netdev
, message
);
701 log_warning_errno(r
, "Could not set ifindex on netdev, ignoring: %m");
706 r
= link_update(link
, message
);
708 log_warning_errno(r
, "Could not update link, ignoring: %m");
721 assert_not_reached("Received invalid RTNL message type.");
727 int manager_rtnl_process_rule(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
728 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*tmp
= NULL
;
729 RoutingPolicyRule
*rule
= NULL
;
730 const char *iif
= NULL
, *oif
= NULL
;
731 Manager
*m
= userdata
;
740 if (sd_netlink_message_is_error(message
)) {
741 r
= sd_netlink_message_get_errno(message
);
743 log_warning_errno(r
, "rtnl: failed to receive rule, ignoring: %m");
748 r
= sd_netlink_message_get_type(message
, &type
);
750 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
752 } else if (!IN_SET(type
, RTM_NEWRULE
, RTM_DELRULE
)) {
753 log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type
);
757 r
= routing_policy_rule_new(&tmp
);
763 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
765 log_warning_errno(r
, "rtnl: could not get rule family, ignoring: %m");
767 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
768 log_debug("rtnl: received address with invalid family %u, ignoring", tmp
->family
);
772 switch (tmp
->family
) {
774 r
= sd_netlink_message_read_in_addr(message
, FRA_SRC
, &tmp
->from
.in
);
775 if (r
< 0 && r
!= -ENODATA
) {
776 log_warning_errno(r
, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
779 r
= sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message
, &tmp
->from_prefixlen
);
781 log_warning_errno(r
, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
786 r
= sd_netlink_message_read_in_addr(message
, FRA_DST
, &tmp
->to
.in
);
787 if (r
< 0 && r
!= -ENODATA
) {
788 log_warning_errno(r
, "rtnl: could not get FRA_DST attribute, ignoring: %m");
791 r
= sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message
, &tmp
->to_prefixlen
);
793 log_warning_errno(r
, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
801 r
= sd_netlink_message_read_in6_addr(message
, FRA_SRC
, &tmp
->from
.in6
);
802 if (r
< 0 && r
!= -ENODATA
) {
803 log_warning_errno(r
, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
806 r
= sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message
, &tmp
->from_prefixlen
);
808 log_warning_errno(r
, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
813 r
= sd_netlink_message_read_in6_addr(message
, FRA_DST
, &tmp
->to
.in6
);
814 if (r
< 0 && r
!= -ENODATA
) {
815 log_warning_errno(r
, "rtnl: could not get FRA_DST attribute, ignoring: %m");
818 r
= sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message
, &tmp
->to_prefixlen
);
820 log_warning_errno(r
, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
828 assert_not_reached("Received unsupported address family");
831 if (tmp
->from_prefixlen
== 0 && tmp
->to_prefixlen
== 0)
834 r
= sd_rtnl_message_routing_policy_rule_get_flags(message
, &flags
);
836 log_warning_errno(r
, "rtnl: could not get flag, ignoring: %m");
839 tmp
->invert_rule
= flags
& FIB_RULE_INVERT
;
841 r
= sd_netlink_message_read_u32(message
, FRA_FWMARK
, &tmp
->fwmark
);
842 if (r
< 0 && r
!= -ENODATA
) {
843 log_warning_errno(r
, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
847 r
= sd_netlink_message_read_u32(message
, FRA_FWMASK
, &tmp
->fwmask
);
848 if (r
< 0 && r
!= -ENODATA
) {
849 log_warning_errno(r
, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
853 r
= sd_netlink_message_read_u32(message
, FRA_PRIORITY
, &tmp
->priority
);
854 if (r
< 0 && r
!= -ENODATA
) {
855 log_warning_errno(r
, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
859 r
= sd_netlink_message_read_u32(message
, FRA_TABLE
, &tmp
->table
);
860 if (r
< 0 && r
!= -ENODATA
) {
861 log_warning_errno(r
, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
865 r
= sd_rtnl_message_routing_policy_rule_get_tos(message
, &tmp
->tos
);
866 if (r
< 0 && r
!= -ENODATA
) {
867 log_warning_errno(r
, "rtnl: could not get ip rule TOS, ignoring: %m");
871 r
= sd_netlink_message_read_string(message
, FRA_IIFNAME
, &iif
);
872 if (r
< 0 && r
!= -ENODATA
) {
873 log_warning_errno(r
, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
876 r
= free_and_strdup(&tmp
->iif
, iif
);
880 r
= sd_netlink_message_read_string(message
, FRA_OIFNAME
, &oif
);
881 if (r
< 0 && r
!= -ENODATA
) {
882 log_warning_errno(r
, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
885 r
= free_and_strdup(&tmp
->oif
, oif
);
889 r
= sd_netlink_message_read_u8(message
, FRA_IP_PROTO
, &tmp
->protocol
);
890 if (r
< 0 && r
!= -ENODATA
) {
891 log_warning_errno(r
, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
895 r
= sd_netlink_message_read(message
, FRA_SPORT_RANGE
, sizeof(tmp
->sport
), &tmp
->sport
);
896 if (r
< 0 && r
!= -ENODATA
) {
897 log_warning_errno(r
, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
901 r
= sd_netlink_message_read(message
, FRA_DPORT_RANGE
, sizeof(tmp
->dport
), &tmp
->dport
);
902 if (r
< 0 && r
!= -ENODATA
) {
903 log_warning_errno(r
, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
907 (void) routing_policy_rule_get(m
, tmp
, &rule
);
912 r
= routing_policy_rule_add_foreign(m
, tmp
, &rule
);
914 log_warning_errno(r
, "Could not add rule, ignoring: %m");
920 routing_policy_rule_free(rule
);
925 assert_not_reached("Received invalid RTNL message type");
931 static int systemd_netlink_fd(void) {
932 int n
, fd
, rtnl_fd
= -EINVAL
;
934 n
= sd_listen_fds(true);
938 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
939 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
950 static int manager_connect_genl(Manager
*m
) {
955 r
= sd_genl_socket_open(&m
->genl
);
959 r
= sd_netlink_inc_rcvbuf(m
->genl
, RCVBUF_SIZE
);
963 r
= sd_netlink_attach_event(m
->genl
, m
->event
, 0);
970 static int manager_connect_rtnl(Manager
*m
) {
975 fd
= systemd_netlink_fd();
977 r
= sd_netlink_open(&m
->rtnl
);
979 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
983 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
987 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
991 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
995 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
999 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
1003 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
1007 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
1011 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
1015 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
1019 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
1026 static int ordered_set_put_in_addr_data(OrderedSet
*s
, const struct in_addr_data
*address
) {
1033 r
= in_addr_to_string(address
->family
, &address
->address
, &p
);
1037 r
= ordered_set_consume(s
, p
);
1044 static int ordered_set_put_in_addr_datav(OrderedSet
*s
, const struct in_addr_data
*addresses
, unsigned n
) {
1049 assert(addresses
|| n
== 0);
1051 for (i
= 0; i
< n
; i
++) {
1052 r
= ordered_set_put_in_addr_data(s
, addresses
+i
);
1062 static int ordered_set_put_in4_addr(OrderedSet
*s
, const struct in_addr
*address
) {
1069 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
1073 r
= ordered_set_consume(s
, p
);
1080 static int ordered_set_put_in4_addrv(OrderedSet
*s
,
1081 const struct in_addr
*addresses
,
1083 bool (*predicate
)(const struct in_addr
*addr
)) {
1088 assert(n
== 0 || addresses
);
1090 for (i
= 0; i
< n
; i
++) {
1091 if (predicate
&& !predicate(&addresses
[i
]))
1093 r
= ordered_set_put_in4_addr(s
, addresses
+i
);
1103 static int manager_save(Manager
*m
) {
1104 _cleanup_ordered_set_free_free_ OrderedSet
*dns
= NULL
, *ntp
= NULL
, *search_domains
= NULL
, *route_domains
= NULL
;
1107 _cleanup_free_
char *temp_path
= NULL
;
1108 _cleanup_fclose_
FILE *f
= NULL
;
1109 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
1110 const char *operstate_str
;
1114 assert(m
->state_file
);
1116 /* We add all NTP and DNS server to a set, to filter out duplicates */
1117 dns
= ordered_set_new(&string_hash_ops
);
1121 ntp
= ordered_set_new(&string_hash_ops
);
1125 search_domains
= ordered_set_new(&dns_name_hash_ops
);
1126 if (!search_domains
)
1129 route_domains
= ordered_set_new(&dns_name_hash_ops
);
1133 HASHMAP_FOREACH(link
, m
->links
, i
) {
1134 if (link
->flags
& IFF_LOOPBACK
)
1137 if (link
->operstate
> operstate
)
1138 operstate
= link
->operstate
;
1143 /* First add the static configured entries */
1144 r
= ordered_set_put_in_addr_datav(dns
, link
->network
->dns
, link
->network
->n_dns
);
1148 r
= ordered_set_put_strdupv(ntp
, link
->network
->ntp
);
1152 r
= ordered_set_put_string_set(search_domains
, link
->network
->search_domains
);
1156 r
= ordered_set_put_string_set(route_domains
, link
->network
->route_domains
);
1160 if (!link
->dhcp_lease
)
1163 /* Secondly, add the entries acquired via DHCP */
1164 if (link
->network
->dhcp_use_dns
) {
1165 const struct in_addr
*addresses
;
1167 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
1169 r
= ordered_set_put_in4_addrv(dns
, addresses
, r
, in4_addr_is_non_local
);
1172 } else if (r
< 0 && r
!= -ENODATA
)
1176 if (link
->network
->dhcp_use_ntp
) {
1177 const struct in_addr
*addresses
;
1179 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
1181 r
= ordered_set_put_in4_addrv(ntp
, addresses
, r
, in4_addr_is_non_local
);
1184 } else if (r
< 0 && r
!= -ENODATA
)
1188 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
1189 const char *domainname
;
1190 char **domains
= NULL
;
1192 OrderedSet
*target_domains
= (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) ? search_domains
: route_domains
;
1193 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
1195 r
= ordered_set_put_strdup(target_domains
, domainname
);
1198 } else if (r
!= -ENODATA
)
1201 r
= sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &domains
);
1203 r
= ordered_set_put_strdupv(target_domains
, domains
);
1206 } else if (r
!= -ENODATA
)
1211 operstate_str
= link_operstate_to_string(operstate
);
1212 assert(operstate_str
);
1214 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
1218 (void) fchmod(fileno(f
), 0644);
1221 "# This is private data. Do not parse.\n"
1222 "OPER_STATE=%s\n", operstate_str
);
1224 ordered_set_print(f
, "DNS=", dns
);
1225 ordered_set_print(f
, "NTP=", ntp
);
1226 ordered_set_print(f
, "DOMAINS=", search_domains
);
1227 ordered_set_print(f
, "ROUTE_DOMAINS=", route_domains
);
1229 r
= routing_policy_serialize_rules(m
->rules
, f
);
1233 r
= fflush_and_check(f
);
1237 if (rename(temp_path
, m
->state_file
) < 0) {
1242 if (m
->operational_state
!= operstate
) {
1243 m
->operational_state
= operstate
;
1244 r
= manager_send_changed(m
, "OperationalState", NULL
);
1246 log_error_errno(r
, "Could not emit changed OperationalState: %m");
1254 (void) unlink(m
->state_file
);
1255 (void) unlink(temp_path
);
1257 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
1260 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
1261 Manager
*m
= userdata
;
1270 SET_FOREACH(link
, m
->dirty_links
, i
)
1271 if (link_save(link
) >= 0)
1277 Link
*manager_dhcp6_prefix_get(Manager
*m
, struct in6_addr
*addr
) {
1278 assert_return(m
, NULL
);
1279 assert_return(addr
, NULL
);
1281 return hashmap_get(m
->dhcp6_prefixes
, addr
);
1284 static int dhcp6_route_add_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
1289 r
= sd_netlink_message_get_errno(m
);
1290 if (r
< 0 && r
!= -EEXIST
)
1291 log_link_debug_errno(link
, r
, "Received error adding DHCPv6 Prefix Delegation route: %m");
1296 int manager_dhcp6_prefix_add(Manager
*m
, struct in6_addr
*addr
, Link
*link
) {
1297 _cleanup_free_
struct in6_addr
*a
= NULL
;
1298 _cleanup_free_
char *buf
= NULL
;
1299 Link
*assigned_link
;
1303 assert_return(m
, -EINVAL
);
1304 assert_return(addr
, -EINVAL
);
1306 r
= route_add(link
, AF_INET6
, (union in_addr_union
*) addr
, 64,
1311 r
= route_configure(route
, link
, dhcp6_route_add_handler
);
1315 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
1316 log_link_debug(link
, "Adding prefix route %s/64", strnull(buf
));
1318 assigned_link
= hashmap_get(m
->dhcp6_prefixes
, addr
);
1319 if (assigned_link
) {
1320 assert(assigned_link
== link
);
1324 a
= newdup(struct in6_addr
, addr
, 1);
1328 r
= hashmap_ensure_allocated(&m
->dhcp6_prefixes
, &in6_addr_hash_ops
);
1332 r
= hashmap_put(m
->dhcp6_prefixes
, a
, link
);
1341 static int dhcp6_route_remove_handler(sd_netlink
*nl
, sd_netlink_message
*m
, Link
*link
) {
1346 r
= sd_netlink_message_get_errno(m
);
1348 log_link_debug_errno(link
, r
, "Received error on DHCPv6 Prefix Delegation route removal: %m");
1353 static int manager_dhcp6_prefix_remove(Manager
*m
, struct in6_addr
*addr
) {
1354 _cleanup_free_
struct in6_addr
*a
= NULL
;
1355 _cleanup_(link_unrefp
) Link
*l
= NULL
;
1356 _cleanup_free_
char *buf
= NULL
;
1360 assert_return(m
, -EINVAL
);
1361 assert_return(addr
, -EINVAL
);
1363 l
= hashmap_remove2(m
->dhcp6_prefixes
, addr
, (void **) &a
);
1367 (void) sd_radv_remove_prefix(l
->radv
, addr
, 64);
1368 r
= route_get(l
, AF_INET6
, (union in_addr_union
*) addr
, 64, 0, 0, 0, &route
);
1372 r
= route_remove(route
, l
, dhcp6_route_remove_handler
);
1376 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) addr
, &buf
);
1377 log_link_debug(l
, "Removing prefix route %s/64", strnull(buf
));
1382 int manager_dhcp6_prefix_remove_all(Manager
*m
, Link
*link
) {
1383 struct in6_addr
*addr
;
1387 assert_return(m
, -EINVAL
);
1388 assert_return(link
, -EINVAL
);
1390 HASHMAP_FOREACH_KEY(l
, addr
, m
->dhcp6_prefixes
, i
)
1392 (void) manager_dhcp6_prefix_remove(m
, addr
);
1397 int manager_new(Manager
**ret
) {
1398 _cleanup_(manager_freep
) Manager
*m
= NULL
;
1401 m
= new(Manager
, 1);
1406 .speed_meter_interval_usec
= SPEED_METER_DEFAULT_TIME_INTERVAL
,
1409 m
->state_file
= strdup("/run/systemd/netif/state");
1413 r
= sd_event_default(&m
->event
);
1417 (void) sd_event_set_watchdog(m
->event
, true);
1418 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
1419 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
1421 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1425 r
= manager_connect_rtnl(m
);
1429 r
= manager_connect_genl(m
);
1433 r
= manager_connect_udev(m
);
1437 r
= sd_resolve_default(&m
->resolve
);
1441 r
= sd_resolve_attach_event(m
->resolve
, m
->event
, 0);
1445 r
= setup_default_address_pool(m
);
1449 m
->duid
.type
= DUID_TYPE_EN
;
1451 (void) routing_policy_load_rules(m
->state_file
, &m
->rules_saved
);
1458 void manager_free(Manager
*m
) {
1466 free(m
->state_file
);
1468 while ((a
= hashmap_first_key(m
->dhcp6_prefixes
)))
1469 (void) manager_dhcp6_prefix_remove(m
, a
);
1470 hashmap_free(m
->dhcp6_prefixes
);
1472 while ((link
= hashmap_steal_first(m
->links
))) {
1473 if (link
->dhcp6_client
)
1474 (void) dhcp6_lease_pd_prefix_lost(link
->dhcp6_client
, link
);
1476 (void) link_stop_clients(link
, true);
1481 m
->dirty_links
= set_free_with_destructor(m
->dirty_links
, link_unref
);
1482 m
->links_requesting_uuid
= set_free_with_destructor(m
->links_requesting_uuid
, link_unref
);
1483 m
->links
= hashmap_free_with_destructor(m
->links
, link_unref
);
1485 m
->duids_requesting_uuid
= set_free(m
->duids_requesting_uuid
);
1486 m
->networks
= ordered_hashmap_free_with_destructor(m
->networks
, network_unref
);
1488 m
->netdevs
= hashmap_free_with_destructor(m
->netdevs
, netdev_unref
);
1490 while ((pool
= m
->address_pools
))
1491 address_pool_free(pool
);
1493 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1494 * So, it is necessary to set NULL after the sets are freed. */
1495 m
->rules
= set_free_with_destructor(m
->rules
, routing_policy_rule_free
);
1496 m
->rules_foreign
= set_free_with_destructor(m
->rules_foreign
, routing_policy_rule_free
);
1497 set_free_with_destructor(m
->rules_saved
, routing_policy_rule_free
);
1499 sd_netlink_unref(m
->rtnl
);
1500 sd_netlink_unref(m
->genl
);
1501 sd_resolve_unref(m
->resolve
);
1503 sd_event_source_unref(m
->speed_meter_event_source
);
1504 sd_event_unref(m
->event
);
1506 sd_device_monitor_unref(m
->device_monitor
);
1508 sd_bus_flush_close_unref(m
->bus
);
1510 free(m
->dynamic_timezone
);
1511 free(m
->dynamic_hostname
);
1516 int manager_start(Manager
*m
) {
1523 r
= manager_start_speed_meter(m
);
1525 return log_error_errno(r
, "Failed to initialize speed meter: %m");
1527 /* The dirty handler will deal with future serialization, but the first one
1528 must be done explicitly. */
1532 HASHMAP_FOREACH(link
, m
->links
, i
)
1538 int manager_load_config(Manager
*m
) {
1541 /* update timestamp */
1542 paths_check_timestamp(NETWORK_DIRS
, &m
->network_dirs_ts_usec
, true);
1548 r
= network_load(m
);
1555 bool manager_should_reload(Manager
*m
) {
1556 return paths_check_timestamp(NETWORK_DIRS
, &m
->network_dirs_ts_usec
, false);
1559 int manager_rtnl_enumerate_links(Manager
*m
) {
1560 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1561 sd_netlink_message
*link
;
1567 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1571 r
= sd_netlink_message_request_dump(req
, true);
1575 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1579 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1582 m
->enumerating
= true;
1584 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1588 m
->enumerating
= false;
1594 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1595 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1596 sd_netlink_message
*addr
;
1602 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1606 r
= sd_netlink_message_request_dump(req
, true);
1610 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1614 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1617 m
->enumerating
= true;
1619 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1623 m
->enumerating
= false;
1629 int manager_rtnl_enumerate_routes(Manager
*m
) {
1630 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1631 sd_netlink_message
*route
;
1637 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
1641 r
= sd_netlink_message_request_dump(req
, true);
1645 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1649 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
1652 m
->enumerating
= true;
1654 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
1658 m
->enumerating
= false;
1664 int manager_rtnl_enumerate_rules(Manager
*m
) {
1665 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1666 sd_netlink_message
*rule
;
1672 r
= sd_rtnl_message_new_routing_policy_rule(m
->rtnl
, &req
, RTM_GETRULE
, 0);
1676 r
= sd_netlink_message_request_dump(req
, true);
1680 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1682 if (r
== -EOPNOTSUPP
) {
1683 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
1690 for (rule
= reply
; rule
; rule
= sd_netlink_message_next(rule
)) {
1693 m
->enumerating
= true;
1695 k
= manager_rtnl_process_rule(m
->rtnl
, rule
, m
);
1699 m
->enumerating
= false;
1705 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
1710 assert(prefixlen
> 0);
1713 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
1714 if (p
->family
!= family
)
1717 r
= address_pool_acquire(p
, prefixlen
, found
);
1725 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
1726 _cleanup_free_
struct local_address
*gateways
= NULL
;
1731 /* Looks for a suitable "uplink", via black magic: an
1732 * interface that is up and where the default route with the
1733 * highest priority points to. */
1735 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
1737 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
1741 for (i
= 0; i
< n
; i
++) {
1744 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
1746 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1750 if (link
== exclude
)
1753 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
1762 void manager_dirty(Manager
*manager
) {
1765 /* the serialized state in /run is no longer up-to-date */
1766 manager
->dirty
= true;
1769 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1770 Manager
*manager
= userdata
;
1771 const sd_bus_error
*e
;
1776 e
= sd_bus_message_get_error(m
);
1778 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1783 int manager_set_hostname(Manager
*m
, const char *hostname
) {
1786 log_debug("Setting transient hostname: '%s'", strna(hostname
));
1788 if (free_and_strdup(&m
->dynamic_hostname
, hostname
) < 0)
1791 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
1792 log_debug("Not connected to system bus, setting hostname later.");
1796 r
= sd_bus_call_method_async(
1799 "org.freedesktop.hostname1",
1800 "/org/freedesktop/hostname1",
1801 "org.freedesktop.hostname1",
1803 set_hostname_handler
,
1810 return log_error_errno(r
, "Could not set transient hostname: %m");
1815 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1816 Manager
*manager
= userdata
;
1817 const sd_bus_error
*e
;
1822 e
= sd_bus_message_get_error(m
);
1824 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1829 int manager_set_timezone(Manager
*m
, const char *tz
) {
1835 log_debug("Setting system timezone: '%s'", tz
);
1836 if (free_and_strdup(&m
->dynamic_timezone
, tz
) < 0)
1839 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
1840 log_debug("Not connected to system bus, setting timezone later.");
1844 r
= sd_bus_call_method_async(
1847 "org.freedesktop.timedate1",
1848 "/org/freedesktop/timedate1",
1849 "org.freedesktop.timedate1",
1851 set_timezone_handler
,
1857 return log_error_errno(r
, "Could not set timezone: %m");
1862 int manager_request_product_uuid(Manager
*m
, Link
*link
) {
1867 if (m
->has_product_uuid
)
1870 log_debug("Requesting product UUID");
1875 assert_se(duid
= link_get_duid(link
));
1877 r
= set_ensure_allocated(&m
->links_requesting_uuid
, NULL
);
1881 r
= set_ensure_allocated(&m
->duids_requesting_uuid
, NULL
);
1885 r
= set_put(m
->links_requesting_uuid
, link
);
1889 r
= set_put(m
->duids_requesting_uuid
, duid
);
1894 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
1895 log_debug("Not connected to system bus, requesting product UUID later.");
1899 r
= sd_bus_call_method_async(
1902 "org.freedesktop.hostname1",
1903 "/org/freedesktop/hostname1",
1904 "org.freedesktop.hostname1",
1906 get_product_uuid_handler
,
1911 return log_warning_errno(r
, "Failed to get product UUID: %m");