1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
25 #include "sd-daemon.h"
26 #include "sd-netlink.h"
28 #include "alloc-util.h"
30 #include "conf-parser.h"
32 #include "dns-domain.h"
35 #include "libudev-private.h"
36 #include "local-addresses.h"
37 #include "netlink-util.h"
39 #include "ordered-set.h"
40 #include "path-util.h"
42 #include "udev-util.h"
45 /* use 8 MB for receive socket kernel queue. */
46 #define RCVBUF_SIZE (8*1024*1024)
48 const char* const network_dirs
[] = {
49 "/etc/systemd/network",
50 "/run/systemd/network",
51 "/usr/lib/systemd/network",
53 "/lib/systemd/network",
57 static int setup_default_address_pool(Manager
*m
) {
63 /* Add in the well-known private address ranges. */
65 r
= address_pool_new_from_string(m
, &p
, AF_INET6
, "fc00::", 7);
69 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "192.168.0.0", 16);
73 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "172.16.0.0", 12);
77 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "10.0.0.0", 8);
84 static int on_bus_retry(sd_event_source
*s
, usec_t usec
, void *userdata
) {
85 Manager
*m
= userdata
;
90 m
->bus_retry_event_source
= sd_event_source_unref(m
->bus_retry_event_source
);
92 manager_connect_bus(m
);
97 static int manager_reset_all(Manager
*m
) {
104 HASHMAP_FOREACH(link
, m
->links
, i
) {
105 r
= link_carrier_reset(link
);
107 log_link_warning_errno(link
, r
, "Could not reset carrier: %m");
113 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
114 Manager
*m
= userdata
;
119 r
= sd_bus_message_read(message
, "b", &b
);
121 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
128 log_debug("Coming back from suspend, resetting all connections...");
130 manager_reset_all(m
);
135 int manager_connect_bus(Manager
*m
) {
140 r
= sd_bus_default_system(&m
->bus
);
142 /* We failed to connect? Yuck, we must be in early
143 * boot. Let's try in 5s again. As soon as we have
144 * kdbus we can stop doing this... */
146 log_debug_errno(r
, "Failed to connect to bus, trying again in 5s: %m");
148 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
);
150 return log_error_errno(r
, "Failed to install bus reconnect time event: %m");
158 r
= sd_bus_add_match(m
->bus
, &m
->prepare_for_sleep_slot
,
160 "sender='org.freedesktop.login1',"
161 "interface='org.freedesktop.login1.Manager',"
162 "member='PrepareForSleep',"
163 "path='/org/freedesktop/login1'",
164 match_prepare_for_sleep
,
167 return log_error_errno(r
, "Failed to add match for PrepareForSleep: %m");
169 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable
, m
);
171 return log_error_errno(r
, "Failed to add manager object vtable: %m");
173 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable
, link_object_find
, m
);
175 return log_error_errno(r
, "Failed to add link object vtable: %m");
177 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/link", link_node_enumerator
, m
);
179 return log_error_errno(r
, "Failed to add link enumerator: %m");
181 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable
, network_object_find
, m
);
183 return log_error_errno(r
, "Failed to add network object vtable: %m");
185 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/network", network_node_enumerator
, m
);
187 return log_error_errno(r
, "Failed to add network enumerator: %m");
189 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.network1", 0);
191 return log_error_errno(r
, "Failed to register name: %m");
193 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
195 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
200 static int manager_udev_process_link(Manager
*m
, struct udev_device
*device
) {
207 if (!streq_ptr(udev_device_get_action(device
), "add"))
210 ifindex
= udev_device_get_ifindex(device
);
212 log_debug("Ignoring udev ADD event for device with invalid ifindex");
216 r
= link_get(m
, ifindex
, &link
);
222 r
= link_initialized(link
, device
);
229 static int manager_dispatch_link_udev(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
230 Manager
*m
= userdata
;
231 struct udev_monitor
*monitor
= m
->udev_monitor
;
232 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
234 device
= udev_monitor_receive_device(monitor
);
238 manager_udev_process_link(m
, device
);
242 static int manager_connect_udev(Manager
*m
) {
245 /* udev does not initialize devices inside containers,
246 * so we rely on them being already initialized before
247 * entering the container */
248 if (detect_container() > 0)
251 m
->udev
= udev_new();
255 m
->udev_monitor
= udev_monitor_new_from_netlink(m
->udev
, "udev");
256 if (!m
->udev_monitor
)
259 r
= udev_monitor_filter_add_match_subsystem_devtype(m
->udev_monitor
, "net", NULL
);
261 return log_error_errno(r
, "Could not add udev monitor filter: %m");
263 r
= udev_monitor_enable_receiving(m
->udev_monitor
);
265 log_error("Could not enable udev monitor");
269 r
= sd_event_add_io(m
->event
,
270 &m
->udev_event_source
,
271 udev_monitor_get_fd(m
->udev_monitor
),
272 EPOLLIN
, manager_dispatch_link_udev
,
277 r
= sd_event_source_set_description(m
->udev_event_source
, "networkd-udev");
284 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
285 Manager
*m
= userdata
;
288 uint32_t ifindex
, priority
= 0;
289 unsigned char protocol
, scope
, tos
, table
;
291 unsigned char dst_prefixlen
, src_prefixlen
;
292 union in_addr_union dst
= {}, gw
= {}, src
= {}, prefsrc
= {};
300 if (sd_netlink_message_is_error(message
)) {
301 r
= sd_netlink_message_get_errno(message
);
303 log_warning_errno(r
, "rtnl: failed to receive route: %m");
308 r
= sd_netlink_message_get_type(message
, &type
);
310 log_warning_errno(r
, "rtnl: could not get message type: %m");
312 } else if (type
!= RTM_NEWROUTE
&& type
!= RTM_DELROUTE
) {
313 log_warning("rtnl: received unexpected message type when processing route");
317 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
319 log_debug("rtnl: received route without ifindex, ignoring");
322 log_warning_errno(r
, "rtnl: could not get ifindex from route, ignoring: %m");
324 } else if (ifindex
<= 0) {
325 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex
);
328 r
= link_get(m
, ifindex
, &link
);
329 if (r
< 0 || !link
) {
330 /* when enumerating we might be out of sync, but we will
331 * get the route again, so just ignore it */
333 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex
);
338 r
= sd_rtnl_message_route_get_family(message
, &family
);
339 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
340 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
344 r
= sd_rtnl_message_route_get_protocol(message
, &protocol
);
346 log_warning_errno(r
, "rtnl: could not get route protocol: %m");
352 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &dst
.in
);
353 if (r
< 0 && r
!= -ENODATA
) {
354 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
358 r
= sd_netlink_message_read_in_addr(message
, RTA_GATEWAY
, &gw
.in
);
359 if (r
< 0 && r
!= -ENODATA
) {
360 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
364 r
= sd_netlink_message_read_in_addr(message
, RTA_SRC
, &src
.in
);
365 if (r
< 0 && r
!= -ENODATA
) {
366 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
370 r
= sd_netlink_message_read_in_addr(message
, RTA_PREFSRC
, &prefsrc
.in
);
371 if (r
< 0 && r
!= -ENODATA
) {
372 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
379 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &dst
.in6
);
380 if (r
< 0 && r
!= -ENODATA
) {
381 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
385 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &gw
.in6
);
386 if (r
< 0 && r
!= -ENODATA
) {
387 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
391 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &src
.in6
);
392 if (r
< 0 && r
!= -ENODATA
) {
393 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
397 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &prefsrc
.in6
);
398 if (r
< 0 && r
!= -ENODATA
) {
399 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
406 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
410 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &dst_prefixlen
);
412 log_link_warning_errno(link
, r
, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
416 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &src_prefixlen
);
418 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source prefixlen, ignoring: %m");
422 r
= sd_rtnl_message_route_get_scope(message
, &scope
);
424 log_link_warning_errno(link
, r
, "rtnl: received route with invalid scope, ignoring: %m");
428 r
= sd_rtnl_message_route_get_tos(message
, &tos
);
430 log_link_warning_errno(link
, r
, "rtnl: received route with invalid tos, ignoring: %m");
434 r
= sd_rtnl_message_route_get_table(message
, &table
);
436 log_link_warning_errno(link
, r
, "rtnl: received route with invalid table, ignoring: %m");
440 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &priority
);
441 if (r
< 0 && r
!= -ENODATA
) {
442 log_link_warning_errno(link
, r
, "rtnl: received route with invalid priority, ignoring: %m");
446 route_get(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
451 /* A route appeared that we did not request */
452 r
= route_add_foreign(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
457 route_update(route
, &src
, src_prefixlen
, &gw
, &prefsrc
, scope
, protocol
);
468 assert_not_reached("Received invalid RTNL message type");
474 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
475 Manager
*m
= userdata
;
480 unsigned char prefixlen
;
482 union in_addr_union in_addr
;
483 struct ifa_cacheinfo cinfo
;
484 Address
*address
= NULL
;
485 char buf
[INET6_ADDRSTRLEN
], valid_buf
[FORMAT_TIMESPAN_MAX
];
486 const char *valid_str
= NULL
;
493 if (sd_netlink_message_is_error(message
)) {
494 r
= sd_netlink_message_get_errno(message
);
496 log_warning_errno(r
, "rtnl: failed to receive address: %m");
501 r
= sd_netlink_message_get_type(message
, &type
);
503 log_warning_errno(r
, "rtnl: could not get message type: %m");
505 } else if (type
!= RTM_NEWADDR
&& type
!= RTM_DELADDR
) {
506 log_warning("rtnl: received unexpected message type when processing address");
510 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
512 log_warning_errno(r
, "rtnl: could not get ifindex from address: %m");
514 } else if (ifindex
<= 0) {
515 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex
);
518 r
= link_get(m
, ifindex
, &link
);
519 if (r
< 0 || !link
) {
520 /* when enumerating we might be out of sync, but we will
521 * get the address again, so just ignore it */
523 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex
);
528 r
= sd_rtnl_message_addr_get_family(message
, &family
);
529 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
530 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
534 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
536 log_link_warning_errno(link
, r
, "rtnl: received address with invalid prefixlen, ignoring: %m");
540 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
542 log_link_warning_errno(link
, r
, "rtnl: received address with invalid scope, ignoring: %m");
546 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
548 log_link_warning_errno(link
, r
, "rtnl: received address with invalid flags, ignoring: %m");
554 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
556 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
563 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
565 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
572 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
575 if (!inet_ntop(family
, &in_addr
, buf
, INET6_ADDRSTRLEN
)) {
576 log_link_warning(link
, "Could not print address");
580 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
582 if (cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
583 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
584 cinfo
.ifa_valid
* USEC_PER_SEC
,
588 address_get(link
, family
, &in_addr
, prefixlen
, &address
);
593 log_link_debug(link
, "Updating address: %s/%u (valid %s%s)", buf
, prefixlen
,
594 valid_str
? "for " : "forever", valid_str
?: "");
596 /* An address appeared that we did not request */
597 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
599 log_link_warning_errno(link
, r
, "Failed to add address %s/%u: %m", buf
, prefixlen
);
602 log_link_debug(link
, "Adding address: %s/%u (valid %s%s)", buf
, prefixlen
,
603 valid_str
? "for " : "forever", valid_str
?: "");
606 address_update(address
, flags
, scope
, &cinfo
);
613 log_link_debug(link
, "Removing address: %s/%u (valid %s%s)", buf
, prefixlen
,
614 valid_str
? "for " : "forever", valid_str
?: "");
615 address_drop(address
);
617 log_link_warning(link
, "Removing non-existent address: %s/%u (valid %s%s)", buf
, prefixlen
,
618 valid_str
? "for " : "forever", valid_str
?: "");
622 assert_not_reached("Received invalid RTNL message type");
628 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
629 Manager
*m
= userdata
;
631 NetDev
*netdev
= NULL
;
640 if (sd_netlink_message_is_error(message
)) {
641 r
= sd_netlink_message_get_errno(message
);
643 log_warning_errno(r
, "rtnl: Could not receive link: %m");
648 r
= sd_netlink_message_get_type(message
, &type
);
650 log_warning_errno(r
, "rtnl: Could not get message type: %m");
652 } else if (type
!= RTM_NEWLINK
&& type
!= RTM_DELLINK
) {
653 log_warning("rtnl: Received unexpected message type when processing link");
657 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
659 log_warning_errno(r
, "rtnl: Could not get ifindex from link: %m");
661 } else if (ifindex
<= 0) {
662 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex
);
666 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
668 log_warning_errno(r
, "rtnl: Received link message without ifname: %m");
672 (void) link_get(m
, ifindex
, &link
);
673 (void) netdev_get(m
, name
, &netdev
);
678 /* link is new, so add it */
679 r
= link_add(m
, message
, &link
);
681 log_warning_errno(r
, "Could not add new link: %m");
687 /* netdev exists, so make sure the ifindex matches */
688 r
= netdev_set_ifindex(netdev
, message
);
690 log_warning_errno(r
, "Could not set ifindex on netdev: %m");
695 r
= link_update(link
, message
);
708 assert_not_reached("Received invalid RTNL message type.");
714 static int systemd_netlink_fd(void) {
715 int n
, fd
, rtnl_fd
= -EINVAL
;
717 n
= sd_listen_fds(true);
721 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
722 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
733 static int manager_connect_rtnl(Manager
*m
) {
738 fd
= systemd_netlink_fd();
740 r
= sd_netlink_open(&m
->rtnl
);
742 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
746 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
750 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
754 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, &manager_rtnl_process_link
, m
);
758 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, &manager_rtnl_process_link
, m
);
762 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWADDR
, &manager_rtnl_process_address
, m
);
766 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELADDR
, &manager_rtnl_process_address
, m
);
770 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWROUTE
, &manager_rtnl_process_route
, m
);
774 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELROUTE
, &manager_rtnl_process_route
, m
);
781 static int ordered_set_put_in_addr(OrderedSet
*s
, const struct in_addr
*address
) {
787 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
791 r
= ordered_set_consume(s
, p
);
798 static int ordered_set_put_in_addrv(OrderedSet
*s
, const struct in_addr
*addresses
, int n
) {
802 assert(n
<= 0 || addresses
);
804 for (i
= 0; i
< n
; i
++) {
805 r
= ordered_set_put_in_addr(s
, addresses
+i
);
815 static void print_string_set(FILE *f
, const char *field
, OrderedSet
*s
) {
820 if (ordered_set_isempty(s
))
825 ORDERED_SET_FOREACH(p
, s
, i
)
826 fputs_with_space(f
, p
, NULL
, &space
);
831 static int manager_save(Manager
*m
) {
832 _cleanup_ordered_set_free_free_ OrderedSet
*dns
= NULL
, *ntp
= NULL
, *search_domains
= NULL
, *route_domains
= NULL
;
835 _cleanup_free_
char *temp_path
= NULL
;
836 _cleanup_fclose_
FILE *f
= NULL
;
837 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
838 const char *operstate_str
;
842 assert(m
->state_file
);
844 /* We add all NTP and DNS server to a set, to filter out duplicates */
845 dns
= ordered_set_new(&string_hash_ops
);
849 ntp
= ordered_set_new(&string_hash_ops
);
853 search_domains
= ordered_set_new(&dns_name_hash_ops
);
857 route_domains
= ordered_set_new(&dns_name_hash_ops
);
861 HASHMAP_FOREACH(link
, m
->links
, i
) {
862 if (link
->flags
& IFF_LOOPBACK
)
865 if (link
->operstate
> operstate
)
866 operstate
= link
->operstate
;
871 /* First add the static configured entries */
872 r
= ordered_set_put_strdupv(dns
, link
->network
->dns
);
876 r
= ordered_set_put_strdupv(ntp
, link
->network
->ntp
);
880 r
= ordered_set_put_strdupv(search_domains
, link
->network
->search_domains
);
884 r
= ordered_set_put_strdupv(route_domains
, link
->network
->route_domains
);
888 if (!link
->dhcp_lease
)
891 /* Secondly, add the entries acquired via DHCP */
892 if (link
->network
->dhcp_use_dns
) {
893 const struct in_addr
*addresses
;
895 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
897 r
= ordered_set_put_in_addrv(dns
, addresses
, r
);
900 } else if (r
< 0 && r
!= -ENODATA
)
904 if (link
->network
->dhcp_use_ntp
) {
905 const struct in_addr
*addresses
;
907 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
909 r
= ordered_set_put_in_addrv(ntp
, addresses
, r
);
912 } else if (r
< 0 && r
!= -ENODATA
)
916 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
917 const char *domainname
;
919 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
922 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
)
923 r
= ordered_set_put_strdup(search_domains
, domainname
);
925 r
= ordered_set_put_strdup(route_domains
, domainname
);
929 } else if (r
!= -ENODATA
)
934 operstate_str
= link_operstate_to_string(operstate
);
935 assert(operstate_str
);
937 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
941 fchmod(fileno(f
), 0644);
944 "# This is private data. Do not parse.\n"
945 "OPER_STATE=%s\n", operstate_str
);
947 print_string_set(f
, "DNS=", dns
);
948 print_string_set(f
, "NTP=", ntp
);
949 print_string_set(f
, "DOMAINS=", search_domains
);
950 print_string_set(f
, "ROUTE_DOMAINS=", route_domains
);
952 r
= fflush_and_check(f
);
956 if (rename(temp_path
, m
->state_file
) < 0) {
961 if (m
->operational_state
!= operstate
) {
962 m
->operational_state
= operstate
;
963 r
= manager_send_changed(m
, "OperationalState", NULL
);
965 log_error_errno(r
, "Could not emit changed OperationalState: %m");
973 (void) unlink(m
->state_file
);
974 (void) unlink(temp_path
);
976 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
979 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
980 Manager
*m
= userdata
;
990 SET_FOREACH(link
, m
->dirty_links
, i
) {
999 int manager_new(Manager
**ret
) {
1000 _cleanup_manager_free_ Manager
*m
= NULL
;
1003 m
= new0(Manager
, 1);
1007 m
->state_file
= strdup("/run/systemd/netif/state");
1011 r
= sd_event_default(&m
->event
);
1015 sd_event_set_watchdog(m
->event
, true);
1017 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
1018 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
1020 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1024 r
= manager_connect_rtnl(m
);
1028 r
= manager_connect_udev(m
);
1032 m
->netdevs
= hashmap_new(&string_hash_ops
);
1036 LIST_HEAD_INIT(m
->networks
);
1038 r
= setup_default_address_pool(m
);
1048 void manager_free(Manager
*m
) {
1057 free(m
->state_file
);
1059 while ((link
= hashmap_first(m
->links
)))
1061 hashmap_free(m
->links
);
1063 while ((network
= m
->networks
))
1064 network_free(network
);
1066 hashmap_free(m
->networks_by_name
);
1068 while ((netdev
= hashmap_first(m
->netdevs
)))
1069 netdev_unref(netdev
);
1070 hashmap_free(m
->netdevs
);
1072 while ((pool
= m
->address_pools
))
1073 address_pool_free(pool
);
1075 sd_netlink_unref(m
->rtnl
);
1076 sd_event_unref(m
->event
);
1078 sd_event_source_unref(m
->udev_event_source
);
1079 udev_monitor_unref(m
->udev_monitor
);
1080 udev_unref(m
->udev
);
1082 sd_bus_unref(m
->bus
);
1083 sd_bus_slot_unref(m
->prepare_for_sleep_slot
);
1084 sd_event_source_unref(m
->bus_retry_event_source
);
1089 static bool manager_check_idle(void *userdata
) {
1090 Manager
*m
= userdata
;
1096 HASHMAP_FOREACH(link
, m
->links
, i
) {
1097 /* we are not woken on udev activity, so let's just wait for the
1098 * pending udev event */
1099 if (link
->state
== LINK_STATE_PENDING
)
1105 /* we are not woken on netork activity, so let's stay around */
1106 if (link_lldp_enabled(link
) ||
1107 link_ipv4ll_enabled(link
) ||
1108 link_dhcp4_server_enabled(link
) ||
1109 link_dhcp4_enabled(link
) ||
1110 link_dhcp6_enabled(link
) ||
1111 link_ipv6_accept_ra_enabled(link
))
1118 int manager_run(Manager
*m
) {
1124 /* The dirty handler will deal with future serialization, but the first one
1125 must be done explicitly. */
1129 HASHMAP_FOREACH(link
, m
->links
, i
)
1133 return bus_event_loop_with_idle(
1136 "org.freedesktop.network1",
1141 /* failed to connect to the bus, so we lose exit-on-idle logic,
1142 this should not happen except if dbus is not around at all */
1143 return sd_event_loop(m
->event
);
1146 int manager_load_config(Manager
*m
) {
1149 /* update timestamp */
1150 paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, true);
1156 r
= network_load(m
);
1163 bool manager_should_reload(Manager
*m
) {
1164 return paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, false);
1167 int manager_rtnl_enumerate_links(Manager
*m
) {
1168 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1169 sd_netlink_message
*link
;
1175 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1179 r
= sd_netlink_message_request_dump(req
, true);
1183 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1187 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1190 m
->enumerating
= true;
1192 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1196 m
->enumerating
= false;
1202 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1203 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1204 sd_netlink_message
*addr
;
1210 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1214 r
= sd_netlink_message_request_dump(req
, true);
1218 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1222 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1225 m
->enumerating
= true;
1227 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1231 m
->enumerating
= false;
1237 int manager_rtnl_enumerate_routes(Manager
*m
) {
1238 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1239 sd_netlink_message
*route
;
1245 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
1249 r
= sd_netlink_message_request_dump(req
, true);
1253 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1257 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
1260 m
->enumerating
= true;
1262 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
1266 m
->enumerating
= false;
1272 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
1277 assert(prefixlen
> 0);
1280 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
1281 if (p
->family
!= family
)
1284 r
= address_pool_acquire(p
, prefixlen
, found
);
1292 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
1293 _cleanup_free_
struct local_address
*gateways
= NULL
;
1298 /* Looks for a suitable "uplink", via black magic: an
1299 * interface that is up and where the default route with the
1300 * highest priority points to. */
1302 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
1304 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
1308 for (i
= 0; i
< n
; i
++) {
1311 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
1313 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1317 if (link
== exclude
)
1320 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
1329 void manager_dirty(Manager
*manager
) {
1332 /* the serialized state in /run is no longer up-to-date */
1333 manager
->dirty
= true;