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 <sys/socket.h>
23 #include "sd-daemon.h"
24 #include "sd-netlink.h"
26 #include "alloc-util.h"
28 #include "conf-parser.h"
30 #include "dns-domain.h"
33 #include "libudev-private.h"
34 #include "local-addresses.h"
35 #include "netlink-util.h"
36 #include "networkd-manager.h"
37 #include "ordered-set.h"
38 #include "path-util.h"
40 #include "udev-util.h"
43 /* use 8 MB for receive socket kernel queue. */
44 #define RCVBUF_SIZE (8*1024*1024)
46 const char* const network_dirs
[] = {
47 "/etc/systemd/network",
48 "/run/systemd/network",
49 "/usr/lib/systemd/network",
51 "/lib/systemd/network",
55 static int setup_default_address_pool(Manager
*m
) {
61 /* Add in the well-known private address ranges. */
63 r
= address_pool_new_from_string(m
, &p
, AF_INET6
, "fc00::", 7);
67 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "192.168.0.0", 16);
71 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "172.16.0.0", 12);
75 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "10.0.0.0", 8);
82 static int on_bus_retry(sd_event_source
*s
, usec_t usec
, void *userdata
) {
83 Manager
*m
= userdata
;
88 m
->bus_retry_event_source
= sd_event_source_unref(m
->bus_retry_event_source
);
90 manager_connect_bus(m
);
95 static int manager_reset_all(Manager
*m
) {
102 HASHMAP_FOREACH(link
, m
->links
, i
) {
103 r
= link_carrier_reset(link
);
105 log_link_warning_errno(link
, r
, "Could not reset carrier: %m");
111 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
112 Manager
*m
= userdata
;
117 r
= sd_bus_message_read(message
, "b", &b
);
119 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
126 log_debug("Coming back from suspend, resetting all connections...");
128 manager_reset_all(m
);
133 int manager_connect_bus(Manager
*m
) {
138 r
= sd_bus_default_system(&m
->bus
);
140 /* We failed to connect? Yuck, we must be in early
141 * boot. Let's try in 5s again. As soon as we have
142 * kdbus we can stop doing this... */
144 log_debug_errno(r
, "Failed to connect to bus, trying again in 5s: %m");
146 r
= sd_event_add_time(m
->event
, &m
->bus_retry_event_source
, CLOCK_MONOTONIC
, now(CLOCK_MONOTONIC
) + 5*USEC_PER_SEC
, 0, on_bus_retry
, m
);
148 return log_error_errno(r
, "Failed to install bus reconnect time event: %m");
156 r
= sd_bus_add_match(m
->bus
, &m
->prepare_for_sleep_slot
,
158 "sender='org.freedesktop.login1',"
159 "interface='org.freedesktop.login1.Manager',"
160 "member='PrepareForSleep',"
161 "path='/org/freedesktop/login1'",
162 match_prepare_for_sleep
,
165 return log_error_errno(r
, "Failed to add match for PrepareForSleep: %m");
167 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable
, m
);
169 return log_error_errno(r
, "Failed to add manager object vtable: %m");
171 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable
, link_object_find
, m
);
173 return log_error_errno(r
, "Failed to add link object vtable: %m");
175 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/link", link_node_enumerator
, m
);
177 return log_error_errno(r
, "Failed to add link enumerator: %m");
179 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable
, network_object_find
, m
);
181 return log_error_errno(r
, "Failed to add network object vtable: %m");
183 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/network", network_node_enumerator
, m
);
185 return log_error_errno(r
, "Failed to add network enumerator: %m");
187 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.network1", 0);
189 return log_error_errno(r
, "Failed to register name: %m");
191 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
193 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
195 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
196 if (m
->dynamic_hostname
) {
197 r
= manager_set_hostname(m
, m
->dynamic_hostname
);
201 if (m
->dynamic_timezone
) {
202 r
= manager_set_timezone(m
, m
->dynamic_timezone
);
210 static int manager_udev_process_link(Manager
*m
, struct udev_device
*device
) {
217 if (!streq_ptr(udev_device_get_action(device
), "add"))
220 ifindex
= udev_device_get_ifindex(device
);
222 log_debug("Ignoring udev ADD event for device with invalid ifindex");
226 r
= link_get(m
, ifindex
, &link
);
232 r
= link_initialized(link
, device
);
239 static int manager_dispatch_link_udev(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
240 Manager
*m
= userdata
;
241 struct udev_monitor
*monitor
= m
->udev_monitor
;
242 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
244 device
= udev_monitor_receive_device(monitor
);
248 manager_udev_process_link(m
, device
);
252 static int manager_connect_udev(Manager
*m
) {
255 /* udev does not initialize devices inside containers,
256 * so we rely on them being already initialized before
257 * entering the container */
258 if (detect_container() > 0)
261 m
->udev
= udev_new();
265 m
->udev_monitor
= udev_monitor_new_from_netlink(m
->udev
, "udev");
266 if (!m
->udev_monitor
)
269 r
= udev_monitor_filter_add_match_subsystem_devtype(m
->udev_monitor
, "net", NULL
);
271 return log_error_errno(r
, "Could not add udev monitor filter: %m");
273 r
= udev_monitor_enable_receiving(m
->udev_monitor
);
275 log_error("Could not enable udev monitor");
279 r
= sd_event_add_io(m
->event
,
280 &m
->udev_event_source
,
281 udev_monitor_get_fd(m
->udev_monitor
),
282 EPOLLIN
, manager_dispatch_link_udev
,
287 r
= sd_event_source_set_description(m
->udev_event_source
, "networkd-udev");
294 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
295 Manager
*m
= userdata
;
298 uint32_t ifindex
, priority
= 0;
299 unsigned char protocol
, scope
, tos
, table
;
301 unsigned char dst_prefixlen
, src_prefixlen
;
302 union in_addr_union dst
= {}, gw
= {}, src
= {}, prefsrc
= {};
310 if (sd_netlink_message_is_error(message
)) {
311 r
= sd_netlink_message_get_errno(message
);
313 log_warning_errno(r
, "rtnl: failed to receive route: %m");
318 r
= sd_netlink_message_get_type(message
, &type
);
320 log_warning_errno(r
, "rtnl: could not get message type: %m");
322 } else if (type
!= RTM_NEWROUTE
&& type
!= RTM_DELROUTE
) {
323 log_warning("rtnl: received unexpected message type when processing route");
327 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
329 log_debug("rtnl: received route without ifindex, ignoring");
332 log_warning_errno(r
, "rtnl: could not get ifindex from route, ignoring: %m");
334 } else if (ifindex
<= 0) {
335 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex
);
338 r
= link_get(m
, ifindex
, &link
);
339 if (r
< 0 || !link
) {
340 /* when enumerating we might be out of sync, but we will
341 * get the route again, so just ignore it */
343 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex
);
348 r
= sd_rtnl_message_route_get_family(message
, &family
);
349 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
350 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
354 r
= sd_rtnl_message_route_get_protocol(message
, &protocol
);
356 log_warning_errno(r
, "rtnl: could not get route protocol: %m");
362 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &dst
.in
);
363 if (r
< 0 && r
!= -ENODATA
) {
364 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
368 r
= sd_netlink_message_read_in_addr(message
, RTA_GATEWAY
, &gw
.in
);
369 if (r
< 0 && r
!= -ENODATA
) {
370 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
374 r
= sd_netlink_message_read_in_addr(message
, RTA_SRC
, &src
.in
);
375 if (r
< 0 && r
!= -ENODATA
) {
376 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
380 r
= sd_netlink_message_read_in_addr(message
, RTA_PREFSRC
, &prefsrc
.in
);
381 if (r
< 0 && r
!= -ENODATA
) {
382 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
389 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &dst
.in6
);
390 if (r
< 0 && r
!= -ENODATA
) {
391 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
395 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &gw
.in6
);
396 if (r
< 0 && r
!= -ENODATA
) {
397 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
401 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &src
.in6
);
402 if (r
< 0 && r
!= -ENODATA
) {
403 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
407 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &prefsrc
.in6
);
408 if (r
< 0 && r
!= -ENODATA
) {
409 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
416 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
420 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &dst_prefixlen
);
422 log_link_warning_errno(link
, r
, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
426 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &src_prefixlen
);
428 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source prefixlen, ignoring: %m");
432 r
= sd_rtnl_message_route_get_scope(message
, &scope
);
434 log_link_warning_errno(link
, r
, "rtnl: received route with invalid scope, ignoring: %m");
438 r
= sd_rtnl_message_route_get_tos(message
, &tos
);
440 log_link_warning_errno(link
, r
, "rtnl: received route with invalid tos, ignoring: %m");
444 r
= sd_rtnl_message_route_get_table(message
, &table
);
446 log_link_warning_errno(link
, r
, "rtnl: received route with invalid table, ignoring: %m");
450 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &priority
);
451 if (r
< 0 && r
!= -ENODATA
) {
452 log_link_warning_errno(link
, r
, "rtnl: received route with invalid priority, ignoring: %m");
456 route_get(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
461 /* A route appeared that we did not request */
462 r
= route_add_foreign(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
467 route_update(route
, &src
, src_prefixlen
, &gw
, &prefsrc
, scope
, protocol
);
476 assert_not_reached("Received invalid RTNL message type");
482 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
483 Manager
*m
= userdata
;
488 unsigned char prefixlen
;
490 union in_addr_union in_addr
;
491 struct ifa_cacheinfo cinfo
;
492 Address
*address
= NULL
;
493 char buf
[INET6_ADDRSTRLEN
], valid_buf
[FORMAT_TIMESPAN_MAX
];
494 const char *valid_str
= NULL
;
501 if (sd_netlink_message_is_error(message
)) {
502 r
= sd_netlink_message_get_errno(message
);
504 log_warning_errno(r
, "rtnl: failed to receive address: %m");
509 r
= sd_netlink_message_get_type(message
, &type
);
511 log_warning_errno(r
, "rtnl: could not get message type: %m");
513 } else if (type
!= RTM_NEWADDR
&& type
!= RTM_DELADDR
) {
514 log_warning("rtnl: received unexpected message type when processing address");
518 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
520 log_warning_errno(r
, "rtnl: could not get ifindex from address: %m");
522 } else if (ifindex
<= 0) {
523 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex
);
526 r
= link_get(m
, ifindex
, &link
);
527 if (r
< 0 || !link
) {
528 /* when enumerating we might be out of sync, but we will
529 * get the address again, so just ignore it */
531 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex
);
536 r
= sd_rtnl_message_addr_get_family(message
, &family
);
537 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
538 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
542 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
544 log_link_warning_errno(link
, r
, "rtnl: received address with invalid prefixlen, ignoring: %m");
548 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
550 log_link_warning_errno(link
, r
, "rtnl: received address with invalid scope, ignoring: %m");
554 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
556 log_link_warning_errno(link
, r
, "rtnl: received address with invalid flags, ignoring: %m");
562 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
564 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
571 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
573 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
580 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
583 if (!inet_ntop(family
, &in_addr
, buf
, INET6_ADDRSTRLEN
)) {
584 log_link_warning(link
, "Could not print address");
588 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
590 if (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
,
596 address_get(link
, family
, &in_addr
, prefixlen
, &address
);
601 log_link_debug(link
, "Updating address: %s/%u (valid %s%s)", buf
, prefixlen
,
602 valid_str
? "for " : "forever", valid_str
?: "");
604 /* An address appeared that we did not request */
605 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
607 log_link_warning_errno(link
, r
, "Failed to add address %s/%u: %m", buf
, prefixlen
);
610 log_link_debug(link
, "Adding address: %s/%u (valid %s%s)", buf
, prefixlen
,
611 valid_str
? "for " : "forever", valid_str
?: "");
614 address_update(address
, flags
, scope
, &cinfo
);
621 log_link_debug(link
, "Removing address: %s/%u (valid %s%s)", buf
, prefixlen
,
622 valid_str
? "for " : "forever", valid_str
?: "");
623 address_drop(address
);
625 log_link_warning(link
, "Removing non-existent address: %s/%u (valid %s%s)", buf
, prefixlen
,
626 valid_str
? "for " : "forever", valid_str
?: "");
630 assert_not_reached("Received invalid RTNL message type");
636 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
637 Manager
*m
= userdata
;
639 NetDev
*netdev
= NULL
;
648 if (sd_netlink_message_is_error(message
)) {
649 r
= sd_netlink_message_get_errno(message
);
651 log_warning_errno(r
, "rtnl: Could not receive link: %m");
656 r
= sd_netlink_message_get_type(message
, &type
);
658 log_warning_errno(r
, "rtnl: Could not get message type: %m");
660 } else if (type
!= RTM_NEWLINK
&& type
!= RTM_DELLINK
) {
661 log_warning("rtnl: Received unexpected message type when processing link");
665 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
667 log_warning_errno(r
, "rtnl: Could not get ifindex from link: %m");
669 } else if (ifindex
<= 0) {
670 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex
);
674 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
676 log_warning_errno(r
, "rtnl: Received link message without ifname: %m");
680 (void) link_get(m
, ifindex
, &link
);
681 (void) netdev_get(m
, name
, &netdev
);
686 /* link is new, so add it */
687 r
= link_add(m
, message
, &link
);
689 log_warning_errno(r
, "Could not add new link: %m");
695 /* netdev exists, so make sure the ifindex matches */
696 r
= netdev_set_ifindex(netdev
, message
);
698 log_warning_errno(r
, "Could not set ifindex on netdev: %m");
703 r
= link_update(link
, message
);
716 assert_not_reached("Received invalid RTNL message type.");
722 static int systemd_netlink_fd(void) {
723 int n
, fd
, rtnl_fd
= -EINVAL
;
725 n
= sd_listen_fds(true);
729 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
730 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
741 static int manager_connect_rtnl(Manager
*m
) {
746 fd
= systemd_netlink_fd();
748 r
= sd_netlink_open(&m
->rtnl
);
750 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
754 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
758 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
762 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, &manager_rtnl_process_link
, m
);
766 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, &manager_rtnl_process_link
, m
);
770 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWADDR
, &manager_rtnl_process_address
, m
);
774 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELADDR
, &manager_rtnl_process_address
, m
);
778 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWROUTE
, &manager_rtnl_process_route
, m
);
782 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELROUTE
, &manager_rtnl_process_route
, m
);
789 static int ordered_set_put_in_addr_data(OrderedSet
*s
, const struct in_addr_data
*address
) {
796 r
= in_addr_to_string(address
->family
, &address
->address
, &p
);
800 r
= ordered_set_consume(s
, p
);
807 static int ordered_set_put_in_addr_datav(OrderedSet
*s
, const struct in_addr_data
*addresses
, unsigned n
) {
812 assert(addresses
|| n
== 0);
814 for (i
= 0; i
< n
; i
++) {
815 r
= ordered_set_put_in_addr_data(s
, addresses
+i
);
825 static int ordered_set_put_in4_addr(OrderedSet
*s
, const struct in_addr
*address
) {
832 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
836 r
= ordered_set_consume(s
, p
);
843 static int ordered_set_put_in4_addrv(OrderedSet
*s
, const struct in_addr
*addresses
, unsigned n
) {
848 assert(n
== 0 || addresses
);
850 for (i
= 0; i
< n
; i
++) {
851 r
= ordered_set_put_in4_addr(s
, addresses
+i
);
861 static void print_string_set(FILE *f
, const char *field
, OrderedSet
*s
) {
866 if (ordered_set_isempty(s
))
871 ORDERED_SET_FOREACH(p
, s
, i
)
872 fputs_with_space(f
, p
, NULL
, &space
);
877 static int manager_save(Manager
*m
) {
878 _cleanup_ordered_set_free_free_ OrderedSet
*dns
= NULL
, *ntp
= NULL
, *search_domains
= NULL
, *route_domains
= NULL
;
881 _cleanup_free_
char *temp_path
= NULL
;
882 _cleanup_fclose_
FILE *f
= NULL
;
883 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
884 const char *operstate_str
;
888 assert(m
->state_file
);
890 /* We add all NTP and DNS server to a set, to filter out duplicates */
891 dns
= ordered_set_new(&string_hash_ops
);
895 ntp
= ordered_set_new(&string_hash_ops
);
899 search_domains
= ordered_set_new(&dns_name_hash_ops
);
903 route_domains
= ordered_set_new(&dns_name_hash_ops
);
907 HASHMAP_FOREACH(link
, m
->links
, i
) {
908 if (link
->flags
& IFF_LOOPBACK
)
911 if (link
->operstate
> operstate
)
912 operstate
= link
->operstate
;
917 /* First add the static configured entries */
918 r
= ordered_set_put_in_addr_datav(dns
, link
->network
->dns
, link
->network
->n_dns
);
922 r
= ordered_set_put_strdupv(ntp
, link
->network
->ntp
);
926 r
= ordered_set_put_strdupv(search_domains
, link
->network
->search_domains
);
930 r
= ordered_set_put_strdupv(route_domains
, link
->network
->route_domains
);
934 if (!link
->dhcp_lease
)
937 /* Secondly, add the entries acquired via DHCP */
938 if (link
->network
->dhcp_use_dns
) {
939 const struct in_addr
*addresses
;
941 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
943 r
= ordered_set_put_in4_addrv(dns
, addresses
, r
);
946 } else if (r
< 0 && r
!= -ENODATA
)
950 if (link
->network
->dhcp_use_ntp
) {
951 const struct in_addr
*addresses
;
953 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
955 r
= ordered_set_put_in4_addrv(ntp
, addresses
, r
);
958 } else if (r
< 0 && r
!= -ENODATA
)
962 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
963 const char *domainname
;
965 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
968 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
)
969 r
= ordered_set_put_strdup(search_domains
, domainname
);
971 r
= ordered_set_put_strdup(route_domains
, domainname
);
975 } else if (r
!= -ENODATA
)
980 operstate_str
= link_operstate_to_string(operstate
);
981 assert(operstate_str
);
983 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
987 (void) fchmod(fileno(f
), 0644);
990 "# This is private data. Do not parse.\n"
991 "OPER_STATE=%s\n", operstate_str
);
993 print_string_set(f
, "DNS=", dns
);
994 print_string_set(f
, "NTP=", ntp
);
995 print_string_set(f
, "DOMAINS=", search_domains
);
996 print_string_set(f
, "ROUTE_DOMAINS=", route_domains
);
998 r
= fflush_and_check(f
);
1002 if (rename(temp_path
, m
->state_file
) < 0) {
1007 if (m
->operational_state
!= operstate
) {
1008 m
->operational_state
= operstate
;
1009 r
= manager_send_changed(m
, "OperationalState", NULL
);
1011 log_error_errno(r
, "Could not emit changed OperationalState: %m");
1019 (void) unlink(m
->state_file
);
1020 (void) unlink(temp_path
);
1022 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
1025 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
1026 Manager
*m
= userdata
;
1036 SET_FOREACH(link
, m
->dirty_links
, i
) {
1037 r
= link_save(link
);
1045 int manager_new(Manager
**ret
) {
1046 _cleanup_manager_free_ Manager
*m
= NULL
;
1049 m
= new0(Manager
, 1);
1053 m
->state_file
= strdup("/run/systemd/netif/state");
1057 r
= sd_event_default(&m
->event
);
1061 sd_event_set_watchdog(m
->event
, true);
1063 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
1064 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
1066 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1070 r
= manager_connect_rtnl(m
);
1074 r
= manager_connect_udev(m
);
1078 m
->netdevs
= hashmap_new(&string_hash_ops
);
1082 LIST_HEAD_INIT(m
->networks
);
1084 r
= setup_default_address_pool(m
);
1088 m
->duid
.type
= DUID_TYPE_EN
;
1096 void manager_free(Manager
*m
) {
1105 free(m
->state_file
);
1107 while ((link
= hashmap_first(m
->links
)))
1109 hashmap_free(m
->links
);
1111 while ((network
= m
->networks
))
1112 network_free(network
);
1114 hashmap_free(m
->networks_by_name
);
1116 while ((netdev
= hashmap_first(m
->netdevs
)))
1117 netdev_unref(netdev
);
1118 hashmap_free(m
->netdevs
);
1120 while ((pool
= m
->address_pools
))
1121 address_pool_free(pool
);
1123 sd_netlink_unref(m
->rtnl
);
1124 sd_event_unref(m
->event
);
1126 sd_event_source_unref(m
->udev_event_source
);
1127 udev_monitor_unref(m
->udev_monitor
);
1128 udev_unref(m
->udev
);
1130 sd_bus_unref(m
->bus
);
1131 sd_bus_slot_unref(m
->prepare_for_sleep_slot
);
1132 sd_event_source_unref(m
->bus_retry_event_source
);
1134 free(m
->dynamic_timezone
);
1135 free(m
->dynamic_hostname
);
1140 static bool manager_check_idle(void *userdata
) {
1141 Manager
*m
= userdata
;
1147 /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback
1148 * device around, for which we have no configuration, and which already left the PENDING state. In all other
1149 * cases we are not idle. */
1151 HASHMAP_FOREACH(link
, m
->links
, i
) {
1152 /* We are not woken on udev activity, so let's just wait for the pending udev event */
1153 if (link
->state
== LINK_STATE_PENDING
)
1156 if ((link
->flags
& IFF_LOOPBACK
) == 0)
1166 int manager_run(Manager
*m
) {
1172 /* The dirty handler will deal with future serialization, but the first one
1173 must be done explicitly. */
1177 HASHMAP_FOREACH(link
, m
->links
, i
)
1181 return bus_event_loop_with_idle(
1184 "org.freedesktop.network1",
1189 /* failed to connect to the bus, so we lose exit-on-idle logic,
1190 this should not happen except if dbus is not around at all */
1191 return sd_event_loop(m
->event
);
1194 int manager_load_config(Manager
*m
) {
1197 /* update timestamp */
1198 paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, true);
1204 r
= network_load(m
);
1211 bool manager_should_reload(Manager
*m
) {
1212 return paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, false);
1215 int manager_rtnl_enumerate_links(Manager
*m
) {
1216 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1217 sd_netlink_message
*link
;
1223 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1227 r
= sd_netlink_message_request_dump(req
, true);
1231 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1235 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1238 m
->enumerating
= true;
1240 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1244 m
->enumerating
= false;
1250 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1251 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1252 sd_netlink_message
*addr
;
1258 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1262 r
= sd_netlink_message_request_dump(req
, true);
1266 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1270 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1273 m
->enumerating
= true;
1275 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1279 m
->enumerating
= false;
1285 int manager_rtnl_enumerate_routes(Manager
*m
) {
1286 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1287 sd_netlink_message
*route
;
1293 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
1297 r
= sd_netlink_message_request_dump(req
, true);
1301 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1305 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
1308 m
->enumerating
= true;
1310 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
1314 m
->enumerating
= false;
1320 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
1325 assert(prefixlen
> 0);
1328 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
1329 if (p
->family
!= family
)
1332 r
= address_pool_acquire(p
, prefixlen
, found
);
1340 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
1341 _cleanup_free_
struct local_address
*gateways
= NULL
;
1346 /* Looks for a suitable "uplink", via black magic: an
1347 * interface that is up and where the default route with the
1348 * highest priority points to. */
1350 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
1352 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
1356 for (i
= 0; i
< n
; i
++) {
1359 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
1361 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1365 if (link
== exclude
)
1368 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
1377 void manager_dirty(Manager
*manager
) {
1380 /* the serialized state in /run is no longer up-to-date */
1381 manager
->dirty
= true;
1384 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1385 Manager
*manager
= userdata
;
1386 const sd_bus_error
*e
;
1391 e
= sd_bus_message_get_error(m
);
1393 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1398 int manager_set_hostname(Manager
*m
, const char *hostname
) {
1401 log_debug("Setting transient hostname: '%s'", strna(hostname
));
1402 if (free_and_strdup(&m
->dynamic_hostname
, hostname
) < 0)
1406 /* TODO: replace by assert when we can rely on kdbus */
1407 log_info("Not connected to system bus, ignoring transient hostname.");
1411 r
= sd_bus_call_method_async(
1414 "org.freedesktop.hostname1",
1415 "/org/freedesktop/hostname1",
1416 "org.freedesktop.hostname1",
1418 set_hostname_handler
,
1425 return log_error_errno(r
, "Could not set transient hostname: %m");
1430 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1431 Manager
*manager
= userdata
;
1432 const sd_bus_error
*e
;
1437 e
= sd_bus_message_get_error(m
);
1439 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1444 int manager_set_timezone(Manager
*m
, const char *tz
) {
1450 log_debug("Setting system timezone: '%s'", tz
);
1451 if (free_and_strdup(&m
->dynamic_timezone
, tz
) < 0)
1455 log_info("Not connected to system bus, ignoring timezone.");
1459 r
= sd_bus_call_method_async(
1462 "org.freedesktop.timedate1",
1463 "/org/freedesktop/timedate1",
1464 "org.freedesktop.timedate1",
1466 set_timezone_handler
,
1472 return log_error_errno(r
, "Could not set timezone: %m");