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"
34 #include "libudev-private.h"
35 #include "local-addresses.h"
36 #include "netlink-util.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");
198 static int manager_udev_process_link(Manager
*m
, struct udev_device
*device
) {
205 if (!streq_ptr(udev_device_get_action(device
), "add"))
208 ifindex
= udev_device_get_ifindex(device
);
210 log_debug("Ignoring udev ADD event for device with invalid ifindex");
214 r
= link_get(m
, ifindex
, &link
);
220 r
= link_initialized(link
, device
);
227 static int manager_dispatch_link_udev(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
228 Manager
*m
= userdata
;
229 struct udev_monitor
*monitor
= m
->udev_monitor
;
230 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
232 device
= udev_monitor_receive_device(monitor
);
236 manager_udev_process_link(m
, device
);
240 static int manager_connect_udev(Manager
*m
) {
243 /* udev does not initialize devices inside containers,
244 * so we rely on them being already initialized before
245 * entering the container */
246 if (detect_container() > 0)
249 m
->udev
= udev_new();
253 m
->udev_monitor
= udev_monitor_new_from_netlink(m
->udev
, "udev");
254 if (!m
->udev_monitor
)
257 r
= udev_monitor_filter_add_match_subsystem_devtype(m
->udev_monitor
, "net", NULL
);
259 return log_error_errno(r
, "Could not add udev monitor filter: %m");
261 r
= udev_monitor_enable_receiving(m
->udev_monitor
);
263 log_error("Could not enable udev monitor");
267 r
= sd_event_add_io(m
->event
,
268 &m
->udev_event_source
,
269 udev_monitor_get_fd(m
->udev_monitor
),
270 EPOLLIN
, manager_dispatch_link_udev
,
275 r
= sd_event_source_set_description(m
->udev_event_source
, "networkd-udev");
282 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
283 Manager
*m
= userdata
;
286 uint32_t ifindex
, priority
= 0;
287 unsigned char protocol
, scope
, tos
, table
;
289 unsigned char dst_prefixlen
, src_prefixlen
;
290 union in_addr_union dst
= {}, gw
= {}, src
= {}, prefsrc
= {};
298 if (sd_netlink_message_is_error(message
)) {
299 r
= sd_netlink_message_get_errno(message
);
301 log_warning_errno(r
, "rtnl: failed to receive route: %m");
306 r
= sd_netlink_message_get_type(message
, &type
);
308 log_warning_errno(r
, "rtnl: could not get message type: %m");
310 } else if (type
!= RTM_NEWROUTE
&& type
!= RTM_DELROUTE
) {
311 log_warning("rtnl: received unexpected message type when processing route");
315 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
317 log_debug("rtnl: received route without ifindex, ignoring");
320 log_warning_errno(r
, "rtnl: could not get ifindex from route, ignoring: %m");
322 } else if (ifindex
<= 0) {
323 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex
);
326 r
= link_get(m
, ifindex
, &link
);
327 if (r
< 0 || !link
) {
328 /* when enumerating we might be out of sync, but we will
329 * get the route again, so just ignore it */
331 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex
);
336 r
= sd_rtnl_message_route_get_family(message
, &family
);
337 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
338 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
342 r
= sd_rtnl_message_route_get_protocol(message
, &protocol
);
344 log_warning_errno(r
, "rtnl: could not get route protocol: %m");
350 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &dst
.in
);
351 if (r
< 0 && r
!= -ENODATA
) {
352 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
356 r
= sd_netlink_message_read_in_addr(message
, RTA_GATEWAY
, &gw
.in
);
357 if (r
< 0 && r
!= -ENODATA
) {
358 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
362 r
= sd_netlink_message_read_in_addr(message
, RTA_SRC
, &src
.in
);
363 if (r
< 0 && r
!= -ENODATA
) {
364 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
368 r
= sd_netlink_message_read_in_addr(message
, RTA_PREFSRC
, &prefsrc
.in
);
369 if (r
< 0 && r
!= -ENODATA
) {
370 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
377 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &dst
.in6
);
378 if (r
< 0 && r
!= -ENODATA
) {
379 log_link_warning_errno(link
, r
, "rtnl: received route without valid destination, ignoring: %m");
383 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &gw
.in6
);
384 if (r
< 0 && r
!= -ENODATA
) {
385 log_link_warning_errno(link
, r
, "rtnl: received route with invalid gateway, ignoring: %m");
389 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &src
.in6
);
390 if (r
< 0 && r
!= -ENODATA
) {
391 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source, ignoring: %m");
395 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &prefsrc
.in6
);
396 if (r
< 0 && r
!= -ENODATA
) {
397 log_link_warning_errno(link
, r
, "rtnl: received route with invalid preferred source, ignoring: %m");
404 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
408 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &dst_prefixlen
);
410 log_link_warning_errno(link
, r
, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
414 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &src_prefixlen
);
416 log_link_warning_errno(link
, r
, "rtnl: received route with invalid source prefixlen, ignoring: %m");
420 r
= sd_rtnl_message_route_get_scope(message
, &scope
);
422 log_link_warning_errno(link
, r
, "rtnl: received route with invalid scope, ignoring: %m");
426 r
= sd_rtnl_message_route_get_tos(message
, &tos
);
428 log_link_warning_errno(link
, r
, "rtnl: received route with invalid tos, ignoring: %m");
432 r
= sd_rtnl_message_route_get_table(message
, &table
);
434 log_link_warning_errno(link
, r
, "rtnl: received route with invalid table, ignoring: %m");
438 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &priority
);
439 if (r
< 0 && r
!= -ENODATA
) {
440 log_link_warning_errno(link
, r
, "rtnl: received route with invalid priority, ignoring: %m");
444 route_get(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
449 /* A route appeared that we did not request */
450 r
= route_add_foreign(link
, family
, &dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
455 route_update(route
, &src
, src_prefixlen
, &gw
, &prefsrc
, scope
, protocol
);
466 assert_not_reached("Received invalid RTNL message type");
472 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
473 Manager
*m
= userdata
;
478 unsigned char prefixlen
;
480 union in_addr_union in_addr
;
481 struct ifa_cacheinfo cinfo
;
482 Address
*address
= NULL
;
483 char buf
[INET6_ADDRSTRLEN
], valid_buf
[FORMAT_TIMESPAN_MAX
];
484 const char *valid_str
= NULL
;
491 if (sd_netlink_message_is_error(message
)) {
492 r
= sd_netlink_message_get_errno(message
);
494 log_warning_errno(r
, "rtnl: failed to receive address: %m");
499 r
= sd_netlink_message_get_type(message
, &type
);
501 log_warning_errno(r
, "rtnl: could not get message type: %m");
503 } else if (type
!= RTM_NEWADDR
&& type
!= RTM_DELADDR
) {
504 log_warning("rtnl: received unexpected message type when processing address");
508 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
510 log_warning_errno(r
, "rtnl: could not get ifindex from address: %m");
512 } else if (ifindex
<= 0) {
513 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex
);
516 r
= link_get(m
, ifindex
, &link
);
517 if (r
< 0 || !link
) {
518 /* when enumerating we might be out of sync, but we will
519 * get the address again, so just ignore it */
521 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex
);
526 r
= sd_rtnl_message_addr_get_family(message
, &family
);
527 if (r
< 0 || !IN_SET(family
, AF_INET
, AF_INET6
)) {
528 log_link_warning(link
, "rtnl: received address with invalid family, ignoring.");
532 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
534 log_link_warning_errno(link
, r
, "rtnl: received address with invalid prefixlen, ignoring: %m");
538 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
540 log_link_warning_errno(link
, r
, "rtnl: received address with invalid scope, ignoring: %m");
544 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
546 log_link_warning_errno(link
, r
, "rtnl: received address with invalid flags, ignoring: %m");
552 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
554 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
561 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
563 log_link_warning_errno(link
, r
, "rtnl: received address without valid address, ignoring: %m");
570 log_link_debug(link
, "rtnl: ignoring unsupported address family: %d", family
);
573 if (!inet_ntop(family
, &in_addr
, buf
, INET6_ADDRSTRLEN
)) {
574 log_link_warning(link
, "Could not print address");
578 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
580 if (cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
581 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
582 cinfo
.ifa_valid
* USEC_PER_SEC
,
586 address_get(link
, family
, &in_addr
, prefixlen
, &address
);
591 log_link_debug(link
, "Updating address: %s/%u (valid %s%s)", buf
, prefixlen
,
592 valid_str
? "for " : "forever", valid_str
?: "");
594 /* An address appeared that we did not request */
595 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
597 log_link_warning_errno(link
, r
, "Failed to add address %s/%u: %m", buf
, prefixlen
);
600 log_link_debug(link
, "Adding address: %s/%u (valid %s%s)", buf
, prefixlen
,
601 valid_str
? "for " : "forever", valid_str
?: "");
604 address_update(address
, flags
, scope
, &cinfo
);
611 log_link_debug(link
, "Removing address: %s/%u (valid %s%s)", buf
, prefixlen
,
612 valid_str
? "for " : "forever", valid_str
?: "");
613 address_drop(address
);
615 log_link_warning(link
, "Removing non-existent address: %s/%u (valid %s%s)", buf
, prefixlen
,
616 valid_str
? "for " : "forever", valid_str
?: "");
620 assert_not_reached("Received invalid RTNL message type");
626 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
627 Manager
*m
= userdata
;
629 NetDev
*netdev
= NULL
;
638 if (sd_netlink_message_is_error(message
)) {
639 r
= sd_netlink_message_get_errno(message
);
641 log_warning_errno(r
, "rtnl: Could not receive link: %m");
646 r
= sd_netlink_message_get_type(message
, &type
);
648 log_warning_errno(r
, "rtnl: Could not get message type: %m");
650 } else if (type
!= RTM_NEWLINK
&& type
!= RTM_DELLINK
) {
651 log_warning("rtnl: Received unexpected message type when processing link");
655 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
657 log_warning_errno(r
, "rtnl: Could not get ifindex from link: %m");
659 } else if (ifindex
<= 0) {
660 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex
);
664 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
666 log_warning_errno(r
, "rtnl: Received link message without ifname: %m");
670 (void) link_get(m
, ifindex
, &link
);
671 (void) netdev_get(m
, name
, &netdev
);
676 /* link is new, so add it */
677 r
= link_add(m
, message
, &link
);
679 log_warning_errno(r
, "Could not add new link: %m");
685 /* netdev exists, so make sure the ifindex matches */
686 r
= netdev_set_ifindex(netdev
, message
);
688 log_warning_errno(r
, "Could not set ifindex on netdev: %m");
693 r
= link_update(link
, message
);
706 assert_not_reached("Received invalid RTNL message type.");
712 static int systemd_netlink_fd(void) {
713 int n
, fd
, rtnl_fd
= -EINVAL
;
715 n
= sd_listen_fds(true);
719 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
720 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
731 static int manager_connect_rtnl(Manager
*m
) {
736 fd
= systemd_netlink_fd();
738 r
= sd_netlink_open(&m
->rtnl
);
740 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
744 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
748 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
752 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWLINK
, &manager_rtnl_process_link
, m
);
756 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELLINK
, &manager_rtnl_process_link
, m
);
760 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWADDR
, &manager_rtnl_process_address
, m
);
764 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELADDR
, &manager_rtnl_process_address
, m
);
768 r
= sd_netlink_add_match(m
->rtnl
, RTM_NEWROUTE
, &manager_rtnl_process_route
, m
);
772 r
= sd_netlink_add_match(m
->rtnl
, RTM_DELROUTE
, &manager_rtnl_process_route
, m
);
779 static int set_put_in_addr(Set
*s
, const struct in_addr
*address
) {
785 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
789 r
= set_consume(s
, p
);
796 static int set_put_in_addrv(Set
*s
, const struct in_addr
*addresses
, int n
) {
800 assert(n
<= 0 || addresses
);
802 for (i
= 0; i
< n
; i
++) {
803 r
= set_put_in_addr(s
, addresses
+i
);
813 static void print_string_set(FILE *f
, const char *field
, Set
*s
) {
823 SET_FOREACH(p
, s
, i
) {
832 static int manager_save(Manager
*m
) {
833 _cleanup_set_free_free_ Set
*dns
= NULL
, *ntp
= NULL
, *domains
= NULL
;
836 _cleanup_free_
char *temp_path
= NULL
;
837 _cleanup_fclose_
FILE *f
= NULL
;
838 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
839 const char *operstate_str
;
843 assert(m
->state_file
);
845 /* We add all NTP and DNS server to a set, to filter out duplicates */
846 dns
= set_new(&string_hash_ops
);
850 ntp
= set_new(&string_hash_ops
);
854 domains
= set_new(&string_hash_ops
);
858 HASHMAP_FOREACH(link
, m
->links
, i
) {
859 if (link
->flags
& IFF_LOOPBACK
)
862 if (link
->operstate
> operstate
)
863 operstate
= link
->operstate
;
868 /* First add the static configured entries */
869 r
= set_put_strdupv(dns
, link
->network
->dns
);
873 r
= set_put_strdupv(ntp
, link
->network
->ntp
);
877 r
= set_put_strdupv(domains
, link
->network
->domains
);
881 if (!link
->dhcp_lease
)
884 /* Secondly, add the entries acquired via DHCP */
885 if (link
->network
->dhcp_dns
) {
886 const struct in_addr
*addresses
;
888 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
890 r
= set_put_in_addrv(dns
, addresses
, r
);
893 } else if (r
< 0 && r
!= -ENODATA
)
897 if (link
->network
->dhcp_ntp
) {
898 const struct in_addr
*addresses
;
900 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
902 r
= set_put_in_addrv(ntp
, addresses
, r
);
905 } else if (r
< 0 && r
!= -ENODATA
)
909 if (link
->network
->dhcp_domains
) {
910 const char *domainname
;
912 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
914 r
= set_put_strdup(domains
, domainname
);
917 } else if (r
!= -ENODATA
)
922 operstate_str
= link_operstate_to_string(operstate
);
923 assert(operstate_str
);
925 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
929 fchmod(fileno(f
), 0644);
932 "# This is private data. Do not parse.\n"
933 "OPER_STATE=%s\n", operstate_str
);
935 print_string_set(f
, "DNS=", dns
);
936 print_string_set(f
, "NTP=", ntp
);
937 print_string_set(f
, "DOMAINS=", domains
);
939 r
= fflush_and_check(f
);
943 if (rename(temp_path
, m
->state_file
) < 0) {
948 if (m
->operational_state
!= operstate
) {
949 m
->operational_state
= operstate
;
950 r
= manager_send_changed(m
, "OperationalState", NULL
);
952 log_error_errno(r
, "Could not emit changed OperationalState: %m");
960 (void) unlink(m
->state_file
);
961 (void) unlink(temp_path
);
963 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
966 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
967 Manager
*m
= userdata
;
977 SET_FOREACH(link
, m
->dirty_links
, i
) {
986 int manager_new(Manager
**ret
) {
987 _cleanup_manager_free_ Manager
*m
= NULL
;
990 m
= new0(Manager
, 1);
994 m
->state_file
= strdup("/run/systemd/netif/state");
998 r
= sd_event_default(&m
->event
);
1002 sd_event_set_watchdog(m
->event
, true);
1004 sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
1005 sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
1007 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1011 r
= manager_connect_rtnl(m
);
1015 r
= manager_connect_udev(m
);
1019 m
->netdevs
= hashmap_new(&string_hash_ops
);
1023 LIST_HEAD_INIT(m
->networks
);
1025 r
= setup_default_address_pool(m
);
1035 void manager_free(Manager
*m
) {
1044 free(m
->state_file
);
1046 while ((link
= hashmap_first(m
->links
)))
1048 hashmap_free(m
->links
);
1050 while ((network
= m
->networks
))
1051 network_free(network
);
1053 hashmap_free(m
->networks_by_name
);
1055 while ((netdev
= hashmap_first(m
->netdevs
)))
1056 netdev_unref(netdev
);
1057 hashmap_free(m
->netdevs
);
1059 while ((pool
= m
->address_pools
))
1060 address_pool_free(pool
);
1062 sd_netlink_unref(m
->rtnl
);
1063 sd_event_unref(m
->event
);
1065 sd_event_source_unref(m
->udev_event_source
);
1066 udev_monitor_unref(m
->udev_monitor
);
1067 udev_unref(m
->udev
);
1069 sd_bus_unref(m
->bus
);
1070 sd_bus_slot_unref(m
->prepare_for_sleep_slot
);
1071 sd_event_source_unref(m
->bus_retry_event_source
);
1076 static bool manager_check_idle(void *userdata
) {
1077 Manager
*m
= userdata
;
1083 HASHMAP_FOREACH(link
, m
->links
, i
) {
1084 /* we are not woken on udev activity, so let's just wait for the
1085 * pending udev event */
1086 if (link
->state
== LINK_STATE_PENDING
)
1092 /* we are not woken on netork activity, so let's stay around */
1093 if (link_lldp_enabled(link
) ||
1094 link_ipv4ll_enabled(link
) ||
1095 link_dhcp4_server_enabled(link
) ||
1096 link_dhcp4_enabled(link
) ||
1097 link_dhcp6_enabled(link
) ||
1098 link_ipv6_accept_ra_enabled(link
))
1105 int manager_run(Manager
*m
) {
1111 /* The dirty handler will deal with future serialization, but the first one
1112 must be done explicitly. */
1116 HASHMAP_FOREACH(link
, m
->links
, i
)
1120 return bus_event_loop_with_idle(
1123 "org.freedesktop.network1",
1128 /* failed to connect to the bus, so we lose exit-on-idle logic,
1129 this should not happen except if dbus is not around at all */
1130 return sd_event_loop(m
->event
);
1133 int manager_load_config(Manager
*m
) {
1136 /* update timestamp */
1137 paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, true);
1143 r
= network_load(m
);
1150 bool manager_should_reload(Manager
*m
) {
1151 return paths_check_timestamp(network_dirs
, &m
->network_dirs_ts_usec
, false);
1154 int manager_rtnl_enumerate_links(Manager
*m
) {
1155 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1156 sd_netlink_message
*link
;
1162 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1166 r
= sd_netlink_message_request_dump(req
, true);
1170 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1174 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1177 m
->enumerating
= true;
1179 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1183 m
->enumerating
= false;
1189 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1190 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1191 sd_netlink_message
*addr
;
1197 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1201 r
= sd_netlink_message_request_dump(req
, true);
1205 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1209 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1212 m
->enumerating
= true;
1214 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1218 m
->enumerating
= false;
1224 int manager_rtnl_enumerate_routes(Manager
*m
) {
1225 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1226 sd_netlink_message
*route
;
1232 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
1236 r
= sd_netlink_message_request_dump(req
, true);
1240 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1244 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
1247 m
->enumerating
= true;
1249 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
1253 m
->enumerating
= false;
1259 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
1264 assert(prefixlen
> 0);
1267 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
1268 if (p
->family
!= family
)
1271 r
= address_pool_acquire(p
, prefixlen
, found
);
1279 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
1280 _cleanup_free_
struct local_address
*gateways
= NULL
;
1285 /* Looks for a suitable "uplink", via black magic: an
1286 * interface that is up and where the default route with the
1287 * highest priority points to. */
1289 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
1291 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
1295 for (i
= 0; i
< n
; i
++) {
1298 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
1300 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1304 if (link
== exclude
)
1307 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
1316 void manager_dirty(Manager
*manager
) {
1319 /* the serialized state in /run is no longer up-to-date */
1320 manager
->dirty
= true;