1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/filter.h>
4 #include <linux/nl80211.h>
5 #include <sys/socket.h>
9 #include "sd-netlink.h"
10 #include "sd-resolve.h"
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-log-control-api.h"
16 #include "bus-object.h"
18 #include "capability-util.h"
19 #include "common-signal.h"
20 #include "daemon-util.h"
21 #include "device-private.h"
22 #include "device-util.h"
24 #include "errno-util.h"
26 #include "firewall-util.h"
27 #include "initrd-util.h"
28 #include "mount-util.h"
29 #include "netlink-util.h"
30 #include "networkd-address.h"
31 #include "networkd-address-label.h"
32 #include "networkd-address-pool.h"
33 #include "networkd-link.h"
34 #include "networkd-manager.h"
35 #include "networkd-manager-bus.h"
36 #include "networkd-manager-varlink.h"
37 #include "networkd-neighbor.h"
38 #include "networkd-nexthop.h"
39 #include "networkd-queue.h"
40 #include "networkd-route.h"
41 #include "networkd-routing-policy-rule.h"
42 #include "networkd-serialize.h"
43 #include "networkd-speed-meter.h"
44 #include "networkd-state-file.h"
45 #include "networkd-wifi.h"
46 #include "networkd-wiphy.h"
47 #include "ordered-set.h"
50 #include "stat-util.h"
51 #include "string-util.h"
55 #include "udev-util.h"
57 /* use 128 MB for receive socket kernel queue. */
58 #define RCVBUF_SIZE (128*1024*1024)
60 static int match_prepare_for_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
61 Manager
*m
= ASSERT_PTR(userdata
);
67 r
= sd_bus_message_read(message
, "b", &b
);
69 bus_log_parse_error(r
);
76 log_debug("Coming back from suspend, reconfiguring all connections...");
78 HASHMAP_FOREACH(link
, m
->links_by_index
)
79 (void) link_reconfigure(link
, LINK_RECONFIGURE_UNCONDITIONALLY
);
84 static int on_connected(sd_bus_message
*message
, void *userdata
, sd_bus_error
*ret_error
) {
85 Manager
*m
= ASSERT_PTR(userdata
);
89 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
90 if (m
->dynamic_hostname
)
91 (void) manager_set_hostname(m
, m
->dynamic_hostname
);
92 if (m
->dynamic_timezone
)
93 (void) manager_set_timezone(m
, m
->dynamic_timezone
);
94 if (m
->product_uuid_requested
)
95 (void) manager_request_product_uuid(m
);
100 static int manager_connect_bus(Manager
*m
) {
106 r
= bus_open_system_watch_bind_with_description(&m
->bus
, "bus-api-network");
108 return log_error_errno(r
, "Failed to connect to bus: %m");
110 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
114 r
= bus_log_control_api_register(m
->bus
);
118 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.network1", 0, NULL
, NULL
);
120 return log_error_errno(r
, "Failed to request name: %m");
122 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
124 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
126 r
= sd_bus_match_signal_async(
129 "org.freedesktop.DBus.Local",
131 "org.freedesktop.DBus.Local",
133 on_connected
, NULL
, m
);
135 return log_error_errno(r
, "Failed to request match on Connected signal: %m");
137 r
= bus_match_signal_async(
142 match_prepare_for_sleep
, NULL
, m
);
144 log_warning_errno(r
, "Failed to request match for PrepareForSleep, ignoring: %m");
149 static int manager_process_uevent(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
150 Manager
*m
= ASSERT_PTR(userdata
);
151 sd_device_action_t action
;
156 r
= sd_device_get_action(device
, &action
);
158 return log_device_warning_errno(device
, r
, "Failed to get udev action, ignoring: %m");
160 if (device_in_subsystem(device
, "net") > 0)
161 r
= manager_udev_process_link(m
, device
, action
);
162 else if (device_in_subsystem(device
, "ieee80211") > 0)
163 r
= manager_udev_process_wiphy(m
, device
, action
);
164 else if (device_in_subsystem(device
, "rfkill") > 0)
165 r
= manager_udev_process_rfkill(m
, device
, action
);
167 log_device_warning_errno(device
, r
, "Failed to process \"%s\" uevent, ignoring: %m",
168 device_action_to_string(action
));
173 static int manager_connect_udev(Manager
*m
) {
176 /* udev does not initialize devices inside containers, so we rely on them being already
177 * initialized before entering the container. */
178 if (!udev_available())
181 r
= sd_device_monitor_new(&m
->device_monitor
);
183 return log_error_errno(r
, "Failed to initialize device monitor: %m");
185 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "net", NULL
);
187 return log_error_errno(r
, "Could not add device monitor filter for net subsystem: %m");
189 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "ieee80211", NULL
);
191 return log_error_errno(r
, "Could not add device monitor filter for ieee80211 subsystem: %m");
193 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "rfkill", NULL
);
195 return log_error_errno(r
, "Could not add device monitor filter for rfkill subsystem: %m");
197 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
199 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
201 r
= sd_device_monitor_start(m
->device_monitor
, manager_process_uevent
, m
);
203 return log_error_errno(r
, "Failed to start device monitor: %m");
208 static int manager_listen_fds(Manager
*m
, int *ret_rtnl_fd
) {
209 _cleanup_strv_free_
char **names
= NULL
;
210 int n
, rtnl_fd
= -EBADF
;
215 n
= sd_listen_fds_with_names(/* unset_environment = */ true, &names
);
219 for (int i
= 0; i
< n
; i
++) {
220 int fd
= i
+ SD_LISTEN_FDS_START
;
222 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
224 log_debug("Received multiple netlink socket, ignoring.");
232 if (manager_set_serialization_fd(m
, fd
, names
[i
]) >= 0)
235 if (manager_add_tuntap_fd(m
, fd
, names
[i
]) >= 0)
242 close_and_notify_warn(fd
, names
[i
]);
245 *ret_rtnl_fd
= rtnl_fd
;
249 static int manager_connect_genl(Manager
*m
) {
254 r
= sd_genl_socket_open(&m
->genl
);
258 r
= sd_netlink_increase_rxbuf(m
->genl
, RCVBUF_SIZE
);
260 log_warning_errno(r
, "Failed to increase receive buffer size for general netlink socket, ignoring: %m");
262 r
= sd_netlink_attach_event(m
->genl
, m
->event
, 0);
266 /* If the kernel is built without CONFIG_WIRELESS, the below will fail with -EOPNOTSUPP. */
267 r
= genl_add_match(m
->genl
, NULL
, NL80211_GENL_NAME
, NL80211_MULTICAST_GROUP_CONFIG
, 0,
268 &manager_genl_process_nl80211_config
, NULL
, m
, "network-genl_process_nl80211_config");
269 if (r
< 0 && r
!= -EOPNOTSUPP
)
272 r
= genl_add_match(m
->genl
, NULL
, NL80211_GENL_NAME
, NL80211_MULTICAST_GROUP_MLME
, 0,
273 &manager_genl_process_nl80211_mlme
, NULL
, m
, "network-genl_process_nl80211_mlme");
274 if (r
< 0 && r
!= -EOPNOTSUPP
)
280 static int manager_setup_rtnl_filter(Manager
*manager
) {
281 struct sock_filter filter
[] = {
282 /* Check the packet length. */
283 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_LEN
, 0), /* A <- packet length */
284 BPF_JUMP(BPF_JMP
+ BPF_JGE
+ BPF_K
, sizeof(struct nlmsghdr
), 1, 0), /* A (packet length) >= sizeof(struct nlmsghdr) ? */
285 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* reject */
286 /* Always accept multipart message. */
287 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(struct nlmsghdr
, nlmsg_flags
)), /* A <- message flags */
288 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, htobe16(NLM_F_MULTI
), 0, 1), /* message flags has NLM_F_MULTI ? */
289 BPF_STMT(BPF_RET
+ BPF_K
, UINT32_MAX
), /* accept */
290 /* Accept all message types except for RTM_NEWNEIGH or RTM_DELNEIGH. */
291 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, offsetof(struct nlmsghdr
, nlmsg_type
)), /* A <- message type */
292 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, htobe16(RTM_NEWNEIGH
), 2, 0), /* message type == RTM_NEWNEIGH ? */
293 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, htobe16(RTM_DELNEIGH
), 1, 0), /* message type == RTM_DELNEIGH ? */
294 BPF_STMT(BPF_RET
+ BPF_K
, UINT32_MAX
), /* accept */
295 /* Check the packet length. */
296 BPF_STMT(BPF_LD
+ BPF_W
+ BPF_LEN
, 0), /* A <- packet length */
297 BPF_JUMP(BPF_JMP
+ BPF_JGE
+ BPF_K
, sizeof(struct nlmsghdr
) + sizeof(struct ndmsg
), 1, 0),
298 /* packet length >= sizeof(struct nlmsghdr) + sizeof(struct ndmsg) ? */
299 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* reject */
300 /* Reject the message when the neighbor state does not have NUD_PERMANENT flag. */
301 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, sizeof(struct nlmsghdr
) + offsetof(struct ndmsg
, ndm_state
)),
302 /* A <- neighbor state */
303 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, htobe16(NUD_PERMANENT
), 1, 0), /* neighbor state has NUD_PERMANENT ? */
304 BPF_STMT(BPF_RET
+ BPF_K
, 0), /* reject */
305 BPF_STMT(BPF_RET
+ BPF_K
, UINT32_MAX
), /* accept */
309 assert(manager
->rtnl
);
311 return sd_netlink_attach_filter(manager
->rtnl
, ELEMENTSOF(filter
), filter
);
314 static int manager_connect_rtnl(Manager
*m
, int fd
) {
315 _unused_ _cleanup_close_
int fd_close
= fd
;
320 /* This takes input fd. */
323 r
= sd_netlink_open(&m
->rtnl
);
325 r
= sd_netlink_open_fd(&m
->rtnl
, fd
);
330 /* Bump receiver buffer, but only if we are not called via socket activation, as in that
331 * case systemd sets the receive buffer size for us, and the value in the .socket unit
332 * should take full effect. */
334 r
= sd_netlink_increase_rxbuf(m
->rtnl
, RCVBUF_SIZE
);
336 log_warning_errno(r
, "Failed to increase receive buffer size for rtnl socket, ignoring: %m");
339 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, 0);
343 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
347 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, &manager_rtnl_process_link
, NULL
, m
, "network-rtnl_process_link");
351 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWQDISC
, &manager_rtnl_process_qdisc
, NULL
, m
, "network-rtnl_process_qdisc");
355 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELQDISC
, &manager_rtnl_process_qdisc
, NULL
, m
, "network-rtnl_process_qdisc");
359 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWTCLASS
, &manager_rtnl_process_tclass
, NULL
, m
, "network-rtnl_process_tclass");
363 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELTCLASS
, &manager_rtnl_process_tclass
, NULL
, m
, "network-rtnl_process_tclass");
367 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
371 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELADDR
, &manager_rtnl_process_address
, NULL
, m
, "network-rtnl_process_address");
375 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWNEIGH
, &manager_rtnl_process_neighbor
, NULL
, m
, "network-rtnl_process_neighbor");
379 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELNEIGH
, &manager_rtnl_process_neighbor
, NULL
, m
, "network-rtnl_process_neighbor");
383 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
387 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELROUTE
, &manager_rtnl_process_route
, NULL
, m
, "network-rtnl_process_route");
391 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
395 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELRULE
, &manager_rtnl_process_rule
, NULL
, m
, "network-rtnl_process_rule");
399 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_NEWNEXTHOP
, &manager_rtnl_process_nexthop
, NULL
, m
, "network-rtnl_process_nexthop");
403 r
= netlink_add_match(m
->rtnl
, NULL
, RTM_DELNEXTHOP
, &manager_rtnl_process_nexthop
, NULL
, m
, "network-rtnl_process_nexthop");
407 return manager_setup_rtnl_filter(m
);
410 static int manager_post_handler(sd_event_source
*s
, void *userdata
) {
411 Manager
*manager
= ASSERT_PTR(userdata
);
413 /* To release dynamic leases, we need to process queued remove requests before stopping networkd.
414 * This is especially important when KeepConfiguration=no. See issue #34837. */
415 (void) manager_process_remove_requests(manager
);
417 switch (manager
->state
) {
418 case MANAGER_RUNNING
:
419 (void) manager_process_requests(manager
);
420 (void) manager_clean_all(manager
);
423 case MANAGER_TERMINATING
:
424 case MANAGER_RESTARTING
:
425 if (!ordered_set_isempty(manager
->remove_request_queue
))
426 return 0; /* There are some unissued remove requests. */
428 if (netlink_get_reply_callback_count(manager
->rtnl
) > 0 ||
429 netlink_get_reply_callback_count(manager
->genl
) > 0 ||
430 fw_ctx_get_reply_callback_count(manager
->fw_ctx
) > 0)
431 return 0; /* There are some message calls waiting for their replies. */
433 (void) manager_serialize(manager
);
434 manager
->state
= MANAGER_STOPPED
;
435 return sd_event_exit(sd_event_source_get_event(s
), 0);
438 assert_not_reached();
444 static int manager_stop(Manager
*manager
, ManagerState state
) {
446 assert(IN_SET(state
, MANAGER_TERMINATING
, MANAGER_RESTARTING
));
448 if (manager
->state
!= MANAGER_RUNNING
) {
449 log_debug("Already terminating or restarting systemd-networkd, refusing further operation request.");
454 case MANAGER_TERMINATING
:
455 log_debug("Terminate operation initiated.");
457 case MANAGER_RESTARTING
:
458 log_debug("Restart operation initiated.");
461 assert_not_reached();
464 manager
->state
= state
;
467 HASHMAP_FOREACH(link
, manager
->links_by_index
)
468 (void) link_stop_engines(link
, /* may_keep_dynamic = */ true);
473 static int signal_terminate_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
474 return manager_stop(userdata
, MANAGER_TERMINATING
);
477 static int signal_restart_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
478 return manager_stop(userdata
, MANAGER_RESTARTING
);
481 static int signal_reload_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
482 Manager
*m
= ASSERT_PTR(userdata
);
484 (void) manager_reload(m
, /* message = */ NULL
);
489 static int manager_set_keep_configuration(Manager
*m
) {
495 log_debug("Running in initrd, keep dynamically assigned configurations on stopping networkd by default.");
496 m
->keep_configuration
= KEEP_CONFIGURATION_DYNAMIC_ON_STOP
;
500 r
= path_is_network_fs_harder("/");
502 log_warning_errno(r
, "Failed to detect if root is network filesystem, assuming not: %m");
506 m
->keep_configuration
= _KEEP_CONFIGURATION_INVALID
;
510 log_debug("Running on network filesystem, enabling KeepConfiguration= by default.");
511 m
->keep_configuration
= KEEP_CONFIGURATION_YES
;
515 int manager_setup(Manager
*m
) {
516 _cleanup_close_
int rtnl_fd
= -EBADF
;
521 r
= sd_event_default(&m
->event
);
525 (void) sd_event_set_watchdog(m
->event
, true);
526 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
| SD_EVENT_SIGNAL_PROCMASK
, signal_terminate_callback
, m
);
527 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
| SD_EVENT_SIGNAL_PROCMASK
, signal_terminate_callback
, m
);
528 (void) sd_event_add_signal(m
->event
, NULL
, SIGUSR2
| SD_EVENT_SIGNAL_PROCMASK
, signal_restart_callback
, m
);
529 (void) sd_event_add_signal(m
->event
, NULL
, SIGHUP
| SD_EVENT_SIGNAL_PROCMASK
, signal_reload_callback
, m
);
530 (void) sd_event_add_signal(m
->event
, NULL
, (SIGRTMIN
+18) | SD_EVENT_SIGNAL_PROCMASK
, sigrtmin18_handler
, NULL
);
532 r
= sd_event_add_memory_pressure(m
->event
, NULL
, NULL
, NULL
);
534 log_debug_errno(r
, "Failed allocate memory pressure event source, ignoring: %m");
536 r
= sd_event_add_post(m
->event
, NULL
, manager_post_handler
, m
);
540 r
= manager_listen_fds(m
, &rtnl_fd
);
544 r
= manager_connect_rtnl(m
, TAKE_FD(rtnl_fd
));
548 r
= manager_connect_genl(m
);
555 r
= manager_connect_varlink(m
);
559 r
= manager_connect_bus(m
);
563 r
= manager_connect_udev(m
);
567 r
= sd_resolve_default(&m
->resolve
);
571 r
= sd_resolve_attach_event(m
->resolve
, m
->event
, 0);
575 r
= address_pool_setup_default(m
);
579 r
= manager_set_keep_configuration(m
);
583 m
->state_file
= strdup("/run/systemd/netif/state");
590 static int persistent_storage_open(void) {
591 _cleanup_close_
int fd
= -EBADF
;
594 r
= getenv_bool("SYSTEMD_NETWORK_PERSISTENT_STORAGE_READY");
595 if (r
< 0 && r
!= -ENXIO
)
596 return log_debug_errno(r
, "Failed to parse $SYSTEMD_NETWORK_PERSISTENT_STORAGE_READY environment variable, ignoring: %m");
600 fd
= open("/var/lib/systemd/network/", O_CLOEXEC
| O_DIRECTORY
);
602 return log_debug_errno(errno
, "Failed to open %s, ignoring: %m", "/var/lib/systemd/network/");
604 r
= fd_is_read_only_fs(fd
);
606 return log_debug_errno(r
, "Failed to check if /var/lib/systemd/network/ is writable: %m");
608 return log_debug_errno(SYNTHETIC_ERRNO(EROFS
), "The directory /var/lib/systemd/network/ is on read-only filesystem.");
613 int manager_new(Manager
**ret
, bool test_mode
) {
614 _cleanup_(manager_freep
) Manager
*m
= NULL
;
621 .keep_configuration
= _KEEP_CONFIGURATION_INVALID
,
622 .ipv6_privacy_extensions
= IPV6_PRIVACY_EXTENSIONS_NO
,
623 .test_mode
= test_mode
,
624 .speed_meter_interval_usec
= SPEED_METER_DEFAULT_TIME_INTERVAL
,
625 .online_state
= _LINK_ONLINE_STATE_INVALID
,
626 .manage_foreign_routes
= true,
627 .manage_foreign_rules
= true,
628 .manage_foreign_nexthops
= true,
629 .ethtool_fd
= -EBADF
,
630 .persistent_storage_fd
= persistent_storage_open(),
631 .dhcp_use_domains
= _USE_DOMAINS_INVALID
,
632 .dhcp6_use_domains
= _USE_DOMAINS_INVALID
,
633 .ndisc_use_domains
= _USE_DOMAINS_INVALID
,
634 .dhcp_client_identifier
= DHCP_CLIENT_ID_DUID
,
635 .dhcp_duid
.type
= DUID_TYPE_EN
,
636 .dhcp6_duid
.type
= DUID_TYPE_EN
,
637 .duid_product_uuid
.type
= DUID_TYPE_UUID
,
638 .dhcp_server_persist_leases
= DHCP_SERVER_PERSIST_LEASES_YES
,
639 .serialization_fd
= -EBADF
,
640 .ip_forwarding
= { -1, -1, },
650 Manager
* manager_free(Manager
*m
) {
654 manager_remove_sysctl_monitor(m
);
658 m
->request_queue
= ordered_set_free(m
->request_queue
);
659 m
->remove_request_queue
= ordered_set_free(m
->remove_request_queue
);
661 m
->new_wlan_ifindices
= set_free(m
->new_wlan_ifindices
);
663 m
->dirty_links
= set_free(m
->dirty_links
);
664 m
->links_by_name
= hashmap_free(m
->links_by_name
);
665 m
->links_by_hw_addr
= hashmap_free(m
->links_by_hw_addr
);
666 m
->links_by_dhcp_pd_subnet_prefix
= hashmap_free(m
->links_by_dhcp_pd_subnet_prefix
);
667 m
->links_by_index
= hashmap_free(m
->links_by_index
);
669 m
->dhcp_pd_subnet_ids
= set_free(m
->dhcp_pd_subnet_ids
);
670 m
->networks
= ordered_hashmap_free(m
->networks
);
672 /* The same object may be registered with multiple names, and netdev_detach() may drop multiple entries. */
673 for (NetDev
*n
; (n
= hashmap_first(m
->netdevs
)); )
675 m
->netdevs
= hashmap_free(m
->netdevs
);
677 m
->tuntap_fds_by_name
= hashmap_free(m
->tuntap_fds_by_name
);
679 m
->wiphy_by_name
= hashmap_free(m
->wiphy_by_name
);
680 m
->wiphy_by_index
= hashmap_free(m
->wiphy_by_index
);
682 ordered_set_free(m
->address_pools
);
684 hashmap_free(m
->route_table_names_by_number
);
685 hashmap_free(m
->route_table_numbers_by_name
);
689 sd_netlink_unref(m
->rtnl
);
690 sd_netlink_unref(m
->genl
);
691 sd_resolve_unref(m
->resolve
);
693 m
->routes
= set_free(m
->routes
);
695 m
->nexthops_by_id
= hashmap_free(m
->nexthops_by_id
);
696 m
->nexthop_ids
= set_free(m
->nexthop_ids
);
698 m
->address_labels_by_section
= hashmap_free(m
->address_labels_by_section
);
700 sd_event_source_unref(m
->speed_meter_event_source
);
701 sd_event_unref(m
->event
);
703 sd_device_monitor_unref(m
->device_monitor
);
705 manager_varlink_done(m
);
706 hashmap_free(m
->polkit_registry
);
707 sd_bus_flush_close_unref(m
->bus
);
709 free(m
->dynamic_timezone
);
710 free(m
->dynamic_hostname
);
712 safe_close(m
->ethtool_fd
);
713 safe_close(m
->persistent_storage_fd
);
715 m
->fw_ctx
= fw_ctx_free(m
->fw_ctx
);
717 m
->serialization_fd
= safe_close(m
->serialization_fd
);
722 int manager_start(Manager
*m
) {
728 log_debug("Starting...");
730 (void) manager_install_sysctl_monitor(m
);
732 /* Loading BPF programs requires CAP_SYS_ADMIN and CAP_BPF.
733 * Drop the capabilities here, regardless if the load succeeds or not. */
734 r
= drop_capability(CAP_SYS_ADMIN
);
736 log_warning_errno(r
, "Failed to drop CAP_SYS_ADMIN, ignoring: %m.");
738 r
= drop_capability(CAP_BPF
);
740 log_warning_errno(r
, "Failed to drop CAP_BPF, ignoring: %m.");
742 manager_set_sysctl(m
);
744 r
= manager_request_static_address_labels(m
);
748 r
= manager_start_speed_meter(m
);
750 return log_error_errno(r
, "Failed to initialize speed meter: %m");
752 HASHMAP_FOREACH(link
, m
->links_by_index
) {
753 if (link
->state
!= LINK_STATE_PENDING
)
756 r
= link_check_initialized(link
);
758 log_link_warning_errno(link
, r
, "Failed to check if link is initialized: %m");
759 link_enter_failed(link
);
763 /* The dirty handler will deal with future serialization, but the first one
764 must be done explicitly. */
768 log_warning_errno(r
, "Failed to update state file %s, ignoring: %m", m
->state_file
);
770 HASHMAP_FOREACH(link
, m
->links_by_index
) {
771 r
= link_save_and_clean(link
);
773 log_link_warning_errno(link
, r
, "Failed to update link state file %s, ignoring: %m", link
->state_file
);
776 log_debug("Started.");
780 int manager_load_config(Manager
*m
) {
783 log_debug("Loading...");
787 return log_debug_errno(r
, "Failed to load .netdev files: %m");
789 manager_clear_unmanaged_tuntap_fds(m
);
791 r
= network_load(m
, &m
->networks
);
793 return log_debug_errno(r
, "Failed to load .network files: %m");
795 r
= manager_build_dhcp_pd_subnet_ids(m
);
797 return log_debug_errno(r
, "Failed to build DHCP-PD subnet ID map: %m");
799 r
= manager_build_nexthop_ids(m
);
801 return log_debug_errno(r
, "Failed to build nexthop ID map: %m");
803 log_debug("Loaded.");
807 int manager_enumerate_internal(
810 sd_netlink_message
*req
,
811 int (*process
)(sd_netlink
*, sd_netlink_message
*, Manager
*)) {
813 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*reply
= NULL
;
821 r
= sd_netlink_message_set_request_dump(req
, true);
825 r
= sd_netlink_call(nl
, req
, 0, &reply
);
829 m
->enumerating
= true;
830 for (sd_netlink_message
*reply_one
= reply
; reply_one
; reply_one
= sd_netlink_message_next(reply_one
))
831 RET_GATHER(r
, process(nl
, reply_one
, m
));
832 m
->enumerating
= false;
837 static int manager_enumerate_links(Manager
*m
) {
838 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
844 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
848 r
= manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_link
);
852 req
= sd_netlink_message_unref(req
);
854 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
858 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
862 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_link
);
865 static int manager_enumerate_qdisc(Manager
*m
) {
866 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
872 r
= sd_rtnl_message_new_traffic_control(m
->rtnl
, &req
, RTM_GETQDISC
, 0, 0, 0);
876 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_qdisc
);
879 static int manager_enumerate_tclass(Manager
*m
) {
886 /* TC class can be enumerated only per link. See tc_dump_tclass() in net/sched/sched_api.c. */
888 HASHMAP_FOREACH(link
, m
->links_by_index
)
889 RET_GATHER(r
, link_enumerate_tclass(link
, 0));
894 static int manager_enumerate_addresses(Manager
*m
) {
895 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
901 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, 0);
905 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_address
);
908 static int manager_enumerate_neighbors(Manager
*m
) {
909 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
915 r
= sd_rtnl_message_new_neigh(m
->rtnl
, &req
, RTM_GETNEIGH
, 0, AF_UNSPEC
);
919 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_neighbor
);
922 static int manager_enumerate_routes(Manager
*m
) {
923 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
929 if (!m
->manage_foreign_routes
)
932 r
= sd_rtnl_message_new_route(m
->rtnl
, &req
, RTM_GETROUTE
, 0, 0);
936 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_route
);
939 static int manager_enumerate_rules(Manager
*m
) {
940 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
946 if (!m
->manage_foreign_rules
)
949 r
= sd_rtnl_message_new_routing_policy_rule(m
->rtnl
, &req
, RTM_GETRULE
, 0);
953 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_rule
);
956 static int manager_enumerate_nexthop(Manager
*m
) {
957 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
963 if (!m
->manage_foreign_nexthops
)
966 r
= sd_rtnl_message_new_nexthop(m
->rtnl
, &req
, RTM_GETNEXTHOP
, 0, 0);
970 return manager_enumerate_internal(m
, m
->rtnl
, req
, manager_rtnl_process_nexthop
);
973 static int manager_enumerate_nl80211_wiphy(Manager
*m
) {
974 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
980 r
= sd_genl_message_new(m
->genl
, NL80211_GENL_NAME
, NL80211_CMD_GET_WIPHY
, &req
);
984 return manager_enumerate_internal(m
, m
->genl
, req
, manager_genl_process_nl80211_wiphy
);
987 static int manager_enumerate_nl80211_config(Manager
*m
) {
988 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
994 r
= sd_genl_message_new(m
->genl
, NL80211_GENL_NAME
, NL80211_CMD_GET_INTERFACE
, &req
);
998 return manager_enumerate_internal(m
, m
->genl
, req
, manager_genl_process_nl80211_config
);
1001 static int manager_enumerate_nl80211_mlme(Manager
*m
) {
1008 HASHMAP_FOREACH(link
, m
->links_by_index
) {
1009 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1011 if (link
->wlan_iftype
!= NL80211_IFTYPE_STATION
)
1014 r
= sd_genl_message_new(m
->genl
, NL80211_GENL_NAME
, NL80211_CMD_GET_STATION
, &req
);
1018 r
= sd_netlink_message_append_u32(req
, NL80211_ATTR_IFINDEX
, link
->ifindex
);
1022 r
= manager_enumerate_internal(m
, m
->genl
, req
, manager_genl_process_nl80211_mlme
);
1030 int manager_enumerate(Manager
*m
) {
1033 log_debug("Enumerating...");
1035 r
= manager_enumerate_links(m
);
1037 return log_error_errno(r
, "Could not enumerate links: %m");
1039 /* If the kernel is built without CONFIG_NET_SCHED, the below will fail with -EOPNOTSUPP. */
1040 r
= manager_enumerate_qdisc(m
);
1041 if (r
== -EOPNOTSUPP
)
1042 log_debug_errno(r
, "Could not enumerate QDiscs, ignoring: %m");
1044 return log_error_errno(r
, "Could not enumerate QDisc: %m");
1046 /* If the kernel is built without CONFIG_NET_CLS, the below will fail with -EOPNOTSUPP. */
1047 r
= manager_enumerate_tclass(m
);
1048 if (r
== -EOPNOTSUPP
)
1049 log_debug_errno(r
, "Could not enumerate TClasses, ignoring: %m");
1051 return log_error_errno(r
, "Could not enumerate TClass: %m");
1053 r
= manager_enumerate_addresses(m
);
1055 return log_error_errno(r
, "Could not enumerate addresses: %m");
1057 r
= manager_enumerate_neighbors(m
);
1059 return log_error_errno(r
, "Could not enumerate neighbors: %m");
1061 r
= manager_enumerate_nexthop(m
);
1063 return log_error_errno(r
, "Could not enumerate nexthops: %m");
1065 r
= manager_enumerate_routes(m
);
1067 return log_error_errno(r
, "Could not enumerate routes: %m");
1069 /* If the kernel is built without CONFIG_FIB_RULES, the below will fail with -EOPNOTSUPP. */
1070 r
= manager_enumerate_rules(m
);
1071 if (r
== -EOPNOTSUPP
)
1072 log_debug_errno(r
, "Could not enumerate routing policy rules, ignoring: %m");
1074 return log_error_errno(r
, "Could not enumerate routing policy rules: %m");
1076 /* If the kernel is built without CONFIG_WIRELESS, the below will fail with -EOPNOTSUPP. */
1077 r
= manager_enumerate_nl80211_wiphy(m
);
1078 if (r
== -EOPNOTSUPP
)
1079 log_debug_errno(r
, "Could not enumerate wireless LAN phy, ignoring: %m");
1081 return log_error_errno(r
, "Could not enumerate wireless LAN phy: %m");
1083 r
= manager_enumerate_nl80211_config(m
);
1084 if (r
== -EOPNOTSUPP
)
1085 log_debug_errno(r
, "Could not enumerate wireless LAN interfaces, ignoring: %m");
1087 return log_error_errno(r
, "Could not enumerate wireless LAN interfaces: %m");
1089 r
= manager_enumerate_nl80211_mlme(m
);
1090 if (r
== -EOPNOTSUPP
)
1091 log_debug_errno(r
, "Could not enumerate wireless LAN stations, ignoring: %m");
1093 return log_error_errno(r
, "Could not enumerate wireless LAN stations: %m");
1095 log_debug("Enumeration completed.");
1099 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1100 const sd_bus_error
*e
;
1105 e
= sd_bus_message_get_error(m
);
1107 r
= sd_bus_error_get_errno(e
);
1108 log_warning_errno(r
, "Could not set hostname: %s", bus_error_message(e
, r
));
1114 int manager_set_hostname(Manager
*m
, const char *hostname
) {
1117 log_debug("Setting transient hostname: '%s'", strna(hostname
));
1119 r
= free_and_strdup_warn(&m
->dynamic_hostname
, hostname
);
1123 if (sd_bus_is_ready(m
->bus
) <= 0) {
1124 log_debug("Not connected to system bus, setting system hostname later.");
1128 r
= bus_call_method_async(
1133 set_hostname_handler
,
1139 return log_error_errno(r
, "Could not set transient hostname: %m");
1144 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1145 const sd_bus_error
*e
;
1150 e
= sd_bus_message_get_error(m
);
1152 r
= sd_bus_error_get_errno(e
);
1153 log_warning_errno(r
, "Could not set timezone: %s", bus_error_message(e
, r
));
1159 int manager_set_timezone(Manager
*m
, const char *tz
) {
1165 log_debug("Setting system timezone: '%s'", tz
);
1166 r
= free_and_strdup_warn(&m
->dynamic_timezone
, tz
);
1170 if (sd_bus_is_ready(m
->bus
) <= 0) {
1171 log_debug("Not connected to system bus, setting system timezone later.");
1175 r
= bus_call_method_async(
1180 set_timezone_handler
,
1186 return log_error_errno(r
, "Could not set timezone: %m");
1191 int manager_reload(Manager
*m
, sd_bus_message
*message
) {
1197 log_debug("Reloading...");
1198 (void) notify_reloading();
1200 r
= netdev_reload(m
);
1202 log_debug_errno(r
, "Failed to reload .netdev files: %m");
1206 r
= network_reload(m
);
1208 log_debug_errno(r
, "Failed to reload .network files: %m");
1212 HASHMAP_FOREACH(link
, m
->links_by_index
)
1213 (void) link_reconfigure_full(link
, /* flags = */ 0, message
,
1214 /* counter = */ message
? &m
->reloading
: NULL
);
1216 log_debug("Reloaded.");
1219 (void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE
);