]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
shared: split out polkit stuff from bus-util.c → bus-polkit.c
[thirdparty/systemd.git] / src / network / networkd-manager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
10 #include "sd-daemon.h"
11 #include "sd-netlink.h"
12
13 #include "alloc-util.h"
14 #include "bus-polkit.h"
15 #include "bus-util.h"
16 #include "conf-parser.h"
17 #include "def.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dns-domain.h"
21 #include "fd-util.h"
22 #include "fileio.h"
23 #include "local-addresses.h"
24 #include "netlink-util.h"
25 #include "network-internal.h"
26 #include "networkd-dhcp6.h"
27 #include "networkd-link-bus.h"
28 #include "networkd-manager-bus.h"
29 #include "networkd-manager.h"
30 #include "networkd-network-bus.h"
31 #include "networkd-speed-meter.h"
32 #include "ordered-set.h"
33 #include "path-util.h"
34 #include "set.h"
35 #include "signal-util.h"
36 #include "strv.h"
37 #include "sysctl-util.h"
38 #include "tmpfile-util.h"
39 #include "udev-util.h"
40 #include "virt.h"
41
42 /* use 128 MB for receive socket kernel queue. */
43 #define RCVBUF_SIZE (128*1024*1024)
44
45 static int log_message_warning_errno(sd_netlink_message *m, int err, const char *msg) {
46 const char *err_msg = NULL;
47
48 (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg);
49 return log_warning_errno(err, "%s: %s%s%m", msg, strempty(err_msg), err_msg ? " " : "");
50 }
51
52 static int setup_default_address_pool(Manager *m) {
53 AddressPool *p;
54 int r;
55
56 assert(m);
57
58 /* Add in the well-known private address ranges. */
59
60 r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8);
61 if (r < 0)
62 return r;
63
64 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
65 if (r < 0)
66 return r;
67
68 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
69 if (r < 0)
70 return r;
71
72 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
73 if (r < 0)
74 return r;
75
76 return 0;
77 }
78
79 static int manager_reset_all(Manager *m) {
80 Link *link;
81 Iterator i;
82 int r;
83
84 assert(m);
85
86 HASHMAP_FOREACH(link, m->links, i) {
87 r = link_carrier_reset(link);
88 if (r < 0)
89 log_link_warning_errno(link, r, "Could not reset carrier: %m");
90 }
91
92 return 0;
93 }
94
95 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
96 Manager *m = userdata;
97 int b, r;
98
99 assert(message);
100 assert(m);
101
102 r = sd_bus_message_read(message, "b", &b);
103 if (r < 0) {
104 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
105 return 0;
106 }
107
108 if (b)
109 return 0;
110
111 log_debug("Coming back from suspend, resetting all connections...");
112
113 (void) manager_reset_all(m);
114
115 return 0;
116 }
117
118 static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
119 Manager *m = userdata;
120
121 assert(message);
122 assert(m);
123
124 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
125 if (m->dynamic_hostname)
126 (void) manager_set_hostname(m, m->dynamic_hostname);
127 if (m->dynamic_timezone)
128 (void) manager_set_timezone(m, m->dynamic_timezone);
129 if (m->links_requesting_uuid)
130 (void) manager_request_product_uuid(m, NULL);
131
132 return 0;
133 }
134
135 int manager_connect_bus(Manager *m) {
136 int r;
137
138 assert(m);
139
140 if (m->bus)
141 return 0;
142
143 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network");
144 if (r < 0)
145 return log_error_errno(r, "Failed to connect to bus: %m");
146
147 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
148 if (r < 0)
149 return log_error_errno(r, "Failed to add manager object vtable: %m");
150
151 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
152 if (r < 0)
153 return log_error_errno(r, "Failed to add link object vtable: %m");
154
155 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
156 if (r < 0)
157 return log_error_errno(r, "Failed to add link enumerator: %m");
158
159 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
160 if (r < 0)
161 return log_error_errno(r, "Failed to add network object vtable: %m");
162
163 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
164 if (r < 0)
165 return log_error_errno(r, "Failed to add network enumerator: %m");
166
167 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL);
168 if (r < 0)
169 return log_error_errno(r, "Failed to request name: %m");
170
171 r = sd_bus_attach_event(m->bus, m->event, 0);
172 if (r < 0)
173 return log_error_errno(r, "Failed to attach bus to event loop: %m");
174
175 r = sd_bus_match_signal_async(
176 m->bus,
177 NULL,
178 "org.freedesktop.DBus.Local",
179 NULL,
180 "org.freedesktop.DBus.Local",
181 "Connected",
182 on_connected, NULL, m);
183 if (r < 0)
184 return log_error_errno(r, "Failed to request match on Connected signal: %m");
185
186 r = sd_bus_match_signal_async(
187 m->bus,
188 NULL,
189 "org.freedesktop.login1",
190 "/org/freedesktop/login1",
191 "org.freedesktop.login1.Manager",
192 "PrepareForSleep",
193 match_prepare_for_sleep, NULL, m);
194 if (r < 0)
195 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
196
197 return 0;
198 }
199
200 static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
201 Manager *m = userdata;
202 DeviceAction action;
203 Link *link = NULL;
204 int r, ifindex;
205
206 assert(m);
207 assert(device);
208
209 r = device_get_action(device, &action);
210 if (r < 0) {
211 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
212 return 0;
213 }
214
215 if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_CHANGE, DEVICE_ACTION_MOVE)) {
216 log_device_debug(device, "Ignoring udev %s event for device.", device_action_to_string(action));
217 return 0;
218 }
219
220 r = sd_device_get_ifindex(device, &ifindex);
221 if (r < 0) {
222 log_device_debug_errno(device, r, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m");
223 return 0;
224 }
225
226 r = device_is_renaming(device);
227 if (r < 0) {
228 log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
229 device_action_to_string(action));
230 return 0;
231 }
232 if (r > 0) {
233 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed: %m");
234 return 0;
235 }
236
237 r = link_get(m, ifindex, &link);
238 if (r < 0) {
239 if (r != -ENODEV)
240 log_debug_errno(r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
241 return 0;
242 }
243
244 (void) link_initialized(link, device);
245
246 return 0;
247 }
248
249 static int manager_connect_udev(Manager *m) {
250 int r;
251
252 /* udev does not initialize devices inside containers,
253 * so we rely on them being already initialized before
254 * entering the container */
255 if (detect_container() > 0)
256 return 0;
257
258 r = sd_device_monitor_new(&m->device_monitor);
259 if (r < 0)
260 return log_error_errno(r, "Failed to initialize device monitor: %m");
261
262 r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL);
263 if (r < 0)
264 return log_error_errno(r, "Could not add device monitor filter: %m");
265
266 r = sd_device_monitor_attach_event(m->device_monitor, m->event);
267 if (r < 0)
268 return log_error_errno(r, "Failed to attach event to device monitor: %m");
269
270 r = sd_device_monitor_start(m->device_monitor, manager_udev_process_link, m);
271 if (r < 0)
272 return log_error_errno(r, "Failed to start device monitor: %m");
273
274 return 0;
275 }
276
277 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
278 _cleanup_(route_freep) Route *tmp = NULL;
279 Route *route = NULL;
280 Manager *m = userdata;
281 Link *link = NULL;
282 uint32_t ifindex;
283 uint16_t type;
284 unsigned char table;
285 int r;
286
287 assert(rtnl);
288 assert(message);
289 assert(m);
290
291 if (sd_netlink_message_is_error(message)) {
292 r = sd_netlink_message_get_errno(message);
293 if (r < 0)
294 log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring");
295
296 return 0;
297 }
298
299 r = sd_netlink_message_get_type(message, &type);
300 if (r < 0) {
301 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
302 return 0;
303 } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) {
304 log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type);
305 return 0;
306 }
307
308 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
309 if (r == -ENODATA) {
310 log_debug("rtnl: received route message without ifindex, ignoring");
311 return 0;
312 } else if (r < 0) {
313 log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m");
314 return 0;
315 } else if (ifindex <= 0) {
316 log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex);
317 return 0;
318 }
319
320 r = link_get(m, ifindex, &link);
321 if (r < 0 || !link) {
322 /* when enumerating we might be out of sync, but we will
323 * get the route again, so just ignore it */
324 if (!m->enumerating)
325 log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex);
326 return 0;
327 }
328
329 r = route_new(&tmp);
330 if (r < 0)
331 return log_oom();
332
333 r = sd_rtnl_message_route_get_family(message, &tmp->family);
334 if (r < 0) {
335 log_link_warning(link, "rtnl: received route message without family, ignoring");
336 return 0;
337 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
338 log_link_debug(link, "rtnl: received route message with invalid family '%i', ignoring", tmp->family);
339 return 0;
340 }
341
342 r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol);
343 if (r < 0) {
344 log_warning_errno(r, "rtnl: received route message without route protocol: %m");
345 return 0;
346 }
347
348 switch (tmp->family) {
349 case AF_INET:
350 r = sd_netlink_message_read_in_addr(message, RTA_DST, &tmp->dst.in);
351 if (r < 0 && r != -ENODATA) {
352 log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m");
353 return 0;
354 }
355
356 r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &tmp->gw.in);
357 if (r < 0 && r != -ENODATA) {
358 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
359 return 0;
360 }
361
362 r = sd_netlink_message_read_in_addr(message, RTA_SRC, &tmp->src.in);
363 if (r < 0 && r != -ENODATA) {
364 log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m");
365 return 0;
366 }
367
368 r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &tmp->prefsrc.in);
369 if (r < 0 && r != -ENODATA) {
370 log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m");
371 return 0;
372 }
373
374 break;
375
376 case AF_INET6:
377 r = sd_netlink_message_read_in6_addr(message, RTA_DST, &tmp->dst.in6);
378 if (r < 0 && r != -ENODATA) {
379 log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m");
380 return 0;
381 }
382
383 r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &tmp->gw.in6);
384 if (r < 0 && r != -ENODATA) {
385 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
386 return 0;
387 }
388
389 r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &tmp->src.in6);
390 if (r < 0 && r != -ENODATA) {
391 log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m");
392 return 0;
393 }
394
395 r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &tmp->prefsrc.in6);
396 if (r < 0 && r != -ENODATA) {
397 log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m");
398 return 0;
399 }
400
401 break;
402
403 default:
404 assert_not_reached("Received route message with unsupported address family");
405 return 0;
406 }
407
408 r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen);
409 if (r < 0) {
410 log_link_warning_errno(link, r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m");
411 return 0;
412 }
413
414 r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen);
415 if (r < 0) {
416 log_link_warning_errno(link, r, "rtnl: received route message with invalid source prefixlen, ignoring: %m");
417 return 0;
418 }
419
420 r = sd_rtnl_message_route_get_scope(message, &tmp->scope);
421 if (r < 0) {
422 log_link_warning_errno(link, r, "rtnl: received route message with invalid scope, ignoring: %m");
423 return 0;
424 }
425
426 r = sd_rtnl_message_route_get_tos(message, &tmp->tos);
427 if (r < 0) {
428 log_link_warning_errno(link, r, "rtnl: received route message with invalid tos, ignoring: %m");
429 return 0;
430 }
431
432 r = sd_rtnl_message_route_get_type(message, &tmp->type);
433 if (r < 0) {
434 log_link_warning_errno(link, r, "rtnl: received route message with invalid type, ignoring: %m");
435 return 0;
436 }
437
438 r = sd_rtnl_message_route_get_table(message, &table);
439 if (r < 0) {
440 log_link_warning_errno(link, r, "rtnl: received route message with invalid table, ignoring: %m");
441 return 0;
442 }
443 tmp->table = table;
444
445 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority);
446 if (r < 0 && r != -ENODATA) {
447 log_link_warning_errno(link, r, "rtnl: received route message with invalid priority, ignoring: %m");
448 return 0;
449 }
450
451 r = sd_netlink_message_enter_container(message, RTA_METRICS);
452 if (r < 0 && r != -ENODATA) {
453 log_link_error_errno(link, r, "rtnl: Could not enter RTA_METRICS container: %m");
454 return 0;
455 }
456 if (r >= 0) {
457 r = sd_netlink_message_read_u32(message, RTAX_INITCWND, &tmp->initcwnd);
458 if (r < 0 && r != -ENODATA) {
459 log_link_warning_errno(link, r, "rtnl: received route message with invalid initcwnd, ignoring: %m");
460 return 0;
461 }
462
463 r = sd_netlink_message_read_u32(message, RTAX_INITRWND, &tmp->initrwnd);
464 if (r < 0 && r != -ENODATA) {
465 log_link_warning_errno(link, r, "rtnl: received route message with invalid initrwnd, ignoring: %m");
466 return 0;
467 }
468
469 r = sd_netlink_message_exit_container(message);
470 if (r < 0) {
471 log_link_error_errno(link, r, "rtnl: Could not exit from RTA_METRICS container: %m");
472 return 0;
473 }
474 }
475
476 (void) route_get(link, tmp, &route);
477
478 if (DEBUG_LOGGING) {
479 _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL,
480 *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL;
481 char buf_scope[ROUTE_SCOPE_STR_MAX], buf_table[ROUTE_TABLE_STR_MAX],
482 buf_protocol[ROUTE_PROTOCOL_STR_MAX];
483
484 if (!in_addr_is_null(tmp->family, &tmp->dst)) {
485 (void) in_addr_to_string(tmp->family, &tmp->dst, &buf_dst);
486 (void) asprintf(&buf_dst_prefixlen, "/%u", tmp->dst_prefixlen);
487 }
488 if (!in_addr_is_null(tmp->family, &tmp->src))
489 (void) in_addr_to_string(tmp->family, &tmp->src, &buf_src);
490 if (!in_addr_is_null(tmp->family, &tmp->gw))
491 (void) in_addr_to_string(tmp->family, &tmp->gw, &buf_gw);
492 if (!in_addr_is_null(tmp->family, &tmp->prefsrc))
493 (void) in_addr_to_string(tmp->family, &tmp->prefsrc, &buf_prefsrc);
494
495 log_link_debug(link,
496 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
497 type == RTM_DELROUTE ? "Forgetting" : route ? "Received remembered" : "Remembering",
498 strna(buf_dst), strempty(buf_dst_prefixlen),
499 strna(buf_src), strna(buf_gw), strna(buf_prefsrc),
500 format_route_scope(tmp->scope, buf_scope, sizeof buf_scope),
501 format_route_table(tmp->table, buf_table, sizeof buf_table),
502 format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol),
503 strna(route_type_to_string(tmp->type)));
504 }
505
506 switch (type) {
507 case RTM_NEWROUTE:
508 if (!route) {
509 /* A route appeared that we did not request */
510 r = route_add_foreign(link, tmp, &route);
511 if (r < 0) {
512 log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m");
513 return 0;
514 }
515 }
516
517 break;
518
519 case RTM_DELROUTE:
520 route_free(route);
521 break;
522
523 default:
524 assert_not_reached("Received route message with invalid RTNL message type");
525 }
526
527 return 1;
528 }
529
530 static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message *message, union lladdr_union *lladdr, size_t *size, char **str) {
531 int r;
532
533 assert(message);
534 assert(lladdr);
535 assert(size);
536 assert(str);
537
538 *str = NULL;
539
540 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in6), &lladdr->ip.in6);
541 if (r >= 0) {
542 *size = sizeof(lladdr->ip.in6);
543 if (in_addr_to_string(AF_INET6, &lladdr->ip, str) < 0)
544 log_warning_errno(r, "Could not print lower address: %m");
545 return r;
546 }
547
548 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->mac), &lladdr->mac);
549 if (r >= 0) {
550 *size = sizeof(lladdr->mac);
551 *str = new(char, ETHER_ADDR_TO_STRING_MAX);
552 if (!*str) {
553 log_oom();
554 return r;
555 }
556 ether_addr_to_string(&lladdr->mac, *str);
557 return r;
558 }
559
560 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in), &lladdr->ip.in);
561 if (r >= 0) {
562 *size = sizeof(lladdr->ip.in);
563 if (in_addr_to_string(AF_INET, &lladdr->ip, str) < 0)
564 log_warning_errno(r, "Could not print lower address: %m");
565 return r;
566 }
567
568 return r;
569 }
570
571 int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
572 Manager *m = userdata;
573 Link *link = NULL;
574 Neighbor *neighbor = NULL;
575 int ifindex, family, r;
576 uint16_t type, state;
577 union in_addr_union in_addr = IN_ADDR_NULL;
578 _cleanup_free_ char *addr_str = NULL;
579 union lladdr_union lladdr;
580 size_t lladdr_size = 0;
581 _cleanup_free_ char *lladdr_str = NULL;
582
583 assert(rtnl);
584 assert(message);
585 assert(m);
586
587 if (sd_netlink_message_is_error(message)) {
588 r = sd_netlink_message_get_errno(message);
589 if (r < 0)
590 log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring");
591
592 return 0;
593 }
594
595 r = sd_netlink_message_get_type(message, &type);
596 if (r < 0) {
597 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
598 return 0;
599 } else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) {
600 log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type);
601 return 0;
602 }
603
604 r = sd_rtnl_message_neigh_get_state(message, &state);
605 if (r < 0) {
606 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid state, ignoring: %m");
607 return 0;
608 } else if (!FLAGS_SET(state, NUD_PERMANENT)) {
609 log_debug("rtnl: received non-static neighbor, ignoring.");
610 return 0;
611 }
612
613 r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex);
614 if (r < 0) {
615 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
616 return 0;
617 } else if (ifindex <= 0) {
618 log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex);
619 return 0;
620 }
621
622 r = link_get(m, ifindex, &link);
623 if (r < 0 || !link) {
624 /* when enumerating we might be out of sync, but we will get the neighbor again, so just
625 * ignore it */
626 if (!m->enumerating)
627 log_warning("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex);
628 return 0;
629 }
630
631 r = sd_rtnl_message_neigh_get_family(message, &family);
632 if (r < 0) {
633 log_link_warning(link, "rtnl: received neighbor message without family, ignoring.");
634 return 0;
635 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
636 log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", family);
637 return 0;
638 }
639
640 switch (family) {
641 case AF_INET:
642 r = sd_netlink_message_read_in_addr(message, NDA_DST, &in_addr.in);
643 if (r < 0) {
644 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
645 return 0;
646 }
647
648 break;
649
650 case AF_INET6:
651 r = sd_netlink_message_read_in6_addr(message, NDA_DST, &in_addr.in6);
652 if (r < 0) {
653 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
654 return 0;
655 }
656
657 break;
658
659 default:
660 assert_not_reached("Received unsupported address family");
661 }
662
663 if (in_addr_to_string(family, &in_addr, &addr_str) < 0)
664 log_link_warning_errno(link, r, "Could not print address: %m");
665
666 r = manager_rtnl_process_neighbor_lladdr(message, &lladdr, &lladdr_size, &lladdr_str);
667 if (r < 0) {
668 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid lladdr, ignoring: %m");
669 return 0;
670 }
671
672 (void) neighbor_get(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
673
674 switch (type) {
675 case RTM_NEWNEIGH:
676 if (neighbor)
677 log_link_debug(link, "Remembering neighbor: %s->%s",
678 strnull(addr_str), strnull(lladdr_str));
679 else {
680 /* A neighbor appeared that we did not request */
681 r = neighbor_add_foreign(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
682 if (r < 0) {
683 log_link_warning_errno(link, r, "Failed to remember foreign neighbor %s->%s, ignoring: %m",
684 strnull(addr_str), strnull(lladdr_str));
685 return 0;
686 } else
687 log_link_debug(link, "Remembering foreign neighbor: %s->%s",
688 strnull(addr_str), strnull(lladdr_str));
689 }
690
691 break;
692
693 case RTM_DELNEIGH:
694 if (neighbor) {
695 log_link_debug(link, "Forgetting neighbor: %s->%s",
696 strnull(addr_str), strnull(lladdr_str));
697 (void) neighbor_free(neighbor);
698 } else
699 log_link_debug(link, "Kernel removed a neighbor we don't remember: %s->%s, ignoring.",
700 strnull(addr_str), strnull(lladdr_str));
701
702 break;
703
704 default:
705 assert_not_reached("Received invalid RTNL message type");
706 }
707
708 return 1;
709 }
710
711 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
712 _cleanup_free_ char *buf = NULL;
713 Manager *m = userdata;
714 Link *link = NULL;
715 uint16_t type;
716 unsigned char flags, prefixlen, scope;
717 union in_addr_union in_addr = IN_ADDR_NULL;
718 struct ifa_cacheinfo cinfo;
719 Address *address = NULL;
720 char valid_buf[FORMAT_TIMESPAN_MAX];
721 const char *valid_str = NULL;
722 int ifindex, family, r;
723
724 assert(rtnl);
725 assert(message);
726 assert(m);
727
728 if (sd_netlink_message_is_error(message)) {
729 r = sd_netlink_message_get_errno(message);
730 if (r < 0)
731 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
732
733 return 0;
734 }
735
736 r = sd_netlink_message_get_type(message, &type);
737 if (r < 0) {
738 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
739 return 0;
740 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
741 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
742 return 0;
743 }
744
745 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
746 if (r < 0) {
747 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
748 return 0;
749 } else if (ifindex <= 0) {
750 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
751 return 0;
752 }
753
754 r = link_get(m, ifindex, &link);
755 if (r < 0 || !link) {
756 /* when enumerating we might be out of sync, but we will get the address again, so just
757 * ignore it */
758 if (!m->enumerating)
759 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
760 return 0;
761 }
762
763 r = sd_rtnl_message_addr_get_family(message, &family);
764 if (r < 0) {
765 log_link_warning(link, "rtnl: received address message without family, ignoring.");
766 return 0;
767 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
768 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", family);
769 return 0;
770 }
771
772 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
773 if (r < 0) {
774 log_link_warning_errno(link, r, "rtnl: received address message with invalid prefixlen, ignoring: %m");
775 return 0;
776 }
777
778 r = sd_rtnl_message_addr_get_scope(message, &scope);
779 if (r < 0) {
780 log_link_warning_errno(link, r, "rtnl: received address message with invalid scope, ignoring: %m");
781 return 0;
782 }
783
784 r = sd_rtnl_message_addr_get_flags(message, &flags);
785 if (r < 0) {
786 log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m");
787 return 0;
788 }
789
790 switch (family) {
791 case AF_INET:
792 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
793 if (r < 0) {
794 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
795 return 0;
796 }
797
798 break;
799
800 case AF_INET6:
801 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
802 if (r < 0) {
803 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
804 return 0;
805 }
806
807 break;
808
809 default:
810 assert_not_reached("Received unsupported address family");
811 }
812
813 r = in_addr_to_string(family, &in_addr, &buf);
814 if (r < 0)
815 log_link_warning_errno(link, r, "Could not print address: %m");
816
817 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
818 if (r < 0 && r != -ENODATA) {
819 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
820 return 0;
821 } else if (r >= 0 && cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
822 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
823 cinfo.ifa_valid * USEC_PER_SEC,
824 USEC_PER_SEC);
825
826 (void) address_get(link, family, &in_addr, prefixlen, &address);
827
828 switch (type) {
829 case RTM_NEWADDR:
830 if (address)
831 log_link_debug(link, "Remembering updated address: %s/%u (valid %s%s)",
832 strnull(buf), prefixlen,
833 valid_str ? "for " : "forever", strempty(valid_str));
834 else {
835 /* An address appeared that we did not request */
836 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
837 if (r < 0) {
838 log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m",
839 strnull(buf), prefixlen);
840 return 0;
841 } else
842 log_link_debug(link, "Remembering foreign address: %s/%u (valid %s%s)",
843 strnull(buf), prefixlen,
844 valid_str ? "for " : "forever", strempty(valid_str));
845 }
846
847 /* address_update() logs internally, so we don't need to. */
848 (void) address_update(address, flags, scope, &cinfo);
849
850 break;
851
852 case RTM_DELADDR:
853 if (address) {
854 log_link_debug(link, "Forgetting address: %s/%u (valid %s%s)",
855 strnull(buf), prefixlen,
856 valid_str ? "for " : "forever", strempty(valid_str));
857 (void) address_drop(address);
858 } else
859 log_link_debug(link, "Kernel removed an address we don't remember: %s/%u (valid %s%s), ignoring.",
860 strnull(buf), prefixlen,
861 valid_str ? "for " : "forever", strempty(valid_str));
862
863 break;
864
865 default:
866 assert_not_reached("Received invalid RTNL message type");
867 }
868
869 return 1;
870 }
871
872 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
873 Manager *m = userdata;
874 Link *link = NULL;
875 NetDev *netdev = NULL;
876 uint16_t type;
877 const char *name;
878 int r, ifindex;
879
880 assert(rtnl);
881 assert(message);
882 assert(m);
883
884 if (sd_netlink_message_is_error(message)) {
885 r = sd_netlink_message_get_errno(message);
886 if (r < 0)
887 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
888
889 return 0;
890 }
891
892 r = sd_netlink_message_get_type(message, &type);
893 if (r < 0) {
894 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
895 return 0;
896 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
897 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
898 return 0;
899 }
900
901 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
902 if (r < 0) {
903 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
904 return 0;
905 } else if (ifindex <= 0) {
906 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
907 return 0;
908 }
909
910 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
911 if (r < 0) {
912 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
913 return 0;
914 }
915
916 (void) link_get(m, ifindex, &link);
917 (void) netdev_get(m, name, &netdev);
918
919 switch (type) {
920 case RTM_NEWLINK:
921 if (!link) {
922 /* link is new, so add it */
923 r = link_add(m, message, &link);
924 if (r < 0) {
925 log_warning_errno(r, "Could not process new link message, ignoring: %m");
926 return 0;
927 }
928 }
929
930 if (netdev) {
931 /* netdev exists, so make sure the ifindex matches */
932 r = netdev_set_ifindex(netdev, message);
933 if (r < 0) {
934 log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m");
935 return 0;
936 }
937 }
938
939 r = link_update(link, message);
940 if (r < 0) {
941 log_warning_errno(r, "Could not process link message, ignoring: %m");
942 return 0;
943 }
944
945 break;
946
947 case RTM_DELLINK:
948 link_drop(link);
949 netdev_drop(netdev);
950
951 break;
952
953 default:
954 assert_not_reached("Received link message with invalid RTNL message type.");
955 }
956
957 return 1;
958 }
959
960 int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
961 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *tmp = NULL;
962 _cleanup_free_ char *from = NULL, *to = NULL;
963 RoutingPolicyRule *rule = NULL;
964 const char *iif = NULL, *oif = NULL;
965 Manager *m = userdata;
966 unsigned flags;
967 uint16_t type;
968 int r;
969
970 assert(rtnl);
971 assert(message);
972 assert(m);
973
974 if (sd_netlink_message_is_error(message)) {
975 r = sd_netlink_message_get_errno(message);
976 if (r < 0)
977 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
978
979 return 0;
980 }
981
982 r = sd_netlink_message_get_type(message, &type);
983 if (r < 0) {
984 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
985 return 0;
986 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
987 log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type);
988 return 0;
989 }
990
991 r = routing_policy_rule_new(&tmp);
992 if (r < 0) {
993 log_oom();
994 return 0;
995 }
996
997 r = sd_rtnl_message_get_family(message, &tmp->family);
998 if (r < 0) {
999 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
1000 return 0;
1001 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1002 log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family);
1003 return 0;
1004 }
1005
1006 switch (tmp->family) {
1007 case AF_INET:
1008 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in);
1009 if (r < 0 && r != -ENODATA) {
1010 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1011 return 0;
1012 } else if (r >= 0) {
1013 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1014 if (r < 0) {
1015 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1016 return 0;
1017 }
1018 }
1019
1020 r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in);
1021 if (r < 0 && r != -ENODATA) {
1022 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1023 return 0;
1024 } else if (r >= 0) {
1025 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1026 if (r < 0) {
1027 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1028 return 0;
1029 }
1030 }
1031
1032 break;
1033
1034 case AF_INET6:
1035 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6);
1036 if (r < 0 && r != -ENODATA) {
1037 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1038 return 0;
1039 } else if (r >= 0) {
1040 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1041 if (r < 0) {
1042 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1043 return 0;
1044 }
1045 }
1046
1047 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6);
1048 if (r < 0 && r != -ENODATA) {
1049 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1050 return 0;
1051 } else if (r >= 0) {
1052 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1053 if (r < 0) {
1054 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1055 return 0;
1056 }
1057 }
1058
1059 break;
1060
1061 default:
1062 assert_not_reached("Received rule message with unsupported address family");
1063 }
1064
1065 if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0)
1066 return 0;
1067
1068 r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags);
1069 if (r < 0) {
1070 log_warning_errno(r, "rtnl: received rule message without valid flag, ignoring: %m");
1071 return 0;
1072 }
1073 tmp->invert_rule = flags & FIB_RULE_INVERT;
1074
1075 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
1076 if (r < 0 && r != -ENODATA) {
1077 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
1078 return 0;
1079 }
1080
1081 r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
1082 if (r < 0 && r != -ENODATA) {
1083 log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
1084 return 0;
1085 }
1086
1087 r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
1088 if (r < 0 && r != -ENODATA) {
1089 log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
1090 return 0;
1091 }
1092
1093 r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
1094 if (r < 0 && r != -ENODATA) {
1095 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
1096 return 0;
1097 }
1098
1099 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
1100 if (r < 0 && r != -ENODATA) {
1101 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
1102 return 0;
1103 }
1104
1105 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
1106 if (r < 0 && r != -ENODATA) {
1107 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
1108 return 0;
1109 }
1110 r = free_and_strdup(&tmp->iif, iif);
1111 if (r < 0)
1112 return log_oom();
1113
1114 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
1115 if (r < 0 && r != -ENODATA) {
1116 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
1117 return 0;
1118 }
1119 r = free_and_strdup(&tmp->oif, oif);
1120 if (r < 0)
1121 return log_oom();
1122
1123 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol);
1124 if (r < 0 && r != -ENODATA) {
1125 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
1126 return 0;
1127 }
1128
1129 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
1130 if (r < 0 && r != -ENODATA) {
1131 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
1132 return 0;
1133 }
1134
1135 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
1136 if (r < 0 && r != -ENODATA) {
1137 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
1138 return 0;
1139 }
1140
1141 (void) routing_policy_rule_get(m, tmp, &rule);
1142
1143 if (DEBUG_LOGGING) {
1144 (void) in_addr_to_string(tmp->family, &tmp->from, &from);
1145 (void) in_addr_to_string(tmp->family, &tmp->to, &to);
1146 }
1147
1148 switch (type) {
1149 case RTM_NEWRULE:
1150 if (!rule) {
1151 log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1152 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1153 r = routing_policy_rule_add_foreign(m, tmp, &rule);
1154 if (r < 0) {
1155 log_warning_errno(r, "Could not remember foreign rule, ignoring: %m");
1156 return 0;
1157 }
1158 }
1159 break;
1160 case RTM_DELRULE:
1161 log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1162 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1163 routing_policy_rule_free(rule);
1164
1165 break;
1166
1167 default:
1168 assert_not_reached("Received invalid RTNL message type");
1169 }
1170
1171 return 1;
1172 }
1173
1174 int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
1175 _cleanup_(nexthop_freep) NextHop *tmp = NULL;
1176 _cleanup_free_ char *gateway = NULL;
1177 NextHop *nexthop = NULL;
1178 Manager *m = userdata;
1179 Link *link = NULL;
1180 uint16_t type;
1181 int r;
1182
1183 assert(rtnl);
1184 assert(message);
1185 assert(m);
1186
1187 if (sd_netlink_message_is_error(message)) {
1188 r = sd_netlink_message_get_errno(message);
1189 if (r < 0)
1190 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
1191
1192 return 0;
1193 }
1194
1195 r = sd_netlink_message_get_type(message, &type);
1196 if (r < 0) {
1197 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1198 return 0;
1199 } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) {
1200 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type);
1201 return 0;
1202 }
1203
1204 r = nexthop_new(&tmp);
1205 if (r < 0)
1206 return log_oom();
1207
1208 r = sd_rtnl_message_get_family(message, &tmp->family);
1209 if (r < 0) {
1210 log_warning_errno(r, "rtnl: could not get nexthop family, ignoring: %m");
1211 return 0;
1212 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1213 log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp->family);
1214 return 0;
1215 }
1216
1217 switch (tmp->family) {
1218 case AF_INET:
1219 r = sd_netlink_message_read_in_addr(message, NHA_GATEWAY, &tmp->gw.in);
1220 if (r < 0 && r != -ENODATA) {
1221 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1222 return 0;
1223 }
1224 break;
1225
1226 case AF_INET6:
1227 r = sd_netlink_message_read_in6_addr(message, NHA_GATEWAY, &tmp->gw.in6);
1228 if (r < 0 && r != -ENODATA) {
1229 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1230 return 0;
1231 }
1232 break;
1233
1234 default:
1235 assert_not_reached("Received rule message with unsupported address family");
1236 }
1237
1238 r = sd_netlink_message_read_u32(message, NHA_ID, &tmp->id);
1239 if (r < 0 && r != -ENODATA) {
1240 log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1241 return 0;
1242 }
1243
1244 r = sd_netlink_message_read_u32(message, NHA_OIF, &tmp->oif);
1245 if (r < 0 && r != -ENODATA) {
1246 log_warning_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1247 return 0;
1248 }
1249
1250 r = link_get(m, tmp->oif, &link);
1251 if (r < 0 || !link) {
1252 if (!m->enumerating)
1253 log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp->oif);
1254 return 0;
1255 }
1256
1257 (void) nexthop_get(link, tmp, &nexthop);
1258
1259 if (DEBUG_LOGGING)
1260 (void) in_addr_to_string(tmp->family, &tmp->gw, &gateway);
1261
1262 switch (type) {
1263 case RTM_NEWNEXTHOP:
1264 if (!nexthop) {
1265 log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1266 r = nexthop_add_foreign(link, tmp, &nexthop);
1267 if (r < 0) {
1268 log_warning_errno(r, "Could not remember foreign nexthop, ignoring: %m");
1269 return 0;
1270 }
1271 }
1272 break;
1273 case RTM_DELNEXTHOP:
1274 log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1275 nexthop_free(nexthop);
1276
1277 break;
1278
1279 default:
1280 assert_not_reached("Received invalid RTNL message type");
1281 }
1282
1283 return 1;
1284 }
1285
1286 static int systemd_netlink_fd(void) {
1287 int n, fd, rtnl_fd = -EINVAL;
1288
1289 n = sd_listen_fds(true);
1290 if (n <= 0)
1291 return -EINVAL;
1292
1293 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
1294 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1295 if (rtnl_fd >= 0)
1296 return -EINVAL;
1297
1298 rtnl_fd = fd;
1299 }
1300 }
1301
1302 return rtnl_fd;
1303 }
1304
1305 static int manager_connect_genl(Manager *m) {
1306 int r;
1307
1308 assert(m);
1309
1310 r = sd_genl_socket_open(&m->genl);
1311 if (r < 0)
1312 return r;
1313
1314 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
1315 if (r < 0)
1316 return r;
1317
1318 r = sd_netlink_attach_event(m->genl, m->event, 0);
1319 if (r < 0)
1320 return r;
1321
1322 return 0;
1323 }
1324
1325 static int manager_connect_rtnl(Manager *m) {
1326 int fd, r;
1327
1328 assert(m);
1329
1330 fd = systemd_netlink_fd();
1331 if (fd < 0)
1332 r = sd_netlink_open(&m->rtnl);
1333 else
1334 r = sd_netlink_open_fd(&m->rtnl, fd);
1335 if (r < 0)
1336 return r;
1337
1338 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
1339 if (r < 0)
1340 return r;
1341
1342 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
1343 if (r < 0)
1344 return r;
1345
1346 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1347 if (r < 0)
1348 return r;
1349
1350 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1351 if (r < 0)
1352 return r;
1353
1354 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1355 if (r < 0)
1356 return r;
1357
1358 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1359 if (r < 0)
1360 return r;
1361
1362 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1363 if (r < 0)
1364 return r;
1365
1366 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1367 if (r < 0)
1368 return r;
1369
1370 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1371 if (r < 0)
1372 return r;
1373
1374 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1375 if (r < 0)
1376 return r;
1377
1378 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1379 if (r < 0)
1380 return r;
1381
1382 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1383 if (r < 0)
1384 return r;
1385
1386 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1387 if (r < 0)
1388 return r;
1389
1390 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1391 if (r < 0)
1392 return r;
1393
1394 return 0;
1395 }
1396
1397 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
1398 char *p;
1399 int r;
1400
1401 assert(s);
1402 assert(address);
1403
1404 r = in_addr_to_string(address->family, &address->address, &p);
1405 if (r < 0)
1406 return r;
1407
1408 r = ordered_set_consume(s, p);
1409 if (r == -EEXIST)
1410 return 0;
1411
1412 return r;
1413 }
1414
1415 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
1416 int r, c = 0;
1417 unsigned i;
1418
1419 assert(s);
1420 assert(addresses || n == 0);
1421
1422 for (i = 0; i < n; i++) {
1423 r = ordered_set_put_in_addr_data(s, addresses+i);
1424 if (r < 0)
1425 return r;
1426
1427 c += r;
1428 }
1429
1430 return c;
1431 }
1432
1433 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1434 char *p;
1435 int r;
1436
1437 assert(s);
1438 assert(address);
1439
1440 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1441 if (r < 0)
1442 return r;
1443
1444 r = ordered_set_consume(s, p);
1445 if (r == -EEXIST)
1446 return 0;
1447
1448 return r;
1449 }
1450
1451 static int ordered_set_put_in4_addrv(OrderedSet *s,
1452 const struct in_addr *addresses,
1453 size_t n,
1454 bool (*predicate)(const struct in_addr *addr)) {
1455 int r, c = 0;
1456 size_t i;
1457
1458 assert(s);
1459 assert(n == 0 || addresses);
1460
1461 for (i = 0; i < n; i++) {
1462 if (predicate && !predicate(&addresses[i]))
1463 continue;
1464 r = ordered_set_put_in4_addr(s, addresses+i);
1465 if (r < 0)
1466 return r;
1467
1468 c += r;
1469 }
1470
1471 return c;
1472 }
1473
1474 static int manager_save(Manager *m) {
1475 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *sip = NULL, *search_domains = NULL, *route_domains = NULL;
1476 const char *operstate_str, *carrier_state_str, *address_state_str;
1477 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1478 LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF;
1479 LinkAddressState address_state = LINK_ADDRESS_STATE_OFF;
1480 _cleanup_free_ char *temp_path = NULL;
1481 _cleanup_strv_free_ char **p = NULL;
1482 _cleanup_fclose_ FILE *f = NULL;
1483 Link *link;
1484 Iterator i;
1485 int r;
1486
1487 assert(m);
1488 assert(m->state_file);
1489
1490 /* We add all NTP and DNS server to a set, to filter out duplicates */
1491 dns = ordered_set_new(&string_hash_ops);
1492 if (!dns)
1493 return -ENOMEM;
1494
1495 ntp = ordered_set_new(&string_hash_ops);
1496 if (!ntp)
1497 return -ENOMEM;
1498
1499 sip = ordered_set_new(&string_hash_ops);
1500 if (!sip)
1501 return -ENOMEM;
1502
1503 search_domains = ordered_set_new(&dns_name_hash_ops);
1504 if (!search_domains)
1505 return -ENOMEM;
1506
1507 route_domains = ordered_set_new(&dns_name_hash_ops);
1508 if (!route_domains)
1509 return -ENOMEM;
1510
1511 HASHMAP_FOREACH(link, m->links, i) {
1512 if (link->flags & IFF_LOOPBACK)
1513 continue;
1514
1515 if (link->operstate > operstate)
1516 operstate = link->operstate;
1517
1518 if (link->carrier_state > carrier_state)
1519 carrier_state = link->carrier_state;
1520
1521 if (link->address_state > address_state)
1522 address_state = link->address_state;
1523
1524 if (!link->network)
1525 continue;
1526
1527 /* First add the static configured entries */
1528 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
1529 if (r < 0)
1530 return r;
1531
1532 r = ordered_set_put_strdupv(ntp, link->ntp ?: link->network->ntp);
1533 if (r < 0)
1534 return r;
1535
1536 r = ordered_set_put_string_set(search_domains, link->search_domains ?: link->network->search_domains);
1537 if (r < 0)
1538 return r;
1539
1540 r = ordered_set_put_string_set(route_domains, link->route_domains ?: link->network->route_domains);
1541 if (r < 0)
1542 return r;
1543
1544 if (!link->dhcp_lease)
1545 continue;
1546
1547 /* Secondly, add the entries acquired via DHCP */
1548 if (link->network->dhcp_use_dns) {
1549 const struct in_addr *addresses;
1550
1551 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1552 if (r > 0) {
1553 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1554 if (r < 0)
1555 return r;
1556 } else if (r < 0 && r != -ENODATA)
1557 return r;
1558 }
1559
1560 if (link->network->dhcp_use_ntp) {
1561 const struct in_addr *addresses;
1562
1563 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1564 if (r > 0) {
1565 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1566 if (r < 0)
1567 return r;
1568 } else if (r < 0 && r != -ENODATA)
1569 return r;
1570 }
1571
1572 if (link->network->dhcp_use_sip) {
1573 const struct in_addr *addresses;
1574
1575 r = sd_dhcp_lease_get_sip(link->dhcp_lease, &addresses);
1576 if (r > 0) {
1577 r = ordered_set_put_in4_addrv(sip, addresses, r, in4_addr_is_non_local);
1578 if (r < 0)
1579 return r;
1580 } else if (r < 0 && r != -ENODATA)
1581 return r;
1582 }
1583
1584 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1585 const char *domainname;
1586 char **domains = NULL;
1587
1588 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1589 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1590 if (r >= 0) {
1591 r = ordered_set_put_strdup(target_domains, domainname);
1592 if (r < 0)
1593 return r;
1594 } else if (r != -ENODATA)
1595 return r;
1596
1597 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1598 if (r >= 0) {
1599 r = ordered_set_put_strdupv(target_domains, domains);
1600 if (r < 0)
1601 return r;
1602 } else if (r != -ENODATA)
1603 return r;
1604 }
1605 }
1606
1607 if (carrier_state >= LINK_CARRIER_STATE_ENSLAVED)
1608 carrier_state = LINK_CARRIER_STATE_CARRIER;
1609
1610 operstate_str = link_operstate_to_string(operstate);
1611 assert(operstate_str);
1612
1613 carrier_state_str = link_carrier_state_to_string(carrier_state);
1614 assert(carrier_state_str);
1615
1616 address_state_str = link_address_state_to_string(address_state);
1617 assert(address_state_str);
1618
1619 r = fopen_temporary(m->state_file, &f, &temp_path);
1620 if (r < 0)
1621 return r;
1622
1623 (void) fchmod(fileno(f), 0644);
1624
1625 fprintf(f,
1626 "# This is private data. Do not parse.\n"
1627 "OPER_STATE=%s\n"
1628 "CARRIER_STATE=%s\n"
1629 "ADDRESS_STATE=%s\n",
1630 operstate_str, carrier_state_str, address_state_str);
1631
1632 ordered_set_print(f, "DNS=", dns);
1633 ordered_set_print(f, "NTP=", ntp);
1634 ordered_set_print(f, "SIP=", sip);
1635 ordered_set_print(f, "DOMAINS=", search_domains);
1636 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1637
1638 r = routing_policy_serialize_rules(m->rules, f);
1639 if (r < 0)
1640 goto fail;
1641
1642 r = fflush_and_check(f);
1643 if (r < 0)
1644 goto fail;
1645
1646 if (rename(temp_path, m->state_file) < 0) {
1647 r = -errno;
1648 goto fail;
1649 }
1650
1651 if (m->operational_state != operstate) {
1652 m->operational_state = operstate;
1653 if (strv_extend(&p, "OperationalState") < 0)
1654 log_oom();
1655 }
1656
1657 if (m->carrier_state != carrier_state) {
1658 m->carrier_state = carrier_state;
1659 if (strv_extend(&p, "CarrierState") < 0)
1660 log_oom();
1661 }
1662
1663 if (m->address_state != address_state) {
1664 m->address_state = address_state;
1665 if (strv_extend(&p, "AddressState") < 0)
1666 log_oom();
1667 }
1668
1669 if (p) {
1670 r = manager_send_changed_strv(m, p);
1671 if (r < 0)
1672 log_error_errno(r, "Could not emit changed properties: %m");
1673 }
1674
1675 m->dirty = false;
1676
1677 return 0;
1678
1679 fail:
1680 (void) unlink(m->state_file);
1681 (void) unlink(temp_path);
1682
1683 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1684 }
1685
1686 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1687 Manager *m = userdata;
1688 Link *link;
1689 Iterator i;
1690
1691 assert(m);
1692
1693 if (m->dirty)
1694 manager_save(m);
1695
1696 SET_FOREACH(link, m->dirty_links, i)
1697 if (link_save(link) >= 0)
1698 link_clean(link);
1699
1700 return 1;
1701 }
1702
1703 static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1704 Manager *m = userdata;
1705
1706 assert(m);
1707 m->restarting = false;
1708
1709 log_debug("Terminate operation initiated.");
1710
1711 return sd_event_exit(sd_event_source_get_event(s), 0);
1712 }
1713
1714 static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1715 Manager *m = userdata;
1716
1717 assert(m);
1718 m->restarting = true;
1719
1720 log_debug("Restart operation initiated.");
1721
1722 return sd_event_exit(sd_event_source_get_event(s), 0);
1723 }
1724
1725 int manager_new(Manager **ret) {
1726 _cleanup_(manager_freep) Manager *m = NULL;
1727 int r;
1728
1729 m = new(Manager, 1);
1730 if (!m)
1731 return -ENOMEM;
1732
1733 *m = (Manager) {
1734 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
1735 };
1736
1737 m->state_file = strdup("/run/systemd/netif/state");
1738 if (!m->state_file)
1739 return -ENOMEM;
1740
1741 r = sd_event_default(&m->event);
1742 if (r < 0)
1743 return r;
1744
1745 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0);
1746
1747 (void) sd_event_set_watchdog(m->event, true);
1748 (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m);
1749 (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m);
1750 (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m);
1751
1752 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1753 if (r < 0)
1754 return r;
1755
1756 r = manager_connect_rtnl(m);
1757 if (r < 0)
1758 return r;
1759
1760 r = manager_connect_genl(m);
1761 if (r < 0)
1762 return r;
1763
1764 r = manager_connect_udev(m);
1765 if (r < 0)
1766 return r;
1767
1768 r = sd_resolve_default(&m->resolve);
1769 if (r < 0)
1770 return r;
1771
1772 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1773 if (r < 0)
1774 return r;
1775
1776 r = setup_default_address_pool(m);
1777 if (r < 0)
1778 return r;
1779
1780 m->duid.type = DUID_TYPE_EN;
1781
1782 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1783
1784 *ret = TAKE_PTR(m);
1785
1786 return 0;
1787 }
1788
1789 void manager_free(Manager *m) {
1790 struct in6_addr *a;
1791 AddressPool *pool;
1792 Link *link;
1793
1794 if (!m)
1795 return;
1796
1797 free(m->state_file);
1798
1799 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1800 (void) dhcp6_prefix_remove(m, a);
1801 m->dhcp6_prefixes = hashmap_free(m->dhcp6_prefixes);
1802
1803 while ((link = hashmap_steal_first(m->links))) {
1804 if (link->dhcp6_client)
1805 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1806
1807 (void) link_stop_clients(link, true);
1808
1809 link_unref(link);
1810 }
1811
1812 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1813 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1814 m->links = hashmap_free_with_destructor(m->links, link_unref);
1815
1816 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1817 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1818
1819 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1820
1821 while ((pool = m->address_pools))
1822 address_pool_free(pool);
1823
1824 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1825 * So, it is necessary to set NULL after the sets are freed. */
1826 m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free);
1827 m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
1828 set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
1829
1830 sd_netlink_unref(m->rtnl);
1831 sd_netlink_unref(m->genl);
1832 sd_resolve_unref(m->resolve);
1833
1834 sd_event_source_unref(m->speed_meter_event_source);
1835 sd_event_unref(m->event);
1836
1837 sd_device_monitor_unref(m->device_monitor);
1838
1839 bus_verify_polkit_async_registry_free(m->polkit_registry);
1840 sd_bus_flush_close_unref(m->bus);
1841
1842 free(m->dynamic_timezone);
1843 free(m->dynamic_hostname);
1844
1845 free(m);
1846 }
1847
1848 int manager_start(Manager *m) {
1849 Link *link;
1850 Iterator i;
1851 int r;
1852
1853 assert(m);
1854
1855 r = manager_start_speed_meter(m);
1856 if (r < 0)
1857 return log_error_errno(r, "Failed to initialize speed meter: %m");
1858
1859 /* The dirty handler will deal with future serialization, but the first one
1860 must be done explicitly. */
1861
1862 manager_save(m);
1863
1864 HASHMAP_FOREACH(link, m->links, i)
1865 link_save(link);
1866
1867 return 0;
1868 }
1869
1870 int manager_load_config(Manager *m) {
1871 int r;
1872
1873 /* update timestamp */
1874 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1875
1876 r = netdev_load(m, false);
1877 if (r < 0)
1878 return r;
1879
1880 r = network_load(m, &m->networks);
1881 if (r < 0)
1882 return r;
1883
1884 return 0;
1885 }
1886
1887 bool manager_should_reload(Manager *m) {
1888 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1889 }
1890
1891 int manager_rtnl_enumerate_links(Manager *m) {
1892 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1893 sd_netlink_message *link;
1894 int r;
1895
1896 assert(m);
1897 assert(m->rtnl);
1898
1899 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1900 if (r < 0)
1901 return r;
1902
1903 r = sd_netlink_message_request_dump(req, true);
1904 if (r < 0)
1905 return r;
1906
1907 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1908 if (r < 0)
1909 return r;
1910
1911 for (link = reply; link; link = sd_netlink_message_next(link)) {
1912 int k;
1913
1914 m->enumerating = true;
1915
1916 k = manager_rtnl_process_link(m->rtnl, link, m);
1917 if (k < 0)
1918 r = k;
1919
1920 m->enumerating = false;
1921 }
1922
1923 return r;
1924 }
1925
1926 int manager_rtnl_enumerate_addresses(Manager *m) {
1927 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1928 sd_netlink_message *addr;
1929 int r;
1930
1931 assert(m);
1932 assert(m->rtnl);
1933
1934 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1935 if (r < 0)
1936 return r;
1937
1938 r = sd_netlink_message_request_dump(req, true);
1939 if (r < 0)
1940 return r;
1941
1942 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1943 if (r < 0)
1944 return r;
1945
1946 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1947 int k;
1948
1949 m->enumerating = true;
1950
1951 k = manager_rtnl_process_address(m->rtnl, addr, m);
1952 if (k < 0)
1953 r = k;
1954
1955 m->enumerating = false;
1956 }
1957
1958 return r;
1959 }
1960
1961 int manager_rtnl_enumerate_neighbors(Manager *m) {
1962 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1963 sd_netlink_message *neigh;
1964 int r;
1965
1966 assert(m);
1967 assert(m->rtnl);
1968
1969 r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC);
1970 if (r < 0)
1971 return r;
1972
1973 r = sd_netlink_message_request_dump(req, true);
1974 if (r < 0)
1975 return r;
1976
1977 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1978 if (r < 0)
1979 return r;
1980
1981 for (neigh = reply; neigh; neigh = sd_netlink_message_next(neigh)) {
1982 int k;
1983
1984 m->enumerating = true;
1985
1986 k = manager_rtnl_process_neighbor(m->rtnl, neigh, m);
1987 if (k < 0)
1988 r = k;
1989
1990 m->enumerating = false;
1991 }
1992
1993 return r;
1994 }
1995
1996 int manager_rtnl_enumerate_routes(Manager *m) {
1997 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1998 sd_netlink_message *route;
1999 int r;
2000
2001 assert(m);
2002 assert(m->rtnl);
2003
2004 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
2005 if (r < 0)
2006 return r;
2007
2008 r = sd_netlink_message_request_dump(req, true);
2009 if (r < 0)
2010 return r;
2011
2012 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2013 if (r < 0)
2014 return r;
2015
2016 for (route = reply; route; route = sd_netlink_message_next(route)) {
2017 int k;
2018
2019 m->enumerating = true;
2020
2021 k = manager_rtnl_process_route(m->rtnl, route, m);
2022 if (k < 0)
2023 r = k;
2024
2025 m->enumerating = false;
2026 }
2027
2028 return r;
2029 }
2030
2031 int manager_rtnl_enumerate_rules(Manager *m) {
2032 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2033 sd_netlink_message *rule;
2034 int r;
2035
2036 assert(m);
2037 assert(m->rtnl);
2038
2039 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
2040 if (r < 0)
2041 return r;
2042
2043 r = sd_netlink_message_request_dump(req, true);
2044 if (r < 0)
2045 return r;
2046
2047 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2048 if (r < 0) {
2049 if (r == -EOPNOTSUPP) {
2050 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
2051 return 0;
2052 }
2053
2054 return r;
2055 }
2056
2057 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
2058 int k;
2059
2060 m->enumerating = true;
2061
2062 k = manager_rtnl_process_rule(m->rtnl, rule, m);
2063 if (k < 0)
2064 r = k;
2065
2066 m->enumerating = false;
2067 }
2068
2069 return r;
2070 }
2071
2072 int manager_rtnl_enumerate_nexthop(Manager *m) {
2073 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2074 sd_netlink_message *nexthop;
2075 int r;
2076
2077 assert(m);
2078 assert(m->rtnl);
2079
2080 r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0);
2081 if (r < 0)
2082 return r;
2083
2084 r = sd_netlink_message_request_dump(req, true);
2085 if (r < 0)
2086 return r;
2087
2088 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2089 if (r < 0) {
2090 if (r == -EOPNOTSUPP) {
2091 log_debug("Nexthop are not supported by the kernel. Ignoring.");
2092 return 0;
2093 }
2094
2095 return r;
2096 }
2097
2098 for (nexthop = reply; nexthop; nexthop = sd_netlink_message_next(nexthop)) {
2099 int k;
2100
2101 m->enumerating = true;
2102
2103 k = manager_rtnl_process_nexthop(m->rtnl, nexthop, m);
2104 if (k < 0)
2105 r = k;
2106
2107 m->enumerating = false;
2108 }
2109
2110 return r;
2111 }
2112
2113 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
2114 AddressPool *p;
2115 int r;
2116
2117 assert(m);
2118 assert(prefixlen > 0);
2119 assert(found);
2120
2121 LIST_FOREACH(address_pools, p, m->address_pools) {
2122 if (p->family != family)
2123 continue;
2124
2125 r = address_pool_acquire(p, prefixlen, found);
2126 if (r != 0)
2127 return r;
2128 }
2129
2130 return 0;
2131 }
2132
2133 Link* manager_find_uplink(Manager *m, Link *exclude) {
2134 _cleanup_free_ struct local_address *gateways = NULL;
2135 int n, i;
2136
2137 assert(m);
2138
2139 /* Looks for a suitable "uplink", via black magic: an
2140 * interface that is up and where the default route with the
2141 * highest priority points to. */
2142
2143 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
2144 if (n < 0) {
2145 log_warning_errno(n, "Failed to determine list of default gateways: %m");
2146 return NULL;
2147 }
2148
2149 for (i = 0; i < n; i++) {
2150 Link *link;
2151
2152 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
2153 if (!link) {
2154 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
2155 continue;
2156 }
2157
2158 if (link == exclude)
2159 continue;
2160
2161 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
2162 continue;
2163
2164 return link;
2165 }
2166
2167 return NULL;
2168 }
2169
2170 void manager_dirty(Manager *manager) {
2171 assert(manager);
2172
2173 /* the serialized state in /run is no longer up-to-date */
2174 manager->dirty = true;
2175 }
2176
2177 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2178 Manager *manager = userdata;
2179 const sd_bus_error *e;
2180
2181 assert(m);
2182 assert(manager);
2183
2184 e = sd_bus_message_get_error(m);
2185 if (e)
2186 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
2187
2188 return 1;
2189 }
2190
2191 int manager_set_hostname(Manager *m, const char *hostname) {
2192 int r;
2193
2194 log_debug("Setting transient hostname: '%s'", strna(hostname));
2195
2196 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
2197 return log_oom();
2198
2199 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2200 log_debug("Not connected to system bus, setting hostname later.");
2201 return 0;
2202 }
2203
2204 r = sd_bus_call_method_async(
2205 m->bus,
2206 NULL,
2207 "org.freedesktop.hostname1",
2208 "/org/freedesktop/hostname1",
2209 "org.freedesktop.hostname1",
2210 "SetHostname",
2211 set_hostname_handler,
2212 m,
2213 "sb",
2214 hostname,
2215 false);
2216
2217 if (r < 0)
2218 return log_error_errno(r, "Could not set transient hostname: %m");
2219
2220 return 0;
2221 }
2222
2223 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2224 Manager *manager = userdata;
2225 const sd_bus_error *e;
2226
2227 assert(m);
2228 assert(manager);
2229
2230 e = sd_bus_message_get_error(m);
2231 if (e)
2232 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
2233
2234 return 1;
2235 }
2236
2237 int manager_set_timezone(Manager *m, const char *tz) {
2238 int r;
2239
2240 assert(m);
2241 assert(tz);
2242
2243 log_debug("Setting system timezone: '%s'", tz);
2244 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
2245 return log_oom();
2246
2247 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2248 log_debug("Not connected to system bus, setting timezone later.");
2249 return 0;
2250 }
2251
2252 r = sd_bus_call_method_async(
2253 m->bus,
2254 NULL,
2255 "org.freedesktop.timedate1",
2256 "/org/freedesktop/timedate1",
2257 "org.freedesktop.timedate1",
2258 "SetTimezone",
2259 set_timezone_handler,
2260 m,
2261 "sb",
2262 tz,
2263 false);
2264 if (r < 0)
2265 return log_error_errno(r, "Could not set timezone: %m");
2266
2267 return 0;
2268 }
2269
2270 int manager_request_product_uuid(Manager *m, Link *link) {
2271 int r;
2272
2273 assert(m);
2274
2275 if (m->has_product_uuid)
2276 return 0;
2277
2278 log_debug("Requesting product UUID");
2279
2280 if (link) {
2281 DUID *duid;
2282
2283 assert_se(duid = link_get_duid(link));
2284
2285 r = set_ensure_allocated(&m->links_requesting_uuid, NULL);
2286 if (r < 0)
2287 return log_oom();
2288
2289 r = set_ensure_allocated(&m->duids_requesting_uuid, NULL);
2290 if (r < 0)
2291 return log_oom();
2292
2293 r = set_put(m->links_requesting_uuid, link);
2294 if (r < 0)
2295 return log_oom();
2296
2297 r = set_put(m->duids_requesting_uuid, duid);
2298 if (r < 0)
2299 return log_oom();
2300
2301 link_ref(link);
2302 }
2303
2304 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2305 log_debug("Not connected to system bus, requesting product UUID later.");
2306 return 0;
2307 }
2308
2309 r = sd_bus_call_method_async(
2310 m->bus,
2311 NULL,
2312 "org.freedesktop.hostname1",
2313 "/org/freedesktop/hostname1",
2314 "org.freedesktop.hostname1",
2315 "GetProductUUID",
2316 get_product_uuid_handler,
2317 m,
2318 "b",
2319 false);
2320 if (r < 0)
2321 return log_warning_errno(r, "Failed to get product UUID: %m");
2322
2323 return 0;
2324 }