]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f579559b | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
091a364c | 4 | #include <sys/socket.h> |
9aa5d8ba | 5 | #include <unistd.h> |
bbf7c048 | 6 | #include <linux/if.h> |
bce67bbe | 7 | #include <linux/fib_rules.h> |
c16c7808 | 8 | #include <linux/nexthop.h> |
96f5f9ef | 9 | #include <linux/nl80211.h> |
3bef724f | 10 | |
07630cea | 11 | #include "sd-netlink.h" |
fc2f9534 | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
f63e09ef | 14 | #include "bus-error.h" |
b607fd3f | 15 | #include "bus-locator.h" |
ac9f55ed | 16 | #include "bus-log-control-api.h" |
269e4d2d | 17 | #include "bus-polkit.h" |
a97dcc12 | 18 | #include "bus-util.h" |
07630cea | 19 | #include "conf-parser.h" |
28db6fbf | 20 | #include "constants.h" |
af7a86b8 | 21 | #include "daemon-util.h" |
1fd3ac1e YW |
22 | #include "device-private.h" |
23 | #include "device-util.h" | |
482d1aeb | 24 | #include "dns-domain.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
0d39fa9c | 26 | #include "fileio.h" |
761cf19d | 27 | #include "firewall-util.h" |
af664001 | 28 | #include "fs-util.h" |
baa6a42d | 29 | #include "initrd-util.h" |
4f5f911e | 30 | #include "local-addresses.h" |
07630cea | 31 | #include "netlink-util.h" |
dc0d4078 | 32 | #include "network-internal.h" |
bfbf150e | 33 | #include "networkd-address-pool.h" |
3b6a3bde | 34 | #include "networkd-address.h" |
cf72a786 | 35 | #include "networkd-dhcp-server-bus.h" |
ca5ad760 | 36 | #include "networkd-dhcp6.h" |
6a1af3d4 | 37 | #include "networkd-link-bus.h" |
79a59fa5 | 38 | #include "networkd-manager-bus.h" |
23f53b99 | 39 | #include "networkd-manager.h" |
1939ebeb | 40 | #include "networkd-neighbor.h" |
ceac2c2b | 41 | #include "networkd-network-bus.h" |
75156ccb | 42 | #include "networkd-nexthop.h" |
19d9a5ad | 43 | #include "networkd-queue.h" |
3b6a3bde | 44 | #include "networkd-route.h" |
ca183bf8 | 45 | #include "networkd-routing-policy-rule.h" |
a879e1a4 | 46 | #include "networkd-speed-meter.h" |
3b5a4fc6 | 47 | #include "networkd-state-file.h" |
96f5f9ef | 48 | #include "networkd-wifi.h" |
edb69db2 | 49 | #include "networkd-wiphy.h" |
00616955 | 50 | #include "ordered-set.h" |
b0c82192 | 51 | #include "path-lookup.h" |
07630cea | 52 | #include "path-util.h" |
828a81a9 | 53 | #include "qdisc.h" |
92b555aa | 54 | #include "selinux-util.h" |
07630cea | 55 | #include "set.h" |
ab76be55 | 56 | #include "signal-util.h" |
d31f33e3 | 57 | #include "stat-util.h" |
21486d9e | 58 | #include "strv.h" |
4b600505 | 59 | #include "sysctl-util.h" |
828a81a9 | 60 | #include "tclass.h" |
e4de7287 | 61 | #include "tmpfile-util.h" |
af7a86b8 | 62 | #include "tuntap.h" |
82f52245 | 63 | #include "udev-util.h" |
505f8da7 | 64 | |
48d0248e YW |
65 | /* use 128 MB for receive socket kernel queue. */ |
66 | #define RCVBUF_SIZE (128*1024*1024) | |
be660c37 | 67 | |
19070062 | 68 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
99534007 | 69 | Manager *m = ASSERT_PTR(userdata); |
a39a9ac8 | 70 | Link *link; |
9c0a72f9 TG |
71 | int b, r; |
72 | ||
19070062 | 73 | assert(message); |
9c0a72f9 TG |
74 | |
75 | r = sd_bus_message_read(message, "b", &b); | |
76 | if (r < 0) { | |
d67b1d18 | 77 | bus_log_parse_error(r); |
9c0a72f9 TG |
78 | return 0; |
79 | } | |
80 | ||
81 | if (b) | |
82 | return 0; | |
83 | ||
a39a9ac8 | 84 | log_debug("Coming back from suspend, reconfiguring all connections..."); |
9c0a72f9 | 85 | |
a39a9ac8 YW |
86 | HASHMAP_FOREACH(link, m->links_by_index) { |
87 | r = link_reconfigure(link, /* force = */ true); | |
88 | if (r < 0) { | |
89 | log_link_warning_errno(link, r, "Failed to reconfigure interface: %m"); | |
90 | link_enter_failed(link); | |
91 | } | |
92 | } | |
9c0a72f9 TG |
93 | |
94 | return 0; | |
95 | } | |
96 | ||
d7afd945 | 97 | static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
99534007 | 98 | Manager *m = ASSERT_PTR(userdata); |
9c0a72f9 | 99 | |
d7afd945 | 100 | assert(message); |
9c0a72f9 | 101 | |
d7afd945 LP |
102 | /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */ |
103 | if (m->dynamic_hostname) | |
104 | (void) manager_set_hostname(m, m->dynamic_hostname); | |
105 | if (m->dynamic_timezone) | |
106 | (void) manager_set_timezone(m, m->dynamic_timezone); | |
ccffa166 | 107 | if (m->product_uuid_requested) |
4e26a5ba | 108 | (void) manager_request_product_uuid(m); |
9c0a72f9 | 109 | |
d7afd945 LP |
110 | return 0; |
111 | } | |
9c0a72f9 | 112 | |
96243149 | 113 | static int manager_connect_bus(Manager *m) { |
d7afd945 LP |
114 | int r; |
115 | ||
116 | assert(m); | |
96243149 | 117 | assert(!m->bus); |
7d6884b6 | 118 | |
621e4509 | 119 | r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network"); |
9c0a72f9 | 120 | if (r < 0) |
d7afd945 | 121 | return log_error_errno(r, "Failed to connect to bus: %m"); |
9c0a72f9 | 122 | |
6e194652 | 123 | r = bus_add_implementation(m->bus, &manager_object, m); |
e331e246 | 124 | if (r < 0) |
6e194652 | 125 | return r; |
e331e246 | 126 | |
ac9f55ed LP |
127 | r = bus_log_control_api_register(m->bus); |
128 | if (r < 0) | |
129 | return r; | |
130 | ||
696fc836 | 131 | r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL); |
e331e246 | 132 | if (r < 0) |
0c0b9306 | 133 | return log_error_errno(r, "Failed to request name: %m"); |
e331e246 TG |
134 | |
135 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
136 | if (r < 0) | |
137 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
138 | ||
d7afd945 LP |
139 | r = sd_bus_match_signal_async( |
140 | m->bus, | |
cad43595 | 141 | NULL, |
d7afd945 LP |
142 | "org.freedesktop.DBus.Local", |
143 | NULL, | |
144 | "org.freedesktop.DBus.Local", | |
145 | "Connected", | |
146 | on_connected, NULL, m); | |
147 | if (r < 0) | |
148 | return log_error_errno(r, "Failed to request match on Connected signal: %m"); | |
149 | ||
150 | r = sd_bus_match_signal_async( | |
151 | m->bus, | |
cad43595 | 152 | NULL, |
d7afd945 LP |
153 | "org.freedesktop.login1", |
154 | "/org/freedesktop/login1", | |
155 | "org.freedesktop.login1.Manager", | |
156 | "PrepareForSleep", | |
157 | match_prepare_for_sleep, NULL, m); | |
158 | if (r < 0) | |
159 | log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m"); | |
7901cea1 | 160 | |
9c0a72f9 TG |
161 | return 0; |
162 | } | |
163 | ||
1fd3ac1e YW |
164 | static int manager_process_uevent(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
165 | Manager *m = ASSERT_PTR(userdata); | |
166 | sd_device_action_t action; | |
167 | const char *s; | |
168 | int r; | |
169 | ||
170 | assert(device); | |
171 | ||
172 | r = sd_device_get_action(device, &action); | |
173 | if (r < 0) | |
174 | return log_device_warning_errno(device, r, "Failed to get udev action, ignoring: %m"); | |
175 | ||
176 | r = sd_device_get_subsystem(device, &s); | |
177 | if (r < 0) | |
178 | return log_device_warning_errno(device, r, "Failed to get subsystem, ignoring: %m"); | |
179 | ||
180 | if (streq(s, "net")) | |
181 | r = manager_udev_process_link(m, device, action); | |
c49d5362 YW |
182 | else if (streq(s, "ieee80211")) |
183 | r = manager_udev_process_wiphy(m, device, action); | |
8642e04b YW |
184 | else if (streq(s, "rfkill")) |
185 | r = manager_udev_process_rfkill(m, device, action); | |
1fd3ac1e YW |
186 | else { |
187 | log_device_debug(device, "Received device with unexpected subsystem \"%s\", ignoring.", s); | |
188 | return 0; | |
189 | } | |
190 | if (r < 0) | |
191 | log_device_warning_errno(device, r, "Failed to process \"%s\" uevent, ignoring: %m", | |
192 | device_action_to_string(action)); | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
5fae368b TG |
197 | static int manager_connect_udev(Manager *m) { |
198 | int r; | |
f579559b | 199 | |
d31f33e3 YW |
200 | /* udev does not initialize devices inside containers, so we rely on them being already |
201 | * initialized before entering the container. */ | |
82f52245 | 202 | if (!udev_available()) |
5fae368b | 203 | return 0; |
f579559b | 204 | |
d2ebf952 | 205 | r = sd_device_monitor_new(&m->device_monitor); |
02b59d57 | 206 | if (r < 0) |
d2ebf952 | 207 | return log_error_errno(r, "Failed to initialize device monitor: %m"); |
02b59d57 | 208 | |
a725efb0 YW |
209 | r = sd_device_monitor_set_receive_buffer_size(m->device_monitor, RCVBUF_SIZE); |
210 | if (r < 0) | |
211 | log_warning_errno(r, "Failed to increase buffer size for device monitor, ignoring: %m"); | |
212 | ||
d2ebf952 YW |
213 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL); |
214 | if (r < 0) | |
1fd3ac1e | 215 | return log_error_errno(r, "Could not add device monitor filter for net subsystem: %m"); |
505f8da7 | 216 | |
c49d5362 YW |
217 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "ieee80211", NULL); |
218 | if (r < 0) | |
219 | return log_error_errno(r, "Could not add device monitor filter for ieee80211 subsystem: %m"); | |
220 | ||
8642e04b YW |
221 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "rfkill", NULL); |
222 | if (r < 0) | |
223 | return log_error_errno(r, "Could not add device monitor filter for rfkill subsystem: %m"); | |
224 | ||
deb2b734 | 225 | r = sd_device_monitor_attach_event(m->device_monitor, m->event); |
5fae368b | 226 | if (r < 0) |
d2ebf952 | 227 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); |
505f8da7 | 228 | |
1fd3ac1e | 229 | r = sd_device_monitor_start(m->device_monitor, manager_process_uevent, m); |
505f8da7 | 230 | if (r < 0) |
d2ebf952 | 231 | return log_error_errno(r, "Failed to start device monitor: %m"); |
11a7f229 | 232 | |
505f8da7 TG |
233 | return 0; |
234 | } | |
f579559b | 235 | |
af7a86b8 YW |
236 | static int manager_listen_fds(Manager *m, int *ret_rtnl_fd) { |
237 | _cleanup_strv_free_ char **names = NULL; | |
254d1313 | 238 | int n, rtnl_fd = -EBADF; |
5fae368b | 239 | |
af7a86b8 YW |
240 | assert(m); |
241 | assert(ret_rtnl_fd); | |
242 | ||
243 | n = sd_listen_fds_with_names(/* unset_environment = */ true, &names); | |
244 | if (n < 0) | |
245 | return n; | |
246 | ||
247 | if (strv_length(names) != (size_t) n) | |
5fae368b TG |
248 | return -EINVAL; |
249 | ||
af7a86b8 YW |
250 | for (int i = 0; i < n; i++) { |
251 | int fd = i + SD_LISTEN_FDS_START; | |
252 | ||
5fae368b | 253 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { |
af7a86b8 YW |
254 | if (rtnl_fd >= 0) { |
255 | log_debug("Received multiple netlink socket, ignoring."); | |
256 | safe_close(fd); | |
257 | continue; | |
258 | } | |
5fae368b TG |
259 | |
260 | rtnl_fd = fd; | |
af7a86b8 | 261 | continue; |
5fae368b | 262 | } |
5fae368b | 263 | |
af7a86b8 YW |
264 | if (manager_add_tuntap_fd(m, fd, names[i]) >= 0) |
265 | continue; | |
266 | ||
267 | if (m->test_mode) | |
268 | safe_close(fd); | |
269 | else | |
270 | close_and_notify_warn(fd, names[i]); | |
271 | } | |
272 | ||
273 | *ret_rtnl_fd = rtnl_fd; | |
274 | return 0; | |
5fae368b TG |
275 | } |
276 | ||
05d0c2e3 JT |
277 | static int manager_connect_genl(Manager *m) { |
278 | int r; | |
279 | ||
280 | assert(m); | |
281 | ||
282 | r = sd_genl_socket_open(&m->genl); | |
283 | if (r < 0) | |
284 | return r; | |
285 | ||
78cca583 | 286 | r = sd_netlink_increase_rxbuf(m->genl, RCVBUF_SIZE); |
05d0c2e3 | 287 | if (r < 0) |
8c63924c | 288 | log_warning_errno(r, "Failed to increase receive buffer size for general netlink socket, ignoring: %m"); |
05d0c2e3 JT |
289 | |
290 | r = sd_netlink_attach_event(m->genl, m->event, 0); | |
291 | if (r < 0) | |
292 | return r; | |
293 | ||
96f5f9ef YW |
294 | r = genl_add_match(m->genl, NULL, NL80211_GENL_NAME, NL80211_MULTICAST_GROUP_CONFIG, 0, |
295 | &manager_genl_process_nl80211_config, NULL, m, "network-genl_process_nl80211_config"); | |
296 | if (r < 0 && r != -EOPNOTSUPP) | |
297 | return r; | |
298 | ||
299 | r = genl_add_match(m->genl, NULL, NL80211_GENL_NAME, NL80211_MULTICAST_GROUP_MLME, 0, | |
300 | &manager_genl_process_nl80211_mlme, NULL, m, "network-genl_process_nl80211_mlme"); | |
301 | if (r < 0 && r != -EOPNOTSUPP) | |
302 | return r; | |
303 | ||
05d0c2e3 JT |
304 | return 0; |
305 | } | |
306 | ||
26a8be48 YW |
307 | static int manager_setup_rtnl_filter(Manager *manager) { |
308 | struct sock_filter filter[] = { | |
309 | /* Check the packet length. */ | |
310 | BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */ | |
311 | BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct nlmsghdr), 1, 0), /* A (packet length) >= sizeof(struct nlmsghdr) ? */ | |
312 | BPF_STMT(BPF_RET + BPF_K, 0), /* reject */ | |
313 | /* Always accept multipart message. */ | |
314 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct nlmsghdr, nlmsg_flags)), /* A <- message flags */ | |
315 | BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, htobe16(NLM_F_MULTI), 0, 1), /* message flags has NLM_F_MULTI ? */ | |
316 | BPF_STMT(BPF_RET + BPF_K, UINT32_MAX), /* accept */ | |
317 | /* Accept all message types except for RTM_NEWNEIGH or RTM_DELNEIGH. */ | |
318 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct nlmsghdr, nlmsg_type)), /* A <- message type */ | |
319 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htobe16(RTM_NEWNEIGH), 2, 0), /* message type == RTM_NEWNEIGH ? */ | |
320 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htobe16(RTM_DELNEIGH), 1, 0), /* message type == RTM_DELNEIGH ? */ | |
321 | BPF_STMT(BPF_RET + BPF_K, UINT32_MAX), /* accept */ | |
322 | /* Check the packet length. */ | |
323 | BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */ | |
324 | BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct nlmsghdr) + sizeof(struct ndmsg), 1, 0), | |
325 | /* packet length >= sizeof(struct nlmsghdr) + sizeof(struct ndmsg) ? */ | |
326 | BPF_STMT(BPF_RET + BPF_K, 0), /* reject */ | |
327 | /* Reject the message when the neighbor state does not have NUD_PERMANENT flag. */ | |
328 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, sizeof(struct nlmsghdr) + offsetof(struct ndmsg, ndm_state)), | |
329 | /* A <- neighbor state */ | |
330 | BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, htobe16(NUD_PERMANENT), 1, 0), /* neighbor state has NUD_PERMANENT ? */ | |
331 | BPF_STMT(BPF_RET + BPF_K, 0), /* reject */ | |
332 | BPF_STMT(BPF_RET + BPF_K, UINT32_MAX), /* accept */ | |
333 | }; | |
334 | ||
335 | assert(manager); | |
336 | assert(manager->rtnl); | |
337 | ||
338 | return sd_netlink_attach_filter(manager->rtnl, ELEMENTSOF(filter), filter); | |
339 | } | |
340 | ||
af7a86b8 YW |
341 | static int manager_connect_rtnl(Manager *m, int fd) { |
342 | _unused_ _cleanup_close_ int fd_close = fd; | |
343 | int r; | |
505f8da7 TG |
344 | |
345 | assert(m); | |
505f8da7 | 346 | |
af7a86b8 YW |
347 | /* This takes input fd. */ |
348 | ||
5fae368b | 349 | if (fd < 0) |
1c4baffc | 350 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 351 | else |
1c4baffc | 352 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
353 | if (r < 0) |
354 | return r; | |
af7a86b8 | 355 | TAKE_FD(fd_close); |
505f8da7 | 356 | |
e13af7bd YW |
357 | /* Bump receiver buffer, but only if we are not called via socket activation, as in that |
358 | * case systemd sets the receive buffer size for us, and the value in the .socket unit | |
359 | * should take full effect. */ | |
360 | if (fd < 0) { | |
78cca583 | 361 | r = sd_netlink_increase_rxbuf(m->rtnl, RCVBUF_SIZE); |
e13af7bd YW |
362 | if (r < 0) |
363 | log_warning_errno(r, "Failed to increase receive buffer size for rtnl socket, ignoring: %m"); | |
364 | } | |
f579559b | 365 | |
1c4baffc | 366 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
367 | if (r < 0) |
368 | return r; | |
f579559b | 369 | |
8dfed23d | 370 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
371 | if (r < 0) |
372 | return r; | |
505f8da7 | 373 | |
8dfed23d | 374 | r = netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link"); |
5fae368b TG |
375 | if (r < 0) |
376 | return r; | |
45af44d4 | 377 | |
828a81a9 YW |
378 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWQDISC, &manager_rtnl_process_qdisc, NULL, m, "network-rtnl_process_qdisc"); |
379 | if (r < 0) | |
380 | return r; | |
381 | ||
382 | r = netlink_add_match(m->rtnl, NULL, RTM_DELQDISC, &manager_rtnl_process_qdisc, NULL, m, "network-rtnl_process_qdisc"); | |
383 | if (r < 0) | |
384 | return r; | |
385 | ||
386 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWTCLASS, &manager_rtnl_process_tclass, NULL, m, "network-rtnl_process_tclass"); | |
387 | if (r < 0) | |
388 | return r; | |
389 | ||
390 | r = netlink_add_match(m->rtnl, NULL, RTM_DELTCLASS, &manager_rtnl_process_tclass, NULL, m, "network-rtnl_process_tclass"); | |
391 | if (r < 0) | |
392 | return r; | |
393 | ||
8dfed23d | 394 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
395 | if (r < 0) |
396 | return r; | |
397 | ||
8dfed23d | 398 | r = netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address"); |
5fae368b TG |
399 | if (r < 0) |
400 | return r; | |
401 | ||
8dfed23d | 402 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor"); |
d1bdafd2 WKI |
403 | if (r < 0) |
404 | return r; | |
405 | ||
8dfed23d | 406 | r = netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor"); |
d1bdafd2 WKI |
407 | if (r < 0) |
408 | return r; | |
409 | ||
8dfed23d | 410 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
411 | if (r < 0) |
412 | return r; | |
413 | ||
8dfed23d | 414 | r = netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route"); |
1c8e710c TG |
415 | if (r < 0) |
416 | return r; | |
417 | ||
8dfed23d | 418 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
419 | if (r < 0) |
420 | return r; | |
421 | ||
8dfed23d | 422 | r = netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule"); |
bce67bbe SS |
423 | if (r < 0) |
424 | return r; | |
425 | ||
8dfed23d | 426 | r = netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop"); |
c16c7808 SS |
427 | if (r < 0) |
428 | return r; | |
429 | ||
8dfed23d | 430 | r = netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop"); |
c16c7808 SS |
431 | if (r < 0) |
432 | return r; | |
433 | ||
26a8be48 | 434 | return manager_setup_rtnl_filter(m); |
45af44d4 | 435 | } |
505f8da7 | 436 | |
84de38c5 | 437 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { |
99534007 | 438 | Manager *m = ASSERT_PTR(userdata); |
84de38c5 | 439 | Link *link; |
d23a66f2 | 440 | int r; |
84de38c5 | 441 | |
d23a66f2 YW |
442 | if (m->dirty) { |
443 | r = manager_save(m); | |
444 | if (r < 0) | |
445 | log_warning_errno(r, "Failed to update state file %s, ignoring: %m", m->state_file); | |
446 | } | |
84de38c5 | 447 | |
d23a66f2 YW |
448 | SET_FOREACH(link, m->dirty_links) { |
449 | r = link_save_and_clean(link); | |
450 | if (r < 0) | |
451 | log_link_warning_errno(link, r, "Failed to update link state file %s, ignoring: %m", link->state_file); | |
452 | } | |
84de38c5 TG |
453 | |
454 | return 1; | |
455 | } | |
456 | ||
ab76be55 | 457 | static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
99534007 | 458 | Manager *m = ASSERT_PTR(userdata); |
ab76be55 | 459 | |
ab76be55 ZJS |
460 | m->restarting = false; |
461 | ||
462 | log_debug("Terminate operation initiated."); | |
463 | ||
464 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
465 | } | |
466 | ||
467 | static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
99534007 | 468 | Manager *m = ASSERT_PTR(userdata); |
ab76be55 | 469 | |
ab76be55 ZJS |
470 | m->restarting = true; |
471 | ||
472 | log_debug("Restart operation initiated."); | |
473 | ||
474 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
475 | } | |
476 | ||
ea853de5 YW |
477 | static int manager_set_keep_configuration(Manager *m) { |
478 | int r; | |
479 | ||
480 | assert(m); | |
481 | ||
482 | if (in_initrd()) { | |
483 | log_debug("Running in initrd, keep DHCPv4 addresses on stopping networkd by default."); | |
484 | m->keep_configuration = KEEP_CONFIGURATION_DHCP_ON_STOP; | |
485 | return 0; | |
486 | } | |
487 | ||
488 | r = path_is_network_fs("/"); | |
489 | if (r < 0) | |
490 | return log_error_errno(r, "Failed to detect if root is network filesystem: %m"); | |
491 | if (r == 0) { | |
492 | m->keep_configuration = _KEEP_CONFIGURATION_INVALID; | |
493 | return 0; | |
494 | } | |
495 | ||
496 | log_debug("Running on network filesystem, enabling KeepConfiguration= by default."); | |
497 | m->keep_configuration = KEEP_CONFIGURATION_YES; | |
498 | return 0; | |
499 | } | |
500 | ||
4c78dc17 | 501 | int manager_setup(Manager *m) { |
254d1313 | 502 | _cleanup_close_ int rtnl_fd = -EBADF; |
45af44d4 | 503 | int r; |
f579559b | 504 | |
96243149 | 505 | assert(m); |
5fae368b | 506 | |
3534a043 YW |
507 | r = sd_event_default(&m->event); |
508 | if (r < 0) | |
509 | return r; | |
510 | ||
ab76be55 ZJS |
511 | assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0); |
512 | ||
05e21627 | 513 | (void) sd_event_set_watchdog(m->event, true); |
ab76be55 ZJS |
514 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m); |
515 | (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m); | |
516 | (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m); | |
5fae368b | 517 | |
84de38c5 TG |
518 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
519 | if (r < 0) | |
520 | return r; | |
521 | ||
19d9a5ad YW |
522 | r = sd_event_add_post(m->event, NULL, manager_process_requests, m); |
523 | if (r < 0) | |
524 | return r; | |
525 | ||
af7a86b8 YW |
526 | r = manager_listen_fds(m, &rtnl_fd); |
527 | if (r < 0) | |
528 | return r; | |
529 | ||
530 | r = manager_connect_rtnl(m, TAKE_FD(rtnl_fd)); | |
45af44d4 TG |
531 | if (r < 0) |
532 | return r; | |
533 | ||
05d0c2e3 JT |
534 | r = manager_connect_genl(m); |
535 | if (r < 0) | |
536 | return r; | |
537 | ||
4c78dc17 | 538 | if (m->test_mode) |
96243149 YW |
539 | return 0; |
540 | ||
541 | r = manager_connect_bus(m); | |
542 | if (r < 0) | |
543 | return r; | |
544 | ||
5fae368b TG |
545 | r = manager_connect_udev(m); |
546 | if (r < 0) | |
547 | return r; | |
45af44d4 | 548 | |
05d0c2e3 JT |
549 | r = sd_resolve_default(&m->resolve); |
550 | if (r < 0) | |
551 | return r; | |
552 | ||
553 | r = sd_resolve_attach_event(m->resolve, m->event, 0); | |
554 | if (r < 0) | |
555 | return r; | |
556 | ||
ed76f585 | 557 | r = address_pool_setup_default(m); |
5fae368b TG |
558 | if (r < 0) |
559 | return r; | |
f579559b | 560 | |
ea853de5 YW |
561 | r = manager_set_keep_configuration(m); |
562 | if (r < 0) | |
563 | return r; | |
564 | ||
96243149 YW |
565 | m->state_file = strdup("/run/systemd/netif/state"); |
566 | if (!m->state_file) | |
567 | return -ENOMEM; | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
4c78dc17 | 572 | int manager_new(Manager **ret, bool test_mode) { |
96243149 | 573 | _cleanup_(manager_freep) Manager *m = NULL; |
f579559b | 574 | |
96243149 YW |
575 | m = new(Manager, 1); |
576 | if (!m) | |
577 | return -ENOMEM; | |
578 | ||
579 | *m = (Manager) { | |
ea853de5 | 580 | .keep_configuration = _KEEP_CONFIGURATION_INVALID, |
4c78dc17 | 581 | .test_mode = test_mode, |
96243149 YW |
582 | .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL, |
583 | .online_state = _LINK_ONLINE_STATE_INVALID, | |
584 | .manage_foreign_routes = true, | |
585 | .manage_foreign_rules = true, | |
254d1313 | 586 | .ethtool_fd = -EBADF, |
96243149 YW |
587 | .dhcp_duid.type = DUID_TYPE_EN, |
588 | .dhcp6_duid.type = DUID_TYPE_EN, | |
589 | .duid_product_uuid.type = DUID_TYPE_UUID, | |
590 | }; | |
591 | ||
592 | *ret = TAKE_PTR(m); | |
f579559b TG |
593 | return 0; |
594 | } | |
595 | ||
75db809a | 596 | Manager* manager_free(Manager *m) { |
5fae368b | 597 | Link *link; |
f579559b | 598 | |
5fae368b | 599 | if (!m) |
75db809a | 600 | return NULL; |
505f8da7 | 601 | |
5fae368b | 602 | free(m->state_file); |
505f8da7 | 603 | |
6eab614d | 604 | HASHMAP_FOREACH(link, m->links_by_index) |
2a99eed0 | 605 | (void) link_stop_engines(link, true); |
946f8e14 | 606 | |
40b12fa2 | 607 | m->request_queue = ordered_set_free(m->request_queue); |
19d9a5ad | 608 | |
c4397d94 | 609 | m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref); |
0b54c870 | 610 | m->links_by_name = hashmap_free(m->links_by_name); |
fe321d45 | 611 | m->links_by_hw_addr = hashmap_free(m->links_by_hw_addr); |
a27588d4 | 612 | m->links_by_dhcp_pd_subnet_prefix = hashmap_free(m->links_by_dhcp_pd_subnet_prefix); |
6eab614d | 613 | m->links_by_index = hashmap_free_with_destructor(m->links_by_index, link_unref); |
27dfc982 | 614 | |
a27588d4 | 615 | m->dhcp_pd_subnet_ids = set_free(m->dhcp_pd_subnet_ids); |
715d398e | 616 | m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref); |
dbffab87 | 617 | |
c4397d94 | 618 | m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref); |
5fae368b | 619 | |
af7a86b8 YW |
620 | m->tuntap_fds_by_name = hashmap_free(m->tuntap_fds_by_name); |
621 | ||
edb69db2 YW |
622 | m->wiphy_by_name = hashmap_free(m->wiphy_by_name); |
623 | m->wiphy_by_index = hashmap_free_with_destructor(m->wiphy_by_index, wiphy_free); | |
624 | ||
bfbf150e | 625 | ordered_set_free_free(m->address_pools); |
5fae368b | 626 | |
552b90a2 YW |
627 | hashmap_free(m->route_table_names_by_number); |
628 | hashmap_free(m->route_table_numbers_by_name); | |
c038ce46 | 629 | |
eb72fa3a | 630 | set_free(m->rules); |
bce67bbe | 631 | |
d4df6326 SS |
632 | sd_netlink_unref(m->rtnl); |
633 | sd_netlink_unref(m->genl); | |
634 | sd_resolve_unref(m->resolve); | |
635 | ||
450fa34b YW |
636 | /* reject (e.g. unreachable) type routes are managed by Manager, but may be referenced by a |
637 | * link. E.g., DHCP6 with prefix delegation creates unreachable routes, and they are referenced | |
638 | * by the upstream link. And the links may be referenced by netlink slots. Hence, two | |
639 | * set_free() must be called after the above sd_netlink_unref(). */ | |
640 | m->routes = set_free(m->routes); | |
641 | m->routes_foreign = set_free(m->routes_foreign); | |
642 | ||
30f10837 | 643 | m->nexthops = set_free(m->nexthops); |
69e244e3 YW |
644 | m->nexthops_by_id = hashmap_free(m->nexthops_by_id); |
645 | ||
a879e1a4 | 646 | sd_event_source_unref(m->speed_meter_event_source); |
2f5b4a77 | 647 | sd_event_unref(m->event); |
5fae368b | 648 | |
d2ebf952 | 649 | sd_device_monitor_unref(m->device_monitor); |
7d20d375 | 650 | |
15761549 | 651 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
92e31da1 | 652 | sd_bus_flush_close_unref(m->bus); |
7d20d375 | 653 | |
7901cea1 MP |
654 | free(m->dynamic_timezone); |
655 | free(m->dynamic_hostname); | |
656 | ||
c643bda5 YW |
657 | safe_close(m->ethtool_fd); |
658 | ||
761cf19d FW |
659 | m->fw_ctx = fw_ctx_free(m->fw_ctx); |
660 | ||
75db809a | 661 | return mfree(m); |
5fae368b TG |
662 | } |
663 | ||
b76d99d9 | 664 | int manager_start(Manager *m) { |
84de38c5 | 665 | Link *link; |
a879e1a4 | 666 | int r; |
84de38c5 | 667 | |
a97dcc12 TG |
668 | assert(m); |
669 | ||
a879e1a4 YW |
670 | r = manager_start_speed_meter(m); |
671 | if (r < 0) | |
672 | return log_error_errno(r, "Failed to initialize speed meter: %m"); | |
673 | ||
84de38c5 TG |
674 | /* The dirty handler will deal with future serialization, but the first one |
675 | must be done explicitly. */ | |
676 | ||
d23a66f2 YW |
677 | r = manager_save(m); |
678 | if (r < 0) | |
679 | log_warning_errno(r, "Failed to update state file %s, ignoring: %m", m->state_file); | |
84de38c5 | 680 | |
6eab614d | 681 | HASHMAP_FOREACH(link, m->links_by_index) { |
d23a66f2 YW |
682 | r = link_save(link); |
683 | if (r < 0) | |
684 | log_link_warning_errno(link, r, "Failed to update link state file %s, ignoring: %m", link->state_file); | |
685 | } | |
84de38c5 | 686 | |
b76d99d9 | 687 | return 0; |
a97dcc12 TG |
688 | } |
689 | ||
5fae368b TG |
690 | int manager_load_config(Manager *m) { |
691 | int r; | |
692 | ||
e272b621 | 693 | r = netdev_load(m, false); |
f579559b TG |
694 | if (r < 0) |
695 | return r; | |
696 | ||
af7a86b8 YW |
697 | manager_clear_unmanaged_tuntap_fds(m); |
698 | ||
7f06b3e1 | 699 | r = network_load(m, &m->networks); |
9021bb9f TG |
700 | if (r < 0) |
701 | return r; | |
702 | ||
a27588d4 | 703 | return manager_build_dhcp_pd_subnet_ids(m); |
f579559b | 704 | } |
f882c247 | 705 | |
446aaaf3 YW |
706 | static int manager_enumerate_internal( |
707 | Manager *m, | |
bdcd4ab2 | 708 | sd_netlink *nl, |
446aaaf3 | 709 | sd_netlink_message *req, |
f12629ae | 710 | int (*process)(sd_netlink *, sd_netlink_message *, Manager *)) { |
446aaaf3 YW |
711 | |
712 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *reply = NULL; | |
bdcd4ab2 | 713 | int k, r; |
f882c247 | 714 | |
5da8149f | 715 | assert(m); |
bdcd4ab2 | 716 | assert(nl); |
446aaaf3 YW |
717 | assert(req); |
718 | assert(process); | |
f882c247 | 719 | |
24c0f385 | 720 | r = sd_netlink_message_set_request_dump(req, true); |
dd3efc09 TG |
721 | if (r < 0) |
722 | return r; | |
723 | ||
bdcd4ab2 | 724 | r = sd_netlink_call(nl, req, 0, &reply); |
f12629ae | 725 | if (r < 0) |
f2236469 TG |
726 | return r; |
727 | ||
a6b08423 | 728 | m->enumerating = true; |
446aaaf3 | 729 | for (sd_netlink_message *reply_one = reply; reply_one; reply_one = sd_netlink_message_next(reply_one)) { |
bdcd4ab2 | 730 | k = process(nl, reply_one, m); |
446aaaf3 | 731 | if (k < 0 && r >= 0) |
5fae368b TG |
732 | r = k; |
733 | } | |
a6b08423 | 734 | m->enumerating = false; |
2e9f08ea | 735 | |
5fae368b | 736 | return r; |
f882c247 | 737 | } |
3bef724f | 738 | |
446aaaf3 YW |
739 | static int manager_enumerate_links(Manager *m) { |
740 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1346b1f0 TG |
741 | int r; |
742 | ||
5fae368b TG |
743 | assert(m); |
744 | assert(m->rtnl); | |
bcbca829 | 745 | |
446aaaf3 | 746 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
5fae368b TG |
747 | if (r < 0) |
748 | return r; | |
749 | ||
f12629ae | 750 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_link); |
446aaaf3 | 751 | } |
5fae368b | 752 | |
828a81a9 YW |
753 | static int manager_enumerate_qdisc(Manager *m) { |
754 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
755 | int r; | |
756 | ||
757 | assert(m); | |
758 | assert(m->rtnl); | |
759 | ||
760 | r = sd_rtnl_message_new_traffic_control(m->rtnl, &req, RTM_GETQDISC, 0, 0, 0); | |
761 | if (r < 0) | |
762 | return r; | |
763 | ||
764 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_qdisc); | |
765 | } | |
766 | ||
767 | static int manager_enumerate_tclass(Manager *m) { | |
768 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
769 | int r; | |
770 | ||
771 | assert(m); | |
772 | assert(m->rtnl); | |
773 | ||
774 | r = sd_rtnl_message_new_traffic_control(m->rtnl, &req, RTM_GETTCLASS, 0, 0, 0); | |
775 | if (r < 0) | |
776 | return r; | |
777 | ||
778 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_tclass); | |
779 | } | |
780 | ||
446aaaf3 YW |
781 | static int manager_enumerate_addresses(Manager *m) { |
782 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
783 | int r; | |
6a24f148 | 784 | |
446aaaf3 YW |
785 | assert(m); |
786 | assert(m->rtnl); | |
6a24f148 | 787 | |
446aaaf3 YW |
788 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
789 | if (r < 0) | |
790 | return r; | |
5fae368b | 791 | |
f12629ae | 792 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_address); |
1346b1f0 | 793 | } |
d1bdafd2 | 794 | |
446aaaf3 YW |
795 | static int manager_enumerate_neighbors(Manager *m) { |
796 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
d1bdafd2 WKI |
797 | int r; |
798 | ||
799 | assert(m); | |
800 | assert(m->rtnl); | |
801 | ||
802 | r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC); | |
803 | if (r < 0) | |
804 | return r; | |
805 | ||
f12629ae | 806 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_neighbor); |
d1bdafd2 | 807 | } |
1346b1f0 | 808 | |
446aaaf3 YW |
809 | static int manager_enumerate_routes(Manager *m) { |
810 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1c8e710c TG |
811 | int r; |
812 | ||
813 | assert(m); | |
814 | assert(m->rtnl); | |
815 | ||
5ff1ef31 YW |
816 | if (!m->manage_foreign_routes) |
817 | return 0; | |
818 | ||
1c8e710c TG |
819 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); |
820 | if (r < 0) | |
821 | return r; | |
822 | ||
f12629ae | 823 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_route); |
1c8e710c TG |
824 | } |
825 | ||
446aaaf3 YW |
826 | static int manager_enumerate_rules(Manager *m) { |
827 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
bce67bbe SS |
828 | int r; |
829 | ||
830 | assert(m); | |
831 | assert(m->rtnl); | |
832 | ||
d94dfe70 YW |
833 | if (!m->manage_foreign_rules) |
834 | return 0; | |
835 | ||
bce67bbe SS |
836 | r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0); |
837 | if (r < 0) | |
838 | return r; | |
839 | ||
f12629ae | 840 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_rule); |
bce67bbe SS |
841 | } |
842 | ||
446aaaf3 YW |
843 | static int manager_enumerate_nexthop(Manager *m) { |
844 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
c16c7808 SS |
845 | int r; |
846 | ||
847 | assert(m); | |
848 | assert(m->rtnl); | |
849 | ||
850 | r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0); | |
851 | if (r < 0) | |
852 | return r; | |
853 | ||
f12629ae | 854 | return manager_enumerate_internal(m, m->rtnl, req, manager_rtnl_process_nexthop); |
446aaaf3 | 855 | } |
c16c7808 | 856 | |
edb69db2 YW |
857 | static int manager_enumerate_nl80211_wiphy(Manager *m) { |
858 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
859 | int r; | |
860 | ||
861 | assert(m); | |
862 | assert(m->genl); | |
863 | ||
864 | r = sd_genl_message_new(m->genl, NL80211_GENL_NAME, NL80211_CMD_GET_WIPHY, &req); | |
865 | if (r < 0) | |
866 | return r; | |
867 | ||
868 | return manager_enumerate_internal(m, m->genl, req, manager_genl_process_nl80211_wiphy); | |
869 | } | |
870 | ||
96f5f9ef YW |
871 | static int manager_enumerate_nl80211_config(Manager *m) { |
872 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
873 | int r; | |
874 | ||
875 | assert(m); | |
876 | assert(m->genl); | |
877 | ||
878 | r = sd_genl_message_new(m->genl, NL80211_GENL_NAME, NL80211_CMD_GET_INTERFACE, &req); | |
879 | if (r < 0) | |
880 | return r; | |
881 | ||
882 | return manager_enumerate_internal(m, m->genl, req, manager_genl_process_nl80211_config); | |
883 | } | |
884 | ||
885 | static int manager_enumerate_nl80211_mlme(Manager *m) { | |
886 | Link *link; | |
887 | int r; | |
888 | ||
889 | assert(m); | |
890 | assert(m->genl); | |
891 | ||
892 | HASHMAP_FOREACH(link, m->links_by_index) { | |
893 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
894 | ||
895 | if (link->wlan_iftype != NL80211_IFTYPE_STATION) | |
896 | continue; | |
897 | ||
898 | r = sd_genl_message_new(m->genl, NL80211_GENL_NAME, NL80211_CMD_GET_STATION, &req); | |
899 | if (r < 0) | |
900 | return r; | |
901 | ||
902 | r = sd_netlink_message_append_u32(req, NL80211_ATTR_IFINDEX, link->ifindex); | |
903 | if (r < 0) | |
904 | return r; | |
905 | ||
906 | r = manager_enumerate_internal(m, m->genl, req, manager_genl_process_nl80211_mlme); | |
907 | if (r < 0) | |
908 | return r; | |
909 | } | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
446aaaf3 YW |
914 | int manager_enumerate(Manager *m) { |
915 | int r; | |
c16c7808 | 916 | |
446aaaf3 YW |
917 | r = manager_enumerate_links(m); |
918 | if (r < 0) | |
919 | return log_error_errno(r, "Could not enumerate links: %m"); | |
c16c7808 | 920 | |
828a81a9 | 921 | r = manager_enumerate_qdisc(m); |
15ac2a62 YW |
922 | if (r == -EOPNOTSUPP) |
923 | log_debug_errno(r, "Could not enumerate QDiscs, ignoring: %m"); | |
924 | else if (r < 0) | |
828a81a9 YW |
925 | return log_error_errno(r, "Could not enumerate QDisc: %m"); |
926 | ||
927 | r = manager_enumerate_tclass(m); | |
15ac2a62 YW |
928 | if (r == -EOPNOTSUPP) |
929 | log_debug_errno(r, "Could not enumerate TClasses, ignoring: %m"); | |
930 | else if (r < 0) | |
828a81a9 YW |
931 | return log_error_errno(r, "Could not enumerate TClass: %m"); |
932 | ||
446aaaf3 YW |
933 | r = manager_enumerate_addresses(m); |
934 | if (r < 0) | |
935 | return log_error_errno(r, "Could not enumerate addresses: %m"); | |
c16c7808 | 936 | |
446aaaf3 YW |
937 | r = manager_enumerate_neighbors(m); |
938 | if (r < 0) | |
939 | return log_error_errno(r, "Could not enumerate neighbors: %m"); | |
c16c7808 | 940 | |
f12629ae YW |
941 | /* NextHop support is added in kernel v5.3 (65ee00a9409f751188a8cdc0988167858eb4a536), |
942 | * and older kernels return -EOPNOTSUPP, or -EINVAL if SELinux is enabled. */ | |
0c0585ca | 943 | r = manager_enumerate_nexthop(m); |
f12629ae YW |
944 | if (r == -EOPNOTSUPP || (r == -EINVAL && mac_selinux_enforcing())) |
945 | log_debug_errno(r, "Could not enumerate nexthops, ignoring: %m"); | |
946 | else if (r < 0) | |
947 | return log_error_errno(r, "Could not enumerate nexthops: %m"); | |
0c0585ca | 948 | |
446aaaf3 YW |
949 | r = manager_enumerate_routes(m); |
950 | if (r < 0) | |
951 | return log_error_errno(r, "Could not enumerate routes: %m"); | |
c16c7808 | 952 | |
f12629ae | 953 | /* If kernel is built with CONFIG_FIB_RULES=n, it returns -EOPNOTSUPP. */ |
446aaaf3 | 954 | r = manager_enumerate_rules(m); |
f12629ae YW |
955 | if (r == -EOPNOTSUPP) |
956 | log_debug_errno(r, "Could not enumerate routing policy rules, ignoring: %m"); | |
957 | else if (r < 0) | |
446aaaf3 | 958 | return log_error_errno(r, "Could not enumerate routing policy rules: %m"); |
c16c7808 | 959 | |
edb69db2 YW |
960 | r = manager_enumerate_nl80211_wiphy(m); |
961 | if (r == -EOPNOTSUPP) | |
962 | log_debug_errno(r, "Could not enumerate wireless LAN phy, ignoring: %m"); | |
963 | else if (r < 0) | |
964 | return log_error_errno(r, "Could not enumerate wireless LAN phy: %m"); | |
965 | ||
96f5f9ef YW |
966 | r = manager_enumerate_nl80211_config(m); |
967 | if (r == -EOPNOTSUPP) | |
968 | log_debug_errno(r, "Could not enumerate wireless LAN interfaces, ignoring: %m"); | |
969 | else if (r < 0) | |
970 | return log_error_errno(r, "Could not enumerate wireless LAN interfaces: %m"); | |
971 | ||
972 | r = manager_enumerate_nl80211_mlme(m); | |
973 | if (r == -EOPNOTSUPP) | |
974 | log_debug_errno(r, "Could not enumerate wireless LAN stations, ignoring: %m"); | |
975 | else if (r < 0) | |
976 | return log_error_errno(r, "Could not enumerate wireless LAN stations: %m"); | |
977 | ||
446aaaf3 | 978 | return 0; |
c16c7808 SS |
979 | } |
980 | ||
59eb33e0 | 981 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
59eb33e0 | 982 | const sd_bus_error *e; |
f63e09ef | 983 | int r; |
59eb33e0 MP |
984 | |
985 | assert(m); | |
59eb33e0 MP |
986 | |
987 | e = sd_bus_message_get_error(m); | |
f63e09ef YW |
988 | if (e) { |
989 | r = sd_bus_error_get_errno(e); | |
990 | log_warning_errno(r, "Could not set hostname: %s", bus_error_message(e, r)); | |
991 | } | |
59eb33e0 MP |
992 | |
993 | return 1; | |
994 | } | |
995 | ||
996 | int manager_set_hostname(Manager *m, const char *hostname) { | |
997 | int r; | |
998 | ||
999 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
d7afd945 | 1000 | |
b3f9c17a YW |
1001 | r = free_and_strdup_warn(&m->dynamic_hostname, hostname); |
1002 | if (r < 0) | |
1003 | return r; | |
59eb33e0 | 1004 | |
5dbec9bd ZJS |
1005 | if (sd_bus_is_ready(m->bus) <= 0) { |
1006 | log_debug("Not connected to system bus, setting system hostname later."); | |
59eb33e0 MP |
1007 | return 0; |
1008 | } | |
1009 | ||
d2775306 | 1010 | r = bus_call_method_async( |
59eb33e0 MP |
1011 | m->bus, |
1012 | NULL, | |
d2775306 | 1013 | bus_hostname, |
59eb33e0 MP |
1014 | "SetHostname", |
1015 | set_hostname_handler, | |
1016 | m, | |
1017 | "sb", | |
1018 | hostname, | |
1019 | false); | |
59eb33e0 MP |
1020 | if (r < 0) |
1021 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
59eb33e0 | 1027 | const sd_bus_error *e; |
f63e09ef | 1028 | int r; |
59eb33e0 MP |
1029 | |
1030 | assert(m); | |
59eb33e0 MP |
1031 | |
1032 | e = sd_bus_message_get_error(m); | |
f63e09ef YW |
1033 | if (e) { |
1034 | r = sd_bus_error_get_errno(e); | |
1035 | log_warning_errno(r, "Could not set timezone: %s", bus_error_message(e, r)); | |
1036 | } | |
59eb33e0 MP |
1037 | |
1038 | return 1; | |
1039 | } | |
1040 | ||
1041 | int manager_set_timezone(Manager *m, const char *tz) { | |
1042 | int r; | |
1043 | ||
1044 | assert(m); | |
1045 | assert(tz); | |
1046 | ||
1047 | log_debug("Setting system timezone: '%s'", tz); | |
b3f9c17a YW |
1048 | r = free_and_strdup_warn(&m->dynamic_timezone, tz); |
1049 | if (r < 0) | |
1050 | return r; | |
59eb33e0 | 1051 | |
5dbec9bd ZJS |
1052 | if (sd_bus_is_ready(m->bus) <= 0) { |
1053 | log_debug("Not connected to system bus, setting system timezone later."); | |
59eb33e0 MP |
1054 | return 0; |
1055 | } | |
1056 | ||
b607fd3f | 1057 | r = bus_call_method_async( |
59eb33e0 MP |
1058 | m->bus, |
1059 | NULL, | |
b607fd3f | 1060 | bus_timedate, |
59eb33e0 MP |
1061 | "SetTimezone", |
1062 | set_timezone_handler, | |
1063 | m, | |
1064 | "sb", | |
1065 | tz, | |
1066 | false); | |
1067 | if (r < 0) | |
1068 | return log_error_errno(r, "Could not set timezone: %m"); | |
1069 | ||
1070 | return 0; | |
1071 | } |