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