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"
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/link", "org.freedesktop.network1.Link.Lease", lease_vtable
, lease_object_find
, m
);
181 return log_error_errno(r
, "Failed to add lease object vtable: %m");
183 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/link", lease_node_enumerator
, m
);
185 return log_error_errno(r
, "Failed to add lease enumerator: %m");
187 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable
, network_object_find
, m
);
189 return log_error_errno(r
, "Failed to add network object vtable: %m");
191 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/network", network_node_enumerator
, m
);
193 return log_error_errno(r
, "Failed to add network enumerator: %m");
195 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.network1", 0);
197 return log_error_errno(r
, "Failed to register name: %m");
199 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
201 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
206 static int manager_udev_process_link(Manager
*m
, struct udev_device
*device
) {
213 if (!streq_ptr(udev_device_get_action(device
), "add"))
216 ifindex
= udev_device_get_ifindex(device
);
218 log_debug("Ignoring udev ADD event for device with invalid ifindex");
222 r
= link_get(m
, ifindex
, &link
);
228 r
= link_initialized(link
, device
);
235 static int manager_dispatch_link_udev(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
236 Manager
*m
= userdata
;
237 struct udev_monitor
*monitor
= m
->udev_monitor
;
238 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
240 device
= udev_monitor_receive_device(monitor
);
244 manager_udev_process_link(m
, device
);
248 static int manager_connect_udev(Manager
*m
) {
251 /* udev does not initialize devices inside containers,
252 * so we rely on them being already initialized before
253 * entering the container */
254 if (detect_container() > 0)
257 m
->udev
= udev_new();
261 m
->udev_monitor
= udev_monitor_new_from_netlink(m
->udev
, "udev");
262 if (!m
->udev_monitor
)
265 r
= udev_monitor_filter_add_match_subsystem_devtype(m
->udev_monitor
, "net", NULL
);
267 return log_error_errno(r
, "Could not add udev monitor filter: %m");
269 r
= udev_monitor_enable_receiving(m
->udev_monitor
);
271 log_error("Could not enable udev monitor");
275 r
= sd_event_add_io(m
->event
,
276 &m
->udev_event_source
,
277 udev_monitor_get_fd(m
->udev_monitor
),
278 EPOLLIN
, manager_dispatch_link_udev
,
283 r
= sd_event_source_set_description(m
->udev_event_source
, "networkd-udev");
290 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
291 Manager
*m
= userdata
;
294 uint32_t ifindex
, priority
= 0;
295 unsigned char protocol
, scope
, tos
, table
;
297 unsigned char dst_prefixlen
, src_prefixlen
;
298 union in_addr_union dst
= {}, gw
= {}, src
= {}, prefsrc
= {};
306 if (sd_netlink_message_is_error(message
)) {
307 r
= sd_netlink_message_get_errno(message
);
309 log_warning_errno(r
, "rtnl: failed to receive route: %m");
314 r
= sd_netlink_message_get_type(message
, &type
);
316 log_warning_errno(r
, "rtnl: could not get message type: %m");
318 } else if (type
!= RTM_NEWROUTE
&& type
!= RTM_DELROUTE
) {
319 log_warning("rtnl: received unexpected message type when processing route");
323 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
325 log_debug("rtnl: received route without ifindex, ignoring");
328 log_warning_errno(r
, "rtnl: could not get ifindex from route, ignoring: %m");
330 } else if (ifindex
<= 0) {
331 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex
);
334 r
= link_get(m
, ifindex
, &link
);
335 if (r
< 0 || !link
) {
336 /* when enumerating we might be out of sync, but we will
337 * get the route again, so just ignore it */
339 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex
);
344 r
= sd_rtnl_message_route_get_family(message
, &family
);
345 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
346 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
350 r
= sd_rtnl_message_route_get_protocol(message
, &protocol
);
352 log_warning_errno(r
, "rtnl: could not get route protocol: %m");
358 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &dst
.in
);
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_in_addr(message
, RTA_GATEWAY
, &gw
.in
);
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_in_addr(message
, RTA_SRC
, &src
.in
);
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_in_addr(message
, RTA_PREFSRC
, &prefsrc
.in
);
377 if (r
< 0 && r
!= -ENODATA
) {
378 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
385 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &dst
.in6
);
386 if (r
< 0 && r
!= -ENODATA
) {
387 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
391 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &gw
.in6
);
392 if (r
< 0 && r
!= -ENODATA
) {
393 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
397 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &src
.in6
);
398 if (r
< 0 && r
!= -ENODATA
) {
399 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
403 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &prefsrc
.in6
);
404 if (r
< 0 && r
!= -ENODATA
) {
405 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
412 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
416 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &dst_prefixlen
);
418 log_link_warning_errno(link
, r
, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
422 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &src_prefixlen
);
424 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source prefixlen, ignoring: %m");
428 r
= sd_rtnl_message_route_get_scope(message
, &scope
);
430 log_link_warning_errno(link
, r
, "rtnl: received route with invalid scope, ignoring: %m");
434 r
= sd_rtnl_message_route_get_tos(message
, &tos
);
436 log_link_warning_errno(link
, r
, "rtnl: received route with invalid tos, ignoring: %m");
440 r
= sd_rtnl_message_route_get_table(message
, &table
);
442 log_link_warning_errno(link
, r
, "rtnl: received route with invalid table, ignoring: %m");
446 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &priority
);
447 if (r
< 0 && r
!= -ENODATA
) {
448 log_link_warning_errno(link
, r
, "rtnl: received route with invalid priority, ignoring: %m");
452 route_get(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
457 /* A route appeared that we did not request */
458 r
= route_add_foreign(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
463 route_update(route
, &src
, src_prefixlen
, &gw
, &prefsrc
, scope
, protocol
);
472 assert_not_reached("Received invalid RTNL message type");
478 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
479 Manager
*m
= userdata
;
484 unsigned char prefixlen
;
486 union in_addr_union in_addr
;
487 struct ifa_cacheinfo cinfo
;
488 Address
*address
= NULL
;
489 char buf
[INET6_ADDRSTRLEN
], valid_buf
[FORMAT_TIMESPAN_MAX
];
490 const char *valid_str
= NULL
;
497 if (sd_netlink_message_is_error(message
)) {
498 r
= sd_netlink_message_get_errno(message
);
500 log_warning_errno(r
, "rtnl: failed to receive address: %m");
505 r
= sd_netlink_message_get_type(message
, &type
);
507 log_warning_errno(r
, "rtnl: could not get message type: %m");
509 } else if (type
!= RTM_NEWADDR
&& type
!= RTM_DELADDR
) {
510 log_warning("rtnl: received unexpected message type when processing address");
514 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
516 log_warning_errno(r
, "rtnl: could not get ifindex from address: %m");
518 } else if (ifindex
<= 0) {
519 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex
);
522 r
= link_get(m
, ifindex
, &link
);
523 if (r
< 0 || !link
) {
524 /* when enumerating we might be out of sync, but we will
525 * get the address again, so just ignore it */
527 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex
);
532 r
= sd_rtnl_message_addr_get_family(message
, &family
);
533 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
534 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
538 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
540 log_link_warning_errno(link
, r
, "rtnl: received address with invalid prefixlen, ignoring: %m");
544 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
546 log_link_warning_errno(link
, r
, "rtnl: received address with invalid scope, ignoring: %m");
550 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
552 log_link_warning_errno(link
, r
, "rtnl: received address with invalid flags, ignoring: %m");
558 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
560 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
567 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
569 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
576 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
579 if (!inet_ntop(family
, &in_addr
, buf
, INET6_ADDRSTRLEN
)) {
580 log_link_warning(link
, "Could not print address");
584 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
586 if (cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
587 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
588 cinfo
.ifa_valid
* USEC_PER_SEC
,
592 address_get(link
, family
, &in_addr
, prefixlen
, &address
);
597 log_link_debug(link
, "Updating address: %s/%u (valid %s%s)", buf
, prefixlen
,
598 valid_str
? "for " : "forever", valid_str
?: "");
600 /* An address appeared that we did not request */
601 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
603 log_link_warning_errno(link
, r
, "Failed to add address %s/%u: %m", buf
, prefixlen
);
606 log_link_debug(link
, "Adding address: %s/%u (valid %s%s)", buf
, prefixlen
,
607 valid_str
? "for " : "forever", valid_str
?: "");
610 address_update(address
, flags
, scope
, &cinfo
);
617 log_link_debug(link
, "Removing address: %s/%u (valid %s%s)", buf
, prefixlen
,
618 valid_str
? "for " : "forever", valid_str
?: "");
619 address_drop(address
);
621 log_link_warning(link
, "Removing non-existent address: %s/%u (valid %s%s)", buf
, prefixlen
,
622 valid_str
? "for " : "forever", valid_str
?: "");
626 assert_not_reached("Received invalid RTNL message type");
632 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
633 Manager
*m
= userdata
;
635 NetDev
*netdev
= NULL
;
644 if (sd_netlink_message_is_error(message
)) {
645 r
= sd_netlink_message_get_errno(message
);
647 log_warning_errno(r
, "rtnl: Could not receive link: %m");
652 r
= sd_netlink_message_get_type(message
, &type
);
654 log_warning_errno(r
, "rtnl: Could not get message type: %m");
656 } else if (type
!= RTM_NEWLINK
&& type
!= RTM_DELLINK
) {
657 log_warning("rtnl: Received unexpected message type when processing link");
661 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
663 log_warning_errno(r
, "rtnl: Could not get ifindex from link: %m");
665 } else if (ifindex
<= 0) {
666 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex
);
670 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
672 log_warning_errno(r
, "rtnl: Received link message without ifname: %m");
676 (void) link_get(m
, ifindex
, &link
);
677 (void) netdev_get(m
, name
, &netdev
);
682 /* link is new, so add it */
683 r
= link_add(m
, message
, &link
);
685 log_warning_errno(r
, "Could not add new link: %m");
691 /* netdev exists, so make sure the ifindex matches */
692 r
= netdev_set_ifindex(netdev
, message
);
694 log_warning_errno(r
, "Could not set ifindex on netdev: %m");
699 r
= link_update(link
, message
);
712 assert_not_reached("Received invalid RTNL message type.");
718 static int systemd_netlink_fd(void) {
719 int n
, fd
, rtnl_fd
= -EINVAL
;
721 n
= sd_listen_fds(true);
725 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
726 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
737 static int manager_connect_rtnl(Manager
*m
) {
742 fd
= systemd_netlink_fd();
744 r
= sd_netlink_open(&m
->rtnl
);
746 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
750 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
754 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
758 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, &manager_rtnl_process_link
, m
);
762 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, &manager_rtnl_process_link
, m
);
766 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWADDR
, &manager_rtnl_process_address
, m
);
770 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELADDR
, &manager_rtnl_process_address
, m
);
774 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWROUTE
, &manager_rtnl_process_route
, m
);
778 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELROUTE
, &manager_rtnl_process_route
, m
);
785 static int ordered_set_put_in_addr(OrderedSet
*s
, const struct in_addr
*address
) {
791 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
795 r
= ordered_set_consume(s
, p
);
802 static int ordered_set_put_in_addrv(OrderedSet
*s
, const struct in_addr
*addresses
, int n
) {
806 assert(n
<= 0 || addresses
);
808 for (i
= 0; i
< n
; i
++) {
809 r
= ordered_set_put_in_addr(s
, addresses
+i
);
819 static void print_string_set(FILE *f
, const char *field
, OrderedSet
*s
) {
824 if (ordered_set_isempty(s
))
829 ORDERED_SET_FOREACH(p
, s
, i
)
830 fputs_with_space(f
, p
, NULL
, &space
);
835 static int manager_save(Manager
*m
) {
836 _cleanup_ordered_set_free_free_ OrderedSet
*dns
= NULL
, *ntp
= NULL
, *search_domains
= NULL
, *route_domains
= NULL
;
839 _cleanup_free_
char *temp_path
= NULL
;
840 _cleanup_fclose_
FILE *f
= NULL
;
841 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
842 const char *operstate_str
;
846 assert(m
->state_file
);
848 /* We add all NTP and DNS server to a set, to filter out duplicates */
849 dns
= ordered_set_new(&string_hash_ops
);
853 ntp
= ordered_set_new(&string_hash_ops
);
857 search_domains
= ordered_set_new(&dns_name_hash_ops
);
861 route_domains
= ordered_set_new(&dns_name_hash_ops
);
865 HASHMAP_FOREACH(link
, m
->links
, i
) {
866 if (link
->flags
& IFF_LOOPBACK
)
869 if (link
->operstate
> operstate
)
870 operstate
= link
->operstate
;
875 /* First add the static configured entries */
876 r
= ordered_set_put_strdupv(dns
, link
->network
->dns
);
880 r
= ordered_set_put_strdupv(ntp
, link
->network
->ntp
);
884 r
= ordered_set_put_strdupv(search_domains
, link
->network
->search_domains
);
888 r
= ordered_set_put_strdupv(route_domains
, link
->network
->route_domains
);
892 if (!link
->dhcp_lease
)
895 /* Secondly, add the entries acquired via DHCP */
896 if (link
->network
->dhcp_use_dns
) {
897 const struct in_addr
*addresses
;
899 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
901 r
= ordered_set_put_in_addrv(dns
, addresses
, r
);
904 } else if (r
< 0 && r
!= -ENODATA
)
908 if (link
->network
->dhcp_use_ntp
) {
909 const struct in_addr
*addresses
;
911 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
913 r
= ordered_set_put_in_addrv(ntp
, addresses
, r
);
916 } else if (r
< 0 && r
!= -ENODATA
)
920 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
921 const char *domainname
;
923 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
926 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
)
927 r
= ordered_set_put_strdup(search_domains
, domainname
);
929 r
= ordered_set_put_strdup(route_domains
, domainname
);
933 } else if (r
!= -ENODATA
)
938 operstate_str
= link_operstate_to_string(operstate
);
939 assert(operstate_str
);
941 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
945 fchmod(fileno(f
), 0644);
948 "# This is private data. Do not parse.\n"
949 "OPER_STATE=%s\n", operstate_str
);
951 print_string_set(f
, "DNS=", dns
);
952 print_string_set(f
, "NTP=", ntp
);
953 print_string_set(f
, "DOMAINS=", search_domains
);
954 print_string_set(f
, "ROUTE_DOMAINS=", route_domains
);
956 r
= fflush_and_check(f
);
960 if (rename(temp_path
, m
->state_file
) < 0) {
965 if (m
->operational_state
!= operstate
) {
966 m
->operational_state
= operstate
;
967 r
= manager_send_changed(m
, "OperationalState", NULL
);
969 log_error_errno(r
, "Could not emit changed OperationalState: %m");
977 (void) unlink(m
->state_file
);
978 (void) unlink(temp_path
);
980 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
983 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
984 Manager
*m
= userdata
;
994 SET_FOREACH(link
, m
->dirty_links
, i
) {
1003 int manager_new(Manager
**ret
) {
1004 _cleanup_manager_free_ Manager
*m
= NULL
;
1007 m
= new0(Manager
, 1);
1011 m
->state_file
= strdup("/run/systemd/netif/state");
1015 r
= sd_event_default(&m
->event
);
1019 sd_event_set_watchdog(m
->event
, true);
1021 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
1022 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
1024 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1028 r
= manager_connect_rtnl(m
);
1032 r
= manager_connect_udev(m
);
1036 m
->netdevs
= hashmap_new(&string_hash_ops
);
1040 LIST_HEAD_INIT(m
->networks
);
1042 r
= setup_default_address_pool(m
);
1046 m
->duid
.type
= DUID_TYPE_EN
;
1054 void manager_free(Manager
*m
) {
1063 free(m
->state_file
);
1065 while ((link
= hashmap_first(m
->links
)))
1067 hashmap_free(m
->links
);
1069 while ((network
= m
->networks
))
1070 network_free(network
);
1072 hashmap_free(m
->networks_by_name
);
1074 while ((netdev
= hashmap_first(m
->netdevs
)))
1075 netdev_unref(netdev
);
1076 hashmap_free(m
->netdevs
);
1078 while ((pool
= m
->address_pools
))
1079 address_pool_free(pool
);
1081 sd_netlink_unref(m
->rtnl
);
1082 sd_event_unref(m
->event
);
1084 sd_event_source_unref(m
->udev_event_source
);
1085 udev_monitor_unref(m
->udev_monitor
);
1086 udev_unref(m
->udev
);
1088 sd_bus_unref(m
->bus
);
1089 sd_bus_slot_unref(m
->prepare_for_sleep_slot
);
1090 sd_event_source_unref(m
->bus_retry_event_source
);
1095 static bool manager_check_idle(void *userdata
) {
1096 Manager
*m
= userdata
;
1102 /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback
1103 * device around, for which we have no configuration, and which already left the PENDING state. In all other
1104 * cases we are not idle. */
1106 HASHMAP_FOREACH(link
, m
->links
, i
) {
1107 /* We are not woken on udev activity, so let's just wait for the pending udev event */
1108 if (link
->state
== LINK_STATE_PENDING
)
1111 if ((link
->flags
& IFF_LOOPBACK
) == 0)
1121 int manager_run(Manager
*m
) {
1127 /* The dirty handler will deal with future serialization, but the first one
1128 must be done explicitly. */
1132 HASHMAP_FOREACH(link
, m
->links
, i
)
1136 return bus_event_loop_with_idle(
1139 "org.freedesktop.network1",
1144 /* failed to connect to the bus, so we lose exit-on-idle logic,
1145 this should not happen except if dbus is not around at all */
1146 return sd_event_loop(m
->event
);
1149 int manager_load_config(Manager
*m
) {
1152 /* update timestamp */
1153 paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, true);
1159 r
= network_load(m
);
1166 bool manager_should_reload(Manager
*m
) {
1167 return paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, false);
1170 int manager_rtnl_enumerate_links(Manager
*m
) {
1171 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1172 sd_netlink_message
*link
;
1178 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1182 r
= sd_netlink_message_request_dump(req
, true);
1186 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1190 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1193 m
->enumerating
= true;
1195 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1199 m
->enumerating
= false;
1205 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1206 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1207 sd_netlink_message
*addr
;
1213 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1217 r
= sd_netlink_message_request_dump(req
, true);
1221 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1225 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1228 m
->enumerating
= true;
1230 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1234 m
->enumerating
= false;
1240 int manager_rtnl_enumerate_routes(Manager
*m
) {
1241 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1242 sd_netlink_message
*route
;
1248 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
1252 r
= sd_netlink_message_request_dump(req
, true);
1256 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1260 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
1263 m
->enumerating
= true;
1265 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
1269 m
->enumerating
= false;
1275 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
1280 assert(prefixlen
> 0);
1283 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
1284 if (p
->family
!= family
)
1287 r
= address_pool_acquire(p
, prefixlen
, found
);
1295 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
1296 _cleanup_free_
struct local_address
*gateways
= NULL
;
1301 /* Looks for a suitable "uplink", via black magic: an
1302 * interface that is up and where the default route with the
1303 * highest priority points to. */
1305 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
1307 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
1311 for (i
= 0; i
< n
; i
++) {
1314 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
1316 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1320 if (link
== exclude
)
1323 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
1332 void manager_dirty(Manager
*manager
) {
1335 /* the serialized state in /run is no longer up-to-date */
1336 manager
->dirty
= true;