1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.h>
4 #include <sys/socket.h>
7 #include <linux/fib_rules.h>
8 #include <linux/nexthop.h>
10 #include "sd-daemon.h"
11 #include "sd-netlink.h"
13 #include "alloc-util.h"
14 #include "bus-polkit.h"
16 #include "conf-parser.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dns-domain.h"
23 #include "local-addresses.h"
24 #include "netlink-util.h"
25 #include "network-internal.h"
26 #include "networkd-dhcp6.h"
27 #include "networkd-link-bus.h"
28 #include "networkd-manager-bus.h"
29 #include "networkd-manager.h"
30 #include "networkd-network-bus.h"
31 #include "networkd-speed-meter.h"
32 #include "ordered-set.h"
33 #include "path-util.h"
35 #include "signal-util.h"
37 #include "sysctl-util.h"
38 #include "tmpfile-util.h"
39 #include "udev-util.h"
42 /* use 128 MB for receive socket kernel queue. */
43 #define RCVBUF_SIZE (128*1024*1024)
45 static int log_message_warning_errno(sd_netlink_message
*m
, int err
, const char *msg
) {
46 const char *err_msg
= NULL
;
48 (void) sd_netlink_message_read_string(m
, NLMSGERR_ATTR_MSG
, &err_msg
);
49 return log_warning_errno(err
, "%s: %s%s%m", msg
, strempty(err_msg
), err_msg
? " " : "");
52 static int setup_default_address_pool(Manager
*m
) {
58 /* Add in the well-known private address ranges. */
60 r
= address_pool_new_from_string(m
, &p
, AF_INET6
, "fd00::", 8);
64 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "10.0.0.0", 8);
68 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "172.16.0.0", 12);
72 r
= address_pool_new_from_string(m
, &p
, AF_INET
, "192.168.0.0", 16);
79 static int manager_reset_all(Manager
*m
) {
86 HASHMAP_FOREACH(link
, m
->links
, i
) {
87 r
= link_carrier_reset(link
);
89 log_link_warning_errno(link
, r
, "Could not reset carrier: %m");
95 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
96 Manager
*m
= userdata
;
102 r
= sd_bus_message_read(message
, "b", &b
);
104 log_debug_errno(r
, "Failed to parse PrepareForSleep signal: %m");
111 log_debug("Coming back from suspend, resetting all connections...");
113 (void) manager_reset_all(m
);
118 static int on_connected(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
119 Manager
*m
= userdata
;
124 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
125 if (m
->dynamic_hostname
)
126 (void) manager_set_hostname(m
, m
->dynamic_hostname
);
127 if (m
->dynamic_timezone
)
128 (void) manager_set_timezone(m
, m
->dynamic_timezone
);
129 if (m
->links_requesting_uuid
)
130 (void) manager_request_product_uuid(m
, NULL
);
135 int manager_connect_bus(Manager
*m
) {
143 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-network");
145 return log_error_errno(r
, "Failed to connect to bus: %m");
147 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable
, m
);
149 return log_error_errno(r
, "Failed to add manager object vtable: %m");
151 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable
, link_object_find
, m
);
153 return log_error_errno(r
, "Failed to add link object vtable: %m");
155 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/link", link_node_enumerator
, m
);
157 return log_error_errno(r
, "Failed to add link enumerator: %m");
159 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable
, network_object_find
, m
);
161 return log_error_errno(r
, "Failed to add network object vtable: %m");
163 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/network1/network", network_node_enumerator
, m
);
165 return log_error_errno(r
, "Failed to add network enumerator: %m");
167 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.network1", 0, NULL
, NULL
);
169 return log_error_errno(r
, "Failed to request name: %m");
171 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
173 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
175 r
= sd_bus_match_signal_async(
178 "org.freedesktop.DBus.Local",
180 "org.freedesktop.DBus.Local",
182 on_connected
, NULL
, m
);
184 return log_error_errno(r
, "Failed to request match on Connected signal: %m");
186 r
= sd_bus_match_signal_async(
189 "org.freedesktop.login1",
190 "/org/freedesktop/login1",
191 "org.freedesktop.login1.Manager",
193 match_prepare_for_sleep
, NULL
, m
);
195 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
200 static int manager_udev_process_link(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
201 Manager
*m
= userdata
;
209 r
= device_get_action(device
, &action
);
211 log_device_debug_errno(device
, r
, "Failed to get udev action, ignoring device: %m");
215 if (!IN_SET(action
, DEVICE_ACTION_ADD
, DEVICE_ACTION_CHANGE
, DEVICE_ACTION_MOVE
)) {
216 log_device_debug(device
, "Ignoring udev %s event for device.", device_action_to_string(action
));
220 r
= sd_device_get_ifindex(device
, &ifindex
);
222 log_device_debug_errno(device
, r
, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m");
226 r
= device_is_renaming(device
);
228 log_device_error_errno(device
, r
, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
229 device_action_to_string(action
));
233 log_device_debug(device
, "Interface is under renaming, wait for the interface to be renamed: %m");
237 r
= link_get(m
, ifindex
, &link
);
240 log_debug_errno(r
, "Failed to get link from ifindex %i, ignoring: %m", ifindex
);
244 (void) link_initialized(link
, device
);
249 static int manager_connect_udev(Manager
*m
) {
252 /* udev does not initialize devices inside containers,
253 * so we rely on them being already initialized before
254 * entering the container */
255 if (detect_container() > 0)
258 r
= sd_device_monitor_new(&m
->device_monitor
);
260 return log_error_errno(r
, "Failed to initialize device monitor: %m");
262 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "net", NULL
);
264 return log_error_errno(r
, "Could not add device monitor filter: %m");
266 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
268 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
270 r
= sd_device_monitor_start(m
->device_monitor
, manager_udev_process_link
, m
);
272 return log_error_errno(r
, "Failed to start device monitor: %m");
277 int manager_rtnl_process_route(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
278 _cleanup_(route_freep
) Route
*tmp
= NULL
;
280 Manager
*m
= userdata
;
291 if (sd_netlink_message_is_error(message
)) {
292 r
= sd_netlink_message_get_errno(message
);
294 log_message_warning_errno(message
, r
, "rtnl: failed to receive route message, ignoring");
299 r
= sd_netlink_message_get_type(message
, &type
);
301 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
303 } else if (!IN_SET(type
, RTM_NEWROUTE
, RTM_DELROUTE
)) {
304 log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type
);
308 r
= sd_netlink_message_read_u32(message
, RTA_OIF
, &ifindex
);
310 log_debug("rtnl: received route message without ifindex, ignoring");
313 log_warning_errno(r
, "rtnl: could not get ifindex from route message, ignoring: %m");
315 } else if (ifindex
<= 0) {
316 log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex
);
320 r
= link_get(m
, ifindex
, &link
);
321 if (r
< 0 || !link
) {
322 /* when enumerating we might be out of sync, but we will
323 * get the route again, so just ignore it */
325 log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex
);
333 r
= sd_rtnl_message_route_get_family(message
, &tmp
->family
);
335 log_link_warning(link
, "rtnl: received route message without family, ignoring");
337 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
338 log_link_debug(link
, "rtnl: received route message with invalid family '%i', ignoring", tmp
->family
);
342 r
= sd_rtnl_message_route_get_protocol(message
, &tmp
->protocol
);
344 log_warning_errno(r
, "rtnl: received route message without route protocol: %m");
348 switch (tmp
->family
) {
350 r
= sd_netlink_message_read_in_addr(message
, RTA_DST
, &tmp
->dst
.in
);
351 if (r
< 0 && r
!= -ENODATA
) {
352 log_link_warning_errno(link
, r
, "rtnl: received route message without valid destination, ignoring: %m");
356 r
= sd_netlink_message_read_in_addr(message
, RTA_GATEWAY
, &tmp
->gw
.in
);
357 if (r
< 0 && r
!= -ENODATA
) {
358 log_link_warning_errno(link
, r
, "rtnl: received route message without valid gateway, ignoring: %m");
362 r
= sd_netlink_message_read_in_addr(message
, RTA_SRC
, &tmp
->src
.in
);
363 if (r
< 0 && r
!= -ENODATA
) {
364 log_link_warning_errno(link
, r
, "rtnl: received route message without valid source, ignoring: %m");
368 r
= sd_netlink_message_read_in_addr(message
, RTA_PREFSRC
, &tmp
->prefsrc
.in
);
369 if (r
< 0 && r
!= -ENODATA
) {
370 log_link_warning_errno(link
, r
, "rtnl: received route message without valid preferred source, ignoring: %m");
377 r
= sd_netlink_message_read_in6_addr(message
, RTA_DST
, &tmp
->dst
.in6
);
378 if (r
< 0 && r
!= -ENODATA
) {
379 log_link_warning_errno(link
, r
, "rtnl: received route message without valid destination, ignoring: %m");
383 r
= sd_netlink_message_read_in6_addr(message
, RTA_GATEWAY
, &tmp
->gw
.in6
);
384 if (r
< 0 && r
!= -ENODATA
) {
385 log_link_warning_errno(link
, r
, "rtnl: received route message without valid gateway, ignoring: %m");
389 r
= sd_netlink_message_read_in6_addr(message
, RTA_SRC
, &tmp
->src
.in6
);
390 if (r
< 0 && r
!= -ENODATA
) {
391 log_link_warning_errno(link
, r
, "rtnl: received route message without valid source, ignoring: %m");
395 r
= sd_netlink_message_read_in6_addr(message
, RTA_PREFSRC
, &tmp
->prefsrc
.in6
);
396 if (r
< 0 && r
!= -ENODATA
) {
397 log_link_warning_errno(link
, r
, "rtnl: received route message without valid preferred source, ignoring: %m");
404 assert_not_reached("Received route message with unsupported address family");
408 r
= sd_rtnl_message_route_get_dst_prefixlen(message
, &tmp
->dst_prefixlen
);
410 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid destination prefixlen, ignoring: %m");
414 r
= sd_rtnl_message_route_get_src_prefixlen(message
, &tmp
->src_prefixlen
);
416 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid source prefixlen, ignoring: %m");
420 r
= sd_rtnl_message_route_get_scope(message
, &tmp
->scope
);
422 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid scope, ignoring: %m");
426 r
= sd_rtnl_message_route_get_tos(message
, &tmp
->tos
);
428 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid tos, ignoring: %m");
432 r
= sd_rtnl_message_route_get_type(message
, &tmp
->type
);
434 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid type, ignoring: %m");
438 r
= sd_rtnl_message_route_get_table(message
, &table
);
440 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid table, ignoring: %m");
445 r
= sd_netlink_message_read_u32(message
, RTA_PRIORITY
, &tmp
->priority
);
446 if (r
< 0 && r
!= -ENODATA
) {
447 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid priority, ignoring: %m");
451 r
= sd_netlink_message_enter_container(message
, RTA_METRICS
);
452 if (r
< 0 && r
!= -ENODATA
) {
453 log_link_error_errno(link
, r
, "rtnl: Could not enter RTA_METRICS container: %m");
457 r
= sd_netlink_message_read_u32(message
, RTAX_INITCWND
, &tmp
->initcwnd
);
458 if (r
< 0 && r
!= -ENODATA
) {
459 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid initcwnd, ignoring: %m");
463 r
= sd_netlink_message_read_u32(message
, RTAX_INITRWND
, &tmp
->initrwnd
);
464 if (r
< 0 && r
!= -ENODATA
) {
465 log_link_warning_errno(link
, r
, "rtnl: received route message with invalid initrwnd, ignoring: %m");
469 r
= sd_netlink_message_exit_container(message
);
471 log_link_error_errno(link
, r
, "rtnl: Could not exit from RTA_METRICS container: %m");
476 (void) route_get(link
, tmp
, &route
);
479 _cleanup_free_
char *buf_dst
= NULL
, *buf_dst_prefixlen
= NULL
,
480 *buf_src
= NULL
, *buf_gw
= NULL
, *buf_prefsrc
= NULL
;
481 char buf_scope
[ROUTE_SCOPE_STR_MAX
], buf_table
[ROUTE_TABLE_STR_MAX
],
482 buf_protocol
[ROUTE_PROTOCOL_STR_MAX
];
484 if (!in_addr_is_null(tmp
->family
, &tmp
->dst
)) {
485 (void) in_addr_to_string(tmp
->family
, &tmp
->dst
, &buf_dst
);
486 (void) asprintf(&buf_dst_prefixlen
, "/%u", tmp
->dst_prefixlen
);
488 if (!in_addr_is_null(tmp
->family
, &tmp
->src
))
489 (void) in_addr_to_string(tmp
->family
, &tmp
->src
, &buf_src
);
490 if (!in_addr_is_null(tmp
->family
, &tmp
->gw
))
491 (void) in_addr_to_string(tmp
->family
, &tmp
->gw
, &buf_gw
);
492 if (!in_addr_is_null(tmp
->family
, &tmp
->prefsrc
))
493 (void) in_addr_to_string(tmp
->family
, &tmp
->prefsrc
, &buf_prefsrc
);
496 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
497 type
== RTM_DELROUTE
? "Forgetting" : route
? "Received remembered" : "Remembering",
498 strna(buf_dst
), strempty(buf_dst_prefixlen
),
499 strna(buf_src
), strna(buf_gw
), strna(buf_prefsrc
),
500 format_route_scope(tmp
->scope
, buf_scope
, sizeof buf_scope
),
501 format_route_table(tmp
->table
, buf_table
, sizeof buf_table
),
502 format_route_protocol(tmp
->protocol
, buf_protocol
, sizeof buf_protocol
),
503 strna(route_type_to_string(tmp
->type
)));
509 /* A route appeared that we did not request */
510 r
= route_add_foreign(link
, tmp
, &route
);
512 log_link_warning_errno(link
, r
, "Failed to remember foreign route, ignoring: %m");
524 assert_not_reached("Received route message with invalid RTNL message type");
530 static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message
*message
, union lladdr_union
*lladdr
, size_t *size
, char **str
) {
540 r
= sd_netlink_message_read(message
, NDA_LLADDR
, sizeof(lladdr
->ip
.in6
), &lladdr
->ip
.in6
);
542 *size
= sizeof(lladdr
->ip
.in6
);
543 if (in_addr_to_string(AF_INET6
, &lladdr
->ip
, str
) < 0)
544 log_warning_errno(r
, "Could not print lower address: %m");
548 r
= sd_netlink_message_read(message
, NDA_LLADDR
, sizeof(lladdr
->mac
), &lladdr
->mac
);
550 *size
= sizeof(lladdr
->mac
);
551 *str
= new(char, ETHER_ADDR_TO_STRING_MAX
);
556 ether_addr_to_string(&lladdr
->mac
, *str
);
560 r
= sd_netlink_message_read(message
, NDA_LLADDR
, sizeof(lladdr
->ip
.in
), &lladdr
->ip
.in
);
562 *size
= sizeof(lladdr
->ip
.in
);
563 if (in_addr_to_string(AF_INET
, &lladdr
->ip
, str
) < 0)
564 log_warning_errno(r
, "Could not print lower address: %m");
571 int manager_rtnl_process_neighbor(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
572 Manager
*m
= userdata
;
574 Neighbor
*neighbor
= NULL
;
575 int ifindex
, family
, r
;
576 uint16_t type
, state
;
577 union in_addr_union in_addr
= IN_ADDR_NULL
;
578 _cleanup_free_
char *addr_str
= NULL
;
579 union lladdr_union lladdr
;
580 size_t lladdr_size
= 0;
581 _cleanup_free_
char *lladdr_str
= NULL
;
587 if (sd_netlink_message_is_error(message
)) {
588 r
= sd_netlink_message_get_errno(message
);
590 log_message_warning_errno(message
, r
, "rtnl: failed to receive neighbor message, ignoring");
595 r
= sd_netlink_message_get_type(message
, &type
);
597 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
599 } else if (!IN_SET(type
, RTM_NEWNEIGH
, RTM_DELNEIGH
)) {
600 log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type
);
604 r
= sd_rtnl_message_neigh_get_state(message
, &state
);
606 log_link_warning_errno(link
, r
, "rtnl: received neighbor message with invalid state, ignoring: %m");
608 } else if (!FLAGS_SET(state
, NUD_PERMANENT
)) {
609 log_debug("rtnl: received non-static neighbor, ignoring.");
613 r
= sd_rtnl_message_neigh_get_ifindex(message
, &ifindex
);
615 log_warning_errno(r
, "rtnl: could not get ifindex from message, ignoring: %m");
617 } else if (ifindex
<= 0) {
618 log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex
);
622 r
= link_get(m
, ifindex
, &link
);
623 if (r
< 0 || !link
) {
624 /* when enumerating we might be out of sync, but we will get the neighbor again, so just
627 log_warning("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex
);
631 r
= sd_rtnl_message_neigh_get_family(message
, &family
);
633 log_link_warning(link
, "rtnl: received neighbor message without family, ignoring.");
635 } else if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
636 log_link_debug(link
, "rtnl: received neighbor message with invalid family '%i', ignoring.", family
);
642 r
= sd_netlink_message_read_in_addr(message
, NDA_DST
, &in_addr
.in
);
644 log_link_warning_errno(link
, r
, "rtnl: received neighbor message without valid address, ignoring: %m");
651 r
= sd_netlink_message_read_in6_addr(message
, NDA_DST
, &in_addr
.in6
);
653 log_link_warning_errno(link
, r
, "rtnl: received neighbor message without valid address, ignoring: %m");
660 assert_not_reached("Received unsupported address family");
663 if (in_addr_to_string(family
, &in_addr
, &addr_str
) < 0)
664 log_link_warning_errno(link
, r
, "Could not print address: %m");
666 r
= manager_rtnl_process_neighbor_lladdr(message
, &lladdr
, &lladdr_size
, &lladdr_str
);
668 log_link_warning_errno(link
, r
, "rtnl: received neighbor message with invalid lladdr, ignoring: %m");
672 (void) neighbor_get(link
, family
, &in_addr
, &lladdr
, lladdr_size
, &neighbor
);
677 log_link_debug(link
, "Remembering neighbor: %s->%s",
678 strnull(addr_str
), strnull(lladdr_str
));
680 /* A neighbor appeared that we did not request */
681 r
= neighbor_add_foreign(link
, family
, &in_addr
, &lladdr
, lladdr_size
, &neighbor
);
683 log_link_warning_errno(link
, r
, "Failed to remember foreign neighbor %s->%s, ignoring: %m",
684 strnull(addr_str
), strnull(lladdr_str
));
687 log_link_debug(link
, "Remembering foreign neighbor: %s->%s",
688 strnull(addr_str
), strnull(lladdr_str
));
695 log_link_debug(link
, "Forgetting neighbor: %s->%s",
696 strnull(addr_str
), strnull(lladdr_str
));
697 (void) neighbor_free(neighbor
);
699 log_link_debug(link
, "Kernel removed a neighbor we don't remember: %s->%s, ignoring.",
700 strnull(addr_str
), strnull(lladdr_str
));
705 assert_not_reached("Received invalid RTNL message type");
711 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
712 _cleanup_free_
char *buf
= NULL
;
713 Manager
*m
= userdata
;
716 unsigned char flags
, prefixlen
, scope
;
717 union in_addr_union in_addr
= IN_ADDR_NULL
;
718 struct ifa_cacheinfo cinfo
;
719 Address
*address
= NULL
;
720 char valid_buf
[FORMAT_TIMESPAN_MAX
];
721 const char *valid_str
= NULL
;
722 int ifindex
, family
, r
;
728 if (sd_netlink_message_is_error(message
)) {
729 r
= sd_netlink_message_get_errno(message
);
731 log_message_warning_errno(message
, r
, "rtnl: failed to receive address message, ignoring");
736 r
= sd_netlink_message_get_type(message
, &type
);
738 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
740 } else if (!IN_SET(type
, RTM_NEWADDR
, RTM_DELADDR
)) {
741 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type
);
745 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
747 log_warning_errno(r
, "rtnl: could not get ifindex from message, ignoring: %m");
749 } else if (ifindex
<= 0) {
750 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex
);
754 r
= link_get(m
, ifindex
, &link
);
755 if (r
< 0 || !link
) {
756 /* when enumerating we might be out of sync, but we will get the address again, so just
759 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex
);
763 r
= sd_rtnl_message_addr_get_family(message
, &family
);
765 log_link_warning(link
, "rtnl: received address message without family, ignoring.");
767 } else if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
768 log_link_debug(link
, "rtnl: received address message with invalid family '%i', ignoring.", family
);
772 r
= sd_rtnl_message_addr_get_prefixlen(message
, &prefixlen
);
774 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid prefixlen, ignoring: %m");
778 r
= sd_rtnl_message_addr_get_scope(message
, &scope
);
780 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid scope, ignoring: %m");
784 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
786 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
792 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &in_addr
.in
);
794 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
801 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &in_addr
.in6
);
803 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
810 assert_not_reached("Received unsupported address family");
813 r
= in_addr_to_string(family
, &in_addr
, &buf
);
815 log_link_warning_errno(link
, r
, "Could not print address: %m");
817 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
818 if (r
< 0 && r
!= -ENODATA
) {
819 log_link_warning_errno(link
, r
, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
821 } else if (r
>= 0 && cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
822 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
823 cinfo
.ifa_valid
* USEC_PER_SEC
,
826 (void) address_get(link
, family
, &in_addr
, prefixlen
, &address
);
831 log_link_debug(link
, "Remembering updated address: %s/%u (valid %s%s)",
832 strnull(buf
), prefixlen
,
833 valid_str
? "for " : "forever", strempty(valid_str
));
835 /* An address appeared that we did not request */
836 r
= address_add_foreign(link
, family
, &in_addr
, prefixlen
, &address
);
838 log_link_warning_errno(link
, r
, "Failed to remember foreign address %s/%u, ignoring: %m",
839 strnull(buf
), prefixlen
);
842 log_link_debug(link
, "Remembering foreign address: %s/%u (valid %s%s)",
843 strnull(buf
), prefixlen
,
844 valid_str
? "for " : "forever", strempty(valid_str
));
847 /* address_update() logs internally, so we don't need to. */
848 (void) address_update(address
, flags
, scope
, &cinfo
);
854 log_link_debug(link
, "Forgetting address: %s/%u (valid %s%s)",
855 strnull(buf
), prefixlen
,
856 valid_str
? "for " : "forever", strempty(valid_str
));
857 (void) address_drop(address
);
859 log_link_debug(link
, "Kernel removed an address we don't remember: %s/%u (valid %s%s), ignoring.",
860 strnull(buf
), prefixlen
,
861 valid_str
? "for " : "forever", strempty(valid_str
));
866 assert_not_reached("Received invalid RTNL message type");
872 static int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
873 Manager
*m
= userdata
;
875 NetDev
*netdev
= NULL
;
884 if (sd_netlink_message_is_error(message
)) {
885 r
= sd_netlink_message_get_errno(message
);
887 log_message_warning_errno(message
, r
, "rtnl: Could not receive link message, ignoring");
892 r
= sd_netlink_message_get_type(message
, &type
);
894 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
896 } else if (!IN_SET(type
, RTM_NEWLINK
, RTM_DELLINK
)) {
897 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type
);
901 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
903 log_warning_errno(r
, "rtnl: Could not get ifindex from link message, ignoring: %m");
905 } else if (ifindex
<= 0) {
906 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex
);
910 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
912 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
916 (void) link_get(m
, ifindex
, &link
);
917 (void) netdev_get(m
, name
, &netdev
);
922 /* link is new, so add it */
923 r
= link_add(m
, message
, &link
);
925 log_warning_errno(r
, "Could not process new link message, ignoring: %m");
931 /* netdev exists, so make sure the ifindex matches */
932 r
= netdev_set_ifindex(netdev
, message
);
934 log_warning_errno(r
, "Could not process new link message for netdev, ignoring: %m");
939 r
= link_update(link
, message
);
941 log_warning_errno(r
, "Could not process link message, ignoring: %m");
954 assert_not_reached("Received link message with invalid RTNL message type.");
960 int manager_rtnl_process_rule(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
961 _cleanup_(routing_policy_rule_freep
) RoutingPolicyRule
*tmp
= NULL
;
962 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
963 RoutingPolicyRule
*rule
= NULL
;
964 const char *iif
= NULL
, *oif
= NULL
;
965 Manager
*m
= userdata
;
974 if (sd_netlink_message_is_error(message
)) {
975 r
= sd_netlink_message_get_errno(message
);
977 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
982 r
= sd_netlink_message_get_type(message
, &type
);
984 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
986 } else if (!IN_SET(type
, RTM_NEWRULE
, RTM_DELRULE
)) {
987 log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type
);
991 r
= routing_policy_rule_new(&tmp
);
997 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
999 log_warning_errno(r
, "rtnl: could not get rule family, ignoring: %m");
1001 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
1002 log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp
->family
);
1006 switch (tmp
->family
) {
1008 r
= sd_netlink_message_read_in_addr(message
, FRA_SRC
, &tmp
->from
.in
);
1009 if (r
< 0 && r
!= -ENODATA
) {
1010 log_warning_errno(r
, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1012 } else if (r
>= 0) {
1013 r
= sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message
, &tmp
->from_prefixlen
);
1015 log_warning_errno(r
, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1020 r
= sd_netlink_message_read_in_addr(message
, FRA_DST
, &tmp
->to
.in
);
1021 if (r
< 0 && r
!= -ENODATA
) {
1022 log_warning_errno(r
, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1024 } else if (r
>= 0) {
1025 r
= sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message
, &tmp
->to_prefixlen
);
1027 log_warning_errno(r
, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1035 r
= sd_netlink_message_read_in6_addr(message
, FRA_SRC
, &tmp
->from
.in6
);
1036 if (r
< 0 && r
!= -ENODATA
) {
1037 log_warning_errno(r
, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1039 } else if (r
>= 0) {
1040 r
= sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message
, &tmp
->from_prefixlen
);
1042 log_warning_errno(r
, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1047 r
= sd_netlink_message_read_in6_addr(message
, FRA_DST
, &tmp
->to
.in6
);
1048 if (r
< 0 && r
!= -ENODATA
) {
1049 log_warning_errno(r
, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1051 } else if (r
>= 0) {
1052 r
= sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message
, &tmp
->to_prefixlen
);
1054 log_warning_errno(r
, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1062 assert_not_reached("Received rule message with unsupported address family");
1065 if (tmp
->from_prefixlen
== 0 && tmp
->to_prefixlen
== 0)
1068 r
= sd_rtnl_message_routing_policy_rule_get_flags(message
, &flags
);
1070 log_warning_errno(r
, "rtnl: received rule message without valid flag, ignoring: %m");
1073 tmp
->invert_rule
= flags
& FIB_RULE_INVERT
;
1075 r
= sd_netlink_message_read_u32(message
, FRA_FWMARK
, &tmp
->fwmark
);
1076 if (r
< 0 && r
!= -ENODATA
) {
1077 log_warning_errno(r
, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
1081 r
= sd_netlink_message_read_u32(message
, FRA_FWMASK
, &tmp
->fwmask
);
1082 if (r
< 0 && r
!= -ENODATA
) {
1083 log_warning_errno(r
, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
1087 r
= sd_netlink_message_read_u32(message
, FRA_PRIORITY
, &tmp
->priority
);
1088 if (r
< 0 && r
!= -ENODATA
) {
1089 log_warning_errno(r
, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
1093 r
= sd_netlink_message_read_u32(message
, FRA_TABLE
, &tmp
->table
);
1094 if (r
< 0 && r
!= -ENODATA
) {
1095 log_warning_errno(r
, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
1099 r
= sd_rtnl_message_routing_policy_rule_get_tos(message
, &tmp
->tos
);
1100 if (r
< 0 && r
!= -ENODATA
) {
1101 log_warning_errno(r
, "rtnl: could not get ip rule TOS, ignoring: %m");
1105 r
= sd_netlink_message_read_string(message
, FRA_IIFNAME
, &iif
);
1106 if (r
< 0 && r
!= -ENODATA
) {
1107 log_warning_errno(r
, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
1110 r
= free_and_strdup(&tmp
->iif
, iif
);
1114 r
= sd_netlink_message_read_string(message
, FRA_OIFNAME
, &oif
);
1115 if (r
< 0 && r
!= -ENODATA
) {
1116 log_warning_errno(r
, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
1119 r
= free_and_strdup(&tmp
->oif
, oif
);
1123 r
= sd_netlink_message_read_u8(message
, FRA_IP_PROTO
, &tmp
->protocol
);
1124 if (r
< 0 && r
!= -ENODATA
) {
1125 log_warning_errno(r
, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
1129 r
= sd_netlink_message_read(message
, FRA_SPORT_RANGE
, sizeof(tmp
->sport
), &tmp
->sport
);
1130 if (r
< 0 && r
!= -ENODATA
) {
1131 log_warning_errno(r
, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
1135 r
= sd_netlink_message_read(message
, FRA_DPORT_RANGE
, sizeof(tmp
->dport
), &tmp
->dport
);
1136 if (r
< 0 && r
!= -ENODATA
) {
1137 log_warning_errno(r
, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
1141 r
= sd_netlink_message_read(message
, FRA_UID_RANGE
, sizeof(tmp
->uid_range
), &tmp
->uid_range
);
1142 if (r
< 0 && r
!= -ENODATA
) {
1143 log_warning_errno(r
, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m");
1147 (void) routing_policy_rule_get(m
, tmp
, &rule
);
1149 if (DEBUG_LOGGING
) {
1150 (void) in_addr_to_string(tmp
->family
, &tmp
->from
, &from
);
1151 (void) in_addr_to_string(tmp
->family
, &tmp
->to
, &to
);
1157 log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1158 from
, tmp
->from_prefixlen
, to
, tmp
->to_prefixlen
, strna(tmp
->iif
), strna(tmp
->oif
), tmp
->table
);
1159 r
= routing_policy_rule_add_foreign(m
, tmp
, &rule
);
1161 log_warning_errno(r
, "Could not remember foreign rule, ignoring: %m");
1167 log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1168 from
, tmp
->from_prefixlen
, to
, tmp
->to_prefixlen
, strna(tmp
->iif
), strna(tmp
->oif
), tmp
->table
);
1169 routing_policy_rule_free(rule
);
1174 assert_not_reached("Received invalid RTNL message type");
1180 int manager_rtnl_process_nexthop(sd_netlink
*rtnl
, sd_netlink_message
*message
, void *userdata
) {
1181 _cleanup_(nexthop_freep
) NextHop
*tmp
= NULL
;
1182 _cleanup_free_
char *gateway
= NULL
;
1183 NextHop
*nexthop
= NULL
;
1184 Manager
*m
= userdata
;
1193 if (sd_netlink_message_is_error(message
)) {
1194 r
= sd_netlink_message_get_errno(message
);
1196 log_message_warning_errno(message
, r
, "rtnl: failed to receive rule message, ignoring");
1201 r
= sd_netlink_message_get_type(message
, &type
);
1203 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1205 } else if (!IN_SET(type
, RTM_NEWNEXTHOP
, RTM_DELNEXTHOP
)) {
1206 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type
);
1210 r
= nexthop_new(&tmp
);
1214 r
= sd_rtnl_message_get_family(message
, &tmp
->family
);
1216 log_warning_errno(r
, "rtnl: could not get nexthop family, ignoring: %m");
1218 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
1219 log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp
->family
);
1223 switch (tmp
->family
) {
1225 r
= sd_netlink_message_read_in_addr(message
, NHA_GATEWAY
, &tmp
->gw
.in
);
1226 if (r
< 0 && r
!= -ENODATA
) {
1227 log_warning_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1233 r
= sd_netlink_message_read_in6_addr(message
, NHA_GATEWAY
, &tmp
->gw
.in6
);
1234 if (r
< 0 && r
!= -ENODATA
) {
1235 log_warning_errno(r
, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1241 assert_not_reached("Received rule message with unsupported address family");
1244 r
= sd_netlink_message_read_u32(message
, NHA_ID
, &tmp
->id
);
1245 if (r
< 0 && r
!= -ENODATA
) {
1246 log_warning_errno(r
, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1250 r
= sd_netlink_message_read_u32(message
, NHA_OIF
, &tmp
->oif
);
1251 if (r
< 0 && r
!= -ENODATA
) {
1252 log_warning_errno(r
, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1256 r
= link_get(m
, tmp
->oif
, &link
);
1257 if (r
< 0 || !link
) {
1258 if (!m
->enumerating
)
1259 log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp
->oif
);
1263 (void) nexthop_get(link
, tmp
, &nexthop
);
1266 (void) in_addr_to_string(tmp
->family
, &tmp
->gw
, &gateway
);
1269 case RTM_NEWNEXTHOP
:
1271 log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway
, tmp
->oif
, tmp
->id
);
1272 r
= nexthop_add_foreign(link
, tmp
, &nexthop
);
1274 log_warning_errno(r
, "Could not remember foreign nexthop, ignoring: %m");
1279 case RTM_DELNEXTHOP
:
1280 log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway
, tmp
->oif
, tmp
->id
);
1281 nexthop_free(nexthop
);
1286 assert_not_reached("Received invalid RTNL message type");
1292 static int systemd_netlink_fd(void) {
1293 int n
, fd
, rtnl_fd
= -EINVAL
;
1295 n
= sd_listen_fds(true);
1299 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
1300 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
1311 static int manager_connect_genl(Manager
*m
) {
1316 r
= sd_genl_socket_open(&m
->genl
);
1320 r
= sd_netlink_inc_rcvbuf(m
->genl
, RCVBUF_SIZE
);
1324 r
= sd_netlink_attach_event(m
->genl
, m
->event
, 0);
1331 static int manager_connect_rtnl(Manager
*m
) {
1336 fd
= systemd_netlink_fd();
1338 r
= sd_netlink_open(&m
->rtnl
);
1340 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
1344 r
= sd_netlink_inc_rcvbuf(m
->rtnl
, RCVBUF_SIZE
);
1348 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
1352 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
1356 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
1360 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
1364 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
1368 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWNEIGH
, &manager_rtnl_process_neighbor
, NULL
, m
, "network-rtnl_process_neighbor");
1372 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELNEIGH
, &manager_rtnl_process_neighbor
, NULL
, m
, "network-rtnl_process_neighbor");
1376 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
1380 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
1384 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
1388 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
1392 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWNEXTHOP
, &manager_rtnl_process_nexthop
, NULL
, m
, "network-rtnl_process_nexthop");
1396 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELNEXTHOP
, &manager_rtnl_process_nexthop
, NULL
, m
, "network-rtnl_process_nexthop");
1403 static int ordered_set_put_in_addr_data(OrderedSet
*s
, const struct in_addr_data
*address
) {
1410 r
= in_addr_to_string(address
->family
, &address
->address
, &p
);
1414 r
= ordered_set_consume(s
, p
);
1421 static int ordered_set_put_in_addr_datav(OrderedSet
*s
, const struct in_addr_data
*addresses
, unsigned n
) {
1426 assert(addresses
|| n
== 0);
1428 for (i
= 0; i
< n
; i
++) {
1429 r
= ordered_set_put_in_addr_data(s
, addresses
+i
);
1439 static int ordered_set_put_in4_addr(OrderedSet
*s
, const struct in_addr
*address
) {
1446 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) address
, &p
);
1450 r
= ordered_set_consume(s
, p
);
1457 static int ordered_set_put_in4_addrv(OrderedSet
*s
,
1458 const struct in_addr
*addresses
,
1460 bool (*predicate
)(const struct in_addr
*addr
)) {
1465 assert(n
== 0 || addresses
);
1467 for (i
= 0; i
< n
; i
++) {
1468 if (predicate
&& !predicate(&addresses
[i
]))
1470 r
= ordered_set_put_in4_addr(s
, addresses
+i
);
1480 static int manager_save(Manager
*m
) {
1481 _cleanup_ordered_set_free_free_ OrderedSet
*dns
= NULL
, *ntp
= NULL
, *sip
= NULL
, *search_domains
= NULL
, *route_domains
= NULL
;
1482 const char *operstate_str
, *carrier_state_str
, *address_state_str
;
1483 LinkOperationalState operstate
= LINK_OPERSTATE_OFF
;
1484 LinkCarrierState carrier_state
= LINK_CARRIER_STATE_OFF
;
1485 LinkAddressState address_state
= LINK_ADDRESS_STATE_OFF
;
1486 _cleanup_free_
char *temp_path
= NULL
;
1487 _cleanup_strv_free_
char **p
= NULL
;
1488 _cleanup_fclose_
FILE *f
= NULL
;
1494 assert(m
->state_file
);
1496 /* We add all NTP and DNS server to a set, to filter out duplicates */
1497 dns
= ordered_set_new(&string_hash_ops
);
1501 ntp
= ordered_set_new(&string_hash_ops
);
1505 sip
= ordered_set_new(&string_hash_ops
);
1509 search_domains
= ordered_set_new(&dns_name_hash_ops
);
1510 if (!search_domains
)
1513 route_domains
= ordered_set_new(&dns_name_hash_ops
);
1517 HASHMAP_FOREACH(link
, m
->links
, i
) {
1518 if (link
->flags
& IFF_LOOPBACK
)
1521 if (link
->operstate
> operstate
)
1522 operstate
= link
->operstate
;
1524 if (link
->carrier_state
> carrier_state
)
1525 carrier_state
= link
->carrier_state
;
1527 if (link
->address_state
> address_state
)
1528 address_state
= link
->address_state
;
1533 /* First add the static configured entries */
1534 r
= ordered_set_put_in_addr_datav(dns
, link
->network
->dns
, link
->network
->n_dns
);
1538 r
= ordered_set_put_strdupv(ntp
, link
->ntp
?: link
->network
->ntp
);
1542 r
= ordered_set_put_string_set(search_domains
, link
->search_domains
?: link
->network
->search_domains
);
1546 r
= ordered_set_put_string_set(route_domains
, link
->route_domains
?: link
->network
->route_domains
);
1550 if (!link
->dhcp_lease
)
1553 /* Secondly, add the entries acquired via DHCP */
1554 if (link
->network
->dhcp_use_dns
) {
1555 const struct in_addr
*addresses
;
1557 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
1559 r
= ordered_set_put_in4_addrv(dns
, addresses
, r
, in4_addr_is_non_local
);
1562 } else if (r
< 0 && r
!= -ENODATA
)
1566 if (link
->network
->dhcp_use_ntp
) {
1567 const struct in_addr
*addresses
;
1569 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
1571 r
= ordered_set_put_in4_addrv(ntp
, addresses
, r
, in4_addr_is_non_local
);
1574 } else if (r
< 0 && r
!= -ENODATA
)
1578 if (link
->network
->dhcp_use_sip
) {
1579 const struct in_addr
*addresses
;
1581 r
= sd_dhcp_lease_get_sip(link
->dhcp_lease
, &addresses
);
1583 r
= ordered_set_put_in4_addrv(sip
, addresses
, r
, in4_addr_is_non_local
);
1586 } else if (r
< 0 && r
!= -ENODATA
)
1590 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
1591 const char *domainname
;
1592 char **domains
= NULL
;
1594 OrderedSet
*target_domains
= (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) ? search_domains
: route_domains
;
1595 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
1597 r
= ordered_set_put_strdup(target_domains
, domainname
);
1600 } else if (r
!= -ENODATA
)
1603 r
= sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &domains
);
1605 r
= ordered_set_put_strdupv(target_domains
, domains
);
1608 } else if (r
!= -ENODATA
)
1613 if (carrier_state
>= LINK_CARRIER_STATE_ENSLAVED
)
1614 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
1616 operstate_str
= link_operstate_to_string(operstate
);
1617 assert(operstate_str
);
1619 carrier_state_str
= link_carrier_state_to_string(carrier_state
);
1620 assert(carrier_state_str
);
1622 address_state_str
= link_address_state_to_string(address_state
);
1623 assert(address_state_str
);
1625 r
= fopen_temporary(m
->state_file
, &f
, &temp_path
);
1629 (void) fchmod(fileno(f
), 0644);
1632 "# This is private data. Do not parse.\n"
1634 "CARRIER_STATE=%s\n"
1635 "ADDRESS_STATE=%s\n",
1636 operstate_str
, carrier_state_str
, address_state_str
);
1638 ordered_set_print(f
, "DNS=", dns
);
1639 ordered_set_print(f
, "NTP=", ntp
);
1640 ordered_set_print(f
, "SIP=", sip
);
1641 ordered_set_print(f
, "DOMAINS=", search_domains
);
1642 ordered_set_print(f
, "ROUTE_DOMAINS=", route_domains
);
1644 r
= routing_policy_serialize_rules(m
->rules
, f
);
1648 r
= fflush_and_check(f
);
1652 if (rename(temp_path
, m
->state_file
) < 0) {
1657 if (m
->operational_state
!= operstate
) {
1658 m
->operational_state
= operstate
;
1659 if (strv_extend(&p
, "OperationalState") < 0)
1663 if (m
->carrier_state
!= carrier_state
) {
1664 m
->carrier_state
= carrier_state
;
1665 if (strv_extend(&p
, "CarrierState") < 0)
1669 if (m
->address_state
!= address_state
) {
1670 m
->address_state
= address_state
;
1671 if (strv_extend(&p
, "AddressState") < 0)
1676 r
= manager_send_changed_strv(m
, p
);
1678 log_error_errno(r
, "Could not emit changed properties: %m");
1686 (void) unlink(m
->state_file
);
1687 (void) unlink(temp_path
);
1689 return log_error_errno(r
, "Failed to save network state to %s: %m", m
->state_file
);
1692 static int manager_dirty_handler(sd_event_source
*s
, void *userdata
) {
1693 Manager
*m
= userdata
;
1702 SET_FOREACH(link
, m
->dirty_links
, i
)
1703 if (link_save(link
) >= 0)
1709 static int signal_terminate_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1710 Manager
*m
= userdata
;
1713 m
->restarting
= false;
1715 log_debug("Terminate operation initiated.");
1717 return sd_event_exit(sd_event_source_get_event(s
), 0);
1720 static int signal_restart_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1721 Manager
*m
= userdata
;
1724 m
->restarting
= true;
1726 log_debug("Restart operation initiated.");
1728 return sd_event_exit(sd_event_source_get_event(s
), 0);
1731 int manager_new(Manager
**ret
) {
1732 _cleanup_(manager_freep
) Manager
*m
= NULL
;
1735 m
= new(Manager
, 1);
1740 .speed_meter_interval_usec
= SPEED_METER_DEFAULT_TIME_INTERVAL
,
1743 m
->state_file
= strdup("/run/systemd/netif/state");
1747 r
= sd_event_default(&m
->event
);
1751 assert_se(sigprocmask_many(SIG_SETMASK
, NULL
, SIGINT
, SIGTERM
, SIGUSR2
, -1) >= 0);
1753 (void) sd_event_set_watchdog(m
->event
, true);
1754 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, signal_terminate_callback
, m
);
1755 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, signal_terminate_callback
, m
);
1756 (void) sd_event_add_signal(m
->event
, NULL
, SIGUSR2
, signal_restart_callback
, m
);
1758 r
= sd_event_add_post(m
->event
, NULL
, manager_dirty_handler
, m
);
1762 r
= manager_connect_rtnl(m
);
1766 r
= manager_connect_genl(m
);
1770 r
= manager_connect_udev(m
);
1774 r
= sd_resolve_default(&m
->resolve
);
1778 r
= sd_resolve_attach_event(m
->resolve
, m
->event
, 0);
1782 r
= setup_default_address_pool(m
);
1786 m
->duid
.type
= DUID_TYPE_EN
;
1788 (void) routing_policy_load_rules(m
->state_file
, &m
->rules_saved
);
1795 void manager_free(Manager
*m
) {
1803 free(m
->state_file
);
1805 while ((a
= hashmap_first_key(m
->dhcp6_prefixes
)))
1806 (void) dhcp6_prefix_remove(m
, a
);
1807 m
->dhcp6_prefixes
= hashmap_free(m
->dhcp6_prefixes
);
1809 while ((link
= hashmap_steal_first(m
->links
))) {
1810 if (link
->dhcp6_client
)
1811 (void) dhcp6_lease_pd_prefix_lost(link
->dhcp6_client
, link
);
1813 (void) link_stop_clients(link
, true);
1818 m
->dirty_links
= set_free_with_destructor(m
->dirty_links
, link_unref
);
1819 m
->links_requesting_uuid
= set_free_with_destructor(m
->links_requesting_uuid
, link_unref
);
1820 m
->links
= hashmap_free_with_destructor(m
->links
, link_unref
);
1822 m
->duids_requesting_uuid
= set_free(m
->duids_requesting_uuid
);
1823 m
->networks
= ordered_hashmap_free_with_destructor(m
->networks
, network_unref
);
1825 m
->netdevs
= hashmap_free_with_destructor(m
->netdevs
, netdev_unref
);
1827 while ((pool
= m
->address_pools
))
1828 address_pool_free(pool
);
1830 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1831 * So, it is necessary to set NULL after the sets are freed. */
1832 m
->rules
= set_free_with_destructor(m
->rules
, routing_policy_rule_free
);
1833 m
->rules_foreign
= set_free_with_destructor(m
->rules_foreign
, routing_policy_rule_free
);
1834 set_free_with_destructor(m
->rules_saved
, routing_policy_rule_free
);
1836 sd_netlink_unref(m
->rtnl
);
1837 sd_netlink_unref(m
->genl
);
1838 sd_resolve_unref(m
->resolve
);
1840 sd_event_source_unref(m
->speed_meter_event_source
);
1841 sd_event_unref(m
->event
);
1843 sd_device_monitor_unref(m
->device_monitor
);
1845 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
1846 sd_bus_flush_close_unref(m
->bus
);
1848 free(m
->dynamic_timezone
);
1849 free(m
->dynamic_hostname
);
1854 int manager_start(Manager
*m
) {
1861 r
= manager_start_speed_meter(m
);
1863 return log_error_errno(r
, "Failed to initialize speed meter: %m");
1865 /* The dirty handler will deal with future serialization, but the first one
1866 must be done explicitly. */
1870 HASHMAP_FOREACH(link
, m
->links
, i
)
1876 int manager_load_config(Manager
*m
) {
1879 /* update timestamp */
1880 paths_check_timestamp(NETWORK_DIRS
, &m
->network_dirs_ts_usec
, true);
1882 r
= netdev_load(m
, false);
1886 r
= network_load(m
, &m
->networks
);
1893 bool manager_should_reload(Manager
*m
) {
1894 return paths_check_timestamp(NETWORK_DIRS
, &m
->network_dirs_ts_usec
, false);
1897 int manager_rtnl_enumerate_links(Manager
*m
) {
1898 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1899 sd_netlink_message
*link
;
1905 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
1909 r
= sd_netlink_message_request_dump(req
, true);
1913 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1917 for (link
= reply
; link
; link
= sd_netlink_message_next(link
)) {
1920 m
->enumerating
= true;
1922 k
= manager_rtnl_process_link(m
->rtnl
, link
, m
);
1926 m
->enumerating
= false;
1932 int manager_rtnl_enumerate_addresses(Manager
*m
) {
1933 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1934 sd_netlink_message
*addr
;
1940 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
1944 r
= sd_netlink_message_request_dump(req
, true);
1948 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1952 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1955 m
->enumerating
= true;
1957 k
= manager_rtnl_process_address(m
->rtnl
, addr
, m
);
1961 m
->enumerating
= false;
1967 int manager_rtnl_enumerate_neighbors(Manager
*m
) {
1968 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1969 sd_netlink_message
*neigh
;
1975 r
= sd_rtnl_message_new_neigh(m
->rtnl
, &req
, RTM_GETNEIGH
, 0, AF_UNSPEC
);
1979 r
= sd_netlink_message_request_dump(req
, true);
1983 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
1987 for (neigh
= reply
; neigh
; neigh
= sd_netlink_message_next(neigh
)) {
1990 m
->enumerating
= true;
1992 k
= manager_rtnl_process_neighbor(m
->rtnl
, neigh
, m
);
1996 m
->enumerating
= false;
2002 int manager_rtnl_enumerate_routes(Manager
*m
) {
2003 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2004 sd_netlink_message
*route
;
2010 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
2014 r
= sd_netlink_message_request_dump(req
, true);
2018 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
2022 for (route
= reply
; route
; route
= sd_netlink_message_next(route
)) {
2025 m
->enumerating
= true;
2027 k
= manager_rtnl_process_route(m
->rtnl
, route
, m
);
2031 m
->enumerating
= false;
2037 int manager_rtnl_enumerate_rules(Manager
*m
) {
2038 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2039 sd_netlink_message
*rule
;
2045 r
= sd_rtnl_message_new_routing_policy_rule(m
->rtnl
, &req
, RTM_GETRULE
, 0);
2049 r
= sd_netlink_message_request_dump(req
, true);
2053 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
2055 if (r
== -EOPNOTSUPP
) {
2056 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
2063 for (rule
= reply
; rule
; rule
= sd_netlink_message_next(rule
)) {
2066 m
->enumerating
= true;
2068 k
= manager_rtnl_process_rule(m
->rtnl
, rule
, m
);
2072 m
->enumerating
= false;
2078 int manager_rtnl_enumerate_nexthop(Manager
*m
) {
2079 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
2080 sd_netlink_message
*nexthop
;
2086 r
= sd_rtnl_message_new_nexthop(m
->rtnl
, &req
, RTM_GETNEXTHOP
, 0, 0);
2090 r
= sd_netlink_message_request_dump(req
, true);
2094 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
2096 if (r
== -EOPNOTSUPP
) {
2097 log_debug("Nexthop are not supported by the kernel. Ignoring.");
2104 for (nexthop
= reply
; nexthop
; nexthop
= sd_netlink_message_next(nexthop
)) {
2107 m
->enumerating
= true;
2109 k
= manager_rtnl_process_nexthop(m
->rtnl
, nexthop
, m
);
2113 m
->enumerating
= false;
2119 int manager_address_pool_acquire(Manager
*m
, int family
, unsigned prefixlen
, union in_addr_union
*found
) {
2124 assert(prefixlen
> 0);
2127 LIST_FOREACH(address_pools
, p
, m
->address_pools
) {
2128 if (p
->family
!= family
)
2131 r
= address_pool_acquire(p
, prefixlen
, found
);
2139 Link
* manager_find_uplink(Manager
*m
, Link
*exclude
) {
2140 _cleanup_free_
struct local_address
*gateways
= NULL
;
2145 /* Looks for a suitable "uplink", via black magic: an
2146 * interface that is up and where the default route with the
2147 * highest priority points to. */
2149 n
= local_gateways(m
->rtnl
, 0, AF_UNSPEC
, &gateways
);
2151 log_warning_errno(n
, "Failed to determine list of default gateways: %m");
2155 for (i
= 0; i
< n
; i
++) {
2158 link
= hashmap_get(m
->links
, INT_TO_PTR(gateways
[i
].ifindex
));
2160 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
2164 if (link
== exclude
)
2167 if (link
->operstate
< LINK_OPERSTATE_ROUTABLE
)
2176 void manager_dirty(Manager
*manager
) {
2179 /* the serialized state in /run is no longer up-to-date */
2180 manager
->dirty
= true;
2183 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2184 Manager
*manager
= userdata
;
2185 const sd_bus_error
*e
;
2190 e
= sd_bus_message_get_error(m
);
2192 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
2197 int manager_set_hostname(Manager
*m
, const char *hostname
) {
2200 log_debug("Setting transient hostname: '%s'", strna(hostname
));
2202 if (free_and_strdup(&m
->dynamic_hostname
, hostname
) < 0)
2205 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
2206 log_debug("Not connected to system bus, setting hostname later.");
2210 r
= sd_bus_call_method_async(
2213 "org.freedesktop.hostname1",
2214 "/org/freedesktop/hostname1",
2215 "org.freedesktop.hostname1",
2217 set_hostname_handler
,
2224 return log_error_errno(r
, "Could not set transient hostname: %m");
2229 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
2230 Manager
*manager
= userdata
;
2231 const sd_bus_error
*e
;
2236 e
= sd_bus_message_get_error(m
);
2238 log_warning_errno(sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
2243 int manager_set_timezone(Manager
*m
, const char *tz
) {
2249 log_debug("Setting system timezone: '%s'", tz
);
2250 if (free_and_strdup(&m
->dynamic_timezone
, tz
) < 0)
2253 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
2254 log_debug("Not connected to system bus, setting timezone later.");
2258 r
= sd_bus_call_method_async(
2261 "org.freedesktop.timedate1",
2262 "/org/freedesktop/timedate1",
2263 "org.freedesktop.timedate1",
2265 set_timezone_handler
,
2271 return log_error_errno(r
, "Could not set timezone: %m");
2276 int manager_request_product_uuid(Manager
*m
, Link
*link
) {
2281 if (m
->has_product_uuid
)
2284 log_debug("Requesting product UUID");
2289 assert_se(duid
= link_get_duid(link
));
2291 r
= set_ensure_allocated(&m
->links_requesting_uuid
, NULL
);
2295 r
= set_ensure_allocated(&m
->duids_requesting_uuid
, NULL
);
2299 r
= set_put(m
->links_requesting_uuid
, link
);
2303 r
= set_put(m
->duids_requesting_uuid
, duid
);
2310 if (!m
->bus
|| sd_bus_is_ready(m
->bus
) <= 0) {
2311 log_debug("Not connected to system bus, requesting product UUID later.");
2315 r
= sd_bus_call_method_async(
2318 "org.freedesktop.hostname1",
2319 "/org/freedesktop/hostname1",
2320 "org.freedesktop.hostname1",
2322 get_product_uuid_handler
,
2327 return log_warning_errno(r
, "Failed to get product UUID: %m");