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