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